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 uint32_t s_hotTemp;    /*!< The value of TEMPMON_TEMPSENSE0[TEMP_VALUE] at room temperature .*/
40 static uint32_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��).*/
42 static uint32_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 roomCount;
59 
60     /* Power on the temperature sensor*/
61     base->TEMPSENSE0 &= ~TEMPMON_TEMPSENSE0_POWER_DOWN_MASK;
62 
63     /* Set temperature monitor frequency */
64     base->TEMPSENSE1 = TEMPMON_TEMPSENSE1_MEASURE_FREQ(config->frequency);
65 
66     /* ready to read calibration data */
67     calibrationData = OCOTP->ANA1;
68     s_hotTemp       = (uint32_t)(calibrationData & TEMPMON_HOTTEMPMASK) >> TEMPMON_HOTTEMPSHIFT;
69     s_hotCount      = (uint32_t)(calibrationData & TEMPMON_HOTCOUNTMASK) >> TEMPMON_HOTCOUNTSHIFT;
70     roomCount       = (uint32_t)(calibrationData & TEMPMON_ROOMCOUNTMASK) >> TEMPMON_ROOMCOUNTSHIFT;
71 
72     s_hotT_ROOM  = (float)s_hotTemp - TEMPMON_ROOMTEMP;
73     s_roomC_hotC = roomCount - s_hotCount;
74 
75     /* Set alarm temperature */
76     TEMPMON_SetTempAlarm(base, config->highAlarmTemp, kTEMPMON_HighAlarmMode);
77     TEMPMON_SetTempAlarm(base, config->panicAlarmTemp, kTEMPMON_PanicAlarmMode);
78     TEMPMON_SetTempAlarm(base, config->lowAlarmTemp, kTEMPMON_LowAlarmMode);
79 }
80 
81 /*!
82  * brief Deinitializes the TEMPMON module.
83  *
84  * param base TEMPMON base pointer
85  */
TEMPMON_Deinit(TEMPMON_Type * base)86 void TEMPMON_Deinit(TEMPMON_Type *base)
87 {
88     base->TEMPSENSE0 |= TEMPMON_TEMPSENSE0_POWER_DOWN_MASK;
89 }
90 
91 /*!
92  * brief Gets the default configuration structure.
93  *
94  * This function initializes the TEMPMON configuration structure to a default value. The default
95  * values are:
96  *   tempmonConfig->frequency = 0x02U;
97  *   tempmonConfig->highAlarmTemp = 44U;
98  *   tempmonConfig->panicAlarmTemp = 90U;
99  *   tempmonConfig->lowAlarmTemp = 39U;
100  *
101  * param config Pointer to a configuration structure.
102  */
TEMPMON_GetDefaultConfig(tempmon_config_t * config)103 void TEMPMON_GetDefaultConfig(tempmon_config_t *config)
104 {
105     assert(config);
106 
107     /* Initializes the configure structure to zero. */
108     (void)memset(config, 0, sizeof(*config));
109 
110     /* Default measure frequency */
111     config->frequency = 0x03U;
112     /* Default high alarm temperature */
113     config->highAlarmTemp = 40U;
114     /* Default panic alarm temperature */
115     config->panicAlarmTemp = 90U;
116     /* Default low alarm temperature */
117     config->lowAlarmTemp = 20U;
118 }
119 
120 /*!
121  * brief Get current temperature with the fused temperature calibration data.
122  *
123  * param base TEMPMON base pointer
124  * return current temperature with degrees Celsius.
125  */
TEMPMON_GetCurrentTemperature(TEMPMON_Type * base)126 float TEMPMON_GetCurrentTemperature(TEMPMON_Type *base)
127 {
128     /* Check arguments */
129     assert(NULL != base);
130 
131     uint32_t nmeas;
132     float tmeas;
133 
134     while (0U == (base->TEMPSENSE0 & TEMPMON_TEMPSENSE0_FINISHED_MASK))
135     {
136     }
137 
138     /* ready to read temperature code value */
139     nmeas = (base->TEMPSENSE0 & TEMPMON_TEMPSENSE0_TEMP_CNT_MASK) >> TEMPMON_TEMPSENSE0_TEMP_CNT_SHIFT;
140 
141     /* Calculate temperature */
142     tmeas = (float)s_hotTemp - (((float)nmeas - (float)s_hotCount) * s_hotT_ROOM / (float)s_roomC_hotC);
143 
144     return tmeas;
145 }
146 
147 /*!
148  * brief Set the temperature count (raw sensor output) that will generate an alarm interrupt.
149  *
150  * param base TEMPMON base pointer
151  * param tempVal The alarm temperature with degrees Celsius
152  * param alarmMode The alarm mode.
153  */
TEMPMON_SetTempAlarm(TEMPMON_Type * base,uint32_t tempVal,tempmon_alarm_mode alarmMode)154 void TEMPMON_SetTempAlarm(TEMPMON_Type *base, uint32_t tempVal, tempmon_alarm_mode alarmMode)
155 {
156     /* Check arguments */
157     assert(NULL != base);
158     /* Different SOC has different qualified temperature level based on AEC-Q100 standard by default, such as Consumer(0
159        to +95 degrees celsius)/Industrial(-40 to +105 degrees celsius)/Automotive(-40 to +125 degrees celsius). */
160     assert(s_hotTemp >= tempVal);
161 
162     uint32_t tempCodeVal;
163     uint32_t tempRegVal;
164 
165     /* Calculate alarm temperature code value */
166     tempCodeVal = (uint32_t)(s_hotCount + (s_hotTemp - tempVal) * s_roomC_hotC / (uint32_t)s_hotT_ROOM);
167 
168     switch (alarmMode)
169     {
170         case kTEMPMON_HighAlarmMode:
171             /* Clear alarm value and set a new high alarm temperature code value */
172             tempRegVal = base->TEMPSENSE0;
173             tempRegVal =
174                 (tempRegVal & ~TEMPMON_TEMPSENSE0_ALARM_VALUE_MASK) | TEMPMON_TEMPSENSE0_ALARM_VALUE(tempCodeVal);
175             base->TEMPSENSE0 = tempRegVal;
176             break;
177 
178         case kTEMPMON_PanicAlarmMode:
179             /* Clear panic alarm value and set a new panic alarm temperature code value */
180             tempRegVal = base->TEMPSENSE2;
181             tempRegVal = (tempRegVal & ~TEMPMON_TEMPSENSE2_PANIC_ALARM_VALUE_MASK) |
182                          TEMPMON_TEMPSENSE2_PANIC_ALARM_VALUE(tempCodeVal);
183             base->TEMPSENSE2 = tempRegVal;
184             break;
185 
186         case kTEMPMON_LowAlarmMode:
187             /* Clear low alarm value and set a new low alarm temperature code value */
188             tempRegVal = base->TEMPSENSE2;
189             tempRegVal = (tempRegVal & ~TEMPMON_TEMPSENSE2_LOW_ALARM_VALUE_MASK) |
190                          TEMPMON_TEMPSENSE2_LOW_ALARM_VALUE(tempCodeVal);
191             base->TEMPSENSE2 = tempRegVal;
192             break;
193 
194         default:
195             assert(false);
196             break;
197     }
198 }
199