1 /*
2 * Copyright 2018-2021 NXP
3 * All rights reserved.
4 *
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_tempmon.h"
10
11 /*******************************************************************************
12 * Definitions
13 ******************************************************************************/
14
15 /* Component ID definition, used by tools. */
16 #ifndef FSL_COMPONENT_ID
17 #define FSL_COMPONENT_ID "platform.drivers.tempmon"
18 #endif
19
20 /*! @brief TEMPMON calibration data mask. */
21 #define TEMPMON_HOTTEMPMASK 0xFFU
22 #define TEMPMON_HOTTEMPSHIFT 0x00U
23 #define TEMPMON_HOTCOUNTMASK 0xFFF00U
24 #define TEMPMON_HOTCOUNTSHIFT 0X08U
25 #define TEMPMON_ROOMCOUNTMASK 0xFFF00000U
26 #define TEMPMON_ROOMCOUNTSHIFT 0x14U
27
28 /*! @brief the room temperature. */
29 #define TEMPMON_ROOMTEMP 25.0f
30
31 /*******************************************************************************
32 * Prototypes
33 ******************************************************************************/
34
35 /*******************************************************************************
36 * Variables
37 ******************************************************************************/
38
39 static int32_t s_hotTemp; /*!< The value of TEMPMON_TEMPSENSE0[TEMP_VALUE] at room temperature .*/
40 static int32_t s_hotCount; /*!< The value of TEMPMON_TEMPSENSE0[TEMP_VALUE] at the hot temperature.*/
41 static float s_hotT_ROOM; /*!< The value of s_hotTemp minus room temperature(25 degrees celsius).*/
42 static int32_t s_roomC_hotC; /*!< The value of s_roomCount minus s_hotCount.*/
43
44 /*******************************************************************************
45 * Code
46 ******************************************************************************/
47 /*!
48 * brief Initializes the TEMPMON module.
49 *
50 * param base TEMPMON base pointer
51 * param config Pointer to configuration structure.
52 */
TEMPMON_Init(TEMPMON_Type * base,const tempmon_config_t * config)53 void TEMPMON_Init(TEMPMON_Type *base, const tempmon_config_t *config)
54 {
55 assert(NULL != config);
56
57 uint32_t calibrationData;
58 uint32_t tmpU32;
59 int32_t roomCount;
60
61 /* Power on the temperature sensor*/
62 base->TEMPSENSE0 &= ~TEMPMON_TEMPSENSE0_POWER_DOWN_MASK;
63
64 /* Set temperature monitor frequency */
65 base->TEMPSENSE1 = TEMPMON_TEMPSENSE1_MEASURE_FREQ(config->frequency);
66
67 /* ready to read calibration data */
68 calibrationData = OCOTP->ANA1;
69 tmpU32 = (calibrationData & TEMPMON_HOTTEMPMASK) >> TEMPMON_HOTTEMPSHIFT;
70 s_hotTemp = (int32_t)tmpU32;
71
72 tmpU32 = (calibrationData & TEMPMON_HOTCOUNTMASK) >> TEMPMON_HOTCOUNTSHIFT;
73 s_hotCount = (int32_t)tmpU32;
74
75 tmpU32 = (calibrationData & TEMPMON_ROOMCOUNTMASK) >> TEMPMON_ROOMCOUNTSHIFT;
76 roomCount = (int32_t)tmpU32;
77
78 s_hotT_ROOM = (float)s_hotTemp - TEMPMON_ROOMTEMP;
79 s_roomC_hotC = roomCount - s_hotCount;
80
81 /* Set alarm temperature */
82 TEMPMON_SetTempAlarm(base, config->highAlarmTemp, kTEMPMON_HighAlarmMode);
83 TEMPMON_SetTempAlarm(base, config->panicAlarmTemp, kTEMPMON_PanicAlarmMode);
84 TEMPMON_SetTempAlarm(base, config->lowAlarmTemp, kTEMPMON_LowAlarmMode);
85 }
86
87 /*!
88 * brief Deinitializes the TEMPMON module.
89 *
90 * param base TEMPMON base pointer
91 */
TEMPMON_Deinit(TEMPMON_Type * base)92 void TEMPMON_Deinit(TEMPMON_Type *base)
93 {
94 base->TEMPSENSE0 |= TEMPMON_TEMPSENSE0_POWER_DOWN_MASK;
95 }
96
97 /*!
98 * brief Gets the default configuration structure.
99 *
100 * This function initializes the TEMPMON configuration structure to a default value. The default
101 * values are:
102 * tempmonConfig->frequency = 0x02U;
103 * tempmonConfig->highAlarmTemp = 44U;
104 * tempmonConfig->panicAlarmTemp = 90U;
105 * tempmonConfig->lowAlarmTemp = 39U;
106 *
107 * param config Pointer to a configuration structure.
108 */
TEMPMON_GetDefaultConfig(tempmon_config_t * config)109 void TEMPMON_GetDefaultConfig(tempmon_config_t *config)
110 {
111 assert(config);
112
113 /* Initializes the configure structure to zero. */
114 (void)memset(config, 0, sizeof(*config));
115
116 /* Default measure frequency */
117 config->frequency = 0x03U;
118 /* Default high alarm temperature */
119 config->highAlarmTemp = 40;
120 /* Default panic alarm temperature */
121 config->panicAlarmTemp = 90;
122 /* Default low alarm temperature */
123 config->lowAlarmTemp = 20;
124 }
125
126 /*!
127 * brief Get current temperature with the fused temperature calibration data.
128 *
129 * param base TEMPMON base pointer
130 * return current temperature with degrees Celsius.
131 */
TEMPMON_GetCurrentTemperature(TEMPMON_Type * base)132 float TEMPMON_GetCurrentTemperature(TEMPMON_Type *base)
133 {
134 /* Check arguments */
135 assert(NULL != base);
136
137 uint32_t nmeas;
138 float tmeas;
139
140 while (0U == (base->TEMPSENSE0 & TEMPMON_TEMPSENSE0_FINISHED_MASK))
141 {
142 }
143
144 /* ready to read temperature code value */
145 nmeas = (base->TEMPSENSE0 & TEMPMON_TEMPSENSE0_TEMP_CNT_MASK) >> TEMPMON_TEMPSENSE0_TEMP_CNT_SHIFT;
146
147 /* Calculate temperature */
148 tmeas = (float)s_hotTemp - (((float)nmeas - (float)s_hotCount) * (s_hotT_ROOM / (float)s_roomC_hotC));
149
150 return tmeas;
151 }
152
153 /*!
154 * brief Set the temperature count (raw sensor output) that will generate an alarm interrupt.
155 *
156 * param base TEMPMON base pointer
157 * param tempVal The alarm temperature with degrees Celsius
158 * param alarmMode The alarm mode.
159 */
TEMPMON_SetTempAlarm(TEMPMON_Type * base,int8_t tempVal,tempmon_alarm_mode alarmMode)160 void TEMPMON_SetTempAlarm(TEMPMON_Type *base, int8_t tempVal, tempmon_alarm_mode alarmMode)
161 {
162 /* Check arguments */
163 assert(NULL != base);
164 /* Different SOC has different qualified temperature level based on AEC-Q100 standard by default, such as Consumer(0
165 to +95 degrees celsius)/Industrial(-40 to +105 degrees celsius)/Automotive(-40 to +125 degrees celsius). */
166 assert(s_hotTemp >= tempVal);
167
168 int32_t tempCodeVal;
169 uint32_t tempRegVal;
170
171 /* Calculate alarm temperature code value */
172 tempCodeVal = s_hotCount + (s_hotTemp - (int32_t)tempVal) * s_roomC_hotC / (int32_t)s_hotT_ROOM;
173
174 switch (alarmMode)
175 {
176 case kTEMPMON_HighAlarmMode:
177 /* Clear alarm value and set a new high alarm temperature code value */
178 tempRegVal = base->TEMPSENSE0;
179 tempRegVal =
180 (tempRegVal & ~TEMPMON_TEMPSENSE0_ALARM_VALUE_MASK) | TEMPMON_TEMPSENSE0_ALARM_VALUE(tempCodeVal);
181 base->TEMPSENSE0 = tempRegVal;
182 break;
183
184 case kTEMPMON_PanicAlarmMode:
185 /* Clear panic alarm value and set a new panic alarm temperature code value */
186 tempRegVal = base->TEMPSENSE2;
187 tempRegVal = (tempRegVal & ~TEMPMON_TEMPSENSE2_PANIC_ALARM_VALUE_MASK) |
188 TEMPMON_TEMPSENSE2_PANIC_ALARM_VALUE(tempCodeVal);
189 base->TEMPSENSE2 = tempRegVal;
190 break;
191
192 case kTEMPMON_LowAlarmMode:
193 /* Clear low alarm value and set a new low alarm temperature code value */
194 tempRegVal = base->TEMPSENSE2;
195 tempRegVal = (tempRegVal & ~TEMPMON_TEMPSENSE2_LOW_ALARM_VALUE_MASK) |
196 TEMPMON_TEMPSENSE2_LOW_ALARM_VALUE(tempCodeVal);
197 base->TEMPSENSE2 = tempRegVal;
198 break;
199
200 default:
201 assert(false);
202 break;
203 }
204 }
205