Teensy 4.1 core updated for C++20
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.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. FLASHMEM void tempmon_init(void)
  12. {
  13. // Notes:
  14. // TEMPMON_TEMPSENSE0 &= ~0x2U; Stops temp monitoring
  15. // TEMPMON_TEMPSENSE0 |= 0x1U; Powers down temp monitoring
  16. uint32_t calibrationData;
  17. uint32_t roomCount;
  18. uint32_t tempCodeVal;
  19. //first power on the temperature sensor - no register change
  20. TEMPMON_TEMPSENSE0 &= ~0x1U;
  21. //set monitoring frequency - no register change
  22. TEMPMON_TEMPSENSE1 = (((uint32_t)(((uint32_t)(frequency)) << 0U)) & 0xFFFFU);
  23. //read calibration data - this works
  24. calibrationData = HW_OCOTP_ANA1;
  25. s_hotTemp = (uint32_t)(calibrationData & 0xFFU) >> 0x00U;
  26. s_hotCount = (uint32_t)(calibrationData & 0xFFF00U) >> 0X08U;
  27. roomCount = (uint32_t)(calibrationData & 0xFFF00000U) >> 0x14U;
  28. s_hot_ROOM = s_hotTemp - 25.0f;
  29. s_roomC_hotC = roomCount - s_hotCount;
  30. //time to set alarm temperatures
  31. //Set High Alarm Temp
  32. tempCodeVal = (uint32_t)(s_hotCount + (s_hotTemp - highAlarmTemp) * s_roomC_hotC / s_hot_ROOM);
  33. TEMPMON_TEMPSENSE0 |= (((uint32_t)(((uint32_t)(tempCodeVal)) << 20U)) & 0xFFF00000U);
  34. //Set Panic Alarm Temp
  35. tempCodeVal = (uint32_t)(s_hotCount + (s_hotTemp - panicAlarmTemp) * s_roomC_hotC / s_hot_ROOM);
  36. TEMPMON_TEMPSENSE2 |= (((uint32_t)(((uint32_t)(tempCodeVal)) << 16U)) & 0xFFF0000U);
  37. // Set Low Temp Alarm Temp
  38. tempCodeVal = (uint32_t)(s_hotCount + (s_hotTemp - lowAlarmTemp) * s_roomC_hotC / s_hot_ROOM);
  39. TEMPMON_TEMPSENSE2 |= (((uint32_t)(((uint32_t)(tempCodeVal)) << 0U)) & 0xFFFU);
  40. //Start temp monitoring
  41. TEMPMON_TEMPSENSE0 |= 0x2U; //starts temp monitoring
  42. }
  43. float tempmonGetTemp(void)
  44. {
  45. uint32_t nmeas;
  46. float tmeas;
  47. while (!(TEMPMON_TEMPSENSE0 & 0x4U))
  48. {
  49. }
  50. /* ready to read temperature code value */
  51. nmeas = (TEMPMON_TEMPSENSE0 & 0xFFF00U) >> 8U;
  52. /* Calculate temperature */
  53. tmeas = s_hotTemp - (float)((nmeas - s_hotCount) * s_hot_ROOM / s_roomC_hotC);
  54. return tmeas;
  55. }
  56. void tempmon_Start()
  57. {
  58. TEMPMON_TEMPSENSE0 |= 0x2U;
  59. }
  60. void tempmon_Stop()
  61. {
  62. TEMPMON_TEMPSENSE0 &= ~0x2U;
  63. }
  64. void tempmon_PwrDwn()
  65. {
  66. TEMPMON_TEMPSENSE0 |= 0x1U;
  67. }