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