2 ******************************************************************************
3 * @file stm32f3xx_hal_sdadc.c
4 * @author MCD Application Team
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Sigma-Delta Analog to Digital Convertor
10 * + Initialization and Configuration
11 * + Regular Channels Configuration
12 * + Injected channels Configuration
14 * + Regular/Injected Channels DMA Configuration
15 * + Interrupts and flags management
18 ==============================================================================
19 ##### SDADC specific features #####
20 ==============================================================================
22 (#) 16-bit sigma delta architecture.
24 (#) Interrupt generation at the end of calibration, regular/injected conversion
25 and in case of overrun events.
26 (#) Single and continuous conversion modes.
27 (#) External trigger option with configurable polarity for injected conversion.
28 (#) Multi mode (synchronized another SDADC with SDADC1).
29 (#) DMA request generation during regular or injected channel conversion.
31 ##### How to use this driver #####
32 ==============================================================================
34 *** Initialization ***
35 ======================
37 (#) As prerequisite, fill in the HAL_SDADC_MspInit() :
38 (+) Enable SDADCx clock interface with __SDADCx_CLK_ENABLE().
39 (+) Configure SDADCx clock divider with HAL_RCCEx_PeriphCLKConfig.
40 (+) Enable power on SDADC with HAL_PWREx_EnableSDADCAnalog().
41 (+) Enable the clocks for the SDADC GPIOS with __GPIOx_CLK_ENABLE().
42 (+) Configure these SDADC pins in analog mode using HAL_GPIO_Init().
43 (+) If interrupt mode is used, enable and configure SDADC global
44 interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
45 (+) If DMA mode is used, configure DMA with HAL_DMA_Init and link it
46 with SDADC handle using __HAL_LINKDMA.
47 (#) Configure the SDADC low power mode, fast conversion mode, slow clock
48 mode and SDADC1 reference voltage using the HAL_ADC_Init() function.
49 If multiple SDADC are used, please configure first SDADC1 with the
50 common reference voltage.
51 (#) Prepare channel configurations (input mode, common mode, gain and
52 offset) using HAL_SDADC_PrepareChannelConfig and associate channel
53 with one configuration using HAL_SDADC_AssociateChannelConfig.
56 ============================================
58 (#) Start calibration using HAL_SDADC_StartCalibration or
59 HAL_SDADC_CalibrationStart_IT.
60 (#) In polling mode, use HAL_SDADC_PollForCalibEvent to detect the end of
62 (#) In interrupt mode, HAL_SDADC_CalibrationCpltCallback will be called at
63 the end of calibration.
65 *** Regular channel conversion ***
66 ============================================
68 (#) Select trigger for regular conversion using
69 HAL_SDADC_SelectRegularTrigger.
70 (#) Select regular channel and enable/disable continuous mode using
71 HAL_SDADC_ConfigChannel.
72 (#) Start regular conversion using HAL_SDADC_Start, HAL_SDADC_Start_IT
73 or HAL_SDADC_Start_DMA.
74 (#) In polling mode, use HAL_SDADC_PollForConversion to detect the end of
76 (#) In interrupt mode, HAL_SDADC_ConvCpltCallback will be called at the
77 end of regular conversion.
78 (#) Get value of regular conversion using HAL_SDADC_GetValue.
79 (#) In DMA mode, HAL_SDADC_ConvHalfCpltCallback and
80 HAL_SDADC_ConvCpltCallback will be called respectively at the half
81 tranfer and at the tranfer complete.
82 (#) Stop regular conversion using HAL_SDADC_Stop, HAL_SDADC_Stop_IT
83 or HAL_SDADC_Stop_DMA.
85 *** Injected channels conversion ***
86 ============================================
88 (#) Enable/disable delay on injected conversion using
89 HAL_SDADC_SelectInjectedDelay.
90 (#) If external trigger is used for injected conversion, configure this
91 trigger using HAL_SDADC_SelectInjectedExtTrigger.
92 (#) Select trigger for injected conversion using
93 HAL_SDADC_SelectInjectedTrigger.
94 (#) Select injected channels and enable/disable continuous mode using
95 HAL_SDADC_InjectedConfigChannel.
96 (#) Start injected conversion using HAL_SDADC_InjectedStart,
97 HAL_SDADC_InjectedStart_IT or HAL_SDADC_InjectedStart_DMA.
98 (#) In polling mode, use HAL_SDADC_PollForInjectedConversion to detect the
99 end of injected conversion.
100 (#) In interrupt mode, HAL_SDADC_InjectedConvCpltCallback will be called
101 at the end of injected conversion.
102 (#) Get value of injected conversion and corresponding channel using
103 HAL_SDADC_InjectedGetValue.
104 (#) In DMA mode, HAL_SDADC_InjectedConvHalfCpltCallback and
105 HAL_SDADC_InjectedConvCpltCallback will be called respectively at the
106 half tranfer and at the tranfer complete.
107 (#) Stop injected conversion using HAL_SDADC_InjectedStop,
108 HAL_SDADC_InjectedStop_IT or HAL_SDADC_InjectedStop_DMA.
110 *** Multi mode regular channels conversions ***
111 ======================================================
113 (#) Select type of multimode (SDADC1/SDADC2 or SDADC1/SDADC3) using
114 HAL_SDADC_MultiModeConfigChannel.
115 (#) Select software trigger for SDADC1 and synchronized trigger for
116 SDADC2 (or SDADC3) using HAL_SDADC_SelectRegularTrigger.
117 (#) Select regular channel for SDADC1 and SDADC2 (or SDADC3) using
118 HAL_SDADC_ConfigChannel.
119 (#) Start regular conversion for SDADC2 (or SDADC3) with HAL_SDADC_Start.
120 (#) Start regular conversion for SDADC1 using HAL_SDADC_Start,
121 HAL_SDADC_Start_IT or HAL_SDADC_MultiModeStart_DMA.
122 (#) In polling mode, use HAL_SDADC_PollForConversion to detect the end of
123 regular conversion for SDADC1.
124 (#) In interrupt mode, HAL_SDADC_ConvCpltCallback will be called at the
125 end of regular conversion for SDADC1.
126 (#) Get value of regular conversions using HAL_SDADC_MultiModeGetValue.
127 (#) In DMA mode, HAL_SDADC_ConvHalfCpltCallback and
128 HAL_SDADC_ConvCpltCallback will be called respectively at the half
129 tranfer and at the tranfer complete for SDADC1.
130 (#) Stop regular conversion using HAL_SDADC_Stop, HAL_SDADC_Stop_IT
131 or HAL_SDADC_MultiModeStop_DMA for SDADC1.
132 (#) Stop regular conversion using HAL_SDADC_Stop for SDADC2 (or SDADC3).
134 *** Multi mode injected channels conversions ***
135 ======================================================
137 (#) Select type of multimode (SDADC1/SDADC2 or SDADC1/SDADC3) using
138 HAL_SDADC_InjectedMultiModeConfigChannel.
139 (#) Select software or external trigger for SDADC1 and synchronized
140 trigger for SDADC2 (or SDADC3) using HAL_SDADC_SelectInjectedTrigger.
141 (#) Select injected channels for SDADC1 and SDADC2 (or SDADC3) using
142 HAL_SDADC_InjectedConfigChannel.
143 (#) Start injected conversion for SDADC2 (or SDADC3) with
144 HAL_SDADC_InjectedStart.
145 (#) Start injected conversion for SDADC1 using HAL_SDADC_InjectedStart,
146 HAL_SDADC_InjectedStart_IT or HAL_SDADC_InjectedMultiModeStart_DMA.
147 (#) In polling mode, use HAL_SDADC_InjectedPollForConversion to detect
148 the end of injected conversion for SDADC1.
149 (#) In interrupt mode, HAL_SDADC_InjectedConvCpltCallback will be called
150 at the end of injected conversion for SDADC1.
151 (#) Get value of injected conversions using
152 HAL_SDADC_InjectedMultiModeGetValue.
153 (#) In DMA mode, HAL_SDADC_InjectedConvHalfCpltCallback and
154 HAL_SDADC_InjectedConvCpltCallback will be called respectively at the
155 half tranfer and at the tranfer complete for SDADC1.
156 (#) Stop injected conversion using HAL_SDADC_InjectedStop,
157 HAL_SDADC_InjectedStop_IT or HAL_SDADC_InjecteddMultiModeStop_DMA
159 (#) Stop injected conversion using HAL_SDADC_InjectedStop for SDADC2
163 ******************************************************************************
166 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
168 * Redistribution and use in source and binary forms, with or without modification,
169 * are permitted provided that the following conditions are met:
170 * 1. Redistributions of source code must retain the above copyright notice,
171 * this list of conditions and the following disclaimer.
172 * 2. Redistributions in binary form must reproduce the above copyright notice,
173 * this list of conditions and the following disclaimer in the documentation
174 * and/or other materials provided with the distribution.
175 * 3. Neither the name of STMicroelectronics nor the names of its contributors
176 * may be used to endorse or promote products derived from this software
177 * without specific prior written permission.
179 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
180 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
182 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
183 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
184 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
185 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
186 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
187 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
188 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
190 ******************************************************************************
193 /* Includes ------------------------------------------------------------------*/
194 #include "stm32f3xx_hal.h"
196 /** @addtogroup STM32F3xx_HAL_Driver
200 #ifdef HAL_SDADC_MODULE_ENABLED
201 #if defined(STM32F373xC) || defined(STM32F378xx)
202 /** @defgroup SDADC SDADC HAL module driver
203 * @brief SDADC HAL driver modules
207 /* Private typedef -----------------------------------------------------------*/
208 /* Private define ------------------------------------------------------------*/
209 /** @defgroup SDADC_Private_Define SDADC Private Define
212 #define SDADC_TIMEOUT 200
213 #define SDADC_CONFREG_OFFSET 0x00000020
214 #define SDADC_JDATAR_CH_OFFSET 24
215 #define SDADC_MSB_MASK 0xFFFF0000
216 #define SDADC_LSB_MASK 0x0000FFFF
221 /* Private macro -------------------------------------------------------------*/
222 /* Private variables ---------------------------------------------------------*/
223 /* Private function prototypes -----------------------------------------------*/
224 /** @defgroup SDADC_Private_Functions SDADC Private Functions
228 static HAL_StatusTypeDef SDADC_EnterInitMode(SDADC_HandleTypeDef* hsdadc);
229 static void SDADC_ExitInitMode(SDADC_HandleTypeDef* hsdadc);
230 static uint32_t SDADC_GetInjChannelsNbr(uint32_t Channels);
231 static HAL_StatusTypeDef SDADC_RegConvStart(SDADC_HandleTypeDef* hsdadc);
232 static HAL_StatusTypeDef SDADC_RegConvStop(SDADC_HandleTypeDef* hsdadc);
233 static HAL_StatusTypeDef SDADC_InjConvStart(SDADC_HandleTypeDef* hsdadc);
234 static HAL_StatusTypeDef SDADC_InjConvStop(SDADC_HandleTypeDef* hsdadc);
235 static void SDADC_DMARegularHalfConvCplt(DMA_HandleTypeDef *hdma);
236 static void SDADC_DMARegularConvCplt(DMA_HandleTypeDef *hdma);
237 static void SDADC_DMAInjectedHalfConvCplt(DMA_HandleTypeDef *hdma);
238 static void SDADC_DMAInjectedConvCplt(DMA_HandleTypeDef *hdma);
239 static void SDADC_DMAError(DMA_HandleTypeDef *hdma);
244 /* Exported functions ---------------------------------------------------------*/
246 /** @defgroup SDADC_Exported_Functions SDADC Exported Functions
250 /** @defgroup SDADC_Exported_Functions_Group1 Initialization and de-initialization functions
251 * @brief Initialization and de-initialization functions
254 ===============================================================================
255 ##### Initialization and de-initialization functions #####
256 ===============================================================================
257 [..] This section provides functions allowing to:
258 (+) Initialize the SDADC.
259 (+) De-initialize the SDADC.
266 * @brief Initializes the SDADC according to the specified
267 * parameters in the SDADC_InitTypeDef structure.
268 * @note If multiple SDADC are used, please configure first SDADC1 to set
269 * the common reference voltage.
270 * @param hsdadc : SDADC handle.
271 * @retval HAL status.
273 HAL_StatusTypeDef HAL_SDADC_Init(SDADC_HandleTypeDef* hsdadc)
275 /* Check parameters */
276 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
277 assert_param(IS_SDADC_LOWPOWER_MODE(hsdadc->Init.IdleLowPowerMode));
278 assert_param(IS_SDADC_FAST_CONV_MODE(hsdadc->Init.FastConversionMode));
279 assert_param(IS_SDADC_SLOW_CLOCK_MODE(hsdadc->Init.SlowClockMode));
280 assert_param(IS_SDADC_VREF(hsdadc->Init.ReferenceVoltage));
282 /* Check SDADC handle */
283 if(hsdadc == HAL_NULL)
288 /* Initialize SDADC variables with default values */
289 hsdadc->RegularContMode = SDADC_CONTINUOUS_CONV_OFF;
290 hsdadc->InjectedContMode = SDADC_CONTINUOUS_CONV_OFF;
291 hsdadc->InjectedChannelsNbr = 1;
292 hsdadc->InjConvRemaining = 1;
293 hsdadc->RegularTrigger = SDADC_SOFTWARE_TRIGGER;
294 hsdadc->InjectedTrigger = SDADC_SOFTWARE_TRIGGER;
295 hsdadc->ExtTriggerEdge = SDADC_EXT_TRIG_RISING_EDGE;
296 hsdadc->RegularMultimode = SDADC_MULTIMODE_SDADC1_SDADC2;
297 hsdadc->InjectedMultimode = SDADC_MULTIMODE_SDADC1_SDADC2;
298 hsdadc->ErrorCode = SDADC_ERROR_NONE;
300 /* Call MSP init function */
301 HAL_SDADC_MspInit(hsdadc);
303 /* Set idle low power and slow clock modes */
304 hsdadc->Instance->CR1 &= ~(SDADC_CR1_SBI|SDADC_CR1_PDI|SDADC_CR1_SLOWCK);
305 hsdadc->Instance->CR1 |= (hsdadc->Init.IdleLowPowerMode | \
306 hsdadc->Init.SlowClockMode);
308 /* Set fast conversion mode */
309 hsdadc->Instance->CR2 &= ~(SDADC_CR2_FAST);
310 hsdadc->Instance->CR2 |= hsdadc->Init.FastConversionMode;
312 /* Set reference voltage only for SDADC1 */
313 if(hsdadc->Instance == SDADC1)
315 hsdadc->Instance->CR1 &= ~(SDADC_CR1_REFV);
316 hsdadc->Instance->CR1 |= hsdadc->Init.ReferenceVoltage;
318 /* Wait at least 2ms before setting ADON */
323 hsdadc->Instance->CR2 |= SDADC_CR2_ADON;
325 /* Wait end of stabilization */
326 while((hsdadc->Instance->ISR & SDADC_ISR_STABIP) != 0);
328 /* Set SDADC to ready state */
329 hsdadc->State = HAL_SDADC_STATE_READY;
331 /* Return HAL status */
336 * @brief De-initializes the SDADC.
337 * @param hsdadc : SDADC handle.
338 * @retval HAL status.
340 HAL_StatusTypeDef HAL_SDADC_DeInit(SDADC_HandleTypeDef* hsdadc)
342 /* Check parameters */
343 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
345 /* Check SDADC handle */
346 if(hsdadc == HAL_NULL)
351 /* Disable the SDADC */
352 hsdadc->Instance->CR2 &= ~(SDADC_CR2_ADON);
354 /* Reset all registers */
355 hsdadc->Instance->CR1 = 0x00000000;
356 hsdadc->Instance->CR2 = 0x00000000;
357 hsdadc->Instance->JCHGR = 0x00000001;
358 hsdadc->Instance->CONF0R = 0x00000000;
359 hsdadc->Instance->CONF1R = 0x00000000;
360 hsdadc->Instance->CONF2R = 0x00000000;
361 hsdadc->Instance->CONFCHR1 = 0x00000000;
362 hsdadc->Instance->CONFCHR2 = 0x00000000;
364 /* Call MSP deinit function */
365 HAL_SDADC_MspDeInit(hsdadc);
367 /* Set SDADC in reset state */
368 hsdadc->State = HAL_SDADC_STATE_RESET;
370 /* Return function status */
375 * @brief Initializes the SDADC MSP.
376 * @param hsdadc : SDADC handle
379 __weak void HAL_SDADC_MspInit(SDADC_HandleTypeDef* hsdadc)
381 /* NOTE : This function should not be modified, when the callback is needed,
382 the HAL_SDADC_MspInit could be implemented in the user file.
387 * @brief De-initializes the SDADC MSP.
388 * @param hsdadc : SDADC handle
391 __weak void HAL_SDADC_MspDeInit(SDADC_HandleTypeDef* hsdadc)
393 /* NOTE : This function should not be modified, when the callback is needed,
394 the HAL_SDADC_MspDeInit could be implemented in the user file.
402 /** @defgroup SDADC_Exported_Functions_Group2 peripheral control functions
403 * @brief Peripheral control functions
406 ===============================================================================
407 ##### Peripheral control functions #####
408 ===============================================================================
409 [..] This section provides functions allowing to:
410 (+) Program on of the three different configurations for channels.
411 (+) Associate channel to one of configurations.
412 (+) Select regular and injected channels.
413 (+) Enable/disable continuous mode for regular and injected conversions.
414 (+) Select regular and injected triggers.
415 (+) Select and configure injected external trigger.
416 (+) Enable/disable delay addition for injected conversions.
417 (+) Configure multimode.
424 * @brief This function allows the user to set parameters for a configuration.
425 * Parameters are input mode, common mode, gain and offset.
426 * @note This function should be called only when SDADC instance is in idle state
427 * (neither calibration nor regular or injected conversion ongoing)
428 * @param hsdadc : SDADC handle.
429 * @param ConfIndex : Index of configuration to modify.
430 * This parameter can be a value of @ref SDADC_ConfIndex.
431 * @param ConfParamStruct : Parameters to apply for this configuration.
434 HAL_StatusTypeDef HAL_SDADC_PrepareChannelConfig(SDADC_HandleTypeDef *hsdadc,
436 SDADC_ConfParamTypeDef* ConfParamStruct)
438 HAL_StatusTypeDef status = HAL_OK;
441 /* Check parameters */
442 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
443 assert_param(IS_SDADC_CONF_INDEX(ConfIndex));
444 assert_param(ConfParamStruct != HAL_NULL);
445 assert_param(IS_SDADC_INPUT_MODE(ConfParamStruct->InputMode));
446 assert_param(IS_SDADC_GAIN(ConfParamStruct->Gain));
447 assert_param(IS_SDADC_COMMON_MODE(ConfParamStruct->CommonMode));
448 assert_param(IS_SDADC_OFFSET_VALUE(ConfParamStruct->Offset));
450 /* Check SDADC state is ready */
451 if(hsdadc->State != HAL_SDADC_STATE_READY)
457 /* Enter init mode */
458 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
460 /* Set SDADC in error state */
461 hsdadc->State = HAL_SDADC_STATE_ERROR;
462 status = HAL_TIMEOUT;
466 /* Program configuration register with parameters */
467 tmp = (uint32_t)((uint32_t)(hsdadc->Instance) + \
468 SDADC_CONFREG_OFFSET + \
469 (uint32_t)(ConfIndex << 2));
470 *(__IO uint32_t *) (tmp) = (uint32_t) (ConfParamStruct->InputMode | \
471 ConfParamStruct->Gain | \
472 ConfParamStruct->CommonMode | \
473 ConfParamStruct->Offset);
475 SDADC_ExitInitMode(hsdadc);
478 /* Return function status */
483 * @brief This function allows the user to associate a channel with one of the
484 * available configurations.
485 * @note This function should be called only when SDADC instance is in idle state
486 * (neither calibration nor regular or injected conversion ongoing)
487 * @param hsdadc : SDADC handle.
488 * @param Channel : Channel to associate with configuration.
489 * This parameter can be a value of @ref SDADC_Channel_Selection.
490 * @param ConfIndex : Index of configuration to associate with channel.
491 * This parameter can be a value of @ref SDADC_ConfIndex.
494 HAL_StatusTypeDef HAL_SDADC_AssociateChannelConfig(SDADC_HandleTypeDef *hsdadc,
498 HAL_StatusTypeDef status = HAL_OK;
499 uint32_t channelnum = 0;
501 /* Check parameters */
502 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
503 assert_param(IS_SDADC_REGULAR_CHANNEL(Channel));
504 assert_param(IS_SDADC_CONF_INDEX(ConfIndex));
506 /* Check SDADC state is ready */
507 if(hsdadc->State != HAL_SDADC_STATE_READY)
513 /* Enter init mode */
514 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
516 /* Set SDADC in error state */
517 hsdadc->State = HAL_SDADC_STATE_ERROR;
518 status = HAL_TIMEOUT;
522 /* Program channel configuration register according parameters */
523 if(Channel != SDADC_CHANNEL_8)
525 /* Get channel number */
526 channelnum = (uint32_t)(Channel>>16);
528 /* Set the channel configuration */
529 hsdadc->Instance->CONFCHR1 &= (uint32_t) ~(SDADC_CONFCHR1_CONFCH0 << (channelnum << 2));
530 hsdadc->Instance->CONFCHR1 |= (uint32_t) (ConfIndex << (channelnum << 2));
534 hsdadc->Instance->CONFCHR2 = (uint32_t) (ConfIndex);
537 SDADC_ExitInitMode(hsdadc);
540 /* Return function status */
545 * @brief This function allows to select channel for regular conversion and
546 * to enable/disable continuous mode for regular conversion.
547 * @param hsdadc : SDADC handle.
548 * @param Channel : Channel for regular conversion.
549 * This parameter can be a value of @ref SDADC_Channel_Selection.
550 * @param ContinuousMode : Enable/disable continuous mode for regular conversion.
551 * This parameter can be a value of @ref SDADC_ContinuousMode.
554 HAL_StatusTypeDef HAL_SDADC_ConfigChannel(SDADC_HandleTypeDef *hsdadc,
556 uint32_t ContinuousMode)
558 HAL_StatusTypeDef status = HAL_OK;
560 /* Check parameters */
561 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
562 assert_param(IS_SDADC_REGULAR_CHANNEL(Channel));
563 assert_param(IS_SDADC_CONTINUOUS_MODE(ContinuousMode));
565 /* Check SDADC state */
566 if((hsdadc->State != HAL_SDADC_STATE_RESET) && (hsdadc->State != HAL_SDADC_STATE_ERROR))
568 /* Set RCH[3:0] and RCONT bits in SDADC_CR2 */
569 hsdadc->Instance->CR2 &= (uint32_t) ~(SDADC_CR2_RCH | SDADC_CR2_RCONT);
570 if(ContinuousMode == SDADC_CONTINUOUS_CONV_ON)
572 hsdadc->Instance->CR2 |= (uint32_t) ((Channel & SDADC_MSB_MASK) | SDADC_CR2_RCONT);
576 hsdadc->Instance->CR2 |= (uint32_t) ((Channel & SDADC_MSB_MASK));
578 /* Store continuous mode information */
579 hsdadc->RegularContMode = ContinuousMode;
585 /* Return function status */
590 * @brief This function allows to select channels for injected conversion and
591 * to enable/disable continuous mode for injected conversion.
592 * @param hsdadc : SDADC handle.
593 * @param Channel : Channels for injected conversion.
594 * This parameter can be a values combination of @ref SDADC_Channel_Selection.
595 * @param ContinuousMode : Enable/disable continuous mode for injected conversion.
596 * This parameter can be a value of @ref SDADC_ContinuousMode.
599 HAL_StatusTypeDef HAL_SDADC_InjectedConfigChannel(SDADC_HandleTypeDef *hsdadc,
601 uint32_t ContinuousMode)
603 HAL_StatusTypeDef status = HAL_OK;
605 /* Check parameters */
606 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
607 assert_param(IS_SDADC_INJECTED_CHANNEL(Channel));
608 assert_param(IS_SDADC_CONTINUOUS_MODE(ContinuousMode));
610 /* Check SDADC state */
611 if((hsdadc->State != HAL_SDADC_STATE_RESET) && (hsdadc->State != HAL_SDADC_STATE_ERROR))
613 /* Set JCHG[8:0] bits in SDADC_JCHG */
614 hsdadc->Instance->JCHGR = (uint32_t) (Channel & SDADC_LSB_MASK);
615 /* Set or clear JCONT bit in SDADC_CR2 */
616 if(ContinuousMode == SDADC_CONTINUOUS_CONV_ON)
618 hsdadc->Instance->CR2 |= SDADC_CR2_JCONT;
622 hsdadc->Instance->CR2 &= ~(SDADC_CR2_JCONT);
624 /* Store continuous mode information */
625 hsdadc->InjectedContMode = ContinuousMode;
626 /* Store number of injected channels */
627 hsdadc->InjectedChannelsNbr = SDADC_GetInjChannelsNbr(Channel);
633 /* Return function status */
638 * @brief This function allows to select trigger for regular conversions.
639 * @note This function should not be called if regular conversion is ongoing.
640 * @param hsdadc : SDADC handle.
641 * @param Trigger : Trigger for regular conversions.
642 * This parameter can be one of the following value :
643 * @arg SDADC_SOFTWARE_TRIGGER : Software trigger.
644 * @arg SDADC_SYNCHRONOUS_TRIGGER : Synchronous with SDADC1 (only for SDADC2 and SDADC3).
647 HAL_StatusTypeDef HAL_SDADC_SelectRegularTrigger(SDADC_HandleTypeDef *hsdadc, uint32_t Trigger)
649 HAL_StatusTypeDef status = HAL_OK;
651 /* Check parameters */
652 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
653 assert_param(IS_SDADC_REGULAR_TRIGGER(Trigger));
655 /* Check parameters compatibility */
656 if((hsdadc->Instance == SDADC1) && (Trigger == SDADC_SYNCHRONOUS_TRIGGER))
660 /* Check SDADC state */
661 else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
662 (hsdadc->State == HAL_SDADC_STATE_CALIB) || \
663 (hsdadc->State == HAL_SDADC_STATE_INJ))
665 /* Store regular trigger information */
666 hsdadc->RegularTrigger = Trigger;
672 /* Return function status */
677 * @brief This function allows to select trigger for injected conversions.
678 * @note This function should not be called if injected conversion is ongoing.
679 * @param hsdadc : SDADC handle.
680 * @param Trigger : Trigger for injected conversions.
681 * This parameter can be one of the following value :
682 * @arg SDADC_SOFTWARE_TRIGGER : Software trigger.
683 * @arg SDADC_SYNCHRONOUS_TRIGGER : Synchronous with SDADC1 (only for SDADC2 and SDADC3).
684 * @arg SDADC_EXTERNAL_TRIGGER : External trigger.
687 HAL_StatusTypeDef HAL_SDADC_SelectInjectedTrigger(SDADC_HandleTypeDef *hsdadc, uint32_t Trigger)
689 HAL_StatusTypeDef status = HAL_OK;
691 /* Check parameters */
692 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
693 assert_param(IS_SDADC_INJECTED_TRIGGER(Trigger));
695 /* Check parameters compatibility */
696 if((hsdadc->Instance == SDADC1) && (Trigger == SDADC_SYNCHRONOUS_TRIGGER))
700 /* Check SDADC state */
701 else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
702 (hsdadc->State == HAL_SDADC_STATE_CALIB) || \
703 (hsdadc->State == HAL_SDADC_STATE_REG))
705 /* Store regular trigger information */
706 hsdadc->InjectedTrigger = Trigger;
712 /* Return function status */
717 * @brief This function allows to select and configure injected external trigger.
718 * @note This function should be called only when SDADC instance is in idle state
719 * (neither calibration nor regular or injected conversion ongoing)
720 * @param hsdadc : SDADC handle.
721 * @param InjectedExtTrigger : External trigger for injected conversions.
722 * This parameter can be a value of @ref SDADC_InjectedExtTrigger.
723 * @param ExtTriggerEdge : Edge of external injected trigger.
724 * This parameter can be a value of @ref SDADC_ExtTriggerEdge.
727 HAL_StatusTypeDef HAL_SDADC_SelectInjectedExtTrigger(SDADC_HandleTypeDef *hsdadc,
728 uint32_t InjectedExtTrigger,
729 uint32_t ExtTriggerEdge)
731 HAL_StatusTypeDef status = HAL_OK;
733 /* Check parameters */
734 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
735 assert_param(IS_SDADC_EXT_INJEC_TRIG(InjectedExtTrigger));
736 assert_param(IS_SDADC_EXT_TRIG_EDGE(ExtTriggerEdge));
738 /* Check SDADC state */
739 if(hsdadc->State == HAL_SDADC_STATE_READY)
741 /* Enter init mode */
742 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
744 /* Set SDADC in error state */
745 hsdadc->State = HAL_SDADC_STATE_ERROR;
746 status = HAL_TIMEOUT;
750 /* Set JEXTSEL[2:0] bits in SDADC_CR2 register */
751 hsdadc->Instance->CR2 &= ~(SDADC_CR2_JEXTSEL);
752 hsdadc->Instance->CR2 |= InjectedExtTrigger;
754 /* Store external trigger edge information */
755 hsdadc->ExtTriggerEdge = ExtTriggerEdge;
758 SDADC_ExitInitMode(hsdadc);
765 /* Return function status */
770 * @brief This function allows to enable/disable delay addition for injected conversions.
771 * @note This function should be called only when SDADC instance is in idle state
772 * (neither calibration nor regular or injected conversion ongoing)
773 * @param hsdadc : SDADC handle.
774 * @param InjectedDelay : Enable/disable delay for injected conversions.
775 * This parameter can be a value of @ref SDADC_InjectedDelay.
778 HAL_StatusTypeDef HAL_SDADC_SelectInjectedDelay(SDADC_HandleTypeDef *hsdadc,
779 uint32_t InjectedDelay)
781 HAL_StatusTypeDef status = HAL_OK;
783 /* Check parameters */
784 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
785 assert_param(IS_SDADC_INJECTED_DELAY(InjectedDelay));
787 /* Check SDADC state */
788 if(hsdadc->State == HAL_SDADC_STATE_READY)
790 /* Enter init mode */
791 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
793 /* Set SDADC in error state */
794 hsdadc->State = HAL_SDADC_STATE_ERROR;
795 status = HAL_TIMEOUT;
799 /* Set JDS bit in SDADC_CR2 register */
800 hsdadc->Instance->CR2 &= ~(SDADC_CR2_JDS);
801 hsdadc->Instance->CR2 |= InjectedDelay;
804 SDADC_ExitInitMode(hsdadc);
811 /* Return function status */
816 * @brief This function allows to configure multimode for regular conversions.
817 * @note This function should not be called if regular conversion is ongoing
818 * and should be could only for SDADC1.
819 * @param hsdadc : SDADC handle.
820 * @param MultimodeType : Type of multimode for regular conversions.
821 * This parameter can be a value of @ref SDADC_MultimodeType.
824 HAL_StatusTypeDef HAL_SDADC_MultiModeConfigChannel(SDADC_HandleTypeDef* hsdadc,
825 uint32_t MultimodeType)
827 HAL_StatusTypeDef status = HAL_OK;
829 /* Check parameters */
830 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
831 assert_param(IS_SDADC_MULTIMODE_TYPE(MultimodeType));
833 /* Check instance is SDADC1 */
834 if(hsdadc->Instance != SDADC1)
838 /* Check SDADC state */
839 else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
840 (hsdadc->State == HAL_SDADC_STATE_CALIB) || \
841 (hsdadc->State == HAL_SDADC_STATE_INJ))
843 /* Store regular trigger information */
844 hsdadc->RegularMultimode = MultimodeType;
850 /* Return function status */
855 * @brief This function allows to configure multimode for injected conversions.
856 * @note This function should not be called if injected conversion is ongoing
857 * and should be could only for SDADC1.
858 * @param hsdadc : SDADC handle.
859 * @param MultimodeType : Type of multimode for injected conversions.
860 * This parameter can be a value of @ref SDADC_MultimodeType.
863 HAL_StatusTypeDef HAL_SDADC_InjectedMultiModeConfigChannel(SDADC_HandleTypeDef* hsdadc,
864 uint32_t MultimodeType)
866 HAL_StatusTypeDef status = HAL_OK;
868 /* Check parameters */
869 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
870 assert_param(IS_SDADC_MULTIMODE_TYPE(MultimodeType));
872 /* Check instance is SDADC1 */
873 if(hsdadc->Instance != SDADC1)
877 /* Check SDADC state */
878 else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
879 (hsdadc->State == HAL_SDADC_STATE_CALIB) || \
880 (hsdadc->State == HAL_SDADC_STATE_REG))
882 /* Store regular trigger information */
883 hsdadc->InjectedMultimode = MultimodeType;
889 /* Return function status */
897 /** @defgroup SDADC_Exported_Functions_Group3 Input and Output operation functions
898 * @brief I/O operation Control functions
901 ===============================================================================
902 ##### I/O operation functions #####
903 ===============================================================================
904 [..] This section provides functions allowing to:
905 (+) Start calibration.
906 (+) Poll for the end of calibration.
907 (+) Start calibration and enable interrupt.
908 (+) Start conversion of regular/injected channel.
909 (+) Poll for the end of regular/injected conversion.
910 (+) Stop conversion of regular/injected channel.
911 (+) Start conversion of regular/injected channel and enable interrupt.
912 (+) Stop conversion of regular/injected channel and disable interrupt.
913 (+) Start conversion of regular/injected channel and enable DMA transfer.
914 (+) Stop conversion of regular/injected channel and disable DMA transfer.
915 (+) Start multimode and enable DMA transfer for regular/injected conversion.
916 (+) Stop multimode and disable DMA transfer for regular/injected conversion..
917 (+) Get result of regular channel conversion.
918 (+) Get result of injected channel conversion.
919 (+) Get result of multimode conversion.
920 (+) Handle SDADC interrupt request.
921 (+) Callbacks for calibration and regular/injected conversions.
928 * @brief This function allows to start calibration in polling mode.
929 * @note This function should be called only when SDADC instance is in idle state
930 * (neither calibration nor regular or injected conversion ongoing).
931 * @param hsdadc : SDADC handle.
932 * @param CalibrationSequence : Calibration sequence.
933 * This parameter can be a value of @ref SDADC_CalibrationSequence.
936 HAL_StatusTypeDef HAL_SDADC_CalibrationStart(SDADC_HandleTypeDef *hsdadc,
937 uint32_t CalibrationSequence)
939 HAL_StatusTypeDef status = HAL_OK;
941 /* Check parameters */
942 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
943 assert_param(IS_SDADC_CALIB_SEQUENCE(CalibrationSequence));
945 /* Check SDADC state */
946 if(hsdadc->State == HAL_SDADC_STATE_READY)
948 /* Enter init mode */
949 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
951 /* Set SDADC in error state */
952 hsdadc->State = HAL_SDADC_STATE_ERROR;
953 status = HAL_TIMEOUT;
957 /* Set CALIBCNT[1:0] bits in SDADC_CR2 register */
958 hsdadc->Instance->CR2 &= ~(SDADC_CR2_CALIBCNT);
959 hsdadc->Instance->CR2 |= CalibrationSequence;
962 SDADC_ExitInitMode(hsdadc);
964 /* Set STARTCALIB in SDADC_CR2 */
965 hsdadc->Instance->CR2 |= SDADC_CR2_STARTCALIB;
967 /* Set SDADC in calibration state */
968 hsdadc->State = HAL_SDADC_STATE_CALIB;
975 /* Return function status */
980 * @brief This function allows to poll for the end of calibration.
981 * @note This function should be called only if calibration is ongoing.
982 * @param hsdadc : SDADC handle.
983 * @param Timeout : Timeout value in milliseconds.
986 HAL_StatusTypeDef HAL_SDADC_PollForCalibEvent(SDADC_HandleTypeDef* hsdadc, uint32_t Timeout)
990 /* Check parameters */
991 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
993 /* Check SDADC state */
994 if(hsdadc->State != HAL_SDADC_STATE_CALIB)
996 /* Return error status */
1002 tickstart = HAL_GetTick();
1004 /* Wait EOCALF bit in SDADC_ISR register */
1005 while((hsdadc->Instance->ISR & SDADC_ISR_EOCALF) != SDADC_ISR_EOCALF)
1007 /* Check the Timeout */
1008 if(Timeout != HAL_MAX_DELAY)
1010 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
1012 /* Return timeout status */
1017 /* Set CLREOCALF bit in SDADC_CLRISR register */
1018 hsdadc->Instance->CLRISR |= SDADC_ISR_CLREOCALF;
1020 /* Set SDADC in ready state */
1021 hsdadc->State = HAL_SDADC_STATE_READY;
1023 /* Return function status */
1029 * @brief This function allows to start calibration in interrupt mode.
1030 * @note This function should be called only when SDADC instance is in idle state
1031 * (neither calibration nor regular or injected conversion ongoing).
1032 * @param hsdadc : SDADC handle.
1033 * @param CalibrationSequence : Calibration sequence.
1034 * This parameter can be a value of @ref SDADC_CalibrationSequence.
1035 * @retval HAL status
1037 HAL_StatusTypeDef HAL_SDADC_CalibrationStart_IT(SDADC_HandleTypeDef *hsdadc,
1038 uint32_t CalibrationSequence)
1040 HAL_StatusTypeDef status = HAL_OK;
1042 /* Check parameters */
1043 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1044 assert_param(IS_SDADC_CALIB_SEQUENCE(CalibrationSequence));
1046 /* Check SDADC state */
1047 if(hsdadc->State == HAL_SDADC_STATE_READY)
1049 /* Enter init mode */
1050 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
1052 /* Set SDADC in error state */
1053 hsdadc->State = HAL_SDADC_STATE_ERROR;
1054 status = HAL_TIMEOUT;
1058 /* Set CALIBCNT[1:0] bits in SDADC_CR2 register */
1059 hsdadc->Instance->CR2 &= ~(SDADC_CR2_CALIBCNT);
1060 hsdadc->Instance->CR2 |= CalibrationSequence;
1062 /* Exit init mode */
1063 SDADC_ExitInitMode(hsdadc);
1065 /* Set EOCALIE bit in SDADC_CR1 register */
1066 hsdadc->Instance->CR1 |= SDADC_CR1_EOCALIE;
1068 /* Set STARTCALIB in SDADC_CR2 */
1069 hsdadc->Instance->CR2 |= SDADC_CR2_STARTCALIB;
1071 /* Set SDADC in calibration state */
1072 hsdadc->State = HAL_SDADC_STATE_CALIB;
1079 /* Return function status */
1084 * @brief This function allows to start regular conversion in polling mode.
1085 * @note This function should be called only when SDADC instance is in idle state
1086 * or if injected conversion is ongoing.
1087 * @param hsdadc : SDADC handle.
1088 * @retval HAL status
1090 HAL_StatusTypeDef HAL_SDADC_Start(SDADC_HandleTypeDef *hsdadc)
1092 HAL_StatusTypeDef status = HAL_OK;
1094 /* Check parameters */
1095 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1097 /* Check SDADC state */
1098 if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1099 (hsdadc->State == HAL_SDADC_STATE_INJ))
1101 /* Start regular conversion */
1102 status = SDADC_RegConvStart(hsdadc);
1108 /* Return function status */
1113 * @brief This function allows to poll for the end of regular conversion.
1114 * @note This function should be called only if regular conversion is ongoing.
1115 * @param hsdadc : SDADC handle.
1116 * @param Timeout : Timeout value in milliseconds.
1117 * @retval HAL status
1119 HAL_StatusTypeDef HAL_SDADC_PollForConversion(SDADC_HandleTypeDef* hsdadc, uint32_t Timeout)
1123 /* Check parameters */
1124 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1126 /* Check SDADC state */
1127 if((hsdadc->State != HAL_SDADC_STATE_REG) && \
1128 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1130 /* Return error status */
1136 tickstart = HAL_GetTick();
1138 /* Wait REOCF bit in SDADC_ISR register */
1139 while((hsdadc->Instance->ISR & SDADC_ISR_REOCF) != SDADC_ISR_REOCF)
1141 /* Check the Timeout */
1142 if(Timeout != HAL_MAX_DELAY)
1144 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
1146 /* Return timeout status */
1151 /* Check if overrun occurs */
1152 if((hsdadc->Instance->ISR & SDADC_ISR_ROVRF) == SDADC_ISR_ROVRF)
1154 /* Update error code and call error callback */
1155 hsdadc->ErrorCode = SDADC_ERROR_REGULAR_OVERRUN;
1156 HAL_SDADC_ErrorCallback(hsdadc);
1158 /* Set CLRROVRF bit in SDADC_CLRISR register */
1159 hsdadc->Instance->CLRISR |= SDADC_ISR_CLRROVRF;
1161 /* Update SDADC state only if not continuous conversion and SW trigger */
1162 if((hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1163 (hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER))
1165 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_REG) ? \
1166 HAL_SDADC_STATE_READY : HAL_SDADC_STATE_INJ;
1168 /* Return function status */
1174 * @brief This function allows to stop regular conversion in polling mode.
1175 * @note This function should be called only if regular conversion is ongoing.
1176 * @param hsdadc : SDADC handle.
1177 * @retval HAL status
1179 HAL_StatusTypeDef HAL_SDADC_Stop(SDADC_HandleTypeDef *hsdadc)
1181 HAL_StatusTypeDef status = HAL_OK;
1183 /* Check parameters */
1184 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1186 /* Check SDADC state */
1187 if((hsdadc->State != HAL_SDADC_STATE_REG) && \
1188 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1190 /* Return error status */
1195 /* Stop regular conversion */
1196 status = SDADC_RegConvStop(hsdadc);
1198 /* Return function status */
1203 * @brief This function allows to start regular conversion in interrupt mode.
1204 * @note This function should be called only when SDADC instance is in idle state
1205 * or if injected conversion is ongoing.
1206 * @param hsdadc : SDADC handle.
1207 * @retval HAL status
1209 HAL_StatusTypeDef HAL_SDADC_Start_IT(SDADC_HandleTypeDef *hsdadc)
1211 HAL_StatusTypeDef status = HAL_OK;
1213 /* Check parameters */
1214 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1216 /* Check SDADC state */
1217 if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1218 (hsdadc->State == HAL_SDADC_STATE_INJ))
1220 /* Set REOCIE and ROVRIE bits in SDADC_CR1 register */
1221 hsdadc->Instance->CR1 |= (uint32_t) (SDADC_CR1_REOCIE | SDADC_CR1_ROVRIE);
1223 /* Start regular conversion */
1224 status = SDADC_RegConvStart(hsdadc);
1230 /* Return function status */
1235 * @brief This function allows to stop regular conversion in interrupt mode.
1236 * @note This function should be called only if regular conversion is ongoing.
1237 * @param hsdadc : SDADC handle.
1238 * @retval HAL status
1240 HAL_StatusTypeDef HAL_SDADC_Stop_IT(SDADC_HandleTypeDef *hsdadc)
1242 HAL_StatusTypeDef status = HAL_OK;
1244 /* Check parameters */
1245 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1247 /* Check SDADC state */
1248 if((hsdadc->State != HAL_SDADC_STATE_REG) && \
1249 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1251 /* Return error status */
1256 /* Clear REOCIE and ROVRIE bits in SDADC_CR1 register */
1257 hsdadc->Instance->CR1 &= (uint32_t) ~(SDADC_CR1_REOCIE | SDADC_CR1_ROVRIE);
1259 /* Stop regular conversion */
1260 status = SDADC_RegConvStop(hsdadc);
1262 /* Return function status */
1267 * @brief This function allows to start regular conversion in DMA mode.
1268 * @note This function should be called only when SDADC instance is in idle state
1269 * or if injected conversion is ongoing.
1270 * @param hsdadc : SDADC handle.
1271 * @param pData : The destination buffer address.
1272 * @param Length : The length of data to be transferred from SDADC peripheral to memory.
1273 * @retval HAL status
1275 HAL_StatusTypeDef HAL_SDADC_Start_DMA(SDADC_HandleTypeDef *hsdadc, uint32_t *pData,
1278 HAL_StatusTypeDef status = HAL_OK;
1280 /* Check parameters */
1281 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1282 assert_param(pData != HAL_NULL);
1283 assert_param(Length != 0);
1285 /* Check that DMA is not enabled for injected conversion */
1286 if((hsdadc->Instance->CR1 & SDADC_CR1_JDMAEN) == SDADC_CR1_JDMAEN)
1290 /* Check parameters compatibility */
1291 else if((hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER) && \
1292 (hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1293 (hsdadc->hdma->Init.Mode == DMA_NORMAL) && \
1298 else if((hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER) && \
1299 (hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1300 (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
1304 /* Check SDADC state */
1305 else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1306 (hsdadc->State == HAL_SDADC_STATE_INJ))
1308 /* Set callbacks on DMA handler */
1309 hsdadc->hdma->XferCpltCallback = SDADC_DMARegularConvCplt;
1310 hsdadc->hdma->XferErrorCallback = SDADC_DMAError;
1311 if(hsdadc->hdma->Init.Mode == DMA_CIRCULAR)
1313 hsdadc->hdma->XferHalfCpltCallback = SDADC_DMARegularHalfConvCplt;
1316 /* Set RDMAEN bit in SDADC_CR1 register */
1317 hsdadc->Instance->CR1 |= SDADC_CR1_RDMAEN;
1319 /* Start DMA in interrupt mode */
1320 if(HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->RDATAR, \
1321 (uint32_t) pData, Length) != HAL_OK)
1323 /* Set SDADC in error state */
1324 hsdadc->State = HAL_SDADC_STATE_ERROR;
1329 /* Start regular conversion */
1330 status = SDADC_RegConvStart(hsdadc);
1337 /* Return function status */
1342 * @brief This function allows to stop regular conversion in DMA mode.
1343 * @note This function should be called only if regular conversion is ongoing.
1344 * @param hsdadc : SDADC handle.
1345 * @retval HAL status
1347 HAL_StatusTypeDef HAL_SDADC_Stop_DMA(SDADC_HandleTypeDef *hsdadc)
1349 HAL_StatusTypeDef status = HAL_OK;
1351 /* Check parameters */
1352 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1354 /* Check SDADC state */
1355 if((hsdadc->State != HAL_SDADC_STATE_REG) && \
1356 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1358 /* Return error status */
1363 /* Clear RDMAEN bit in SDADC_CR1 register */
1364 hsdadc->Instance->CR1 &= ~(SDADC_CR1_RDMAEN);
1366 /* Stop current DMA transfer */
1367 if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
1369 /* Set SDADC in error state */
1370 hsdadc->State = HAL_SDADC_STATE_ERROR;
1375 /* Stop regular conversion */
1376 status = SDADC_RegConvStop(hsdadc);
1379 /* Return function status */
1384 * @brief This function allows to get regular conversion value.
1385 * @param hsdadc : SDADC handle.
1386 * @retval Regular conversion value
1388 uint32_t HAL_SDADC_GetValue(SDADC_HandleTypeDef *hsdadc)
1390 /* Check parameters */
1391 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1393 /* Return regular conversion value */
1394 return hsdadc->Instance->RDATAR;
1398 * @brief This function allows to start injected conversion in polling mode.
1399 * @note This function should be called only when SDADC instance is in idle state
1400 * or if regular conversion is ongoing.
1401 * @param hsdadc : SDADC handle.
1402 * @retval HAL status
1404 HAL_StatusTypeDef HAL_SDADC_InjectedStart(SDADC_HandleTypeDef *hsdadc)
1406 HAL_StatusTypeDef status = HAL_OK;
1408 /* Check parameters */
1409 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1411 /* Check SDADC state */
1412 if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1413 (hsdadc->State == HAL_SDADC_STATE_REG))
1415 /* Start injected conversion */
1416 status = SDADC_InjConvStart(hsdadc);
1422 /* Return function status */
1427 * @brief This function allows to poll for the end of injected conversion.
1428 * @note This function should be called only if injected conversion is ongoing.
1429 * @param hsdadc : SDADC handle.
1430 * @param Timeout : Timeout value in milliseconds.
1431 * @retval HAL status
1433 HAL_StatusTypeDef HAL_SDADC_PollForInjectedConversion(SDADC_HandleTypeDef* hsdadc,
1438 /* Check parameters */
1439 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1441 /* Check SDADC state */
1442 if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
1443 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1445 /* Return error status */
1451 tickstart = HAL_GetTick();
1453 /* Wait JEOCF bit in SDADC_ISR register */
1454 while((hsdadc->Instance->ISR & SDADC_ISR_JEOCF) != SDADC_ISR_JEOCF)
1456 /* Check the Timeout */
1457 if(Timeout != HAL_MAX_DELAY)
1459 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
1461 /* Return timeout status */
1466 /* Check if overrun occurs */
1467 if((hsdadc->Instance->ISR & SDADC_ISR_JOVRF) == SDADC_ISR_JOVRF)
1469 /* Update error code and call error callback */
1470 hsdadc->ErrorCode = SDADC_ERROR_INJECTED_OVERRUN;
1471 HAL_SDADC_ErrorCallback(hsdadc);
1473 /* Set CLRJOVRF bit in SDADC_CLRISR register */
1474 hsdadc->Instance->CLRISR |= SDADC_ISR_CLRJOVRF;
1476 /* Update remaining injected conversions */
1477 hsdadc->InjConvRemaining--;
1478 if(hsdadc->InjConvRemaining == 0)
1480 /* end of injected sequence, reset the value */
1481 hsdadc->InjConvRemaining = hsdadc->InjectedChannelsNbr;
1484 /* Update SDADC state only if not continuous conversion, SW trigger */
1485 /* and end of injected sequence */
1486 if((hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1487 (hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
1488 (hsdadc->InjConvRemaining == hsdadc->InjectedChannelsNbr))
1490 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_INJ) ? \
1491 HAL_SDADC_STATE_READY : HAL_SDADC_STATE_REG;
1493 /* Return function status */
1499 * @brief This function allows to stop injected conversion in polling mode.
1500 * @note This function should be called only if injected conversion is ongoing.
1501 * @param hsdadc : SDADC handle.
1502 * @retval HAL status
1504 HAL_StatusTypeDef HAL_SDADC_InjectedStop(SDADC_HandleTypeDef *hsdadc)
1506 HAL_StatusTypeDef status = HAL_OK;
1508 /* Check parameters */
1509 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1511 /* Check SDADC state */
1512 if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
1513 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1515 /* Return error status */
1520 /* Stop injected conversion */
1521 status = SDADC_InjConvStop(hsdadc);
1523 /* Return function status */
1528 * @brief This function allows to start injected conversion in interrupt mode.
1529 * @note This function should be called only when SDADC instance is in idle state
1530 * or if regular conversion is ongoing.
1531 * @param hsdadc : SDADC handle.
1532 * @retval HAL status
1534 HAL_StatusTypeDef HAL_SDADC_InjectedStart_IT(SDADC_HandleTypeDef *hsdadc)
1536 HAL_StatusTypeDef status = HAL_OK;
1538 /* Check parameters */
1539 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1541 /* Check SDADC state */
1542 if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1543 (hsdadc->State == HAL_SDADC_STATE_REG))
1545 /* Set JEOCIE and JOVRIE bits in SDADC_CR1 register */
1546 hsdadc->Instance->CR1 |= (uint32_t) (SDADC_CR1_JEOCIE | SDADC_CR1_JOVRIE);
1548 /* Start injected conversion */
1549 status = SDADC_InjConvStart(hsdadc);
1555 /* Return function status */
1560 * @brief This function allows to stop injected conversion in interrupt mode.
1561 * @note This function should be called only if injected conversion is ongoing.
1562 * @param hsdadc : SDADC handle.
1563 * @retval HAL status
1565 HAL_StatusTypeDef HAL_SDADC_InjectedStop_IT(SDADC_HandleTypeDef *hsdadc)
1567 HAL_StatusTypeDef status = HAL_OK;
1569 /* Check parameters */
1570 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1572 /* Check SDADC state */
1573 if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
1574 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1576 /* Return error status */
1581 /* Clear JEOCIE and JOVRIE bits in SDADC_CR1 register */
1582 hsdadc->Instance->CR1 &= (uint32_t) ~(SDADC_CR1_JEOCIE | SDADC_CR1_JOVRIE);
1584 /* Stop injected conversion */
1585 status = SDADC_InjConvStop(hsdadc);
1587 /* Return function status */
1592 * @brief This function allows to start injected conversion in DMA mode.
1593 * @note This function should be called only when SDADC instance is in idle state
1594 * or if regular conversion is ongoing.
1595 * @param hsdadc : SDADC handle.
1596 * @param pData : The destination buffer address.
1597 * @param Length : The length of data to be transferred from SDADC peripheral to memory.
1598 * @retval HAL status
1600 HAL_StatusTypeDef HAL_SDADC_InjectedStart_DMA(SDADC_HandleTypeDef *hsdadc, uint32_t *pData,
1603 HAL_StatusTypeDef status = HAL_OK;
1605 /* Check parameters */
1606 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1607 assert_param(pData != HAL_NULL);
1608 assert_param(Length != 0);
1610 /* Check that DMA is not enabled for regular conversion */
1611 if((hsdadc->Instance->CR1 & SDADC_CR1_RDMAEN) == SDADC_CR1_RDMAEN)
1615 /* Check parameters compatibility */
1616 else if((hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
1617 (hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1618 (hsdadc->hdma->Init.Mode == DMA_NORMAL) && \
1619 (Length > hsdadc->InjectedChannelsNbr))
1623 else if((hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
1624 (hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1625 (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
1629 /* Check SDADC state */
1630 else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1631 (hsdadc->State == HAL_SDADC_STATE_REG))
1633 /* Set callbacks on DMA handler */
1634 hsdadc->hdma->XferCpltCallback = SDADC_DMAInjectedConvCplt;
1635 hsdadc->hdma->XferErrorCallback = SDADC_DMAError;
1636 if(hsdadc->hdma->Init.Mode == DMA_CIRCULAR)
1638 hsdadc->hdma->XferHalfCpltCallback = SDADC_DMAInjectedHalfConvCplt;
1641 /* Set JDMAEN bit in SDADC_CR1 register */
1642 hsdadc->Instance->CR1 |= SDADC_CR1_JDMAEN;
1644 /* Start DMA in interrupt mode */
1645 if(HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->JDATAR, \
1646 (uint32_t) pData, Length) != HAL_OK)
1648 /* Set SDADC in error state */
1649 hsdadc->State = HAL_SDADC_STATE_ERROR;
1654 /* Start injected conversion */
1655 status = SDADC_InjConvStart(hsdadc);
1662 /* Return function status */
1667 * @brief This function allows to stop injected conversion in DMA mode.
1668 * @note This function should be called only if injected conversion is ongoing.
1669 * @param hsdadc : SDADC handle.
1670 * @retval HAL status
1672 HAL_StatusTypeDef HAL_SDADC_InjectedStop_DMA(SDADC_HandleTypeDef *hsdadc)
1674 HAL_StatusTypeDef status = HAL_OK;
1676 /* Check parameters */
1677 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1679 /* Check SDADC state */
1680 if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
1681 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1683 /* Return error status */
1688 /* Clear JDMAEN bit in SDADC_CR1 register */
1689 hsdadc->Instance->CR1 &= ~(SDADC_CR1_JDMAEN);
1691 /* Stop current DMA transfer */
1692 if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
1694 /* Set SDADC in error state */
1695 hsdadc->State = HAL_SDADC_STATE_ERROR;
1700 /* Stop injected conversion */
1701 status = SDADC_InjConvStop(hsdadc);
1704 /* Return function status */
1709 * @brief This function allows to get injected conversion value.
1710 * @param hsdadc : SDADC handle.
1711 * @param Channel : Corresponding channel of injected conversion.
1712 * @retval Injected conversion value
1714 uint32_t HAL_SDADC_InjectedGetValue(SDADC_HandleTypeDef *hsdadc, uint32_t* Channel)
1718 /* Check parameters */
1719 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1720 assert_param(Channel != HAL_NULL);
1722 /* Read SDADC_JDATAR register and extract channel and conversion value */
1723 value = hsdadc->Instance->JDATAR;
1724 *Channel = ((value & SDADC_JDATAR_JDATACH) >> SDADC_JDATAR_CH_OFFSET);
1725 value &= SDADC_JDATAR_JDATA;
1727 /* Return injected conversion value */
1732 * @brief This function allows to start multimode regular conversions in DMA mode.
1733 * @note This function should be called only when SDADC instance is in idle state
1734 * or if injected conversion is ongoing.
1735 * @param hsdadc : SDADC handle.
1736 * @param pData : The destination buffer address.
1737 * @param Length : The length of data to be transferred from SDADC peripheral to memory.
1738 * @retval HAL status
1740 HAL_StatusTypeDef HAL_SDADC_MultiModeStart_DMA(SDADC_HandleTypeDef* hsdadc, uint32_t* pData,
1743 HAL_StatusTypeDef status = HAL_OK;
1745 /* Check parameters */
1746 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1747 assert_param(pData != HAL_NULL);
1748 assert_param(Length != 0);
1750 /* Check instance is SDADC1 */
1751 if(hsdadc->Instance != SDADC1)
1755 /* Check that DMA is not enabled for injected conversion */
1756 else if((hsdadc->Instance->CR1 & SDADC_CR1_JDMAEN) == SDADC_CR1_JDMAEN)
1760 /* Check parameters compatibility */
1761 else if((hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER) && \
1762 (hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1763 (hsdadc->hdma->Init.Mode == DMA_NORMAL) && \
1768 else if((hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER) && \
1769 (hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1770 (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
1774 /* Check SDADC state */
1775 else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1776 (hsdadc->State == HAL_SDADC_STATE_INJ))
1778 /* Set callbacks on DMA handler */
1779 hsdadc->hdma->XferCpltCallback = SDADC_DMARegularConvCplt;
1780 hsdadc->hdma->XferErrorCallback = SDADC_DMAError;
1781 if(hsdadc->hdma->Init.Mode == DMA_CIRCULAR)
1783 hsdadc->hdma->XferHalfCpltCallback = SDADC_DMARegularHalfConvCplt;
1785 /* Set RDMAEN bit in SDADC_CR1 register */
1786 hsdadc->Instance->CR1 |= SDADC_CR1_RDMAEN;
1788 /* Start DMA in interrupt mode */
1789 if(hsdadc->RegularMultimode == SDADC_MULTIMODE_SDADC1_SDADC2)
1791 status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->RDATA12R, \
1792 (uint32_t) pData, Length);
1796 status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->RDATA13R, \
1797 (uint32_t) pData, Length);
1799 if(status != HAL_OK)
1801 /* Set SDADC in error state */
1802 hsdadc->State = HAL_SDADC_STATE_ERROR;
1807 /* Start regular conversion */
1808 status = SDADC_RegConvStart(hsdadc);
1815 /* Return function status */
1820 * @brief This function allows to stop multimode regular conversions in DMA mode.
1821 * @note This function should be called only if regular conversion is ongoing.
1822 * @param hsdadc : SDADC handle.
1823 * @retval HAL status
1825 HAL_StatusTypeDef HAL_SDADC_MultiModeStop_DMA(SDADC_HandleTypeDef* hsdadc)
1827 HAL_StatusTypeDef status = HAL_OK;
1829 /* Check parameters */
1830 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1832 /* Check instance is SDADC1 */
1833 if(hsdadc->Instance != SDADC1)
1837 /* Check SDADC state */
1838 else if((hsdadc->State != HAL_SDADC_STATE_REG) && \
1839 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1841 /* Return error status */
1846 /* Clear RDMAEN bit in SDADC_CR1 register */
1847 hsdadc->Instance->CR1 &= ~(SDADC_CR1_RDMAEN);
1849 /* Stop current DMA transfer */
1850 if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
1852 /* Set SDADC in error state */
1853 hsdadc->State = HAL_SDADC_STATE_ERROR;
1858 /* Stop regular conversion */
1859 status = SDADC_RegConvStop(hsdadc);
1862 /* Return function status */
1867 * @brief This function allows to get multimode regular conversion value.
1868 * @param hsdadc : SDADC handle.
1869 * @retval Multimode regular conversion value
1871 uint32_t HAL_SDADC_MultiModeGetValue(SDADC_HandleTypeDef* hsdadc)
1875 /* Check parameters and check instance is SDADC1 */
1876 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1877 assert_param(hsdadc->Instance == SDADC1);
1879 /* read multimode regular value */
1880 value = (hsdadc->RegularMultimode == SDADC_MULTIMODE_SDADC1_SDADC2) ? \
1881 hsdadc->Instance->RDATA12R : hsdadc->Instance->RDATA13R;
1883 /* Return multimode regular conversions value */
1888 * @brief This function allows to start multimode injected conversions in DMA mode.
1889 * @note This function should be called only when SDADC instance is in idle state
1890 * or if regular conversion is ongoing.
1891 * @param hsdadc : SDADC handle.
1892 * @param pData : The destination buffer address.
1893 * @param Length : The length of data to be transferred from SDADC peripheral to memory.
1894 * @retval HAL status
1896 HAL_StatusTypeDef HAL_SDADC_InjectedMultiModeStart_DMA(SDADC_HandleTypeDef* hsdadc,
1897 uint32_t* pData, uint32_t Length)
1899 HAL_StatusTypeDef status = HAL_OK;
1901 /* Check parameters */
1902 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1903 assert_param(pData != HAL_NULL);
1904 assert_param(Length != 0);
1906 /* Check instance is SDADC1 */
1907 if(hsdadc->Instance != SDADC1)
1911 /* Check that DMA is not enabled for regular conversion */
1912 else if((hsdadc->Instance->CR1 & SDADC_CR1_RDMAEN) == SDADC_CR1_RDMAEN)
1916 /* Check parameters compatibility */
1917 else if((hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
1918 (hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1919 (hsdadc->hdma->Init.Mode == DMA_NORMAL) && \
1920 (Length > (hsdadc->InjectedChannelsNbr << 1)))
1924 else if((hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
1925 (hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1926 (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
1930 /* Check SDADC state */
1931 else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1932 (hsdadc->State == HAL_SDADC_STATE_REG))
1934 /* Set callbacks on DMA handler */
1935 hsdadc->hdma->XferCpltCallback = SDADC_DMAInjectedConvCplt;
1936 hsdadc->hdma->XferErrorCallback = SDADC_DMAError;
1937 if(hsdadc->hdma->Init.Mode == DMA_CIRCULAR)
1939 hsdadc->hdma->XferHalfCpltCallback = SDADC_DMAInjectedHalfConvCplt;
1941 /* Set JDMAEN bit in SDADC_CR1 register */
1942 hsdadc->Instance->CR1 |= SDADC_CR1_JDMAEN;
1944 /* Start DMA in interrupt mode */
1945 if(hsdadc->InjectedMultimode == SDADC_MULTIMODE_SDADC1_SDADC2)
1947 status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->JDATA12R, \
1948 (uint32_t) pData, Length);
1952 status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->JDATA13R, \
1953 (uint32_t) pData, Length);
1955 if(status != HAL_OK)
1957 /* Set SDADC in error state */
1958 hsdadc->State = HAL_SDADC_STATE_ERROR;
1963 /* Start injected conversion */
1964 status = SDADC_InjConvStart(hsdadc);
1971 /* Return function status */
1976 * @brief This function allows to stop multimode injected conversions in DMA mode.
1977 * @note This function should be called only if injected conversion is ongoing.
1978 * @param hsdadc : SDADC handle.
1979 * @retval HAL status
1981 HAL_StatusTypeDef HAL_SDADC_InjectedMultiModeStop_DMA(SDADC_HandleTypeDef* hsdadc)
1983 HAL_StatusTypeDef status = HAL_OK;
1985 /* Check parameters */
1986 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1988 /* Check instance is SDADC1 */
1989 if(hsdadc->Instance != SDADC1)
1993 /* Check SDADC state */
1994 else if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
1995 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1997 /* Return error status */
2002 /* Clear JDMAEN bit in SDADC_CR1 register */
2003 hsdadc->Instance->CR1 &= ~(SDADC_CR1_JDMAEN);
2005 /* Stop current DMA transfer */
2006 if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
2008 /* Set SDADC in error state */
2009 hsdadc->State = HAL_SDADC_STATE_ERROR;
2014 /* Stop injected conversion */
2015 status = SDADC_InjConvStop(hsdadc);
2018 /* Return function status */
2023 * @brief This function allows to get multimode injected conversion value.
2024 * @param hsdadc : SDADC handle.
2025 * @retval Multimode injected conversion value
2027 uint32_t HAL_SDADC_InjectedMultiModeGetValue(SDADC_HandleTypeDef* hsdadc)
2031 /* Check parameters and check instance is SDADC1 */
2032 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
2033 assert_param(hsdadc->Instance == SDADC1);
2035 /* read multimode injected value */
2036 value = (hsdadc->InjectedMultimode == SDADC_MULTIMODE_SDADC1_SDADC2) ? \
2037 hsdadc->Instance->JDATA12R : hsdadc->Instance->JDATA13R;
2039 /* Return multimode injected conversions value */
2044 * @brief This function handles the SDADC interrupts.
2045 * @param hsdadc : SDADC handle.
2048 void HAL_SDADC_IRQHandler(SDADC_HandleTypeDef* hsdadc)
2050 /* Check if end of regular conversion */
2051 if(((hsdadc->Instance->ISR & SDADC_ISR_REOCF) == SDADC_ISR_REOCF) && \
2052 ((hsdadc->Instance->CR1 & SDADC_CR1_REOCIE) == SDADC_CR1_REOCIE))
2054 /* Call regular conversion complete callback */
2055 HAL_SDADC_ConvCpltCallback(hsdadc);
2057 /* End of conversion if mode is not continuous and software trigger */
2058 if((hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
2059 (hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER))
2061 /* Clear REOCIE and ROVRIE bits in SDADC_CR1 register */
2062 hsdadc->Instance->CR1 &= ~(SDADC_CR1_REOCIE | SDADC_CR1_ROVRIE);
2064 /* Update SDADC state */
2065 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_REG) ? \
2066 HAL_SDADC_STATE_READY : HAL_SDADC_STATE_INJ;
2069 /* Check if end of injected conversion */
2070 else if(((hsdadc->Instance->ISR & SDADC_ISR_JEOCF) == SDADC_ISR_JEOCF) && \
2071 ((hsdadc->Instance->CR1 & SDADC_CR1_JEOCIE) == SDADC_CR1_JEOCIE))
2073 /* Call injected conversion complete callback */
2074 HAL_SDADC_InjectedConvCpltCallback(hsdadc);
2076 /* Update remaining injected conversions */
2077 hsdadc->InjConvRemaining--;
2078 if(hsdadc->InjConvRemaining ==0)
2080 /* end of injected sequence, reset the value */
2081 hsdadc->InjConvRemaining = hsdadc->InjectedChannelsNbr;
2083 /* End of conversion if mode is not continuous, software trigger */
2084 /* and end of injected sequence */
2085 if((hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
2086 (hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
2087 (hsdadc->InjConvRemaining == hsdadc->InjectedChannelsNbr))
2089 /* Clear JEOCIE and JOVRIE bits in SDADC_CR1 register */
2090 hsdadc->Instance->CR1 &= ~(SDADC_CR1_JEOCIE | SDADC_CR1_JOVRIE);
2092 /* Update SDADC state */
2093 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_INJ) ? \
2094 HAL_SDADC_STATE_READY : HAL_SDADC_STATE_REG;
2097 /* Check if end of calibration */
2098 else if(((hsdadc->Instance->ISR & SDADC_ISR_EOCALF) == SDADC_ISR_EOCALF) && \
2099 ((hsdadc->Instance->CR1 & SDADC_CR1_EOCALIE) == SDADC_CR1_EOCALIE))
2101 /* Clear EOCALIE bit in SDADC_CR1 register */
2102 hsdadc->Instance->CR1 &= ~(SDADC_CR1_EOCALIE);
2104 /* Set CLREOCALF bit in SDADC_CLRISR register */
2105 hsdadc->Instance->CLRISR |= SDADC_ISR_CLREOCALF;
2107 /* Call calibration callback */
2108 HAL_SDADC_CalibrationCpltCallback(hsdadc);
2110 /* Update SDADC state */
2111 hsdadc->State = HAL_SDADC_STATE_READY;
2113 /* Check if overrun occurs during regular conversion */
2114 else if(((hsdadc->Instance->ISR & SDADC_ISR_ROVRF) == SDADC_ISR_ROVRF) && \
2115 ((hsdadc->Instance->CR1 & SDADC_CR1_ROVRIE) == SDADC_CR1_ROVRIE))
2117 /* Set CLRROVRF bit in SDADC_CLRISR register */
2118 hsdadc->Instance->CLRISR |= SDADC_ISR_CLRROVRF;
2120 /* Update error code */
2121 hsdadc->ErrorCode = SDADC_ERROR_REGULAR_OVERRUN;
2123 /* Call error callback */
2124 HAL_SDADC_ErrorCallback(hsdadc);
2126 /* Check if overrun occurs during injected conversion */
2127 else if(((hsdadc->Instance->ISR & SDADC_ISR_JOVRF) == SDADC_ISR_JOVRF) && \
2128 ((hsdadc->Instance->CR1 & SDADC_CR1_JOVRIE) == SDADC_CR1_JOVRIE))
2130 /* Set CLRJOVRF bit in SDADC_CLRISR register */
2131 hsdadc->Instance->CLRISR |= SDADC_ISR_CLRJOVRF;
2133 /* Update error code */
2134 hsdadc->ErrorCode = SDADC_ERROR_INJECTED_OVERRUN;
2136 /* Call error callback */
2137 HAL_SDADC_ErrorCallback(hsdadc);
2143 * @brief Calibration complete callback.
2144 * @param hsdadc : SDADC handle.
2147 __weak void HAL_SDADC_CalibrationCpltCallback(SDADC_HandleTypeDef* hsdadc)
2149 /* NOTE : This function should not be modified, when the callback is needed,
2150 the HAL_SDADC_CalibrationCpltCallback could be implemented in the user file
2155 * @brief Half regular conversion complete callback.
2156 * @param hsdadc : SDADC handle.
2159 __weak void HAL_SDADC_ConvHalfCpltCallback(SDADC_HandleTypeDef* hsdadc)
2161 /* NOTE : This function should not be modified, when the callback is needed,
2162 the HAL_SDADC_ConvHalfCpltCallback could be implemented in the user file
2167 * @brief Regular conversion complete callback.
2168 * @note In interrupt mode, user has to read conversion value in this function
2169 using HAL_SDADC_GetValue or HAL_SDADC_MultiModeGetValue.
2170 * @param hsdadc : SDADC handle.
2173 __weak void HAL_SDADC_ConvCpltCallback(SDADC_HandleTypeDef* hsdadc)
2175 /* NOTE : This function should not be modified, when the callback is needed,
2176 the HAL_SDADC_ConvCpltCallback could be implemented in the user file.
2181 * @brief Half injected conversion complete callback.
2182 * @param hsdadc : SDADC handle.
2185 __weak void HAL_SDADC_InjectedConvHalfCpltCallback(SDADC_HandleTypeDef* hsdadc)
2187 /* NOTE : This function should not be modified, when the callback is needed,
2188 the HAL_SDADC_InjectedConvHalfCpltCallback could be implemented in the user file.
2193 * @brief Injected conversion complete callback.
2194 * @note In interrupt mode, user has to read conversion value in this function
2195 using HAL_SDADC_InjectedGetValue or HAL_SDADC_InjectedMultiModeGetValue.
2196 * @param hsdadc : SDADC handle.
2199 __weak void HAL_SDADC_InjectedConvCpltCallback(SDADC_HandleTypeDef* hsdadc)
2201 /* NOTE : This function should not be modified, when the callback is needed,
2202 the HAL_SDADC_InjectedConvCpltCallback could be implemented in the user file.
2207 * @brief Error callback.
2208 * @param hsdadc : SDADC handle.
2211 __weak void HAL_SDADC_ErrorCallback(SDADC_HandleTypeDef* hsdadc)
2213 /* NOTE : This function should not be modified, when the callback is needed,
2214 the HAL_SDADC_ErrorCallback could be implemented in the user file.
2219 * @brief DMA half transfer complete callback for regular conversion.
2220 * @param hdma : DMA handle.
2223 static void SDADC_DMARegularHalfConvCplt(DMA_HandleTypeDef *hdma)
2225 /* Get SDADC handle */
2226 SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2228 /* Call regular half conversion complete callback */
2229 HAL_SDADC_ConvHalfCpltCallback(hsdadc);
2233 * @brief DMA transfer complete callback for regular conversion.
2234 * @param hdma : DMA handle.
2237 static void SDADC_DMARegularConvCplt(DMA_HandleTypeDef *hdma)
2239 /* Get SDADC handle */
2240 SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2242 /* Call regular conversion complete callback */
2243 HAL_SDADC_ConvCpltCallback(hsdadc);
2247 * @brief DMA half transfer complete callback for injected conversion.
2248 * @param hdma : DMA handle.
2251 static void SDADC_DMAInjectedHalfConvCplt(DMA_HandleTypeDef *hdma)
2253 /* Get SDADC handle */
2254 SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2256 /* Call injected half conversion complete callback */
2257 HAL_SDADC_InjectedConvHalfCpltCallback(hsdadc);
2261 * @brief DMA transfer complete callback for injected conversion.
2262 * @param hdma : DMA handle.
2265 static void SDADC_DMAInjectedConvCplt(DMA_HandleTypeDef *hdma)
2267 /* Get SDADC handle */
2268 SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2270 /* Call injected conversion complete callback */
2271 HAL_SDADC_InjectedConvCpltCallback(hsdadc);
2275 * @brief DMA error callback.
2276 * @param hdma : DMA handle.
2279 static void SDADC_DMAError(DMA_HandleTypeDef *hdma)
2281 /* Get SDADC handle */
2282 SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2284 /* Update error code */
2285 hsdadc->ErrorCode = SDADC_ERROR_DMA;
2287 /* Call error callback */
2288 HAL_SDADC_ErrorCallback(hsdadc);
2295 /** @defgroup SDADC_Exported_Functions_Group4 Peripheral State functions
2296 * @brief SDADC Peripheral State functions
2299 ===============================================================================
2300 ##### ADC Peripheral State functions #####
2301 ===============================================================================
2302 [..] This subsection provides functions allowing to
2303 (+) Get the SDADC state
2304 (+) Get the SDADC Error
2311 * @brief This function allows to get the current SDADC state.
2312 * @param hsdadc : SDADC handle.
2313 * @retval SDADC state.
2315 HAL_SDADC_StateTypeDef HAL_SDADC_GetState(SDADC_HandleTypeDef* hsdadc)
2317 return hsdadc->State;
2321 * @brief This function allows to get the current SDADC error code.
2322 * @param hsdadc : SDADC handle.
2323 * @retval SDADC error code.
2325 uint32_t HAL_SDADC_GetError(SDADC_HandleTypeDef* hsdadc)
2327 return hsdadc->ErrorCode;
2335 * @brief This function allows to enter in init mode for SDADC instance.
2336 * @param hsdadc : SDADC handle.
2337 * @retval HAL status.
2339 static HAL_StatusTypeDef SDADC_EnterInitMode(SDADC_HandleTypeDef* hsdadc)
2341 uint32_t tickstart = 0;
2343 /* Set INIT bit on SDADC_CR1 register */
2344 hsdadc->Instance->CR1 |= SDADC_CR1_INIT;
2346 /* Wait INITRDY bit on SDADC_ISR */
2347 tickstart = HAL_GetTick();
2348 while((hsdadc->Instance->ISR & SDADC_ISR_INITRDY) == (uint32_t)RESET)
2350 if((HAL_GetTick()-tickstart) > SDADC_TIMEOUT)
2356 /* Return HAL status */
2361 * @brief This function allows to exit from init mode for SDADC instance.
2362 * @param hsdadc : SDADC handle.
2365 static void SDADC_ExitInitMode(SDADC_HandleTypeDef* hsdadc)
2367 /* Reset INIT bit in SDADC_CR1 register */
2368 hsdadc->Instance->CR1 &= ~(SDADC_CR1_INIT);
2372 * @brief This function allows to get the number of injected channels.
2373 * @param Channels : bitfield of injected channels.
2374 * @retval Number of injected channels.
2376 static uint32_t SDADC_GetInjChannelsNbr(uint32_t Channels)
2378 uint32_t nbChannels = 0;
2381 /* Get the number of channels from bitfield */
2382 tmp = (uint32_t) (Channels & SDADC_LSB_MASK);
2383 for(i = 0 ; i < 9 ; i++)
2389 tmp = (uint32_t) (tmp >> 1);
2395 * @brief This function allows to really start regular conversion.
2396 * @param hsdadc : SDADC handle.
2397 * @retval HAL status.
2399 static HAL_StatusTypeDef SDADC_RegConvStart(SDADC_HandleTypeDef* hsdadc)
2401 HAL_StatusTypeDef status = HAL_OK;
2403 /* Check regular trigger */
2404 if(hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER)
2406 /* Set RSWSTART bit in SDADC_CR2 register */
2407 hsdadc->Instance->CR2 |= SDADC_CR2_RSWSTART;
2409 else /* synchronuous trigger */
2411 /* Enter init mode */
2412 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
2414 /* Set SDADC in error state */
2415 hsdadc->State = HAL_SDADC_STATE_ERROR;
2416 status = HAL_TIMEOUT;
2420 /* Set RSYNC bit in SDADC_CR1 register */
2421 hsdadc->Instance->CR1 |= SDADC_CR1_RSYNC;
2423 /* Exit init mode */
2424 SDADC_ExitInitMode(hsdadc);
2427 /* Update SDADC state only if status is OK */
2428 if(status == HAL_OK)
2430 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_READY) ? \
2431 HAL_SDADC_STATE_REG : HAL_SDADC_STATE_REG_INJ;
2433 /* Return function status */
2438 * @brief This function allows to really stop regular conversion.
2439 * @param hsdadc : SDADC handle.
2440 * @retval HAL status.
2442 static HAL_StatusTypeDef SDADC_RegConvStop(SDADC_HandleTypeDef* hsdadc)
2446 /* Check continuous mode */
2447 if(hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_ON)
2449 /* Clear REOCF by reading SDADC_RDATAR register */
2450 hsdadc->Instance->RDATAR;
2452 /* Clear RCONT bit in SDADC_CR2 register */
2453 hsdadc->Instance->CR2 &= ~(SDADC_CR2_RCONT);
2455 /* Wait for the end of regular conversion */
2456 tickstart = HAL_GetTick();
2457 while((hsdadc->Instance->ISR & SDADC_ISR_RCIP) != 0)
2459 if((HAL_GetTick()-tickstart) > SDADC_TIMEOUT)
2461 /* Set SDADC in error state and return timeout status */
2462 hsdadc->State = HAL_SDADC_STATE_ERROR;
2466 /* Check if trigger is synchronuous */
2467 if(hsdadc->RegularTrigger == SDADC_SYNCHRONOUS_TRIGGER)
2469 /* Enter init mode */
2470 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
2472 /* Set SDADC in error state and return timeout status */
2473 hsdadc->State = HAL_SDADC_STATE_ERROR;
2478 /* Clear RSYNC bit in SDADC_CR1 register */
2479 hsdadc->Instance->CR1 &= ~(SDADC_CR1_RSYNC);
2481 /* Exit init mode */
2482 SDADC_ExitInitMode(hsdadc);
2485 /* Check if continuous mode */
2486 if(hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_ON)
2488 /* Restore RCONT bit in SDADC_CR2 register */
2489 hsdadc->Instance->CR2 |= SDADC_CR2_RCONT;
2491 /* Clear REOCF by reading SDADC_RDATAR register */
2492 hsdadc->Instance->RDATAR;
2494 /* Set CLRROVRF bit in SDADC_CLRISR register */
2495 hsdadc->Instance->CLRISR |= SDADC_ISR_CLRROVRF;
2497 /* Update SDADC state */
2498 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_REG) ? \
2499 HAL_SDADC_STATE_READY : HAL_SDADC_STATE_INJ;
2501 /* Return function status */
2506 * @brief This function allows to really start injected conversion.
2507 * @param hsdadc : SDADC handle.
2508 * @retval HAL status.
2510 static HAL_StatusTypeDef SDADC_InjConvStart(SDADC_HandleTypeDef* hsdadc)
2512 HAL_StatusTypeDef status = HAL_OK;
2514 /* Initialize number of injected conversions remaining */
2515 hsdadc->InjConvRemaining = hsdadc->InjectedChannelsNbr;
2517 /* Check injected trigger */
2518 if(hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER)
2520 /* Set JSWSTART bit in SDADC_CR2 register */
2521 hsdadc->Instance->CR2 |= SDADC_CR2_JSWSTART;
2523 else /* external or synchronuous trigger */
2525 /* Enter init mode */
2526 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
2528 /* Set SDADC in error state */
2529 hsdadc->State = HAL_SDADC_STATE_ERROR;
2530 status = HAL_TIMEOUT;
2534 if(hsdadc->InjectedTrigger == SDADC_SYNCHRONOUS_TRIGGER)
2536 /* Set JSYNC bit in SDADC_CR1 register */
2537 hsdadc->Instance->CR1 |= SDADC_CR1_JSYNC;
2539 else /* external trigger */
2541 /* Set JEXTEN[1:0] bits in SDADC_CR2 register */
2542 hsdadc->Instance->CR2 |= hsdadc->ExtTriggerEdge;
2544 /* Exit init mode */
2545 SDADC_ExitInitMode(hsdadc);
2548 /* Update SDADC state only if status is OK */
2549 if(status == HAL_OK)
2551 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_READY) ? \
2552 HAL_SDADC_STATE_INJ : HAL_SDADC_STATE_REG_INJ;
2554 /* Return function status */
2559 * @brief This function allows to really stop injected conversion.
2560 * @param hsdadc : SDADC handle.
2561 * @retval HAL status.
2563 static HAL_StatusTypeDef SDADC_InjConvStop(SDADC_HandleTypeDef* hsdadc)
2567 /* Check continuous mode */
2568 if(hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_ON)
2570 /* Clear JEOCF by reading SDADC_JDATAR register */
2571 hsdadc->Instance->JDATAR;
2573 /* Clear JCONT bit in SDADC_CR2 register */
2574 hsdadc->Instance->CR2 &= ~(SDADC_CR2_JCONT);
2576 /* Wait for the end of injected conversion */
2577 tickstart = HAL_GetTick();
2578 while((hsdadc->Instance->ISR & SDADC_ISR_JCIP) != 0)
2580 if((HAL_GetTick()-tickstart) > SDADC_TIMEOUT)
2582 /* Set SDADC in error state and return timeout status */
2583 hsdadc->State = HAL_SDADC_STATE_ERROR;
2587 /* Check if trigger is not software */
2588 if(hsdadc->InjectedTrigger != SDADC_SOFTWARE_TRIGGER)
2590 /* Enter init mode */
2591 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
2593 /* Set SDADC in error state and return timeout status */
2594 hsdadc->State = HAL_SDADC_STATE_ERROR;
2599 /* Check if trigger is synchronuous */
2600 if(hsdadc->InjectedTrigger == SDADC_SYNCHRONOUS_TRIGGER)
2602 /* Clear JSYNC bit in SDADC_CR1 register */
2603 hsdadc->Instance->CR1 &= ~(SDADC_CR1_JSYNC);
2605 else /* external trigger */
2607 /* Clear JEXTEN[1:0] bits in SDADC_CR2 register */
2608 hsdadc->Instance->CR2 &= ~(SDADC_CR2_JEXTEN);
2610 /* Exit init mode */
2611 SDADC_ExitInitMode(hsdadc);
2614 /* Check if continuous mode */
2615 if(hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_ON)
2617 /* Restore JCONT bit in SDADC_CR2 register */
2618 hsdadc->Instance->CR2 |= SDADC_CR2_JCONT;
2620 /* Clear JEOCF by reading SDADC_JDATAR register */
2621 hsdadc->Instance->JDATAR;
2623 /* Set CLRJOVRF bit in SDADC_CLRISR register */
2624 hsdadc->Instance->CLRISR |= SDADC_ISR_CLRJOVRF;
2626 /* Update SDADC state */
2627 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_INJ) ? \
2628 HAL_SDADC_STATE_READY : HAL_SDADC_STATE_REG;
2630 /* Return function status */
2642 #endif /* defined(STM32F373xC) || defined(STM32F378xx) */
2643 #endif /* HAL_SDADC_MODULE_ENABLED */
2648 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/