• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Talkweb Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "adc_temperature.h"
17 #include "los_arch_interrupt.h"
18 #include "los_interrupt.h"
19 
20 ADC_HandleTypeDef hadcx;
21 DMA_HandleTypeDef hdma_adcx;
22 
23 static uint16_t ADC_ConvertedValue;
24 
MX_ADCx_Init(void)25 void MX_ADCx_Init(void)
26 {
27     ADC_ChannelConfTypeDef sConfig;
28     hadcx.Instance = ADC1;
29     hadcx.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
30     hadcx.Init.Resolution = ADC_RESOLUTION_12B;
31     hadcx.Init.ScanConvMode = DISABLE;
32     hadcx.Init.ContinuousConvMode = ENABLE;
33     hadcx.Init.DiscontinuousConvMode = DISABLE;
34     hadcx.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
35     hadcx.Init.ExternalTrigConv = ADC_SOFTWARE_START;
36     hadcx.Init.DataAlign = ADC_DATAALIGN_RIGHT;
37     hadcx.Init.NbrOfConversion = 1;
38     hadcx.Init.DMAContinuousRequests = ENABLE;
39     hadcx.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
40 
41     HAL_ADC_Init(&hadcx);
42     sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
43     sConfig.Rank = 1;
44     sConfig.SamplingTime = ADC_SAMPLETIME_56CYCLES;
45     HAL_ADC_ConfigChannel(&hadcx, &sConfig);
46 }
47 
ADCx_DMA_IRQx_Handler(void)48 void ADCx_DMA_IRQx_Handler(void)
49 {
50     HAL_DMA_IRQHandler(&hdma_adcx);
51 }
52 
MX_DMA_Init(void)53 void MX_DMA_Init(void)
54 {
55     __HAL_RCC_DMA2_CLK_ENABLE();
56     LOS_HwiCreate(OS_SYS_VECTOR_CNT + DMA2_Stream0_IRQn, 0, 1, (HWI_PROC_FUNC)ADCx_DMA_IRQx_Handler, 0);
57 }
58 
HAL_ADC_MspInit(ADC_HandleTypeDef * hadc)59 void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
60 {
61     if (hadc->Instance == ADC1) {
62         __HAL_RCC_ADC1_CLK_ENABLE();
63         hdma_adcx.Instance = DMA2_Stream0;
64         hdma_adcx.Init.Channel = DMA_CHANNEL_0;
65         hdma_adcx.Init.Direction = DMA_PERIPH_TO_MEMORY;
66         hdma_adcx.Init.PeriphInc = DMA_PINC_DISABLE;
67         hdma_adcx.Init.MemInc = DMA_MINC_ENABLE;
68         hdma_adcx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
69         hdma_adcx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
70         hdma_adcx.Init.Mode = DMA_CIRCULAR;
71         hdma_adcx.Init.Priority = DMA_PRIORITY_HIGH;
72         hdma_adcx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
73         HAL_DMA_Init(&hdma_adcx);
74         __HAL_LINKDMA(hadc, DMA_Handle, hdma_adcx);
75     }
76 }
77 
HAL_ADC_MspDeInit(ADC_HandleTypeDef * hadc)78 void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
79 {
80     if (hadc->Instance == ADC1) {
81         __HAL_RCC_ADC1_CLK_DISABLE();
82         HAL_DMA_DeInit(hadc->DMA_Handle);
83     }
84 }
85 
Temperature_ADC_Init()86 void Temperature_ADC_Init()
87 {
88     MX_DMA_Init();
89     MX_ADCx_Init();
90     HAL_ADC_Start_DMA(&hadcx, (uint32_t *)&ADC_ConvertedValue, sizeof(ADC_ConvertedValue));
91 }
92 
Temperature_Get()93 double Temperature_Get()
94 {
95     double ADC_ConvertedValueLocal;
96     __IO double Current_Temperature;
97     ADC_ConvertedValueLocal = (double)(ADC_ConvertedValue & 0xFFF) * 3.3 / 4096;
98     Current_Temperature = (ADC_ConvertedValueLocal - 0.76) / 0.0025 + 25;
99     return Current_Temperature;
100 }