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