From 11dd43144e117d82c756f89e607f20a4bd6407bd Mon Sep 17 00:00:00 2001 From: Felix Rusu Date: Sun, 6 May 2018 20:29:10 -0400 Subject: [PATCH] Support for MoteinoM0 and GUI v1.6 --- RFM69_OTA.cpp | 68 +++++++++++++++++++++++++++++++++++++++------------ RFM69_OTA.h | 24 +++++++++--------- 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/RFM69_OTA.cpp b/RFM69_OTA.cpp index a2f826a..8804143 100644 --- a/RFM69_OTA.cpp +++ b/RFM69_OTA.cpp @@ -30,12 +30,18 @@ // Please maintain this license information along with authorship // and copyright notices in any redistribution of this code // ********************************************************************************** -#ifdef __AVR__ #include #include -#include +#ifdef __AVR__ + #include +#endif +#ifdef MOTEINO_ZERO + #if defined(SERIAL_PORT_USBVIRTUAL) + #define Serial SERIAL_PORT_USBVIRTUAL // output on SerialUSB instead of Serial + #endif +#endif //=================================================================================================================== // CheckForWirelessHEX() - Checks whether the last message received was a wireless programming request handshake // If so it will start the handshake protocol, receive the new HEX image and @@ -115,7 +121,7 @@ uint8_t HandleWirelessHEXData(RFM69& radio, uint8_t remoteID, SPIFlash& flash, u uint16_t tmp,seq=0; char buffer[16]; uint16_t timeout = 3000; //3s for flash data - uint16_t bytesFlashed=10; + #ifndef SHIFTCHANNEL HandleHandshakeACK(radio, flash); if (DEBUG) Serial.println(F("FLX?OK (ACK sent)")); @@ -124,8 +130,15 @@ uint8_t HandleWirelessHEXData(RFM69& radio, uint8_t remoteID, SPIFlash& flash, u //first clear the fist 32k block (dedicated to a new FLASH image) flash.blockErase32K(0); flash.writeBytes(0,"FLXIMG:", 7); +#if defined (MOTEINO_ZERO) + flash.writeByte(10,':'); + uint32_t bytesFlashed=11; +#else flash.writeByte(9,':'); + uint32_t bytesFlashed=10; +#endif now=millis(); + pinMode(LEDpin,OUTPUT); while(1) { @@ -133,6 +146,7 @@ uint8_t HandleWirelessHEXData(RFM69& radio, uint8_t remoteID, SPIFlash& flash, u { uint8_t dataLen = radio.DATALEN; + digitalWrite(LEDpin,HIGH); if (dataLen >= 4 && radio.DATA[0]=='F' && radio.DATA[1]=='L' && radio.DATA[2]=='X') { if (radio.DATA[3]==':' && dataLen >= 7) //FLX:_:_ @@ -196,7 +210,14 @@ uint8_t HandleWirelessHEXData(RFM69& radio, uint8_t remoteID, SPIFlash& flash, u } if (dataLen==7 && radio.DATA[4]=='E' && radio.DATA[5]=='O' && radio.DATA[6]=='F') //Expected EOF { -#ifdef __AVR_ATmega1284P__ + +#if defined (MOTEINO_ZERO) + if ((bytesFlashed-10)>253952) { //max 65536 - 10 bytes (signature) + if (DEBUG) Serial.println(F("IMG > 64k, too big")); + radio.sendACK("FLX?NOK:HEX>64k",15); + return false; //just return, let MAIN timeout + } +#elif defined(__AVR_ATmega1284P__) if ((bytesFlashed-10)>65526) { //max 65536 - 10 bytes (signature) if (DEBUG) Serial.println(F("IMG > 64k, too big")); radio.sendACK("FLX?NOK:HEX>64k",15); @@ -212,16 +233,21 @@ uint8_t HandleWirelessHEXData(RFM69& radio, uint8_t remoteID, SPIFlash& flash, u HandleHandshakeACK(radio, flash, false); if (DEBUG) Serial.println(F("FLX?OK")); //save # of bytes written +#ifdef MOTEINO_ZERO + flash.writeByte(7,(bytesFlashed-11)>>16); + flash.writeByte(8,(bytesFlashed-11)>>8); + flash.writeByte(9,(bytesFlashed-11)); + //flash.writeByte(10,':'); //already done +#else flash.writeByte(7,(bytesFlashed-10)>>8); flash.writeByte(8,(bytesFlashed-10)); - flash.writeByte(9,':'); + //flash.writeByte(9,':'); //already done +#endif return true; } } } - #ifdef LED //blink! - pinMode(LEDpin,OUTPUT); digitalWrite(LEDpin,HIGH); delay(1); digitalWrite(LEDpin,LOW); - #endif + digitalWrite(LEDpin,LOW); } //abort FLASH sequence if no valid packet received for a long time @@ -358,7 +384,7 @@ uint8_t HandleSerialHEXData(RFM69& radio, uint8_t targetID, uint16_t TIMEOUT, ui index++; uint8_t hexDataLen = validateHEXData(input+index, inputLen-index); - if (hexDataLen>0) + if (hexDataLen>0 && hexDataLen<253) { if (tmp==seq) //only read data when packet number is the next expected SEQ number { @@ -375,7 +401,8 @@ uint8_t HandleSerialHEXData(RFM69& radio, uint8_t targetID, uint16_t TIMEOUT, ui else return false; } } - else Serial.println(F("FLX:INV")); + //else Serial.print(F("FLX:INV")); + else { Serial.print(F("FLX:INV:"));Serial.println(hexDataLen); } } if (inputLen==7 && input[3]=='?' && input[4]=='E' && input[5]=='O' && input[6]=='F') { @@ -414,24 +441,24 @@ uint8_t validateHEXData(void* data, uint8_t length) for (uint8_t i=0; i=48 && input[i]<=57) || (input[i] >=65 && input[i]<=70))) //0-9,A-F - return 0; + return 255; if (i%2 && iCTRL.reg = 0; // disable watchdog + while (WDT->STATUS.bit.SYNCBUSY == 1); // sync is required + WDT->CONFIG.reg = 0; // see Table 18.8.2 Timeout Period (valid values 0-11) + WDT->CTRL.reg = WDT_CTRL_ENABLE; //enable WDT + while (WDT->STATUS.bit.SYNCBUSY == 1); + WDT->CLEAR.reg= 0x00; // system reset via WDT + while (WDT->STATUS.bit.SYNCBUSY == 1); +#endif +} \ No newline at end of file diff --git a/RFM69_OTA.h b/RFM69_OTA.h index 5fe0e04..baa4314 100644 --- a/RFM69_OTA.h +++ b/RFM69_OTA.h @@ -2,11 +2,12 @@ // Library for OTA wireless programming of Moteinos using an RFM69 transceiver // ********************************************************************************** // Hardware requirements: -// - DualOptiboot bootloader - ships with all Moteinos -// - SPI "Flash MEM" chip on Moteino (optional) +// - DualOptiboot bootloader - ships with all AVR Moteino boards +// - LowPowerLab Samba MultiBoot - ships with all ARM SAMD21 MoteinoM0 boards +// - SPI "Flash MEM" chip on Moteino // Library requirements: // - RFM69 - get library at: https://github.com/LowPowerLab/RFM69 -// - SPIFLash.h - get it here: http://github.com/LowPowerLab/SPIFlash +// - SPIFlash.h - get it here: http://github.com/LowPowerLab/SPIFlash // ********************************************************************************** // Copyright LowPowerLab LLC 2018, https://www.LowPowerLab.com/contact // ********************************************************************************** @@ -30,15 +31,20 @@ // Please maintain this license information along with authorship // and copyright notices in any redistribution of this code // ********************************************************************************** -#ifdef __AVR__ - #ifndef RFM69_OTA_H #define RFM69_OTA_H -#ifdef __AVR_ATmega1284P__ + +#include +#include + +#if defined(MOTEINO_ZERO) + #define LED 13 // Moteino M0 +#elif defined(__AVR_ATmega1284P__) #define LED 15 // Moteino MEGAs have LEDs on D15 -#else +#elif defined (__AVR_ATmega328P__) #define LED 9 // Moteinos have LEDs on D9 #endif + #define SHIFTCHANNEL 1000000 //amount to shift frequency of HEX transmission to keep original channel free of the HEX transmission traffic #ifndef DEFAULT_TIMEOUT @@ -49,9 +55,6 @@ #define ACK_TIMEOUT 20 #endif -#include -#include - //functions used in the REMOTE node void CheckForWirelessHEX(RFM69& radio, SPIFlash& flash, uint8_t DEBUG=false, uint8_t LEDpin=LED); void HandleHandshakeACK(RFM69& radio, SPIFlash& flash, uint8_t flashCheck=true); @@ -78,5 +81,4 @@ uint8_t BYTEfromHEX(char MSB, char LSB); uint8_t readSerialLine(char* input, char endOfLineChar=10, uint8_t maxLength=115, uint16_t timeout=1000); void PrintHex83(uint8_t* data, uint8_t length); -#endif #endif \ No newline at end of file