1 /**
2 **********************************************************************************************************************
3 * @file stm32h5xx_hal_dts.c
4 * @author MCD Application Team
5 * @brief DTS HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the DTS peripheral:
8 * + Initialization and de-initialization functions
9 * + Start/Stop operation functions in polling mode.
10 * + Start/Stop operation functions in interrupt mode.
11 * + Peripheral Control functions
12 * + Peripheral State functions
13 *
14 **********************************************************************************************************************
15 * @attention
16 *
17 * Copyright (c) 2022 STMicroelectronics.
18 * All rights reserved.
19 *
20 * This software is licensed under terms that can be found in the LICENSE file
21 * in the root directory of this software component.
22 * If no LICENSE file comes with this software, it is provided AS-IS.
23 *
24 **********************************************************************************************************************
25 @verbatim
26 ======================================================================================================================
27 ##### DTS Peripheral features #####
28 ======================================================================================================================
29
30 [..]
31 The STM32h5xx device family integrate one DTS sensor interface that converts the temperature into a square wave
32 which frequency is proportional to the temperature.
33
34 (+) Temperature window comparator feature:
35 The DTS allows defining the high and low threshold that will be used for temperature comparison.
36 If the temperature data is equal or higher than the high threshold, or equal or lower than the low threshold,
37 an interrupt is generated and the corresponding flag.
38
39 (+) Synchronous interrupts:
40 A global interrupt output line is available on the DTS block. The interrupt can be generated
41 at the end of measurement and/or when the measurement result is equal/higher or equal/lower than
42 a predefined threshold.
43
44 (+) Asynchronous wakeup:
45 The DTS block provides an asynchronous interrupt line. It is used only when the LSE is selected as reference
46 clock. This line can generate a signal that wakes up the system from Sleep and stop mode at the end of
47 measurement and/or when the measurement result is equal/higher or equal/lower than a
48 predefined threshold.
49
50 (+) Trigger inputs:
51 The temperature measurement can be triggered either by software or by an external event
52 as well as lowpower timer.
53
54 (+) Measurement modes:
55 (++) Quick measure : measure without a calibration when a high precision is not required.
56 (++) Slow measure : measure with a calibration used when a high precision is required.
57
58 (+) Sampling time:
59 The sampling time can be used to increase temperature measurement precision.
60
61 (+) Prescaler:
62 When a calibration is ongoing, the counter clock must be slower than 1 MHz. This is
63 achieved by the PCLK clock prescaler embedded in the temperature sensor.
64
65 (+) Operating Modes:
66
67 (++) PCLK only : The temperature sensor registers can be accessed. The interface can consequently be
68 reconfigured and the measurement sequence is performed using PCLK clock.
69
70 (++) PCLK and LSE : The temperature sensor registers can be accessed. The interface can consequently be
71 reconfigured and the measurement sequence is performed using the LSE clock.
72
73 (++) LSE only : The registers cannot be accessed. The measurement can be performed using the LSE
74 clock This mode is used to exit from Sleep and stop mode by using hardware triggers
75 and the asynchronous interrupt line.
76 (+) Calibration:
77 The temperature sensor must run the calibration prior to any frequency measurement. The calibration is
78 performed automatically when the temperature measurement is triggered except for quick measurement mode.
79
80 (+) Low power modes:
81 (++) Sleep mode: only works in LSE mode.
82 (++) Stop mode: only works in LSE mode.
83
84 ======================================================================================================================
85 ##### How to use this driver #####
86 ======================================================================================================================
87 [..]
88 This driver provides functions to configure and program the DTS instances of STM32H5xx devices.
89
90 To use the DTS, perform the following steps:
91
92 (+) Initialize the DTS low level resources by implementing the HAL_DTS_MspInit():
93
94 (++) If required enable the DTS interrupt by configuring and enabling EXTI line in Interrupt mode After that
95 enable the DTS interrupt vector using HAL_NVIC_EnableIRQ() function.
96
97 (+) Configure the DTS using HAL_DTS_Init() function:
98
99 (++) Select the quick measure option
100 (++) Select the reference clock.
101 (++) Select the trigger input.
102 (++) Select the sampling time.
103 (++) Select the high speed clock ratio divider.
104 (++) Select the low threshold
105 (++) Select the high threshold
106
107
108 (+) Use HAL_DTS_Start() to enable and start the DTS sensor in polling mode.
109
110 (+) Use HAL_DTS_Start_IT() to enable and start the DTS sensor in interrupt mode.
111
112 (+) Use HAL_DTS_GetTemperature() to get temperature from DTS.
113
114 (+) Use HAL_DTS_Stop() to disable and stop the DTS sensor in polling mode.
115
116 (+) Use HAL_DTS_Stop_IT() to disable and stop the DTS sensor in interrupt mode.
117
118 (+) De-initialize the DTS using HAL_DTS_DeInit() function.
119
120 *** Callback and interrupts ***
121 =============================================
122 [..]
123 When the compilation flag USE_HAL_DTS_REGISTER_CALLBACKS is set to 0 or not defined, the callback registration
124 feature is not available and all callbacks are set to the corresponding weak functions.
125
126 (+) Implement the weak function HAL_DTS_EndCallback() to get a callback at the end of the temperature measurement.
127 (+) Implement the weak function HAL_DTS_HighCallback() to get a callback when the temperature
128 exceed the high threshold.
129 (+) Implement the weak function HAL_DTS_LowCallback() to get a callback when the temperature
130 go down the low threshold.
131 (+) Implement the weak function HAL_DTS_AsyncEndCallback() to get a callback at the end
132 of an asynchronous temperature measurement.
133 (+) Implement the weak function HAL_DTS_AsyncHighCallback() to get a callback when the temperature
134 exceed the high threshold in an asynchronous measurement.
135 (+) Implement the weak function HAL_DTS_AsyncLowCallback() to get a callback when the temperature
136 go down the low threshold in an asynchronous measurement.
137
138 [..]
139 The compilation flag USE_HAL_DTS_REGISTER_CALLBACKS, when set to 1, allows the user to configure dynamically
140 the driver callbacks.
141
142 *** State ***
143 =============================================
144 [..]
145 The driver permits to get in run time the status of the peripheral.
146
147 (+) Use HAL_DTS_GetState() to get the state of the DTS.
148
149 @endverbatim
150 **********************************************************************************************************************
151 */
152
153 /* Includes ----------------------------------------------------------------------------------------------------------*/
154 #include "stm32h5xx_hal.h"
155
156 /** @addtogroup STM32H5xx_HAL_Driver
157 * @{
158 */
159
160 #ifdef HAL_DTS_MODULE_ENABLED
161
162 #if defined(DTS)
163
164 /** @defgroup DTS DTS
165 * @brief DTS HAL module driver
166 * @{
167 */
168
169 /* Private typedef ---------------------------------------------------------------------------------------------------*/
170 /* Private define ----------------------------------------------------------------------------------------------------*/
171 /** @addtogroup DTS_Private_Constants
172 * @{
173 */
174
175 /* @brief Delay for DTS startup time
176 * @note Delay required to get ready for DTS Block.
177 * @note Unit: ms
178 */
179 #define DTS_DELAY_STARTUP (1UL)
180
181 /* @brief DTS measure ready flag time out value.
182 * @note Maximal measurement time is when LSE is selected as ref_clock and
183 * maximal sampling time is used, taking calibration into account this
184 * is equivalent to ~620 us. Use 5 ms as arbitrary timeout
185 * @note Unit: ms
186 */
187 #define TS_TIMEOUT_MS (5UL)
188
189 /* @brief DTS factory temperatures
190 * @note Unit: degree Celsius
191 */
192 #define DTS_FACTORY_TEMPERATURE1 (30UL)
193 #define DTS_FACTORY_TEMPERATURE2 (130UL)
194
195 /* Private macro -----------------------------------------------------------------------------------------------------*/
196 /* Private variables -------------------------------------------------------------------------------------------------*/
197 /* Private function prototypes ---------------------------------------------------------------------------------------*/
198 /* Exported functions ------------------------------------------------------------------------------------------------*/
199 /** @defgroup DTS_Exported_Functions DTS Exported Functions
200 * @{
201 */
202
203 /** @defgroup DTS_Exported_Functions_Group1 Initialization/de-initialization functions
204 * @brief Initialization and de-initialization functions.
205 *
206 @verbatim
207 =======================================================================================================================
208 ##### Initialization and de-initialization functions #####
209 =======================================================================================================================
210 [..] This section provides functions to initialize and de-initialize DTS
211
212 @endverbatim
213 * @{
214 */
215
216 /**
217 * @brief Initialize the DTS according to the specified
218 * parameters in the DTS_InitTypeDef and initialize the associated handle.
219 * @param hdts DTS handle
220 * @retval HAL status
221 */
HAL_DTS_Init(DTS_HandleTypeDef * hdts)222 HAL_StatusTypeDef HAL_DTS_Init(DTS_HandleTypeDef *hdts)
223 {
224 /* Check the DTS handle allocation */
225 if (hdts == NULL)
226 {
227 return HAL_ERROR;
228 }
229
230 /* Check the parameters */
231 assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
232 assert_param(IS_DTS_QUICKMEAS(hdts->Init.QuickMeasure));
233 assert_param(IS_DTS_REFCLK(hdts->Init.RefClock));
234 assert_param(IS_DTS_TRIGGERINPUT(hdts->Init.TriggerInput));
235 assert_param(IS_DTS_SAMPLINGTIME(hdts->Init.SamplingTime));
236 assert_param(IS_DTS_THRESHOLD(hdts->Init.HighThreshold));
237 assert_param(IS_DTS_THRESHOLD(hdts->Init.LowThreshold));
238
239 if (hdts->State == HAL_DTS_STATE_RESET)
240 {
241 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
242 /* Reset the DTS callback to the legacy weak callbacks */
243 hdts->EndCallback = HAL_DTS_EndCallback; /* End measure Callback */
244 hdts->LowCallback = HAL_DTS_LowCallback; /* low threshold Callback */
245 hdts->HighCallback = HAL_DTS_HighCallback; /* high threshold Callback */
246 hdts->AsyncEndCallback = HAL_DTS_AsyncEndCallback; /* Asynchronous end of measure Callback */
247 hdts->AsyncLowCallback = HAL_DTS_AsyncLowCallback; /* Asynchronous low threshold Callback */
248 hdts->AsyncHighCallback = HAL_DTS_AsyncHighCallback; /* Asynchronous high threshold Callback */
249
250 if (hdts->MspInitCallback == NULL)
251 {
252 hdts->MspInitCallback = HAL_DTS_MspInit;
253 }
254
255 /* Init the low level hardware : GPIO, CLOCK, NVIC */
256 hdts->MspInitCallback(hdts);
257 #else
258 /* Init the low level hardware : GPIO, CLOCK, NVIC */
259 HAL_DTS_MspInit(hdts);
260 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
261 }
262
263 /* Change the DTS state */
264 hdts->State = HAL_DTS_STATE_BUSY;
265
266 /* Check ramp coefficient */
267 if (hdts->Instance->RAMPVALR == 0UL)
268 {
269 return HAL_ERROR;
270 }
271
272 /* Check factory calibration temperature */
273 if (hdts->Instance->T0VALR1 == 0UL)
274 {
275 return HAL_ERROR;
276 }
277
278 /* Check Quick Measure option is enabled or disabled */
279 if (hdts->Init.QuickMeasure == DTS_QUICKMEAS_DISABLE)
280 {
281 /* Check Reference clock selection */
282 if (hdts->Init.RefClock == DTS_REFCLKSEL_PCLK)
283 {
284 assert_param(IS_DTS_DIVIDER_RATIO_NUMBER(hdts->Init.Divider));
285 }
286 /* Quick measurement mode disabled */
287 CLEAR_BIT(hdts->Instance->CFGR1, DTS_CFGR1_Q_MEAS_OPT);
288 }
289 else
290 {
291 /* DTS_QUICKMEAS_ENABLE shall be used only when the LSE clock is
292 selected as reference clock */
293 if (hdts->Init.RefClock != DTS_REFCLKSEL_LSE)
294 {
295 return HAL_ERROR;
296 }
297
298 /* Quick measurement mode enabled - no calibration needed */
299 SET_BIT(hdts->Instance->CFGR1, DTS_CFGR1_Q_MEAS_OPT);
300 }
301
302 /* set the DTS clk source */
303 if (hdts->Init.RefClock == DTS_REFCLKSEL_LSE)
304 {
305 SET_BIT(hdts->Instance->CFGR1, DTS_CFGR1_REFCLK_SEL);
306 }
307 else
308 {
309 CLEAR_BIT(hdts->Instance->CFGR1, DTS_CFGR1_REFCLK_SEL);
310 }
311
312 MODIFY_REG(hdts->Instance->CFGR1, DTS_CFGR1_HSREF_CLK_DIV, (hdts->Init.Divider << DTS_CFGR1_HSREF_CLK_DIV_Pos));
313 MODIFY_REG(hdts->Instance->CFGR1, DTS_CFGR1_TS1_SMP_TIME, hdts->Init.SamplingTime);
314 MODIFY_REG(hdts->Instance->CFGR1, DTS_CFGR1_TS1_INTRIG_SEL, hdts->Init.TriggerInput);
315 MODIFY_REG(hdts->Instance->ITR1, DTS_ITR1_TS1_HITTHD, (hdts->Init.HighThreshold << DTS_ITR1_TS1_HITTHD_Pos));
316 MODIFY_REG(hdts->Instance->ITR1, DTS_ITR1_TS1_LITTHD, hdts->Init.LowThreshold);
317
318 /* Change the DTS state */
319 hdts->State = HAL_DTS_STATE_READY;
320
321 return HAL_OK;
322 }
323
324 /**
325 * @brief DeInitialize the DTS peripheral.
326 * @note Deinitialization cannot be performed if the DTS configuration is locked.
327 * To unlock the configuration, perform a system reset.
328 * @param hdts DTS handle
329 * @retval HAL status
330 */
HAL_DTS_DeInit(DTS_HandleTypeDef * hdts)331 HAL_StatusTypeDef HAL_DTS_DeInit(DTS_HandleTypeDef *hdts)
332 {
333 /* Check the DTS handle allocation */
334 if (hdts == NULL)
335 {
336 return HAL_ERROR;
337 }
338
339 /* Check the parameter */
340 assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
341
342 /* Set DTS_CFGR register to reset value */
343 CLEAR_REG(hdts->Instance->CFGR1);
344
345 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
346 if (hdts->MspDeInitCallback == NULL)
347 {
348 hdts->MspDeInitCallback = HAL_DTS_MspDeInit;
349 }
350
351 /* DeInit the low level hardware: CLOCK, NVIC.*/
352 hdts->MspDeInitCallback(hdts);
353 #else
354 /* DeInit the low level hardware: CLOCK, NVIC.*/
355 HAL_DTS_MspDeInit(hdts);
356 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
357
358 hdts->State = HAL_DTS_STATE_RESET;
359
360 return HAL_OK;
361 }
362
363 /**
364 * @brief Initialize the DTS MSP.
365 * @param hdts DTS handle
366 * @retval None
367 */
HAL_DTS_MspInit(DTS_HandleTypeDef * hdts)368 __weak void HAL_DTS_MspInit(DTS_HandleTypeDef *hdts)
369 {
370 /* Prevent unused argument(s) compilation warning */
371 UNUSED(hdts);
372
373 /* NOTE : This function should not be modified, when the callback is needed,
374 the HAL_DTS_MspInit could be implemented in the user file
375 */
376 }
377
378 /**
379 * @brief DeInitialize the DTS MSP.
380 * @param hdts DTS handle
381 * @retval None
382 */
HAL_DTS_MspDeInit(DTS_HandleTypeDef * hdts)383 __weak void HAL_DTS_MspDeInit(DTS_HandleTypeDef *hdts)
384 {
385 /* Prevent unused argument(s) compilation warning */
386 UNUSED(hdts);
387
388 /* NOTE : This function should not be modified, when the callback is needed,
389 the HAL_DTS_MspDeInit could be implemented in the user file
390 */
391 }
392
393 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
394 /**
395 * @brief Register a user DTS callback to be used instead of the weak predefined callback.
396 * @param hdts DTS handle.
397 * @param CallbackID ID of the callback to be registered.
398 * This parameter can be one of the following values:
399 * @arg @ref HAL_DTS_MEAS_COMPLETE_CB_ID measure complete callback ID.
400 * @arg @ref HAL_DTS_ASYNC_MEAS_COMPLETE_CB_ID asynchronous measure complete callback ID.
401 * @arg @ref HAL_DTS_LOW_THRESHOLD_CB_ID low threshold detection callback ID.
402 * @arg @ref HAL_DTS_ASYNC_LOW_THRESHOLD_CB_ID asynchronous low threshold detection callback ID.
403 * @arg @ref HAL_DTS_HIGH_THRESHOLD_CB_ID high threshold detection callback ID.
404 * @arg @ref HAL_DTS_ASYNC_HIGH_THRESHOLD_CB_ID asynchronous high threshold detection callback ID.
405 * @arg @ref HAL_DTS_MSPINIT_CB_ID MSP init callback ID.
406 * @arg @ref HAL_DTS_MSPDEINIT_CB_ID MSP de-init callback ID.
407 * @param pCallback pointer to the callback function.
408 * @retval HAL status.
409 */
HAL_DTS_RegisterCallback(DTS_HandleTypeDef * hdts,HAL_DTS_CallbackIDTypeDef CallbackID,pDTS_CallbackTypeDef pCallback)410 HAL_StatusTypeDef HAL_DTS_RegisterCallback(DTS_HandleTypeDef *hdts,
411 HAL_DTS_CallbackIDTypeDef CallbackID,
412 pDTS_CallbackTypeDef pCallback)
413 {
414 HAL_StatusTypeDef status = HAL_OK;
415
416 /* Check parameters */
417 if (pCallback == NULL)
418 {
419 /* Update status */
420 status = HAL_ERROR;
421 }
422 else
423 {
424 if (hdts->State == HAL_DTS_STATE_READY)
425 {
426 switch (CallbackID)
427 {
428 case HAL_DTS_MEAS_COMPLETE_CB_ID :
429 hdts->EndCallback = pCallback;
430 break;
431 case HAL_DTS_ASYNC_MEAS_COMPLETE_CB_ID :
432 hdts->AsyncEndCallback = pCallback;
433 break;
434 case HAL_DTS_LOW_THRESHOLD_CB_ID :
435 hdts->LowCallback = pCallback;
436 break;
437 case HAL_DTS_ASYNC_LOW_THRESHOLD_CB_ID :
438 hdts->AsyncLowCallback = pCallback;
439 break;
440 case HAL_DTS_HIGH_THRESHOLD_CB_ID :
441 hdts->HighCallback = pCallback;
442 break;
443 case HAL_DTS_ASYNC_HIGH_THRESHOLD_CB_ID :
444 hdts->AsyncHighCallback = pCallback;
445 break;
446 case HAL_DTS_MSPINIT_CB_ID :
447 hdts->MspInitCallback = pCallback;
448 break;
449 case HAL_DTS_MSPDEINIT_CB_ID :
450 hdts->MspDeInitCallback = pCallback;
451 break;
452 default :
453 /* Update status */
454 status = HAL_ERROR;
455 break;
456 }
457 }
458 else if (hdts->State == HAL_DTS_STATE_RESET)
459 {
460 switch (CallbackID)
461 {
462 case HAL_DTS_MSPINIT_CB_ID :
463 hdts->MspInitCallback = pCallback;
464 break;
465 case HAL_DTS_MSPDEINIT_CB_ID :
466 hdts->MspDeInitCallback = pCallback;
467 break;
468 default :
469 /* Update status */
470 status = HAL_ERROR;
471 break;
472 }
473 }
474 else
475 {
476 /* Update status */
477 status = HAL_ERROR;
478 }
479 }
480
481 /* Return function status */
482 return status;
483 }
484
485 /**
486 * @brief Unregister a user DTS callback.
487 * DTS callback is redirected to the weak predefined callback.
488 * @param hdts DTS handle.
489 * @param CallbackID ID of the callback to be unregistered.
490 * This parameter can be one of the following values:
491 * @arg @ref HAL_DTS_MEAS_COMPLETE_CB_ID measure complete callback ID.
492 * @arg @ref HAL_DTS_ASYNC_MEAS_COMPLETE_CB_ID asynchronous measure complete callback ID.
493 * @arg @ref HAL_DTS_LOW_THRESHOLD_CB_ID low threshold detection callback ID.
494 * @arg @ref HAL_DTS_ASYNC_LOW_THRESHOLD_CB_ID asynchronous low threshold detection callback ID.
495 * @arg @ref HAL_DTS_HIGH_THRESHOLD_CB_ID high threshold detection callback ID.
496 * @arg @ref HAL_DTS_ASYNC_HIGH_THRESHOLD_CB_ID asynchronous high threshold detection callback ID.
497 * @arg @ref HAL_DTS_MSPINIT_CB_ID MSP init callback ID.
498 * @arg @ref HAL_DTS_MSPDEINIT_CB_ID MSP de-init callback ID.
499 * @retval HAL status.
500 */
HAL_DTS_UnRegisterCallback(DTS_HandleTypeDef * hdts,HAL_DTS_CallbackIDTypeDef CallbackID)501 HAL_StatusTypeDef HAL_DTS_UnRegisterCallback(DTS_HandleTypeDef *hdts,
502 HAL_DTS_CallbackIDTypeDef CallbackID)
503 {
504 HAL_StatusTypeDef status = HAL_OK;
505
506 if (hdts->State == HAL_DTS_STATE_READY)
507 {
508 switch (CallbackID)
509 {
510 case HAL_DTS_MEAS_COMPLETE_CB_ID :
511 hdts->EndCallback = HAL_DTS_EndCallback;
512 break;
513 case HAL_DTS_ASYNC_MEAS_COMPLETE_CB_ID :
514 hdts->AsyncEndCallback = HAL_DTS_AsyncEndCallback;
515 break;
516 case HAL_DTS_LOW_THRESHOLD_CB_ID :
517 hdts->LowCallback = HAL_DTS_LowCallback;
518 break;
519 case HAL_DTS_ASYNC_LOW_THRESHOLD_CB_ID :
520 hdts->AsyncLowCallback = HAL_DTS_AsyncLowCallback;
521 break;
522 case HAL_DTS_HIGH_THRESHOLD_CB_ID :
523 hdts->HighCallback = HAL_DTS_HighCallback;
524 break;
525 case HAL_DTS_ASYNC_HIGH_THRESHOLD_CB_ID :
526 hdts->AsyncHighCallback = HAL_DTS_AsyncHighCallback;
527 break;
528 case HAL_DTS_MSPINIT_CB_ID :
529 hdts->MspInitCallback = HAL_DTS_MspInit;
530 break;
531 case HAL_DTS_MSPDEINIT_CB_ID :
532 hdts->MspDeInitCallback = HAL_DTS_MspDeInit;
533 break;
534 default :
535 /* Update status */
536 status = HAL_ERROR;
537 break;
538 }
539 }
540 else if (hdts->State == HAL_DTS_STATE_RESET)
541 {
542 switch (CallbackID)
543 {
544 case HAL_DTS_MSPINIT_CB_ID :
545 hdts->MspInitCallback = HAL_DTS_MspInit;
546 break;
547 case HAL_DTS_MSPDEINIT_CB_ID :
548 hdts->MspDeInitCallback = HAL_DTS_MspDeInit;
549 break;
550 default :
551 /* Update status */
552 status = HAL_ERROR;
553 break;
554 }
555 }
556 else
557 {
558 /* Update status */
559 status = HAL_ERROR;
560 }
561
562 /* Return function status */
563 return status;
564 }
565 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
566
567 /**
568 * @}
569 */
570
571 /** @defgroup DTS_Exported_Functions_Group2 Start-Stop operation functions
572 * @brief Start-Stop operation functions.
573 *
574 @verbatim
575 =======================================================================================================================
576 ##### DTS Start Stop operation functions #####
577 =======================================================================================================================
578 [..] This section provides functions allowing to:
579 (+) Start a DTS Sensor without interrupt.
580 (+) Stop a DTS Sensor without interrupt.
581 (+) Start a DTS Sensor with interrupt generation.
582 (+) Stop a DTS Sensor with interrupt generation.
583
584 @endverbatim
585 * @{
586 */
587
588 /**
589 * @brief Start the DTS sensor.
590 * @param hdts DTS handle
591 * @retval HAL status
592 */
HAL_DTS_Start(DTS_HandleTypeDef * hdts)593 HAL_StatusTypeDef HAL_DTS_Start(DTS_HandleTypeDef *hdts)
594 {
595 uint32_t Ref_Time;
596
597 /* Check the DTS handle allocation */
598 if (hdts == NULL)
599 {
600 return HAL_ERROR;
601 }
602
603 if (hdts->State == HAL_DTS_STATE_READY)
604 {
605 hdts->State = HAL_DTS_STATE_BUSY;
606
607 /* Enable DTS sensor */
608 __HAL_DTS_ENABLE(hdts);
609
610 /* Get Start Tick*/
611 Ref_Time = HAL_GetTick();
612
613 /* Wait till TS1_RDY flag is set */
614 while (__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_RDY) == RESET)
615 {
616 if ((HAL_GetTick() - Ref_Time) > DTS_DELAY_STARTUP)
617 {
618 return HAL_TIMEOUT;
619 }
620 }
621
622 if (__HAL_DTS_GET_TRIGGER(hdts) == DTS_TRIGGER_HW_NONE)
623 {
624 /* Start continuous measures */
625 SET_BIT(hdts->Instance->CFGR1, DTS_CFGR1_TS1_START);
626
627 /* Ensure start is taken into account */
628 HAL_Delay(TS_TIMEOUT_MS);
629 }
630
631 hdts->State = HAL_DTS_STATE_READY;
632 }
633 else
634 {
635 return HAL_BUSY;
636 }
637
638 return HAL_OK;
639 }
640
641 /**
642 * @brief Stop the DTS Sensor.
643 * @param hdts DTS handle
644 * @retval HAL status
645 */
HAL_DTS_Stop(DTS_HandleTypeDef * hdts)646 HAL_StatusTypeDef HAL_DTS_Stop(DTS_HandleTypeDef *hdts)
647 {
648 /* Check the DTS handle allocation */
649 if (hdts == NULL)
650 {
651 return HAL_ERROR;
652 }
653
654 if (hdts->State == HAL_DTS_STATE_READY)
655 {
656 hdts->State = HAL_DTS_STATE_BUSY;
657
658 if (__HAL_DTS_GET_TRIGGER(hdts) == DTS_TRIGGER_HW_NONE)
659 {
660 CLEAR_BIT(hdts->Instance->CFGR1, DTS_CFGR1_TS1_START);
661 }
662
663 /* Disable the selected DTS sensor */
664 __HAL_DTS_DISABLE(hdts);
665
666 hdts->State = HAL_DTS_STATE_READY;
667 }
668 else
669 {
670 return HAL_BUSY;
671 }
672
673 return HAL_OK;
674 }
675
676 /**
677 * @brief Enable the interrupt(s) and start the DTS sensor
678 * @param hdts DTS handle
679 * @retval HAL status
680 */
HAL_DTS_Start_IT(DTS_HandleTypeDef * hdts)681 HAL_StatusTypeDef HAL_DTS_Start_IT(DTS_HandleTypeDef *hdts)
682 {
683 uint32_t Ref_Time;
684
685 /* Check the DTS handle allocation */
686 if (hdts == NULL)
687 {
688 return HAL_ERROR;
689 }
690
691 if (hdts->State == HAL_DTS_STATE_READY)
692 {
693 hdts->State = HAL_DTS_STATE_BUSY;
694
695 /* On Asynchronous mode enable the asynchronous IT */
696 if (hdts->Init.RefClock == DTS_REFCLKSEL_LSE)
697 {
698 __HAL_DTS_ENABLE_IT(hdts, DTS_IT_TS1_AITE | DTS_IT_TS1_AITL | DTS_IT_TS1_AITH);
699 }
700 else
701 {
702 /* Enable the IT(s) */
703 __HAL_DTS_ENABLE_IT(hdts, DTS_IT_TS1_ITE | DTS_IT_TS1_ITL | DTS_IT_TS1_ITH);
704 }
705
706 /* Enable the selected DTS sensor */
707 __HAL_DTS_ENABLE(hdts);
708
709 /* Get Start Tick*/
710 Ref_Time = HAL_GetTick();
711
712 /* Wait till TS1_RDY flag is set */
713 while (__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_RDY) == RESET)
714 {
715 if ((HAL_GetTick() - Ref_Time) > DTS_DELAY_STARTUP)
716 {
717 return HAL_TIMEOUT;
718 }
719 }
720
721 if (__HAL_DTS_GET_TRIGGER(hdts) == DTS_TRIGGER_HW_NONE)
722 {
723 /* Start continuous measures */
724 SET_BIT(hdts->Instance->CFGR1, DTS_CFGR1_TS1_START);
725
726 /* Ensure start is taken into account */
727 HAL_Delay(TS_TIMEOUT_MS);
728 }
729
730 hdts->State = HAL_DTS_STATE_READY;
731 }
732 else
733 {
734 return HAL_BUSY;
735 }
736
737 return HAL_OK;
738 }
739
740 /**
741 * @brief Disable the interrupt(s) and stop the DTS sensor.
742 * @param hdts DTS handle
743 * @retval HAL status
744 */
HAL_DTS_Stop_IT(DTS_HandleTypeDef * hdts)745 HAL_StatusTypeDef HAL_DTS_Stop_IT(DTS_HandleTypeDef *hdts)
746 {
747 /* Check the DTS handle allocation */
748 if (hdts == NULL)
749 {
750 return HAL_ERROR;
751 }
752
753 if (hdts->State == HAL_DTS_STATE_READY)
754 {
755 hdts->State = HAL_DTS_STATE_BUSY;
756
757 /* On Asynchronous mode disable the asynchronous IT */
758 if (hdts->Init.RefClock == DTS_REFCLKSEL_LSE)
759 {
760 __HAL_DTS_DISABLE_IT(hdts, DTS_IT_TS1_AITE | DTS_IT_TS1_AITL | DTS_IT_TS1_AITH);
761 }
762 else
763 {
764 /* Disable the IT(s) */
765 __HAL_DTS_DISABLE_IT(hdts, DTS_IT_TS1_ITE | DTS_IT_TS1_ITL | DTS_IT_TS1_ITH);
766 }
767
768 if (__HAL_DTS_GET_TRIGGER(hdts) == DTS_TRIGGER_HW_NONE)
769 {
770 CLEAR_BIT(hdts->Instance->CFGR1, DTS_CFGR1_TS1_START);
771 }
772
773 /* Disable the selected DTS sensor */
774 __HAL_DTS_DISABLE(hdts);
775
776 hdts->State = HAL_DTS_STATE_READY;
777 }
778 else
779 {
780 return HAL_BUSY;
781 }
782
783 return HAL_OK;
784 }
785
786 /**
787 * @brief Get temperature from DTS
788 * @param hdts DTS handle
789 * @param Temperature Temperature in deg C
790 * @note This function retrieves latest available measure
791 * @retval HAL status
792 */
HAL_DTS_GetTemperature(DTS_HandleTypeDef * hdts,int32_t * Temperature)793 HAL_StatusTypeDef HAL_DTS_GetTemperature(DTS_HandleTypeDef *hdts, int32_t *Temperature)
794 {
795 uint32_t freq_meas;
796 uint32_t samples;
797 uint32_t t0_temp;
798 uint32_t t0_freq;
799 uint32_t ramp_coeff;
800
801 if (hdts->State == HAL_DTS_STATE_READY)
802 {
803 hdts->State = HAL_DTS_STATE_BUSY;
804
805 /* Get the total number of samples */
806 samples = (hdts->Instance->DR & DTS_DR_TS1_MFREQ);
807
808 if ((hdts->Init.SamplingTime == 0UL) || (samples == 0UL))
809 {
810 hdts->State = HAL_DTS_STATE_READY;
811 return HAL_ERROR;
812 }
813
814 if ((hdts->Init.RefClock) == DTS_REFCLKSEL_LSE)
815 {
816 /* Measured frequency On Hz */
817 freq_meas = (LSE_VALUE * samples) / (hdts->Init.SamplingTime >> DTS_CFGR1_TS1_SMP_TIME_Pos);
818 }
819 else
820 {
821 /* Measured frequency On Hz */
822 freq_meas = (HAL_RCC_GetPCLK1Freq() * (hdts->Init.SamplingTime >> DTS_CFGR1_TS1_SMP_TIME_Pos)) / samples;
823 }
824
825 /* Read factory settings */
826 t0_temp = hdts->Instance->T0VALR1 >> DTS_T0VALR1_TS1_T0_Pos;
827
828 if (t0_temp == 0UL)
829 {
830 t0_temp = DTS_FACTORY_TEMPERATURE1; /* 30 deg C */
831 }
832 else if (t0_temp == 1UL)
833 {
834 t0_temp = DTS_FACTORY_TEMPERATURE2; /* 130 deg C */
835 }
836 else
837 {
838 hdts->State = HAL_DTS_STATE_READY;
839 return HAL_ERROR;
840 }
841
842 t0_freq = (hdts->Instance->T0VALR1 & DTS_T0VALR1_TS1_FMT0) * 100UL; /* Hz */
843
844 ramp_coeff = hdts->Instance->RAMPVALR & DTS_RAMPVALR_TS1_RAMP_COEFF; /* deg C/Hz */
845
846 if (ramp_coeff == 0UL)
847 {
848 hdts->State = HAL_DTS_STATE_READY;
849 return HAL_ERROR;
850 }
851
852 /* Figure out the temperature deg C */
853 *Temperature = (int32_t)t0_temp + (((int32_t)freq_meas - (int32_t)t0_freq) / (int32_t)ramp_coeff);
854
855 hdts->State = HAL_DTS_STATE_READY;
856 }
857 else
858 {
859 return HAL_BUSY;
860 }
861
862 return HAL_OK;
863 }
864
865 /**
866 * @brief DTS sensor IRQ Handler.
867 * @param hdts DTS handle
868 * @retval None
869 */
HAL_DTS_IRQHandler(DTS_HandleTypeDef * hdts)870 void HAL_DTS_IRQHandler(DTS_HandleTypeDef *hdts)
871 {
872 /* Check end of measure Asynchronous IT */
873 if ((__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_AITE)) != RESET)
874 {
875 __HAL_DTS_CLEAR_FLAG(hdts, DTS_FLAG_TS1_AITE);
876
877 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
878 hdts->AsyncEndCallback(hdts);
879 #else
880 HAL_DTS_AsyncEndCallback(hdts);
881 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
882 }
883
884 /* Check low threshold Asynchronous IT */
885 if ((__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_AITL)) != RESET)
886 {
887 __HAL_DTS_CLEAR_FLAG(hdts, DTS_FLAG_TS1_AITL);
888
889 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
890 hdts->AsyncLowCallback(hdts);
891 #else
892 HAL_DTS_AsyncLowCallback(hdts);
893 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
894 }
895
896 /* Check high threshold Asynchronous IT */
897 if ((__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_AITH)) != RESET)
898 {
899 __HAL_DTS_CLEAR_FLAG(hdts, DTS_FLAG_TS1_AITH);
900
901 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
902 hdts->AsyncHighCallback(hdts);
903 #else
904 HAL_DTS_AsyncHighCallback(hdts);
905 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
906 }
907
908 /* Check end of measure IT */
909 if ((__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_ITE)) != RESET)
910 {
911 __HAL_DTS_CLEAR_FLAG(hdts, DTS_FLAG_TS1_ITE);
912
913 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
914 hdts->EndCallback(hdts);
915 #else
916 HAL_DTS_EndCallback(hdts);
917 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
918 }
919
920 /* Check low threshold IT */
921 if ((__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_ITL)) != RESET)
922 {
923 __HAL_DTS_CLEAR_FLAG(hdts, DTS_FLAG_TS1_ITL);
924
925 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
926 hdts->LowCallback(hdts);
927 #else
928 HAL_DTS_LowCallback(hdts);
929 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
930 }
931
932 /* Check high threshold IT */
933 if ((__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_ITH)) != RESET)
934 {
935 __HAL_DTS_CLEAR_FLAG(hdts, DTS_FLAG_TS1_ITH);
936
937 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
938 hdts->HighCallback(hdts);
939 #else
940 HAL_DTS_HighCallback(hdts);
941 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
942 }
943 }
944
945 /**
946 * @brief DTS Sensor End measure callback.
947 * @param hdts DTS handle
948 * @retval None
949 */
HAL_DTS_EndCallback(DTS_HandleTypeDef * hdts)950 __weak void HAL_DTS_EndCallback(DTS_HandleTypeDef *hdts)
951 {
952 /* Prevent unused argument(s) compilation warning */
953 UNUSED(hdts);
954
955 /* NOTE : This function should not be modified, when the callback is needed,
956 the HAL_DTS_EndCallback should be implemented in the user file
957 */
958 }
959
960 /**
961 * @brief DTS Sensor low threshold measure callback.
962 * @param hdts DTS handle
963 * @retval None
964 */
HAL_DTS_LowCallback(DTS_HandleTypeDef * hdts)965 __weak void HAL_DTS_LowCallback(DTS_HandleTypeDef *hdts)
966 {
967 /* Prevent unused argument(s) compilation warning */
968 UNUSED(hdts);
969
970 /* NOTE : This function should not be modified, when the callback is needed,
971 the HAL_DTS_LowCallback should be implemented in the user file
972 */
973 }
974
975 /**
976 * @brief DTS Sensor high threshold measure callback.
977 * @param hdts DTS handle
978 * @retval None
979 */
HAL_DTS_HighCallback(DTS_HandleTypeDef * hdts)980 __weak void HAL_DTS_HighCallback(DTS_HandleTypeDef *hdts)
981 {
982 /* Prevent unused argument(s) compilation warning */
983 UNUSED(hdts);
984
985 /* NOTE : This function should not be modified, when the callback is needed,
986 the HAL_DTS_HighCallback should be implemented in the user file
987 */
988 }
989
990 /**
991 * @brief DTS Sensor asynchronous end measure callback.
992 * @param hdts DTS handle
993 * @retval None
994 */
HAL_DTS_AsyncEndCallback(DTS_HandleTypeDef * hdts)995 __weak void HAL_DTS_AsyncEndCallback(DTS_HandleTypeDef *hdts)
996 {
997 /* Prevent unused argument(s) compilation warning */
998 UNUSED(hdts);
999
1000 /* NOTE : This function should not be modified, when the callback is needed,
1001 the HAL_DTS_AsyncEndCallback should be implemented in the user file
1002 */
1003 }
1004
1005 /**
1006 * @brief DTS Sensor asynchronous low threshold measure callback.
1007 * @param hdts DTS handle
1008 * @retval None
1009 */
HAL_DTS_AsyncLowCallback(DTS_HandleTypeDef * hdts)1010 __weak void HAL_DTS_AsyncLowCallback(DTS_HandleTypeDef *hdts)
1011 {
1012 /* Prevent unused argument(s) compilation warning */
1013 UNUSED(hdts);
1014
1015 /* NOTE : This function should not be modified, when the callback is needed,
1016 the HAL_DTS_AsyncLowCallback should be implemented in the user file
1017 */
1018 }
1019
1020 /**
1021 * @brief DTS Sensor asynchronous high threshold measure callback.
1022 * @param hdts DTS handle
1023 * @retval None
1024 */
HAL_DTS_AsyncHighCallback(DTS_HandleTypeDef * hdts)1025 __weak void HAL_DTS_AsyncHighCallback(DTS_HandleTypeDef *hdts)
1026 {
1027 /* Prevent unused argument(s) compilation warning */
1028 UNUSED(hdts);
1029
1030 /* NOTE : This function should not be modified, when the callback is needed,
1031 the HAL_DTS_AsyncHighCallback should be implemented in the user file
1032 */
1033 }
1034
1035 /**
1036 * @}
1037 */
1038
1039 /** @defgroup DTS_Exported_Functions_Group3 Peripheral State functions
1040 * @brief Peripheral State functions.
1041 *
1042 @verbatim
1043 =======================================================================================================================
1044 ##### Peripheral State functions #####
1045 =======================================================================================================================
1046 [..]
1047 This subsection permits to get in run-time the status of the peripheral.
1048
1049 @endverbatim
1050 * @{
1051 */
1052
1053 /**
1054 * @brief Return the DTS handle state.
1055 * @param hdts DTS handle
1056 * @retval HAL state
1057 */
HAL_DTS_GetState(DTS_HandleTypeDef * hdts)1058 HAL_DTS_StateTypeDef HAL_DTS_GetState(DTS_HandleTypeDef *hdts)
1059 {
1060 /* Check the DTS handle allocation */
1061 if (hdts == NULL)
1062 {
1063 return HAL_DTS_STATE_RESET;
1064 }
1065
1066 /* Return DTS handle state */
1067 return hdts->State;
1068 }
1069 /**
1070 * @}
1071 */
1072
1073 /**
1074 * @}
1075 */
1076
1077 /**
1078 * @}
1079 */
1080
1081 #endif /* DTS */
1082
1083 #endif /* HAL_DTS_MODULE_ENABLED */
1084
1085 /**
1086 * @}
1087 */
1088