You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tempmon.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "imxrt.h"
  2. #include "core_pins.h"
  3. #include "avr/pgmspace.h"
  4. #include "debug/printf.h"
  5. static uint16_t frequency = 0x03U;
  6. static uint32_t highAlarmTemp = 85U;
  7. static uint32_t lowAlarmTemp = 25U;
  8. static uint32_t panicAlarmTemp = 90U;
  9. static uint32_t s_hotTemp, s_hotCount, s_roomC_hotC;
  10. static float s_hot_ROOM;
  11. void Panic_Temp_isr(void) {
  12. __disable_irq();
  13. IOMUXC_GPR_GPR16 = 0x00000007;
  14. SNVS_LPCR |= SNVS_LPCR_TOP; //Switch off now
  15. asm volatile ("dsb":::"memory");
  16. while (1) asm ("wfi");
  17. }
  18. FLASHMEM void tempmon_init(void)
  19. {
  20. // Notes:
  21. // TEMPMON_TEMPSENSE0 &= ~0x2U; Stops temp monitoring
  22. // TEMPMON_TEMPSENSE0 |= 0x1U; Powers down temp monitoring
  23. uint32_t calibrationData;
  24. uint32_t roomCount;
  25. uint32_t tempCodeVal;
  26. //first power on the temperature sensor - no register change
  27. TEMPMON_TEMPSENSE0 &= ~0x1U;
  28. //set monitoring frequency - no register change
  29. TEMPMON_TEMPSENSE1 = (((uint32_t)(((uint32_t)(frequency)) << 0U)) & 0xFFFFU);
  30. //read calibration data - this works
  31. calibrationData = HW_OCOTP_ANA1;
  32. s_hotTemp = (uint32_t)(calibrationData & 0xFFU) >> 0x00U;
  33. s_hotCount = (uint32_t)(calibrationData & 0xFFF00U) >> 0X08U;
  34. roomCount = (uint32_t)(calibrationData & 0xFFF00000U) >> 0x14U;
  35. s_hot_ROOM = s_hotTemp - 25.0f;
  36. s_roomC_hotC = roomCount - s_hotCount;
  37. //time to set alarm temperatures
  38. //Set High Alarm Temp
  39. tempCodeVal = (uint32_t)(s_hotCount + (s_hotTemp - highAlarmTemp) * s_roomC_hotC / s_hot_ROOM);
  40. TEMPMON_TEMPSENSE0 |= (((uint32_t)(((uint32_t)(tempCodeVal)) << 20U)) & 0xFFF00000U);
  41. //Set Panic Alarm Temp
  42. tempCodeVal = (uint32_t)(s_hotCount + (s_hotTemp - panicAlarmTemp) * s_roomC_hotC / s_hot_ROOM);
  43. TEMPMON_TEMPSENSE2 |= (((uint32_t)(((uint32_t)(tempCodeVal)) << 16U)) & 0xFFF0000U);
  44. // Set Low Temp Alarm Temp
  45. tempCodeVal = (uint32_t)(s_hotCount + (s_hotTemp - lowAlarmTemp) * s_roomC_hotC / s_hot_ROOM);
  46. TEMPMON_TEMPSENSE2 |= (((uint32_t)(((uint32_t)(tempCodeVal)) << 0U)) & 0xFFFU);
  47. //Start temp monitoring
  48. TEMPMON_TEMPSENSE0 |= 0x2U; //starts temp monitoring
  49. //PANIC shutdown:
  50. NVIC_SET_PRIORITY(IRQ_TEMPERATURE_PANIC, 0);
  51. attachInterruptVector(IRQ_TEMPERATURE_PANIC, &Panic_Temp_isr);
  52. NVIC_ENABLE_IRQ(IRQ_TEMPERATURE_PANIC);
  53. }
  54. float tempmonGetTemp(void)
  55. {
  56. uint32_t nmeas;
  57. float tmeas;
  58. while (!(TEMPMON_TEMPSENSE0 & 0x4U))
  59. {
  60. }
  61. /* ready to read temperature code value */
  62. nmeas = (TEMPMON_TEMPSENSE0 & 0xFFF00U) >> 8U;
  63. /* Calculate temperature */
  64. tmeas = s_hotTemp - (float)((nmeas - s_hotCount) * s_hot_ROOM / s_roomC_hotC);
  65. return tmeas;
  66. }
  67. void tempmon_Start()
  68. {
  69. TEMPMON_TEMPSENSE0 |= 0x2U;
  70. }
  71. void tempmon_Stop()
  72. {
  73. TEMPMON_TEMPSENSE0 &= ~0x2U;
  74. }
  75. void tempmon_PwrDwn()
  76. {
  77. TEMPMON_TEMPSENSE0 |= 0x1U;
  78. }