1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_hal_sdadc.c
4   * @author  MCD Application Team
5   * @brief   This file provides firmware functions to manage the following
6   *          functionalities of the Sigma-Delta Analog to Digital Converter
7   *          (SDADC) peripherals:
8   *           + Initialization and Configuration
9   *           + Regular Channels Configuration
10   *           + Injected channels Configuration
11   *           + Power saving
12   *           + Regular/Injected Channels DMA Configuration
13   *           + Interrupts and flags management
14   ******************************************************************************
15   * @attention
16   *
17   * Copyright (c) 2016 STMicroelectronics.
18   * All rights reserved.
19   *
20   * This software is licensed under terms that can be found in the LICENSE file
21   * in the root directory of this software component.
22   * If no LICENSE file comes with this software, it is provided AS-IS.
23   *
24   ******************************************************************************
25   @verbatim
26   ==============================================================================
27                     ##### SDADC specific features #####
28   ==============================================================================
29   [..]
30   (#) 16-bit sigma delta architecture.
31   (#) Self calibration.
32   (#) Interrupt generation at the end of calibration, regular/injected conversion
33       and in case of overrun events.
34   (#) Single and continuous conversion modes.
35   (#) External trigger option with configurable polarity for injected conversion.
36   (#) Multi mode (synchronized another SDADC with SDADC1).
37   (#) DMA request generation during regular or injected channel conversion.
38 
39                      ##### How to use this driver #####
40   ==============================================================================
41   [..]
42     *** Initialization ***
43     ======================
44     [..]
45       (#) As prerequisite, fill in the HAL_SDADC_MspInit() :
46         (++) Enable SDADCx clock interface with __SDADCx_CLK_ENABLE().
47         (++) Configure SDADCx clock divider with HAL_RCCEx_PeriphCLKConfig.
48         (++) Enable power on SDADC with HAL_PWREx_EnableSDADC().
49         (++) Enable the clocks for the SDADC GPIOS with __HAL_RCC_GPIOx_CLK_ENABLE().
50         (++) Configure these SDADC pins in analog mode using HAL_GPIO_Init().
51         (++) If interrupt mode is used, enable and configure SDADC global
52             interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
53         (++) If DMA mode is used, configure DMA with HAL_DMA_Init and link it
54             with SDADC handle using __HAL_LINKDMA.
55       (#) Configure the SDADC low power mode, fast conversion mode, slow clock
56           mode and SDADC1 reference voltage using the HAL_SDADC_Init() function.
57           Note: Common reference voltage. is common to all SDADC instances.
58       (#) Prepare channel configurations (input mode, common mode, gain and
59           offset) using HAL_SDADC_PrepareChannelConfig and associate channel
60           with one configuration using HAL_SDADC_AssociateChannelConfig.
61 
62     *** Calibration ***
63     ============================================
64     [..]
65       (#) Start calibration using HAL_SDADC_StartCalibration or
66           HAL_SDADC_CalibrationStart_IT.
67       (#) In polling mode, use HAL_SDADC_PollForCalibEvent to detect the end of
68           calibration.
69       (#) In interrupt mode, HAL_SDADC_CalibrationCpltCallback will be called at
70           the end of calibration.
71 
72     *** Regular channel conversion ***
73     ============================================
74     [..]
75       (#) Select trigger for regular conversion using
76           HAL_SDADC_SelectRegularTrigger.
77       (#) Select regular channel and enable/disable continuous mode using
78           HAL_SDADC_ConfigChannel.
79       (#) Start regular conversion using HAL_SDADC_Start, HAL_SDADC_Start_IT
80           or HAL_SDADC_Start_DMA.
81       (#) In polling mode, use HAL_SDADC_PollForConversion to detect the end of
82           regular conversion.
83       (#) In interrupt mode, HAL_SDADC_ConvCpltCallback will be called at the
84           end of regular conversion.
85       (#) Get value of regular conversion using HAL_SDADC_GetValue.
86       (#) In DMA mode, HAL_SDADC_ConvHalfCpltCallback and
87           HAL_SDADC_ConvCpltCallback will be called respectively at the half
88           transfer and at the transfer complete.
89       (#) Stop regular conversion using HAL_SDADC_Stop, HAL_SDADC_Stop_IT
90           or HAL_SDADC_Stop_DMA.
91 
92     *** Injected channels conversion ***
93     ============================================
94     [..]
95       (#) Enable/disable delay on injected conversion using
96           HAL_SDADC_SelectInjectedDelay.
97       (#) If external trigger is used for injected conversion, configure this
98           trigger using HAL_SDADC_SelectInjectedExtTrigger.
99       (#) Select trigger for injected conversion using
100           HAL_SDADC_SelectInjectedTrigger.
101       (#) Select injected channels and enable/disable continuous mode using
102           HAL_SDADC_InjectedConfigChannel.
103       (#) Start injected conversion using HAL_SDADC_InjectedStart,
104           HAL_SDADC_InjectedStart_IT or HAL_SDADC_InjectedStart_DMA.
105       (#) In polling mode, use HAL_SDADC_PollForInjectedConversion to detect the
106           end of injected conversion.
107       (#) In interrupt mode, HAL_SDADC_InjectedConvCpltCallback will be called
108           at the end of injected conversion.
109       (#) Get value of injected conversion and corresponding channel using
110           HAL_SDADC_InjectedGetValue.
111       (#) In DMA mode, HAL_SDADC_InjectedConvHalfCpltCallback and
112           HAL_SDADC_InjectedConvCpltCallback will be called respectively at the
113           half transfer and at the transfer complete.
114       (#) Stop injected conversion using HAL_SDADC_InjectedStop,
115           HAL_SDADC_InjectedStop_IT or HAL_SDADC_InjectedStop_DMA.
116 
117     *** Multi mode regular channels conversions ***
118     ======================================================
119     [..]
120       (#) Select type of multimode (SDADC1/SDADC2 or SDADC1/SDADC3) using
121           HAL_SDADC_MultiModeConfigChannel.
122       (#) Select software trigger for SDADC1 and synchronized trigger for
123           SDADC2 (or SDADC3) using HAL_SDADC_SelectRegularTrigger.
124       (#) Select regular channel for SDADC1 and SDADC2 (or SDADC3) using
125           HAL_SDADC_ConfigChannel.
126       (#) Start regular conversion for SDADC2 (or SDADC3) with HAL_SDADC_Start.
127       (#) Start regular conversion for SDADC1 using HAL_SDADC_Start,
128           HAL_SDADC_Start_IT or HAL_SDADC_MultiModeStart_DMA.
129       (#) In polling mode, use HAL_SDADC_PollForConversion to detect the end of
130           regular conversion for SDADC1.
131       (#) In interrupt mode, HAL_SDADC_ConvCpltCallback will be called at the
132           end of regular conversion for SDADC1.
133       (#) Get value of regular conversions using HAL_SDADC_MultiModeGetValue.
134       (#) In DMA mode, HAL_SDADC_ConvHalfCpltCallback and
135           HAL_SDADC_ConvCpltCallback will be called respectively at the half
136           transfer and at the transfer complete for SDADC1.
137       (#) Stop regular conversion using HAL_SDADC_Stop, HAL_SDADC_Stop_IT
138           or HAL_SDADC_MultiModeStop_DMA for SDADC1.
139       (#) Stop regular conversion using HAL_SDADC_Stop for SDADC2 (or SDADC3).
140 
141     *** Multi mode injected channels conversions ***
142     ======================================================
143     [..]
144       (#) Select type of multimode (SDADC1/SDADC2 or SDADC1/SDADC3) using
145           HAL_SDADC_InjectedMultiModeConfigChannel.
146       (#) Select software or external trigger for SDADC1 and synchronized
147           trigger for SDADC2 (or SDADC3) using HAL_SDADC_SelectInjectedTrigger.
148       (#) Select injected channels for SDADC1 and SDADC2 (or SDADC3) using
149           HAL_SDADC_InjectedConfigChannel.
150       (#) Start injected conversion for SDADC2 (or SDADC3) with
151           HAL_SDADC_InjectedStart.
152       (#) Start injected conversion for SDADC1 using HAL_SDADC_InjectedStart,
153           HAL_SDADC_InjectedStart_IT or HAL_SDADC_InjectedMultiModeStart_DMA.
154       (#) In polling mode, use HAL_SDADC_InjectedPollForConversion to detect
155           the end of injected conversion for SDADC1.
156       (#) In interrupt mode, HAL_SDADC_InjectedConvCpltCallback will be called
157           at the end of injected conversion for SDADC1.
158       (#) Get value of injected conversions using
159           HAL_SDADC_InjectedMultiModeGetValue.
160       (#) In DMA mode, HAL_SDADC_InjectedConvHalfCpltCallback and
161           HAL_SDADC_InjectedConvCpltCallback will be called respectively at the
162           half transfer and at the transfer complete for SDADC1.
163       (#) Stop injected conversion using HAL_SDADC_InjectedStop,
164           HAL_SDADC_InjectedStop_IT or HAL_SDADC_InjecteddMultiModeStop_DMA
165           for SDADC1.
166       (#) Stop injected conversion using HAL_SDADC_InjectedStop for SDADC2
167           (or SDADC3).
168 
169     *** Callback registration ***
170     =============================================
171     [..]
172 
173      The compilation flag USE_HAL_SDADC_REGISTER_CALLBACKS, when set to 1,
174      allows the user to configure dynamically the driver callbacks.
175      Use Functions HAL_SDADC_RegisterCallback()
176      to register an interrupt callback.
177     [..]
178 
179      Function HAL_SDADC_RegisterCallback() allows to register following callbacks:
180        (+) ConvHalfCpltCallback : callback for half regular conversion complete.
181        (+) ConvCpltCallback : callback for regular conversion complete
182        (+) InjectedConvHalfCpltCallback : callback for half injected conversion complete
183        (+) InjectedConvCpltCallback : callback for injected conversion complete
184        (+) CalibrationCpltCallback : callback for calibration
185        (+) ErrorCallback : callback for error detection.
186        (+) MspInitCallback : callback for Msp Init.
187        (+) MspDeInitCallback : callback for Msp DeInit.
188      This function takes as parameters the HAL peripheral handle, the Callback ID
189      and a pointer to the user callback function.
190     [..]
191 
192      Use function HAL_SDADC_UnRegisterCallback to reset a callback to the default
193      weak function.
194     [..]
195 
196      HAL_SDADC_UnRegisterCallback takes as parameters the HAL peripheral handle,
197      and the Callback ID.
198      This function allows to reset following callbacks:
199        (+) ConvHalfCpltCallback : callback for half regular conversion complete.
200        (+) ConvCpltCallback : callback for regular conversion complete
201        (+) InjectedConvHalfCpltCallback : callback for half injected conversion complete
202        (+) InjectedConvCpltCallback : callback for injected conversion complete
203        (+) CalibrationCpltCallback : callback for calibration
204        (+) ErrorCallback : callback for error detection.
205        (+) MspInitCallback : callback for Msp Init.
206        (+) MspDeInitCallback : callback for Msp DeInit.
207      [..]
208 
209      By default, after the HAL_SDADC_Init() and when the state is HAL_SDADC_STATE_RESET
210      all callbacks are set to the corresponding weak functions:
211      examples HAL_SDADC_ConvCpltCallback(), HAL_SDADC_ErrorCallback().
212      Exception done for MspInit and MspDeInit functions that are
213      reset to the legacy weak functions in the HAL_SDADC_Init()/ HAL_SDADC_DeInit() only when
214      these callbacks are null (not registered beforehand).
215     [..]
216 
217      If MspInit or MspDeInit are not null, the HAL_SDADC_Init()/ HAL_SDADC_DeInit()
218      keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
219      [..]
220 
221      Callbacks can be registered/unregistered in HAL_SDADC_STATE_READY state only.
222      Exception done MspInit/MspDeInit functions that can be registered/unregistered
223      in HAL_SDADC_STATE_READY or HAL_SDADC_STATE_RESET state,
224      thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
225     [..]
226 
227      Then, the user first registers the MspInit/MspDeInit user callbacks
228      using HAL_SDADC_RegisterCallback() before calling HAL_SDADC_DeInit()
229      or HAL_SDADC_Init() function.
230      [..]
231 
232      When the compilation flag USE_HAL_SDADC_REGISTER_CALLBACKS is set to 0 or
233      not defined, the callback registration feature is not available and all callbacks
234      are set to the corresponding weak functions.
235 
236     @endverbatim
237   */
238 
239 /* Includes ------------------------------------------------------------------*/
240 #include "stm32f3xx_hal.h"
241 
242 /** @addtogroup STM32F3xx_HAL_Driver
243   * @{
244   */
245 
246 #ifdef HAL_SDADC_MODULE_ENABLED
247 #if defined(SDADC1) || defined(SDADC2) || defined(SDADC3)
248 /** @defgroup SDADC SDADC
249   * @brief SDADC HAL driver modules
250   * @{
251   */
252 
253 /* Private typedef -----------------------------------------------------------*/
254 /* Private define ------------------------------------------------------------*/
255 /** @defgroup SDADC_Private_Define SDADC Private Define
256  * @{
257  */
258 #define SDADC_TIMEOUT          200UL
259 #define SDADC_CONFREG_OFFSET   0x00000020UL
260 #define SDADC_JDATAR_CH_OFFSET 24UL
261 #define SDADC_MSB_MASK         0xFFFF0000UL
262 #define SDADC_LSB_MASK         0x0000FFFFUL
263 /**
264   * @}
265   */
266 
267 /* Private macro -------------------------------------------------------------*/
268 /* Private variables ---------------------------------------------------------*/
269 /* Private function prototypes -----------------------------------------------*/
270 /** @defgroup SDADC_Private_Functions SDADC Private Functions
271   * @{
272   */
273 
274 static HAL_StatusTypeDef SDADC_EnterInitMode(SDADC_HandleTypeDef* hsdadc);
275 static void              SDADC_ExitInitMode(SDADC_HandleTypeDef* hsdadc);
276 static uint32_t          SDADC_GetInjChannelsNbr(uint32_t Channels);
277 static HAL_StatusTypeDef SDADC_RegConvStart(SDADC_HandleTypeDef* hsdadc);
278 static HAL_StatusTypeDef SDADC_RegConvStop(SDADC_HandleTypeDef* hsdadc);
279 static HAL_StatusTypeDef SDADC_InjConvStart(SDADC_HandleTypeDef* hsdadc);
280 static HAL_StatusTypeDef SDADC_InjConvStop(SDADC_HandleTypeDef* hsdadc);
281 static void              SDADC_DMARegularHalfConvCplt(DMA_HandleTypeDef *hdma);
282 static void              SDADC_DMARegularConvCplt(DMA_HandleTypeDef *hdma);
283 static void              SDADC_DMAInjectedHalfConvCplt(DMA_HandleTypeDef *hdma);
284 static void              SDADC_DMAInjectedConvCplt(DMA_HandleTypeDef *hdma);
285 static void              SDADC_DMAError(DMA_HandleTypeDef *hdma);
286 /**
287   * @}
288   */
289 
290 /* Exported functions ---------------------------------------------------------*/
291 
292 /** @defgroup SDADC_Exported_Functions SDADC Exported Functions
293   * @{
294   */
295 
296 /** @defgroup SDADC_Exported_Functions_Group1 Initialization and de-initialization functions
297  *  @brief    Initialization and de-initialization functions
298  *
299 @verbatim
300   ===============================================================================
301               ##### Initialization and de-initialization functions #####
302   ===============================================================================
303     [..]  This section provides functions allowing to:
304       (+) Initialize the SDADC.
305       (+) De-initialize the SDADC.
306 
307 @endverbatim
308   * @{
309   */
310 
311 /**
312   * @brief  Initializes the SDADC according to the specified
313   *         parameters in the SDADC_InitTypeDef structure.
314   * @note   If multiple SDADC are used, please configure first SDADC1 to set
315   *         the common reference voltage.
316   * @param  hsdadc SDADC handle.
317   * @retval HAL status.
318   */
HAL_SDADC_Init(SDADC_HandleTypeDef * hsdadc)319 HAL_StatusTypeDef HAL_SDADC_Init(SDADC_HandleTypeDef* hsdadc)
320 {
321   uint32_t tickstart;
322 
323   /* Check SDADC handle */
324   if(hsdadc == NULL)
325   {
326     return HAL_ERROR;
327   }
328 
329   /* Check parameters */
330   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
331   assert_param(IS_SDADC_LOWPOWER_MODE(hsdadc->Init.IdleLowPowerMode));
332   assert_param(IS_SDADC_FAST_CONV_MODE(hsdadc->Init.FastConversionMode));
333   assert_param(IS_SDADC_SLOW_CLOCK_MODE(hsdadc->Init.SlowClockMode));
334   assert_param(IS_SDADC_VREF(hsdadc->Init.ReferenceVoltage));
335 
336   /* Initialize SDADC variables with default values */
337   hsdadc->RegularContMode     = SDADC_CONTINUOUS_CONV_OFF;
338   hsdadc->InjectedContMode    = SDADC_CONTINUOUS_CONV_OFF;
339   hsdadc->InjectedChannelsNbr = 1U;
340   hsdadc->InjConvRemaining    = 1U;
341   hsdadc->RegularTrigger      = SDADC_SOFTWARE_TRIGGER;
342   hsdadc->InjectedTrigger     = SDADC_SOFTWARE_TRIGGER;
343   hsdadc->ExtTriggerEdge      = SDADC_EXT_TRIG_RISING_EDGE;
344   hsdadc->RegularMultimode    = SDADC_MULTIMODE_SDADC1_SDADC2;
345   hsdadc->InjectedMultimode   = SDADC_MULTIMODE_SDADC1_SDADC2;
346   hsdadc->ErrorCode           = SDADC_ERROR_NONE;
347 
348 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
349   if(hsdadc->State == HAL_SDADC_STATE_RESET)
350   {
351     /* Init the SDADC Callback settings */
352     hsdadc->ConvHalfCpltCallback         = HAL_SDADC_ConvHalfCpltCallback;
353     hsdadc->ConvCpltCallback             = HAL_SDADC_ConvCpltCallback;
354     hsdadc->InjectedConvHalfCpltCallback = HAL_SDADC_InjectedConvHalfCpltCallback;
355     hsdadc->InjectedConvCpltCallback     = HAL_SDADC_InjectedConvCpltCallback;
356     hsdadc->CalibrationCpltCallback      = HAL_SDADC_CalibrationCpltCallback;
357     hsdadc->ErrorCallback                = HAL_SDADC_ErrorCallback;
358   }
359 
360   if (hsdadc->MspInitCallback == NULL)
361   {
362     hsdadc->MspInitCallback = HAL_SDADC_MspInit; /* Legacy weak MspInit  */
363   }
364 
365   /* Init the low level hardware */
366   hsdadc->MspInitCallback(hsdadc);
367 #else
368   /* Init the low level hardware */
369   HAL_SDADC_MspInit(hsdadc);
370 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
371 
372   /* Set idle low power and slow clock modes */
373   hsdadc->Instance->CR1 &= ~(SDADC_CR1_SBI|SDADC_CR1_PDI|SDADC_CR1_SLOWCK);
374   hsdadc->Instance->CR1 |= (hsdadc->Init.IdleLowPowerMode | \
375                             hsdadc->Init.SlowClockMode);
376 
377   /* Set fast conversion mode */
378   hsdadc->Instance->CR2 &= ~(SDADC_CR2_FAST);
379   hsdadc->Instance->CR2 |= hsdadc->Init.FastConversionMode;
380 
381   /* Set reference voltage common to all SDADC instances */
382   /* Update this parameter only if needed to avoid unnecessary settling time */
383   if((SDADC1->CR1 & SDADC_CR1_REFV) != hsdadc->Init.ReferenceVoltage)
384   {
385     /* Voltage reference bits are common to all SADC instances but are        */
386     /* present in SDADC1 register.                                            */
387     SDADC1->CR1 &= ~(SDADC_CR1_REFV);
388     SDADC1->CR1 |= hsdadc->Init.ReferenceVoltage;
389 
390     /* Wait at least 2ms before setting ADON */
391     HAL_Delay(2U);
392   }
393 
394   /* Enable SDADC */
395   hsdadc->Instance->CR2 |= SDADC_CR2_ADON;
396 
397   /* Wait end of stabilization */
398   tickstart = HAL_GetTick();
399   while((hsdadc->Instance->ISR & SDADC_ISR_STABIP) != 0UL)
400   {
401     if((HAL_GetTick()-tickstart) > SDADC_TIMEOUT)
402     {
403       return HAL_TIMEOUT;
404     }
405   }
406 
407   /* Set SDADC to ready state */
408   hsdadc->State = HAL_SDADC_STATE_READY;
409 
410   /* Return HAL status */
411   return HAL_OK;
412 }
413 
414 /**
415   * @brief  De-initializes the SDADC.
416   * @param  hsdadc SDADC handle.
417   * @retval HAL status.
418   */
HAL_SDADC_DeInit(SDADC_HandleTypeDef * hsdadc)419 HAL_StatusTypeDef HAL_SDADC_DeInit(SDADC_HandleTypeDef* hsdadc)
420 {
421   /* Check SDADC handle */
422   if(hsdadc == NULL)
423   {
424     return HAL_ERROR;
425   }
426 
427   /* Check parameters */
428   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
429 
430   /* Disable the SDADC */
431   hsdadc->Instance->CR2 &= ~(SDADC_CR2_ADON);
432 
433   /* Reset all registers */
434   hsdadc->Instance->CR1      = 0x00000000UL;
435   hsdadc->Instance->CR2      = 0x00000000UL;
436   hsdadc->Instance->JCHGR    = 0x00000001UL;
437   hsdadc->Instance->CONF0R   = 0x00000000UL;
438   hsdadc->Instance->CONF1R   = 0x00000000UL;
439   hsdadc->Instance->CONF2R   = 0x00000000UL;
440   hsdadc->Instance->CONFCHR1 = 0x00000000UL;
441   hsdadc->Instance->CONFCHR2 = 0x00000000UL;
442 
443 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
444     if (hsdadc->MspDeInitCallback == NULL)
445     {
446       hsdadc->MspDeInitCallback = HAL_SDADC_MspDeInit; /* Legacy weak MspDeInit */
447     }
448 
449     /* DeInit the low level hardware */
450     hsdadc->MspDeInitCallback(hsdadc);
451 #else
452     /* DeInit the low level hardware */
453     HAL_SDADC_MspDeInit(hsdadc);
454 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
455 
456   /* Set SDADC in reset state */
457   hsdadc->State = HAL_SDADC_STATE_RESET;
458 
459   /* Return function status */
460   return HAL_OK;
461 }
462 
463 /**
464   * @brief  Initializes the SDADC MSP.
465   * @param  hsdadc SDADC handle
466   * @retval None
467   */
HAL_SDADC_MspInit(SDADC_HandleTypeDef * hsdadc)468 __weak void HAL_SDADC_MspInit(SDADC_HandleTypeDef* hsdadc)
469 {
470   /* Prevent unused argument(s) compilation warning */
471   UNUSED(hsdadc);
472 
473   /* NOTE : This function should not be modified, when the callback is needed,
474             the HAL_SDADC_MspInit could be implemented in the user file.
475    */
476 }
477 
478 /**
479   * @brief  De-initializes the SDADC MSP.
480   * @param  hsdadc SDADC handle
481   * @retval None
482   */
HAL_SDADC_MspDeInit(SDADC_HandleTypeDef * hsdadc)483 __weak void HAL_SDADC_MspDeInit(SDADC_HandleTypeDef* hsdadc)
484 {
485   /* Prevent unused argument(s) compilation warning */
486   UNUSED(hsdadc);
487 
488   /* NOTE : This function should not be modified, when the callback is needed,
489             the HAL_SDADC_MspDeInit could be implemented in the user file.
490    */
491 }
492 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
493 /**
494   * @brief  Register a User SDADC Callback
495   *         To be used instead of the weak predefined callback
496   * @param  hsdadc Pointer to a SDADC_HandleTypeDef structure that contains
497   *                the configuration information for the specified SDADC.
498   * @param  CallbackID ID of the callback to be registered
499   *         This parameter can be one of the following values:
500   *          @arg @ref HAL_SDADC_CONVERSION_HALF_CB_ID          SDADC half regular conversion complete callback ID
501   *          @arg @ref HAL_SDADC_CONVERSION_COMPLETE_CB_ID      SDADC regular conversion complete callback ID
502   *          @arg @ref HAL_SDADC_INJ_CONVERSION_HALF_CB_ID      SDADC half injected conversion complete callback ID
503   *          @arg @ref HAL_SDADC_INJ_CONVERSION_COMPLETE_CB_ID  SDADC injected conversion complete callback ID
504   *          @arg @ref HAL_SDADC_CALIBRATION_COMPLETE_CB_ID     SDADC calibration callback ID
505   *          @arg @ref HAL_SDADC_ERROR_CB_ID                    SDADC error callback ID
506   *          @arg @ref HAL_SDADC_MSPINIT_CB_ID                  SDADC Msp Init callback ID
507   *          @arg @ref HAL_SDADC_MSPDEINIT_CB_ID                SDADC Msp DeInit callback ID
508   * @param  pCallback pointer to the Callback function
509   * @retval HAL status
510   */
HAL_SDADC_RegisterCallback(SDADC_HandleTypeDef * hsdadc,HAL_SDADC_CallbackIDTypeDef CallbackID,pSDADC_CallbackTypeDef pCallback)511 HAL_StatusTypeDef HAL_SDADC_RegisterCallback(SDADC_HandleTypeDef *hsdadc, HAL_SDADC_CallbackIDTypeDef CallbackID, pSDADC_CallbackTypeDef pCallback)
512 {
513   HAL_StatusTypeDef status = HAL_OK;
514 
515   if (pCallback == NULL)
516   {
517     /* Update the error code */
518     hsdadc->ErrorCode |= SDADC_ERROR_INVALID_CALLBACK;
519 
520     return HAL_ERROR;
521   }
522 
523   if (HAL_SDADC_STATE_READY == hsdadc->State)
524   {
525     switch (CallbackID)
526     {
527       case HAL_SDADC_CONVERSION_HALF_CB_ID :
528         hsdadc->ConvHalfCpltCallback = pCallback;
529         break;
530 
531       case HAL_SDADC_CONVERSION_COMPLETE_CB_ID :
532         hsdadc->ConvCpltCallback = pCallback;
533         break;
534 
535       case HAL_SDADC_INJ_CONVERSION_HALF_CB_ID :
536         hsdadc->InjectedConvHalfCpltCallback = pCallback;
537         break;
538 
539       case HAL_SDADC_INJ_CONVERSION_COMPLETE_CB_ID :
540         hsdadc->InjectedConvCpltCallback = pCallback;
541         break;
542 
543       case HAL_SDADC_CALIBRATION_COMPLETE_CB_ID :
544         hsdadc->CalibrationCpltCallback = pCallback;
545         break;
546 
547       case HAL_SDADC_ERROR_CB_ID :
548         hsdadc->ErrorCallback = pCallback;
549         break;
550 
551       case HAL_SDADC_MSPINIT_CB_ID :
552         hsdadc->MspInitCallback = pCallback;
553         break;
554 
555       case HAL_SDADC_MSPDEINIT_CB_ID :
556         hsdadc->MspDeInitCallback = pCallback;
557         break;
558 
559       default :
560         /* Update the error code */
561         hsdadc->ErrorCode |= SDADC_ERROR_INVALID_CALLBACK;
562 
563         /* Return error status */
564         status = HAL_ERROR;
565         break;
566     }
567   }
568   else if (HAL_SDADC_STATE_RESET == hsdadc->State)
569   {
570     switch (CallbackID)
571     {
572       case HAL_SDADC_MSPINIT_CB_ID :
573         hsdadc->MspInitCallback = pCallback;
574         break;
575 
576       case HAL_SDADC_MSPDEINIT_CB_ID :
577         hsdadc->MspDeInitCallback = pCallback;
578         break;
579 
580       default :
581         /* Update the error code */
582         hsdadc->ErrorCode |= SDADC_ERROR_INVALID_CALLBACK;
583 
584         /* Return error status */
585         status = HAL_ERROR;
586         break;
587     }
588   }
589   else
590   {
591     /* Update the error code */
592     hsdadc->ErrorCode |= SDADC_ERROR_INVALID_CALLBACK;
593 
594     /* Return error status */
595     status =  HAL_ERROR;
596   }
597 
598   return status;
599 }
600 
601 /**
602   * @brief  Unregister a SDADC Callback
603   *         ADC callback is redirected to the weak predefined callback
604   * @param  hsdadc Pointer to a SDADC_HandleTypeDef structure that contains
605   *                the configuration information for the specified ADC.
606   * @param  CallbackID ID of the callback to be unregistered
607   *         This parameter can be one of the following values:
608   *          @arg @ref HAL_SDADC_CONVERSION_HALF_CB_ID          SDADC half regular conversion complete callback ID
609   *          @arg @ref HAL_SDADC_CONVERSION_COMPLETE_CB_ID      SDADC regular conversion complete callback ID
610   *          @arg @ref HAL_SDADC_INJ_CONVERSION_HALF_CB_ID      SDADC half injected conversion complete callback ID
611   *          @arg @ref HAL_SDADC_INJ_CONVERSION_COMPLETE_CB_ID  SDADC injected conversion complete callback ID
612   *          @arg @ref HAL_SDADC_CALIBRATION_COMPLETE_CB_ID     SDADC calibration callback ID
613   *          @arg @ref HAL_SDADC_ERROR_CB_ID                    SDADC error callback ID
614   *          @arg @ref HAL_SDADC_MSPINIT_CB_ID                  SDADC Msp Init callback ID
615   *          @arg @ref HAL_SDADC_MSPDEINIT_CB_ID                SDADC Msp DeInit callback ID
616   * @retval HAL status
617   */
HAL_SDADC_UnRegisterCallback(SDADC_HandleTypeDef * hsdadc,HAL_SDADC_CallbackIDTypeDef CallbackID)618 HAL_StatusTypeDef HAL_SDADC_UnRegisterCallback(SDADC_HandleTypeDef *hsdadc, HAL_SDADC_CallbackIDTypeDef CallbackID)
619 {
620   HAL_StatusTypeDef status = HAL_OK;
621 
622   if (HAL_SDADC_STATE_READY == hsdadc->State)
623   {
624     switch (CallbackID)
625     {
626       case HAL_SDADC_CONVERSION_HALF_CB_ID :
627         hsdadc->ConvHalfCpltCallback = HAL_SDADC_ConvHalfCpltCallback;
628         break;
629 
630       case HAL_SDADC_CONVERSION_COMPLETE_CB_ID :
631         hsdadc->ConvCpltCallback = HAL_SDADC_ConvCpltCallback;
632         break;
633 
634       case HAL_SDADC_INJ_CONVERSION_HALF_CB_ID :
635         hsdadc->InjectedConvHalfCpltCallback = HAL_SDADC_InjectedConvHalfCpltCallback;
636         break;
637 
638       case HAL_SDADC_INJ_CONVERSION_COMPLETE_CB_ID :
639         hsdadc->InjectedConvCpltCallback = HAL_SDADC_InjectedConvCpltCallback;
640         break;
641 
642       case HAL_SDADC_CALIBRATION_COMPLETE_CB_ID :
643         hsdadc->CalibrationCpltCallback = HAL_SDADC_CalibrationCpltCallback;
644         break;
645 
646       case HAL_SDADC_ERROR_CB_ID :
647         hsdadc->ErrorCallback = HAL_SDADC_ErrorCallback;
648         break;
649 
650       case HAL_SDADC_MSPINIT_CB_ID :
651         hsdadc->MspInitCallback = HAL_SDADC_MspInit;
652         break;
653 
654       case HAL_SDADC_MSPDEINIT_CB_ID :
655         hsdadc->MspDeInitCallback = HAL_SDADC_MspDeInit;
656         break;
657 
658       default :
659         /* Update the error code */
660         hsdadc->ErrorCode |= SDADC_ERROR_INVALID_CALLBACK;
661 
662         /* Return error status */
663         status =  HAL_ERROR;
664         break;
665     }
666   }
667   else if (HAL_SDADC_STATE_RESET == hsdadc->State)
668   {
669     switch (CallbackID)
670     {
671       case HAL_SDADC_MSPINIT_CB_ID :
672         hsdadc->MspInitCallback = HAL_SDADC_MspInit;                   /* Legacy weak MspInit              */
673         break;
674 
675       case HAL_SDADC_MSPDEINIT_CB_ID :
676         hsdadc->MspDeInitCallback = HAL_SDADC_MspDeInit;               /* Legacy weak MspDeInit            */
677         break;
678 
679       default :
680         /* Update the error code */
681         hsdadc->ErrorCode |= SDADC_ERROR_INVALID_CALLBACK;
682 
683         /* Return error status */
684         status =  HAL_ERROR;
685         break;
686     }
687   }
688   else
689   {
690     /* Update the error code */
691     hsdadc->ErrorCode |= SDADC_ERROR_INVALID_CALLBACK;
692 
693     /* Return error status */
694     status =  HAL_ERROR;
695   }
696 
697   return status;
698 }
699 
700 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
701 
702 /**
703   * @}
704   */
705 
706 /** @defgroup SDADC_Exported_Functions_Group2 peripheral control functions
707  *  @brief    Peripheral control functions
708  *
709 @verbatim
710   ===============================================================================
711               ##### Peripheral control functions #####
712   ===============================================================================
713     [..]  This section provides functions allowing to:
714       (+) Program one of the three different configurations for channels.
715       (+) Associate channel to one of configurations.
716       (+) Select regular and injected channels.
717       (+) Enable/disable continuous mode for regular and injected conversions.
718       (+) Select regular and injected triggers.
719       (+) Select and configure injected external trigger.
720       (+) Enable/disable delay addition for injected conversions.
721       (+) Configure multimode.
722 
723 @endverbatim
724   * @{
725   */
726 
727 /**
728   * @brief  This function allows the user to set parameters for a configuration.
729   *         Parameters are input mode, common mode, gain and offset.
730   * @note   This function should be called only when SDADC instance is in idle state
731   *         (neither calibration nor regular or injected conversion ongoing)
732   * @param  hsdadc SDADC handle.
733   * @param  ConfIndex Index of configuration to modify.
734   *         This parameter can be a value of @ref SDADC_ConfIndex.
735   * @param  ConfParamStruct Parameters to apply for this configuration.
736   * @retval HAL status
737   */
HAL_SDADC_PrepareChannelConfig(SDADC_HandleTypeDef * hsdadc,uint32_t ConfIndex,SDADC_ConfParamTypeDef * ConfParamStruct)738 HAL_StatusTypeDef HAL_SDADC_PrepareChannelConfig(SDADC_HandleTypeDef *hsdadc,
739                                                  uint32_t ConfIndex,
740                                                  SDADC_ConfParamTypeDef* ConfParamStruct)
741 {
742   HAL_StatusTypeDef status = HAL_OK;
743   uint32_t          tmp;
744 
745   /* Check parameters */
746   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
747   assert_param(IS_SDADC_CONF_INDEX(ConfIndex));
748   assert_param(ConfParamStruct != ((void*) 0));
749   assert_param(IS_SDADC_INPUT_MODE(ConfParamStruct->InputMode));
750   assert_param(IS_SDADC_GAIN(ConfParamStruct->Gain));
751   assert_param(IS_SDADC_COMMON_MODE(ConfParamStruct->CommonMode));
752   assert_param(IS_SDADC_OFFSET_VALUE(ConfParamStruct->Offset));
753 
754   /* Check SDADC state is ready */
755   if(hsdadc->State != HAL_SDADC_STATE_READY)
756   {
757     status = HAL_ERROR;
758   }
759   else
760   {
761     /* Enter init mode */
762     if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
763     {
764       /* Set SDADC in error state */
765       hsdadc->State = HAL_SDADC_STATE_ERROR;
766       status = HAL_TIMEOUT;
767     }
768     else
769     {
770       /* Program configuration register with parameters */
771       tmp = (uint32_t)((uint32_t)(hsdadc->Instance) + \
772                        SDADC_CONFREG_OFFSET + \
773                        (uint32_t)(ConfIndex << 2UL));
774       *(__IO uint32_t *) (tmp) = (uint32_t) (ConfParamStruct->InputMode | \
775                                              ConfParamStruct->Gain | \
776                                              ConfParamStruct->CommonMode | \
777                                              ConfParamStruct->Offset);
778       /* Exit init mode */
779       SDADC_ExitInitMode(hsdadc);
780     }
781   }
782   /* Return function status */
783   return status;
784 }
785 
786 /**
787   * @brief  This function allows the user to associate a channel with one of the
788   *         available configurations.
789   * @note   This function should be called only when SDADC instance is in idle state
790   *         (neither calibration nor regular or injected conversion ongoing)
791   * @param  hsdadc SDADC handle.
792   * @param  Channel Channel to associate with configuration.
793   *         This parameter can be a value of @ref SDADC_Channel_Selection.
794   * @param  ConfIndex Index of configuration to associate with channel.
795   *         This parameter can be a value of @ref SDADC_ConfIndex.
796   * @retval HAL status
797   */
HAL_SDADC_AssociateChannelConfig(SDADC_HandleTypeDef * hsdadc,uint32_t Channel,uint32_t ConfIndex)798 HAL_StatusTypeDef HAL_SDADC_AssociateChannelConfig(SDADC_HandleTypeDef *hsdadc,
799                                                    uint32_t Channel,
800                                                    uint32_t ConfIndex)
801 {
802   HAL_StatusTypeDef status = HAL_OK;
803   uint32_t          channelnum;
804 
805   /* Check parameters */
806   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
807   assert_param(IS_SDADC_REGULAR_CHANNEL(Channel));
808   assert_param(IS_SDADC_CONF_INDEX(ConfIndex));
809 
810   /* Check SDADC state is ready */
811   if(hsdadc->State != HAL_SDADC_STATE_READY)
812   {
813     status = HAL_ERROR;
814   }
815   else
816   {
817     /* Enter init mode */
818     if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
819     {
820       /* Set SDADC in error state */
821       hsdadc->State = HAL_SDADC_STATE_ERROR;
822       status = HAL_TIMEOUT;
823     }
824     else
825     {
826       /* Program channel configuration register according parameters */
827       if(Channel != SDADC_CHANNEL_8)
828       {
829         /* Get channel number */
830         channelnum = (uint32_t)(Channel>>16UL);
831 
832         /* Set the channel configuration */
833         hsdadc->Instance->CONFCHR1 &= (uint32_t) ~((uint32_t)SDADC_CONFCHR1_CONFCH0 << ((channelnum << 2UL) & 0x1FUL));
834         hsdadc->Instance->CONFCHR1 |= (uint32_t) (ConfIndex << ((channelnum << 2UL) & 0x1FUL));
835       }
836       else
837       {
838         hsdadc->Instance->CONFCHR2 = (uint32_t) (ConfIndex);
839       }
840       /* Exit init mode */
841       SDADC_ExitInitMode(hsdadc);
842     }
843   }
844   /* Return function status */
845   return status;
846 }
847 
848 /**
849   * @brief  This function allows to select channel for regular conversion and
850   *         to enable/disable continuous mode for regular conversion.
851   * @param  hsdadc SDADC handle.
852   * @param  Channel Channel for regular conversion.
853   *         This parameter can be a value of @ref SDADC_Channel_Selection.
854   * @param  ContinuousMode Enable/disable continuous mode for regular conversion.
855   *         This parameter can be a value of @ref SDADC_ContinuousMode.
856   * @retval HAL status
857   */
HAL_SDADC_ConfigChannel(SDADC_HandleTypeDef * hsdadc,uint32_t Channel,uint32_t ContinuousMode)858 HAL_StatusTypeDef HAL_SDADC_ConfigChannel(SDADC_HandleTypeDef *hsdadc,
859                                           uint32_t Channel,
860                                           uint32_t ContinuousMode)
861 {
862   HAL_StatusTypeDef status = HAL_OK;
863 
864   /* Check parameters */
865   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
866   assert_param(IS_SDADC_REGULAR_CHANNEL(Channel));
867   assert_param(IS_SDADC_CONTINUOUS_MODE(ContinuousMode));
868 
869   /* Check SDADC state */
870   if((hsdadc->State != HAL_SDADC_STATE_RESET) && (hsdadc->State != HAL_SDADC_STATE_ERROR))
871   {
872     /* Set RCH[3:0] and RCONT bits in SDADC_CR2 */
873     hsdadc->Instance->CR2 &= (uint32_t) ~(SDADC_CR2_RCH | SDADC_CR2_RCONT);
874     if(ContinuousMode == SDADC_CONTINUOUS_CONV_ON)
875     {
876       hsdadc->Instance->CR2 |= (uint32_t) ((Channel & SDADC_MSB_MASK) | SDADC_CR2_RCONT);
877     }
878     else
879     {
880       hsdadc->Instance->CR2 |= (uint32_t) ((Channel & SDADC_MSB_MASK));
881     }
882     /* Store continuous mode information */
883     hsdadc->RegularContMode = ContinuousMode;
884   }
885   else
886   {
887     status = HAL_ERROR;
888   }
889   /* Return function status */
890   return status;
891 }
892 
893 /**
894   * @brief  This function allows to select channels for injected conversion and
895   *         to enable/disable continuous mode for injected conversion.
896   * @param  hsdadc SDADC handle.
897   * @param  Channel Channels for injected conversion.
898   *         This parameter can be a values combination of @ref SDADC_Channel_Selection.
899   * @param  ContinuousMode Enable/disable continuous mode for injected conversion.
900   *         This parameter can be a value of @ref SDADC_ContinuousMode.
901   * @retval HAL status
902   */
HAL_SDADC_InjectedConfigChannel(SDADC_HandleTypeDef * hsdadc,uint32_t Channel,uint32_t ContinuousMode)903 HAL_StatusTypeDef HAL_SDADC_InjectedConfigChannel(SDADC_HandleTypeDef *hsdadc,
904                                                   uint32_t Channel,
905                                                   uint32_t ContinuousMode)
906 {
907   HAL_StatusTypeDef status = HAL_OK;
908 
909   /* Check parameters */
910   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
911   assert_param(IS_SDADC_INJECTED_CHANNEL(Channel));
912   assert_param(IS_SDADC_CONTINUOUS_MODE(ContinuousMode));
913 
914   /* Check SDADC state */
915   if((hsdadc->State != HAL_SDADC_STATE_RESET) && (hsdadc->State != HAL_SDADC_STATE_ERROR))
916   {
917     /* Set JCHG[8:0] bits in SDADC_JCHG */
918     hsdadc->Instance->JCHGR = (uint32_t) (Channel & SDADC_LSB_MASK);
919     /* Set or clear JCONT bit in SDADC_CR2 */
920     if(ContinuousMode == SDADC_CONTINUOUS_CONV_ON)
921     {
922       hsdadc->Instance->CR2 |= SDADC_CR2_JCONT;
923     }
924     else
925     {
926       hsdadc->Instance->CR2 &= ~(SDADC_CR2_JCONT);
927     }
928     /* Store continuous mode information */
929     hsdadc->InjectedContMode = ContinuousMode;
930     /* Store number of injected channels */
931     hsdadc->InjectedChannelsNbr = SDADC_GetInjChannelsNbr(Channel);
932   }
933   else
934   {
935     status = HAL_ERROR;
936   }
937   /* Return function status */
938   return status;
939 }
940 
941 /**
942   * @brief  This function allows to select trigger for regular conversions.
943   * @note   This function should not be called if regular conversion is ongoing.
944   * @param  hsdadc SDADC handle.
945   * @param  Trigger Trigger for regular conversions.
946   *         This parameter can be one of the following value :
947   *            @arg SDADC_SOFTWARE_TRIGGER : Software trigger.
948   *            @arg SDADC_SYNCHRONOUS_TRIGGER : Synchronous with SDADC1 (only for SDADC2 and SDADC3).
949   * @retval HAL status
950   */
HAL_SDADC_SelectRegularTrigger(SDADC_HandleTypeDef * hsdadc,uint32_t Trigger)951 HAL_StatusTypeDef HAL_SDADC_SelectRegularTrigger(SDADC_HandleTypeDef *hsdadc, uint32_t Trigger)
952 {
953   HAL_StatusTypeDef status = HAL_OK;
954 
955   /* Check parameters */
956   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
957   assert_param(IS_SDADC_REGULAR_TRIGGER(Trigger));
958 
959   /* Check parameters compatibility */
960   if((hsdadc->Instance == SDADC1) && (Trigger == SDADC_SYNCHRONOUS_TRIGGER))
961   {
962     status = HAL_ERROR;
963   }
964   /* Check SDADC state */
965   else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
966           (hsdadc->State == HAL_SDADC_STATE_CALIB) || \
967           (hsdadc->State == HAL_SDADC_STATE_INJ))
968   {
969     /* Store regular trigger information */
970     hsdadc->RegularTrigger = Trigger;
971   }
972   else
973   {
974     status = HAL_ERROR;
975   }
976   /* Return function status */
977   return status;
978 }
979 
980 /**
981   * @brief  This function allows to select trigger for injected conversions.
982   * @note   This function should not be called if injected conversion is ongoing.
983   * @param  hsdadc SDADC handle.
984   * @param  Trigger Trigger for injected conversions.
985   *         This parameter can be one of the following value :
986   *            @arg SDADC_SOFTWARE_TRIGGER : Software trigger.
987   *            @arg SDADC_SYNCHRONOUS_TRIGGER : Synchronous with SDADC1 (only for SDADC2 and SDADC3).
988   *            @arg SDADC_EXTERNAL_TRIGGER : External trigger.
989   * @retval HAL status
990   */
HAL_SDADC_SelectInjectedTrigger(SDADC_HandleTypeDef * hsdadc,uint32_t Trigger)991 HAL_StatusTypeDef HAL_SDADC_SelectInjectedTrigger(SDADC_HandleTypeDef *hsdadc, uint32_t Trigger)
992 {
993   HAL_StatusTypeDef status = HAL_OK;
994 
995   /* Check parameters */
996   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
997   assert_param(IS_SDADC_INJECTED_TRIGGER(Trigger));
998 
999   /* Check parameters compatibility */
1000   if((hsdadc->Instance == SDADC1) && (Trigger == SDADC_SYNCHRONOUS_TRIGGER))
1001   {
1002     status = HAL_ERROR;
1003   }
1004   /* Check SDADC state */
1005   else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1006           (hsdadc->State == HAL_SDADC_STATE_CALIB) || \
1007           (hsdadc->State == HAL_SDADC_STATE_REG))
1008   {
1009     /* Store regular trigger information */
1010     hsdadc->InjectedTrigger = Trigger;
1011   }
1012   else
1013   {
1014     status = HAL_ERROR;
1015   }
1016   /* Return function status */
1017   return status;
1018 }
1019 
1020 /**
1021   * @brief  This function allows to select and configure injected external trigger.
1022   * @note   This function should be called only when SDADC instance is in idle state
1023   *         (neither calibration nor regular or injected conversion ongoing)
1024   * @param  hsdadc SDADC handle.
1025   * @param  InjectedExtTrigger External trigger for injected conversions.
1026   *         This parameter can be a value of @ref SDADC_InjectedExtTrigger.
1027   * @param  ExtTriggerEdge Edge of external injected trigger.
1028   *         This parameter can be a value of @ref SDADC_ExtTriggerEdge.
1029   * @retval HAL status
1030   */
HAL_SDADC_SelectInjectedExtTrigger(SDADC_HandleTypeDef * hsdadc,uint32_t InjectedExtTrigger,uint32_t ExtTriggerEdge)1031 HAL_StatusTypeDef HAL_SDADC_SelectInjectedExtTrigger(SDADC_HandleTypeDef *hsdadc,
1032                                                      uint32_t InjectedExtTrigger,
1033                                                      uint32_t ExtTriggerEdge)
1034 {
1035   HAL_StatusTypeDef status = HAL_OK;
1036 
1037   /* Check parameters */
1038   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1039   assert_param(IS_SDADC_EXT_INJEC_TRIG(InjectedExtTrigger));
1040   assert_param(IS_SDADC_EXT_TRIG_EDGE(ExtTriggerEdge));
1041 
1042   /* Check SDADC state */
1043   if(hsdadc->State == HAL_SDADC_STATE_READY)
1044   {
1045     /* Enter init mode */
1046     if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
1047     {
1048       /* Set SDADC in error state */
1049       hsdadc->State = HAL_SDADC_STATE_ERROR;
1050       status = HAL_TIMEOUT;
1051     }
1052     else
1053     {
1054       /* Set JEXTSEL[2:0] bits in SDADC_CR2 register */
1055       hsdadc->Instance->CR2 &= ~(SDADC_CR2_JEXTSEL);
1056       hsdadc->Instance->CR2 |= InjectedExtTrigger;
1057 
1058       /* Store external trigger edge information */
1059       hsdadc->ExtTriggerEdge = ExtTriggerEdge;
1060 
1061       /* Exit init mode */
1062       SDADC_ExitInitMode(hsdadc);
1063     }
1064   }
1065   else
1066   {
1067     status = HAL_ERROR;
1068   }
1069   /* Return function status */
1070   return status;
1071 }
1072 
1073 /**
1074   * @brief  This function allows to enable/disable delay addition for injected conversions.
1075   * @note   This function should be called only when SDADC instance is in idle state
1076   *         (neither calibration nor regular or injected conversion ongoing)
1077   * @param  hsdadc SDADC handle.
1078   * @param  InjectedDelay Enable/disable delay for injected conversions.
1079   *         This parameter can be a value of @ref SDADC_InjectedDelay.
1080   * @retval HAL status
1081   */
HAL_SDADC_SelectInjectedDelay(SDADC_HandleTypeDef * hsdadc,uint32_t InjectedDelay)1082 HAL_StatusTypeDef HAL_SDADC_SelectInjectedDelay(SDADC_HandleTypeDef *hsdadc,
1083                                                 uint32_t InjectedDelay)
1084 {
1085   HAL_StatusTypeDef status = HAL_OK;
1086 
1087   /* Check parameters */
1088   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1089   assert_param(IS_SDADC_INJECTED_DELAY(InjectedDelay));
1090 
1091   /* Check SDADC state */
1092   if(hsdadc->State == HAL_SDADC_STATE_READY)
1093   {
1094     /* Enter init mode */
1095     if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
1096     {
1097       /* Set SDADC in error state */
1098       hsdadc->State = HAL_SDADC_STATE_ERROR;
1099       status = HAL_TIMEOUT;
1100     }
1101     else
1102     {
1103       /* Set JDS bit in SDADC_CR2 register */
1104       hsdadc->Instance->CR2 &= ~(SDADC_CR2_JDS);
1105       hsdadc->Instance->CR2 |= InjectedDelay;
1106 
1107       /* Exit init mode */
1108       SDADC_ExitInitMode(hsdadc);
1109     }
1110   }
1111   else
1112   {
1113     status = HAL_ERROR;
1114   }
1115   /* Return function status */
1116   return status;
1117 }
1118 
1119 /**
1120   * @brief  This function allows to configure multimode for regular conversions.
1121   * @note   This function should not be called if regular conversion is ongoing
1122   *         and should be could only for SDADC1.
1123   * @param  hsdadc SDADC handle.
1124   * @param  MultimodeType Type of multimode for regular conversions.
1125   *         This parameter can be a value of @ref SDADC_MultimodeType.
1126   * @retval HAL status
1127   */
HAL_SDADC_MultiModeConfigChannel(SDADC_HandleTypeDef * hsdadc,uint32_t MultimodeType)1128 HAL_StatusTypeDef HAL_SDADC_MultiModeConfigChannel(SDADC_HandleTypeDef* hsdadc,
1129                                                    uint32_t MultimodeType)
1130 {
1131   HAL_StatusTypeDef status = HAL_OK;
1132 
1133   /* Check parameters */
1134   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1135   assert_param(IS_SDADC_MULTIMODE_TYPE(MultimodeType));
1136 
1137   /* Check instance is SDADC1 */
1138   if(hsdadc->Instance != SDADC1)
1139   {
1140     status = HAL_ERROR;
1141   }
1142   /* Check SDADC state */
1143   else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1144           (hsdadc->State == HAL_SDADC_STATE_CALIB) || \
1145           (hsdadc->State == HAL_SDADC_STATE_INJ))
1146   {
1147     /* Store regular trigger information */
1148     hsdadc->RegularMultimode = MultimodeType;
1149   }
1150   else
1151   {
1152     status = HAL_ERROR;
1153   }
1154   /* Return function status */
1155   return status;
1156 }
1157 
1158 /**
1159   * @brief  This function allows to configure multimode for injected conversions.
1160   * @note   This function should not be called if injected conversion is ongoing
1161   *         and should be could only for SDADC1.
1162   * @param  hsdadc SDADC handle.
1163   * @param  MultimodeType Type of multimode for injected conversions.
1164   *         This parameter can be a value of @ref SDADC_MultimodeType.
1165   * @retval HAL status
1166   */
HAL_SDADC_InjectedMultiModeConfigChannel(SDADC_HandleTypeDef * hsdadc,uint32_t MultimodeType)1167 HAL_StatusTypeDef HAL_SDADC_InjectedMultiModeConfigChannel(SDADC_HandleTypeDef* hsdadc,
1168                                                            uint32_t MultimodeType)
1169 {
1170   HAL_StatusTypeDef status = HAL_OK;
1171 
1172   /* Check parameters */
1173   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1174   assert_param(IS_SDADC_MULTIMODE_TYPE(MultimodeType));
1175 
1176   /* Check instance is SDADC1 */
1177   if(hsdadc->Instance != SDADC1)
1178   {
1179     status = HAL_ERROR;
1180   }
1181   /* Check SDADC state */
1182   else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1183           (hsdadc->State == HAL_SDADC_STATE_CALIB) || \
1184           (hsdadc->State == HAL_SDADC_STATE_REG))
1185   {
1186     /* Store regular trigger information */
1187     hsdadc->InjectedMultimode = MultimodeType;
1188   }
1189   else
1190   {
1191     status = HAL_ERROR;
1192   }
1193   /* Return function status */
1194   return status;
1195 }
1196 
1197 /**
1198   * @}
1199   */
1200 
1201 /** @defgroup SDADC_Exported_Functions_Group3 Input and Output operation functions
1202  *  @brief    IO operation Control functions
1203  *
1204 @verbatim
1205   ===============================================================================
1206               ##### IO operation functions #####
1207   ===============================================================================
1208     [..]  This section provides functions allowing to:
1209       (+) Start calibration.
1210       (+) Poll for the end of calibration.
1211       (+) Start calibration and enable interrupt.
1212       (+) Start conversion of regular/injected channel.
1213       (+) Poll for the end of regular/injected conversion.
1214       (+) Stop conversion of regular/injected channel.
1215       (+) Start conversion of regular/injected channel and enable interrupt.
1216       (+) Stop conversion of regular/injected channel and disable interrupt.
1217       (+) Start conversion of regular/injected channel and enable DMA transfer.
1218       (+) Stop conversion of regular/injected channel and disable DMA transfer.
1219       (+) Start multimode and enable DMA transfer for regular/injected conversion.
1220       (+) Stop multimode and disable DMA transfer for regular/injected conversion..
1221       (+) Get result of regular channel conversion.
1222       (+) Get result of injected channel conversion.
1223       (+) Get result of multimode conversion.
1224       (+) Handle SDADC interrupt request.
1225       (+) Callbacks for calibration and regular/injected conversions.
1226 
1227 @endverbatim
1228   * @{
1229   */
1230 
1231 /**
1232   * @brief  This function allows to start calibration in polling mode.
1233   * @note   This function should be called only when SDADC instance is in idle state
1234   *         (neither calibration nor regular or injected conversion ongoing).
1235   * @param  hsdadc SDADC handle.
1236   * @param  CalibrationSequence Calibration sequence.
1237   *         This parameter can be a value of @ref SDADC_CalibrationSequence.
1238   * @retval HAL status
1239   */
HAL_SDADC_CalibrationStart(SDADC_HandleTypeDef * hsdadc,uint32_t CalibrationSequence)1240 HAL_StatusTypeDef HAL_SDADC_CalibrationStart(SDADC_HandleTypeDef *hsdadc,
1241                                              uint32_t CalibrationSequence)
1242 {
1243   HAL_StatusTypeDef status = HAL_OK;
1244 
1245   /* Check parameters */
1246   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1247   assert_param(IS_SDADC_CALIB_SEQUENCE(CalibrationSequence));
1248 
1249   /* Check SDADC state */
1250   if(hsdadc->State == HAL_SDADC_STATE_READY)
1251   {
1252     /* Enter init mode */
1253     if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
1254     {
1255       /* Set SDADC in error state */
1256       hsdadc->State = HAL_SDADC_STATE_ERROR;
1257       status = HAL_TIMEOUT;
1258     }
1259     else
1260     {
1261       /* Set CALIBCNT[1:0] bits in SDADC_CR2 register */
1262       hsdadc->Instance->CR2 &= ~(SDADC_CR2_CALIBCNT);
1263       hsdadc->Instance->CR2 |= CalibrationSequence;
1264 
1265       /* Exit init mode */
1266       SDADC_ExitInitMode(hsdadc);
1267 
1268       /* Set STARTCALIB in SDADC_CR2 */
1269       hsdadc->Instance->CR2 |= SDADC_CR2_STARTCALIB;
1270 
1271       /* Set SDADC in calibration state */
1272       hsdadc->State = HAL_SDADC_STATE_CALIB;
1273     }
1274   }
1275   else
1276   {
1277     status = HAL_ERROR;
1278   }
1279   /* Return function status */
1280   return status;
1281 }
1282 
1283 /**
1284   * @brief  This function allows to poll for the end of calibration.
1285   * @note   This function should be called only if calibration is ongoing.
1286   * @param  hsdadc SDADC handle.
1287   * @param  Timeout Timeout value in milliseconds.
1288   * @retval HAL status
1289   */
HAL_SDADC_PollForCalibEvent(SDADC_HandleTypeDef * hsdadc,uint32_t Timeout)1290 HAL_StatusTypeDef HAL_SDADC_PollForCalibEvent(SDADC_HandleTypeDef* hsdadc, uint32_t Timeout)
1291 {
1292   uint32_t tickstart;
1293 
1294   /* Check parameters */
1295   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1296 
1297   /* Check SDADC state */
1298   if(hsdadc->State != HAL_SDADC_STATE_CALIB)
1299   {
1300     /* Return error status */
1301     return HAL_ERROR;
1302   }
1303   else
1304   {
1305     /* Get timeout */
1306     tickstart = HAL_GetTick();
1307 
1308     /* Wait EOCALF bit in SDADC_ISR register */
1309     while((hsdadc->Instance->ISR & SDADC_ISR_EOCALF) != SDADC_ISR_EOCALF)
1310     {
1311       /* Check the Timeout */
1312       if(Timeout != HAL_MAX_DELAY)
1313       {
1314         if(((HAL_GetTick()-tickstart) > Timeout) || (Timeout == 0UL))
1315         {
1316           /* Return timeout status */
1317           return HAL_TIMEOUT;
1318         }
1319       }
1320     }
1321     /* Set CLREOCALF bit in SDADC_CLRISR register */
1322     hsdadc->Instance->CLRISR |= SDADC_ISR_CLREOCALF;
1323 
1324     /* Set SDADC in ready state */
1325     hsdadc->State = HAL_SDADC_STATE_READY;
1326 
1327     /* Return function status */
1328     return HAL_OK;
1329   }
1330 }
1331 
1332 /**
1333   * @brief  This function allows to start calibration in interrupt mode.
1334   * @note   This function should be called only when SDADC instance is in idle state
1335   *         (neither calibration nor regular or injected conversion ongoing).
1336   * @param  hsdadc SDADC handle.
1337   * @param  CalibrationSequence Calibration sequence.
1338   *         This parameter can be a value of @ref SDADC_CalibrationSequence.
1339   * @retval HAL status
1340   */
HAL_SDADC_CalibrationStart_IT(SDADC_HandleTypeDef * hsdadc,uint32_t CalibrationSequence)1341 HAL_StatusTypeDef HAL_SDADC_CalibrationStart_IT(SDADC_HandleTypeDef *hsdadc,
1342                                                 uint32_t CalibrationSequence)
1343 {
1344   HAL_StatusTypeDef status = HAL_OK;
1345 
1346   /* Check parameters */
1347   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1348   assert_param(IS_SDADC_CALIB_SEQUENCE(CalibrationSequence));
1349 
1350   /* Check SDADC state */
1351   if(hsdadc->State == HAL_SDADC_STATE_READY)
1352   {
1353     /* Enter init mode */
1354     if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
1355     {
1356       /* Set SDADC in error state */
1357       hsdadc->State = HAL_SDADC_STATE_ERROR;
1358       status = HAL_TIMEOUT;
1359     }
1360     else
1361     {
1362       /* Set CALIBCNT[1:0] bits in SDADC_CR2 register */
1363       hsdadc->Instance->CR2 &= ~(SDADC_CR2_CALIBCNT);
1364       hsdadc->Instance->CR2 |= CalibrationSequence;
1365 
1366       /* Exit init mode */
1367       SDADC_ExitInitMode(hsdadc);
1368 
1369       /* Set EOCALIE bit in SDADC_CR1 register */
1370       hsdadc->Instance->CR1 |= SDADC_CR1_EOCALIE;
1371 
1372       /* Set STARTCALIB in SDADC_CR2 */
1373       hsdadc->Instance->CR2 |= SDADC_CR2_STARTCALIB;
1374 
1375       /* Set SDADC in calibration state */
1376       hsdadc->State = HAL_SDADC_STATE_CALIB;
1377     }
1378   }
1379   else
1380   {
1381     status = HAL_ERROR;
1382   }
1383   /* Return function status */
1384   return status;
1385 }
1386 
1387 /**
1388   * @brief  This function allows to start regular conversion in polling mode.
1389   * @note   This function should be called only when SDADC instance is in idle state
1390   *         or if injected conversion is ongoing.
1391   * @param  hsdadc SDADC handle.
1392   * @retval HAL status
1393   */
HAL_SDADC_Start(SDADC_HandleTypeDef * hsdadc)1394 HAL_StatusTypeDef HAL_SDADC_Start(SDADC_HandleTypeDef *hsdadc)
1395 {
1396   HAL_StatusTypeDef status;
1397 
1398   /* Check parameters */
1399   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1400 
1401   /* Check SDADC state */
1402   if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1403      (hsdadc->State == HAL_SDADC_STATE_INJ))
1404   {
1405     /* Start regular conversion */
1406     status = SDADC_RegConvStart(hsdadc);
1407   }
1408   else
1409   {
1410     status = HAL_ERROR;
1411   }
1412   /* Return function status */
1413   return status;
1414 }
1415 
1416 /**
1417   * @brief  This function allows to poll for the end of regular conversion.
1418   * @note   This function should be called only if regular conversion is ongoing.
1419   * @param  hsdadc SDADC handle.
1420   * @param  Timeout Timeout value in milliseconds.
1421   * @retval HAL status
1422   */
HAL_SDADC_PollForConversion(SDADC_HandleTypeDef * hsdadc,uint32_t Timeout)1423 HAL_StatusTypeDef HAL_SDADC_PollForConversion(SDADC_HandleTypeDef* hsdadc, uint32_t Timeout)
1424 {
1425   uint32_t tickstart;
1426 
1427   /* Check parameters */
1428   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1429 
1430   /* Check SDADC state */
1431   if((hsdadc->State != HAL_SDADC_STATE_REG) && \
1432      (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1433   {
1434     /* Return error status */
1435     return HAL_ERROR;
1436   }
1437   else
1438   {
1439     /* Get timeout */
1440     tickstart = HAL_GetTick();
1441 
1442     /* Wait REOCF bit in SDADC_ISR register */
1443     while((hsdadc->Instance->ISR & SDADC_ISR_REOCF) != SDADC_ISR_REOCF)
1444     {
1445       /* Check the Timeout */
1446       if(Timeout != HAL_MAX_DELAY)
1447       {
1448         if(((HAL_GetTick()-tickstart) > Timeout) || (Timeout == 0UL))
1449         {
1450           /* Return timeout status */
1451           return HAL_TIMEOUT;
1452         }
1453       }
1454     }
1455     /* Check if overrun occurs */
1456     if((hsdadc->Instance->ISR & SDADC_ISR_ROVRF) == SDADC_ISR_ROVRF)
1457     {
1458       /* Update error code and call error callback */
1459       hsdadc->ErrorCode = SDADC_ERROR_REGULAR_OVERRUN;
1460 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
1461       hsdadc->ErrorCallback(hsdadc);
1462 #else
1463       HAL_SDADC_ErrorCallback(hsdadc);
1464 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
1465 
1466       /* Set CLRROVRF bit in SDADC_CLRISR register */
1467       hsdadc->Instance->CLRISR |= SDADC_ISR_CLRROVRF;
1468     }
1469     /* Update SDADC state only if not continuous conversion and SW trigger */
1470     if((hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1471        (hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER))
1472     {
1473       hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_REG) ? \
1474                       HAL_SDADC_STATE_READY : HAL_SDADC_STATE_INJ;
1475     }
1476     /* Return function status */
1477     return HAL_OK;
1478   }
1479 }
1480 
1481 /**
1482   * @brief  This function allows to stop regular conversion in polling mode.
1483   * @note   This function should be called only if regular conversion is ongoing.
1484   * @param  hsdadc SDADC handle.
1485   * @retval HAL status
1486   */
HAL_SDADC_Stop(SDADC_HandleTypeDef * hsdadc)1487 HAL_StatusTypeDef HAL_SDADC_Stop(SDADC_HandleTypeDef *hsdadc)
1488 {
1489   HAL_StatusTypeDef status;
1490 
1491   /* Check parameters */
1492   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1493 
1494   /* Check SDADC state */
1495   if((hsdadc->State != HAL_SDADC_STATE_REG) && \
1496      (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1497   {
1498     /* Return error status */
1499     status = HAL_ERROR;
1500   }
1501   else
1502   {
1503     /* Stop regular conversion */
1504     status = SDADC_RegConvStop(hsdadc);
1505   }
1506   /* Return function status */
1507   return status;
1508 }
1509 
1510 /**
1511   * @brief  This function allows to start regular conversion in interrupt mode.
1512   * @note   This function should be called only when SDADC instance is in idle state
1513   *         or if injected conversion is ongoing.
1514   * @param  hsdadc SDADC handle.
1515   * @retval HAL status
1516   */
HAL_SDADC_Start_IT(SDADC_HandleTypeDef * hsdadc)1517 HAL_StatusTypeDef HAL_SDADC_Start_IT(SDADC_HandleTypeDef *hsdadc)
1518 {
1519   HAL_StatusTypeDef status;
1520 
1521   /* Check parameters */
1522   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1523 
1524   /* Check SDADC state */
1525   if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1526      (hsdadc->State == HAL_SDADC_STATE_INJ))
1527   {
1528     /* Set REOCIE and ROVRIE bits in SDADC_CR1 register */
1529     hsdadc->Instance->CR1 |= (uint32_t) (SDADC_CR1_REOCIE | SDADC_CR1_ROVRIE);
1530 
1531     /* Start regular conversion */
1532     status = SDADC_RegConvStart(hsdadc);
1533   }
1534   else
1535   {
1536     status = HAL_ERROR;
1537   }
1538   /* Return function status */
1539   return status;
1540 }
1541 
1542 /**
1543   * @brief  This function allows to stop regular conversion in interrupt mode.
1544   * @note   This function should be called only if regular conversion is ongoing.
1545   * @param  hsdadc SDADC handle.
1546   * @retval HAL status
1547   */
HAL_SDADC_Stop_IT(SDADC_HandleTypeDef * hsdadc)1548 HAL_StatusTypeDef HAL_SDADC_Stop_IT(SDADC_HandleTypeDef *hsdadc)
1549 {
1550   HAL_StatusTypeDef status;
1551 
1552   /* Check parameters */
1553   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1554 
1555   /* Check SDADC state */
1556   if((hsdadc->State != HAL_SDADC_STATE_REG) && \
1557      (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1558   {
1559     /* Return error status */
1560     status = HAL_ERROR;
1561   }
1562   else
1563   {
1564     /* Clear REOCIE and ROVRIE bits in SDADC_CR1 register */
1565     hsdadc->Instance->CR1 &= (uint32_t) ~(SDADC_CR1_REOCIE | SDADC_CR1_ROVRIE);
1566 
1567     /* Stop regular conversion */
1568     status = SDADC_RegConvStop(hsdadc);
1569   }
1570   /* Return function status */
1571   return status;
1572 }
1573 
1574 /**
1575   * @brief  This function allows to start regular conversion in DMA mode.
1576   * @note   This function should be called only when SDADC instance is in idle state
1577   *         or if injected conversion is ongoing.
1578   * @param  hsdadc SDADC handle.
1579   * @param  pData The destination buffer address.
1580   * @param  Length The length of data to be transferred from SDADC peripheral to memory.
1581   * @retval HAL status
1582   */
HAL_SDADC_Start_DMA(SDADC_HandleTypeDef * hsdadc,uint32_t * pData,uint32_t Length)1583 HAL_StatusTypeDef HAL_SDADC_Start_DMA(SDADC_HandleTypeDef *hsdadc, uint32_t *pData,
1584                                       uint32_t Length)
1585 {
1586   HAL_StatusTypeDef status;
1587 
1588   /* Check parameters */
1589   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1590   assert_param(pData != ((void*) 0));
1591   assert_param(Length != 0UL);
1592 
1593   /* Check that DMA is not enabled for injected conversion */
1594   if((hsdadc->Instance->CR1 & SDADC_CR1_JDMAEN) == SDADC_CR1_JDMAEN)
1595   {
1596     status = HAL_ERROR;
1597   }
1598   /* Check parameters compatibility */
1599   else if((hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER) && \
1600           (hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1601           (hsdadc->hdma->Init.Mode == DMA_NORMAL) && \
1602           (Length != 1U))
1603   {
1604     status = HAL_ERROR;
1605   }
1606   else if((hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER) && \
1607           (hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1608           (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
1609   {
1610     status = HAL_ERROR;
1611   }
1612   /* Check SDADC state */
1613   else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1614           (hsdadc->State == HAL_SDADC_STATE_INJ))
1615   {
1616     /* Set callbacks on DMA handler */
1617     hsdadc->hdma->XferCpltCallback = SDADC_DMARegularConvCplt;
1618     hsdadc->hdma->XferErrorCallback = SDADC_DMAError;
1619     if(hsdadc->hdma->Init.Mode == DMA_CIRCULAR)
1620     {
1621       hsdadc->hdma->XferHalfCpltCallback = SDADC_DMARegularHalfConvCplt;
1622     }
1623 
1624     /* Set RDMAEN bit in SDADC_CR1 register */
1625     hsdadc->Instance->CR1 |= SDADC_CR1_RDMAEN;
1626 
1627     /* Start DMA in interrupt mode */
1628     if(HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->RDATAR, \
1629                         (uint32_t) pData, Length) != HAL_OK)
1630     {
1631       /* Set SDADC in error state */
1632       hsdadc->State = HAL_SDADC_STATE_ERROR;
1633       status = HAL_ERROR;
1634     }
1635     else
1636     {
1637       /* Start regular conversion */
1638       status = SDADC_RegConvStart(hsdadc);
1639     }
1640   }
1641   else
1642   {
1643     status = HAL_ERROR;
1644   }
1645   /* Return function status */
1646   return status;
1647 }
1648 
1649 /**
1650   * @brief  This function allows to stop regular conversion in DMA mode.
1651   * @note   This function should be called only if regular conversion is ongoing.
1652   * @param  hsdadc SDADC handle.
1653   * @retval HAL status
1654   */
HAL_SDADC_Stop_DMA(SDADC_HandleTypeDef * hsdadc)1655 HAL_StatusTypeDef HAL_SDADC_Stop_DMA(SDADC_HandleTypeDef *hsdadc)
1656 {
1657   HAL_StatusTypeDef status;
1658 
1659   /* Check parameters */
1660   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1661 
1662   /* Check SDADC state */
1663   if((hsdadc->State != HAL_SDADC_STATE_REG) && \
1664      (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1665   {
1666     /* Return error status */
1667     status = HAL_ERROR;
1668   }
1669   else
1670   {
1671     /* Clear RDMAEN bit in SDADC_CR1 register */
1672     hsdadc->Instance->CR1 &= ~(SDADC_CR1_RDMAEN);
1673 
1674     /* Stop current DMA transfer */
1675     if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
1676     {
1677       /* Set SDADC in error state */
1678       hsdadc->State = HAL_SDADC_STATE_ERROR;
1679       status = HAL_ERROR;
1680     }
1681     else
1682     {
1683       /* Stop regular conversion */
1684       status = SDADC_RegConvStop(hsdadc);
1685     }
1686   }
1687   /* Return function status */
1688   return status;
1689 }
1690 
1691 /**
1692   * @brief  This function allows to get regular conversion value.
1693   * @param  hsdadc SDADC handle.
1694   * @retval Regular conversion value
1695   */
HAL_SDADC_GetValue(SDADC_HandleTypeDef * hsdadc)1696 uint32_t HAL_SDADC_GetValue(SDADC_HandleTypeDef *hsdadc)
1697 {
1698   /* Check parameters */
1699   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1700 
1701   /* Return regular conversion value */
1702   return hsdadc->Instance->RDATAR;
1703 }
1704 
1705 /**
1706   * @brief  This function allows to start injected conversion in polling mode.
1707   * @note   This function should be called only when SDADC instance is in idle state
1708   *         or if regular conversion is ongoing.
1709   * @param  hsdadc SDADC handle.
1710   * @retval HAL status
1711   */
HAL_SDADC_InjectedStart(SDADC_HandleTypeDef * hsdadc)1712 HAL_StatusTypeDef HAL_SDADC_InjectedStart(SDADC_HandleTypeDef *hsdadc)
1713 {
1714   HAL_StatusTypeDef status;
1715 
1716   /* Check parameters */
1717   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1718 
1719   /* Check SDADC state */
1720   if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1721      (hsdadc->State == HAL_SDADC_STATE_REG))
1722   {
1723     /* Start injected conversion */
1724     status = SDADC_InjConvStart(hsdadc);
1725   }
1726   else
1727   {
1728     status = HAL_ERROR;
1729   }
1730   /* Return function status */
1731   return status;
1732 }
1733 
1734 /**
1735   * @brief  This function allows to poll for the end of injected conversion.
1736   * @note   This function should be called only if injected conversion is ongoing.
1737   * @param  hsdadc SDADC handle.
1738   * @param  Timeout Timeout value in milliseconds.
1739   * @retval HAL status
1740   */
HAL_SDADC_PollForInjectedConversion(SDADC_HandleTypeDef * hsdadc,uint32_t Timeout)1741 HAL_StatusTypeDef HAL_SDADC_PollForInjectedConversion(SDADC_HandleTypeDef* hsdadc,
1742                                                       uint32_t Timeout)
1743 {
1744   uint32_t tickstart;
1745 
1746   /* Check parameters */
1747   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1748 
1749   /* Check SDADC state */
1750   if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
1751      (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1752   {
1753     /* Return error status */
1754     return HAL_ERROR;
1755   }
1756   else
1757   {
1758     /* Get timeout */
1759     tickstart = HAL_GetTick();
1760 
1761     /* Wait JEOCF bit in SDADC_ISR register */
1762     while((hsdadc->Instance->ISR & SDADC_ISR_JEOCF) != SDADC_ISR_JEOCF)
1763     {
1764       /* Check the Timeout */
1765       if(Timeout != HAL_MAX_DELAY)
1766       {
1767         if(((HAL_GetTick()-tickstart) > Timeout) || (Timeout == 0UL))
1768         {
1769           /* Return timeout status */
1770           return HAL_TIMEOUT;
1771         }
1772       }
1773     }
1774     /* Check if overrun occurs */
1775     if((hsdadc->Instance->ISR & SDADC_ISR_JOVRF) == SDADC_ISR_JOVRF)
1776     {
1777       /* Update error code and call error callback */
1778       hsdadc->ErrorCode = SDADC_ERROR_INJECTED_OVERRUN;
1779 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
1780       hsdadc->ErrorCallback(hsdadc);
1781 #else
1782       HAL_SDADC_ErrorCallback(hsdadc);
1783 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
1784 
1785       /* Set CLRJOVRF bit in SDADC_CLRISR register */
1786       hsdadc->Instance->CLRISR |= SDADC_ISR_CLRJOVRF;
1787     }
1788     /* Update remaining injected conversions */
1789     hsdadc->InjConvRemaining--;
1790     if(hsdadc->InjConvRemaining == 0UL)
1791     {
1792       /* end of injected sequence, reset the value */
1793       hsdadc->InjConvRemaining = hsdadc->InjectedChannelsNbr;
1794     }
1795 
1796     /* Update SDADC state only if not continuous conversion, SW trigger */
1797     /* and end of injected sequence */
1798     if((hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1799        (hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
1800        (hsdadc->InjConvRemaining == hsdadc->InjectedChannelsNbr))
1801     {
1802       hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_INJ) ? \
1803                       HAL_SDADC_STATE_READY : HAL_SDADC_STATE_REG;
1804     }
1805     /* Return function status */
1806     return HAL_OK;
1807   }
1808 }
1809 
1810 /**
1811   * @brief  This function allows to stop injected conversion in polling mode.
1812   * @note   This function should be called only if injected conversion is ongoing.
1813   * @param  hsdadc SDADC handle.
1814   * @retval HAL status
1815   */
HAL_SDADC_InjectedStop(SDADC_HandleTypeDef * hsdadc)1816 HAL_StatusTypeDef HAL_SDADC_InjectedStop(SDADC_HandleTypeDef *hsdadc)
1817 {
1818   HAL_StatusTypeDef status;
1819 
1820   /* Check parameters */
1821   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1822 
1823   /* Check SDADC state */
1824   if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
1825      (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1826   {
1827     /* Return error status */
1828     status = HAL_ERROR;
1829   }
1830   else
1831   {
1832     /* Stop injected conversion */
1833     status = SDADC_InjConvStop(hsdadc);
1834   }
1835   /* Return function status */
1836   return status;
1837 }
1838 
1839 /**
1840   * @brief  This function allows to start injected conversion in interrupt mode.
1841   * @note   This function should be called only when SDADC instance is in idle state
1842   *         or if regular conversion is ongoing.
1843   * @param  hsdadc SDADC handle.
1844   * @retval HAL status
1845   */
HAL_SDADC_InjectedStart_IT(SDADC_HandleTypeDef * hsdadc)1846 HAL_StatusTypeDef HAL_SDADC_InjectedStart_IT(SDADC_HandleTypeDef *hsdadc)
1847 {
1848   HAL_StatusTypeDef status;
1849 
1850   /* Check parameters */
1851   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1852 
1853   /* Check SDADC state */
1854   if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1855      (hsdadc->State == HAL_SDADC_STATE_REG))
1856   {
1857     /* Set JEOCIE and JOVRIE bits in SDADC_CR1 register */
1858     hsdadc->Instance->CR1 |= (uint32_t) (SDADC_CR1_JEOCIE | SDADC_CR1_JOVRIE);
1859 
1860     /* Start injected conversion */
1861     status = SDADC_InjConvStart(hsdadc);
1862   }
1863   else
1864   {
1865     status = HAL_ERROR;
1866   }
1867   /* Return function status */
1868   return status;
1869 }
1870 
1871 /**
1872   * @brief  This function allows to stop injected conversion in interrupt mode.
1873   * @note   This function should be called only if injected conversion is ongoing.
1874   * @param  hsdadc SDADC handle.
1875   * @retval HAL status
1876   */
HAL_SDADC_InjectedStop_IT(SDADC_HandleTypeDef * hsdadc)1877 HAL_StatusTypeDef HAL_SDADC_InjectedStop_IT(SDADC_HandleTypeDef *hsdadc)
1878 {
1879   HAL_StatusTypeDef status;
1880 
1881   /* Check parameters */
1882   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1883 
1884   /* Check SDADC state */
1885   if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
1886      (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1887   {
1888     /* Return error status */
1889     status = HAL_ERROR;
1890   }
1891   else
1892   {
1893     /* Clear JEOCIE and JOVRIE bits in SDADC_CR1 register */
1894     hsdadc->Instance->CR1 &= (uint32_t) ~(SDADC_CR1_JEOCIE | SDADC_CR1_JOVRIE);
1895 
1896     /* Stop injected conversion */
1897     status = SDADC_InjConvStop(hsdadc);
1898   }
1899   /* Return function status */
1900   return status;
1901 }
1902 
1903 /**
1904   * @brief  This function allows to start injected conversion in DMA mode.
1905   * @note   This function should be called only when SDADC instance is in idle state
1906   *         or if regular conversion is ongoing.
1907   * @param  hsdadc SDADC handle.
1908   * @param  pData The destination buffer address.
1909   * @param  Length The length of data to be transferred from SDADC peripheral to memory.
1910   * @retval HAL status
1911   */
HAL_SDADC_InjectedStart_DMA(SDADC_HandleTypeDef * hsdadc,uint32_t * pData,uint32_t Length)1912 HAL_StatusTypeDef HAL_SDADC_InjectedStart_DMA(SDADC_HandleTypeDef *hsdadc, uint32_t *pData,
1913                                               uint32_t Length)
1914 {
1915   HAL_StatusTypeDef status;
1916 
1917   /* Check parameters */
1918   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1919   assert_param(pData != ((void*) 0));
1920   assert_param(Length != 0UL);
1921 
1922   /* Check that DMA is not enabled for regular conversion */
1923   if((hsdadc->Instance->CR1 & SDADC_CR1_RDMAEN) == SDADC_CR1_RDMAEN)
1924   {
1925     status = HAL_ERROR;
1926   }
1927   /* Check parameters compatibility */
1928   else if((hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
1929           (hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1930           (hsdadc->hdma->Init.Mode == DMA_NORMAL) && \
1931           (Length > hsdadc->InjectedChannelsNbr))
1932   {
1933     status = HAL_ERROR;
1934   }
1935   else if((hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
1936           (hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1937           (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
1938   {
1939     status = HAL_ERROR;
1940   }
1941   /* Check SDADC state */
1942   else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1943           (hsdadc->State == HAL_SDADC_STATE_REG))
1944   {
1945     /* Set callbacks on DMA handler */
1946     hsdadc->hdma->XferCpltCallback = SDADC_DMAInjectedConvCplt;
1947     hsdadc->hdma->XferErrorCallback = SDADC_DMAError;
1948     if(hsdadc->hdma->Init.Mode == DMA_CIRCULAR)
1949     {
1950       hsdadc->hdma->XferHalfCpltCallback = SDADC_DMAInjectedHalfConvCplt;
1951     }
1952 
1953     /* Set JDMAEN bit in SDADC_CR1 register */
1954     hsdadc->Instance->CR1 |= SDADC_CR1_JDMAEN;
1955 
1956     /* Start DMA in interrupt mode */
1957     if(HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->JDATAR, \
1958                         (uint32_t) pData, Length) != HAL_OK)
1959     {
1960       /* Set SDADC in error state */
1961       hsdadc->State = HAL_SDADC_STATE_ERROR;
1962       status = HAL_ERROR;
1963     }
1964     else
1965     {
1966       /* Start injected conversion */
1967       status = SDADC_InjConvStart(hsdadc);
1968     }
1969   }
1970   else
1971   {
1972     status = HAL_ERROR;
1973   }
1974   /* Return function status */
1975   return status;
1976 }
1977 
1978 /**
1979   * @brief  This function allows to stop injected conversion in DMA mode.
1980   * @note   This function should be called only if injected conversion is ongoing.
1981   * @param  hsdadc SDADC handle.
1982   * @retval HAL status
1983   */
HAL_SDADC_InjectedStop_DMA(SDADC_HandleTypeDef * hsdadc)1984 HAL_StatusTypeDef HAL_SDADC_InjectedStop_DMA(SDADC_HandleTypeDef *hsdadc)
1985 {
1986   HAL_StatusTypeDef status;
1987 
1988   /* Check parameters */
1989   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1990 
1991   /* Check SDADC state */
1992   if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
1993      (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1994   {
1995     /* Return error status */
1996     status = HAL_ERROR;
1997   }
1998   else
1999   {
2000     /* Clear JDMAEN bit in SDADC_CR1 register */
2001     hsdadc->Instance->CR1 &= ~(SDADC_CR1_JDMAEN);
2002 
2003     /* Stop current DMA transfer */
2004     if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
2005     {
2006       /* Set SDADC in error state */
2007       hsdadc->State = HAL_SDADC_STATE_ERROR;
2008       status = HAL_ERROR;
2009     }
2010     else
2011     {
2012       /* Stop injected conversion */
2013       status = SDADC_InjConvStop(hsdadc);
2014     }
2015   }
2016   /* Return function status */
2017   return status;
2018 }
2019 
2020 /**
2021   * @brief  This function allows to get injected conversion value.
2022   * @param  hsdadc SDADC handle.
2023   * @param  Channel Corresponding channel of injected conversion.
2024   * @retval Injected conversion value
2025   */
HAL_SDADC_InjectedGetValue(SDADC_HandleTypeDef * hsdadc,uint32_t * Channel)2026 uint32_t HAL_SDADC_InjectedGetValue(SDADC_HandleTypeDef *hsdadc, uint32_t* Channel)
2027 {
2028   uint32_t value;
2029 
2030   /* Check parameters */
2031   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
2032   assert_param(Channel != ((void*) 0));
2033 
2034   /* Read SDADC_JDATAR register and extract channel and conversion value */
2035   value = hsdadc->Instance->JDATAR;
2036   *Channel = ((value & SDADC_JDATAR_JDATACH) >> SDADC_JDATAR_CH_OFFSET);
2037   value &= SDADC_JDATAR_JDATA;
2038 
2039   /* Return injected conversion value */
2040   return value;
2041 }
2042 
2043 /**
2044   * @brief  This function allows to start multimode regular conversions in DMA mode.
2045   * @note   This function should be called only when SDADC instance is in idle state
2046   *         or if injected conversion is ongoing.
2047   * @param  hsdadc SDADC handle.
2048   * @param  pData The destination buffer address.
2049   * @param  Length The length of data to be transferred from SDADC peripheral to memory.
2050   * @retval HAL status
2051   */
HAL_SDADC_MultiModeStart_DMA(SDADC_HandleTypeDef * hsdadc,uint32_t * pData,uint32_t Length)2052 HAL_StatusTypeDef HAL_SDADC_MultiModeStart_DMA(SDADC_HandleTypeDef* hsdadc, uint32_t* pData,
2053                                                uint32_t Length)
2054 {
2055   HAL_StatusTypeDef status;
2056 
2057   /* Check parameters */
2058   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
2059   assert_param(pData != ((void*) 0));
2060   assert_param(Length != 0UL);
2061 
2062   /* Check instance is SDADC1 */
2063   if(hsdadc->Instance != SDADC1)
2064   {
2065     status = HAL_ERROR;
2066   }
2067   /* Check that DMA is not enabled for injected conversion */
2068   else if((hsdadc->Instance->CR1 & SDADC_CR1_JDMAEN) == SDADC_CR1_JDMAEN)
2069   {
2070     status = HAL_ERROR;
2071   }
2072   /* Check parameters compatibility */
2073   else if((hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER) && \
2074           (hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
2075           (hsdadc->hdma->Init.Mode == DMA_NORMAL) && \
2076           (Length != 1U))
2077   {
2078     status = HAL_ERROR;
2079   }
2080   else if((hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER) && \
2081           (hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
2082           (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
2083   {
2084     status = HAL_ERROR;
2085   }
2086   /* Check SDADC state */
2087   else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
2088           (hsdadc->State == HAL_SDADC_STATE_INJ))
2089   {
2090     /* Set callbacks on DMA handler */
2091     hsdadc->hdma->XferCpltCallback = SDADC_DMARegularConvCplt;
2092     hsdadc->hdma->XferErrorCallback = SDADC_DMAError;
2093     if(hsdadc->hdma->Init.Mode == DMA_CIRCULAR)
2094     {
2095       hsdadc->hdma->XferHalfCpltCallback = SDADC_DMARegularHalfConvCplt;
2096     }
2097     /* Set RDMAEN bit in SDADC_CR1 register */
2098     hsdadc->Instance->CR1 |= SDADC_CR1_RDMAEN;
2099 
2100     /* Start DMA in interrupt mode */
2101     if(hsdadc->RegularMultimode == SDADC_MULTIMODE_SDADC1_SDADC2)
2102     {
2103       status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->RDATA12R, \
2104                                 (uint32_t) pData, Length);
2105     }
2106     else
2107     {
2108       status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->RDATA13R, \
2109                                 (uint32_t) pData, Length);
2110     }
2111     if(status != HAL_OK)
2112     {
2113       /* Set SDADC in error state */
2114       hsdadc->State = HAL_SDADC_STATE_ERROR;
2115       status = HAL_ERROR;
2116     }
2117     else
2118     {
2119       /* Start regular conversion */
2120       status = SDADC_RegConvStart(hsdadc);
2121     }
2122   }
2123   else
2124   {
2125     status = HAL_ERROR;
2126   }
2127   /* Return function status */
2128   return status;
2129 }
2130 
2131 /**
2132   * @brief  This function allows to stop multimode regular conversions in DMA mode.
2133   * @note   This function should be called only if regular conversion is ongoing.
2134   * @param  hsdadc SDADC handle.
2135   * @retval HAL status
2136   */
HAL_SDADC_MultiModeStop_DMA(SDADC_HandleTypeDef * hsdadc)2137 HAL_StatusTypeDef HAL_SDADC_MultiModeStop_DMA(SDADC_HandleTypeDef* hsdadc)
2138 {
2139   HAL_StatusTypeDef status;
2140 
2141   /* Check parameters */
2142   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
2143 
2144   /* Check instance is SDADC1 */
2145   if(hsdadc->Instance != SDADC1)
2146   {
2147     status = HAL_ERROR;
2148   }
2149   /* Check SDADC state */
2150   else if((hsdadc->State != HAL_SDADC_STATE_REG) && \
2151           (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
2152   {
2153     /* Return error status */
2154     status = HAL_ERROR;
2155   }
2156   else
2157   {
2158     /* Clear RDMAEN bit in SDADC_CR1 register */
2159     hsdadc->Instance->CR1 &= ~(SDADC_CR1_RDMAEN);
2160 
2161     /* Stop current DMA transfer */
2162     if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
2163     {
2164       /* Set SDADC in error state */
2165       hsdadc->State = HAL_SDADC_STATE_ERROR;
2166       status = HAL_ERROR;
2167     }
2168     else
2169     {
2170       /* Stop regular conversion */
2171       status = SDADC_RegConvStop(hsdadc);
2172     }
2173   }
2174   /* Return function status */
2175   return status;
2176 }
2177 
2178 /**
2179   * @brief  This function allows to get multimode regular conversion value.
2180   * @param  hsdadc SDADC handle.
2181   * @retval Multimode regular conversion value
2182   */
HAL_SDADC_MultiModeGetValue(SDADC_HandleTypeDef * hsdadc)2183 uint32_t HAL_SDADC_MultiModeGetValue(SDADC_HandleTypeDef* hsdadc)
2184 {
2185   uint32_t value;
2186 
2187   /* Check parameters and check instance is SDADC1 */
2188   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
2189   assert_param(hsdadc->Instance == SDADC1);
2190 
2191   /* read multimode regular value */
2192   value = (hsdadc->RegularMultimode == SDADC_MULTIMODE_SDADC1_SDADC2) ? \
2193           hsdadc->Instance->RDATA12R : hsdadc->Instance->RDATA13R;
2194 
2195   /* Return multimode regular conversions value */
2196   return value;
2197 }
2198 
2199 /**
2200   * @brief  This function allows to start multimode injected conversions in DMA mode.
2201   * @note   This function should be called only when SDADC instance is in idle state
2202   *         or if regular conversion is ongoing.
2203   * @param  hsdadc SDADC handle.
2204   * @param  pData The destination buffer address.
2205   * @param  Length The length of data to be transferred from SDADC peripheral to memory.
2206   * @retval HAL status
2207   */
HAL_SDADC_InjectedMultiModeStart_DMA(SDADC_HandleTypeDef * hsdadc,uint32_t * pData,uint32_t Length)2208 HAL_StatusTypeDef HAL_SDADC_InjectedMultiModeStart_DMA(SDADC_HandleTypeDef* hsdadc,
2209                                                        uint32_t* pData, uint32_t Length)
2210 {
2211   HAL_StatusTypeDef status;
2212 
2213   /* Check parameters */
2214   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
2215   assert_param(pData != ((void*) 0));
2216   assert_param(Length != 0UL);
2217 
2218   /* Check instance is SDADC1 */
2219   if(hsdadc->Instance != SDADC1)
2220   {
2221     status = HAL_ERROR;
2222   }
2223   /* Check that DMA is not enabled for regular conversion */
2224   else if((hsdadc->Instance->CR1 & SDADC_CR1_RDMAEN) == SDADC_CR1_RDMAEN)
2225   {
2226     status = HAL_ERROR;
2227   }
2228   /* Check parameters compatibility */
2229   else if((hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
2230           (hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
2231           (hsdadc->hdma->Init.Mode == DMA_NORMAL) && \
2232           (Length > (hsdadc->InjectedChannelsNbr << 1U)))
2233   {
2234     status = HAL_ERROR;
2235   }
2236   else if((hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
2237           (hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
2238           (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
2239   {
2240     status = HAL_ERROR;
2241   }
2242   /* Check SDADC state */
2243   else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
2244           (hsdadc->State == HAL_SDADC_STATE_REG))
2245   {
2246     /* Set callbacks on DMA handler */
2247     hsdadc->hdma->XferCpltCallback = SDADC_DMAInjectedConvCplt;
2248     hsdadc->hdma->XferErrorCallback = SDADC_DMAError;
2249     if(hsdadc->hdma->Init.Mode == DMA_CIRCULAR)
2250     {
2251       hsdadc->hdma->XferHalfCpltCallback = SDADC_DMAInjectedHalfConvCplt;
2252     }
2253     /* Set JDMAEN bit in SDADC_CR1 register */
2254     hsdadc->Instance->CR1 |= SDADC_CR1_JDMAEN;
2255 
2256     /* Start DMA in interrupt mode */
2257     if(hsdadc->InjectedMultimode == SDADC_MULTIMODE_SDADC1_SDADC2)
2258     {
2259       status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->JDATA12R, \
2260                                 (uint32_t) pData, Length);
2261     }
2262     else
2263     {
2264       status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->JDATA13R, \
2265                                 (uint32_t) pData, Length);
2266     }
2267     if(status != HAL_OK)
2268     {
2269       /* Set SDADC in error state */
2270       hsdadc->State = HAL_SDADC_STATE_ERROR;
2271       status = HAL_ERROR;
2272     }
2273     else
2274     {
2275       /* Start injected conversion */
2276       status = SDADC_InjConvStart(hsdadc);
2277     }
2278   }
2279   else
2280   {
2281     status = HAL_ERROR;
2282   }
2283   /* Return function status */
2284   return status;
2285 }
2286 
2287 /**
2288   * @brief  This function allows to stop multimode injected conversions in DMA mode.
2289   * @note   This function should be called only if injected conversion is ongoing.
2290   * @param  hsdadc SDADC handle.
2291   * @retval HAL status
2292   */
HAL_SDADC_InjectedMultiModeStop_DMA(SDADC_HandleTypeDef * hsdadc)2293 HAL_StatusTypeDef HAL_SDADC_InjectedMultiModeStop_DMA(SDADC_HandleTypeDef* hsdadc)
2294 {
2295   HAL_StatusTypeDef status;
2296 
2297   /* Check parameters */
2298   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
2299 
2300   /* Check instance is SDADC1 */
2301   if(hsdadc->Instance != SDADC1)
2302   {
2303     status = HAL_ERROR;
2304   }
2305   /* Check SDADC state */
2306   else if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
2307           (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
2308   {
2309     /* Return error status */
2310     status = HAL_ERROR;
2311   }
2312   else
2313   {
2314     /* Clear JDMAEN bit in SDADC_CR1 register */
2315     hsdadc->Instance->CR1 &= ~(SDADC_CR1_JDMAEN);
2316 
2317     /* Stop current DMA transfer */
2318     if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
2319     {
2320       /* Set SDADC in error state */
2321       hsdadc->State = HAL_SDADC_STATE_ERROR;
2322       status = HAL_ERROR;
2323     }
2324     else
2325     {
2326       /* Stop injected conversion */
2327       status = SDADC_InjConvStop(hsdadc);
2328     }
2329   }
2330   /* Return function status */
2331   return status;
2332 }
2333 
2334 /**
2335   * @brief  This function allows to get multimode injected conversion value.
2336   * @param  hsdadc SDADC handle.
2337   * @retval Multimode injected conversion value
2338   */
HAL_SDADC_InjectedMultiModeGetValue(SDADC_HandleTypeDef * hsdadc)2339 uint32_t HAL_SDADC_InjectedMultiModeGetValue(SDADC_HandleTypeDef* hsdadc)
2340 {
2341   uint32_t value;
2342 
2343   /* Check parameters and check instance is SDADC1 */
2344   assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
2345   assert_param(hsdadc->Instance == SDADC1);
2346 
2347   /* read multimode injected value */
2348   value = (hsdadc->InjectedMultimode == SDADC_MULTIMODE_SDADC1_SDADC2) ? \
2349           hsdadc->Instance->JDATA12R : hsdadc->Instance->JDATA13R;
2350 
2351   /* Return multimode injected conversions value */
2352   return value;
2353 }
2354 
2355 /**
2356   * @brief  This function handles the SDADC interrupts.
2357   * @param  hsdadc SDADC handle.
2358   * @retval None
2359   */
HAL_SDADC_IRQHandler(SDADC_HandleTypeDef * hsdadc)2360 void HAL_SDADC_IRQHandler(SDADC_HandleTypeDef* hsdadc)
2361 {
2362   uint32_t tmp_isr = hsdadc->Instance->ISR;
2363   uint32_t tmp_cr1 = hsdadc->Instance->CR1;
2364 
2365   /* Check if end of regular conversion */
2366   if(((tmp_cr1 & SDADC_CR1_REOCIE) == SDADC_CR1_REOCIE) &&
2367      ((tmp_isr & SDADC_ISR_REOCF) == SDADC_ISR_REOCF))
2368   {
2369     /* Call regular conversion complete callback */
2370 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
2371     hsdadc->ConvCpltCallback(hsdadc);
2372 #else
2373     HAL_SDADC_ConvCpltCallback(hsdadc);
2374 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
2375 
2376     /* End of conversion if mode is not continuous and software trigger */
2377     if((hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
2378        (hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER))
2379     {
2380       /* Clear REOCIE and ROVRIE bits in SDADC_CR1 register */
2381       hsdadc->Instance->CR1 &= ~(SDADC_CR1_REOCIE | SDADC_CR1_ROVRIE);
2382 
2383       /* Update SDADC state */
2384       hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_REG) ? \
2385                       HAL_SDADC_STATE_READY : HAL_SDADC_STATE_INJ;
2386     }
2387   }
2388   /* Check if end of injected conversion */
2389   else if(((tmp_cr1 & SDADC_CR1_JEOCIE) == SDADC_CR1_JEOCIE) &&
2390           ((tmp_isr & SDADC_ISR_JEOCF) == SDADC_ISR_JEOCF))
2391   {
2392     /* Call injected conversion complete callback */
2393 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
2394     hsdadc->InjectedConvCpltCallback(hsdadc);
2395 #else
2396     HAL_SDADC_InjectedConvCpltCallback(hsdadc);
2397 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
2398 
2399     /* Update remaining injected conversions */
2400     hsdadc->InjConvRemaining--;
2401     if(hsdadc->InjConvRemaining ==0UL)
2402     {
2403       /* end of injected sequence, reset the value */
2404       hsdadc->InjConvRemaining = hsdadc->InjectedChannelsNbr;
2405     }
2406     /* End of conversion if mode is not continuous, software trigger */
2407     /* and end of injected sequence */
2408     if((hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
2409        (hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
2410        (hsdadc->InjConvRemaining == hsdadc->InjectedChannelsNbr))
2411     {
2412       /* Clear JEOCIE and JOVRIE bits in SDADC_CR1 register */
2413       hsdadc->Instance->CR1 &= ~(SDADC_CR1_JEOCIE | SDADC_CR1_JOVRIE);
2414 
2415       /* Update SDADC state */
2416       hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_INJ) ? \
2417                       HAL_SDADC_STATE_READY : HAL_SDADC_STATE_REG;
2418     }
2419   }
2420   /* Check if end of calibration */
2421   else if(((tmp_cr1 & SDADC_CR1_EOCALIE) == SDADC_CR1_EOCALIE) &&
2422           ((tmp_isr & SDADC_ISR_EOCALF) == SDADC_ISR_EOCALF))
2423   {
2424     /* Clear EOCALIE bit in SDADC_CR1 register */
2425     hsdadc->Instance->CR1 &= ~(SDADC_CR1_EOCALIE);
2426 
2427     /* Set CLREOCALF bit in SDADC_CLRISR register */
2428     hsdadc->Instance->CLRISR |= SDADC_ISR_CLREOCALF;
2429 
2430     /* Call calibration callback */
2431 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
2432     hsdadc->CalibrationCpltCallback(hsdadc);
2433 #else
2434     HAL_SDADC_CalibrationCpltCallback(hsdadc);
2435 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
2436 
2437     /* Update SDADC state */
2438     hsdadc->State = HAL_SDADC_STATE_READY;
2439   }
2440   /* Check if overrun occurs during regular conversion */
2441   else if(((tmp_cr1 & SDADC_CR1_ROVRIE) == SDADC_CR1_ROVRIE) &&
2442           ((tmp_isr & SDADC_ISR_ROVRF) == SDADC_ISR_ROVRF))
2443   {
2444     /* Set CLRROVRF bit in SDADC_CLRISR register */
2445     hsdadc->Instance->CLRISR |= SDADC_ISR_CLRROVRF;
2446 
2447     /* Update error code */
2448     hsdadc->ErrorCode = SDADC_ERROR_REGULAR_OVERRUN;
2449 
2450     /* Call error callback */
2451 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
2452     hsdadc->ErrorCallback(hsdadc);
2453 #else
2454     HAL_SDADC_ErrorCallback(hsdadc);
2455 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
2456   }
2457   /* Check if overrun occurs during injected conversion */
2458   else if(((tmp_cr1 & SDADC_CR1_JOVRIE) == SDADC_CR1_JOVRIE) &&
2459           ((tmp_isr & SDADC_ISR_JOVRF) == SDADC_ISR_JOVRF))
2460   {
2461     /* Set CLRJOVRF bit in SDADC_CLRISR register */
2462     hsdadc->Instance->CLRISR |= SDADC_ISR_CLRJOVRF;
2463 
2464     /* Update error code */
2465     hsdadc->ErrorCode = SDADC_ERROR_INJECTED_OVERRUN;
2466 
2467     /* Call error callback */
2468 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
2469     hsdadc->ErrorCallback(hsdadc);
2470 #else
2471     HAL_SDADC_ErrorCallback(hsdadc);
2472 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
2473   }
2474   else
2475   {
2476     /* No additional IRQ source */
2477   }
2478 
2479   return;
2480 }
2481 
2482 /**
2483   * @brief  Calibration complete callback.
2484   * @param  hsdadc SDADC handle.
2485   * @retval None
2486   */
HAL_SDADC_CalibrationCpltCallback(SDADC_HandleTypeDef * hsdadc)2487 __weak void HAL_SDADC_CalibrationCpltCallback(SDADC_HandleTypeDef* hsdadc)
2488 {
2489   /* Prevent unused argument(s) compilation warning */
2490   UNUSED(hsdadc);
2491 
2492   /* NOTE : This function should not be modified, when the callback is needed,
2493             the HAL_SDADC_CalibrationCpltCallback could be implemented in the user file
2494    */
2495 }
2496 
2497 /**
2498   * @brief  Half regular conversion complete callback.
2499   * @param  hsdadc SDADC handle.
2500   * @retval None
2501   */
HAL_SDADC_ConvHalfCpltCallback(SDADC_HandleTypeDef * hsdadc)2502 __weak void HAL_SDADC_ConvHalfCpltCallback(SDADC_HandleTypeDef* hsdadc)
2503 {
2504   /* Prevent unused argument(s) compilation warning */
2505   UNUSED(hsdadc);
2506 
2507   /* NOTE : This function should not be modified, when the callback is needed,
2508             the HAL_SDADC_ConvHalfCpltCallback could be implemented in the user file
2509    */
2510 }
2511 
2512 /**
2513   * @brief  Regular conversion complete callback.
2514   * @note   In interrupt mode, user has to read conversion value in this function
2515             using HAL_SDADC_GetValue or HAL_SDADC_MultiModeGetValue.
2516   * @param  hsdadc SDADC handle.
2517   * @retval None
2518   */
HAL_SDADC_ConvCpltCallback(SDADC_HandleTypeDef * hsdadc)2519 __weak void HAL_SDADC_ConvCpltCallback(SDADC_HandleTypeDef* hsdadc)
2520 {
2521   /* Prevent unused argument(s) compilation warning */
2522   UNUSED(hsdadc);
2523 
2524   /* NOTE : This function should not be modified, when the callback is needed,
2525             the HAL_SDADC_ConvCpltCallback could be implemented in the user file.
2526    */
2527 }
2528 
2529 /**
2530   * @brief  Half injected conversion complete callback.
2531   * @param  hsdadc SDADC handle.
2532   * @retval None
2533   */
HAL_SDADC_InjectedConvHalfCpltCallback(SDADC_HandleTypeDef * hsdadc)2534 __weak void HAL_SDADC_InjectedConvHalfCpltCallback(SDADC_HandleTypeDef* hsdadc)
2535 {
2536   /* Prevent unused argument(s) compilation warning */
2537   UNUSED(hsdadc);
2538 
2539   /* NOTE : This function should not be modified, when the callback is needed,
2540             the HAL_SDADC_InjectedConvHalfCpltCallback could be implemented in the user file.
2541    */
2542 }
2543 
2544 /**
2545   * @brief  Injected conversion complete callback.
2546   * @note   In interrupt mode, user has to read conversion value in this function
2547             using HAL_SDADC_InjectedGetValue or HAL_SDADC_InjectedMultiModeGetValue.
2548   * @param  hsdadc SDADC handle.
2549   * @retval None
2550   */
HAL_SDADC_InjectedConvCpltCallback(SDADC_HandleTypeDef * hsdadc)2551 __weak void HAL_SDADC_InjectedConvCpltCallback(SDADC_HandleTypeDef* hsdadc)
2552 {
2553   /* Prevent unused argument(s) compilation warning */
2554   UNUSED(hsdadc);
2555 
2556   /* NOTE : This function should not be modified, when the callback is needed,
2557             the HAL_SDADC_InjectedConvCpltCallback could be implemented in the user file.
2558    */
2559 }
2560 
2561 /**
2562   * @brief  Error callback.
2563   * @param  hsdadc SDADC handle.
2564   * @retval None
2565   */
HAL_SDADC_ErrorCallback(SDADC_HandleTypeDef * hsdadc)2566 __weak void HAL_SDADC_ErrorCallback(SDADC_HandleTypeDef* hsdadc)
2567 {
2568   /* Prevent unused argument(s) compilation warning */
2569   UNUSED(hsdadc);
2570 
2571   /* NOTE : This function should not be modified, when the callback is needed,
2572             the HAL_SDADC_ErrorCallback could be implemented in the user file.
2573    */
2574 }
2575 
2576 /**
2577   * @brief  DMA half transfer complete callback for regular conversion.
2578   * @param  hdma DMA handle.
2579   * @retval None
2580   */
SDADC_DMARegularHalfConvCplt(DMA_HandleTypeDef * hdma)2581 static void SDADC_DMARegularHalfConvCplt(DMA_HandleTypeDef *hdma)
2582 {
2583   /* Get SDADC handle */
2584   SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2585 
2586   /* Call regular half conversion complete callback */
2587 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
2588   hsdadc->ConvHalfCpltCallback(hsdadc);
2589 #else
2590   HAL_SDADC_ConvHalfCpltCallback(hsdadc);
2591 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
2592 }
2593 
2594 /**
2595   * @brief  DMA transfer complete callback for regular conversion.
2596   * @param  hdma DMA handle.
2597   * @retval None
2598   */
SDADC_DMARegularConvCplt(DMA_HandleTypeDef * hdma)2599 static void SDADC_DMARegularConvCplt(DMA_HandleTypeDef *hdma)
2600 {
2601   /* Get SDADC handle */
2602   SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2603 
2604   /* Call regular conversion complete callback */
2605 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
2606   hsdadc->ConvCpltCallback(hsdadc);
2607 #else
2608   HAL_SDADC_ConvCpltCallback(hsdadc);
2609 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
2610 }
2611 
2612 /**
2613   * @brief  DMA half transfer complete callback for injected conversion.
2614   * @param  hdma DMA handle.
2615   * @retval None
2616   */
SDADC_DMAInjectedHalfConvCplt(DMA_HandleTypeDef * hdma)2617 static void SDADC_DMAInjectedHalfConvCplt(DMA_HandleTypeDef *hdma)
2618 {
2619   /* Get SDADC handle */
2620   SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2621 
2622   /* Call injected half conversion complete callback */
2623 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
2624   hsdadc->InjectedConvHalfCpltCallback(hsdadc);
2625 #else
2626   HAL_SDADC_InjectedConvHalfCpltCallback(hsdadc);
2627 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
2628 }
2629 
2630 /**
2631   * @brief  DMA transfer complete callback for injected conversion.
2632   * @param  hdma DMA handle.
2633   * @retval None
2634   */
SDADC_DMAInjectedConvCplt(DMA_HandleTypeDef * hdma)2635 static void SDADC_DMAInjectedConvCplt(DMA_HandleTypeDef *hdma)
2636 {
2637   /* Get SDADC handle */
2638   SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2639 
2640   /* Call injected conversion complete callback */
2641 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
2642   hsdadc->InjectedConvCpltCallback(hsdadc);
2643 #else
2644   HAL_SDADC_InjectedConvCpltCallback(hsdadc);
2645 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
2646 }
2647 
2648 /**
2649   * @brief  DMA error callback.
2650   * @param  hdma DMA handle.
2651   * @retval None
2652   */
SDADC_DMAError(DMA_HandleTypeDef * hdma)2653 static void SDADC_DMAError(DMA_HandleTypeDef *hdma)
2654 {
2655   /* Get SDADC handle */
2656   SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2657 
2658   /* Update error code */
2659   hsdadc->ErrorCode = SDADC_ERROR_DMA;
2660 
2661   /* Call error callback */
2662 #if (USE_HAL_SDADC_REGISTER_CALLBACKS == 1)
2663   hsdadc->ErrorCallback(hsdadc);
2664 #else
2665   HAL_SDADC_ErrorCallback(hsdadc);
2666 #endif /* USE_HAL_SDADC_REGISTER_CALLBACKS */
2667 }
2668 
2669 /**
2670   * @}
2671   */
2672 
2673 /** @defgroup SDADC_Exported_Functions_Group4 Peripheral State functions
2674  *  @brief   SDADC Peripheral State functions
2675  *
2676 @verbatim
2677   ===============================================================================
2678              ##### ADC Peripheral State functions #####
2679   ===============================================================================
2680     [..] This subsection provides functions allowing to
2681       (+) Get the SDADC state
2682       (+) Get the SDADC Error
2683 
2684 @endverbatim
2685   * @{
2686   */
2687 
2688 /**
2689   * @brief  This function allows to get the current SDADC state.
2690   * @param  hsdadc SDADC handle.
2691   * @retval SDADC state.
2692   */
HAL_SDADC_GetState(SDADC_HandleTypeDef * hsdadc)2693 HAL_SDADC_StateTypeDef HAL_SDADC_GetState(SDADC_HandleTypeDef* hsdadc)
2694 {
2695   return hsdadc->State;
2696 }
2697 
2698 /**
2699   * @brief  This function allows to get the current SDADC error code.
2700   * @param  hsdadc SDADC handle.
2701   * @retval SDADC error code.
2702   */
HAL_SDADC_GetError(SDADC_HandleTypeDef * hsdadc)2703 uint32_t HAL_SDADC_GetError(SDADC_HandleTypeDef* hsdadc)
2704 {
2705   return hsdadc->ErrorCode;
2706 }
2707 
2708 /**
2709   * @}
2710   */
2711 
2712 /** @addtogroup SDADC_Private_Functions SDADC Private Functions
2713   * @{
2714   */
2715 
2716 /**
2717   * @brief  This function allows to enter in init mode for SDADC instance.
2718   * @param  hsdadc SDADC handle.
2719   * @retval HAL status.
2720   */
SDADC_EnterInitMode(SDADC_HandleTypeDef * hsdadc)2721 static HAL_StatusTypeDef SDADC_EnterInitMode(SDADC_HandleTypeDef* hsdadc)
2722 {
2723   uint32_t tickstart;
2724 
2725   /* Set INIT bit on SDADC_CR1 register */
2726   hsdadc->Instance->CR1 |= SDADC_CR1_INIT;
2727 
2728   /* Wait INITRDY bit on SDADC_ISR */
2729   tickstart = HAL_GetTick();
2730   while((hsdadc->Instance->ISR & SDADC_ISR_INITRDY) == (uint32_t)RESET)
2731   {
2732     if((HAL_GetTick()-tickstart) > SDADC_TIMEOUT)
2733     {
2734       return HAL_TIMEOUT;
2735     }
2736   }
2737 
2738   /* Return HAL status */
2739   return HAL_OK;
2740 }
2741 
2742 /**
2743   * @brief  This function allows to exit from init mode for SDADC instance.
2744   * @param  hsdadc SDADC handle.
2745   * @retval None.
2746   */
SDADC_ExitInitMode(SDADC_HandleTypeDef * hsdadc)2747 static void SDADC_ExitInitMode(SDADC_HandleTypeDef* hsdadc)
2748 {
2749   /* Reset INIT bit in SDADC_CR1 register */
2750   hsdadc->Instance->CR1 &= ~(SDADC_CR1_INIT);
2751 }
2752 
2753 /**
2754   * @brief  This function allows to get the number of injected channels.
2755   * @param  Channels bitfield of injected channels.
2756   * @retval Number of injected channels.
2757   */
SDADC_GetInjChannelsNbr(uint32_t Channels)2758 static uint32_t SDADC_GetInjChannelsNbr(uint32_t Channels)
2759 {
2760   uint32_t nbChannels = 0UL;
2761   uint32_t tmp,i;
2762 
2763   /* Get the number of channels from bitfield */
2764   tmp = (uint32_t) (Channels & SDADC_LSB_MASK);
2765   for(i = 0UL ; i < 9UL ; i++)
2766   {
2767     if((tmp & 0x00000001UL) != 0UL)
2768     {
2769       nbChannels++;
2770     }
2771     tmp = (uint32_t) (tmp >> 1UL);
2772   }
2773   return nbChannels;
2774 }
2775 
2776 /**
2777   * @brief  This function allows to really start regular conversion.
2778   * @param  hsdadc SDADC handle.
2779   * @retval HAL status.
2780   */
SDADC_RegConvStart(SDADC_HandleTypeDef * hsdadc)2781 static HAL_StatusTypeDef SDADC_RegConvStart(SDADC_HandleTypeDef* hsdadc)
2782 {
2783   HAL_StatusTypeDef status = HAL_OK;
2784 
2785   /* Check regular trigger */
2786   if(hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER)
2787   {
2788     /* Set RSWSTART bit in SDADC_CR2 register */
2789     hsdadc->Instance->CR2 |= SDADC_CR2_RSWSTART;
2790   }
2791   else /* synchronous trigger */
2792   {
2793     /* Enter init mode */
2794     if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
2795     {
2796       /* Set SDADC in error state */
2797       hsdadc->State = HAL_SDADC_STATE_ERROR;
2798       status = HAL_TIMEOUT;
2799     }
2800     else
2801     {
2802       /* Set RSYNC bit in SDADC_CR1 register */
2803       hsdadc->Instance->CR1 |= SDADC_CR1_RSYNC;
2804 
2805       /* Exit init mode */
2806       SDADC_ExitInitMode(hsdadc);
2807     }
2808   }
2809   /* Update SDADC state only if status is OK */
2810   if(status == HAL_OK)
2811   {
2812     hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_READY) ? \
2813                     HAL_SDADC_STATE_REG : HAL_SDADC_STATE_REG_INJ;
2814   }
2815   /* Return function status */
2816   return status;
2817 }
2818 
2819 /**
2820   * @brief  This function allows to really stop regular conversion.
2821   * @param  hsdadc SDADC handle.
2822   * @retval HAL status.
2823   */
SDADC_RegConvStop(SDADC_HandleTypeDef * hsdadc)2824 static HAL_StatusTypeDef SDADC_RegConvStop(SDADC_HandleTypeDef* hsdadc)
2825 {
2826   uint32_t tickstart;
2827   __IO uint32_t dummy_read_for_register_reset;
2828 
2829   /* Check continuous mode */
2830   if(hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_ON)
2831   {
2832     /* Clear REOCF by reading SDADC_RDATAR register */
2833     dummy_read_for_register_reset = hsdadc->Instance->RDATAR;
2834     UNUSED(dummy_read_for_register_reset);
2835 
2836     /* Clear RCONT bit in SDADC_CR2 register */
2837     hsdadc->Instance->CR2 &= ~(SDADC_CR2_RCONT);
2838   }
2839   /* Wait for the end of regular conversion */
2840   tickstart = HAL_GetTick();
2841   while((hsdadc->Instance->ISR & SDADC_ISR_RCIP) != 0UL)
2842   {
2843     if((HAL_GetTick()-tickstart) > SDADC_TIMEOUT)
2844     {
2845       /* Set SDADC in error state and return timeout status */
2846       hsdadc->State = HAL_SDADC_STATE_ERROR;
2847       return HAL_TIMEOUT;
2848     }
2849   }
2850   /* Check if trigger is synchronous */
2851   if(hsdadc->RegularTrigger == SDADC_SYNCHRONOUS_TRIGGER)
2852   {
2853     /* Enter init mode */
2854     if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
2855     {
2856       /* Set SDADC in error state and return timeout status */
2857       hsdadc->State = HAL_SDADC_STATE_ERROR;
2858       return HAL_TIMEOUT;
2859     }
2860     else
2861     {
2862       /* Clear RSYNC bit in SDADC_CR1 register */
2863       hsdadc->Instance->CR1 &= ~(SDADC_CR1_RSYNC);
2864 
2865       /* Exit init mode */
2866       SDADC_ExitInitMode(hsdadc);
2867     }
2868   }
2869   /* Check if continuous mode */
2870   if(hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_ON)
2871   {
2872     /* Restore RCONT bit in SDADC_CR2 register */
2873     hsdadc->Instance->CR2 |= SDADC_CR2_RCONT;
2874   }
2875   /* Clear REOCF by reading SDADC_RDATAR register */
2876   dummy_read_for_register_reset = hsdadc->Instance->RDATAR;
2877   UNUSED(dummy_read_for_register_reset);
2878 
2879   /* Set CLRROVRF bit in SDADC_CLRISR register */
2880   hsdadc->Instance->CLRISR |= SDADC_ISR_CLRROVRF;
2881 
2882   /* Update SDADC state */
2883   hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_REG) ? \
2884                   HAL_SDADC_STATE_READY : HAL_SDADC_STATE_INJ;
2885 
2886   /* Return function status */
2887   return HAL_OK;
2888 }
2889 
2890 /**
2891   * @brief  This function allows to really start injected conversion.
2892   * @param  hsdadc SDADC handle.
2893   * @retval HAL status.
2894   */
SDADC_InjConvStart(SDADC_HandleTypeDef * hsdadc)2895 static HAL_StatusTypeDef SDADC_InjConvStart(SDADC_HandleTypeDef* hsdadc)
2896 {
2897   HAL_StatusTypeDef status = HAL_OK;
2898 
2899   /* Initialize number of injected conversions remaining */
2900   hsdadc->InjConvRemaining = hsdadc->InjectedChannelsNbr;
2901 
2902   /* Check injected trigger */
2903   if(hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER)
2904   {
2905     /* Set JSWSTART bit in SDADC_CR2 register */
2906     hsdadc->Instance->CR2 |= SDADC_CR2_JSWSTART;
2907   }
2908   else /* external or synchronous trigger */
2909   {
2910     /* Enter init mode */
2911     if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
2912     {
2913       /* Set SDADC in error state */
2914       hsdadc->State = HAL_SDADC_STATE_ERROR;
2915       status = HAL_TIMEOUT;
2916     }
2917     else
2918     {
2919       if(hsdadc->InjectedTrigger == SDADC_SYNCHRONOUS_TRIGGER)
2920       {
2921         /* Set JSYNC bit in SDADC_CR1 register */
2922         hsdadc->Instance->CR1 |= SDADC_CR1_JSYNC;
2923       }
2924       else /* external trigger */
2925       {
2926         /* Set JEXTEN[1:0] bits in SDADC_CR2 register */
2927         hsdadc->Instance->CR2 |= hsdadc->ExtTriggerEdge;
2928       }
2929       /* Exit init mode */
2930       SDADC_ExitInitMode(hsdadc);
2931     }
2932   }
2933   /* Update SDADC state only if status is OK */
2934   if(status == HAL_OK)
2935   {
2936     hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_READY) ? \
2937                     HAL_SDADC_STATE_INJ : HAL_SDADC_STATE_REG_INJ;
2938   }
2939   /* Return function status */
2940   return status;
2941 }
2942 
2943 /**
2944   * @brief  This function allows to really stop injected conversion.
2945   * @param  hsdadc SDADC handle.
2946   * @retval HAL status.
2947   */
SDADC_InjConvStop(SDADC_HandleTypeDef * hsdadc)2948 static HAL_StatusTypeDef SDADC_InjConvStop(SDADC_HandleTypeDef* hsdadc)
2949 {
2950   uint32_t tickstart;
2951   __IO uint32_t dummy_read_for_register_reset;
2952 
2953   /* Check continuous mode */
2954   if(hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_ON)
2955   {
2956     /* Clear JEOCF by reading SDADC_JDATAR register */
2957     dummy_read_for_register_reset =  hsdadc->Instance->JDATAR;
2958     UNUSED(dummy_read_for_register_reset);
2959 
2960     /* Clear JCONT bit in SDADC_CR2 register */
2961     hsdadc->Instance->CR2 &= ~(SDADC_CR2_JCONT);
2962   }
2963   /* Wait for the end of injected conversion */
2964   tickstart = HAL_GetTick();
2965   while((hsdadc->Instance->ISR & SDADC_ISR_JCIP) != 0UL)
2966   {
2967     if((HAL_GetTick()-tickstart) > SDADC_TIMEOUT)
2968     {
2969       /* Set SDADC in error state and return timeout status */
2970       hsdadc->State = HAL_SDADC_STATE_ERROR;
2971       return HAL_TIMEOUT;
2972     }
2973   }
2974   /* Check if trigger is not software */
2975   if(hsdadc->InjectedTrigger != SDADC_SOFTWARE_TRIGGER)
2976   {
2977     /* Enter init mode */
2978     if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
2979     {
2980       /* Set SDADC in error state and return timeout status */
2981       hsdadc->State = HAL_SDADC_STATE_ERROR;
2982       return HAL_TIMEOUT;
2983     }
2984     else
2985     {
2986       /* Check if trigger is synchronous */
2987       if(hsdadc->InjectedTrigger == SDADC_SYNCHRONOUS_TRIGGER)
2988       {
2989         /* Clear JSYNC bit in SDADC_CR1 register */
2990         hsdadc->Instance->CR1 &= ~(SDADC_CR1_JSYNC);
2991       }
2992       else /* external trigger */
2993       {
2994         /* Clear JEXTEN[1:0] bits in SDADC_CR2 register */
2995         hsdadc->Instance->CR2 &= ~(SDADC_CR2_JEXTEN);
2996       }
2997       /* Exit init mode */
2998       SDADC_ExitInitMode(hsdadc);
2999     }
3000   }
3001   /* Check if continuous mode */
3002   if(hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_ON)
3003   {
3004     /* Restore JCONT bit in SDADC_CR2 register */
3005     hsdadc->Instance->CR2 |= SDADC_CR2_JCONT;
3006   }
3007   /* Clear JEOCF by reading SDADC_JDATAR register */
3008   dummy_read_for_register_reset = hsdadc->Instance->JDATAR;
3009   UNUSED(dummy_read_for_register_reset);
3010 
3011   /* Set CLRJOVRF bit in SDADC_CLRISR register */
3012   hsdadc->Instance->CLRISR |= SDADC_ISR_CLRJOVRF;
3013 
3014   /* Update SDADC state */
3015   hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_INJ) ? \
3016                   HAL_SDADC_STATE_READY : HAL_SDADC_STATE_REG;
3017 
3018   /* Return function status */
3019   return HAL_OK;
3020 }
3021 
3022 /**
3023   * @}
3024   */
3025 
3026 /**
3027   * @}
3028   */
3029 
3030 /**
3031   * @}
3032   */
3033 
3034 #endif /* SDADC1 || SDADC2 || SDADC3 */
3035 #endif /* HAL_SDADC_MODULE_ENABLED */
3036 /**
3037   * @}
3038   */
3039