L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
adc_if.c
Go to the documentation of this file.
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file adc_if.c
5 * @author MCD Application Team
6 * @brief Read status related to the chip (battery level, VREF, chip temperature)
7 ******************************************************************************
8 * @attention
9 *
10 * Copyright (c) 2026 STMicroelectronics.
11 * All rights reserved.
12 *
13 * This software is licensed under terms that can be found in the LICENSE file
14 * in the root directory of this software component.
15 * If no LICENSE file comes with this software, it is provided AS-IS.
16 *
17 ******************************************************************************
18 */
19/* USER CODE END Header */
20
21/* Includes ------------------------------------------------------------------*/
22#include "adc_if.h"
23#include "sys_app.h"
24
25/* USER CODE BEGIN Includes */
26
27/* USER CODE END Includes */
28
29/* External variables ---------------------------------------------------------*/
30/**
31 * @brief ADC handle
32 */
33extern ADC_HandleTypeDef hadc;
34/* USER CODE BEGIN EV */
35
36/* USER CODE END EV */
37
38/* Private typedef -----------------------------------------------------------*/
39/* USER CODE BEGIN PTD */
40
41/* USER CODE END PTD */
42
43/* Private define ------------------------------------------------------------*/
44#define TEMPSENSOR_TYP_CAL1_V (( int32_t) 760) /*!< Internal temperature sensor, parameter V30 (unit: mV). Refer to device datasheet for min/typ/max values. */
45#define TEMPSENSOR_TYP_AVGSLOPE (( int32_t) 2500) /*!< Internal temperature sensor, parameter Avg_Slope (unit: uV/DegCelsius). Refer to device datasheet for min/typ/max values. */
46
47/* USER CODE BEGIN PD */
48
49/* USER CODE END PD */
50
51/* Private macro -------------------------------------------------------------*/
52/* USER CODE BEGIN PM */
53
54/* USER CODE END PM */
55
56/* Private variables ---------------------------------------------------------*/
57
58/* USER CODE BEGIN PV */
59
60/* USER CODE END PV */
61
62/* Private function prototypes -----------------------------------------------*/
63/**
64 * @brief This function reads the ADC channel
65 * @param channel channel number to read
66 * @return adc measured level value
67 */
68static uint32_t ADC_ReadChannels(uint32_t channel);
69
70/* USER CODE BEGIN PFP */
71
72/* USER CODE END PFP */
73
74/* Exported functions --------------------------------------------------------*/
75/* USER CODE BEGIN EF */
76
77/* USER CODE END EF */
78
80{
81 /* USER CODE BEGIN SYS_InitMeasurement_1 */
82
83 /* USER CODE END SYS_InitMeasurement_1 */
84 hadc.Instance = ADC;
85 /* USER CODE BEGIN SYS_InitMeasurement_2 */
86
87 /* USER CODE END SYS_InitMeasurement_2 */
88}
89
91{
92 /* USER CODE BEGIN SYS_DeInitMeasurement_1 */
93
94 /* USER CODE END SYS_DeInitMeasurement_1 */
95}
96
98{
99 /* USER CODE BEGIN SYS_GetTemperatureLevel_1 */
100
101 /* USER CODE END SYS_GetTemperatureLevel_1 */
102 __IO int16_t temperatureDegreeC = 0;
103 uint32_t measuredLevel = 0;
104 uint16_t batteryLevelmV = SYS_GetBatteryLevel();
105
106 measuredLevel = ADC_ReadChannels(ADC_CHANNEL_TEMPSENSOR);
107
108 /* convert ADC level to temperature */
109 /* check whether device has temperature sensor calibrated in production */
110 if (((int32_t)*TEMPSENSOR_CAL2_ADDR - (int32_t)*TEMPSENSOR_CAL1_ADDR) != 0)
111 {
112 /* Device with temperature sensor calibrated in production:
113 use device optimized parameters */
114 temperatureDegreeC = __LL_ADC_CALC_TEMPERATURE(batteryLevelmV,
115 measuredLevel,
116 LL_ADC_RESOLUTION_12B);
117 }
118 else
119 {
120 /* Device with temperature sensor not calibrated in production:
121 use generic parameters */
122 temperatureDegreeC = __LL_ADC_CALC_TEMPERATURE_TYP_PARAMS(TEMPSENSOR_TYP_AVGSLOPE,
124 TEMPSENSOR_CAL1_TEMP,
125 batteryLevelmV,
126 measuredLevel,
127 LL_ADC_RESOLUTION_12B);
128 }
129
130 /* from int16 to q8.7*/
131 temperatureDegreeC <<= 8;
132
133 return (int16_t) temperatureDegreeC;
134 /* USER CODE BEGIN SYS_GetTemperatureLevel_2 */
135
136 /* USER CODE END SYS_GetTemperatureLevel_2 */
137}
138
140{
141 /* USER CODE BEGIN SYS_GetBatteryLevel_1 */
142
143 /* USER CODE END SYS_GetBatteryLevel_1 */
144 uint16_t batteryLevelmV = 0;
145 uint32_t measuredLevel = 0;
146
147 measuredLevel = ADC_ReadChannels(ADC_CHANNEL_VREFINT);
148
149 if (measuredLevel == 0)
150 {
151 batteryLevelmV = 0;
152 }
153 else
154 {
155 if ((uint32_t)*VREFINT_CAL_ADDR != (uint32_t)0xFFFFU)
156 {
157 /* Device with Reference voltage calibrated in production:
158 use device optimized parameters */
159 batteryLevelmV = __LL_ADC_CALC_VREFANALOG_VOLTAGE(measuredLevel,
160 ADC_RESOLUTION_12B);
161 }
162 else
163 {
164 /* Device with Reference voltage not calibrated in production:
165 use generic parameters */
166 batteryLevelmV = (VREFINT_CAL_VREF * 1510) / measuredLevel;
167 }
168 }
169
170 return batteryLevelmV;
171 /* USER CODE BEGIN SYS_GetBatteryLevel_2 */
172
173 /* USER CODE END SYS_GetBatteryLevel_2 */
174}
175
176/* Private Functions Definition -----------------------------------------------*/
177/* USER CODE BEGIN PrFD */
178
179/* USER CODE END PrFD */
180
181static uint32_t ADC_ReadChannels(uint32_t channel)
182{
183 /* USER CODE BEGIN ADC_ReadChannels_1 */
184
185 /* USER CODE END ADC_ReadChannels_1 */
186 uint32_t ADCxConvertedValues = 0;
187 ADC_ChannelConfTypeDef sConfig = {0};
188
189 MX_ADC_Init();
190
191 /* Start Calibration */
192 if (HAL_ADCEx_Calibration_Start(&hadc) != HAL_OK)
193 {
195 }
196
197 /* Configure Regular Channel */
198 sConfig.Channel = channel;
199 sConfig.Rank = ADC_REGULAR_RANK_1;
200 sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
201 if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
202 {
204 }
205
206 if (HAL_ADC_Start(&hadc) != HAL_OK)
207 {
208 /* Start Error */
210 }
211 /** Wait for end of conversion */
212 HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
213
214 /** Wait for end of conversion */
215 HAL_ADC_Stop(&hadc); /* it calls also ADC_Disable() */
216
217 ADCxConvertedValues = HAL_ADC_GetValue(&hadc);
218
219 HAL_ADC_DeInit(&hadc);
220
221 return ADCxConvertedValues;
222 /* USER CODE BEGIN ADC_ReadChannels_2 */
223
224 /* USER CODE END ADC_ReadChannels_2 */
225}
ADC_HandleTypeDef hadc
Definition adc.c:27
void MX_ADC_Init(void)
Initialise the ADC peripheral (hadc) with settings generated by STM32CubeMX. Configures the ADC clock...
Definition adc.c:30
void SYS_DeInitMeasurement(void)
DeInitializes the ADC.
Definition adc_if.c:90
uint16_t SYS_GetBatteryLevel(void)
Get the current battery level.
Definition adc_if.c:139
void SYS_InitMeasurement(void)
Initializes the ADC input.
Definition adc_if.c:79
static uint32_t ADC_ReadChannels(uint32_t channel)
This function reads the ADC channel.
Definition adc_if.c:181
#define TEMPSENSOR_TYP_CAL1_V
Definition adc_if.c:44
#define TEMPSENSOR_TYP_AVGSLOPE
Definition adc_if.c:45
int16_t SYS_GetTemperatureLevel(void)
Get the current temperature.
Definition adc_if.c:97
Header for ADC interface configuration.
void Error_Handler(void)
Default error handler called by HAL on unrecoverable errors. Disables interrupts and enters an infini...
Definition main.c:505
Function prototypes for sys_app.c file.