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) 2023 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 /**
196   * @}
197   */
198 
199 /* Private macro -----------------------------------------------------------------------------------------------------*/
200 /* Private variables -------------------------------------------------------------------------------------------------*/
201 /* Private function prototypes ---------------------------------------------------------------------------------------*/
202 /* Exported functions ------------------------------------------------------------------------------------------------*/
203 /** @defgroup DTS_Exported_Functions DTS Exported Functions
204   * @{
205   */
206 
207 /** @defgroup DTS_Exported_Functions_Group1 Initialization/de-initialization functions
208   *  @brief    Initialization and de-initialization functions.
209   *
210 @verbatim
211  =======================================================================================================================
212                            ##### Initialization and de-initialization functions #####
213  =======================================================================================================================
214     [..]  This section provides functions to initialize and de-initialize DTS
215 
216 @endverbatim
217   * @{
218   */
219 
220 /**
221   * @brief  Initialize the DTS according to the specified
222   *         parameters in the DTS_InitTypeDef and initialize the associated handle.
223   * @param  hdts  DTS handle
224   * @retval HAL status
225   */
HAL_DTS_Init(DTS_HandleTypeDef * hdts)226 HAL_StatusTypeDef HAL_DTS_Init(DTS_HandleTypeDef *hdts)
227 {
228   /* Check the DTS handle allocation */
229   if (hdts == NULL)
230   {
231     return HAL_ERROR;
232   }
233 
234   /* Check the parameters */
235   assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
236   assert_param(IS_DTS_QUICKMEAS(hdts->Init.QuickMeasure));
237   assert_param(IS_DTS_REFCLK(hdts->Init.RefClock));
238   assert_param(IS_DTS_TRIGGERINPUT(hdts->Init.TriggerInput));
239   assert_param(IS_DTS_SAMPLINGTIME(hdts->Init.SamplingTime));
240   assert_param(IS_DTS_THRESHOLD(hdts->Init.HighThreshold));
241   assert_param(IS_DTS_THRESHOLD(hdts->Init.LowThreshold));
242 
243   if (hdts->State == HAL_DTS_STATE_RESET)
244   {
245 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
246     /* Reset the DTS callback to the legacy weak callbacks */
247     hdts->EndCallback       = HAL_DTS_EndCallback;        /* End measure Callback                 */
248     hdts->LowCallback       = HAL_DTS_LowCallback;        /* low threshold Callback               */
249     hdts->HighCallback      = HAL_DTS_HighCallback;       /* high threshold Callback              */
250     hdts->AsyncEndCallback  = HAL_DTS_AsyncEndCallback;   /* Asynchronous end of measure Callback */
251     hdts->AsyncLowCallback  = HAL_DTS_AsyncLowCallback;   /* Asynchronous low threshold Callback  */
252     hdts->AsyncHighCallback = HAL_DTS_AsyncHighCallback;  /* Asynchronous high threshold Callback */
253 
254     if (hdts->MspInitCallback == NULL)
255     {
256       hdts->MspInitCallback = HAL_DTS_MspInit;
257     }
258 
259     /* Init the low level hardware : GPIO, CLOCK, NVIC */
260     hdts->MspInitCallback(hdts);
261 #else
262     /* Init the low level hardware : GPIO, CLOCK, NVIC */
263     HAL_DTS_MspInit(hdts);
264 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
265   }
266 
267   /* Change the DTS state */
268   hdts->State = HAL_DTS_STATE_BUSY;
269 
270   /* Check ramp coefficient */
271   if (hdts->Instance->RAMPVALR == 0UL)
272   {
273     return HAL_ERROR;
274   }
275 
276   /* Check factory calibration temperature  */
277   if (hdts->Instance->T0VALR1 == 0UL)
278   {
279     return HAL_ERROR;
280   }
281 
282   /* Check Quick Measure option is enabled or disabled */
283   if (hdts->Init.QuickMeasure == DTS_QUICKMEAS_DISABLE)
284   {
285     /* Check Reference clock selection */
286     if (hdts->Init.RefClock == DTS_REFCLKSEL_PCLK)
287     {
288       assert_param(IS_DTS_DIVIDER_RATIO_NUMBER(hdts->Init.Divider));
289     }
290     /* Quick measurement mode disabled */
291     CLEAR_BIT(hdts->Instance->CFGR1, DTS_CFGR1_Q_MEAS_OPT);
292   }
293   else
294   {
295     /* DTS_QUICKMEAS_ENABLE shall be used only when the LSE clock is
296        selected as reference clock */
297     if (hdts->Init.RefClock != DTS_REFCLKSEL_LSE)
298     {
299       return HAL_ERROR;
300     }
301 
302     /* Quick measurement mode enabled - no calibration needed */
303     SET_BIT(hdts->Instance->CFGR1, DTS_CFGR1_Q_MEAS_OPT);
304   }
305 
306   /* set the DTS clk source */
307   if (hdts->Init.RefClock == DTS_REFCLKSEL_LSE)
308   {
309     SET_BIT(hdts->Instance->CFGR1, DTS_CFGR1_REFCLK_SEL);
310   }
311   else
312   {
313     CLEAR_BIT(hdts->Instance->CFGR1, DTS_CFGR1_REFCLK_SEL);
314   }
315 
316   MODIFY_REG(hdts->Instance->CFGR1, DTS_CFGR1_HSREF_CLK_DIV, (hdts->Init.Divider << DTS_CFGR1_HSREF_CLK_DIV_Pos));
317   MODIFY_REG(hdts->Instance->CFGR1, DTS_CFGR1_TS1_SMP_TIME, hdts->Init.SamplingTime);
318   MODIFY_REG(hdts->Instance->CFGR1, DTS_CFGR1_TS1_INTRIG_SEL, hdts->Init.TriggerInput);
319   MODIFY_REG(hdts->Instance->ITR1, DTS_ITR1_TS1_HITTHD, (hdts->Init.HighThreshold << DTS_ITR1_TS1_HITTHD_Pos));
320   MODIFY_REG(hdts->Instance->ITR1, DTS_ITR1_TS1_LITTHD, hdts->Init.LowThreshold);
321 
322   /* Change the DTS state */
323   hdts->State = HAL_DTS_STATE_READY;
324 
325   return HAL_OK;
326 }
327 
328 /**
329   * @brief  DeInitialize the DTS peripheral.
330   * @note   Deinitialization cannot be performed if the DTS configuration is locked.
331   *         To unlock the configuration, perform a system reset.
332   * @param  hdts  DTS handle
333   * @retval HAL status
334   */
HAL_DTS_DeInit(DTS_HandleTypeDef * hdts)335 HAL_StatusTypeDef HAL_DTS_DeInit(DTS_HandleTypeDef *hdts)
336 {
337   /* Check the DTS handle allocation */
338   if (hdts == NULL)
339   {
340     return HAL_ERROR;
341   }
342 
343   /* Check the parameter */
344   assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
345 
346   /* Set DTS_CFGR register to reset value */
347   CLEAR_REG(hdts->Instance->CFGR1);
348 
349 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
350   if (hdts->MspDeInitCallback == NULL)
351   {
352     hdts->MspDeInitCallback = HAL_DTS_MspDeInit;
353   }
354 
355   /* DeInit the low level hardware: CLOCK, NVIC.*/
356   hdts->MspDeInitCallback(hdts);
357 #else
358   /* DeInit the low level hardware: CLOCK, NVIC.*/
359   HAL_DTS_MspDeInit(hdts);
360 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
361 
362   hdts->State = HAL_DTS_STATE_RESET;
363 
364   return HAL_OK;
365 }
366 
367 /**
368   * @brief  Initialize the DTS MSP.
369   * @param  hdts  DTS handle
370   * @retval None
371   */
HAL_DTS_MspInit(DTS_HandleTypeDef * hdts)372 __weak void HAL_DTS_MspInit(DTS_HandleTypeDef *hdts)
373 {
374   /* Prevent unused argument(s) compilation warning */
375   UNUSED(hdts);
376 
377   /* NOTE : This function should not be modified, when the callback is needed,
378   the HAL_DTS_MspInit could be implemented in the user file
379   */
380 }
381 
382 /**
383   * @brief  DeInitialize the DTS MSP.
384   * @param  hdts  DTS handle
385   * @retval None
386   */
HAL_DTS_MspDeInit(DTS_HandleTypeDef * hdts)387 __weak void HAL_DTS_MspDeInit(DTS_HandleTypeDef *hdts)
388 {
389   /* Prevent unused argument(s) compilation warning */
390   UNUSED(hdts);
391 
392   /* NOTE : This function should not be modified, when the callback is needed,
393   the HAL_DTS_MspDeInit could be implemented in the user file
394   */
395 }
396 
397 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
398 /**
399   * @brief  Register a user DTS callback to be used instead of the weak predefined callback.
400   * @param  hdts DTS handle.
401   * @param  CallbackID ID of the callback to be registered.
402   *         This parameter can be one of the following values:
403   *           @arg @ref HAL_DTS_MEAS_COMPLETE_CB_ID measure complete callback ID.
404   *           @arg @ref HAL_DTS_ASYNC_MEAS_COMPLETE_CB_ID asynchronous measure complete callback ID.
405   *           @arg @ref HAL_DTS_LOW_THRESHOLD_CB_ID low threshold detection callback ID.
406   *           @arg @ref HAL_DTS_ASYNC_LOW_THRESHOLD_CB_ID asynchronous low threshold detection callback ID.
407   *           @arg @ref HAL_DTS_HIGH_THRESHOLD_CB_ID high threshold detection callback ID.
408   *           @arg @ref HAL_DTS_ASYNC_HIGH_THRESHOLD_CB_ID asynchronous high threshold detection callback ID.
409   *           @arg @ref HAL_DTS_MSPINIT_CB_ID MSP init callback ID.
410   *           @arg @ref HAL_DTS_MSPDEINIT_CB_ID MSP de-init callback ID.
411   * @param  pCallback pointer to the callback function.
412   * @retval HAL status.
413   */
HAL_DTS_RegisterCallback(DTS_HandleTypeDef * hdts,HAL_DTS_CallbackIDTypeDef CallbackID,pDTS_CallbackTypeDef pCallback)414 HAL_StatusTypeDef HAL_DTS_RegisterCallback(DTS_HandleTypeDef        *hdts,
415                                            HAL_DTS_CallbackIDTypeDef CallbackID,
416                                            pDTS_CallbackTypeDef      pCallback)
417 {
418   HAL_StatusTypeDef status = HAL_OK;
419 
420   /* Check parameters */
421   if (pCallback == NULL)
422   {
423     /* Update status */
424     status = HAL_ERROR;
425   }
426   else
427   {
428     if (hdts->State == HAL_DTS_STATE_READY)
429     {
430       switch (CallbackID)
431       {
432         case HAL_DTS_MEAS_COMPLETE_CB_ID :
433           hdts->EndCallback = pCallback;
434           break;
435         case HAL_DTS_ASYNC_MEAS_COMPLETE_CB_ID :
436           hdts->AsyncEndCallback = pCallback;
437           break;
438         case HAL_DTS_LOW_THRESHOLD_CB_ID :
439           hdts->LowCallback = pCallback;
440           break;
441         case HAL_DTS_ASYNC_LOW_THRESHOLD_CB_ID :
442           hdts->AsyncLowCallback = pCallback;
443           break;
444         case HAL_DTS_HIGH_THRESHOLD_CB_ID :
445           hdts->HighCallback = pCallback;
446           break;
447         case HAL_DTS_ASYNC_HIGH_THRESHOLD_CB_ID :
448           hdts->AsyncHighCallback = pCallback;
449           break;
450         case HAL_DTS_MSPINIT_CB_ID :
451           hdts->MspInitCallback = pCallback;
452           break;
453         case HAL_DTS_MSPDEINIT_CB_ID :
454           hdts->MspDeInitCallback = pCallback;
455           break;
456         default :
457           /* Update status */
458           status = HAL_ERROR;
459           break;
460       }
461     }
462     else if (hdts->State == HAL_DTS_STATE_RESET)
463     {
464       switch (CallbackID)
465       {
466         case HAL_DTS_MSPINIT_CB_ID :
467           hdts->MspInitCallback = pCallback;
468           break;
469         case HAL_DTS_MSPDEINIT_CB_ID :
470           hdts->MspDeInitCallback = pCallback;
471           break;
472         default :
473           /* Update status */
474           status = HAL_ERROR;
475           break;
476       }
477     }
478     else
479     {
480       /* Update status */
481       status = HAL_ERROR;
482     }
483   }
484 
485   /* Return function status */
486   return status;
487 }
488 
489 /**
490   * @brief  Unregister a user DTS callback.
491   *         DTS callback is redirected to the weak predefined callback.
492   * @param  hdts DTS handle.
493   * @param  CallbackID ID of the callback to be unregistered.
494   *         This parameter can be one of the following values:
495   *           @arg @ref HAL_DTS_MEAS_COMPLETE_CB_ID measure complete callback ID.
496   *           @arg @ref HAL_DTS_ASYNC_MEAS_COMPLETE_CB_ID asynchronous measure complete callback ID.
497   *           @arg @ref HAL_DTS_LOW_THRESHOLD_CB_ID low threshold detection callback ID.
498   *           @arg @ref HAL_DTS_ASYNC_LOW_THRESHOLD_CB_ID asynchronous low threshold detection callback ID.
499   *           @arg @ref HAL_DTS_HIGH_THRESHOLD_CB_ID high threshold detection callback ID.
500   *           @arg @ref HAL_DTS_ASYNC_HIGH_THRESHOLD_CB_ID asynchronous high threshold detection callback ID.
501   *           @arg @ref HAL_DTS_MSPINIT_CB_ID MSP init callback ID.
502   *           @arg @ref HAL_DTS_MSPDEINIT_CB_ID MSP de-init callback ID.
503   * @retval HAL status.
504   */
HAL_DTS_UnRegisterCallback(DTS_HandleTypeDef * hdts,HAL_DTS_CallbackIDTypeDef CallbackID)505 HAL_StatusTypeDef HAL_DTS_UnRegisterCallback(DTS_HandleTypeDef        *hdts,
506                                              HAL_DTS_CallbackIDTypeDef CallbackID)
507 {
508   HAL_StatusTypeDef status = HAL_OK;
509 
510   if (hdts->State == HAL_DTS_STATE_READY)
511   {
512     switch (CallbackID)
513     {
514       case HAL_DTS_MEAS_COMPLETE_CB_ID :
515         hdts->EndCallback = HAL_DTS_EndCallback;
516         break;
517       case HAL_DTS_ASYNC_MEAS_COMPLETE_CB_ID :
518         hdts->AsyncEndCallback = HAL_DTS_AsyncEndCallback;
519         break;
520       case HAL_DTS_LOW_THRESHOLD_CB_ID :
521         hdts->LowCallback = HAL_DTS_LowCallback;
522         break;
523       case HAL_DTS_ASYNC_LOW_THRESHOLD_CB_ID :
524         hdts->AsyncLowCallback = HAL_DTS_AsyncLowCallback;
525         break;
526       case HAL_DTS_HIGH_THRESHOLD_CB_ID :
527         hdts->HighCallback = HAL_DTS_HighCallback;
528         break;
529       case HAL_DTS_ASYNC_HIGH_THRESHOLD_CB_ID :
530         hdts->AsyncHighCallback = HAL_DTS_AsyncHighCallback;
531         break;
532       case HAL_DTS_MSPINIT_CB_ID :
533         hdts->MspInitCallback = HAL_DTS_MspInit;
534         break;
535       case HAL_DTS_MSPDEINIT_CB_ID :
536         hdts->MspDeInitCallback = HAL_DTS_MspDeInit;
537         break;
538       default :
539         /* Update status */
540         status = HAL_ERROR;
541         break;
542     }
543   }
544   else if (hdts->State == HAL_DTS_STATE_RESET)
545   {
546     switch (CallbackID)
547     {
548       case HAL_DTS_MSPINIT_CB_ID :
549         hdts->MspInitCallback = HAL_DTS_MspInit;
550         break;
551       case HAL_DTS_MSPDEINIT_CB_ID :
552         hdts->MspDeInitCallback = HAL_DTS_MspDeInit;
553         break;
554       default :
555         /* Update status */
556         status = HAL_ERROR;
557         break;
558     }
559   }
560   else
561   {
562     /* Update status */
563     status = HAL_ERROR;
564   }
565 
566   /* Return function status */
567   return status;
568 }
569 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
570 
571 /**
572   * @}
573   */
574 
575 /** @defgroup DTS_Exported_Functions_Group2 Start-Stop operation functions
576   *  @brief    Start-Stop operation functions.
577   *
578 @verbatim
579  =======================================================================================================================
580                                   ##### DTS Start Stop operation functions #####
581  =======================================================================================================================
582     [..]  This section provides functions allowing to:
583            (+) Start a DTS Sensor without interrupt.
584            (+) Stop a DTS Sensor without interrupt.
585            (+) Start a DTS Sensor with interrupt generation.
586            (+) Stop a DTS Sensor with interrupt generation.
587 
588 @endverbatim
589   * @{
590   */
591 
592 /**
593   * @brief  Start the DTS sensor.
594   * @param  hdts  DTS handle
595   * @retval HAL status
596   */
HAL_DTS_Start(DTS_HandleTypeDef * hdts)597 HAL_StatusTypeDef HAL_DTS_Start(DTS_HandleTypeDef *hdts)
598 {
599   uint32_t Ref_Time;
600 
601   /* Check the DTS handle allocation */
602   if (hdts == NULL)
603   {
604     return HAL_ERROR;
605   }
606 
607   if (hdts->State == HAL_DTS_STATE_READY)
608   {
609     hdts->State = HAL_DTS_STATE_BUSY;
610 
611     /* Enable DTS sensor */
612     __HAL_DTS_ENABLE(hdts);
613 
614     /* Get Start Tick*/
615     Ref_Time = HAL_GetTick();
616 
617     /* Wait till TS1_RDY flag is set */
618     while (__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_RDY) == RESET)
619     {
620       if ((HAL_GetTick() - Ref_Time) > DTS_DELAY_STARTUP)
621       {
622         return HAL_TIMEOUT;
623       }
624     }
625 
626     if (__HAL_DTS_GET_TRIGGER(hdts) == DTS_TRIGGER_HW_NONE)
627     {
628       /* Start continuous measures */
629       SET_BIT(hdts->Instance->CFGR1, DTS_CFGR1_TS1_START);
630 
631       /* Ensure start is taken into account */
632       HAL_Delay(TS_TIMEOUT_MS);
633     }
634 
635     hdts->State = HAL_DTS_STATE_READY;
636   }
637   else
638   {
639     return HAL_BUSY;
640   }
641 
642   return HAL_OK;
643 }
644 
645 /**
646   * @brief  Stop the DTS Sensor.
647   * @param  hdts  DTS handle
648   * @retval HAL status
649   */
HAL_DTS_Stop(DTS_HandleTypeDef * hdts)650 HAL_StatusTypeDef HAL_DTS_Stop(DTS_HandleTypeDef *hdts)
651 {
652   /* Check the DTS handle allocation */
653   if (hdts == NULL)
654   {
655     return HAL_ERROR;
656   }
657 
658   if (hdts->State == HAL_DTS_STATE_READY)
659   {
660     hdts->State = HAL_DTS_STATE_BUSY;
661 
662     if (__HAL_DTS_GET_TRIGGER(hdts) == DTS_TRIGGER_HW_NONE)
663     {
664       CLEAR_BIT(hdts->Instance->CFGR1, DTS_CFGR1_TS1_START);
665     }
666 
667     /* Disable the selected DTS sensor */
668     __HAL_DTS_DISABLE(hdts);
669 
670     hdts->State = HAL_DTS_STATE_READY;
671   }
672   else
673   {
674     return HAL_BUSY;
675   }
676 
677   return HAL_OK;
678 }
679 
680 /**
681   * @brief  Enable the interrupt(s) and start the DTS sensor
682   * @param  hdts  DTS handle
683   * @retval HAL status
684   */
HAL_DTS_Start_IT(DTS_HandleTypeDef * hdts)685 HAL_StatusTypeDef HAL_DTS_Start_IT(DTS_HandleTypeDef *hdts)
686 {
687   uint32_t Ref_Time;
688 
689   /* Check the DTS handle allocation */
690   if (hdts == NULL)
691   {
692     return HAL_ERROR;
693   }
694 
695   if (hdts->State == HAL_DTS_STATE_READY)
696   {
697     hdts->State = HAL_DTS_STATE_BUSY;
698 
699     /* On Asynchronous mode enable the asynchronous IT */
700     if (hdts->Init.RefClock == DTS_REFCLKSEL_LSE)
701     {
702       __HAL_DTS_ENABLE_IT(hdts, DTS_IT_TS1_AITE | DTS_IT_TS1_AITL | DTS_IT_TS1_AITH);
703     }
704     else
705     {
706       /* Enable the IT(s) */
707       __HAL_DTS_ENABLE_IT(hdts, DTS_IT_TS1_ITE | DTS_IT_TS1_ITL | DTS_IT_TS1_ITH);
708     }
709 
710     /* Enable the selected DTS sensor */
711     __HAL_DTS_ENABLE(hdts);
712 
713     /* Get Start Tick*/
714     Ref_Time = HAL_GetTick();
715 
716     /* Wait till TS1_RDY flag is set */
717     while (__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_RDY) == RESET)
718     {
719       if ((HAL_GetTick() - Ref_Time) > DTS_DELAY_STARTUP)
720       {
721         return HAL_TIMEOUT;
722       }
723     }
724 
725     if (__HAL_DTS_GET_TRIGGER(hdts) == DTS_TRIGGER_HW_NONE)
726     {
727       /* Start continuous measures */
728       SET_BIT(hdts->Instance->CFGR1, DTS_CFGR1_TS1_START);
729 
730       /* Ensure start is taken into account */
731       HAL_Delay(TS_TIMEOUT_MS);
732     }
733 
734     hdts->State = HAL_DTS_STATE_READY;
735   }
736   else
737   {
738     return HAL_BUSY;
739   }
740 
741   return HAL_OK;
742 }
743 
744 /**
745   * @brief  Disable the interrupt(s) and stop the DTS sensor.
746   * @param  hdts  DTS handle
747   * @retval HAL status
748   */
HAL_DTS_Stop_IT(DTS_HandleTypeDef * hdts)749 HAL_StatusTypeDef HAL_DTS_Stop_IT(DTS_HandleTypeDef *hdts)
750 {
751   /* Check the DTS handle allocation */
752   if (hdts == NULL)
753   {
754     return HAL_ERROR;
755   }
756 
757   if (hdts->State == HAL_DTS_STATE_READY)
758   {
759     hdts->State = HAL_DTS_STATE_BUSY;
760 
761     /* On Asynchronous mode disable the asynchronous IT */
762     if (hdts->Init.RefClock == DTS_REFCLKSEL_LSE)
763     {
764       __HAL_DTS_DISABLE_IT(hdts, DTS_IT_TS1_AITE | DTS_IT_TS1_AITL | DTS_IT_TS1_AITH);
765     }
766     else
767     {
768       /* Disable the IT(s) */
769       __HAL_DTS_DISABLE_IT(hdts, DTS_IT_TS1_ITE | DTS_IT_TS1_ITL | DTS_IT_TS1_ITH);
770     }
771 
772     if (__HAL_DTS_GET_TRIGGER(hdts) == DTS_TRIGGER_HW_NONE)
773     {
774       CLEAR_BIT(hdts->Instance->CFGR1, DTS_CFGR1_TS1_START);
775     }
776 
777     /* Disable the selected DTS sensor */
778     __HAL_DTS_DISABLE(hdts);
779 
780     hdts->State = HAL_DTS_STATE_READY;
781   }
782   else
783   {
784     return HAL_BUSY;
785   }
786 
787   return HAL_OK;
788 }
789 
790 /**
791   * @brief  Get temperature from DTS
792   * @param  hdts         DTS handle
793   * @param  Temperature  Temperature in deg C
794   * @note This function retrieves latest available measure
795   * @retval HAL status
796   */
HAL_DTS_GetTemperature(DTS_HandleTypeDef * hdts,int32_t * Temperature)797 HAL_StatusTypeDef HAL_DTS_GetTemperature(DTS_HandleTypeDef *hdts, int32_t *Temperature)
798 {
799   uint32_t freq_meas;
800   uint32_t samples;
801   uint32_t t0_temp;
802   uint32_t t0_freq;
803   uint32_t ramp_coeff;
804 
805   if (hdts->State == HAL_DTS_STATE_READY)
806   {
807     hdts->State = HAL_DTS_STATE_BUSY;
808 
809     /* Get the total number of samples */
810     samples = (hdts->Instance->DR & DTS_DR_TS1_MFREQ);
811 
812     if ((hdts->Init.SamplingTime == 0UL) || (samples == 0UL))
813     {
814       hdts->State = HAL_DTS_STATE_READY;
815       return HAL_ERROR;
816     }
817 
818     if ((hdts->Init.RefClock) == DTS_REFCLKSEL_LSE)
819     {
820       /* Measured frequency On Hz */
821       freq_meas = (LSE_VALUE * samples) / (hdts->Init.SamplingTime >> DTS_CFGR1_TS1_SMP_TIME_Pos);
822     }
823     else
824     {
825       /* Measured frequency On Hz */
826       freq_meas = (HAL_RCC_GetPCLK1Freq() * (hdts->Init.SamplingTime >> DTS_CFGR1_TS1_SMP_TIME_Pos)) / samples;
827     }
828 
829     /* Read factory settings */
830     t0_temp = hdts->Instance->T0VALR1 >> DTS_T0VALR1_TS1_T0_Pos;
831 
832     if (t0_temp == 0UL)
833     {
834       t0_temp = DTS_FACTORY_TEMPERATURE1; /* 30 deg C */
835     }
836     else if (t0_temp == 1UL)
837     {
838       t0_temp = DTS_FACTORY_TEMPERATURE2; /* 130 deg C */
839     }
840     else
841     {
842       hdts->State = HAL_DTS_STATE_READY;
843       return HAL_ERROR;
844     }
845 
846     t0_freq = (hdts->Instance->T0VALR1 & DTS_T0VALR1_TS1_FMT0) * 100UL; /* Hz */
847 
848     ramp_coeff = hdts->Instance->RAMPVALR & DTS_RAMPVALR_TS1_RAMP_COEFF; /* deg C/Hz */
849 
850     if (ramp_coeff == 0UL)
851     {
852       hdts->State = HAL_DTS_STATE_READY;
853       return HAL_ERROR;
854     }
855 
856     /* Figure out the temperature deg C */
857     *Temperature = (int32_t)t0_temp + (((int32_t)freq_meas - (int32_t)t0_freq) / (int32_t)ramp_coeff);
858 
859     hdts->State = HAL_DTS_STATE_READY;
860   }
861   else
862   {
863     return HAL_BUSY;
864   }
865 
866   return HAL_OK;
867 }
868 
869 /**
870   * @brief  DTS sensor IRQ Handler.
871   * @param  hdts  DTS handle
872   * @retval None
873   */
HAL_DTS_IRQHandler(DTS_HandleTypeDef * hdts)874 void HAL_DTS_IRQHandler(DTS_HandleTypeDef *hdts)
875 {
876   /* Check end of measure Asynchronous IT */
877   if ((__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_AITE)) != RESET)
878   {
879     __HAL_DTS_CLEAR_FLAG(hdts, DTS_FLAG_TS1_AITE);
880 
881 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
882     hdts->AsyncEndCallback(hdts);
883 #else
884     HAL_DTS_AsyncEndCallback(hdts);
885 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
886   }
887 
888   /* Check low threshold Asynchronous IT */
889   if ((__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_AITL)) != RESET)
890   {
891     __HAL_DTS_CLEAR_FLAG(hdts, DTS_FLAG_TS1_AITL);
892 
893 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
894     hdts->AsyncLowCallback(hdts);
895 #else
896     HAL_DTS_AsyncLowCallback(hdts);
897 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
898   }
899 
900   /* Check high threshold Asynchronous IT */
901   if ((__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_AITH)) != RESET)
902   {
903     __HAL_DTS_CLEAR_FLAG(hdts, DTS_FLAG_TS1_AITH);
904 
905 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
906     hdts->AsyncHighCallback(hdts);
907 #else
908     HAL_DTS_AsyncHighCallback(hdts);
909 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
910   }
911 
912   /* Check end of measure IT */
913   if ((__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_ITE)) != RESET)
914   {
915     __HAL_DTS_CLEAR_FLAG(hdts, DTS_FLAG_TS1_ITE);
916 
917 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
918     hdts->EndCallback(hdts);
919 #else
920     HAL_DTS_EndCallback(hdts);
921 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
922   }
923 
924   /* Check low threshold IT */
925   if ((__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_ITL)) != RESET)
926   {
927     __HAL_DTS_CLEAR_FLAG(hdts, DTS_FLAG_TS1_ITL);
928 
929 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
930     hdts->LowCallback(hdts);
931 #else
932     HAL_DTS_LowCallback(hdts);
933 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
934   }
935 
936   /* Check high threshold IT */
937   if ((__HAL_DTS_GET_FLAG(hdts, DTS_FLAG_TS1_ITH)) != RESET)
938   {
939     __HAL_DTS_CLEAR_FLAG(hdts, DTS_FLAG_TS1_ITH);
940 
941 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1U)
942     hdts->HighCallback(hdts);
943 #else
944     HAL_DTS_HighCallback(hdts);
945 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
946   }
947 }
948 
949 /**
950   * @brief  DTS Sensor End measure callback.
951   * @param  hdts  DTS handle
952   * @retval None
953   */
HAL_DTS_EndCallback(DTS_HandleTypeDef * hdts)954 __weak void HAL_DTS_EndCallback(DTS_HandleTypeDef *hdts)
955 {
956   /* Prevent unused argument(s) compilation warning */
957   UNUSED(hdts);
958 
959   /* NOTE : This function should not be modified, when the callback is needed,
960   the HAL_DTS_EndCallback should be implemented in the user file
961   */
962 }
963 
964 /**
965   * @brief  DTS Sensor low threshold measure callback.
966   * @param  hdts  DTS handle
967   * @retval None
968   */
HAL_DTS_LowCallback(DTS_HandleTypeDef * hdts)969 __weak void HAL_DTS_LowCallback(DTS_HandleTypeDef *hdts)
970 {
971   /* Prevent unused argument(s) compilation warning */
972   UNUSED(hdts);
973 
974   /* NOTE : This function should not be modified, when the callback is needed,
975   the HAL_DTS_LowCallback should be implemented in the user file
976   */
977 }
978 
979 /**
980   * @brief  DTS Sensor high threshold measure callback.
981   * @param  hdts  DTS handle
982   * @retval None
983   */
HAL_DTS_HighCallback(DTS_HandleTypeDef * hdts)984 __weak void HAL_DTS_HighCallback(DTS_HandleTypeDef *hdts)
985 {
986   /* Prevent unused argument(s) compilation warning */
987   UNUSED(hdts);
988 
989   /* NOTE : This function should not be modified, when the callback is needed,
990   the HAL_DTS_HighCallback should be implemented in the user file
991   */
992 }
993 
994 /**
995   * @brief  DTS Sensor asynchronous end measure callback.
996   * @param  hdts  DTS handle
997   * @retval None
998   */
HAL_DTS_AsyncEndCallback(DTS_HandleTypeDef * hdts)999 __weak void HAL_DTS_AsyncEndCallback(DTS_HandleTypeDef *hdts)
1000 {
1001   /* Prevent unused argument(s) compilation warning */
1002   UNUSED(hdts);
1003 
1004   /* NOTE : This function should not be modified, when the callback is needed,
1005   the HAL_DTS_AsyncEndCallback should be implemented in the user file
1006   */
1007 }
1008 
1009 /**
1010   * @brief  DTS Sensor asynchronous low threshold measure callback.
1011   * @param  hdts  DTS handle
1012   * @retval None
1013   */
HAL_DTS_AsyncLowCallback(DTS_HandleTypeDef * hdts)1014 __weak void HAL_DTS_AsyncLowCallback(DTS_HandleTypeDef *hdts)
1015 {
1016   /* Prevent unused argument(s) compilation warning */
1017   UNUSED(hdts);
1018 
1019   /* NOTE : This function should not be modified, when the callback is needed,
1020   the HAL_DTS_AsyncLowCallback should be implemented in the user file
1021   */
1022 }
1023 
1024 /**
1025   * @brief  DTS Sensor asynchronous high threshold measure callback.
1026   * @param  hdts  DTS handle
1027   * @retval None
1028   */
HAL_DTS_AsyncHighCallback(DTS_HandleTypeDef * hdts)1029 __weak void HAL_DTS_AsyncHighCallback(DTS_HandleTypeDef *hdts)
1030 {
1031   /* Prevent unused argument(s) compilation warning */
1032   UNUSED(hdts);
1033 
1034   /* NOTE : This function should not be modified, when the callback is needed,
1035   the HAL_DTS_AsyncHighCallback should be implemented in the user file
1036   */
1037 }
1038 
1039 /**
1040   * @}
1041   */
1042 
1043 /** @defgroup DTS_Exported_Functions_Group3 Peripheral State functions
1044   *  @brief    Peripheral State functions.
1045   *
1046 @verbatim
1047  =======================================================================================================================
1048                                      ##### Peripheral State functions #####
1049  =======================================================================================================================
1050     [..]
1051     This subsection permits to get in run-time the status of the peripheral.
1052 
1053 @endverbatim
1054   * @{
1055   */
1056 
1057 /**
1058   * @brief  Return the DTS handle state.
1059   * @param  hdts  DTS handle
1060   * @retval HAL state
1061   */
HAL_DTS_GetState(const DTS_HandleTypeDef * hdts)1062 HAL_DTS_StateTypeDef HAL_DTS_GetState(const DTS_HandleTypeDef *hdts)
1063 {
1064   /* Check the DTS handle allocation */
1065   if (hdts == NULL)
1066   {
1067     return HAL_DTS_STATE_RESET;
1068   }
1069 
1070   /* Return DTS handle state */
1071   return hdts->State;
1072 }
1073 /**
1074   * @}
1075   */
1076 
1077 /**
1078   * @}
1079   */
1080 
1081 /**
1082   * @}
1083   */
1084 
1085 #endif /* DTS */
1086 
1087 #endif /* HAL_DTS_MODULE_ENABLED */
1088 
1089 /**
1090   * @}
1091   */
1092