]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3/stm32f3xx_hal_sdadc.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3 / stm32f3xx_hal_sdadc.c
1 /**
2 ******************************************************************************
3 * @file stm32f3xx_hal_sdadc.c
4 * @author MCD Application Team
5 * @version V1.1.0
6 * @date 12-Sept-2014
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Sigma-Delta Analog to Digital Convertor
9 * (SDADC) peripherals:
10 * + Initialization and Configuration
11 * + Regular Channels Configuration
12 * + Injected channels Configuration
13 * + Power saving
14 * + Regular/Injected Channels DMA Configuration
15 * + Interrupts and flags management
16 *
17 @verbatim
18 ==============================================================================
19 ##### SDADC specific features #####
20 ==============================================================================
21 [..]
22 (#) 16-bit sigma delta architecture.
23 (#) Self calibration.
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.
30
31 ##### How to use this driver #####
32 ==============================================================================
33 [..]
34 *** Initialization ***
35 ======================
36 [..]
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.
54
55 *** Calibration ***
56 ============================================
57 [..]
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
61 calibration.
62 (#) In interrupt mode, HAL_SDADC_CalibrationCpltCallback will be called at
63 the end of calibration.
64
65 *** Regular channel conversion ***
66 ============================================
67 [..]
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
75 regular conversion.
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.
84
85 *** Injected channels conversion ***
86 ============================================
87 [..]
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.
109
110 *** Multi mode regular channels conversions ***
111 ======================================================
112 [..]
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).
133
134 *** Multi mode injected channels conversions ***
135 ======================================================
136 [..]
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
158 for SDADC1.
159 (#) Stop injected conversion using HAL_SDADC_InjectedStop for SDADC2
160 (or SDADC3).
161
162 @endverbatim
163 ******************************************************************************
164 * @attention
165 *
166 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
167 *
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.
178 *
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.
189 *
190 ******************************************************************************
191 */
192
193 /* Includes ------------------------------------------------------------------*/
194 #include "stm32f3xx_hal.h"
195
196 /** @addtogroup STM32F3xx_HAL_Driver
197 * @{
198 */
199
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
204 * @{
205 */
206
207 /* Private typedef -----------------------------------------------------------*/
208 /* Private define ------------------------------------------------------------*/
209 /** @defgroup SDADC_Private_Define SDADC Private Define
210 * @{
211 */
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
217 /**
218 * @}
219 */
220
221 /* Private macro -------------------------------------------------------------*/
222 /* Private variables ---------------------------------------------------------*/
223 /* Private function prototypes -----------------------------------------------*/
224 /** @defgroup SDADC_Private_Functions SDADC Private Functions
225 * @{
226 */
227
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);
240 /**
241 * @}
242 */
243
244 /* Exported functions ---------------------------------------------------------*/
245
246 /** @defgroup SDADC_Exported_Functions SDADC Exported Functions
247 * @{
248 */
249
250 /** @defgroup SDADC_Exported_Functions_Group1 Initialization and de-initialization functions
251 * @brief Initialization and de-initialization functions
252 *
253 @verbatim
254 ===============================================================================
255 ##### Initialization and de-initialization functions #####
256 ===============================================================================
257 [..] This section provides functions allowing to:
258 (+) Initialize the SDADC.
259 (+) De-initialize the SDADC.
260
261 @endverbatim
262 * @{
263 */
264
265 /**
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.
272 */
273 HAL_StatusTypeDef HAL_SDADC_Init(SDADC_HandleTypeDef* hsdadc)
274 {
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));
281
282 /* Check SDADC handle */
283 if(hsdadc == HAL_NULL)
284 {
285 return HAL_ERROR;
286 }
287
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;
299
300 /* Call MSP init function */
301 HAL_SDADC_MspInit(hsdadc);
302
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);
307
308 /* Set fast conversion mode */
309 hsdadc->Instance->CR2 &= ~(SDADC_CR2_FAST);
310 hsdadc->Instance->CR2 |= hsdadc->Init.FastConversionMode;
311
312 /* Set reference voltage only for SDADC1 */
313 if(hsdadc->Instance == SDADC1)
314 {
315 hsdadc->Instance->CR1 &= ~(SDADC_CR1_REFV);
316 hsdadc->Instance->CR1 |= hsdadc->Init.ReferenceVoltage;
317
318 /* Wait at least 2ms before setting ADON */
319 HAL_Delay(2);
320 }
321
322 /* Enable SDADC */
323 hsdadc->Instance->CR2 |= SDADC_CR2_ADON;
324
325 /* Wait end of stabilization */
326 while((hsdadc->Instance->ISR & SDADC_ISR_STABIP) != 0);
327
328 /* Set SDADC to ready state */
329 hsdadc->State = HAL_SDADC_STATE_READY;
330
331 /* Return HAL status */
332 return HAL_OK;
333 }
334
335 /**
336 * @brief De-initializes the SDADC.
337 * @param hsdadc : SDADC handle.
338 * @retval HAL status.
339 */
340 HAL_StatusTypeDef HAL_SDADC_DeInit(SDADC_HandleTypeDef* hsdadc)
341 {
342 /* Check parameters */
343 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
344
345 /* Check SDADC handle */
346 if(hsdadc == HAL_NULL)
347 {
348 return HAL_ERROR;
349 }
350
351 /* Disable the SDADC */
352 hsdadc->Instance->CR2 &= ~(SDADC_CR2_ADON);
353
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;
363
364 /* Call MSP deinit function */
365 HAL_SDADC_MspDeInit(hsdadc);
366
367 /* Set SDADC in reset state */
368 hsdadc->State = HAL_SDADC_STATE_RESET;
369
370 /* Return function status */
371 return HAL_OK;
372 }
373
374 /**
375 * @brief Initializes the SDADC MSP.
376 * @param hsdadc : SDADC handle
377 * @retval None
378 */
379 __weak void HAL_SDADC_MspInit(SDADC_HandleTypeDef* hsdadc)
380 {
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.
383 */
384 }
385
386 /**
387 * @brief De-initializes the SDADC MSP.
388 * @param hsdadc : SDADC handle
389 * @retval None
390 */
391 __weak void HAL_SDADC_MspDeInit(SDADC_HandleTypeDef* hsdadc)
392 {
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.
395 */
396 }
397
398 /**
399 * @}
400 */
401
402 /** @defgroup SDADC_Exported_Functions_Group2 peripheral control functions
403 * @brief Peripheral control functions
404 *
405 @verbatim
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.
418
419 @endverbatim
420 * @{
421 */
422
423 /**
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.
432 * @retval HAL status
433 */
434 HAL_StatusTypeDef HAL_SDADC_PrepareChannelConfig(SDADC_HandleTypeDef *hsdadc,
435 uint32_t ConfIndex,
436 SDADC_ConfParamTypeDef* ConfParamStruct)
437 {
438 HAL_StatusTypeDef status = HAL_OK;
439 uint32_t tmp = 0;
440
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));
449
450 /* Check SDADC state is ready */
451 if(hsdadc->State != HAL_SDADC_STATE_READY)
452 {
453 status = HAL_ERROR;
454 }
455 else
456 {
457 /* Enter init mode */
458 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
459 {
460 /* Set SDADC in error state */
461 hsdadc->State = HAL_SDADC_STATE_ERROR;
462 status = HAL_TIMEOUT;
463 }
464 else
465 {
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);
474 /* Exit init mode */
475 SDADC_ExitInitMode(hsdadc);
476 }
477 }
478 /* Return function status */
479 return status;
480 }
481
482 /**
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.
492 * @retval HAL status
493 */
494 HAL_StatusTypeDef HAL_SDADC_AssociateChannelConfig(SDADC_HandleTypeDef *hsdadc,
495 uint32_t Channel,
496 uint32_t ConfIndex)
497 {
498 HAL_StatusTypeDef status = HAL_OK;
499 uint32_t channelnum = 0;
500
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));
505
506 /* Check SDADC state is ready */
507 if(hsdadc->State != HAL_SDADC_STATE_READY)
508 {
509 status = HAL_ERROR;
510 }
511 else
512 {
513 /* Enter init mode */
514 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
515 {
516 /* Set SDADC in error state */
517 hsdadc->State = HAL_SDADC_STATE_ERROR;
518 status = HAL_TIMEOUT;
519 }
520 else
521 {
522 /* Program channel configuration register according parameters */
523 if(Channel != SDADC_CHANNEL_8)
524 {
525 /* Get channel number */
526 channelnum = (uint32_t)(Channel>>16);
527
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));
531 }
532 else
533 {
534 hsdadc->Instance->CONFCHR2 = (uint32_t) (ConfIndex);
535 }
536 /* Exit init mode */
537 SDADC_ExitInitMode(hsdadc);
538 }
539 }
540 /* Return function status */
541 return status;
542 }
543
544 /**
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.
552 * @retval HAL status
553 */
554 HAL_StatusTypeDef HAL_SDADC_ConfigChannel(SDADC_HandleTypeDef *hsdadc,
555 uint32_t Channel,
556 uint32_t ContinuousMode)
557 {
558 HAL_StatusTypeDef status = HAL_OK;
559
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));
564
565 /* Check SDADC state */
566 if((hsdadc->State != HAL_SDADC_STATE_RESET) && (hsdadc->State != HAL_SDADC_STATE_ERROR))
567 {
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)
571 {
572 hsdadc->Instance->CR2 |= (uint32_t) ((Channel & SDADC_MSB_MASK) | SDADC_CR2_RCONT);
573 }
574 else
575 {
576 hsdadc->Instance->CR2 |= (uint32_t) ((Channel & SDADC_MSB_MASK));
577 }
578 /* Store continuous mode information */
579 hsdadc->RegularContMode = ContinuousMode;
580 }
581 else
582 {
583 status = HAL_ERROR;
584 }
585 /* Return function status */
586 return status;
587 }
588
589 /**
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.
597 * @retval HAL status
598 */
599 HAL_StatusTypeDef HAL_SDADC_InjectedConfigChannel(SDADC_HandleTypeDef *hsdadc,
600 uint32_t Channel,
601 uint32_t ContinuousMode)
602 {
603 HAL_StatusTypeDef status = HAL_OK;
604
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));
609
610 /* Check SDADC state */
611 if((hsdadc->State != HAL_SDADC_STATE_RESET) && (hsdadc->State != HAL_SDADC_STATE_ERROR))
612 {
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)
617 {
618 hsdadc->Instance->CR2 |= SDADC_CR2_JCONT;
619 }
620 else
621 {
622 hsdadc->Instance->CR2 &= ~(SDADC_CR2_JCONT);
623 }
624 /* Store continuous mode information */
625 hsdadc->InjectedContMode = ContinuousMode;
626 /* Store number of injected channels */
627 hsdadc->InjectedChannelsNbr = SDADC_GetInjChannelsNbr(Channel);
628 }
629 else
630 {
631 status = HAL_ERROR;
632 }
633 /* Return function status */
634 return status;
635 }
636
637 /**
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).
645 * @retval HAL status
646 */
647 HAL_StatusTypeDef HAL_SDADC_SelectRegularTrigger(SDADC_HandleTypeDef *hsdadc, uint32_t Trigger)
648 {
649 HAL_StatusTypeDef status = HAL_OK;
650
651 /* Check parameters */
652 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
653 assert_param(IS_SDADC_REGULAR_TRIGGER(Trigger));
654
655 /* Check parameters compatibility */
656 if((hsdadc->Instance == SDADC1) && (Trigger == SDADC_SYNCHRONOUS_TRIGGER))
657 {
658 status = HAL_ERROR;
659 }
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))
664 {
665 /* Store regular trigger information */
666 hsdadc->RegularTrigger = Trigger;
667 }
668 else
669 {
670 status = HAL_ERROR;
671 }
672 /* Return function status */
673 return status;
674 }
675
676 /**
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.
685 * @retval HAL status
686 */
687 HAL_StatusTypeDef HAL_SDADC_SelectInjectedTrigger(SDADC_HandleTypeDef *hsdadc, uint32_t Trigger)
688 {
689 HAL_StatusTypeDef status = HAL_OK;
690
691 /* Check parameters */
692 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
693 assert_param(IS_SDADC_INJECTED_TRIGGER(Trigger));
694
695 /* Check parameters compatibility */
696 if((hsdadc->Instance == SDADC1) && (Trigger == SDADC_SYNCHRONOUS_TRIGGER))
697 {
698 status = HAL_ERROR;
699 }
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))
704 {
705 /* Store regular trigger information */
706 hsdadc->InjectedTrigger = Trigger;
707 }
708 else
709 {
710 status = HAL_ERROR;
711 }
712 /* Return function status */
713 return status;
714 }
715
716 /**
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.
725 * @retval HAL status
726 */
727 HAL_StatusTypeDef HAL_SDADC_SelectInjectedExtTrigger(SDADC_HandleTypeDef *hsdadc,
728 uint32_t InjectedExtTrigger,
729 uint32_t ExtTriggerEdge)
730 {
731 HAL_StatusTypeDef status = HAL_OK;
732
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));
737
738 /* Check SDADC state */
739 if(hsdadc->State == HAL_SDADC_STATE_READY)
740 {
741 /* Enter init mode */
742 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
743 {
744 /* Set SDADC in error state */
745 hsdadc->State = HAL_SDADC_STATE_ERROR;
746 status = HAL_TIMEOUT;
747 }
748 else
749 {
750 /* Set JEXTSEL[2:0] bits in SDADC_CR2 register */
751 hsdadc->Instance->CR2 &= ~(SDADC_CR2_JEXTSEL);
752 hsdadc->Instance->CR2 |= InjectedExtTrigger;
753
754 /* Store external trigger edge information */
755 hsdadc->ExtTriggerEdge = ExtTriggerEdge;
756
757 /* Exit init mode */
758 SDADC_ExitInitMode(hsdadc);
759 }
760 }
761 else
762 {
763 status = HAL_ERROR;
764 }
765 /* Return function status */
766 return status;
767 }
768
769 /**
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.
776 * @retval HAL status
777 */
778 HAL_StatusTypeDef HAL_SDADC_SelectInjectedDelay(SDADC_HandleTypeDef *hsdadc,
779 uint32_t InjectedDelay)
780 {
781 HAL_StatusTypeDef status = HAL_OK;
782
783 /* Check parameters */
784 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
785 assert_param(IS_SDADC_INJECTED_DELAY(InjectedDelay));
786
787 /* Check SDADC state */
788 if(hsdadc->State == HAL_SDADC_STATE_READY)
789 {
790 /* Enter init mode */
791 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
792 {
793 /* Set SDADC in error state */
794 hsdadc->State = HAL_SDADC_STATE_ERROR;
795 status = HAL_TIMEOUT;
796 }
797 else
798 {
799 /* Set JDS bit in SDADC_CR2 register */
800 hsdadc->Instance->CR2 &= ~(SDADC_CR2_JDS);
801 hsdadc->Instance->CR2 |= InjectedDelay;
802
803 /* Exit init mode */
804 SDADC_ExitInitMode(hsdadc);
805 }
806 }
807 else
808 {
809 status = HAL_ERROR;
810 }
811 /* Return function status */
812 return status;
813 }
814
815 /**
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.
822 * @retval HAL status
823 */
824 HAL_StatusTypeDef HAL_SDADC_MultiModeConfigChannel(SDADC_HandleTypeDef* hsdadc,
825 uint32_t MultimodeType)
826 {
827 HAL_StatusTypeDef status = HAL_OK;
828
829 /* Check parameters */
830 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
831 assert_param(IS_SDADC_MULTIMODE_TYPE(MultimodeType));
832
833 /* Check instance is SDADC1 */
834 if(hsdadc->Instance != SDADC1)
835 {
836 status = HAL_ERROR;
837 }
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))
842 {
843 /* Store regular trigger information */
844 hsdadc->RegularMultimode = MultimodeType;
845 }
846 else
847 {
848 status = HAL_ERROR;
849 }
850 /* Return function status */
851 return status;
852 }
853
854 /**
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.
861 * @retval HAL status
862 */
863 HAL_StatusTypeDef HAL_SDADC_InjectedMultiModeConfigChannel(SDADC_HandleTypeDef* hsdadc,
864 uint32_t MultimodeType)
865 {
866 HAL_StatusTypeDef status = HAL_OK;
867
868 /* Check parameters */
869 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
870 assert_param(IS_SDADC_MULTIMODE_TYPE(MultimodeType));
871
872 /* Check instance is SDADC1 */
873 if(hsdadc->Instance != SDADC1)
874 {
875 status = HAL_ERROR;
876 }
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))
881 {
882 /* Store regular trigger information */
883 hsdadc->InjectedMultimode = MultimodeType;
884 }
885 else
886 {
887 status = HAL_ERROR;
888 }
889 /* Return function status */
890 return status;
891 }
892
893 /**
894 * @}
895 */
896
897 /** @defgroup SDADC_Exported_Functions_Group3 Input and Output operation functions
898 * @brief I/O operation Control functions
899 *
900 @verbatim
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.
922
923 @endverbatim
924 * @{
925 */
926
927 /**
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.
934 * @retval HAL status
935 */
936 HAL_StatusTypeDef HAL_SDADC_CalibrationStart(SDADC_HandleTypeDef *hsdadc,
937 uint32_t CalibrationSequence)
938 {
939 HAL_StatusTypeDef status = HAL_OK;
940
941 /* Check parameters */
942 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
943 assert_param(IS_SDADC_CALIB_SEQUENCE(CalibrationSequence));
944
945 /* Check SDADC state */
946 if(hsdadc->State == HAL_SDADC_STATE_READY)
947 {
948 /* Enter init mode */
949 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
950 {
951 /* Set SDADC in error state */
952 hsdadc->State = HAL_SDADC_STATE_ERROR;
953 status = HAL_TIMEOUT;
954 }
955 else
956 {
957 /* Set CALIBCNT[1:0] bits in SDADC_CR2 register */
958 hsdadc->Instance->CR2 &= ~(SDADC_CR2_CALIBCNT);
959 hsdadc->Instance->CR2 |= CalibrationSequence;
960
961 /* Exit init mode */
962 SDADC_ExitInitMode(hsdadc);
963
964 /* Set STARTCALIB in SDADC_CR2 */
965 hsdadc->Instance->CR2 |= SDADC_CR2_STARTCALIB;
966
967 /* Set SDADC in calibration state */
968 hsdadc->State = HAL_SDADC_STATE_CALIB;
969 }
970 }
971 else
972 {
973 status = HAL_ERROR;
974 }
975 /* Return function status */
976 return status;
977 }
978
979 /**
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.
984 * @retval HAL status
985 */
986 HAL_StatusTypeDef HAL_SDADC_PollForCalibEvent(SDADC_HandleTypeDef* hsdadc, uint32_t Timeout)
987 {
988 uint32_t tickstart;
989
990 /* Check parameters */
991 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
992
993 /* Check SDADC state */
994 if(hsdadc->State != HAL_SDADC_STATE_CALIB)
995 {
996 /* Return error status */
997 return HAL_ERROR;
998 }
999 else
1000 {
1001 /* Get timeout */
1002 tickstart = HAL_GetTick();
1003
1004 /* Wait EOCALF bit in SDADC_ISR register */
1005 while((hsdadc->Instance->ISR & SDADC_ISR_EOCALF) != SDADC_ISR_EOCALF)
1006 {
1007 /* Check the Timeout */
1008 if(Timeout != HAL_MAX_DELAY)
1009 {
1010 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
1011 {
1012 /* Return timeout status */
1013 return HAL_TIMEOUT;
1014 }
1015 }
1016 }
1017 /* Set CLREOCALF bit in SDADC_CLRISR register */
1018 hsdadc->Instance->CLRISR |= SDADC_ISR_CLREOCALF;
1019
1020 /* Set SDADC in ready state */
1021 hsdadc->State = HAL_SDADC_STATE_READY;
1022
1023 /* Return function status */
1024 return HAL_OK;
1025 }
1026 }
1027
1028 /**
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
1036 */
1037 HAL_StatusTypeDef HAL_SDADC_CalibrationStart_IT(SDADC_HandleTypeDef *hsdadc,
1038 uint32_t CalibrationSequence)
1039 {
1040 HAL_StatusTypeDef status = HAL_OK;
1041
1042 /* Check parameters */
1043 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1044 assert_param(IS_SDADC_CALIB_SEQUENCE(CalibrationSequence));
1045
1046 /* Check SDADC state */
1047 if(hsdadc->State == HAL_SDADC_STATE_READY)
1048 {
1049 /* Enter init mode */
1050 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
1051 {
1052 /* Set SDADC in error state */
1053 hsdadc->State = HAL_SDADC_STATE_ERROR;
1054 status = HAL_TIMEOUT;
1055 }
1056 else
1057 {
1058 /* Set CALIBCNT[1:0] bits in SDADC_CR2 register */
1059 hsdadc->Instance->CR2 &= ~(SDADC_CR2_CALIBCNT);
1060 hsdadc->Instance->CR2 |= CalibrationSequence;
1061
1062 /* Exit init mode */
1063 SDADC_ExitInitMode(hsdadc);
1064
1065 /* Set EOCALIE bit in SDADC_CR1 register */
1066 hsdadc->Instance->CR1 |= SDADC_CR1_EOCALIE;
1067
1068 /* Set STARTCALIB in SDADC_CR2 */
1069 hsdadc->Instance->CR2 |= SDADC_CR2_STARTCALIB;
1070
1071 /* Set SDADC in calibration state */
1072 hsdadc->State = HAL_SDADC_STATE_CALIB;
1073 }
1074 }
1075 else
1076 {
1077 status = HAL_ERROR;
1078 }
1079 /* Return function status */
1080 return status;
1081 }
1082
1083 /**
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
1089 */
1090 HAL_StatusTypeDef HAL_SDADC_Start(SDADC_HandleTypeDef *hsdadc)
1091 {
1092 HAL_StatusTypeDef status = HAL_OK;
1093
1094 /* Check parameters */
1095 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1096
1097 /* Check SDADC state */
1098 if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1099 (hsdadc->State == HAL_SDADC_STATE_INJ))
1100 {
1101 /* Start regular conversion */
1102 status = SDADC_RegConvStart(hsdadc);
1103 }
1104 else
1105 {
1106 status = HAL_ERROR;
1107 }
1108 /* Return function status */
1109 return status;
1110 }
1111
1112 /**
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
1118 */
1119 HAL_StatusTypeDef HAL_SDADC_PollForConversion(SDADC_HandleTypeDef* hsdadc, uint32_t Timeout)
1120 {
1121 uint32_t tickstart;
1122
1123 /* Check parameters */
1124 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1125
1126 /* Check SDADC state */
1127 if((hsdadc->State != HAL_SDADC_STATE_REG) && \
1128 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1129 {
1130 /* Return error status */
1131 return HAL_ERROR;
1132 }
1133 else
1134 {
1135 /* Get timeout */
1136 tickstart = HAL_GetTick();
1137
1138 /* Wait REOCF bit in SDADC_ISR register */
1139 while((hsdadc->Instance->ISR & SDADC_ISR_REOCF) != SDADC_ISR_REOCF)
1140 {
1141 /* Check the Timeout */
1142 if(Timeout != HAL_MAX_DELAY)
1143 {
1144 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
1145 {
1146 /* Return timeout status */
1147 return HAL_TIMEOUT;
1148 }
1149 }
1150 }
1151 /* Check if overrun occurs */
1152 if((hsdadc->Instance->ISR & SDADC_ISR_ROVRF) == SDADC_ISR_ROVRF)
1153 {
1154 /* Update error code and call error callback */
1155 hsdadc->ErrorCode = SDADC_ERROR_REGULAR_OVERRUN;
1156 HAL_SDADC_ErrorCallback(hsdadc);
1157
1158 /* Set CLRROVRF bit in SDADC_CLRISR register */
1159 hsdadc->Instance->CLRISR |= SDADC_ISR_CLRROVRF;
1160 }
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))
1164 {
1165 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_REG) ? \
1166 HAL_SDADC_STATE_READY : HAL_SDADC_STATE_INJ;
1167 }
1168 /* Return function status */
1169 return HAL_OK;
1170 }
1171 }
1172
1173 /**
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
1178 */
1179 HAL_StatusTypeDef HAL_SDADC_Stop(SDADC_HandleTypeDef *hsdadc)
1180 {
1181 HAL_StatusTypeDef status = HAL_OK;
1182
1183 /* Check parameters */
1184 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1185
1186 /* Check SDADC state */
1187 if((hsdadc->State != HAL_SDADC_STATE_REG) && \
1188 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1189 {
1190 /* Return error status */
1191 status = HAL_ERROR;
1192 }
1193 else
1194 {
1195 /* Stop regular conversion */
1196 status = SDADC_RegConvStop(hsdadc);
1197 }
1198 /* Return function status */
1199 return status;
1200 }
1201
1202 /**
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
1208 */
1209 HAL_StatusTypeDef HAL_SDADC_Start_IT(SDADC_HandleTypeDef *hsdadc)
1210 {
1211 HAL_StatusTypeDef status = HAL_OK;
1212
1213 /* Check parameters */
1214 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1215
1216 /* Check SDADC state */
1217 if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1218 (hsdadc->State == HAL_SDADC_STATE_INJ))
1219 {
1220 /* Set REOCIE and ROVRIE bits in SDADC_CR1 register */
1221 hsdadc->Instance->CR1 |= (uint32_t) (SDADC_CR1_REOCIE | SDADC_CR1_ROVRIE);
1222
1223 /* Start regular conversion */
1224 status = SDADC_RegConvStart(hsdadc);
1225 }
1226 else
1227 {
1228 status = HAL_ERROR;
1229 }
1230 /* Return function status */
1231 return status;
1232 }
1233
1234 /**
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
1239 */
1240 HAL_StatusTypeDef HAL_SDADC_Stop_IT(SDADC_HandleTypeDef *hsdadc)
1241 {
1242 HAL_StatusTypeDef status = HAL_OK;
1243
1244 /* Check parameters */
1245 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1246
1247 /* Check SDADC state */
1248 if((hsdadc->State != HAL_SDADC_STATE_REG) && \
1249 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1250 {
1251 /* Return error status */
1252 status = HAL_ERROR;
1253 }
1254 else
1255 {
1256 /* Clear REOCIE and ROVRIE bits in SDADC_CR1 register */
1257 hsdadc->Instance->CR1 &= (uint32_t) ~(SDADC_CR1_REOCIE | SDADC_CR1_ROVRIE);
1258
1259 /* Stop regular conversion */
1260 status = SDADC_RegConvStop(hsdadc);
1261 }
1262 /* Return function status */
1263 return status;
1264 }
1265
1266 /**
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
1274 */
1275 HAL_StatusTypeDef HAL_SDADC_Start_DMA(SDADC_HandleTypeDef *hsdadc, uint32_t *pData,
1276 uint32_t Length)
1277 {
1278 HAL_StatusTypeDef status = HAL_OK;
1279
1280 /* Check parameters */
1281 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1282 assert_param(pData != HAL_NULL);
1283 assert_param(Length != 0);
1284
1285 /* Check that DMA is not enabled for injected conversion */
1286 if((hsdadc->Instance->CR1 & SDADC_CR1_JDMAEN) == SDADC_CR1_JDMAEN)
1287 {
1288 status = HAL_ERROR;
1289 }
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) && \
1294 (Length != 1))
1295 {
1296 status = HAL_ERROR;
1297 }
1298 else if((hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER) && \
1299 (hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1300 (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
1301 {
1302 status = HAL_ERROR;
1303 }
1304 /* Check SDADC state */
1305 else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1306 (hsdadc->State == HAL_SDADC_STATE_INJ))
1307 {
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)
1312 {
1313 hsdadc->hdma->XferHalfCpltCallback = SDADC_DMARegularHalfConvCplt;
1314 }
1315
1316 /* Set RDMAEN bit in SDADC_CR1 register */
1317 hsdadc->Instance->CR1 |= SDADC_CR1_RDMAEN;
1318
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)
1322 {
1323 /* Set SDADC in error state */
1324 hsdadc->State = HAL_SDADC_STATE_ERROR;
1325 status = HAL_ERROR;
1326 }
1327 else
1328 {
1329 /* Start regular conversion */
1330 status = SDADC_RegConvStart(hsdadc);
1331 }
1332 }
1333 else
1334 {
1335 status = HAL_ERROR;
1336 }
1337 /* Return function status */
1338 return status;
1339 }
1340
1341 /**
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
1346 */
1347 HAL_StatusTypeDef HAL_SDADC_Stop_DMA(SDADC_HandleTypeDef *hsdadc)
1348 {
1349 HAL_StatusTypeDef status = HAL_OK;
1350
1351 /* Check parameters */
1352 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1353
1354 /* Check SDADC state */
1355 if((hsdadc->State != HAL_SDADC_STATE_REG) && \
1356 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1357 {
1358 /* Return error status */
1359 status = HAL_ERROR;
1360 }
1361 else
1362 {
1363 /* Clear RDMAEN bit in SDADC_CR1 register */
1364 hsdadc->Instance->CR1 &= ~(SDADC_CR1_RDMAEN);
1365
1366 /* Stop current DMA transfer */
1367 if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
1368 {
1369 /* Set SDADC in error state */
1370 hsdadc->State = HAL_SDADC_STATE_ERROR;
1371 status = HAL_ERROR;
1372 }
1373 else
1374 {
1375 /* Stop regular conversion */
1376 status = SDADC_RegConvStop(hsdadc);
1377 }
1378 }
1379 /* Return function status */
1380 return status;
1381 }
1382
1383 /**
1384 * @brief This function allows to get regular conversion value.
1385 * @param hsdadc : SDADC handle.
1386 * @retval Regular conversion value
1387 */
1388 uint32_t HAL_SDADC_GetValue(SDADC_HandleTypeDef *hsdadc)
1389 {
1390 /* Check parameters */
1391 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1392
1393 /* Return regular conversion value */
1394 return hsdadc->Instance->RDATAR;
1395 }
1396
1397 /**
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
1403 */
1404 HAL_StatusTypeDef HAL_SDADC_InjectedStart(SDADC_HandleTypeDef *hsdadc)
1405 {
1406 HAL_StatusTypeDef status = HAL_OK;
1407
1408 /* Check parameters */
1409 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1410
1411 /* Check SDADC state */
1412 if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1413 (hsdadc->State == HAL_SDADC_STATE_REG))
1414 {
1415 /* Start injected conversion */
1416 status = SDADC_InjConvStart(hsdadc);
1417 }
1418 else
1419 {
1420 status = HAL_ERROR;
1421 }
1422 /* Return function status */
1423 return status;
1424 }
1425
1426 /**
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
1432 */
1433 HAL_StatusTypeDef HAL_SDADC_PollForInjectedConversion(SDADC_HandleTypeDef* hsdadc,
1434 uint32_t Timeout)
1435 {
1436 uint32_t tickstart;
1437
1438 /* Check parameters */
1439 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1440
1441 /* Check SDADC state */
1442 if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
1443 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1444 {
1445 /* Return error status */
1446 return HAL_ERROR;
1447 }
1448 else
1449 {
1450 /* Get timeout */
1451 tickstart = HAL_GetTick();
1452
1453 /* Wait JEOCF bit in SDADC_ISR register */
1454 while((hsdadc->Instance->ISR & SDADC_ISR_JEOCF) != SDADC_ISR_JEOCF)
1455 {
1456 /* Check the Timeout */
1457 if(Timeout != HAL_MAX_DELAY)
1458 {
1459 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
1460 {
1461 /* Return timeout status */
1462 return HAL_TIMEOUT;
1463 }
1464 }
1465 }
1466 /* Check if overrun occurs */
1467 if((hsdadc->Instance->ISR & SDADC_ISR_JOVRF) == SDADC_ISR_JOVRF)
1468 {
1469 /* Update error code and call error callback */
1470 hsdadc->ErrorCode = SDADC_ERROR_INJECTED_OVERRUN;
1471 HAL_SDADC_ErrorCallback(hsdadc);
1472
1473 /* Set CLRJOVRF bit in SDADC_CLRISR register */
1474 hsdadc->Instance->CLRISR |= SDADC_ISR_CLRJOVRF;
1475 }
1476 /* Update remaining injected conversions */
1477 hsdadc->InjConvRemaining--;
1478 if(hsdadc->InjConvRemaining == 0)
1479 {
1480 /* end of injected sequence, reset the value */
1481 hsdadc->InjConvRemaining = hsdadc->InjectedChannelsNbr;
1482 }
1483
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))
1489 {
1490 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_INJ) ? \
1491 HAL_SDADC_STATE_READY : HAL_SDADC_STATE_REG;
1492 }
1493 /* Return function status */
1494 return HAL_OK;
1495 }
1496 }
1497
1498 /**
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
1503 */
1504 HAL_StatusTypeDef HAL_SDADC_InjectedStop(SDADC_HandleTypeDef *hsdadc)
1505 {
1506 HAL_StatusTypeDef status = HAL_OK;
1507
1508 /* Check parameters */
1509 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1510
1511 /* Check SDADC state */
1512 if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
1513 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1514 {
1515 /* Return error status */
1516 status = HAL_ERROR;
1517 }
1518 else
1519 {
1520 /* Stop injected conversion */
1521 status = SDADC_InjConvStop(hsdadc);
1522 }
1523 /* Return function status */
1524 return status;
1525 }
1526
1527 /**
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
1533 */
1534 HAL_StatusTypeDef HAL_SDADC_InjectedStart_IT(SDADC_HandleTypeDef *hsdadc)
1535 {
1536 HAL_StatusTypeDef status = HAL_OK;
1537
1538 /* Check parameters */
1539 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1540
1541 /* Check SDADC state */
1542 if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1543 (hsdadc->State == HAL_SDADC_STATE_REG))
1544 {
1545 /* Set JEOCIE and JOVRIE bits in SDADC_CR1 register */
1546 hsdadc->Instance->CR1 |= (uint32_t) (SDADC_CR1_JEOCIE | SDADC_CR1_JOVRIE);
1547
1548 /* Start injected conversion */
1549 status = SDADC_InjConvStart(hsdadc);
1550 }
1551 else
1552 {
1553 status = HAL_ERROR;
1554 }
1555 /* Return function status */
1556 return status;
1557 }
1558
1559 /**
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
1564 */
1565 HAL_StatusTypeDef HAL_SDADC_InjectedStop_IT(SDADC_HandleTypeDef *hsdadc)
1566 {
1567 HAL_StatusTypeDef status = HAL_OK;
1568
1569 /* Check parameters */
1570 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1571
1572 /* Check SDADC state */
1573 if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
1574 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1575 {
1576 /* Return error status */
1577 status = HAL_ERROR;
1578 }
1579 else
1580 {
1581 /* Clear JEOCIE and JOVRIE bits in SDADC_CR1 register */
1582 hsdadc->Instance->CR1 &= (uint32_t) ~(SDADC_CR1_JEOCIE | SDADC_CR1_JOVRIE);
1583
1584 /* Stop injected conversion */
1585 status = SDADC_InjConvStop(hsdadc);
1586 }
1587 /* Return function status */
1588 return status;
1589 }
1590
1591 /**
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
1599 */
1600 HAL_StatusTypeDef HAL_SDADC_InjectedStart_DMA(SDADC_HandleTypeDef *hsdadc, uint32_t *pData,
1601 uint32_t Length)
1602 {
1603 HAL_StatusTypeDef status = HAL_OK;
1604
1605 /* Check parameters */
1606 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1607 assert_param(pData != HAL_NULL);
1608 assert_param(Length != 0);
1609
1610 /* Check that DMA is not enabled for regular conversion */
1611 if((hsdadc->Instance->CR1 & SDADC_CR1_RDMAEN) == SDADC_CR1_RDMAEN)
1612 {
1613 status = HAL_ERROR;
1614 }
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))
1620 {
1621 status = HAL_ERROR;
1622 }
1623 else if((hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
1624 (hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1625 (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
1626 {
1627 status = HAL_ERROR;
1628 }
1629 /* Check SDADC state */
1630 else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1631 (hsdadc->State == HAL_SDADC_STATE_REG))
1632 {
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)
1637 {
1638 hsdadc->hdma->XferHalfCpltCallback = SDADC_DMAInjectedHalfConvCplt;
1639 }
1640
1641 /* Set JDMAEN bit in SDADC_CR1 register */
1642 hsdadc->Instance->CR1 |= SDADC_CR1_JDMAEN;
1643
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)
1647 {
1648 /* Set SDADC in error state */
1649 hsdadc->State = HAL_SDADC_STATE_ERROR;
1650 status = HAL_ERROR;
1651 }
1652 else
1653 {
1654 /* Start injected conversion */
1655 status = SDADC_InjConvStart(hsdadc);
1656 }
1657 }
1658 else
1659 {
1660 status = HAL_ERROR;
1661 }
1662 /* Return function status */
1663 return status;
1664 }
1665
1666 /**
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
1671 */
1672 HAL_StatusTypeDef HAL_SDADC_InjectedStop_DMA(SDADC_HandleTypeDef *hsdadc)
1673 {
1674 HAL_StatusTypeDef status = HAL_OK;
1675
1676 /* Check parameters */
1677 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1678
1679 /* Check SDADC state */
1680 if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
1681 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1682 {
1683 /* Return error status */
1684 status = HAL_ERROR;
1685 }
1686 else
1687 {
1688 /* Clear JDMAEN bit in SDADC_CR1 register */
1689 hsdadc->Instance->CR1 &= ~(SDADC_CR1_JDMAEN);
1690
1691 /* Stop current DMA transfer */
1692 if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
1693 {
1694 /* Set SDADC in error state */
1695 hsdadc->State = HAL_SDADC_STATE_ERROR;
1696 status = HAL_ERROR;
1697 }
1698 else
1699 {
1700 /* Stop injected conversion */
1701 status = SDADC_InjConvStop(hsdadc);
1702 }
1703 }
1704 /* Return function status */
1705 return status;
1706 }
1707
1708 /**
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
1713 */
1714 uint32_t HAL_SDADC_InjectedGetValue(SDADC_HandleTypeDef *hsdadc, uint32_t* Channel)
1715 {
1716 uint32_t value = 0;
1717
1718 /* Check parameters */
1719 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1720 assert_param(Channel != HAL_NULL);
1721
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;
1726
1727 /* Return injected conversion value */
1728 return value;
1729 }
1730
1731 /**
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
1739 */
1740 HAL_StatusTypeDef HAL_SDADC_MultiModeStart_DMA(SDADC_HandleTypeDef* hsdadc, uint32_t* pData,
1741 uint32_t Length)
1742 {
1743 HAL_StatusTypeDef status = HAL_OK;
1744
1745 /* Check parameters */
1746 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1747 assert_param(pData != HAL_NULL);
1748 assert_param(Length != 0);
1749
1750 /* Check instance is SDADC1 */
1751 if(hsdadc->Instance != SDADC1)
1752 {
1753 status = HAL_ERROR;
1754 }
1755 /* Check that DMA is not enabled for injected conversion */
1756 else if((hsdadc->Instance->CR1 & SDADC_CR1_JDMAEN) == SDADC_CR1_JDMAEN)
1757 {
1758 status = HAL_ERROR;
1759 }
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) && \
1764 (Length != 1))
1765 {
1766 status = HAL_ERROR;
1767 }
1768 else if((hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER) && \
1769 (hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1770 (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
1771 {
1772 status = HAL_ERROR;
1773 }
1774 /* Check SDADC state */
1775 else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1776 (hsdadc->State == HAL_SDADC_STATE_INJ))
1777 {
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)
1782 {
1783 hsdadc->hdma->XferHalfCpltCallback = SDADC_DMARegularHalfConvCplt;
1784 }
1785 /* Set RDMAEN bit in SDADC_CR1 register */
1786 hsdadc->Instance->CR1 |= SDADC_CR1_RDMAEN;
1787
1788 /* Start DMA in interrupt mode */
1789 if(hsdadc->RegularMultimode == SDADC_MULTIMODE_SDADC1_SDADC2)
1790 {
1791 status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->RDATA12R, \
1792 (uint32_t) pData, Length);
1793 }
1794 else
1795 {
1796 status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->RDATA13R, \
1797 (uint32_t) pData, Length);
1798 }
1799 if(status != HAL_OK)
1800 {
1801 /* Set SDADC in error state */
1802 hsdadc->State = HAL_SDADC_STATE_ERROR;
1803 status = HAL_ERROR;
1804 }
1805 else
1806 {
1807 /* Start regular conversion */
1808 status = SDADC_RegConvStart(hsdadc);
1809 }
1810 }
1811 else
1812 {
1813 status = HAL_ERROR;
1814 }
1815 /* Return function status */
1816 return status;
1817 }
1818
1819 /**
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
1824 */
1825 HAL_StatusTypeDef HAL_SDADC_MultiModeStop_DMA(SDADC_HandleTypeDef* hsdadc)
1826 {
1827 HAL_StatusTypeDef status = HAL_OK;
1828
1829 /* Check parameters */
1830 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1831
1832 /* Check instance is SDADC1 */
1833 if(hsdadc->Instance != SDADC1)
1834 {
1835 status = HAL_ERROR;
1836 }
1837 /* Check SDADC state */
1838 else if((hsdadc->State != HAL_SDADC_STATE_REG) && \
1839 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1840 {
1841 /* Return error status */
1842 status = HAL_ERROR;
1843 }
1844 else
1845 {
1846 /* Clear RDMAEN bit in SDADC_CR1 register */
1847 hsdadc->Instance->CR1 &= ~(SDADC_CR1_RDMAEN);
1848
1849 /* Stop current DMA transfer */
1850 if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
1851 {
1852 /* Set SDADC in error state */
1853 hsdadc->State = HAL_SDADC_STATE_ERROR;
1854 status = HAL_ERROR;
1855 }
1856 else
1857 {
1858 /* Stop regular conversion */
1859 status = SDADC_RegConvStop(hsdadc);
1860 }
1861 }
1862 /* Return function status */
1863 return status;
1864 }
1865
1866 /**
1867 * @brief This function allows to get multimode regular conversion value.
1868 * @param hsdadc : SDADC handle.
1869 * @retval Multimode regular conversion value
1870 */
1871 uint32_t HAL_SDADC_MultiModeGetValue(SDADC_HandleTypeDef* hsdadc)
1872 {
1873 uint32_t value = 0;
1874
1875 /* Check parameters and check instance is SDADC1 */
1876 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1877 assert_param(hsdadc->Instance == SDADC1);
1878
1879 /* read multimode regular value */
1880 value = (hsdadc->RegularMultimode == SDADC_MULTIMODE_SDADC1_SDADC2) ? \
1881 hsdadc->Instance->RDATA12R : hsdadc->Instance->RDATA13R;
1882
1883 /* Return multimode regular conversions value */
1884 return value;
1885 }
1886
1887 /**
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
1895 */
1896 HAL_StatusTypeDef HAL_SDADC_InjectedMultiModeStart_DMA(SDADC_HandleTypeDef* hsdadc,
1897 uint32_t* pData, uint32_t Length)
1898 {
1899 HAL_StatusTypeDef status = HAL_OK;
1900
1901 /* Check parameters */
1902 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1903 assert_param(pData != HAL_NULL);
1904 assert_param(Length != 0);
1905
1906 /* Check instance is SDADC1 */
1907 if(hsdadc->Instance != SDADC1)
1908 {
1909 status = HAL_ERROR;
1910 }
1911 /* Check that DMA is not enabled for regular conversion */
1912 else if((hsdadc->Instance->CR1 & SDADC_CR1_RDMAEN) == SDADC_CR1_RDMAEN)
1913 {
1914 status = HAL_ERROR;
1915 }
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)))
1921 {
1922 status = HAL_ERROR;
1923 }
1924 else if((hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER) && \
1925 (hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_OFF) && \
1926 (hsdadc->hdma->Init.Mode == DMA_CIRCULAR))
1927 {
1928 status = HAL_ERROR;
1929 }
1930 /* Check SDADC state */
1931 else if((hsdadc->State == HAL_SDADC_STATE_READY) || \
1932 (hsdadc->State == HAL_SDADC_STATE_REG))
1933 {
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)
1938 {
1939 hsdadc->hdma->XferHalfCpltCallback = SDADC_DMAInjectedHalfConvCplt;
1940 }
1941 /* Set JDMAEN bit in SDADC_CR1 register */
1942 hsdadc->Instance->CR1 |= SDADC_CR1_JDMAEN;
1943
1944 /* Start DMA in interrupt mode */
1945 if(hsdadc->InjectedMultimode == SDADC_MULTIMODE_SDADC1_SDADC2)
1946 {
1947 status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->JDATA12R, \
1948 (uint32_t) pData, Length);
1949 }
1950 else
1951 {
1952 status = HAL_DMA_Start_IT(hsdadc->hdma, (uint32_t)&hsdadc->Instance->JDATA13R, \
1953 (uint32_t) pData, Length);
1954 }
1955 if(status != HAL_OK)
1956 {
1957 /* Set SDADC in error state */
1958 hsdadc->State = HAL_SDADC_STATE_ERROR;
1959 status = HAL_ERROR;
1960 }
1961 else
1962 {
1963 /* Start injected conversion */
1964 status = SDADC_InjConvStart(hsdadc);
1965 }
1966 }
1967 else
1968 {
1969 status = HAL_ERROR;
1970 }
1971 /* Return function status */
1972 return status;
1973 }
1974
1975 /**
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
1980 */
1981 HAL_StatusTypeDef HAL_SDADC_InjectedMultiModeStop_DMA(SDADC_HandleTypeDef* hsdadc)
1982 {
1983 HAL_StatusTypeDef status = HAL_OK;
1984
1985 /* Check parameters */
1986 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
1987
1988 /* Check instance is SDADC1 */
1989 if(hsdadc->Instance != SDADC1)
1990 {
1991 status = HAL_ERROR;
1992 }
1993 /* Check SDADC state */
1994 else if((hsdadc->State != HAL_SDADC_STATE_INJ) && \
1995 (hsdadc->State != HAL_SDADC_STATE_REG_INJ))
1996 {
1997 /* Return error status */
1998 status = HAL_ERROR;
1999 }
2000 else
2001 {
2002 /* Clear JDMAEN bit in SDADC_CR1 register */
2003 hsdadc->Instance->CR1 &= ~(SDADC_CR1_JDMAEN);
2004
2005 /* Stop current DMA transfer */
2006 if(HAL_DMA_Abort(hsdadc->hdma) != HAL_OK)
2007 {
2008 /* Set SDADC in error state */
2009 hsdadc->State = HAL_SDADC_STATE_ERROR;
2010 status = HAL_ERROR;
2011 }
2012 else
2013 {
2014 /* Stop injected conversion */
2015 status = SDADC_InjConvStop(hsdadc);
2016 }
2017 }
2018 /* Return function status */
2019 return status;
2020 }
2021
2022 /**
2023 * @brief This function allows to get multimode injected conversion value.
2024 * @param hsdadc : SDADC handle.
2025 * @retval Multimode injected conversion value
2026 */
2027 uint32_t HAL_SDADC_InjectedMultiModeGetValue(SDADC_HandleTypeDef* hsdadc)
2028 {
2029 uint32_t value = 0;
2030
2031 /* Check parameters and check instance is SDADC1 */
2032 assert_param(IS_SDADC_ALL_INSTANCE(hsdadc->Instance));
2033 assert_param(hsdadc->Instance == SDADC1);
2034
2035 /* read multimode injected value */
2036 value = (hsdadc->InjectedMultimode == SDADC_MULTIMODE_SDADC1_SDADC2) ? \
2037 hsdadc->Instance->JDATA12R : hsdadc->Instance->JDATA13R;
2038
2039 /* Return multimode injected conversions value */
2040 return value;
2041 }
2042
2043 /**
2044 * @brief This function handles the SDADC interrupts.
2045 * @param hsdadc : SDADC handle.
2046 * @retval None
2047 */
2048 void HAL_SDADC_IRQHandler(SDADC_HandleTypeDef* hsdadc)
2049 {
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))
2053 {
2054 /* Call regular conversion complete callback */
2055 HAL_SDADC_ConvCpltCallback(hsdadc);
2056
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))
2060 {
2061 /* Clear REOCIE and ROVRIE bits in SDADC_CR1 register */
2062 hsdadc->Instance->CR1 &= ~(SDADC_CR1_REOCIE | SDADC_CR1_ROVRIE);
2063
2064 /* Update SDADC state */
2065 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_REG) ? \
2066 HAL_SDADC_STATE_READY : HAL_SDADC_STATE_INJ;
2067 }
2068 }
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))
2072 {
2073 /* Call injected conversion complete callback */
2074 HAL_SDADC_InjectedConvCpltCallback(hsdadc);
2075
2076 /* Update remaining injected conversions */
2077 hsdadc->InjConvRemaining--;
2078 if(hsdadc->InjConvRemaining ==0)
2079 {
2080 /* end of injected sequence, reset the value */
2081 hsdadc->InjConvRemaining = hsdadc->InjectedChannelsNbr;
2082 }
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))
2088 {
2089 /* Clear JEOCIE and JOVRIE bits in SDADC_CR1 register */
2090 hsdadc->Instance->CR1 &= ~(SDADC_CR1_JEOCIE | SDADC_CR1_JOVRIE);
2091
2092 /* Update SDADC state */
2093 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_INJ) ? \
2094 HAL_SDADC_STATE_READY : HAL_SDADC_STATE_REG;
2095 }
2096 }
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))
2100 {
2101 /* Clear EOCALIE bit in SDADC_CR1 register */
2102 hsdadc->Instance->CR1 &= ~(SDADC_CR1_EOCALIE);
2103
2104 /* Set CLREOCALF bit in SDADC_CLRISR register */
2105 hsdadc->Instance->CLRISR |= SDADC_ISR_CLREOCALF;
2106
2107 /* Call calibration callback */
2108 HAL_SDADC_CalibrationCpltCallback(hsdadc);
2109
2110 /* Update SDADC state */
2111 hsdadc->State = HAL_SDADC_STATE_READY;
2112 }
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))
2116 {
2117 /* Set CLRROVRF bit in SDADC_CLRISR register */
2118 hsdadc->Instance->CLRISR |= SDADC_ISR_CLRROVRF;
2119
2120 /* Update error code */
2121 hsdadc->ErrorCode = SDADC_ERROR_REGULAR_OVERRUN;
2122
2123 /* Call error callback */
2124 HAL_SDADC_ErrorCallback(hsdadc);
2125 }
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))
2129 {
2130 /* Set CLRJOVRF bit in SDADC_CLRISR register */
2131 hsdadc->Instance->CLRISR |= SDADC_ISR_CLRJOVRF;
2132
2133 /* Update error code */
2134 hsdadc->ErrorCode = SDADC_ERROR_INJECTED_OVERRUN;
2135
2136 /* Call error callback */
2137 HAL_SDADC_ErrorCallback(hsdadc);
2138 }
2139 return;
2140 }
2141
2142 /**
2143 * @brief Calibration complete callback.
2144 * @param hsdadc : SDADC handle.
2145 * @retval None
2146 */
2147 __weak void HAL_SDADC_CalibrationCpltCallback(SDADC_HandleTypeDef* hsdadc)
2148 {
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
2151 */
2152 }
2153
2154 /**
2155 * @brief Half regular conversion complete callback.
2156 * @param hsdadc : SDADC handle.
2157 * @retval None
2158 */
2159 __weak void HAL_SDADC_ConvHalfCpltCallback(SDADC_HandleTypeDef* hsdadc)
2160 {
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
2163 */
2164 }
2165
2166 /**
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.
2171 * @retval None
2172 */
2173 __weak void HAL_SDADC_ConvCpltCallback(SDADC_HandleTypeDef* hsdadc)
2174 {
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.
2177 */
2178 }
2179
2180 /**
2181 * @brief Half injected conversion complete callback.
2182 * @param hsdadc : SDADC handle.
2183 * @retval None
2184 */
2185 __weak void HAL_SDADC_InjectedConvHalfCpltCallback(SDADC_HandleTypeDef* hsdadc)
2186 {
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.
2189 */
2190 }
2191
2192 /**
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.
2197 * @retval None
2198 */
2199 __weak void HAL_SDADC_InjectedConvCpltCallback(SDADC_HandleTypeDef* hsdadc)
2200 {
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.
2203 */
2204 }
2205
2206 /**
2207 * @brief Error callback.
2208 * @param hsdadc : SDADC handle.
2209 * @retval None
2210 */
2211 __weak void HAL_SDADC_ErrorCallback(SDADC_HandleTypeDef* hsdadc)
2212 {
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.
2215 */
2216 }
2217
2218 /**
2219 * @brief DMA half transfer complete callback for regular conversion.
2220 * @param hdma : DMA handle.
2221 * @retval None
2222 */
2223 static void SDADC_DMARegularHalfConvCplt(DMA_HandleTypeDef *hdma)
2224 {
2225 /* Get SDADC handle */
2226 SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2227
2228 /* Call regular half conversion complete callback */
2229 HAL_SDADC_ConvHalfCpltCallback(hsdadc);
2230 }
2231
2232 /**
2233 * @brief DMA transfer complete callback for regular conversion.
2234 * @param hdma : DMA handle.
2235 * @retval None
2236 */
2237 static void SDADC_DMARegularConvCplt(DMA_HandleTypeDef *hdma)
2238 {
2239 /* Get SDADC handle */
2240 SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2241
2242 /* Call regular conversion complete callback */
2243 HAL_SDADC_ConvCpltCallback(hsdadc);
2244 }
2245
2246 /**
2247 * @brief DMA half transfer complete callback for injected conversion.
2248 * @param hdma : DMA handle.
2249 * @retval None
2250 */
2251 static void SDADC_DMAInjectedHalfConvCplt(DMA_HandleTypeDef *hdma)
2252 {
2253 /* Get SDADC handle */
2254 SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2255
2256 /* Call injected half conversion complete callback */
2257 HAL_SDADC_InjectedConvHalfCpltCallback(hsdadc);
2258 }
2259
2260 /**
2261 * @brief DMA transfer complete callback for injected conversion.
2262 * @param hdma : DMA handle.
2263 * @retval None
2264 */
2265 static void SDADC_DMAInjectedConvCplt(DMA_HandleTypeDef *hdma)
2266 {
2267 /* Get SDADC handle */
2268 SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2269
2270 /* Call injected conversion complete callback */
2271 HAL_SDADC_InjectedConvCpltCallback(hsdadc);
2272 }
2273
2274 /**
2275 * @brief DMA error callback.
2276 * @param hdma : DMA handle.
2277 * @retval None
2278 */
2279 static void SDADC_DMAError(DMA_HandleTypeDef *hdma)
2280 {
2281 /* Get SDADC handle */
2282 SDADC_HandleTypeDef* hsdadc = (SDADC_HandleTypeDef*) ((DMA_HandleTypeDef*)hdma)->Parent;
2283
2284 /* Update error code */
2285 hsdadc->ErrorCode = SDADC_ERROR_DMA;
2286
2287 /* Call error callback */
2288 HAL_SDADC_ErrorCallback(hsdadc);
2289 }
2290
2291 /**
2292 * @}
2293 */
2294
2295 /** @defgroup SDADC_Exported_Functions_Group4 Peripheral State functions
2296 * @brief SDADC Peripheral State functions
2297 *
2298 @verbatim
2299 ===============================================================================
2300 ##### ADC Peripheral State functions #####
2301 ===============================================================================
2302 [..] This subsection provides functions allowing to
2303 (+) Get the SDADC state
2304 (+) Get the SDADC Error
2305
2306 @endverbatim
2307 * @{
2308 */
2309
2310 /**
2311 * @brief This function allows to get the current SDADC state.
2312 * @param hsdadc : SDADC handle.
2313 * @retval SDADC state.
2314 */
2315 HAL_SDADC_StateTypeDef HAL_SDADC_GetState(SDADC_HandleTypeDef* hsdadc)
2316 {
2317 return hsdadc->State;
2318 }
2319
2320 /**
2321 * @brief This function allows to get the current SDADC error code.
2322 * @param hsdadc : SDADC handle.
2323 * @retval SDADC error code.
2324 */
2325 uint32_t HAL_SDADC_GetError(SDADC_HandleTypeDef* hsdadc)
2326 {
2327 return hsdadc->ErrorCode;
2328 }
2329
2330 /**
2331 * @}
2332 */
2333
2334 /**
2335 * @brief This function allows to enter in init mode for SDADC instance.
2336 * @param hsdadc : SDADC handle.
2337 * @retval HAL status.
2338 */
2339 static HAL_StatusTypeDef SDADC_EnterInitMode(SDADC_HandleTypeDef* hsdadc)
2340 {
2341 uint32_t tickstart = 0;
2342
2343 /* Set INIT bit on SDADC_CR1 register */
2344 hsdadc->Instance->CR1 |= SDADC_CR1_INIT;
2345
2346 /* Wait INITRDY bit on SDADC_ISR */
2347 tickstart = HAL_GetTick();
2348 while((hsdadc->Instance->ISR & SDADC_ISR_INITRDY) == (uint32_t)RESET)
2349 {
2350 if((HAL_GetTick()-tickstart) > SDADC_TIMEOUT)
2351 {
2352 return HAL_TIMEOUT;
2353 }
2354 }
2355
2356 /* Return HAL status */
2357 return HAL_OK;
2358 }
2359
2360 /**
2361 * @brief This function allows to exit from init mode for SDADC instance.
2362 * @param hsdadc : SDADC handle.
2363 * @retval None.
2364 */
2365 static void SDADC_ExitInitMode(SDADC_HandleTypeDef* hsdadc)
2366 {
2367 /* Reset INIT bit in SDADC_CR1 register */
2368 hsdadc->Instance->CR1 &= ~(SDADC_CR1_INIT);
2369 }
2370
2371 /**
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.
2375 */
2376 static uint32_t SDADC_GetInjChannelsNbr(uint32_t Channels)
2377 {
2378 uint32_t nbChannels = 0;
2379 uint32_t tmp,i;
2380
2381 /* Get the number of channels from bitfield */
2382 tmp = (uint32_t) (Channels & SDADC_LSB_MASK);
2383 for(i = 0 ; i < 9 ; i++)
2384 {
2385 if(tmp & 1)
2386 {
2387 nbChannels++;
2388 }
2389 tmp = (uint32_t) (tmp >> 1);
2390 }
2391 return nbChannels;
2392 }
2393
2394 /**
2395 * @brief This function allows to really start regular conversion.
2396 * @param hsdadc : SDADC handle.
2397 * @retval HAL status.
2398 */
2399 static HAL_StatusTypeDef SDADC_RegConvStart(SDADC_HandleTypeDef* hsdadc)
2400 {
2401 HAL_StatusTypeDef status = HAL_OK;
2402
2403 /* Check regular trigger */
2404 if(hsdadc->RegularTrigger == SDADC_SOFTWARE_TRIGGER)
2405 {
2406 /* Set RSWSTART bit in SDADC_CR2 register */
2407 hsdadc->Instance->CR2 |= SDADC_CR2_RSWSTART;
2408 }
2409 else /* synchronuous trigger */
2410 {
2411 /* Enter init mode */
2412 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
2413 {
2414 /* Set SDADC in error state */
2415 hsdadc->State = HAL_SDADC_STATE_ERROR;
2416 status = HAL_TIMEOUT;
2417 }
2418 else
2419 {
2420 /* Set RSYNC bit in SDADC_CR1 register */
2421 hsdadc->Instance->CR1 |= SDADC_CR1_RSYNC;
2422
2423 /* Exit init mode */
2424 SDADC_ExitInitMode(hsdadc);
2425 }
2426 }
2427 /* Update SDADC state only if status is OK */
2428 if(status == HAL_OK)
2429 {
2430 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_READY) ? \
2431 HAL_SDADC_STATE_REG : HAL_SDADC_STATE_REG_INJ;
2432 }
2433 /* Return function status */
2434 return status;
2435 }
2436
2437 /**
2438 * @brief This function allows to really stop regular conversion.
2439 * @param hsdadc : SDADC handle.
2440 * @retval HAL status.
2441 */
2442 static HAL_StatusTypeDef SDADC_RegConvStop(SDADC_HandleTypeDef* hsdadc)
2443 {
2444 uint32_t tickstart;
2445
2446 /* Check continuous mode */
2447 if(hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_ON)
2448 {
2449 /* Clear REOCF by reading SDADC_RDATAR register */
2450 hsdadc->Instance->RDATAR;
2451
2452 /* Clear RCONT bit in SDADC_CR2 register */
2453 hsdadc->Instance->CR2 &= ~(SDADC_CR2_RCONT);
2454 }
2455 /* Wait for the end of regular conversion */
2456 tickstart = HAL_GetTick();
2457 while((hsdadc->Instance->ISR & SDADC_ISR_RCIP) != 0)
2458 {
2459 if((HAL_GetTick()-tickstart) > SDADC_TIMEOUT)
2460 {
2461 /* Set SDADC in error state and return timeout status */
2462 hsdadc->State = HAL_SDADC_STATE_ERROR;
2463 return HAL_TIMEOUT;
2464 }
2465 }
2466 /* Check if trigger is synchronuous */
2467 if(hsdadc->RegularTrigger == SDADC_SYNCHRONOUS_TRIGGER)
2468 {
2469 /* Enter init mode */
2470 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
2471 {
2472 /* Set SDADC in error state and return timeout status */
2473 hsdadc->State = HAL_SDADC_STATE_ERROR;
2474 return HAL_TIMEOUT;
2475 }
2476 else
2477 {
2478 /* Clear RSYNC bit in SDADC_CR1 register */
2479 hsdadc->Instance->CR1 &= ~(SDADC_CR1_RSYNC);
2480
2481 /* Exit init mode */
2482 SDADC_ExitInitMode(hsdadc);
2483 }
2484 }
2485 /* Check if continuous mode */
2486 if(hsdadc->RegularContMode == SDADC_CONTINUOUS_CONV_ON)
2487 {
2488 /* Restore RCONT bit in SDADC_CR2 register */
2489 hsdadc->Instance->CR2 |= SDADC_CR2_RCONT;
2490 }
2491 /* Clear REOCF by reading SDADC_RDATAR register */
2492 hsdadc->Instance->RDATAR;
2493
2494 /* Set CLRROVRF bit in SDADC_CLRISR register */
2495 hsdadc->Instance->CLRISR |= SDADC_ISR_CLRROVRF;
2496
2497 /* Update SDADC state */
2498 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_REG) ? \
2499 HAL_SDADC_STATE_READY : HAL_SDADC_STATE_INJ;
2500
2501 /* Return function status */
2502 return HAL_OK;
2503 }
2504
2505 /**
2506 * @brief This function allows to really start injected conversion.
2507 * @param hsdadc : SDADC handle.
2508 * @retval HAL status.
2509 */
2510 static HAL_StatusTypeDef SDADC_InjConvStart(SDADC_HandleTypeDef* hsdadc)
2511 {
2512 HAL_StatusTypeDef status = HAL_OK;
2513
2514 /* Initialize number of injected conversions remaining */
2515 hsdadc->InjConvRemaining = hsdadc->InjectedChannelsNbr;
2516
2517 /* Check injected trigger */
2518 if(hsdadc->InjectedTrigger == SDADC_SOFTWARE_TRIGGER)
2519 {
2520 /* Set JSWSTART bit in SDADC_CR2 register */
2521 hsdadc->Instance->CR2 |= SDADC_CR2_JSWSTART;
2522 }
2523 else /* external or synchronuous trigger */
2524 {
2525 /* Enter init mode */
2526 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
2527 {
2528 /* Set SDADC in error state */
2529 hsdadc->State = HAL_SDADC_STATE_ERROR;
2530 status = HAL_TIMEOUT;
2531 }
2532 else
2533 {
2534 if(hsdadc->InjectedTrigger == SDADC_SYNCHRONOUS_TRIGGER)
2535 {
2536 /* Set JSYNC bit in SDADC_CR1 register */
2537 hsdadc->Instance->CR1 |= SDADC_CR1_JSYNC;
2538 }
2539 else /* external trigger */
2540 {
2541 /* Set JEXTEN[1:0] bits in SDADC_CR2 register */
2542 hsdadc->Instance->CR2 |= hsdadc->ExtTriggerEdge;
2543 }
2544 /* Exit init mode */
2545 SDADC_ExitInitMode(hsdadc);
2546 }
2547 }
2548 /* Update SDADC state only if status is OK */
2549 if(status == HAL_OK)
2550 {
2551 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_READY) ? \
2552 HAL_SDADC_STATE_INJ : HAL_SDADC_STATE_REG_INJ;
2553 }
2554 /* Return function status */
2555 return status;
2556 }
2557
2558 /**
2559 * @brief This function allows to really stop injected conversion.
2560 * @param hsdadc : SDADC handle.
2561 * @retval HAL status.
2562 */
2563 static HAL_StatusTypeDef SDADC_InjConvStop(SDADC_HandleTypeDef* hsdadc)
2564 {
2565 uint32_t tickstart;
2566
2567 /* Check continuous mode */
2568 if(hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_ON)
2569 {
2570 /* Clear JEOCF by reading SDADC_JDATAR register */
2571 hsdadc->Instance->JDATAR;
2572
2573 /* Clear JCONT bit in SDADC_CR2 register */
2574 hsdadc->Instance->CR2 &= ~(SDADC_CR2_JCONT);
2575 }
2576 /* Wait for the end of injected conversion */
2577 tickstart = HAL_GetTick();
2578 while((hsdadc->Instance->ISR & SDADC_ISR_JCIP) != 0)
2579 {
2580 if((HAL_GetTick()-tickstart) > SDADC_TIMEOUT)
2581 {
2582 /* Set SDADC in error state and return timeout status */
2583 hsdadc->State = HAL_SDADC_STATE_ERROR;
2584 return HAL_TIMEOUT;
2585 }
2586 }
2587 /* Check if trigger is not software */
2588 if(hsdadc->InjectedTrigger != SDADC_SOFTWARE_TRIGGER)
2589 {
2590 /* Enter init mode */
2591 if(SDADC_EnterInitMode(hsdadc) != HAL_OK)
2592 {
2593 /* Set SDADC in error state and return timeout status */
2594 hsdadc->State = HAL_SDADC_STATE_ERROR;
2595 return HAL_TIMEOUT;
2596 }
2597 else
2598 {
2599 /* Check if trigger is synchronuous */
2600 if(hsdadc->InjectedTrigger == SDADC_SYNCHRONOUS_TRIGGER)
2601 {
2602 /* Clear JSYNC bit in SDADC_CR1 register */
2603 hsdadc->Instance->CR1 &= ~(SDADC_CR1_JSYNC);
2604 }
2605 else /* external trigger */
2606 {
2607 /* Clear JEXTEN[1:0] bits in SDADC_CR2 register */
2608 hsdadc->Instance->CR2 &= ~(SDADC_CR2_JEXTEN);
2609 }
2610 /* Exit init mode */
2611 SDADC_ExitInitMode(hsdadc);
2612 }
2613 }
2614 /* Check if continuous mode */
2615 if(hsdadc->InjectedContMode == SDADC_CONTINUOUS_CONV_ON)
2616 {
2617 /* Restore JCONT bit in SDADC_CR2 register */
2618 hsdadc->Instance->CR2 |= SDADC_CR2_JCONT;
2619 }
2620 /* Clear JEOCF by reading SDADC_JDATAR register */
2621 hsdadc->Instance->JDATAR;
2622
2623 /* Set CLRJOVRF bit in SDADC_CLRISR register */
2624 hsdadc->Instance->CLRISR |= SDADC_ISR_CLRJOVRF;
2625
2626 /* Update SDADC state */
2627 hsdadc->State = (hsdadc->State == HAL_SDADC_STATE_INJ) ? \
2628 HAL_SDADC_STATE_READY : HAL_SDADC_STATE_REG;
2629
2630 /* Return function status */
2631 return HAL_OK;
2632 }
2633
2634 /**
2635 * @}
2636 */
2637
2638 /**
2639 * @}
2640 */
2641
2642 #endif /* defined(STM32F373xC) || defined(STM32F378xx) */
2643 #endif /* HAL_SDADC_MODULE_ENABLED */
2644 /**
2645 * @}
2646 */
2647
2648 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Imprint / Impressum