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.

86 lines
2.4KB

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