1 /*
2  * Copyright 2022-2024, NXP
3  *
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef FSL_TMU_H__
9 #define FSL_TMU_H__
10 
11 #include "fsl_common.h"
12 
13 /*!
14  * @addtogroup tmu
15  * @{
16  */
17 
18 /*******************************************************************************
19  * Definitions
20  ******************************************************************************/
21 /*! @brief TMU driver version. */
22 #define FSL_TMU_DRIVER_VERSION (MAKE_VERSION(2, 1, 0)) /*!< Version 2.1.0. */
23 
24 /*!
25  * @brief TMU monitor site.
26  */
27 enum _tmu_monitor_site
28 {
29     kTMU_MonitorSite0 = 1U << 0U, /*!< TMU monitoring site 0. */
30 };
31 
32 /*!
33  * @brief TMU interrupt enable.
34  */
35 enum _tmu_interrupt_enable
36 {
37     kTMU_ImmediateTemperatureInterruptEnable =
38         TMU_TIER_IHTTIE_MASK, /*!< Immediate temperature threshold exceeded interrupt enable. */
39     kTMU_AverageTemperatureInterruptEnable =
40         TMU_TIER_AHTTIE_MASK, /*!< Average temperature threshold exceeded interrupt enable. */
41     kTMU_AverageTemperatureCriticalInterruptEnable =
42         TMU_TIER_AHTCTIE_MASK, /*!< Average temperature critical threshold exceeded interrupt enable. */
43     kTMU_RisingTemperatureCriticalInterruptEnable =
44         TMU_TIER_RTRCTIE_MASK, /*!< Rising temperature critical threshold exceeded interrupt enable. */
45     kTMU_FallingTemperatureCriticalInterruptEnable =
46         TMU_TIER_FTRCTIE_MASK, /*!< Falling temperature critical threshold exceeded interrupt enable. */
47 };
48 
49 /*!
50  * @brief TMU interrupt status flags.
51  */
52 enum _tmu_interrupt_status_flags
53 {
54     kTMU_ImmediateTemperatureStatusFlags = TMU_TIDR_IHTT_MASK, /*!< Immediate temperature threshold exceeded(IHTT).  */
55     kTMU_AverageTemperatureStatusFlags   = TMU_TIDR_AHTT_MASK, /*!< Average temperature threshold exceeded(AHTT). */
56     kTMU_AverageTemperatureCriticalStatusFlags =
57         TMU_TIDR_AHTCT_MASK, /*!< Average temperature critical threshold exceeded.(AHTCT) */
58     kTMU_RisingTemperatureCriticalStatusFlags =
59         TMU_TIDR_RTRCT_MASK, /*!< Rising temperature critical threshold exceeded.(RTRCT) */
60     kTMU_FallingTemperatureCriticalStatusFlags =
61         TMU_TIDR_FTRCT_MASK, /*!< Falling temperature critical threshold exceeded.(FTRCT) */
62 };
63 
64 /*!
65  * @brief TMU status flags.
66  */
67 enum _tmu_status_flags
68 {
69     kTMU_IntervalExceededStatusFlags = TMU_TSR_MIE_MASK, /*!< Monitoring interval exceeded. The time required to perform
70                                                             measurement of all monitored sites has
71                                                               exceeded the monitoring interval as defined by TMTMIR. */
72     kTMU_OutOfLowRangeStatusFlags = TMU_TSR_ORL_MASK,    /*!< Out-of-range low temperature measurement detected. A
73                                                             temperature sensor detected a temperature
74                                                               reading below the lowest measurable temperature of 0 °C. */
75     kTMU_OutOfHighRangeStatusFlags =
76         TMU_TSR_ORH_MASK, /*!< Out-of-range high temperature measurement detected. A temperature sensor detected a
77                              temperature
78                                reading above the highest measurable temperature of 160 °C. */
79 };
80 
81 /*!
82  * @brief configuration for TMU thresold.
83  */
84 typedef struct _tmu_thresold_config
85 {
86     bool immediateThresoldEnable;       /*!< Enable high temperature immediate threshold. */
87     bool averageThresoldEnable;         /*!< Enable high temperature average threshold. */
88     bool averageCriticalThresoldEnable; /*!< Enable high temperature average critical threshold. */
89     bool risingCriticalThresoldEnable; /*!< Enable rising temperature rate critical threshold. */
90     bool fallingCriticalThresoldEnable; /*!< Enable rising temperature rate critical threshold. */
91     uint8_t immediateThresoldValue; /*!< Range:0U-160U. Valid when corresponding thresold is enabled. High temperature
92                                        immediate threshold value.
93                                                Determines the current upper temperature threshold, for anyenabled
94                                        monitored site. */
95     uint8_t averageThresoldValue;   /*!< Range:0U-160U. Valid when corresponding thresold is enabled. High temperature
96                                        average threshold value.
97                                                Determines the average upper temperature threshold, for any enabled
98                                        monitored site. */
99     uint8_t averageCriticalThresoldValue; /*!< Range:0U-160U. Valid when corresponding thresold is enabled. High
100                                              temperature average critical threshold value.
101                                                      Determines the average upper critical temperature threshold, for
102                                              any enabled monitored site. */
103     uint8_t risingfallingCriticalThresoldValue; /*!< Range:0U-160U. Valid when corresponding thresold is enabled. Rising
104                                              temperature rate critical threshold value.
105                                                      Determines the rising upper critical temperature threshold, for
106                                              any enabled monitored site. */
107 } tmu_thresold_config_t;
108 
109 /*!
110  * @brief TMU interrupt status.
111  */
112 typedef struct _tmu_interrupt_status
113 {
114     uint32_t interruptDetectMask; /*!< The mask of interrupt status flags. Refer to "_tmu_interrupt_status_flags"
115                                      enumeration. */
116     uint16_t immediateInterruptsSiteMask; /*!< The mask of the temperature sensor site associated with a detected IHTT
117                                              event. Please refer to "_tmu_monitor_site" enumeration. */
118     uint16_t averageInterruptsSiteMask;   /*!< The mask of the temperature sensor site associated with a detected AHTT
119                                              event.   Please refer to "_tmu_monitor_site" enumeration. */
120     uint16_t averageCriticalInterruptsSiteMask; /*!< The mask of the temperature sensor site associated with a detected
121                                                    AHTCT event.
122                                                      Please refer to "_tmu_monitor_site" enumeration. */
123     uint16_t risingCriticalInterruptsSiteMask; /*!< The mask of the temperature sensor site associated with a detected
124                                                    RTRCT event.
125                                                      Please refer to "_tmu_monitor_site" enumeration. */
126     uint16_t fallingCriticalInterruptsSiteMask; /*!< The mask of the temperature sensor site associated with a detected
127                                                    FTRCT event.
128                                                      Please refer to "_tmu_monitor_site" enumeration. */
129 } tmu_interrupt_status_t;
130 /*!
131  * @brief Average low pass filter setting.
132  */
133 typedef enum _tmu_average_low_pass_filter
134 {
135     kTMU_AverageLowPassFilter1_0   = 0U, /*!< Average low pass filter = 1. */
136     kTMU_AverageLowPassFilter0_5   = 1U, /*!< Average low pass filter = 0.5. */
137     kTMU_AverageLowPassFilter0_25  = 2U, /*!< Average low pass filter = 0.25. */
138     kTMU_AverageLowPassFilter0_125 = 3U, /*!< Average low pass filter = 0.125. */
139 } tmu_average_low_pass_filter_t;
140 
141 /*!
142  * @brief Configuration for TMU module.
143  */
144 typedef struct _tmu_config
145 {
146     uint8_t monitorInterval; /*!< Temperature monitoring interval in seconds. Please refer to specific table in RM.  */
147     uint16_t monitorSiteSelection; /*!< By setting the select bit for a temperature sensor site, it is enabled and
148                                       included in all monitoring functions.
149                                          If no site is selected, site 0 is monitored by default. Refer to
150                                       "_tmu_monitor_site" enumeration. Please look up
151                                          relevant table in reference manual. */
152     tmu_average_low_pass_filter_t
153         averageLPF; /*!< The average temperature is calculated as: ALPF x Current_Temp + (1 - ALPF) x Average_Temp.
154                          For proper operation, this field should only change when monitoring is disabled. */
155 } tmu_config_t;
156 
157 #if defined(__cplusplus)
158 extern "C" {
159 #endif
160 
161 /*******************************************************************************
162  * API
163  ******************************************************************************/
164 /*!
165  * @brief Enable the access to TMU registers and Initialize TMU module.
166  *
167  * @param base TMU peripheral base address.
168  * @param config Pointer to configuration structure. Refer to "tmu_config_t" structure.
169  */
170 void TMU_Init(TMU_Type *base, const tmu_config_t *config);
171 
172 /*!
173  * @brief De-initialize TMU module and Disable the access to DCDC registers.
174  *
175  * @param base TMU peripheral base address.
176  */
177 void TMU_Deinit(TMU_Type *base);
178 
179 /*!
180  * @brief Gets the default configuration for TMU.
181  *
182  * This function initializes the user configuration structure to default value. The default value are:
183  *
184  * Example:
185    @code
186    config->monitorInterval = 0U;
187    config->monitorSiteSelection = 0U;
188    config->averageLPF = kTMU_AverageLowPassFilter1_0;
189    @endcode
190  *
191  * @param config Pointer to TMU configuration structure.
192  */
193 void TMU_GetDefaultConfig(tmu_config_t *config);
194 
195 /*!
196  * @brief Enable/Disable the TMU module.
197  *
198  * @param base TMU peripheral base address.
199  * @param enable Switcher to enable/disable TMU.
200  */
TMU_Enable(TMU_Type * base,bool enable)201 static inline void TMU_Enable(TMU_Type *base, bool enable)
202 {
203     if (enable)
204     {
205         base->TMR = ((base->TMR & ~TMU_TMR_MODE_MASK) | TMU_TMR_MODE(2));
206     }
207     else
208     {
209         base->TMR &= ~TMU_TMR_MODE_MASK;
210     }
211 }
212 
213 /*!
214  * @brief Enable the TMU interrupts.
215  *
216  * @param base TMU peripheral base address.
217  * @param mask The interrupt mask. Refer to "_tmu_interrupt_enable" enumeration.
218  */
TMU_EnableInterrupts(TMU_Type * base,uint32_t mask)219 static inline void TMU_EnableInterrupts(TMU_Type *base, uint32_t mask)
220 {
221     base->TIER |= mask;
222 }
223 
224 /*!
225  * @brief Disable the TMU interrupts.
226  *
227  * @param base TMU peripheral base address.
228  * @param mask The interrupt mask. Refer to "_tmu_interrupt_enable" enumeration.
229  */
TMU_DisableInterrupts(TMU_Type * base,uint32_t mask)230 static inline void TMU_DisableInterrupts(TMU_Type *base, uint32_t mask)
231 {
232     base->TIER &= ~mask;
233 }
234 
235 /*!
236  * @brief Get interrupt status flags.
237  *
238  * @param base TMU peripheral base address.
239  * @param status The pointer to interrupt status structure. Record the current interrupt status.
240  *        Please refer to "tmu_interrupt_status_t" structure.
241  */
242 void TMU_GetInterruptStatusFlags(TMU_Type *base, tmu_interrupt_status_t *status);
243 
244 /*!
245  * @brief Clear interrupt status flags and corresponding interrupt critical site capture register.
246  *
247  * @param base TMU peripheral base address.
248  * @param mask The mask of interrupt status flags. Refer to "_tmu_interrupt_status_flags" enumeration.
249  */
250 void TMU_ClearInterruptStatusFlags(TMU_Type *base, uint32_t mask);
251 
252 /*!
253  * @brief Get TMU status flags.
254  *
255  * @param base TMU peripheral base address.
256  *
257  * @return The mask of status flags. Refer to "_tmu_status_flags" enumeration.
258  */
TMU_GetStatusFlags(TMU_Type * base)259 static inline uint32_t TMU_GetStatusFlags(TMU_Type *base)
260 {
261     return base->TSR;
262 }
263 
264 /*!
265  * @brief Get the highest temperature reached for any enabled monitored site within the temperature
266  *        sensor range.
267  *
268  * @param base TMU peripheral base address.
269  * @param temperature Highest temperature recorded in degrees Celsius by any enabled monitored site.
270  *
271  * @return Execution status.
272  * @retval kStatus_Success Temperature reading is valid.
273  * @retval kStatus_Fail    Temperature reading is not valid due to no measured temperature within the
274  *                         sensor range of 0-160 °C for an enabled monitored site.
275  */
276 status_t TMU_GetHighestTemperature(TMU_Type *base, uint32_t *temperature);
277 
278 /*!
279  * @brief Get the lowest temperature reached for any enabled monitored site within the temperature
280  *        sensor range.
281  *
282  * @param base TMU peripheral base address.
283  * @param temperature Lowest temperature recorded in degrees Celsius by any enabled monitored site.
284  *
285  * @return Execution status.
286  * @retval kStatus_Success Temperature reading is valid.
287  * @retval kStatus_Fail    Temperature reading is not valid due to no measured temperature within the
288  *                         sensor range of 0-160 °C for an enabled monitored site.
289  */
290 status_t TMU_GetLowestTemperature(TMU_Type *base, uint32_t *temperature);
291 
292 /*!
293  * @brief Get the last immediate temperature at site n. The site must be part of the list of enabled
294  *        monitored sites as defined by monitorSiteSelection in "tmu_config_t" structure.
295  *
296  * @param base TMU peripheral base address.
297  * @param temperature Last immediate temperature reading at site 0.
298  *
299  * @return Execution status.
300  * @retval kStatus_Success Temperature reading is valid.
301  * @retval kStatus_Fail    Temperature reading is not valid because temperature out of sensor range or
302  *                         first measurement still pending.
303  */
304 status_t TMU_GetImmediateTemperature(TMU_Type *base, float *temperature);
305 
306 /*!
307  * @brief Get the last average temperature at site n. The site must be part of the list of enabled
308  *        monitored sites as defined by monitorSiteSelection in "tmu_config_t" structure.
309  *
310  * @param base TMU peripheral base address.
311  * @param temperature Last average temperature reading at site 0.
312  *
313  * @return Execution status.
314  * @retval kStatus_Success Temperature reading is valid.
315  * @retval kStatus_Fail    Temperature reading is not valid because temperature out of sensor range or
316  *                         first measurement still pending.
317  */
318 status_t TMU_GetAverageTemperature(TMU_Type *base, uint32_t *temperature);
319 
320 /*!
321  * @brief Configure the high temperature thresold value and enable/disable relevant thresold.
322  *
323  * @param base TMU peripheral base address.
324  * @param config Pointer to configuration structure. Refer to "tmu_thresold_config_t" structure.
325  */
326 void TMU_SetHighTemperatureThresold(TMU_Type *base, const tmu_thresold_config_t *config);
327 
328 #if defined(__cplusplus)
329 }
330 #endif
331 
332 /*! @} */
333 
334 #endif /* FSL_TMU_H__ */
335