fsl_tempmon.c 6.8 KB

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