1 /**
2   **********************************************************************************************************************
3   * @file    stm32n6xx_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   *           + Temperature measurement functions.
10   *           + Alarms functions.
11   *           + Sample counter functions.
12   *           + Get extreme temperatures and handle 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 DTS hardware IP is a high-precision low-power junction temperature sensor (TS). It is composed of a
32       configurable controller plus two embedded temperature sensors.
33       In addition of temperature measurements, we have following features for each temperature sensor:
34 
35       (+) Two programmable alarms incorporating hysteresis.
36       (+) Recording of the minimum and maximum temperatures measured.
37 
38       A sample counter is also available.
39 
40   ======================================================================================================================
41                                       ##### How to use this driver #####
42   ======================================================================================================================
43   [..]
44     *** Initialization and de-initialization ***
45     ============================================
46     [..]
47       (#) User has first to initialize DTS instance.
48       (#) As prerequisite, fill in the HAL_DTS_MspInit():
49         (++) Enable DTS clock interface with __HAL_RCC_DTS_CLK_ENABLE().
50         (++) If interrupt mode is used, enable and configure DTS interrupt with HAL_NVIC_SetPriority()
51              and HAL_NVIC_EnableIRQ().
52       (#) Call HAL_DTS_Init() function.
53 
54     [..]
55       (#) User can de-initialize DTS instance with HAL_DTS_DeInit() function.
56 
57     *** Temperature measurement ***
58     ===============================
59     [..]
60       (#) Configure senors using HAL_DTS_ConfigSensor() for each sensor.
61       (#) In polling mode:
62             (++) Use HAL_DTS_Start() to start temperature measurement.
63             (++) Use HAL_DTS_PollForTemperature() to detect the end of measurement.
64                  Use HAL_DTS_GetTemperature to get measured temperature.
65       (#) In interrupt mode:
66             (++) Use HAL_DTS_Start_IT() to start temperature measurement.
67             (++) HAL_DTS_TemperatureCallback() will be called at the end of measurement.
68                  Use HAL_DTS_GetTemperature to get measured temperature.
69       (#) Use HAL_DTS_GetExtremeTemperatures() to get the extreme measured temperatures.
70       (#) Stop measurement using HAL_DTS_Stop() or HAL_DTS_Stop_IT().
71       (#) Configure sample counter using HAL_DTS_ConfigSampleCounter().
72       (#) Get sample counter value using HAL_DTS_GetSampleCounter().
73       (#) Activate sample discard using HAL_DTS_ConfigSampleDiscard().
74 
75     *** Alarms usage ***
76     ====================
77     [..]
78       (#) Configure alarms using HAL_DTS_ConfigAlarmX() for each sensor (X is A or B).
79       (#] HAL_DTS_AlarmXCallback() will be called when alarm X occurs (X is A or B).
80       (#) Disable alarms using HAL_DTS_ConfigAlarmX() (X is A or B).
81 
82     *** generic functions ***
83     =========================
84     [..]
85       (#) HAL_DTS_IRQHandler will be called when DTS interrupt occurs.
86       (#) HAL_DTS_ErrorCallback will be called when DTS error occurs.
87       (#) Use HAL_DTS_GetState() to get the current DTS instance state.
88 
89     *** Callback registration ***
90     =============================
91     [..]
92       The compilation define USE_HAL_DTS_REGISTER_CALLBACKS when set to 1
93       allows the user to configure dynamically the driver callbacks.
94       Use functions HAL_DTS_RegisterCallback() to register a user callback.
95 
96     [..]
97       Functions HAL_DTS_RegisterCallback() and HAL_DTS_UnRegisterCallback allow to register
98       and unregister following callbacks:
99         (+) MspInitCallback     : MSP init callback.
100         (+) MspDeInitCallback   : MSP de-init callback.
101 
102     [..]
103       For temperature measurement complete callback use dedicated register
104       and unregister callback functions:
105         (+) HAL_DTS_RegisterTemperatureCallback().
106         (+) HAL_DTS_UnRegisterTemperatureCallback().
107 
108     [..]
109       For alarms callback use dedicated register and unregister callback functions:
110         (+) HAL_DTS_RegisterAlarmACallback().
111         (+) HAL_DTS_RegisterAlarmBCallback().
112         (+) HAL_DTS_UnRegisterAlarmACallback().
113         (+) HAL_DTS_UnRegisterAlarmBCallback().
114 
115     [..]
116       For error callback use dedicated register and unregister callback functions:
117         (+) HAL_DTS_RegisterErrorCallback().
118         (+) HAL_DTS_UnRegisterErrorCallback().
119 
120     [..]
121       By default, after the call of init function and if the state is RESET
122       all callbacks are reset to the corresponding legacy weak functions.
123       Exception done for MspInit and MspDeInit callbacks that are respectively
124       reset to the legacy weak functions in the init and de-init only when these
125       callbacks are null (not registered beforehand).
126       If not, MspInit or MspDeInit are not null, the init and de-init keep and use
127       the user MspInit/MspDeInit callbacks (registered beforehand).
128 
129     [..]
130       Callbacks can be registered/unregistered in READY state only.
131       Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
132       in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
133       during the init/de-init.
134       In that case first register the MspInit/MspDeInit user callbacks using
135       HAL_DTS_RegisterCallback() before calling init or de-init function.
136 
137     [..]
138       When the compilation define USE_HAL_DTS_REGISTER_CALLBACKS is set to 0 or
139       not defined, the callback registering feature is not available
140       and weak callbacks are used.
141 
142   @endverbatim
143   **********************************************************************************************************************
144   */
145 
146 /* Includes ----------------------------------------------------------------------------------------------------------*/
147 #include "stm32n6xx_hal.h"
148 
149 /** @addtogroup STM32N6xx_HAL_Driver
150   * @{
151   */
152 
153 #ifdef HAL_DTS_MODULE_ENABLED
154 
155 /** @defgroup DTS DTS
156   * @brief DTS HAL module driver
157   * @{
158   */
159 
160 /* Private typedef ---------------------------------------------------------------------------------------------------*/
161 
162 /** @defgroup DTS_Private_Types  DTS Private Types
163   * @{
164   */
165 /**
166   * @brief  DTS SDA registers
167   */
168 typedef enum
169 {
170   DTS_SDATS_CR_REG     = 0x00000000U, /*!< SDA TS control register */
171   DTS_SDATS_CFGR_REG   = 0x01000000U, /*!< SDA TS configuration register */
172   DTS_SDATS_TIMERR_REG = 0x05000000U, /*!< SDA TS timer register */
173 } DTS_SdaRegisterTypeDef;
174 /**
175   * @}
176   */
177 
178 /* Private define ----------------------------------------------------------------------------------------------------*/
179 
180 /** @defgroup DTS_Private_Define  DTS Private Define
181   * @{
182   */
183 #define DTS_SENSOR_ALL            2U          /* DTS sensor 0 and sensor 1 */
184 #define DTS_MAXIMUM_TIMEOUT       1000U       /* DTS maximum timeout of 1 second */
185 #define DTS_SDA_SENSOR_POWER_DOWN 0x00000001U /* Value to set on SDATS_CR register to power down sensor */
186 #define DTS_SDA_POWER_UP_DELAY    256U        /* Value to set on SDATS_TIMERR register for power-up typical delay */
187 #define DTS_CAL5_PARAM            4094.0f     /* Cal5 parameter used for temperature ccomputation */
188 #define DTS_J_PARAM               -0.1f       /* J parameter used for temperature ccomputation */
189 #define DTS_H_PARAM               200.0f      /* H parameter used for temperature ccomputation */
190 #define DTS_G_PARAM               60.0f       /* G parameter used for temperature ccomputation */
191 /**
192   * @}
193   */
194 
195 /* Private macro -----------------------------------------------------------------------------------------------------*/
196 /* Private variables -------------------------------------------------------------------------------------------------*/
197 /* Private function prototypes ---------------------------------------------------------------------------------------*/
198 
199 /** @defgroup DTS_Private_Functions  DTS Private Functions
200   * @{
201   */
202 static HAL_StatusTypeDef DTS_ProgramSdaRegister(DTS_HandleTypeDef *hdts, uint32_t Sensor,
203                                                 DTS_SdaRegisterTypeDef Reg, uint32_t Value);
204 static float_t           DTS_ConvertToCelsiusDegree(uint32_t sample);
205 static uint32_t          DTS_ConvertFromCelsiusDegree(float_t temperature);
206 /**
207   * @}
208   */
209 
210 /* Exported functions ------------------------------------------------------------------------------------------------*/
211 
212 /** @defgroup DTS_Exported_Functions  DTS Exported Functions
213   * @{
214   */
215 
216 /** @defgroup DTS_Exported_Functions_Group1  Initialization and de-initialization functions
217   * @brief    Initialization and de-initialization functions
218   *
219 @verbatim
220   ==============================================================================
221             ##### Initialization and de-initialization functions #####
222   ==============================================================================
223     [..]  This section provides functions allowing to :
224       (+) Initialize the DTS instance.
225       (+) De-initialize the DTS instance.
226       (+) Register and unregister callbacks.
227 @endverbatim
228   * @{
229   */
230 
231 /**
232   * @brief  Initialize the DTS instance and configure DTS system clock.
233   * @param  hdts DTS handle.
234   * @retval HAL status.
235   */
HAL_DTS_Init(DTS_HandleTypeDef * hdts)236 HAL_StatusTypeDef HAL_DTS_Init(DTS_HandleTypeDef *hdts)
237 {
238   HAL_StatusTypeDef status;
239 
240   /* Check DTS handle */
241   if (hdts == NULL)
242   {
243     status = HAL_ERROR;
244   }
245   else
246   {
247     /* Check parameter */
248     assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
249 
250     /* Check that instance has not been already initialized */
251     if (hdts->State != HAL_DTS_STATE_RESET)
252     {
253       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
254       status = HAL_ERROR;
255     }
256     else
257     {
258 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1)
259       /* Reset callback pointers to the weak predefined callbacks */
260       hdts->TemperatureCallback = HAL_DTS_TemperatureCallback;
261       hdts->AlarmACallback      = HAL_DTS_AlarmACallback;
262       hdts->AlarmBCallback      = HAL_DTS_AlarmBCallback;
263       hdts->ErrorCallback       = HAL_DTS_ErrorCallback;
264 
265       /* Call DTS MSP init function */
266       if (hdts->MspInitCallback == NULL)
267       {
268         hdts->MspInitCallback = HAL_DTS_MspInit;
269       }
270       hdts->MspInitCallback(hdts);
271 #else /* USE_HAL_DTS_REGISTER_CALLBACKS */
272       /* Call DTS MSP init function */
273       HAL_DTS_MspInit(hdts);
274 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
275 
276       /* Configure DTS TS clock to TS operating frequency range (4MHz to 8MHz). */
277       /* DTS kernel clock (hsi_div8_ck clock) is divided by (CLK_SYNTH_HI + 1) + (CLK_SYNTH_LO + 1). */
278       /* Because HSI frequency is 64MHz, hsi_div8_ck frequency is 8MHz. */
279       /* So CLK_SYNTH_HI and CLK_SYNTH_LO have to be set to 0 on TSCCLKSYNTHR register */
280       /* in order to have TS clock frequency at 4MHz. */
281       hdts->Instance->TSCCLKSYNTHR = (DTS_TSCCLKSYNTHR_CLK_SYNTH_EN |
282                                       (1U << DTS_TSCCLKSYNTHR_CLK_SYNTH_HOLD_Pos));
283 
284       /* Program typical power-up delay for all sensors */
285       status = DTS_ProgramSdaRegister(hdts, DTS_SENSOR_ALL, DTS_SDATS_TIMERR_REG, DTS_SDA_POWER_UP_DELAY);
286 
287       if (status == HAL_OK)
288       {
289         /* Enable TS interrupt */
290         hdts->Instance->PVT_IER = DTS_PVT_IER_TS_IRQ_ENABLE;
291 
292         /* Reset sensor modes and error code and update state */
293         hdts->SensorMode[0U] = DTS_SENSOR_MODE_DISABLE;
294         hdts->SensorMode[1U] = DTS_SENSOR_MODE_DISABLE;
295         hdts->ErrorCode      = HAL_DTS_ERROR_NONE;
296         /* Update state */
297         hdts->State = HAL_DTS_STATE_READY;
298       }
299     }
300   }
301 
302   /* Return function status */
303   return status;
304 }
305 
306 /**
307   * @brief  De-initialize the DTS instance.
308   * @param  hdts DTS handle.
309   * @retval HAL status.
310   */
HAL_DTS_DeInit(DTS_HandleTypeDef * hdts)311 HAL_StatusTypeDef HAL_DTS_DeInit(DTS_HandleTypeDef *hdts)
312 {
313   HAL_StatusTypeDef status;
314 
315   /* Check DTS handle */
316   if (hdts == NULL)
317   {
318     status = HAL_ERROR;
319   }
320   else
321   {
322     /* Check parameter */
323     assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
324 
325     /* Check that instance has not been already deinitialized */
326     if (hdts->State == HAL_DTS_STATE_RESET)
327     {
328       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
329       status = HAL_ERROR;
330     }
331     else
332     {
333       /* Power down temperature sensors */
334       status = DTS_ProgramSdaRegister(hdts, DTS_SENSOR_ALL, DTS_SDATS_CR_REG, DTS_SDA_SENSOR_POWER_DOWN);
335 
336       if (status == HAL_OK)
337       {
338         /* Disable all interrupts */
339         DTS_Sensor0->TS_IER = 0U;
340         DTS_Sensor1->TS_IER = 0U;
341 
342         /* Clear all pendings flags */
343         DTS_Sensor0->TS_ICR = (DTS_TS_ICR_IRQ_CLEAR_FAULT  | DTS_TS_ICR_IRQ_CLEAR_DONE |
344                                DTS_TS_ICR_IRQ_CLEAR_ALARMA | DTS_TS_ICR_IRQ_CLEAR_ALARMB);
345         DTS_Sensor1->TS_ICR = (DTS_TS_ICR_IRQ_CLEAR_FAULT  | DTS_TS_ICR_IRQ_CLEAR_DONE |
346                                DTS_TS_ICR_IRQ_CLEAR_ALARMA | DTS_TS_ICR_IRQ_CLEAR_ALARMB);
347 
348         /* Disable TS interrupt */
349         hdts->Instance->PVT_IER = 0U;
350 
351         /* Disable DTS system clock */
352         hdts->Instance->TSCCLKSYNTHR &= ~(DTS_TSCCLKSYNTHR_CLK_SYNTH_EN);
353 
354         /* Call DTS MSP deinit function */
355 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1)
356         if (hdts->MspDeInitCallback == NULL)
357         {
358           hdts->MspDeInitCallback = HAL_DTS_MspDeInit;
359         }
360         hdts->MspDeInitCallback(hdts);
361 #else /* USE_HAL_DTS_REGISTER_CALLBACKS */
362         HAL_DTS_MspDeInit(hdts);
363 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
364 
365         /*Update state */
366         hdts->State = HAL_DTS_STATE_RESET;
367       }
368     }
369   }
370 
371   /* Return function status */
372   return status;
373 }
374 
375 /**
376   * @brief  Initialize the DTS instance MSP.
377   * @param  hdts DTS handle.
378   * @retval None.
379   */
HAL_DTS_MspInit(DTS_HandleTypeDef * hdts)380 __weak void HAL_DTS_MspInit(DTS_HandleTypeDef *hdts)
381 {
382   /* Prevent unused argument(s) compilation warning */
383   UNUSED(hdts);
384 
385   /* NOTE : This function should not be modified, when the function is needed,
386             the HAL_DTS_MspInit could be implemented in the user file */
387 }
388 
389 /**
390   * @brief  De-initialize the DTS instance MSP.
391   * @param  hdts DTS handle.
392   * @retval None.
393   */
HAL_DTS_MspDeInit(DTS_HandleTypeDef * hdts)394 __weak void HAL_DTS_MspDeInit(DTS_HandleTypeDef *hdts)
395 {
396   /* Prevent unused argument(s) compilation warning */
397   UNUSED(hdts);
398 
399   /* NOTE : This function should not be modified, when the function is needed,
400             the HAL_DTS_MspDeInit could be implemented in the user file */
401 }
402 
403 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1)
404 /**
405   * @brief  Register a user DTS callback to be used instead of the weak predefined callback.
406   * @param  hdts DTS handle.
407   * @param  CallbackID ID of the callback to be registered.
408   *         This parameter can be one of the following values:
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 parameter */
421   if (hdts == NULL)
422   {
423     status = HAL_ERROR;
424   }
425   else if (pCallback == NULL)
426   {
427     hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_PARAM;
428     status = HAL_ERROR;
429   }
430   else
431   {
432     const HAL_DTS_StateTypeDef temp_state = hdts->State;
433 
434     if ((temp_state == HAL_DTS_STATE_READY) || (temp_state == HAL_DTS_STATE_RESET))
435     {
436       switch (CallbackID)
437       {
438         case HAL_DTS_MSPINIT_CB_ID :
439           hdts->MspInitCallback = pCallback;
440           break;
441         case HAL_DTS_MSPDEINIT_CB_ID :
442           hdts->MspDeInitCallback = pCallback;
443           break;
444         default :
445           hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_CALLBACK;
446           status = HAL_ERROR;
447           break;
448       }
449     }
450     else
451     {
452       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
453       status = HAL_ERROR;
454     }
455   }
456 
457   /* Return function status */
458   return status;
459 }
460 
461 /**
462   * @brief  Unregister a user DTS callback.
463   *         DTS callback is redirected to the weak predefined callback.
464   * @param  hdts DTS handle.
465   * @param  CallbackID ID of the callback to be unregistered.
466   *         This parameter can be one of the following values:
467   *           @arg @ref HAL_DTS_MSPINIT_CB_ID MSP init callback ID.
468   *           @arg @ref HAL_DTS_MSPDEINIT_CB_ID MSP de-init callback ID.
469   * @retval HAL status.
470   */
HAL_DTS_UnRegisterCallback(DTS_HandleTypeDef * hdts,HAL_DTS_CallbackIDTypeDef CallbackID)471 HAL_StatusTypeDef HAL_DTS_UnRegisterCallback(DTS_HandleTypeDef        *hdts,
472                                              HAL_DTS_CallbackIDTypeDef CallbackID)
473 {
474   HAL_StatusTypeDef status = HAL_OK;
475 
476   /* Check parameter */
477   if (hdts == NULL)
478   {
479     status = HAL_ERROR;
480   }
481   else
482   {
483     const HAL_DTS_StateTypeDef temp_state = hdts->State;
484 
485     if ((temp_state == HAL_DTS_STATE_READY) || (temp_state == HAL_DTS_STATE_RESET))
486     {
487       switch (CallbackID)
488       {
489         case HAL_DTS_MSPINIT_CB_ID :
490           hdts->MspInitCallback = HAL_DTS_MspInit;
491           break;
492         case HAL_DTS_MSPDEINIT_CB_ID :
493           hdts->MspDeInitCallback = HAL_DTS_MspDeInit;
494           break;
495         default :
496           hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_CALLBACK;
497           status = HAL_ERROR;
498           break;
499       }
500     }
501     else
502     {
503       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
504       status = HAL_ERROR;
505     }
506   }
507 
508   /* Return function status */
509   return status;
510 }
511 
512 /**
513   * @brief  Register a user DTS sensor callback to be used instead of the weak predefined callback.
514   * @param  hdts DTS handle.
515   * @param  CallbackID ID of the sensor callback to be registered.
516   *         This parameter can be one of the following values:
517   *           @arg @ref HAL_DTS_SENSOR_TEMPERATURE_CB_ID Sensor temperature callback ID.
518   *           @arg @ref HAL_DTS_SENSOR_ALARMA_CB_ID Sensor alarm A callback ID.
519   *           @arg @ref HAL_DTS_SENSOR_ALARMB_CB_ID Sensor alarm B callback ID.
520   *           @arg @ref HAL_DTS_SENSOR_ERROR_CB_ID Sensor error callback ID.
521   * @param  pCallback pointer to the sensor callback function.
522   * @retval HAL status.
523   */
HAL_DTS_RegisterSensorCallback(DTS_HandleTypeDef * hdts,HAL_DTS_SensorCallbackIDTypeDef CallbackID,pDTS_SensorCallbackTypeDef pCallback)524 HAL_StatusTypeDef HAL_DTS_RegisterSensorCallback(DTS_HandleTypeDef              *hdts,
525                                                  HAL_DTS_SensorCallbackIDTypeDef CallbackID,
526                                                  pDTS_SensorCallbackTypeDef      pCallback)
527 {
528   HAL_StatusTypeDef status = HAL_OK;
529 
530   /* Check parameter */
531   if (hdts == NULL)
532   {
533     status = HAL_ERROR;
534   }
535   else if (pCallback == NULL)
536   {
537     hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_PARAM;
538     status = HAL_ERROR;
539   }
540   else
541   {
542     if (hdts->State == HAL_DTS_STATE_READY)
543     {
544       switch (CallbackID)
545       {
546         case HAL_DTS_SENSOR_TEMPERATURE_CB_ID :
547           hdts->TemperatureCallback = pCallback;
548           break;
549         case HAL_DTS_SENSOR_ALARMA_CB_ID :
550           hdts->AlarmACallback = pCallback;
551           break;
552         case HAL_DTS_SENSOR_ALARMB_CB_ID :
553           hdts->AlarmBCallback = pCallback;
554           break;
555         case HAL_DTS_SENSOR_ERROR_CB_ID :
556           hdts->ErrorCallback = pCallback;
557           break;
558         default :
559           hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_CALLBACK;
560           status = HAL_ERROR;
561           break;
562       }
563     }
564     else
565     {
566       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
567       status = HAL_ERROR;
568     }
569   }
570 
571   /* Return function status */
572   return status;
573 }
574 
575 /**
576   * @brief  Unregister a user DTS sensor callback.
577   *         DTS sensor callback is redirected to the weak predefined callback.
578   * @param  hdts DTS handle.
579   * @param  CallbackID ID of the sensor callback to be unregistered.
580   *         This parameter can be one of the following values:
581   *           @arg @ref HAL_DTS_SENSOR_TEMPERATURE_CB_ID Sensor temperature callback ID.
582   *           @arg @ref HAL_DTS_SENSOR_ALARMA_CB_ID Sensor alarm A callback ID.
583   *           @arg @ref HAL_DTS_SENSOR_ALARMB_CB_ID Sensor alarm B callback ID.
584   *           @arg @ref HAL_DTS_SENSOR_ERROR_CB_ID Sensor error callback ID.
585   * @retval HAL status.
586   */
HAL_DTS_UnRegisterSensorCallback(DTS_HandleTypeDef * hdts,HAL_DTS_SensorCallbackIDTypeDef CallbackID)587 HAL_StatusTypeDef HAL_DTS_UnRegisterSensorCallback(DTS_HandleTypeDef              *hdts,
588                                                    HAL_DTS_SensorCallbackIDTypeDef CallbackID)
589 {
590   HAL_StatusTypeDef status = HAL_OK;
591 
592   /* Check parameter */
593   if (hdts == NULL)
594   {
595     status = HAL_ERROR;
596   }
597   else
598   {
599     if (hdts->State == HAL_DTS_STATE_READY)
600     {
601       switch (CallbackID)
602       {
603         case HAL_DTS_SENSOR_TEMPERATURE_CB_ID :
604           hdts->TemperatureCallback = HAL_DTS_TemperatureCallback;
605           break;
606         case HAL_DTS_SENSOR_ALARMA_CB_ID :
607           hdts->AlarmACallback = HAL_DTS_AlarmACallback;
608           break;
609         case HAL_DTS_SENSOR_ALARMB_CB_ID :
610           hdts->AlarmBCallback = HAL_DTS_AlarmBCallback;
611           break;
612         case HAL_DTS_SENSOR_ERROR_CB_ID :
613           hdts->ErrorCallback = HAL_DTS_ErrorCallback;
614           break;
615         default :
616           hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_CALLBACK;
617           status = HAL_ERROR;
618           break;
619       }
620     }
621     else
622     {
623       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
624       status = HAL_ERROR;
625     }
626   }
627 
628   /* Return function status */
629   return status;
630 }
631 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
632 
633 /**
634   * @}
635   */
636 
637 /** @defgroup DTS_Exported_Functions_Group2  Temperature measurement functions
638   * @brief    Temperature measuremen functions
639   *
640 @verbatim
641   ==============================================================================
642                     ##### Temperature measurement functions #####
643   ==============================================================================
644     [..]  This section provides functions allowing to :
645       (+) configure sensor.
646       (+) Start and stop temperature measurement in polling or interrupt mode.
647       (+) Wait and get temperature values.
648       (+) Get extreme temperature values.
649       (+) Configure and get sample counter.
650       (+) Configure sample discard.
651 @endverbatim
652   * @{
653   */
654 
655 /**
656   * @brief  Configure a sensor.
657   * @param  hdts DTS handle.
658   * @param  Sensor Sensor to configure.
659   *         This parameter can be one of the following values:
660   *           @arg @ref DTS_SENSOR_0 Sensor 0.
661   *           @arg @ref DTS_SENSOR_1 Sensor 1.
662   * @param  pSensorParams Sensor parameters.
663   * @retval HAL status.
664   */
HAL_DTS_ConfigSensor(DTS_HandleTypeDef * hdts,HAL_DTS_Sensor Sensor,DTS_SensorConfigTypeDef * pSensorParams)665 HAL_StatusTypeDef HAL_DTS_ConfigSensor(DTS_HandleTypeDef       *hdts,
666                                        HAL_DTS_Sensor           Sensor,
667                                        DTS_SensorConfigTypeDef *pSensorParams)
668 {
669   HAL_StatusTypeDef status = HAL_OK;
670 
671   /* Check parameters */
672   if (hdts == NULL)
673   {
674     status = HAL_ERROR;
675   }
676   else if (pSensorParams == NULL)
677   {
678     hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_PARAM;
679     status = HAL_ERROR;
680   }
681   else
682   {
683     const HAL_DTS_StateTypeDef temp_state = hdts->State;
684 
685     /* Check parameters */
686     assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
687     assert_param(IS_DTS_SENSOR_MODE(pSensorParams->Mode));
688 
689     /* Check current state */
690     if ((temp_state == HAL_DTS_STATE_READY) ||
691         ((temp_state == HAL_DTS_STATE_RUNNING_1) && (Sensor == DTS_SENSOR_0)) ||
692         ((temp_state == HAL_DTS_STATE_RUNNING_0) && (Sensor == DTS_SENSOR_1)))
693     {
694       if (pSensorParams->Mode != DTS_SENSOR_MODE_DISABLE)
695       {
696         /* Check resolution parameter */
697         assert_param(IS_DTS_SENSOR_RESOLUTION(pSensorParams->Resolution));
698 
699         /* Program sensor trigger and resolution */
700         if (pSensorParams->Mode == DTS_SENSOR_MODE_TRIGGER)
701         {
702           /* Check sensor trigger parameter */
703           assert_param(IS_DTS_SENSOR_TRIGGER(pSensorParams->Trigger));
704           status = DTS_ProgramSdaRegister(hdts, (uint32_t) Sensor, DTS_SDATS_CFGR_REG,
705                                           (pSensorParams->Trigger | pSensorParams->Resolution));
706         }
707         else
708         {
709           status = DTS_ProgramSdaRegister(hdts, (uint32_t) Sensor, DTS_SDATS_CFGR_REG, pSensorParams->Resolution);
710         }
711 
712         if (status == HAL_OK)
713         {
714           /* Remove potential sensor disabling in TSCSDIFDISABLER register */
715           hdts->Instance->TSCSDIFDISABLER &= (Sensor == DTS_SENSOR_0) ? ~(DTS_TSCSDIFDISABLER_TS0_SDIF_DISABLE) :
716                                              ~(DTS_TSCSDIFDISABLER_TS1_SDIF_DISABLE);
717 
718           /* Update sensor mode */
719           hdts->SensorMode[Sensor] = pSensorParams->Mode;
720         }
721       }
722       else
723       {
724         /* Disable sensor */
725         hdts->Instance->TSCSDIFDISABLER |= (Sensor == DTS_SENSOR_0) ? DTS_TSCSDIFDISABLER_TS0_SDIF_DISABLE :
726                                            DTS_TSCSDIFDISABLER_TS1_SDIF_DISABLE;
727 
728         /* Update sensor mode */
729         hdts->SensorMode[Sensor] = DTS_SENSOR_MODE_DISABLE;
730       }
731     }
732     else
733     {
734       /* function call in wrong state */
735       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
736       status = HAL_ERROR;
737     }
738   }
739 
740   /* Return function status */
741   return status;
742 }
743 
744 /**
745   * @brief  Start temperature measurement on one sensor in polling mode.
746   * @param  hdts DTS handle.
747   * @param  Sensor Sensor.
748   *         This parameter can be one of the following values:
749   *           @arg @ref DTS_SENSOR_0 Sensor 0.
750   *           @arg @ref DTS_SENSOR_1 Sensor 1.
751   * @retval HAL status.
752   */
HAL_DTS_Start(DTS_HandleTypeDef * hdts,HAL_DTS_Sensor Sensor)753 HAL_StatusTypeDef HAL_DTS_Start(DTS_HandleTypeDef *hdts,
754                                 HAL_DTS_Sensor     Sensor)
755 {
756   HAL_StatusTypeDef status;
757 
758   /* Check parameter */
759   if (hdts == NULL)
760   {
761     status = HAL_ERROR;
762   }
763   else
764   {
765     const HAL_DTS_StateTypeDef temp_state = hdts->State;
766 
767     /* Check parameter */
768     assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
769 
770     /* Check current state */
771     if ((temp_state == HAL_DTS_STATE_READY) ||
772         ((temp_state == HAL_DTS_STATE_RUNNING_1) && (Sensor == DTS_SENSOR_0)) ||
773         ((temp_state == HAL_DTS_STATE_RUNNING_0) && (Sensor == DTS_SENSOR_1)))
774     {
775       /* Check sensor mode */
776       if (hdts->SensorMode[Sensor] == DTS_SENSOR_MODE_DISABLE)
777       {
778         hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_SENSOR_MODE;
779         status = HAL_ERROR;
780       }
781       else
782       {
783         DTS_SensorTypeDef *psensor;
784 
785         psensor = (Sensor == DTS_SENSOR_0) ? DTS_Sensor0 : DTS_Sensor1;
786 
787         /* Reset min and max measured values for this sensor */
788         psensor->TSHILORESETR = (DTS_TSHILORESETR_SMPL_LO_SET | DTS_TSHILORESETR_SMPL_HI_CLR);
789 
790         /* Start sensor with associated mode */
791         status = DTS_ProgramSdaRegister(hdts, (uint32_t) Sensor, DTS_SDATS_CR_REG, hdts->SensorMode[Sensor]);
792 
793         if (status == HAL_OK)
794         {
795           /* Update state */
796           if (hdts->State == HAL_DTS_STATE_READY)
797           {
798             hdts->State = (Sensor == DTS_SENSOR_0) ? HAL_DTS_STATE_RUNNING_0 : HAL_DTS_STATE_RUNNING_1;
799           }
800           else
801           {
802             hdts->State = HAL_DTS_STATE_RUNNING_BOTH;
803           }
804         }
805       }
806     }
807     else
808     {
809       /* function call in wrong state */
810       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
811       status = HAL_ERROR;
812     }
813   }
814 
815   /* Return function status */
816   return status;
817 }
818 
819 /**
820   * @brief  Poll for available temperature measurement on one sensor.
821   * @param  hdts DTS handle.
822   * @param  Sensor Sensor.
823   *         This parameter can be one of the following values:
824   *           @arg @ref DTS_SENSOR_0 Sensor 0.
825   *           @arg @ref DTS_SENSOR_1 Sensor 1.
826   * @param  Timeout Timeout value in milliseconds.
827   * @retval HAL status.
828   */
HAL_DTS_PollForTemperature(DTS_HandleTypeDef * hdts,HAL_DTS_Sensor Sensor,uint32_t Timeout)829 HAL_StatusTypeDef HAL_DTS_PollForTemperature(DTS_HandleTypeDef *hdts,
830                                              HAL_DTS_Sensor     Sensor,
831                                              uint32_t           Timeout)
832 {
833   HAL_StatusTypeDef status = HAL_OK;
834 
835   /* Check parameter */
836   if (hdts == NULL)
837   {
838     status = HAL_ERROR;
839   }
840   else
841   {
842     const HAL_DTS_StateTypeDef temp_state = hdts->State;
843 
844     /* Check parameter */
845     assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
846 
847     /* Check current state */
848     if ((temp_state == HAL_DTS_STATE_RUNNING_BOTH) ||
849         ((temp_state == HAL_DTS_STATE_RUNNING_0) && (Sensor == DTS_SENSOR_0)) ||
850         ((temp_state == HAL_DTS_STATE_RUNNING_1) && (Sensor == DTS_SENSOR_1)))
851     {
852       uint32_t tickstart = HAL_GetTick();
853       const DTS_SensorTypeDef *psensor;
854 
855       psensor = (Sensor == DTS_SENSOR_0) ? DTS_Sensor0 : DTS_Sensor1;
856 
857       /* Wait for available temperature measurement */
858       while (((psensor->TSSDIFDONER & DTS_TSSDIFDONER_SDIF_SMPL_DONE) != DTS_TSSDIFDONER_SDIF_SMPL_DONE) &&
859              (status == HAL_OK))
860       {
861         /* Check the timeout */
862         if (Timeout != HAL_MAX_DELAY)
863         {
864           if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
865           {
866             status = HAL_TIMEOUT;
867           }
868         }
869       }
870 
871       if (status == HAL_OK)
872       {
873         /* Update state */
874         if (hdts->SensorMode[Sensor] == DTS_SENSOR_MODE_SINGLE)
875         {
876           if (hdts->State == HAL_DTS_STATE_RUNNING_BOTH)
877           {
878             hdts->State = (Sensor == DTS_SENSOR_0) ? HAL_DTS_STATE_RUNNING_1 : HAL_DTS_STATE_RUNNING_0;
879           }
880           else
881           {
882             hdts->State = HAL_DTS_STATE_READY;
883           }
884         }
885       }
886     }
887     else
888     {
889       /* function call in wrong state */
890       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
891       status = HAL_ERROR;
892     }
893   }
894 
895   /* Return function status */
896   return status;
897 }
898 
899 /**
900   * @brief  Get temperature measurement on one sensor.
901   * @param  hdts DTS handle.
902   * @param  Sensor Sensor.
903   *         This parameter can be one of the following values:
904   *           @arg @ref DTS_SENSOR_0 Sensor 0.
905   *           @arg @ref DTS_SENSOR_1 Sensor 1.
906   * @param  pTemperature Pointer to temperature in celsius degree.
907   * @retval HAL status.
908   */
HAL_DTS_GetTemperature(DTS_HandleTypeDef * hdts,HAL_DTS_Sensor Sensor,float_t * pTemperature)909 HAL_StatusTypeDef HAL_DTS_GetTemperature(DTS_HandleTypeDef *hdts,
910                                          HAL_DTS_Sensor     Sensor,
911                                          float_t           *pTemperature)
912 {
913   HAL_StatusTypeDef status = HAL_OK;
914 
915   /* Check parameters */
916   if (hdts == NULL)
917   {
918     status = HAL_ERROR;
919   }
920   else if (pTemperature == NULL)
921   {
922     hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_PARAM;
923     status = HAL_ERROR;
924   }
925   else
926   {
927     /* Check parameter */
928     assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
929 
930     /* Check current state */
931     if (hdts->State != HAL_DTS_STATE_RESET)
932     {
933       const DTS_SensorTypeDef *psensor;
934       uint32_t sample;
935 
936       psensor = (Sensor == DTS_SENSOR_0) ? DTS_Sensor0 : DTS_Sensor1;
937 
938       /* Get sample */
939       sample = psensor->TSSDIFDATAR;
940 
941       /* Check if sample is valid */
942       if ((sample & (DTS_TSSDIFDATAR_SAMPLE_TYPE | DTS_TSSDIFDATAR_SAMPLE_FAULT)) == 0U)
943       {
944         /* Convert sample in celsius degree */
945         *pTemperature = DTS_ConvertToCelsiusDegree((sample & DTS_TSSDIFDATAR_SAMPLE_DATA_Msk));
946       }
947       else
948       {
949         /* Invalid sample or fault*/
950         if ((sample & DTS_TSSDIFDATAR_SAMPLE_TYPE) != 0U)
951         {
952           hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_SAMPLE;
953         }
954         else
955         {
956           hdts->ErrorCode |= HAL_DTS_ERROR_FAULT;
957         }
958         status = HAL_ERROR;
959       }
960     }
961     else
962     {
963       /* function call in wrong state */
964       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
965       status = HAL_ERROR;
966     }
967   }
968 
969   /* Return function status */
970   return status;
971 }
972 
973 /**
974   * @brief  Stop temperature measurement on one sensor in polling mode.
975   * @param  hdts DTS handle.
976   * @param  Sensor Sensor.
977   *         This parameter can be one of the following values:
978   *           @arg @ref DTS_SENSOR_0 Sensor 0.
979   *           @arg @ref DTS_SENSOR_1 Sensor 1.
980   * @retval HAL status.
981   */
HAL_DTS_Stop(DTS_HandleTypeDef * hdts,HAL_DTS_Sensor Sensor)982 HAL_StatusTypeDef HAL_DTS_Stop(DTS_HandleTypeDef *hdts,
983                                HAL_DTS_Sensor     Sensor)
984 {
985   HAL_StatusTypeDef status;
986 
987   /* Check parameter */
988   if (hdts == NULL)
989   {
990     status = HAL_ERROR;
991   }
992   else
993   {
994     const HAL_DTS_StateTypeDef temp_state = hdts->State;
995 
996     /* Check parameter */
997     assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
998 
999     /* Check current state */
1000     if ((temp_state == HAL_DTS_STATE_RUNNING_BOTH) ||
1001         ((temp_state == HAL_DTS_STATE_RUNNING_0) && (Sensor == DTS_SENSOR_0)) ||
1002         ((temp_state == HAL_DTS_STATE_RUNNING_1) && (Sensor == DTS_SENSOR_1)))
1003     {
1004       /* Power down sensor */
1005       status = DTS_ProgramSdaRegister(hdts, (uint32_t) Sensor, DTS_SDATS_CR_REG, DTS_SDA_SENSOR_POWER_DOWN);
1006 
1007       if (status == HAL_OK)
1008       {
1009         /* Update state */
1010         if (hdts->State == HAL_DTS_STATE_RUNNING_BOTH)
1011         {
1012           hdts->State = (Sensor == DTS_SENSOR_0) ? HAL_DTS_STATE_RUNNING_1 : HAL_DTS_STATE_RUNNING_0;
1013         }
1014         else
1015         {
1016           hdts->State = HAL_DTS_STATE_READY;
1017         }
1018       }
1019     }
1020     else
1021     {
1022       /* function call in wrong state */
1023       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
1024       status = HAL_ERROR;
1025     }
1026   }
1027 
1028   /* Return function status */
1029   return status;
1030 }
1031 
1032 /**
1033   * @brief  Start temperature measurement on one sensor in interrupt mode.
1034   * @param  hdts DTS handle.
1035   * @param  Sensor Sensor.
1036   *         This parameter can be one of the following values:
1037   *           @arg @ref DTS_SENSOR_0 Sensor 0.
1038   *           @arg @ref DTS_SENSOR_1 Sensor 1.
1039   * @retval HAL status.
1040   */
HAL_DTS_Start_IT(DTS_HandleTypeDef * hdts,HAL_DTS_Sensor Sensor)1041 HAL_StatusTypeDef HAL_DTS_Start_IT(DTS_HandleTypeDef *hdts,
1042                                    HAL_DTS_Sensor     Sensor)
1043 {
1044   HAL_StatusTypeDef status;
1045 
1046   /* Check parameter */
1047   if (hdts == NULL)
1048   {
1049     status = HAL_ERROR;
1050   }
1051   else
1052   {
1053     const HAL_DTS_StateTypeDef temp_state = hdts->State;
1054 
1055     /* Check parameter */
1056     assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
1057 
1058     /* Check current state */
1059     if ((temp_state == HAL_DTS_STATE_READY) ||
1060         ((temp_state == HAL_DTS_STATE_RUNNING_1) && (Sensor == DTS_SENSOR_0)) ||
1061         ((temp_state == HAL_DTS_STATE_RUNNING_0) && (Sensor == DTS_SENSOR_1)))
1062     {
1063       /* Check sensor mode */
1064       if (hdts->SensorMode[Sensor] == DTS_SENSOR_MODE_DISABLE)
1065       {
1066         hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_SENSOR_MODE;
1067         status = HAL_ERROR;
1068       }
1069       else
1070       {
1071         DTS_SensorTypeDef *psensor;
1072 
1073         psensor = (Sensor == DTS_SENSOR_0) ? DTS_Sensor0 : DTS_Sensor1;
1074 
1075         /* Enable sample done and fault interrupts */
1076         psensor->TS_IER |= (DTS_TS_IER_IRQ_EN_FAULT | DTS_TS_IER_IRQ_EN_DONE);
1077 
1078         /* Reset min and max measured values for this sensor */
1079         psensor->TSHILORESETR = (DTS_TSHILORESETR_SMPL_LO_SET | DTS_TSHILORESETR_SMPL_HI_CLR);
1080 
1081         /* Start sensor with associated mode */
1082         status = DTS_ProgramSdaRegister(hdts, (uint32_t) Sensor, DTS_SDATS_CR_REG, hdts->SensorMode[Sensor]);
1083 
1084         if (status == HAL_OK)
1085         {
1086           /* Update state */
1087           if (hdts->State == HAL_DTS_STATE_READY)
1088           {
1089             hdts->State = (Sensor == DTS_SENSOR_0) ? HAL_DTS_STATE_RUNNING_0 : HAL_DTS_STATE_RUNNING_1;
1090           }
1091           else
1092           {
1093             hdts->State = HAL_DTS_STATE_RUNNING_BOTH;
1094           }
1095         }
1096       }
1097     }
1098     else
1099     {
1100       /* function call in wrong state */
1101       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
1102       status = HAL_ERROR;
1103     }
1104   }
1105 
1106   /* Return function status */
1107   return status;
1108 }
1109 
1110 /**
1111   * @brief  Stop temperature measurement on one sensor in interrupt mode.
1112   * @param  hdts DTS handle.
1113   * @param  Sensor Sensor.
1114   *         This parameter can be one of the following values:
1115   *           @arg @ref DTS_SENSOR_0 Sensor 0.
1116   *           @arg @ref DTS_SENSOR_1 Sensor 1.
1117   * @retval HAL status.
1118   */
HAL_DTS_Stop_IT(DTS_HandleTypeDef * hdts,HAL_DTS_Sensor Sensor)1119 HAL_StatusTypeDef HAL_DTS_Stop_IT(DTS_HandleTypeDef *hdts,
1120                                   HAL_DTS_Sensor     Sensor)
1121 {
1122   HAL_StatusTypeDef status;
1123 
1124   /* Check parameter */
1125   if (hdts == NULL)
1126   {
1127     status = HAL_ERROR;
1128   }
1129   else
1130   {
1131     const HAL_DTS_StateTypeDef temp_state = hdts->State;
1132 
1133     /* Check parameter */
1134     assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
1135 
1136     /* Check current state */
1137     if ((temp_state == HAL_DTS_STATE_RUNNING_BOTH) ||
1138         ((temp_state == HAL_DTS_STATE_RUNNING_0) && (Sensor == DTS_SENSOR_0)) ||
1139         ((temp_state == HAL_DTS_STATE_RUNNING_1) && (Sensor == DTS_SENSOR_1)))
1140     {
1141       DTS_SensorTypeDef *psensor;
1142 
1143       psensor = (Sensor == DTS_SENSOR_0) ? DTS_Sensor0 : DTS_Sensor1;
1144 
1145       /* Power down sensor */
1146       status = DTS_ProgramSdaRegister(hdts, (uint32_t) Sensor, DTS_SDATS_CR_REG, DTS_SDA_SENSOR_POWER_DOWN);
1147 
1148       if (status == HAL_OK)
1149       {
1150         /* Disable sample done and fault interrupts */
1151         psensor->TS_IER &= ~(DTS_TS_IER_IRQ_EN_FAULT | DTS_TS_IER_IRQ_EN_DONE);
1152 
1153         /* Clear potential sample done and fault interrupt flags */
1154         psensor->TS_ICR |= (DTS_TS_ICR_IRQ_CLEAR_FAULT | DTS_TS_ICR_IRQ_CLEAR_DONE);
1155 
1156         /* Update state */
1157         if (hdts->State == HAL_DTS_STATE_RUNNING_BOTH)
1158         {
1159           hdts->State = (Sensor == DTS_SENSOR_0) ? HAL_DTS_STATE_RUNNING_1 : HAL_DTS_STATE_RUNNING_0;
1160         }
1161         else
1162         {
1163           hdts->State = HAL_DTS_STATE_READY;
1164         }
1165       }
1166     }
1167     else
1168     {
1169       /* function call in wrong state */
1170       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
1171       status = HAL_ERROR;
1172     }
1173   }
1174 
1175   /* Return function status */
1176   return status;
1177 }
1178 
1179 /**
1180   * @brief  DTS temperature callback.
1181   * @param  hdts DTS handle.
1182   * @param  Sensor Sensor.
1183   *         This parameter can be one of the following values:
1184   *           @arg @ref DTS_SENSOR_0 Sensor 0.
1185   *           @arg @ref DTS_SENSOR_1 Sensor 1.
1186   * @retval None.
1187   */
HAL_DTS_TemperatureCallback(DTS_HandleTypeDef * hdts,HAL_DTS_Sensor Sensor)1188 __weak void HAL_DTS_TemperatureCallback(DTS_HandleTypeDef *hdts,
1189                                         HAL_DTS_Sensor     Sensor)
1190 {
1191   /* Prevent unused argument(s) compilation warning */
1192   UNUSED(hdts);
1193   UNUSED(Sensor);
1194 
1195   /* NOTE : This function should not be modified, when the function is needed,
1196             the HAL_DTS_TemperatureCallback could be implemented in the user file */
1197 }
1198 
1199 /**
1200   * @brief  Get extreme temperatures on one sensor.
1201   * @param  hdts DTS handle.
1202   * @param  Sensor Sensor.
1203   *         This parameter can be one of the following values:
1204   *           @arg @ref DTS_SENSOR_0 Sensor 0.
1205   *           @arg @ref DTS_SENSOR_1 Sensor 1.
1206   * @param  pMin Pointer to minimum temperature in celsius degree.
1207   * @param  pMax Pointer to maximum temperature in celsius degree.
1208   * @retval HAL status.
1209   */
HAL_DTS_GetExtremeTemperatures(DTS_HandleTypeDef * hdts,HAL_DTS_Sensor Sensor,float_t * pMin,float_t * pMax)1210 HAL_StatusTypeDef HAL_DTS_GetExtremeTemperatures(DTS_HandleTypeDef *hdts,
1211                                                  HAL_DTS_Sensor     Sensor,
1212                                                  float_t           *pMin,
1213                                                  float_t           *pMax)
1214 {
1215   HAL_StatusTypeDef status = HAL_OK;
1216 
1217   /* Check parameters */
1218   if (hdts == NULL)
1219   {
1220     status = HAL_ERROR;
1221   }
1222   else if ((pMin == NULL) || (pMax == NULL))
1223   {
1224     hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_PARAM;
1225     status = HAL_ERROR;
1226   }
1227   else
1228   {
1229     /* Check parameter */
1230     assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
1231 
1232     /* Check current state */
1233     if (hdts->State != HAL_DTS_STATE_RESET)
1234     {
1235       const DTS_SensorTypeDef *psensor;
1236       uint32_t sample;
1237 
1238       psensor = (Sensor == DTS_SENSOR_0) ? DTS_Sensor0 : DTS_Sensor1;
1239 
1240       /* Get min and max samples then convert it */
1241       sample = psensor->TSHLSAMPLER;
1242       *pMin = DTS_ConvertToCelsiusDegree((sample & DTS_TSHLSAMPLER_SMPL_LO_Msk));
1243       *pMax = DTS_ConvertToCelsiusDegree((sample & DTS_TSHLSAMPLER_SMPL_HI_Msk) >> DTS_TSHLSAMPLER_SMPL_HI_Pos);
1244     }
1245     else
1246     {
1247       /* function call in wrong state */
1248       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
1249       status = HAL_ERROR;
1250     }
1251   }
1252 
1253   /* Return function status */
1254   return status;
1255 }
1256 
1257 /**
1258   * @brief  Configure sample counter.
1259   * @param  hdts DTS handle.
1260   * @param  Enable Enable sample counter.
1261   * @param  Clear Clear sample counter.
1262   * @param  Hold Set sample counter on hold.
1263   * @retval HAL status.
1264   */
HAL_DTS_ConfigSampleCounter(DTS_HandleTypeDef * hdts,FunctionalState Enable,FunctionalState Clear,FunctionalState Hold)1265 HAL_StatusTypeDef HAL_DTS_ConfigSampleCounter(DTS_HandleTypeDef *hdts,
1266                                               FunctionalState    Enable,
1267                                               FunctionalState    Clear,
1268                                               FunctionalState    Hold)
1269 {
1270   HAL_StatusTypeDef status = HAL_OK;
1271 
1272   /* Check parameter */
1273   if (hdts == NULL)
1274   {
1275     status = HAL_ERROR;
1276   }
1277   else
1278   {
1279     /* Check parameter */
1280     assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
1281 
1282     /* Check current state */
1283     if (hdts->State != HAL_DTS_STATE_RESET)
1284     {
1285       /* Enable or disable sample counter */
1286       if (Enable == ENABLE)
1287       {
1288         hdts->Instance->TSCSMPL_CR &= ~(DTS_TSCSMPL_CR_SMPL_CTR_DISABLE);
1289       }
1290       else
1291       {
1292         hdts->Instance->TSCSMPL_CR |= DTS_TSCSMPL_CR_SMPL_CTR_DISABLE;
1293       }
1294 
1295       /* Clear sample counter */
1296       if (Clear == ENABLE)
1297       {
1298         hdts->Instance->TSCSDIFSMPLCLRR |= DTS_TSCSDIFSMPLCLRR_SMPL_CNTER_CLEAR;
1299       }
1300 
1301       /* Configure hold status for sample counter */
1302       if (Hold == ENABLE)
1303       {
1304         hdts->Instance->TSCSMPL_CR |= DTS_TSCSMPL_CR_SMPL_CTR_HOLD;
1305       }
1306       else
1307       {
1308         hdts->Instance->TSCSMPL_CR &= ~(DTS_TSCSMPL_CR_SMPL_CTR_HOLD);
1309       }
1310     }
1311     else
1312     {
1313       /* function call in wrong state */
1314       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
1315       status = HAL_ERROR;
1316     }
1317   }
1318 
1319   /* Return function status */
1320   return status;
1321 }
1322 
1323 /**
1324   * @brief  Get sample counter value.
1325   * @param  hdts DTS handle.
1326   * @param  pNumber Pointer to sample counter value.
1327   * @retval HAL status.
1328   */
HAL_DTS_GetSampleCounterValue(DTS_HandleTypeDef * hdts,uint32_t * pNumber)1329 HAL_StatusTypeDef HAL_DTS_GetSampleCounterValue(DTS_HandleTypeDef *hdts,
1330                                                 uint32_t          *pNumber)
1331 {
1332   HAL_StatusTypeDef status = HAL_OK;
1333 
1334   /* Check parameters */
1335   if (hdts == NULL)
1336   {
1337     status = HAL_ERROR;
1338   }
1339   else if (pNumber == NULL)
1340   {
1341     hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_PARAM;
1342     status = HAL_ERROR;
1343   }
1344   else
1345   {
1346     /* Check parameter */
1347     assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
1348 
1349     /* Check current state */
1350     if (hdts->State != HAL_DTS_STATE_RESET)
1351     {
1352       /* Get sample counter value */
1353       *pNumber = hdts->Instance->TSCSMPLCNTR;
1354     }
1355     else
1356     {
1357       /* function call in wrong state */
1358       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
1359       status = HAL_ERROR;
1360     }
1361   }
1362 
1363   /* Return function status */
1364   return status;
1365 }
1366 
1367 /**
1368   * @brief  Configure sample discard.
1369   * @param  hdts DTS handle.
1370   * @param  Status Enable or disable.
1371   * @retval HAL status.
1372   */
HAL_DTS_ConfigSampleDiscard(DTS_HandleTypeDef * hdts,FunctionalState Status)1373 HAL_StatusTypeDef HAL_DTS_ConfigSampleDiscard(DTS_HandleTypeDef *hdts,
1374                                               FunctionalState    Status)
1375 {
1376   HAL_StatusTypeDef retVal = HAL_OK;
1377 
1378   /* Check parameter */
1379   if (hdts == NULL)
1380   {
1381     retVal = HAL_ERROR;
1382   }
1383   else
1384   {
1385     /* Check current state */
1386     if (hdts->State != HAL_DTS_STATE_RESET)
1387     {
1388       /* Configure sample discard */
1389       if (Status == ENABLE)
1390       {
1391         hdts->Instance->TSCSMPL_CR |= DTS_TSCSMPL_CR_SMPL_DISCARD;
1392       }
1393       else
1394       {
1395         hdts->Instance->TSCSMPL_CR &= ~(DTS_TSCSMPL_CR_SMPL_DISCARD);
1396       }
1397     }
1398     else
1399     {
1400       /* function call in wrong state */
1401       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
1402       retVal = HAL_ERROR;
1403     }
1404   }
1405 
1406   /* Return function status */
1407   return retVal;
1408 }
1409 
1410 /**
1411   * @}
1412   */
1413 
1414 /** @defgroup DTS_Exported_Functions_Group3  Alarm functions
1415   * @brief    Alarm functions
1416   *
1417 @verbatim
1418   ==============================================================================
1419                        ##### Alarms functions #####
1420   ==============================================================================
1421     [..]  This section provides functions allowing to :
1422       (+) Configure alarms.
1423       (+) Manage alarm callbacks.
1424 @endverbatim
1425   * @{
1426   */
1427 
1428 /**
1429   * @brief  Configure alarmA for one sensor.
1430   * @param  hdts DTS handle.
1431   * @param  Sensor Sensor.
1432   *         This parameter can be one of the following values:
1433   *           @arg @ref DTS_SENSOR_0 Sensor 0.
1434   *           @arg @ref DTS_SENSOR_1 Sensor 1.
1435   * @param  pAlarmParams Pointer to alarm parameters.
1436   * @retval HAL status.
1437   */
HAL_DTS_ConfigAlarmA(DTS_HandleTypeDef * hdts,HAL_DTS_Sensor Sensor,const DTS_AlarmConfigTypeDef * pAlarmParams)1438 HAL_StatusTypeDef HAL_DTS_ConfigAlarmA(DTS_HandleTypeDef            *hdts,
1439                                        HAL_DTS_Sensor                Sensor,
1440                                        const DTS_AlarmConfigTypeDef *pAlarmParams)
1441 {
1442   HAL_StatusTypeDef status = HAL_OK;
1443 
1444   /* Check parameters */
1445   if (hdts == NULL)
1446   {
1447     status = HAL_ERROR;
1448   }
1449   else if (pAlarmParams == NULL)
1450   {
1451     hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_PARAM;
1452     status = HAL_ERROR;
1453   }
1454   else
1455   {
1456     /* Check parameter */
1457     assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
1458     assert_param(IS_FUNCTIONAL_STATE(pAlarmParams->Enable));
1459 
1460     /* Check current state */
1461     if (hdts->State == HAL_DTS_STATE_READY)
1462     {
1463       uint32_t alarmThreshold;
1464       uint32_t alarmHysteresis;
1465       DTS_SensorTypeDef *psensor;
1466 
1467       psensor = (Sensor == DTS_SENSOR_0) ? DTS_Sensor0 : DTS_Sensor1;
1468 
1469       /* Configure alarm A */
1470       if (pAlarmParams->Enable == ENABLE)
1471       {
1472         /* Check threshold and hysteresis parameters */
1473         assert_param(IS_DTS_ALARM_PARAM(pAlarmParams->Threshold));
1474         assert_param(IS_DTS_ALARM_PARAM(pAlarmParams->Hysteresis));
1475 
1476         /* Convert threshold and hysteresis from celsius degree  and store it on DTS register */
1477         alarmThreshold = DTS_ConvertFromCelsiusDegree(pAlarmParams->Threshold);
1478         alarmHysteresis = DTS_ConvertFromCelsiusDegree(pAlarmParams->Hysteresis);
1479         psensor->TSALARMA_CFGR = (alarmThreshold << DTS_TSALARMA_CFGR_ALARMA_THRESH_Pos) |
1480                                  (alarmHysteresis << DTS_TSALARMA_CFGR_HYSTA_THRESH_Pos);
1481 
1482         /* Enable alarm A interrupt */
1483         psensor->TS_IER |= DTS_TS_IER_IRQ_EN_ALARMA;
1484       }
1485       else
1486       {
1487         /* Disable alarm A interrupt */
1488         psensor->TS_IER &= ~(DTS_TS_IER_IRQ_EN_ALARMA);
1489 
1490         /* Clear potential alarm A pending flag */
1491         psensor->TS_ICR |= DTS_TS_ICR_IRQ_CLEAR_ALARMA;
1492 
1493         /* Disable alarm A */
1494         psensor->TSALARMA_CFGR = 0U;
1495       }
1496     }
1497     else
1498     {
1499       /* function call in wrong state */
1500       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
1501       status = HAL_ERROR;
1502     }
1503   }
1504 
1505   /* Return function status */
1506   return status;
1507 }
1508 
1509 /**
1510   * @brief  Configure alarmB for one sensor.
1511   * @param  hdts DTS handle.
1512   * @param  Sensor Sensor.
1513   *         This parameter can be one of the following values:
1514   *           @arg @ref DTS_SENSOR_0 Sensor 0.
1515   *           @arg @ref DTS_SENSOR_1 Sensor 1.
1516   * @param  pAlarmParams Pointer to alarm parameters.
1517   * @retval HAL status.
1518   */
HAL_DTS_ConfigAlarmB(DTS_HandleTypeDef * hdts,HAL_DTS_Sensor Sensor,const DTS_AlarmConfigTypeDef * pAlarmParams)1519 HAL_StatusTypeDef HAL_DTS_ConfigAlarmB(DTS_HandleTypeDef            *hdts,
1520                                        HAL_DTS_Sensor                Sensor,
1521                                        const DTS_AlarmConfigTypeDef *pAlarmParams)
1522 {
1523   HAL_StatusTypeDef status = HAL_OK;
1524 
1525   /* Check parameters */
1526   if (hdts == NULL)
1527   {
1528     status = HAL_ERROR;
1529   }
1530   else if (pAlarmParams == NULL)
1531   {
1532     hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_PARAM;
1533     status = HAL_ERROR;
1534   }
1535   else
1536   {
1537     /* Check parameter */
1538     assert_param(IS_DTS_ALL_INSTANCE(hdts->Instance));
1539     assert_param(IS_FUNCTIONAL_STATE(pAlarmParams->Enable));
1540 
1541     /* Check current state */
1542     if (hdts->State == HAL_DTS_STATE_READY)
1543     {
1544       uint32_t alarmThreshold;
1545       uint32_t alarmHysteresis;
1546       DTS_SensorTypeDef *psensor;
1547 
1548       psensor = (Sensor == DTS_SENSOR_0) ? DTS_Sensor0 : DTS_Sensor1;
1549 
1550       /* Configure alarm B */
1551       if (pAlarmParams->Enable == ENABLE)
1552       {
1553         /* Check threshold and hysteresis parameters */
1554         assert_param(IS_DTS_ALARM_PARAM(pAlarmParams->Threshold));
1555         assert_param(IS_DTS_ALARM_PARAM(pAlarmParams->Hysteresis));
1556 
1557         /* Convert threshold and hysteresis from celsius degree and store it on DTS register */
1558         alarmThreshold = DTS_ConvertFromCelsiusDegree(pAlarmParams->Threshold);
1559         alarmHysteresis = DTS_ConvertFromCelsiusDegree(pAlarmParams->Hysteresis);
1560         psensor->TSALARMB_CFGR = (alarmThreshold << DTS_TSALARMB_CFGR_ALARMB_THRESH_Pos) |
1561                                  (alarmHysteresis << DTS_TSALARMB_CFGR_HYSTB_THRESH_Pos);
1562 
1563         /* Enable alarm B interrupt */
1564         psensor->TS_IER |= DTS_TS_IER_IRQ_EN_ALARMB;
1565       }
1566       else
1567       {
1568         /* Disable alarm B interrupt */
1569         psensor->TS_IER &= ~(DTS_TS_IER_IRQ_EN_ALARMB);
1570 
1571         /* Clear potential alarm B pending flag */
1572         psensor->TS_ICR |= DTS_TS_ICR_IRQ_CLEAR_ALARMB;
1573 
1574         /* Disable alarm A */
1575         psensor->TSALARMB_CFGR = 0U;
1576       }
1577     }
1578     else
1579     {
1580       /* function call in wrong state */
1581       hdts->ErrorCode |= HAL_DTS_ERROR_INVALID_STATE;
1582       status = HAL_ERROR;
1583     }
1584   }
1585 
1586   /* Return function status */
1587   return status;
1588 }
1589 
1590 /**
1591   * @brief  DTS alarm A callback.
1592   * @param  hdts DTS handle.
1593   * @param  Sensor Sensor.
1594   *         This parameter can be one of the following values:
1595   *           @arg @ref DTS_SENSOR_0 Sensor 0.
1596   *           @arg @ref DTS_SENSOR_1 Sensor 1.
1597   * @retval None.
1598   */
HAL_DTS_AlarmACallback(DTS_HandleTypeDef * hdts,HAL_DTS_Sensor Sensor)1599 __weak void HAL_DTS_AlarmACallback(DTS_HandleTypeDef *hdts,
1600                                    HAL_DTS_Sensor     Sensor)
1601 {
1602   /* Prevent unused argument(s) compilation warning */
1603   UNUSED(hdts);
1604   UNUSED(Sensor);
1605 
1606   /* NOTE : This function should not be modified, when the function is needed,
1607             the HAL_DTS_AlarmACallback could be implemented in the user file */
1608 }
1609 
1610 /**
1611   * @brief  DTS alarm B callback.
1612   * @param  hdts DTS handle.
1613   * @param  Sensor Sensor.
1614   *         This parameter can be one of the following values:
1615   *           @arg @ref DTS_SENSOR_0 Sensor 0.
1616   *           @arg @ref DTS_SENSOR_1 Sensor 1.
1617   * @retval None.
1618   */
HAL_DTS_AlarmBCallback(DTS_HandleTypeDef * hdts,HAL_DTS_Sensor Sensor)1619 __weak void HAL_DTS_AlarmBCallback(DTS_HandleTypeDef *hdts,
1620                                    HAL_DTS_Sensor     Sensor)
1621 {
1622   /* Prevent unused argument(s) compilation warning */
1623   UNUSED(hdts);
1624   UNUSED(Sensor);
1625 
1626   /* NOTE : This function should not be modified, when the function is needed,
1627             the HAL_DTS_AlarmBCallback could be implemented in the user file */
1628 }
1629 
1630 /**
1631   * @}
1632   */
1633 
1634 /** @defgroup DTS_Exported_Functions_Group4  Generic functions
1635   * @brief    Generic functions
1636   *
1637 @verbatim
1638   ==============================================================================
1639                        ##### Generic functions #####
1640   ==============================================================================
1641     [..]  This section provides functions allowing to :
1642       (+) Handle DTS interrupt.
1643       (+) Inform user that error occurs.
1644       (+) Get the current DTS instance state.
1645 @endverbatim
1646   * @{
1647   */
1648 
1649 /**
1650   * @brief  Handle DTS interrupts.
1651   * @param  hdts DTS handle.
1652   * @retval None.
1653   */
HAL_DTS_IRQHandler(DTS_HandleTypeDef * hdts)1654 void HAL_DTS_IRQHandler(DTS_HandleTypeDef *hdts)
1655 {
1656   DTS_SensorTypeDef *psensor;
1657   HAL_DTS_Sensor Sensor;
1658   uint32_t tmp_reg1;
1659   uint32_t tmp_reg2;
1660   uint32_t interrupts;
1661 
1662   /* Check on which sensor interrupt occurs */
1663   if ((hdts->Instance->TS_ISR & DTS_TS_ISR_TS0_IRQ_STATUS) == DTS_TS_ISR_TS0_IRQ_STATUS)
1664   {
1665     psensor = DTS_Sensor0;
1666     Sensor = DTS_SENSOR_0;
1667   }
1668   else
1669   {
1670     psensor = DTS_Sensor1;
1671     Sensor = DTS_SENSOR_1;
1672   }
1673 
1674   /* Read current flags and interrupts on sensor */
1675   tmp_reg1 = psensor->TS_IER;
1676   tmp_reg2 = psensor->TS_ISR;
1677   interrupts = (tmp_reg1 & tmp_reg2);
1678 
1679   /* Check if sample done occurs */
1680   if ((interrupts & DTS_TS_ISR_IRQ_STATUS_DONE) == DTS_TS_ISR_IRQ_STATUS_DONE)
1681   {
1682     /* Clear corresponding flag */
1683     psensor->TS_ICR |= DTS_TS_ICR_IRQ_CLEAR_DONE;
1684 
1685     /* Call temperature callback */
1686 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1)
1687     hdts->TemperatureCallback(hdts, Sensor);
1688 #else /* USE_HAL_DTS_REGISTER_CALLBACKS */
1689     HAL_DTS_TemperatureCallback(hdts, Sensor);
1690 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
1691 
1692     /* Update state */
1693     if (hdts->SensorMode[Sensor] == DTS_SENSOR_MODE_SINGLE)
1694     {
1695       /* Disable sample done interrupt */
1696       psensor->TS_IER &= ~(DTS_TS_IER_IRQ_EN_DONE);
1697 
1698       if (hdts->State == HAL_DTS_STATE_RUNNING_BOTH)
1699       {
1700         hdts->State = (Sensor == DTS_SENSOR_0) ? HAL_DTS_STATE_RUNNING_1 : HAL_DTS_STATE_RUNNING_0;
1701       }
1702       else
1703       {
1704         hdts->State = HAL_DTS_STATE_READY;
1705       }
1706     }
1707   }
1708   /* Check if alarm A occurs */
1709   if ((interrupts & DTS_TS_ISR_IRQ_STATUS_ALARMA) == DTS_TS_ISR_IRQ_STATUS_ALARMA)
1710   {
1711     /* Clear corresponding flag */
1712     psensor->TS_ICR |= DTS_TS_ICR_IRQ_CLEAR_ALARMA;
1713 
1714     /* Call alarm A callback */
1715 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1)
1716     hdts->AlarmACallback(hdts, Sensor);
1717 #else /* USE_HAL_DTS_REGISTER_CALLBACKS */
1718     HAL_DTS_AlarmACallback(hdts, Sensor);
1719 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
1720   }
1721   /* Check if alarm B occurs */
1722   if ((interrupts & DTS_TS_ISR_IRQ_STATUS_ALARMB) == DTS_TS_ISR_IRQ_STATUS_ALARMB)
1723   {
1724     /* Clear corresponding flag */
1725     psensor->TS_ICR |= DTS_TS_ICR_IRQ_CLEAR_ALARMB;
1726 
1727     /* Call alarm B callback */
1728 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1)
1729     hdts->AlarmBCallback(hdts, Sensor);
1730 #else /* USE_HAL_DTS_REGISTER_CALLBACKS */
1731     HAL_DTS_AlarmBCallback(hdts, Sensor);
1732 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
1733   }
1734   /* Check if fault occurs */
1735   if ((interrupts & DTS_TS_ISR_IRQ_STATUS_FAULT) == DTS_TS_ISR_IRQ_STATUS_FAULT)
1736   {
1737     /* Clear corresponding flag */
1738     psensor->TS_ICR |= DTS_TS_ICR_IRQ_CLEAR_FAULT;
1739 
1740     /* Update error code */
1741     hdts->ErrorCode |= HAL_DTS_ERROR_FAULT;
1742 
1743     /* Call error callback */
1744 #if (USE_HAL_DTS_REGISTER_CALLBACKS == 1)
1745     hdts->ErrorCallback(hdts, Sensor);
1746 #else /* USE_HAL_DTS_REGISTER_CALLBACKS */
1747     HAL_DTS_ErrorCallback(hdts, Sensor);
1748 #endif /* USE_HAL_DTS_REGISTER_CALLBACKS */
1749   }
1750 }
1751 
1752 /**
1753   * @brief  DTS error callback.
1754   * @param  hdts DTS handle.
1755   * @param  Sensor Sensor.
1756   *         This parameter can be one of the following values:
1757   *           @arg @ref DTS_SENSOR_0 Sensor 0.
1758   *           @arg @ref DTS_SENSOR_1 Sensor 1.
1759   * @retval None.
1760   */
HAL_DTS_ErrorCallback(DTS_HandleTypeDef * hdts,HAL_DTS_Sensor Sensor)1761 __weak void HAL_DTS_ErrorCallback(DTS_HandleTypeDef *hdts,
1762                                   HAL_DTS_Sensor     Sensor)
1763 {
1764   /* Prevent unused argument(s) compilation warning */
1765   UNUSED(hdts);
1766   UNUSED(Sensor);
1767 
1768   /* NOTE : This function should not be modified, when the function is needed,
1769             the HAL_DTS_ErrorCallback could be implemented in the user file */
1770 }
1771 
1772 /**
1773   * @brief  Get current DTS handle state.
1774   * @param  hdts DTS handle.
1775   * @retval DTS state.
1776   */
HAL_DTS_GetState(const DTS_HandleTypeDef * hdts)1777 HAL_DTS_StateTypeDef HAL_DTS_GetState(const DTS_HandleTypeDef *hdts)
1778 {
1779   /* Return DTS state */
1780   return hdts->State;
1781 }
1782 
1783 /**
1784   * @brief  Get DTS error code.
1785   * @param  hdts DTS handle.
1786   * @retval DTS error code.
1787   */
HAL_DTS_GetError(const DTS_HandleTypeDef * hdts)1788 uint32_t HAL_DTS_GetError(const DTS_HandleTypeDef *hdts)
1789 {
1790   return hdts->ErrorCode;
1791 }
1792 
1793 /**
1794   * @}
1795   */
1796 
1797 /**
1798   * @}
1799   */
1800 
1801 /* Private functions -------------------------------------------------------------------------------------------------*/
1802 
1803 /** @addtogroup DTS_Private_Functions
1804   * @brief      Private functions
1805   * @{
1806   */
1807 
1808 /**
1809   * @brief  Program SDA register.
1810   * @param  hdts DTS handle.
1811   * @param  Sensor Sensor.
1812   *         This parameter can be one of the following values:
1813   *           @arg @ref DTS_SENSOR_0 Sensor 0.
1814   *           @arg @ref DTS_SENSOR_1 Sensor 1.
1815   * @param  Reg SDA register to program.
1816   * @param  Value Value to program on SDA register.
1817   * @retval HAL status.
1818   */
DTS_ProgramSdaRegister(DTS_HandleTypeDef * hdts,uint32_t Sensor,DTS_SdaRegisterTypeDef Reg,uint32_t Value)1819 static HAL_StatusTypeDef DTS_ProgramSdaRegister(DTS_HandleTypeDef *hdts, uint32_t Sensor,
1820                                                 DTS_SdaRegisterTypeDef Reg, uint32_t Value)
1821 {
1822   HAL_StatusTypeDef status = HAL_OK;
1823   uint32_t tickstart = HAL_GetTick();
1824 
1825   /* Check that serial data interface is not busy */
1826   while (((hdts->Instance->TSCSDIF_SR & DTS_TSCSDIF_SR_SDIF_BUSY) != 0U) && (status == HAL_OK))
1827   {
1828     if ((HAL_GetTick() - tickstart) > DTS_MAXIMUM_TIMEOUT)
1829     {
1830       status = HAL_TIMEOUT;
1831     }
1832   }
1833 
1834   if (status == HAL_OK)
1835   {
1836     if (Sensor == DTS_SENSOR_ALL)
1837     {
1838       hdts->Instance->TSCSDIF_CFGR = 0U;
1839     }
1840     else
1841     {
1842       /* Inhibit serial programming of other sensor */
1843       hdts->Instance->TSCSDIF_CFGR = (Sensor == (uint32_t) DTS_SENSOR_0) ? DTS_TSCSDIF_CFGR_SDIF_INHIBIT_1 :
1844                                      DTS_TSCSDIF_CFGR_SDIF_INHIBIT_0;
1845     }
1846 
1847     /* program value on SDA register using DTS_TSCSDIF_CR */
1848     hdts->Instance->TSCSDIF_CR = (DTS_TSCSDIF_CR_SDIF_PROG | DTS_TSCSDIF_CR_SDIF_WRN | (uint32_t) Reg | Value);
1849   }
1850 
1851   /* Return function status */
1852   return status;
1853 }
1854 
1855 /**
1856   * @brief  Convert DTS sample to celsius degree.
1857   * @param  sample DTS sample.
1858   * @retval Temperature in celsius degree.
1859   */
DTS_ConvertToCelsiusDegree(uint32_t sample)1860 static float_t DTS_ConvertToCelsiusDegree(uint32_t sample)
1861 {
1862   float_t value;
1863 
1864   /* The temperature computation is Temp (celsius degree) = G + (H x Eqbs) + (J x Fclk_ts) */
1865   /* where Eqbs is equal to (sample/Cal5) - 0.5 */
1866   /* and Fclk_ts is 4MHz */
1867 
1868   /* Computation of Eqbs */
1869   value = ((((float_t) sample) / DTS_CAL5_PARAM) - 0.5f);
1870 
1871   /* Computation of temperature */
1872   value = DTS_G_PARAM + (DTS_H_PARAM * value) + (DTS_J_PARAM * 4.0f);
1873 
1874   /* Return temperature value */
1875   return value;
1876 }
1877 
1878 /**
1879   * @brief  Convert DTS sample from celsius degree.
1880   * @param  temperature Temperature in celsius degree.
1881   * @retval DTS sample.
1882   */
DTS_ConvertFromCelsiusDegree(float_t temperature)1883 static uint32_t DTS_ConvertFromCelsiusDegree(float_t temperature)
1884 {
1885   float_t value;
1886 
1887   /* The temperature computation is Temp (celsius degree) = G + (H x Eqbs) + (J x Fclk_ts) */
1888   /* where Eqbs = (sample/Cal5) - 0.5 */
1889   /* and Fclk_ts is 4MHz. */
1890   /* So we have sample = (Eqbs + 0.5) x Cal5 */
1891   /* and Eqbs = (Temp - G - (J x Fclk_ts)) / H. */
1892 
1893   /* Computation of Eqbs */
1894   value = (temperature - DTS_G_PARAM - (DTS_J_PARAM * 4.0f)) / DTS_H_PARAM;
1895 
1896   /* Computation of sample */
1897   value = (value + 0.5f) * DTS_CAL5_PARAM;
1898 
1899   /* Return sample value */
1900   return (uint32_t) value;
1901 }
1902 
1903 /**
1904   * @}
1905   */
1906 
1907 /**
1908   * @}
1909   */
1910 
1911 #endif /* HAL_DTS_MODULE_ENABLED */
1912 
1913 /**
1914   * @}
1915   */
1916