Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:01

0001 /*
0002  * Copyright  2018-2021 NXP
0003  * All rights reserved.
0004  *
0005  *
0006  * SPDX-License-Identifier: BSD-3-Clause
0007  */
0008 
0009 #include "fsl_tempmon.h"
0010 
0011 /*******************************************************************************
0012  * Definitions
0013  ******************************************************************************/
0014 
0015 /* Component ID definition, used by tools. */
0016 #ifndef FSL_COMPONENT_ID
0017 #define FSL_COMPONENT_ID "platform.drivers.tempmon"
0018 #endif
0019 
0020 /*! @brief TEMPMON calibration data mask. */
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 /*! @brief the room temperature. */
0029 #define TEMPMON_ROOMTEMP 25.0f
0030 
0031 /*******************************************************************************
0032  * Prototypes
0033  ******************************************************************************/
0034 
0035 /*******************************************************************************
0036  * Variables
0037  ******************************************************************************/
0038 
0039 static int32_t s_hotTemp;    /*!< The value of TEMPMON_TEMPSENSE0[TEMP_VALUE] at room temperature .*/
0040 static int32_t s_hotCount;   /*!< The value of TEMPMON_TEMPSENSE0[TEMP_VALUE] at the hot temperature.*/
0041 static float s_hotT_ROOM;    /*!< The value of s_hotTemp minus room temperature(25 degrees celsius).*/
0042 static int32_t s_roomC_hotC; /*!< The value of s_roomCount minus s_hotCount.*/
0043 
0044 /*******************************************************************************
0045  * Code
0046  ******************************************************************************/
0047 /*!
0048  * brief Initializes the TEMPMON module.
0049  *
0050  * param base TEMPMON base pointer
0051  * param config Pointer to configuration structure.
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     /* Power on the temperature sensor*/
0062     base->TEMPSENSE0 &= ~TEMPMON_TEMPSENSE0_POWER_DOWN_MASK;
0063 
0064     /* Set temperature monitor frequency */
0065     base->TEMPSENSE1 = TEMPMON_TEMPSENSE1_MEASURE_FREQ(config->frequency);
0066 
0067     /* ready to read calibration data */
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     /* Set alarm temperature */
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  * brief Deinitializes the TEMPMON module.
0089  *
0090  * param base TEMPMON base pointer
0091  */
0092 void TEMPMON_Deinit(TEMPMON_Type *base)
0093 {
0094     base->TEMPSENSE0 |= TEMPMON_TEMPSENSE0_POWER_DOWN_MASK;
0095 }
0096 
0097 /*!
0098  * brief Gets the default configuration structure.
0099  *
0100  * This function initializes the TEMPMON configuration structure to a default value. The default
0101  * values are:
0102  *   tempmonConfig->frequency = 0x02U;
0103  *   tempmonConfig->highAlarmTemp = 44U;
0104  *   tempmonConfig->panicAlarmTemp = 90U;
0105  *   tempmonConfig->lowAlarmTemp = 39U;
0106  *
0107  * param config Pointer to a configuration structure.
0108  */
0109 void TEMPMON_GetDefaultConfig(tempmon_config_t *config)
0110 {
0111     assert(config);
0112 
0113     /* Initializes the configure structure to zero. */
0114     (void)memset(config, 0, sizeof(*config));
0115 
0116     /* Default measure frequency */
0117     config->frequency = 0x03U;
0118     /* Default high alarm temperature */
0119     config->highAlarmTemp = 40;
0120     /* Default panic alarm temperature */
0121     config->panicAlarmTemp = 90;
0122     /* Default low alarm temperature */
0123     config->lowAlarmTemp = 20;
0124 }
0125 
0126 /*!
0127  * brief Get current temperature with the fused temperature calibration data.
0128  *
0129  * param base TEMPMON base pointer
0130  * return current temperature with degrees Celsius.
0131  */
0132 float TEMPMON_GetCurrentTemperature(TEMPMON_Type *base)
0133 {
0134     /* Check arguments */
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     /* ready to read temperature code value */
0145     nmeas = (base->TEMPSENSE0 & TEMPMON_TEMPSENSE0_TEMP_CNT_MASK) >> TEMPMON_TEMPSENSE0_TEMP_CNT_SHIFT;
0146 
0147     /* Calculate temperature */
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  * brief Set the temperature count (raw sensor output) that will generate an alarm interrupt.
0155  *
0156  * param base TEMPMON base pointer
0157  * param tempVal The alarm temperature with degrees Celsius
0158  * param alarmMode The alarm mode.
0159  */
0160 void TEMPMON_SetTempAlarm(TEMPMON_Type *base, int8_t tempVal, tempmon_alarm_mode alarmMode)
0161 {
0162     /* Check arguments */
0163     assert(NULL != base);
0164     /* Different SOC has different qualified temperature level based on AEC-Q100 standard by default, such as Consumer(0
0165        to +95 degrees celsius)/Industrial(-40 to +105 degrees celsius)/Automotive(-40 to +125 degrees celsius). */
0166     assert(s_hotTemp >= tempVal);
0167 
0168     int32_t tempCodeVal;
0169     uint32_t tempRegVal;
0170 
0171     /* Calculate alarm temperature code value */
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             /* Clear alarm value and set a new high alarm temperature code value */
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             /* Clear panic alarm value and set a new panic alarm temperature code value */
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             /* Clear low alarm value and set a new low alarm temperature code value */
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 }