]> git.tnoah.ca Git - lora-radio.git/commitdiff
Added boot diagnostics
authorMark Qvist <mark@unsigned.io>
Sun, 26 Dec 2021 10:27:32 +0000 (11:27 +0100)
committerMark Qvist <mark@unsigned.io>
Sun, 26 Dec 2021 10:27:32 +0000 (11:27 +0100)
Config.h
RNode_Firmware.ino
Utilities.h

index 179ead65352804f9e89800e5d6df232c81d7a2fb..af7dff8a2b5836b4269ce812fdd1894966b244a8 100644 (file)
--- a/Config.h
+++ b/Config.h
@@ -4,7 +4,7 @@
        #define CONFIG_H
 
        #define MAJ_VERS  0x01
-       #define MIN_VERS  0x11
+       #define MIN_VERS  0x12
 
        #define MCU_1284P 0x91
 
        const uint8_t SIG_SYNCED = 0x02;
        const uint8_t RX_ONGOING = 0x04;
 
+       // Boot flags
+       #define START_FROM_BOOTLOADER 0x01
+       #define START_FROM_POWERON 0x02
+       #define START_FROM_BROWNOUT 0x03
+       #define START_FROM_JTAG 0x04
+
 #endif
\ No newline at end of file
index d7a562e22ed13454e9ab2d8e9161e38360dfbf55..dfcaae4c13532ebf5cd01c43088b3097b5e01513 100644 (file)
@@ -562,22 +562,39 @@ void checkModemStatus() {
 }
 
 void validateStatus() {
-  if (eeprom_lock_set()) {
-    if (eeprom_product_valid() && eeprom_model_valid() && eeprom_hwrev_valid()) {
-      if (eeprom_checksum_valid()) {
-        hw_ready = true;
-
-        if (eeprom_have_conf()) {
-          eeprom_conf_load();
-          op_mode = MODE_TNC;
-          startRadio();
+  if (OPTIBOOT_MCUSR & (1<<PORF)) {
+    boot_vector = START_FROM_POWERON;
+  } else if (OPTIBOOT_MCUSR & (1<<BORF)) {
+    boot_vector = START_FROM_BROWNOUT;
+  } else if (OPTIBOOT_MCUSR & (1<<WDRF)) {
+    boot_vector = START_FROM_BOOTLOADER;
+  } else {
+      Serial.write("Error, indeterminate boot vector\r\n");
+      led_indicate_boot_error();
+  }
+
+  if (boot_vector == START_FROM_BOOTLOADER) {
+    if (eeprom_lock_set()) {
+      if (eeprom_product_valid() && eeprom_model_valid() && eeprom_hwrev_valid()) {
+        if (eeprom_checksum_valid()) {
+          hw_ready = true;
+
+          if (eeprom_have_conf()) {
+            eeprom_conf_load();
+            op_mode = MODE_TNC;
+            startRadio();
+          }
         }
+      } else {
+        hw_ready = false;
       }
     } else {
       hw_ready = false;
     }
   } else {
     hw_ready = false;
+    Serial.write("Error, incorrect boot vector\r\n");
+    led_indicate_boot_error();
   }
 }
 
index 5c52b5321bd6e15d843b78f98019858543f370b8..2431e1fb14a59fd0532c9e7c0724aec2aff375da 100644 (file)
@@ -7,6 +7,13 @@
 #include "Framing.h"
 #include "MD5.h"
 
+uint8_t boot_vector = 0x00;
+uint8_t OPTIBOOT_MCUSR __attribute__ ((section(".noinit")));
+void resetFlagsInit(void) __attribute__ ((naked)) __attribute__ ((used)) __attribute__ ((section (".init0")));
+void resetFlagsInit(void) {
+    __asm__ __volatile__ ("sts %0, r2\n" : "=m" (OPTIBOOT_MCUSR) :);
+}
+
 void led_rx_on()  { digitalWrite(pin_led_rx, HIGH); }
 void led_rx_off() {    digitalWrite(pin_led_rx, LOW); }
 void led_tx_on()  { digitalWrite(pin_led_tx, HIGH); }
@@ -28,6 +35,17 @@ void led_indicate_error(int cycles) {
     digitalWrite(pin_led_tx, LOW);
 }
 
+void led_indicate_boot_error() {
+       while (true) {
+           led_tx_on();
+           led_rx_off();
+           delay(10);
+           led_rx_on();
+           led_tx_off();
+           delay(5);
+       }
+}
+
 void led_indicate_warning(int cycles) {
        bool forever = (cycles == 0) ? true : false;
        cycles = forever ? 1 : cycles;