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.

274 lines
7.0 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. /*
  2. SYSTEM MODULE
  3. Copyright (C) 2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include <Ticker.h>
  6. #include <EEPROM_Rotate.h>
  7. // -----------------------------------------------------------------------------
  8. bool _system_send_heartbeat = false;
  9. unsigned char _heartbeat_mode = HEARTBEAT_MODE;
  10. unsigned long _heartbeat_interval = HEARTBEAT_INTERVAL;
  11. // Calculated load average 0 to 100;
  12. unsigned short int _load_average = 100;
  13. // -----------------------------------------------------------------------------
  14. union system_rtcmem_t {
  15. struct {
  16. uint8_t stability_counter;
  17. uint8_t reset_reason;
  18. uint16_t _reserved_;
  19. } parts;
  20. uint32_t value;
  21. };
  22. uint8_t systemStabilityCounter() {
  23. system_rtcmem_t data;
  24. data.value = Rtcmem->sys;
  25. return data.parts.stability_counter;
  26. }
  27. void systemStabilityCounter(uint8_t counter) {
  28. system_rtcmem_t data;
  29. data.value = Rtcmem->sys;
  30. data.parts.stability_counter = counter;
  31. Rtcmem->sys = data.value;
  32. }
  33. uint8_t _systemResetReason() {
  34. system_rtcmem_t data;
  35. data.value = Rtcmem->sys;
  36. return data.parts.reset_reason;
  37. }
  38. void _systemResetReason(uint8_t reason) {
  39. system_rtcmem_t data;
  40. data.value = Rtcmem->sys;
  41. data.parts.reset_reason = reason;
  42. Rtcmem->sys = data.value;
  43. }
  44. #if SYSTEM_CHECK_ENABLED
  45. // Call this method on boot with start=true to increase the crash counter
  46. // Call it again once the system is stable to decrease the counter
  47. // If the counter reaches SYSTEM_CHECK_MAX then the system is flagged as unstable
  48. // setting _systemOK = false;
  49. //
  50. // An unstable system will only have serial access, WiFi in AP mode and OTA
  51. bool _systemStable = true;
  52. void systemCheck(bool stable) {
  53. uint8_t value = 0;
  54. if (stable) {
  55. value = 0;
  56. DEBUG_MSG_P(PSTR("[MAIN] System OK\n"));
  57. } else {
  58. if (!rtcmemStatus()) {
  59. systemStabilityCounter(1);
  60. return;
  61. }
  62. value = systemStabilityCounter();
  63. if (++value > SYSTEM_CHECK_MAX) {
  64. _systemStable = false;
  65. value = 0;
  66. DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n"));
  67. }
  68. }
  69. systemStabilityCounter(value);
  70. }
  71. bool systemCheck() {
  72. return _systemStable;
  73. }
  74. void systemCheckLoop() {
  75. static bool checked = false;
  76. if (!checked && (millis() > SYSTEM_CHECK_TIME)) {
  77. // Flag system as stable
  78. systemCheck(true);
  79. checked = true;
  80. }
  81. }
  82. #endif
  83. // -----------------------------------------------------------------------------
  84. // Reset
  85. // -----------------------------------------------------------------------------
  86. Ticker _defer_reset;
  87. uint8_t _reset_reason = 0;
  88. // system_get_rst_info() result is cached by the Core init for internal use
  89. uint32_t systemResetReason() {
  90. return resetInfo.reason;
  91. }
  92. void customResetReason(unsigned char reason) {
  93. _reset_reason = reason;
  94. _systemResetReason(reason);
  95. }
  96. unsigned char customResetReason() {
  97. static unsigned char status = 255;
  98. if (status == 255) {
  99. if (rtcmemStatus()) status = _systemResetReason();
  100. if (status > 0) customResetReason(0);
  101. if (status > CUSTOM_RESET_MAX) status = 0;
  102. }
  103. return status;
  104. }
  105. void reset() {
  106. ESP.restart();
  107. }
  108. void deferredReset(unsigned long delay, unsigned char reason) {
  109. _defer_reset.once_ms(delay, customResetReason, reason);
  110. }
  111. bool checkNeedsReset() {
  112. return _reset_reason > 0;
  113. }
  114. // -----------------------------------------------------------------------------
  115. void systemSendHeartbeat() {
  116. _system_send_heartbeat = true;
  117. }
  118. bool systemGetHeartbeat() {
  119. return _system_send_heartbeat;
  120. }
  121. unsigned long systemLoadAverage() {
  122. return _load_average;
  123. }
  124. void _systemSetupHeartbeat() {
  125. _heartbeat_mode = getSetting("hbMode", HEARTBEAT_MODE).toInt();
  126. _heartbeat_interval = getSetting("hbInterval", HEARTBEAT_INTERVAL).toInt();
  127. }
  128. #if WEB_SUPPORT
  129. bool _systemWebSocketOnReceive(const char * key, JsonVariant& value) {
  130. return (strncmp(key, "hb", 2) == 0);
  131. }
  132. #endif
  133. void systemLoop() {
  134. // -------------------------------------------------------------------------
  135. // User requested reset
  136. // -------------------------------------------------------------------------
  137. if (checkNeedsReset()) {
  138. reset();
  139. }
  140. // -------------------------------------------------------------------------
  141. // Check system stability
  142. // -------------------------------------------------------------------------
  143. #if SYSTEM_CHECK_ENABLED
  144. systemCheckLoop();
  145. #endif
  146. // -------------------------------------------------------------------------
  147. // Heartbeat
  148. // -------------------------------------------------------------------------
  149. if (_system_send_heartbeat && _heartbeat_mode == HEARTBEAT_ONCE) {
  150. heartbeat();
  151. _system_send_heartbeat = false;
  152. } else if (_heartbeat_mode == HEARTBEAT_REPEAT || _heartbeat_mode == HEARTBEAT_REPEAT_STATUS) {
  153. static unsigned long last_hbeat = 0;
  154. #if NTP_SUPPORT
  155. if ((_system_send_heartbeat && ntpSynced()) || (millis() - last_hbeat > _heartbeat_interval * 1000)) {
  156. #else
  157. if (_system_send_heartbeat || (millis() - last_hbeat > _heartbeat_interval * 1000)) {
  158. #endif
  159. last_hbeat = millis();
  160. heartbeat();
  161. _system_send_heartbeat = false;
  162. }
  163. }
  164. // -------------------------------------------------------------------------
  165. // Load Average calculation
  166. // -------------------------------------------------------------------------
  167. static unsigned long last_loadcheck = 0;
  168. static unsigned long load_counter_temp = 0;
  169. load_counter_temp++;
  170. if (millis() - last_loadcheck > LOADAVG_INTERVAL) {
  171. static unsigned long load_counter = 0;
  172. static unsigned long load_counter_max = 1;
  173. load_counter = load_counter_temp;
  174. load_counter_temp = 0;
  175. if (load_counter > load_counter_max) {
  176. load_counter_max = load_counter;
  177. }
  178. _load_average = 100 - (100 * load_counter / load_counter_max);
  179. last_loadcheck = millis();
  180. }
  181. }
  182. void _systemSetupSpecificHardware() {
  183. //The ESPLive has an ADC MUX which needs to be configured.
  184. #if defined(MANCAVEMADE_ESPLIVE)
  185. pinMode(16, OUTPUT);
  186. digitalWrite(16, HIGH); //Defualt CT input (pin B, solder jumper B)
  187. #endif
  188. // These devices use the hardware UART
  189. // to communicate to secondary microcontrollers
  190. #if (RF_SUPPORT && !RFB_DIRECT) || (RELAY_PROVIDER == RELAY_PROVIDER_DUAL) || (RELAY_PROVIDER == RELAY_PROVIDER_STM)
  191. Serial.begin(SERIAL_BAUDRATE);
  192. #endif
  193. }
  194. void systemSetup() {
  195. #if SPIFFS_SUPPORT
  196. SPIFFS.begin();
  197. #endif
  198. // Question system stability
  199. #if SYSTEM_CHECK_ENABLED
  200. systemCheck(false);
  201. #endif
  202. #if WEB_SUPPORT
  203. wsOnReceiveRegister(_systemWebSocketOnReceive);
  204. #endif
  205. // Init device-specific hardware
  206. _systemSetupSpecificHardware();
  207. // Register Loop
  208. espurnaRegisterLoop(systemLoop);
  209. // Cache Heartbeat values
  210. _systemSetupHeartbeat();
  211. }