1 /*
2  * Copyright 2020 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef __FSL_TMU_H__
10 #define __FSL_TMU_H__
11 
12 #include "fsl_common.h"
13 
14 /*!
15  * @addtogroup tmu
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 /*! @brief TMU driver version. */
23 #define FSL_TMU_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0. */
24 
25 /*!
26  * @brief TMU interrupt enable, _tmu_interrupt_enable.
27  */
28 enum
29 {
30     kTMU_ImmediateTemperature0InterruptEnable =
31         TMU_TIER_ITTEIE0_MASK, /*!< Immediate temperature threshold exceeded interrupt enable of probe0. */
32     kTMU_AverageTemperature0InterruptEnable =
33         TMU_TIER_ATTEIE0_MASK, /*!< Average temperature threshold exceeded interrupt enable of probe0. */
34     kTMU_AverageTemperature0CriticalInterruptEnable =
35         TMU_TIER_ATCTEIE0_MASK, /*!< Average temperature critical threshold exceeded interrupt enable of probe0. */
36     kTMU_ImmediateTemperature1Interrupt1Enable =
37         TMU_TIER_ITTEIE1_MASK, /*!< Immediate temperature threshold exceeded interrupt enable of probe1. */
38     kTMU_AverageTemperature1Interrupt1Enable =
39         TMU_TIER_ATTEIE1_MASK, /*!< Average temperature threshold exceeded interrupt enable of probe1. */
40     kTMU_AverageTemperature1CriticalInterrupt1Enable =
41         TMU_TIER_ATCTEIE1_MASK, /*!< Average temperature critical threshold exceeded interrupt enable of probe1. */
42 };
43 
44 /*!
45  * @brief TMU interrupt enable, _tmu_interrupt_status_flags.
46  */
47 enum
48 {
49     kTMU_ImmediateTemperature0InterruptStausFlags =
50         TMU_TIDR_ITTE0_MASK, /*!< Immediate temperature threshold exceeded interrupt status of probe0. */
51     kTMU_AverageTemperature0InterruptStausFlags =
52         TMU_TIDR_ATTE0_MASK, /*!< Average temperature threshold exceeded interrupt status of probe0. */
53     kTMU_AverageTemperature0CriticalInterruptStausFlags =
54         TMU_TIDR_ATCTE0_MASK, /*!< Average temperature critical threshold exceeded interrupt status of probe0. */
55     kTMU_ImmediateTemperature1Interrupt1StausFlags =
56         TMU_TIDR_ITTE1_MASK, /*!< Immediate temperature threshold exceeded interrupt status of probe1. */
57     kTMU_AverageTemperature1Interrupt1StausFlags =
58         TMU_TIDR_ATTE1_MASK, /*!< Average temperature threshold exceeded interrupt status of probe1. */
59     kTMU_AverageTemperature1CriticalInterrupt1StausFlags =
60         TMU_TIDR_ATCTE1_MASK, /*!< Average temperature critical threshold exceeded interrupt status of probe1. */
61 };
62 
63 /*!
64  * @brief configuration for TMU threshold.
65  */
66 typedef struct _tmu_threshold_config
67 {
68     bool immediateThresholdEnable;       /*!< Enable high temperature immediate threshold. */
69     bool AverageThresholdEnable;         /*!< Enable high temperature average threshold. */
70     bool AverageCriticalThresholdEnable; /*!< Enable high temperature average critical threshold. */
71     uint8_t immediateThresholdValueOfMainProbe; /*!< Range:-40~125. Valid when corresponding threshold is enabled. High temperature
72                                        immediate threshold value of main probe.
73                                           Besides, bit-8 is sign bit: 1 means nagetive and 0 means positive. */
74     uint8_t averageThresholdValueOfMainProbe;   /*!< Range:-40~125. Valid when corresponding threshold is enabled. High temperature
75                                        average threshold value of main probe.
76                                           Besides, bit-8 is sign bit: 1 means nagetive and 0 means positive. */
77     uint8_t averageCriticalThresholdValueOfMainProbe; /*!< Range:-40~125. Valid when corresponding threshold is enabled. High
78                                              temperature average critical threshold value of main probe.
79                                                 Besides, bit-8 is sign bit: 1 means nagetive and 0 means positive. */
80     uint8_t immediateThresholdValueOfRemoteProbe; /*!< Range:-40~125. Valid when corresponding threshold is enabled. High temperature
81                                        immediate threshold value of remote probe.
82                                           Besides, bit-8 is sign bit: 1 means nagetive and 0 means positive. */
83     uint8_t averageThresholdValueOfRemoteProbe;   /*!< Range:-40~125. Valid when corresponding threshold is enabled. High temperature
84                                        average threshold value of remote probe.
85                                           Besides, bit-8 is sign bit: 1 means nagetive and 0 means positive. */
86     uint8_t averageCriticalThresholdValueOfRemoteProbe; /*!< Range:-40~125. Valid when corresponding threshold is enabled. High
87                                              temperature average critical threshold value of remote probe.
88                                                 Besides, bit-8 is sign bit: 1 means nagetive and 0 means positive. */
89 } tmu_threshold_config_t;
90 
91 /*!
92  * @brief Probe selection.
93  */
94 typedef enum _tmu_probe_select
95 {
96     kTMU_ProbeSelectMainProbe   = 1U << 0U, /*!< Select the main probe only. */
97     kTMU_ProbeSelectRemoteProbe = 1U << 1U, /*!< Select the remote probe(near A53) only. */
98     kTMU_ProbeSelectBothProbes  = ( 1U << 0U) |  (1U << 1U), /*!< Select both 2 probes. */
99 } tmu_probe_select_t;
100 
101 /*!
102  * @brief Average low pass filter setting.
103  */
104 typedef enum _tmu_average_low_pass_filter
105 {
106     kTMU_AverageLowPassFilter1_0   = 0U, /*!< Average low pass filter = 1. */
107     kTMU_AverageLowPassFilter0_5   = 1U, /*!< Average low pass filter = 0.5. */
108     kTMU_AverageLowPassFilter0_25  = 2U, /*!< Average low pass filter = 0.25. */
109     kTMU_AverageLowPassFilter0_125 = 3U, /*!< Average low pass filter = 0.125. */
110 } tmu_average_low_pass_filter_t;
111 
112 /*!
113  * @brief Configuration for TMU module.
114  */
115 typedef struct _tmu_config
116 {
117     tmu_probe_select_t probeSelect; /*!< The temperature monitor probe select.*/
118     tmu_average_low_pass_filter_t averageLPF; /*!< The average temperature is calculated as:
119                                                    ALPF x Current_Temp + (1 - ALPF) x Average_Temp.
120                                                    For proper operation, this field should only change when monitoring is disabled. */
121     tmu_threshold_config_t thresholdConfig; /*!< The high temperature threshold configuration. */
122 } tmu_config_t;
123 
124 #if defined(__cplusplus)
125 extern "C" {
126 #endif
127 
128 /*******************************************************************************
129  * API
130  ******************************************************************************/
131 /*!
132  * @brief Enable the access to TMU registers and Initialize TMU module.
133  *
134  * @param base TMU peripheral base address.
135  * @param config Pointer to configuration structure. Refer to "tmu_config_t" structure.
136  */
137 void TMU_Init(TMU_Type *base, const tmu_config_t *config);
138 
139 /*!
140  * @brief De-initialize TMU module and Disable the access to DCDC registers.
141  *
142  * @param base TMU peripheral base address.
143  */
144 void TMU_Deinit(TMU_Type *base);
145 
146 /*!
147  * @brief Enable/disable TMU module.
148  *
149  * @param base TMU peripheral base address.
150  * @param enable enable or disable TMU.
151  */
152 void TMU_Enable(TMU_Type *base, bool enable);
153 
154 /*!
155  * @brief Gets the default configuration for TMU.
156  *
157  * This function initializes the user configuration structure to default value. The default value are:
158  *
159  * Example:
160    @code
161    config.averageLPF                                     = kTMU_AverageLowPassFilter0_5;
162    config.probeSelect                                    = kTMU_ProbeSelectMainProbe;
163    config.thresholdConfig.immediateThresholdEnable       = false;
164    config.thresholdConfig.immediateThresholdValue        = DEMO_TMU_IMMEDIATE_THRESOLD;
165    config.thresholdConfig.AverageThresholdEnable         = true;
166    config.thresholdConfig.averageThresholdValue          = DEMO_TMU_AVERAGE_THRESOLD;
167    config.thresholdConfig.AverageCriticalThresholdEnable = false;
168    config.thresholdConfig.averageCriticalThresholdValue  = DEMO_TMU_AVERAGE_CRITICAL_THRESOLD;
169    @endcode
170  *
171  * @param config Pointer to TMU configuration structure.
172  */
173 void TMU_GetDefaultConfig(tmu_config_t *config);
174 
175 /*!
176  * @brief Enable the TMU interrupts.
177  *
178  * @param base TMU peripheral base address.
179  * @param mask The interrupt mask. Refer to "_tmu_interrupt_enable" enumeration.
180  */
TMU_EnableInterrupts(TMU_Type * base,uint32_t mask)181 static inline void TMU_EnableInterrupts(TMU_Type *base, uint32_t mask)
182 {
183     base->TIER |= mask;
184 }
185 
186 /*!
187  * @brief Disable the TMU interrupts.
188  *
189  * @param base TMU peripheral base address.
190  * @param mask The interrupt mask. Refer to "_tmu_interrupt_enable" enumeration.
191  */
TMU_DisableInterrupts(TMU_Type * base,uint32_t mask)192 static inline void TMU_DisableInterrupts(TMU_Type *base, uint32_t mask)
193 {
194     base->TIER &= ~mask;
195 }
196 
197 /*!
198  * @brief Get interrupt status flags.
199  *
200  * @param base TMU peripheral base address.
201  * @retval The current interrupt status.
202  */
TMU_GetInterruptStatusFlags(TMU_Type * base)203 static inline uint32_t TMU_GetInterruptStatusFlags(TMU_Type *base)
204 {
205     return base->TIDR;
206 }
207 
208 /*!
209  * @brief Clear interrupt status flags.
210  *
211  * @param base TMU peripheral base address.
212  * @param mask The mask of interrupt status flags. Refer to "_tmu_interrupt_status_flags" enumeration.
213  */
TMU_ClearInterruptStatusFlags(TMU_Type * base,uint32_t mask)214 static inline void TMU_ClearInterruptStatusFlags(TMU_Type *base, uint32_t mask)
215 {
216     base->TIDR = mask;
217 }
218 
219 /*!
220  * @brief Get the last immediate temperature at site.
221  *
222  * @param base TMU peripheral base address.
223  * @param probe probe selection, if select both 2 probes, return main probe temeperature by default. Refer to
224  * "tmu_probe_select_t" structure.
225  * @param temperature Last immediate temperature reading at site when V=1.
226  *                    besides, Bit-8 is sign bit: 1 means nagetive and 0 means positive.
227  * @retval get immediate temperature status.
228  */
229 status_t TMU_GetImmediateTemperature(TMU_Type *base, tmu_probe_select_t probe, int8_t *temperature);
230 
231 /*!
232  * @brief Get the last average temperature at site.
233  *
234  * @param base TMU peripheral base address.
235  * @param probe probe selection, if select both 2 probes, return main probe temeperature by default. Refer to
236  * "tmu_probe_select_t" structure.
237  * @param temperature Last average temperature reading at site;
238  *                    besides, Bit-8 is sign bit: 1 means nagetive and 0 means positive.
239  * @retval get average temperature status.
240  */
241 status_t TMU_GetAverageTemperature(TMU_Type *base, tmu_probe_select_t probe, int8_t *temperature);
242 
243 /*!
244  * @brief Update the high temperature threshold value.
245  *
246  * @param base TMU peripheral base address.
247  * @param probe probe selection, if select both 2 probes, set main probe path by default. Refer to "tmu_probe_select_t"
248  structure.
249  * @param thresholdConfig threshold configuration. Refer to "tmu_threshold_config_t" structure.
250  */
251 void TMU_UpdateHighTemperatureThreshold(TMU_Type *base,
252                                         tmu_probe_select_t probe,
253                                         const tmu_threshold_config_t *thresholdConfig);
254 
255 #if defined(__cplusplus)
256 }
257 #endif
258 
259 /*@}*/
260 
261 #endif /* __FSL_TMU_H__ */
262