From 720f34d41f9206da113742a63343a08982acec97 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Wed, 20 Jun 2018 16:32:30 +0200 Subject: [PATCH] EEPROM config and TNC mode --- Config.h | 6 +++--- Framing.h | 25 ++----------------------- LoRa.cpp | 12 ++++++------ LoRa.h | 2 +- RNode_Firmware.ino | 9 +++++++++ Utilities.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 66 insertions(+), 34 deletions(-) diff --git a/Config.h b/Config.h index ec87d76..ecdafcc 100644 --- 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; diff --git a/Framing.h b/Framing.h index eeebdf5..fcccb3c 100644 --- 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 @@ -53,26 +54,4 @@ 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 diff --git a/LoRa.cpp b/LoRa.cpp index 61166dd..1736d70 100644 --- 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 daaa77f..25b24f2 100644 --- 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(); diff --git a/RNode_Firmware.ino b/RNode_Firmware.ino index 9a9e2a6..cc8cdee 100644 --- a/RNode_Firmware.ino +++ b/RNode_Firmware.ino @@ -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; diff --git a/Utilities.cpp b/Utilities.cpp index 7417086..ba8da4c 100644 --- a/Utilities.cpp +++ b/Utilities.cpp @@ -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();