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.

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