DIY Logging Volt/Ampmeter
adc.h File Reference

This file contains all the function prototypes for the adc.c file. More...

#include "main.h"

Go to the source code of this file.

Functions

void MX_ADC1_Init (void)
 
void MX_ADC2_Init (void)
 
void Activate_ADC (void)
 Perform ADC activation procedure to make it ready to convert (ADC instance: ADC2). More...
 
void startBattVoltMeasurement (void)
 
void AdcGrpRegularUnitaryConvComplete_Callback (void)
 ADC group regular end of unitary conversion interruption callback. More...
 

Detailed Description

This file contains all the function prototypes for the adc.c file.

Attention

© Copyright (c) 2021 STMicroelectronics. All rights reserved.

This software component is licensed by ST under BSD 3-Clause license, the "License"; You may not use this file except in compliance with the License. You may obtain a copy of the License at: opensource.org/licenses/BSD-3-Clause

Definition in file adc.h.

Function Documentation

◆ Activate_ADC()

void Activate_ADC ( void  )

Perform ADC activation procedure to make it ready to convert (ADC instance: ADC2).

Note
Operations:
  • ADC instance
    • Run ADC self calibration
    • Enable ADC
  • ADC group regular none: ADC conversion start-stop to be performed after this function
  • ADC group injected none: ADC conversion start-stop to be performed after this function
Parameters
None
Return values
None

Definition at line 197 of file adc.c.

198 {
199  /* Increase sampling time */
200  LL_ADC_SetChannelSamplingTime(ADC2, LL_ADC_CHANNEL_8, LL_ADC_SAMPLINGTIME_28CYCLES_5);
201 
202  /* Enable interruption ADC group regular end of unitary conversion */
203  LL_ADC_EnableIT_EOS(ADC2);
204 
205 
206  /* Logic discribe in the note now follows */
207  __IO uint32_t wait_loop_index = 0;
208 
209  uint32_t Timeout = 0; /* Variable used for timeout management */
210 
211  /*## Operation on ADC hierarchical scope: ADC instance #####################*/
212 
213  /* Note: Hardware constraint (refer to description of the functions */
214  /* below): */
215  /* On this STM32 serie, setting of these features are not */
216  /* conditioned to ADC state. */
217  /* However, in order to be compliant with other STM32 series */
218  /* and to show the best practice usages, ADC state is checked. */
219  /* Software can be optimized by removing some of these checks, if */
220  /* they are not relevant considering previous settings and actions */
221  /* in user application. */
222  if (LL_ADC_IsEnabled(ADC2) == 0)
223  {
224  /* Enable ADC */
225  LL_ADC_Enable(ADC2);
226 
227  /* Delay between ADC enable and ADC start of calibration. */
228  /* Note: Variable divided by 2 to compensate partially */
229  /* CPU processing cycles (depends on compilation optimization). */
230  wait_loop_index = (ADC_DELAY_ENABLE_CALIB_CPU_CYCLES >> 1);
231  while (wait_loop_index != 0) wait_loop_index--;
232 
233  /* Run ADC self calibration */
234  LL_ADC_StartCalibration(ADC2);
235 
236  /* Poll for ADC effectively calibrated */
238 
239  while (LL_ADC_IsCalibrationOnGoing(ADC2) != 0)
240  {
241  /* Check Systick counter flag to decrement the time-out value */
243  {
244  if (Timeout-- == 0) while (1); /* Time-out occurred */
245  }
246  }
247  }
248 
249  /*## Operation on ADC hierarchical scope: ADC group regular ################*/
250  /* Note: No operation on ADC group regular performed here. */
251  /* ADC group regular conversions to be performed after this function */
252  /* using function: */
253  /* "LL_ADC_REG_StartConversion();" */
254 
255  /*## Operation on ADC hierarchical scope: ADC group injected ###############*/
256  /* Note: No operation on ADC group injected performed here. */
257  /* ADC group injected conversions to be performed after this function */
258  /* using function: */
259  /* "LL_ADC_INJ_StartConversion();" */
260 
261 }

◆ AdcGrpRegularUnitaryConvComplete_Callback()

void AdcGrpRegularUnitaryConvComplete_Callback ( void  )

ADC group regular end of unitary conversion interruption callback.

Note
This function is executed when the ADC group regular sequencer has converted one rank of the sequence. Therefore, this function is executed as many times as number of ranks in the sequence.
Return values
None

Definition at line 313 of file adc.c.

314 {
315  /* Retrieve ADC conversion data */
316  /* (data maximum amplitude corresponds to ADC resolution: 12 bits) */
317  uhADCxConvertedData = LL_ADC_REG_ReadConversionData12(ADC2);
318 
319  /* Computation of ADC conversions raw data to physical values */
320  /* using LL ADC driver helper macro. */
321  uhADCxConvertedData_Voltage_mVolt = __LL_ADC_CALC_DATA_TO_VOLTAGE(VDDA_APPLI, uhADCxConvertedData, LL_ADC_RESOLUTION_12B);
322 
323  /* Update status variable of ADC unitary conversion */
325 
326  /* Calculate real voltage */
327  settings.vbat = ((uhADCxConvertedData_Voltage_mVolt * 0.001) * 3.2) / 2.2; /* Voltage divider of 1kohm and 2.2kohm */
328 
329  /* Read voltage tends to be 0.03V lower so add this value manually (~calibration) */
331 
332 // SEGGER_RTT_printf(0, "ADC = %u mV vbat = %u mV\n\r", uhADCxConvertedData_Voltage_mVolt, (uint16_t)(settings.vbat * 1000));
333 
334  /* Signal logic that the battery voltage needs to be converted */
335  settings.newVbat = 1;
336 }

◆ MX_ADC1_Init()

void MX_ADC1_Init ( void  )

Common config

Configure Regular Channel

Definition at line 105 of file adc.c.

106 {
107  LL_ADC_InitTypeDef ADC_InitStruct = {0};
108  LL_ADC_CommonInitTypeDef ADC_CommonInitStruct = {0};
109  LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
110 
111  /* Peripheral clock enable */
112  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1);
113 
114  /* ADC1 interrupt Init */
117 
118  /** Common config
119  */
120  ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
121  ADC_InitStruct.SequencersScanMode = LL_ADC_SEQ_SCAN_DISABLE;
122  LL_ADC_Init(ADC1, &ADC_InitStruct);
123  ADC_CommonInitStruct.Multimode = LL_ADC_MULTI_INDEPENDENT;
124  LL_ADC_CommonInit(__LL_ADC_COMMON_INSTANCE(ADC1), &ADC_CommonInitStruct);
125  ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
126  ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
127  ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
128  ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_SINGLE;
129  ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
130  LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);
131  /** Configure Regular Channel
132  */
133  LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_TEMPSENSOR);
134  LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_TEMPSENSOR, LL_ADC_SAMPLINGTIME_1CYCLE_5);
135  LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(ADC1), LL_ADC_PATH_INTERNAL_TEMPSENSOR);
136 }

◆ MX_ADC2_Init()

void MX_ADC2_Init ( void  )

ADC2 GPIO Configuration PB0 ---—> ADC2_IN8

Common config

Configure Regular Channel

Definition at line 140 of file adc.c.

141 {
142  LL_ADC_InitTypeDef ADC_InitStruct = {0};
143  LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
144 
145  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
146 
147  /* Peripheral clock enable */
148  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC2);
149 
150  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
151  /**ADC2 GPIO Configuration
152  PB0 ------> ADC2_IN8
153  */
154  GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
155  GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
156  LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
157 
158  /* ADC2 interrupt Init */
161 
162  /** Common config
163  */
164  ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
165  ADC_InitStruct.SequencersScanMode = LL_ADC_SEQ_SCAN_DISABLE;
166  LL_ADC_Init(ADC2, &ADC_InitStruct);
167  ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
168  ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
169  ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
170  ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_SINGLE;
171  ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
172  LL_ADC_REG_Init(ADC2, &ADC_REG_InitStruct);
173  /** Configure Regular Channel
174  */
175  LL_ADC_REG_SetSequencerRanks(ADC2, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_8);
176  LL_ADC_SetChannelSamplingTime(ADC2, LL_ADC_CHANNEL_8, LL_ADC_SAMPLINGTIME_1CYCLE_5);
177 }

◆ startBattVoltMeasurement()

void startBattVoltMeasurement ( void  )

Definition at line 264 of file adc.c.

265 {
266  /* Reset status variable of ADC group regular unitary conversion before */
267  /* performing a new ADC group regular conversion start. */
268  /* Note: Optionally, for this example purpose, check ADC unitary */
269  /* conversion status before starting another ADC conversion. */
270 
272  {
274  }
275  else
276  {
277  /* Error: Previous action (ADC conversion or DMA transfer) not yet */
278  /* completed. */
279  while (1);
280  }
281 
282  /* Init variable containing ADC conversion data */
284 
285  /* Start ADC group regular conversion */
286  /* Note: Hardware constraint (refer to description of the functions */
287  /* below): */
288  /* On this STM32 serie, setting of these features are not */
289  /* conditioned to ADC state. */
290  /* However, in order to be compliant with other STM32 series */
291  /* and to show the best practice usages, ADC state is checked. */
292  /* Software can be optimized by removing some of these checks, if */
293  /* they are not relevant considering previous settings and actions */
294  /* in user application. */
295  if (LL_ADC_IsEnabled(ADC2) == 1) LL_ADC_REG_StartConversionSWStart(ADC2);
296  else
297  {
298  /* Error: ADC conversion start could not be performed */
299  while(1);
300  }
301 
302 }
ADC_CALIBRATION_TIMEOUT_MS
#define ADC_CALIBRATION_TIMEOUT_MS
Definition: adc.c:59
ubAdcGrpRegularUnitaryConvStatus
__IO uint8_t ubAdcGrpRegularUnitaryConvStatus
Definition: adc.c:99
Timeout
static uint32_t Timeout
Definition: rtc.c:85
settings
struct Settings_t settings
NVIC_GetPriorityGrouping
#define NVIC_GetPriorityGrouping
Definition: core_armv8mbl.h:1189
uhADCxConvertedData_Voltage_mVolt
__IO uint16_t uhADCxConvertedData_Voltage_mVolt
Definition: adc.c:92
ADC_DELAY_ENABLE_CALIB_CPU_CYCLES
#define ADC_DELAY_ENABLE_CALIB_CPU_CYCLES
Definition: adc.c:70
uhADCxConvertedData
__IO uint16_t uhADCxConvertedData
Definition: adc.c:89
LL_SYSTICK_IsActiveCounterFlag
__STATIC_INLINE uint32_t LL_SYSTICK_IsActiveCounterFlag(void)
This function checks if the Systick counter flag is active or not.
Definition: stm32f1xx_ll_cortex.h:231
NVIC_EncodePriority
__STATIC_INLINE uint32_t NVIC_EncodePriority(uint32_t PriorityGroup, uint32_t PreemptPriority, uint32_t SubPriority)
Encode Priority.
Definition: core_armv8mbl.h:1496
VAR_CONVERTED_DATA_INIT_VALUE
#define VAR_CONVERTED_DATA_INIT_VALUE
Definition: adc.c:80
ADC1_2_IRQn
@ ADC1_2_IRQn
Definition: stm32f103xb.h:101
adcVoltReadoutCalbration
static const float adcVoltReadoutCalbration
Definition: adc.c:86
ADC1
#define ADC1
Definition: stm32f103xb.h:670
GPIOB
#define GPIOB
Definition: stm32f103xb.h:666
__IO
#define __IO
Definition: core_armv8mbl.h:196
NVIC_SetPriority
#define NVIC_SetPriority
Definition: core_armv8mbl.h:1197
NVIC_EnableIRQ
#define NVIC_EnableIRQ
Definition: core_armv8mbl.h:1190
ADC2
#define ADC2
Definition: stm32f103xb.h:671
Settings_t::vbat
volatile float vbat
Definition: conversion.h:139
Settings_t::newVbat
volatile uint8_t newVbat
Definition: conversion.h:140
VDDA_APPLI
#define VDDA_APPLI
Definition: adc.c:76