]> git.tnoah.ca Git - lora-radio.git/commitdiff
EEPROM config and TNC mode
authorMark Qvist <mark@adepta.io>
Wed, 20 Jun 2018 14:32:30 +0000 (16:32 +0200)
committerMark Qvist <mark@adepta.io>
Wed, 20 Jun 2018 14:32:30 +0000 (16:32 +0200)
Config.h
Framing.h
LoRa.cpp
LoRa.h
RNode_Firmware.ino
Utilities.cpp

index ec87d76177488068187bacdd34992195bae09e94..ecdafcc743b45ca2682678ef29478ccb8d4936d8 100644 (file)
--- a/Config.h
+++ b/Config.h
@@ -62,9 +62,9 @@
        const int lora_rx_turnaround_ms = 50;
 
        // Default LoRa settings
-       int  lora_sf   = 0;
-       int  lora_cr   = 5;
-       int  lora_txp  = 0xFF;
+       int  lora_sf       = 0;
+       int  lora_cr       = 5;
+       int  lora_txp      = 0xFF;
        uint32_t lora_bw   = 0;
        uint32_t lora_freq = 0;
 
index eeebdf50099afcd705c2d65265071796663697a7..fcccb3c8b5c99714d8396c38ebf6647f69f6f523 100644 (file)
--- a/Framing.h
+++ b/Framing.h
@@ -28,6 +28,7 @@
        #define CMD_ROM_READ    0x51
        #define CMD_ROM_WRITE   0x52
        #define CMD_CONF_SAVE   0x53
+       #define CMD_CONF_DELETE 0x54
        #define CMD_UNLOCK_ROM  0x59
        #define ROM_UNLOCK_BYTE 0xF8
 
        bool ESCAPE = false;
        uint8_t command = CMD_UNKNOWN;
 
-#endif
-
-/*
-Frequency 433.200              0xc00119d21b80c0
-Bandwidth 20.800               0xc00200005140c0
-TX Power 8dbm                  0xc00308c0
-SF 7                                   0xc00407c0
-
-All:                                   0xc00119d21b80c00200005140c00308c00407c0
-
-Radio on                               0xc00501c0
-
-Config+on                              0xc00119d21b80c00200005140c00301c00407c00601c0
-
-
-               c1 = self.bandwidth >> 24
-               c2 = self.bandwidth >> 16 & 0xFF
-               c3 = self.bandwidth >> 8 & 0xFF
-               c4 = self.bandwidth & 0xFF
-               data = KISS.escape(chr(c1)+chr(c2)+chr(c3)+chr(c4))
-
-
-*/
\ No newline at end of file
+#endif
\ No newline at end of file
index 61166ddc8a39fe73126ba1fb07be9f5fa172b204..1736d7060454ec71682548fa05ade54276a974fb 100644 (file)
--- a/LoRa.cpp
+++ b/LoRa.cpp
@@ -370,24 +370,24 @@ void LoRaClass::setTxPower(int level, int outputPin)
   }
 }
 
-void LoRaClass::setFrequency(long frequency)
-{
+void LoRaClass::setFrequency(long frequency) {
   _frequency = frequency;
 
-  uint64_t frf = ((uint64_t)frequency << 19) / 32000000;
+  uint32_t frf = ((uint64_t)frequency << 19) / 32000000;
 
   writeRegister(REG_FRF_MSB, (uint8_t)(frf >> 16));
   writeRegister(REG_FRF_MID, (uint8_t)(frf >> 8));
   writeRegister(REG_FRF_LSB, (uint8_t)(frf >> 0));
 }
 
-long LoRaClass::getFrequency() {
+uint32_t LoRaClass::getFrequency() {
   uint8_t msb = readRegister(REG_FRF_MSB);
   uint8_t mid = readRegister(REG_FRF_MID);
   uint8_t lsb = readRegister(REG_FRF_LSB);
 
-  uint64_t frf = msb << 16 | mid << 8 | lsb;
-  long frequency = (uint64_t)(frf*32000000) >> 19;
+  uint32_t frf = ((uint32_t)msb << 16) | ((uint32_t)mid << 8) | (uint32_t)lsb;
+  uint64_t frm = (uint64_t)frf*32000000;
+  uint32_t frequency = (frm >> 19);
 
   return frequency;
 }
diff --git a/LoRa.h b/LoRa.h
index daaa77fe6dd2140e3c8ff58b5b400ec99e47051a..25b24f2787904ba4e9ff0f0e1e98db779fb5244f 100644 (file)
--- a/LoRa.h
+++ b/LoRa.h
@@ -46,7 +46,7 @@ public:
   void sleep();
 
   void setTxPower(int level, int outputPin = PA_OUTPUT_PA_BOOST_PIN);
-  long getFrequency();
+  uint32_t getFrequency();
   void setFrequency(long frequency);
   void setSpreadingFactor(int sf);
   long getSignalBandwidth();
index 9a9e2a604ce7f5b7edecac587f9628f47cfd5a70..cc8cdee65d787255c2e8f30701c3af63e0c540e4 100644 (file)
@@ -344,6 +344,10 @@ void serialCallback(uint8_t sbyte) {
         }
     } else if (command == CMD_FW_VERSION) {
       kiss_indicate_version();
+    } else if (command == CMD_CONF_SAVE) {
+      eeprom_conf_save();
+    } else if (command == CMD_CONF_DELETE) {
+      eeprom_conf_delete();
     }
   }
 }
@@ -390,6 +394,11 @@ void validateStatus() {
     if (eeprom_product_valid() && eeprom_model_valid() && eeprom_hwrev_valid()) {
       if (eeprom_checksum_valid()) {
         hw_ready = true;
+
+        if (eeprom_have_conf()) {
+          eeprom_conf_load();
+          startRadio();
+        }
       }
     } else {
       hw_ready = false;
index 74170869ae1c79d340cdc04884ea122860e3e36d..ba8da4ce37cf253abd4b61a35e33580c5a75bb47 100644 (file)
@@ -228,7 +228,6 @@ void getPacketData(int len) {
        }
 }
 
-
 void setSpreadingFactor() {
        if (radio_online) LoRa.setSpreadingFactor(lora_sf);
 }
@@ -387,6 +386,51 @@ bool eeprom_checksum_valid() {
        return checksum_valid;
 }
 
+bool eeprom_have_conf() {
+       if (EEPROM.read(eeprom_addr(ADDR_CONF_OK)) == CONF_OK_BYTE) {
+               return true;
+       } else {
+               return false;
+       }
+}
+
+void eeprom_conf_load() {
+       if (eeprom_have_conf()) {
+               lora_sf = EEPROM.read(eeprom_addr(ADDR_CONF_SF));
+               lora_cr = EEPROM.read(eeprom_addr(ADDR_CONF_CR));
+               lora_txp = EEPROM.read(eeprom_addr(ADDR_CONF_TXP));
+               lora_freq = (uint32_t)EEPROM.read(eeprom_addr(ADDR_CONF_FREQ)+0x00) << 24 | (uint32_t)EEPROM.read(eeprom_addr(ADDR_CONF_FREQ)+0x01) << 16 | (uint32_t)EEPROM.read(eeprom_addr(ADDR_CONF_FREQ)+0x02) << 8 | (uint32_t)EEPROM.read(eeprom_addr(ADDR_CONF_FREQ)+0x03);
+               lora_bw = (uint32_t)EEPROM.read(eeprom_addr(ADDR_CONF_BW)+0x00) << 24 | (uint32_t)EEPROM.read(eeprom_addr(ADDR_CONF_BW)+0x01) << 16 | (uint32_t)EEPROM.read(eeprom_addr(ADDR_CONF_BW)+0x02) << 8 | (uint32_t)EEPROM.read(eeprom_addr(ADDR_CONF_BW)+0x03);
+       }
+}
+
+void eeprom_conf_save() {
+       if (hw_ready && radio_online) {
+               EEPROM.update(eeprom_addr(ADDR_CONF_SF), lora_sf);
+               EEPROM.update(eeprom_addr(ADDR_CONF_CR), lora_cr);
+               EEPROM.update(eeprom_addr(ADDR_CONF_TXP), lora_txp);
+
+               EEPROM.update(eeprom_addr(ADDR_CONF_BW)+0x00, lora_bw>>24);
+               EEPROM.update(eeprom_addr(ADDR_CONF_BW)+0x01, lora_bw>>16);
+               EEPROM.update(eeprom_addr(ADDR_CONF_BW)+0x02, lora_bw>>8);
+               EEPROM.update(eeprom_addr(ADDR_CONF_BW)+0x03, lora_bw);
+
+               EEPROM.update(eeprom_addr(ADDR_CONF_FREQ)+0x00, lora_freq>>24);
+               EEPROM.update(eeprom_addr(ADDR_CONF_FREQ)+0x01, lora_freq>>16);
+               EEPROM.update(eeprom_addr(ADDR_CONF_FREQ)+0x02, lora_freq>>8);
+               EEPROM.update(eeprom_addr(ADDR_CONF_FREQ)+0x03, lora_freq);
+
+               EEPROM.update(eeprom_addr(ADDR_CONF_OK), CONF_OK_BYTE);
+               led_indicate_info(10);
+       } else {
+               led_indicate_warning(10);
+       }
+}
+
+void eeprom_conf_delete() {
+       EEPROM.update(eeprom_addr(ADDR_CONF_OK), 0x00);
+}
+
 void unlock_rom() {
        led_indicate_error(50);
        eeprom_erase();