6969/** @addtogroup STM32F4xx_System_Private_Defines
7070 * @{
7171 */
72+ #if STM32L0xx
73+ // Contained by handle
74+ #elif defined(ADC_SAMPLETIME_15CYCLES )
75+ #define SAMPLINGTIME ADC_SAMPLETIME_15CYCLES;
76+ #elif defined(ADC_SAMPLETIME_13CYCLES_5 )
77+ #define SAMPLINGTIME ADC_SAMPLETIME_13CYCLES_5;
78+ #elif defined(ADC_SAMPLETIME_19CYCLES_5 )
79+ #define SAMPLINGTIME ADC_SAMPLETIME_19CYCLES_5;
80+ #elif defined(ADC_SAMPLETIME_16CYCLES )
81+ #define SAMPLINGTIME ADC_SAMPLETIME_16CYCLES;
82+ #elif defined(ADC_SAMPLETIME_12CYCLES_5 )
83+ #define SAMPLINGTIME ADC_SAMPLETIME_12CYCLES_5;
84+ #else
85+ #error "ADC SAMPLINGTIME could not be defined"
86+ #endif
7287
73- #ifdef STM32F0xx
74- #define SAMPLINGTIME ADC_SAMPLETIME_1CYCLE_5
88+ #ifdef ADC_CLOCK_SYNC_PCLK_DIV2
89+ #define ADC_CLOCK_DIV ADC_CLOCK_SYNC_PCLK_DIV2
90+ #elif defined(ADC_CLOCK_ASYNC_DIV1 )
91+ #define ADC_CLOCK_DIV ADC_CLOCK_ASYNC_DIV1
92+ #elif defined(ADC_CLOCKPRESCALER_PCLK_DIV2 )
93+ #define ADC_CLOCK_DIV ADC_CLOCKPRESCALER_PCLK_DIV2
7594#else
76- #define SAMPLINGTIME ADC_SAMPLETIME_3CYCLES /*!< ADC conversions sampling time. */
95+ #error "ADC_CLOCK_DIV could not be defined"
7796#endif
97+
7898#define ADC_REGULAR_RANK_1 1
7999/**
80100 * @}
@@ -105,9 +125,11 @@ static uint32_t get_adc_channel(PinName pin)
105125 uint32_t function = pinmap_function (pin , PinMap_ADC );
106126 uint32_t channel = 0 ;
107127 switch (STM_PIN_CHANNEL (function )) {
128+ #ifdef ADC_CHANNEL_0
108129 case 0 :
109130 channel = ADC_CHANNEL_0 ;
110131 break ;
132+ #endif
111133 case 1 :
112134 channel = ADC_CHANNEL_1 ;
113135 break ;
@@ -174,9 +196,11 @@ static uint32_t get_dac_channel(PinName pin)
174196 uint32_t function = pinmap_function (pin , PinMap_DAC );
175197 uint32_t channel = 0 ;
176198 switch (STM_PIN_CHANNEL (function )) {
199+ #ifdef DAC_CHANNEL_0
177200 case 0 :
178- channel = ADC_CHANNEL_0 ;
201+ channel = DAC_CHANNEL_0 ;
179202 break ;
203+ #endif
180204 case 1 :
181205 channel = DAC_CHANNEL_1 ;
182206 break ;
@@ -232,8 +256,12 @@ void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
232256 port = set_GPIO_Port_Clock (STM_PORT (g_current_pin ));
233257
234258 /* DAC Periph clock enable */
259+ #ifdef __HAL_RCC_DAC1_CLK_ENABLE
260+ __HAL_RCC_DAC1_CLK_ENABLE ();
261+ #endif
262+ #ifdef __HAL_RCC_DAC_CLK_ENABLE
235263 __HAL_RCC_DAC_CLK_ENABLE ();
236-
264+ #endif
237265 /*##-2- Configure peripheral GPIO ##########################################*/
238266 /* DAC Channel1 GPIO pin configuration */
239267 GPIO_InitStruct .Pin = STM_GPIO_PIN (g_current_pin );
@@ -253,8 +281,8 @@ void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
253281 */
254282void dac_write_value (PinName pin , uint32_t value , uint8_t do_init )
255283{
256- DAC_HandleTypeDef DacHandle = {0 };
257- DAC_ChannelConfTypeDef dacChannelConf ;
284+ DAC_HandleTypeDef DacHandle = {};
285+ DAC_ChannelConfTypeDef dacChannelConf = {} ;
258286 uint32_t dacChannel ;
259287
260288 DacHandle .Instance = pinmap_peripheral (pin , PinMap_DAC );
@@ -307,7 +335,12 @@ void dac_write_value(PinName pin, uint32_t value, uint8_t do_init)
307335void HAL_DAC_MspDeInit (DAC_HandleTypeDef * hdac )
308336{
309337 /* DAC Periph clock disable */
338+ #ifdef __HAL_RCC_DAC1_CLK_DISABLE
339+ __HAL_RCC_DAC1_CLK_DISABLE ();
340+ #endif
341+ #ifdef __HAL_RCC_DAC_CLK_DISABLE
310342 __HAL_RCC_DAC_CLK_DISABLE ();
343+ #endif
311344}
312345
313346/**
@@ -353,11 +386,36 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
353386 /*##-1- Enable peripherals and GPIO Clocks #################################*/
354387 /* ADC Periph clock enable */
355388 if (hadc -> Instance == ADC1 ) {
356- __HAL_RCC_ADC1_CLK_ENABLE ();
389+ #ifdef __HAL_RCC_ADC1_CLK_ENABLE
390+ __HAL_RCC_ADC1_CLK_ENABLE ();
391+ #endif
392+ #ifdef __HAL_RCC_ADC12_CLK_ENABLE
393+ __HAL_RCC_ADC12_CLK_ENABLE ();
394+ #endif
357395 }
396+ #ifdef ADC2
397+ else if (hadc -> Instance == ADC2 ) {
398+ #ifdef __HAL_RCC_ADC2_CLK_ENABLE
399+ __HAL_RCC_ADC2_CLK_ENABLE ();
400+ #endif
401+ #ifdef __HAL_RCC_ADC12_CLK_ENABLE
402+ __HAL_RCC_ADC12_CLK_ENABLE ();
403+ #endif
404+ }
405+ #endif
358406#ifdef ADC3
359407 else if (hadc -> Instance == ADC3 ) {
408+ #ifdef __HAL_RCC_ADC3_CLK_ENABLE
360409 __HAL_RCC_ADC3_CLK_ENABLE ();
410+ #endif
411+ #ifdef __HAL_RCC_ADC34_CLK_ENABLE
412+ __HAL_RCC_ADC34_CLK_ENABLE ();
413+ #endif
414+ }
415+ #endif
416+ #ifdef ADC4
417+ else if (hadc -> Instance == ADC4 ) {
418+ __HAL_RCC_ADC34_CLK_ENABLE ();
361419 }
362420#endif
363421
@@ -379,18 +437,87 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
379437 */
380438void HAL_ADC_MspDeInit (ADC_HandleTypeDef * hadc )
381439{
382-
440+ #ifdef __HAL_RCC_ADC_FORCE_RESET
383441 __HAL_RCC_ADC_FORCE_RESET ();
442+ #endif
443+ #ifdef __HAL_RCC_ADC_RELEASE_RESET
384444 __HAL_RCC_ADC_RELEASE_RESET ();
445+ #endif
385446
386447 if (hadc -> Instance == ADC1 ) {
387- __HAL_RCC_ADC1_CLK_DISABLE ();
448+ #ifdef __HAL_RCC_ADC1_FORCE_RESET
449+ __HAL_RCC_ADC1_FORCE_RESET ();
450+ #endif
451+ #ifdef __HAL_RCC_ADC1_RELEASE_RESET
452+ __HAL_RCC_ADC1_RELEASE_RESET ();
453+ #endif
454+ #ifdef __HAL_RCC_ADC12_FORCE_RESET
455+ __HAL_RCC_ADC12_FORCE_RESET ();
456+ #endif
457+ #ifdef __HAL_RCC_ADC12_RELEASE_RESET
458+ __HAL_RCC_ADC12_RELEASE_RESET ();
459+ #endif
460+ #ifdef __HAL_RCC_ADC1_CLK_DISABLE
461+ __HAL_RCC_ADC1_CLK_DISABLE ();
462+ #endif
463+ #ifdef __HAL_RCC_ADC12_CLK_DISABLE
464+ __HAL_RCC_ADC12_CLK_DISABLE ();
465+ #endif
466+ }
467+ #ifdef ADC2
468+ else if (hadc -> Instance == ADC2 ) {
469+ #ifdef __HAL_RCC_ADC2_FORCE_RESET
470+ __HAL_RCC_ADC2_FORCE_RESET ();
471+ #endif
472+ #ifdef __HAL_RCC_ADC2_RELEASE_RESET
473+ __HAL_RCC_ADC2_RELEASE_RESET ();
474+ #endif
475+ #ifdef __HAL_RCC_ADC12_FORCE_RESET
476+ __HAL_RCC_ADC12_FORCE_RESET ();
477+ #endif
478+ #ifdef __HAL_RCC_ADC12_RELEASE_RESET
479+ __HAL_RCC_ADC12_RELEASE_RESET ();
480+ #endif
481+ #ifdef __HAL_RCC_ADC2_CLK_DISABLE
482+ __HAL_RCC_ADC2_CLK_DISABLE ();
483+ #endif
484+ #ifdef __HAL_RCC_ADC2_CLK_DISABLE
485+ __HAL_RCC_ADC2_CLK_DISABLE ();
486+ #endif
388487 }
488+ #endif
389489#ifdef ADC3
390490 else if (hadc -> Instance == ADC3 ) {
491+ #ifdef __HAL_RCC_ADC3_FORCE_RESET
492+ __HAL_RCC_ADC3_FORCE_RESET ();
493+ #endif
494+ #ifdef __HAL_RCC_ADC3_RELEASE_RESET
495+ __HAL_RCC_ADC3_RELEASE_RESET ();
496+ #endif
497+ #ifdef __HAL_RCC_ADC34_FORCE_RESET
498+ __HAL_RCC_ADC34_FORCE_RESET ();
499+ #endif
500+ #ifdef __HAL_RCC_ADC34_RELEASE_RESET
501+ __HAL_RCC_ADC34_RELEASE_RESET ();
502+ #endif
503+ #ifdef __HAL_RCC_ADC3_CLK_DISABLE
391504 __HAL_RCC_ADC3_CLK_DISABLE ();
505+ #endif
506+ #ifdef __HAL_RCC_ADC34_CLK_DISABLE
507+ __HAL_RCC_ADC34_CLK_DISABLE ();
508+ #endif
392509 }
393510#endif
511+ #ifdef ADC4
512+ else if (hadc -> Instance == ADC4 ) {
513+ __HAL_RCC_ADC34_FORCE_RESET ();
514+ __HAL_RCC_ADC34_RELEASE_RESET ();
515+ __HAL_RCC_ADC34_CLK_DISABLE ();
516+ }
517+ #endif
518+ #ifdef __HAL_RCC_ADC_CLK_DISABLE
519+ __HAL_RCC_ADC_CLK_DISABLE ();
520+ #endif
394521}
395522
396523/**
@@ -400,15 +527,15 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
400527 */
401528uint16_t adc_read_value (PinName pin )
402529{
403- ADC_HandleTypeDef AdcHandle ;
404- ADC_ChannelConfTypeDef AdcChannelConf ;
530+ ADC_HandleTypeDef AdcHandle = {} ;
531+ ADC_ChannelConfTypeDef AdcChannelConf = {} ;
405532 __IO uint16_t uhADCxConvertedValue = 0 ;
406533
407534 AdcHandle .Instance = pinmap_peripheral (pin , PinMap_ADC );
408535
409536 if (AdcHandle .Instance == NC ) return 0 ;
410537
411- AdcHandle .Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2 ; /* Asynchronous clock mode, input ADC clock divided */
538+ AdcHandle .Init .ClockPrescaler = ADC_CLOCK_DIV ; /* Asynchronous clock mode, input ADC clock divided */
412539 AdcHandle .Init .Resolution = ADC_RESOLUTION_12B ; /* 12-bit resolution for converted data */
413540 AdcHandle .Init .DataAlign = ADC_DATAALIGN_RIGHT ; /* Right-alignment for converted data */
414541 AdcHandle .Init .ScanConvMode = DISABLE ; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
@@ -425,6 +552,9 @@ uint16_t adc_read_value(PinName pin)
425552 AdcHandle .Init .Overrun = ADC_OVR_DATA_OVERWRITTEN ; /* DR register is overwritten with the last conversion result in case of overrun */
426553 AdcHandle .Init .SamplingTimeCommon = SAMPLINGTIME ;
427554#else
555+ #ifdef STM32F3xx
556+ AdcHandle .Init .LowPowerAutoWait = DISABLE ; /* Auto-delayed conversion feature disabled */
557+ #endif
428558 AdcHandle .Init .NbrOfConversion = 1 ; /* Specifies the number of ranks that will be converted within the regular group sequencer. */
429559 AdcHandle .Init .NbrOfDiscConversion = 0 ; /* Parameter discarded because sequencer is disabled */
430560#endif
@@ -446,6 +576,15 @@ uint16_t adc_read_value(PinName pin)
446576 return 0 ;
447577 }
448578
579+ #ifdef STM32F3xx
580+ /*##-2.1- Calibrate ADC then Start the conversion process ####################*/
581+ if (HAL_ADCEx_Calibration_Start (& AdcHandle , ADC_SINGLE_ENDED ) != HAL_OK )
582+ {
583+ /* ADC Calibration Error */
584+ return 0 ;
585+ }
586+ #endif
587+
449588 /*##-3- Start the conversion process ####################*/
450589 if (HAL_ADC_Start (& AdcHandle ) != HAL_OK )
451590 {
0 commit comments