File indexing completed on 2025-05-11 08:23:01
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "fsl_tempmon.h"
0010
0011
0012
0013
0014
0015
0016 #ifndef FSL_COMPONENT_ID
0017 #define FSL_COMPONENT_ID "platform.drivers.tempmon"
0018 #endif
0019
0020
0021 #define TEMPMON_HOTTEMPMASK 0xFFU
0022 #define TEMPMON_HOTTEMPSHIFT 0x00U
0023 #define TEMPMON_HOTCOUNTMASK 0xFFF00U
0024 #define TEMPMON_HOTCOUNTSHIFT 0X08U
0025 #define TEMPMON_ROOMCOUNTMASK 0xFFF00000U
0026 #define TEMPMON_ROOMCOUNTSHIFT 0x14U
0027
0028
0029 #define TEMPMON_ROOMTEMP 25.0f
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 static int32_t s_hotTemp;
0040 static int32_t s_hotCount;
0041 static float s_hotT_ROOM;
0042 static int32_t s_roomC_hotC;
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 void TEMPMON_Init(TEMPMON_Type *base, const tempmon_config_t *config)
0054 {
0055 assert(NULL != config);
0056
0057 uint32_t calibrationData;
0058 uint32_t tmpU32;
0059 int32_t roomCount;
0060
0061
0062 base->TEMPSENSE0 &= ~TEMPMON_TEMPSENSE0_POWER_DOWN_MASK;
0063
0064
0065 base->TEMPSENSE1 = TEMPMON_TEMPSENSE1_MEASURE_FREQ(config->frequency);
0066
0067
0068 calibrationData = OCOTP->ANA1;
0069 tmpU32 = (calibrationData & TEMPMON_HOTTEMPMASK) >> TEMPMON_HOTTEMPSHIFT;
0070 s_hotTemp = (int32_t)tmpU32;
0071
0072 tmpU32 = (calibrationData & TEMPMON_HOTCOUNTMASK) >> TEMPMON_HOTCOUNTSHIFT;
0073 s_hotCount = (int32_t)tmpU32;
0074
0075 tmpU32 = (calibrationData & TEMPMON_ROOMCOUNTMASK) >> TEMPMON_ROOMCOUNTSHIFT;
0076 roomCount = (int32_t)tmpU32;
0077
0078 s_hotT_ROOM = (float)s_hotTemp - TEMPMON_ROOMTEMP;
0079 s_roomC_hotC = roomCount - s_hotCount;
0080
0081
0082 TEMPMON_SetTempAlarm(base, config->highAlarmTemp, kTEMPMON_HighAlarmMode);
0083 TEMPMON_SetTempAlarm(base, config->panicAlarmTemp, kTEMPMON_PanicAlarmMode);
0084 TEMPMON_SetTempAlarm(base, config->lowAlarmTemp, kTEMPMON_LowAlarmMode);
0085 }
0086
0087
0088
0089
0090
0091
0092 void TEMPMON_Deinit(TEMPMON_Type *base)
0093 {
0094 base->TEMPSENSE0 |= TEMPMON_TEMPSENSE0_POWER_DOWN_MASK;
0095 }
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109 void TEMPMON_GetDefaultConfig(tempmon_config_t *config)
0110 {
0111 assert(config);
0112
0113
0114 (void)memset(config, 0, sizeof(*config));
0115
0116
0117 config->frequency = 0x03U;
0118
0119 config->highAlarmTemp = 40;
0120
0121 config->panicAlarmTemp = 90;
0122
0123 config->lowAlarmTemp = 20;
0124 }
0125
0126
0127
0128
0129
0130
0131
0132 float TEMPMON_GetCurrentTemperature(TEMPMON_Type *base)
0133 {
0134
0135 assert(NULL != base);
0136
0137 uint32_t nmeas;
0138 float tmeas;
0139
0140 while (0U == (base->TEMPSENSE0 & TEMPMON_TEMPSENSE0_FINISHED_MASK))
0141 {
0142 }
0143
0144
0145 nmeas = (base->TEMPSENSE0 & TEMPMON_TEMPSENSE0_TEMP_CNT_MASK) >> TEMPMON_TEMPSENSE0_TEMP_CNT_SHIFT;
0146
0147
0148 tmeas = (float)s_hotTemp - (((float)nmeas - (float)s_hotCount) * (s_hotT_ROOM / (float)s_roomC_hotC));
0149
0150 return tmeas;
0151 }
0152
0153
0154
0155
0156
0157
0158
0159
0160 void TEMPMON_SetTempAlarm(TEMPMON_Type *base, int8_t tempVal, tempmon_alarm_mode alarmMode)
0161 {
0162
0163 assert(NULL != base);
0164
0165
0166 assert(s_hotTemp >= tempVal);
0167
0168 int32_t tempCodeVal;
0169 uint32_t tempRegVal;
0170
0171
0172 tempCodeVal = s_hotCount + (s_hotTemp - (int32_t)tempVal) * s_roomC_hotC / (int32_t)s_hotT_ROOM;
0173
0174 switch (alarmMode)
0175 {
0176 case kTEMPMON_HighAlarmMode:
0177
0178 tempRegVal = base->TEMPSENSE0;
0179 tempRegVal =
0180 (tempRegVal & ~TEMPMON_TEMPSENSE0_ALARM_VALUE_MASK) | TEMPMON_TEMPSENSE0_ALARM_VALUE(tempCodeVal);
0181 base->TEMPSENSE0 = tempRegVal;
0182 break;
0183
0184 case kTEMPMON_PanicAlarmMode:
0185
0186 tempRegVal = base->TEMPSENSE2;
0187 tempRegVal = (tempRegVal & ~TEMPMON_TEMPSENSE2_PANIC_ALARM_VALUE_MASK) |
0188 TEMPMON_TEMPSENSE2_PANIC_ALARM_VALUE(tempCodeVal);
0189 base->TEMPSENSE2 = tempRegVal;
0190 break;
0191
0192 case kTEMPMON_LowAlarmMode:
0193
0194 tempRegVal = base->TEMPSENSE2;
0195 tempRegVal = (tempRegVal & ~TEMPMON_TEMPSENSE2_LOW_ALARM_VALUE_MASK) |
0196 TEMPMON_TEMPSENSE2_LOW_ALARM_VALUE(tempCodeVal);
0197 base->TEMPSENSE2 = tempRegVal;
0198 break;
0199
0200 default:
0201 assert(false);
0202 break;
0203 }
0204 }