Fork of the espurna firmware for `mhsw` switches
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.

61 lines
1.6 KiB

  1. /*
  2. RTMEM MODULE
  3. Copyright (C) 2019 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  4. */
  5. #pragma once
  6. // Base address of USER RTC memory
  7. // https://github.com/esp8266/esp8266-wiki/wiki/Memory-Map#memmory-mapped-io-registers
  8. #define RTCMEM_ADDR_BASE (0x60001200)
  9. // RTC memory is accessed using blocks of 4 bytes.
  10. // Blocks 0..63 are reserved by the SDK, 64..192 are available to the user.
  11. // Blocks 64..96 are reserved by the eboot 'struct eboot_command' (128 -> (128 / 4) -> 32):
  12. // https://github.com/esp8266/Arduino/blob/master/bootloaders/eboot/eboot_command.h
  13. #define RTCMEM_OFFSET 32u
  14. #define RTCMEM_ADDR (RTCMEM_ADDR_BASE + (RTCMEM_OFFSET * 4u))
  15. #define RTCMEM_BLOCKS 96u
  16. // Change this when modifying RtcmemData
  17. #define RTCMEM_MAGIC 0x46535076
  18. // XXX: All access must be 4-byte aligned and always at full length.
  19. // Exactly like PROGMEM works. For example, using bitfields / inner structs / etc:
  20. // ...
  21. // uint32_t a : 8;
  22. // uint32_t b : 8;
  23. // uint32_t c : 8;
  24. // uint32_t d : 8;
  25. // ...
  26. //
  27. // This would not write the expected thing:
  28. // mem->d = 4;
  29. // TODO replace with custom memory segment in ldscript?
  30. // `magic` would need to be tracked differently
  31. struct RtcmemEnergy {
  32. uint32_t kwh;
  33. uint32_t ws;
  34. };
  35. struct RtcmemData {
  36. uint32_t magic;
  37. uint32_t sys;
  38. uint32_t relay;
  39. uint32_t mqtt;
  40. uint64_t light;
  41. RtcmemEnergy energy[4];
  42. };
  43. static_assert(sizeof(RtcmemData) <= (RTCMEM_BLOCKS * 4u), "RTCMEM struct is too big");
  44. constexpr uint8_t RtcmemSize = (sizeof(RtcmemData) / 4u);
  45. auto Rtcmem = reinterpret_cast<volatile RtcmemData*>(RTCMEM_ADDR);
  46. bool rtcmemStatus();