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.

276 lines
7.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 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. if (strncmp(key, "sys", 3) == 0) return true;
  131. if (strncmp(key, "hb", 2) == 0) return true;
  132. return false;
  133. }
  134. #endif
  135. void systemLoop() {
  136. // -------------------------------------------------------------------------
  137. // User requested reset
  138. // -------------------------------------------------------------------------
  139. if (checkNeedsReset()) {
  140. reset();
  141. }
  142. // -------------------------------------------------------------------------
  143. // Check system stability
  144. // -------------------------------------------------------------------------
  145. #if SYSTEM_CHECK_ENABLED
  146. systemCheckLoop();
  147. #endif
  148. // -------------------------------------------------------------------------
  149. // Heartbeat
  150. // -------------------------------------------------------------------------
  151. if (_system_send_heartbeat && _heartbeat_mode == HEARTBEAT_ONCE) {
  152. heartbeat();
  153. _system_send_heartbeat = false;
  154. } else if (_heartbeat_mode == HEARTBEAT_REPEAT || _heartbeat_mode == HEARTBEAT_REPEAT_STATUS) {
  155. static unsigned long last_hbeat = 0;
  156. #if NTP_SUPPORT
  157. if ((_system_send_heartbeat && ntpSynced()) || (millis() - last_hbeat > _heartbeat_interval * 1000)) {
  158. #else
  159. if (_system_send_heartbeat || (millis() - last_hbeat > _heartbeat_interval * 1000)) {
  160. #endif
  161. last_hbeat = millis();
  162. heartbeat();
  163. _system_send_heartbeat = false;
  164. }
  165. }
  166. // -------------------------------------------------------------------------
  167. // Load Average calculation
  168. // -------------------------------------------------------------------------
  169. static unsigned long last_loadcheck = 0;
  170. static unsigned long load_counter_temp = 0;
  171. load_counter_temp++;
  172. if (millis() - last_loadcheck > LOADAVG_INTERVAL) {
  173. static unsigned long load_counter = 0;
  174. static unsigned long load_counter_max = 1;
  175. load_counter = load_counter_temp;
  176. load_counter_temp = 0;
  177. if (load_counter > load_counter_max) {
  178. load_counter_max = load_counter;
  179. }
  180. _load_average = 100 - (100 * load_counter / load_counter_max);
  181. last_loadcheck = millis();
  182. }
  183. }
  184. void _systemSetupSpecificHardware() {
  185. //The ESPLive has an ADC MUX which needs to be configured.
  186. #if defined(MANCAVEMADE_ESPLIVE)
  187. pinMode(16, OUTPUT);
  188. digitalWrite(16, HIGH); //Defualt CT input (pin B, solder jumper B)
  189. #endif
  190. // These devices use the hardware UART
  191. // to communicate to secondary microcontrollers
  192. #if (RF_SUPPORT && !RFB_DIRECT) || (RELAY_PROVIDER == RELAY_PROVIDER_DUAL) || (RELAY_PROVIDER == RELAY_PROVIDER_STM)
  193. Serial.begin(SERIAL_BAUDRATE);
  194. #endif
  195. }
  196. void systemSetup() {
  197. #if SPIFFS_SUPPORT
  198. SPIFFS.begin();
  199. #endif
  200. // Question system stability
  201. #if SYSTEM_CHECK_ENABLED
  202. systemCheck(false);
  203. #endif
  204. #if WEB_SUPPORT
  205. wsOnReceiveRegister(_systemWebSocketOnReceive);
  206. #endif
  207. // Init device-specific hardware
  208. _systemSetupSpecificHardware();
  209. // Register Loop
  210. espurnaRegisterLoop(systemLoop);
  211. // Cache Heartbeat values
  212. _systemSetupHeartbeat();
  213. }