Commit 051c67b5 authored by Daniil Kukushkin's avatar Daniil Kukushkin

STM OTA refactoring and flashing speed optimizations

parent fdcf8f8a
*.bat
*.txt
\ No newline at end of file
......@@ -114,7 +114,7 @@ void MainController::initializeSystem()
openAsyncUDP();
STM32_OTA::begin();
STM_OTA_UDP_Receiver::start(STM32_OTA_UDP_PORT, STM32_OTA_SERVER_UDP_PORT);
WiFi.onEvent(WiFiEvent);
localIP = WiFi.softAPIP();
......@@ -125,11 +125,12 @@ void MainController::initializeSystem()
ArduinoOTA.onStart([]() {
//Serial.println("Start");
STM32_OTA::STMtoResetState();
STM_BootManager::toResetState();
});
ArduinoOTA.onEnd([]() {
//Serial.println("\nEnd");
STM32_OTA::resetSTM();
STM_BootManager::reset();
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
//Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
......@@ -348,7 +349,7 @@ void MainController::setLEDsColors(uint8_t forwardR, uint8_t forwardG, uint8_t f
bc.B = backwardB;
bc.mode = COLOR_NOT_SAVE_MODE;
//for(int i = 0; i < 5; i++) {
setParam("FrontLed", fc.floatParamValue);
setParam("FrontLed", fc.floatParamValue);
setParam("BackLed", bc.floatParamValue);
//}
}
......@@ -483,15 +484,18 @@ uint8_t* MainController::grabSerialAndRetransmit()
void MainController::mainServiceRoutine()
{
if(STM32_OTA::isOTA_Request())
if(STM_OTA_UDP_Receiver::isOTA_Request())
{
os_timer_disarm(&stmToAppRetransmitTimer);
asyncUdpBlocked = true;
STM32_OTA::handle();
STM_OTA_UDP_Receiver::handle();
Serial.end();
Serial.begin(UART_BAUDRATE);
asyncUdpBlocked = false;
os_timer_arm(&stmToAppRetransmitTimer, 1000/RETRANSMIT_TIMER_FREQ, true);
os_timer_arm(&stmToAppRetransmitTimer, 1000 / RETRANSMIT_TIMER_FREQ, true);
STM_OTA_UDP_Receiver::restart();
}
ArduinoOTA.handle();
......@@ -501,9 +505,11 @@ void MainController::mainServiceRoutine()
loopFrequency++;
currentMillis = millis();
if (currentMillis - prevMillis >= measureIntervalMillis) {
if (currentMillis - prevMillis >= measureIntervalMillis)
{
if (pixReceiverCount > 0) {
if (pixReceiverCount > 0)
{
Udp.writeTo(pixReceiverBuffer, pixReceiverCount, controllerIP, controllerUdpListenerPort);
pixReceiverCount = 0;
}
......@@ -512,16 +518,17 @@ void MainController::mainServiceRoutine()
loopFrequency = 0;
udpReadFrequency = 0;
if(isSomeConnectedBefore == false) {
if(isSomeConnectedBefore == false)
{
noConnectSeconds++;
//settingsManager.sendString( "No connect seconds: " + String(noConnectSeconds) );
//Serial.println(noConnectSeconds);
if( (noConnectSeconds > RESET_SSID_PASS_SECONDS) && (!settingsManager.isDefaultPasswordNow()) )
{
settingsManager.resetSSID_PassToDefault();
STM32_OTA::STMtoResetState();
STM_BootManager::toResetState();
delay(2000);
STM32_OTA::resetSTM();
STM_BootManager::reset();
ESP.restart();
//
}
......@@ -556,7 +563,8 @@ void MainController::mainServiceRoutine()
void MainController::mainRoutine()
{
do {
do
{
mainServiceRoutine();
} while (isControlStationConnected == false);
}
......
......@@ -35,7 +35,8 @@
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "ESPAsyncUDP.h"
#include "STM32_OTA.h"
#include "stm_boot_manager.h"
#include "stm_ota_udp_receiver.h"
#include "settings_manager.h"
#include "Color.h"
......@@ -52,6 +53,9 @@ namespace NanopixCore {
#define UART_BAUDRATE 460800
#endif
#define STM32_OTA_UDP_PORT 5700
#define STM32_OTA_SERVER_UDP_PORT 5705
#define RESET_SSID_PASS_SECONDS 60*5
#define CMD_LOOP_FREQ 19
#define CMD_UDP_RX_FREQ 20
......
This diff is collapsed.
......@@ -30,52 +30,47 @@
#ifndef STM32_OTA_H
#define STM32_OTA_H
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "stdint.h"
#include "SerialLink.h"
namespace NanopixCore {
#define RX_BUFFER_SIZE 255
class STM32_OTA {
public:
enum BootloaderAckStatus { STATUS_UNINITIALIZED = -1, BOOTLOADER_ACK_FAIL = 0, BOOTLOADER_ACK_OK, BOOTLOADER_ACK_TIMEOUT };
private:
static bool isOTA_Update;
static WiFiUDP UdpOTA;
static byte rxBuffer[RX_BUFFER_SIZE];
static IPAddress remoteIPaddr;
static SerialLink serial_link;
static bool startAddrIsInit, runAddrIsInit;
static byte startAddrHighMSB, startAddrHighLSB;
static byte runAddrHighMSB, runAddrHighLSB, runAddrLowMSB, runAddrLowLSB; // 4 byte run address
static uint8_t startAddrHighMSB, startAddrHighLSB;
static uint8_t runAddrHighMSB, runAddrHighLSB, runAddrLowMSB, runAddrLowLSB; // 4 byte run address
static BootloaderAckStatus waitBootloaderAck(int timeout = 100000);
enum OTA_Commands { EnterBOOT = 0, StartUpload, StartVerify, GlobalErase, FinishUpdate, ReadoutUnprotect, WriteUnprotect };
enum UpdateStates {UploadFlash = 0, VerifyFlash};
static UpdateStates currentState;
public:
static const int ACK_TOKEN_BYTE = 0x79;
static void startUpdate();
static void finishUpdate();
static bool waitBootloaderAck(int timeout = 100000);
static bool writeReceivedBlock();
static bool verifyReceivedBlock();
static void parceSettingsRecord();
static void sendAddress(byte addrMSB, byte addrLSB);
static bool enterToBOOT();
static bool globalErase(); // for STM32F1
static bool globalEraseEx(); // for STM32F3
static bool setReadoutUnprotect();
static bool setWriteUnprotect();
static void sendLog(const char* str);
public:
static BootloaderAckStatus writeReceivedBlock(uint8_t* packet_data);
static BootloaderAckStatus verifyReceivedBlock(uint8_t* packet_data);
static void parceSettingsRecord(uint8_t* packet_data);
static void sendAddress(uint8_t addrMSB, uint8_t addrLSB);
static BootloaderAckStatus enterToBOOT();
static BootloaderAckStatus globalErase(); // for STM32F1
static BootloaderAckStatus globalEraseEx(); // for STM32F3
static BootloaderAckStatus setReadoutUnprotect();
static BootloaderAckStatus setWriteUnprotect();
static bool isOTA_Updating();
static void begin();
static bool handle();
static void resetSTM();
static void STMtoResetState();
static bool isOTA_Request();
static void STM_ToBootMode();
static void sendResponce(byte val);
};
}
......
/*
Copyright (C) 2018 Nanopix
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Nanopix nor the names
of its contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "SerialLink.h"
#include "Arduino.h"
namespace NanopixCore
{
void SerialLink::begin(int baudrate)
{
Serial.begin(baudrate, SERIAL_8E1);
}
void SerialLink::end()
{
Serial.end();
}
void SerialLink::write(uint8_t val)
{
Serial.write(val);
}
void SerialLink::write(uint8_t* buf, int size)
{
Serial.write(buf, size);
}
uint8_t SerialLink::read()
{
return Serial.read();
}
bool SerialLink::available()
{
return Serial.available();
}
}
\ No newline at end of file
/*
Copyright (C) 2018 Nanopix
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Nanopix nor the names
of its contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#ifndef __SERIAL_LINK_H_
#define __SERIAL_LINK_H_
#include "stdint.h"
namespace NanopixCore
{
class SerialLink {
public:
void begin(int baudrate);
void end();
void write(uint8_t* buf, int size);
void write(uint8_t val);
uint8_t read();
bool available();
};
}
#endif /* __SERIAL_LINK_H_ */
\ No newline at end of file
/*
Copyright (C) 2018 Nanopix
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Nanopix nor the names
of its contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "platform_hal.h"
#include "Arduino.h"
void delayMs(int ms)
{
delay(ms);
}
/*
Copyright (C) 2018 Nanopix
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Nanopix nor the names
of its contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#ifndef __PLATFORM_HAL_H_
#define __PLATFORM_HAL_H_
void delayMs(int ms);
#endif // __PLATFORM_HAL_H_
/*
Copyright (C) 2018 Nanopix
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Nanopix nor the names
of its contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "stm_boot_manager.h"
#include "Arduino.h"
namespace NanopixCore
{
#define STM32_RESET_GPIO 4
#define STM32_BOOT_GPIO 5
void STM_BootManager::toResetState()
{
pinMode(STM32_RESET_GPIO, OUTPUT);
digitalWrite(STM32_RESET_GPIO, LOW);
}
void STM_BootManager::reset()
{
pinMode(STM32_RESET_GPIO, OUTPUT);
digitalWrite(STM32_RESET_GPIO, LOW);
delay(70);
pinMode(STM32_RESET_GPIO, INPUT);
digitalWrite(STM32_RESET_GPIO, HIGH);
}
void STM_BootManager::toBootMode()
{
pinMode(STM32_BOOT_GPIO, OUTPUT);
digitalWrite(STM32_BOOT_GPIO, HIGH);
reset();
delay(40);
pinMode(STM32_BOOT_GPIO, INPUT);
digitalWrite(STM32_BOOT_GPIO, LOW);
}
}
\ No newline at end of file
/*
Copyright (C) 2018 Nanopix
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Nanopix nor the names
of its contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#ifndef __STM_BOOT_MANAGER_H_
#define __STM_BOOT_MANAGER_H_
#include "stdint.h"
namespace NanopixCore
{
class STM_BootManager {
public:
static void reset();
static void toResetState();
static void toBootMode();
};
}
#endif /* __STM_BOOT_MANAGER_H_ */
\ No newline at end of file
/*
Copyright (C) 2018 Nanopix
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Nanopix nor the names
of its contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "stm_ota_udp_receiver.h"
#include "STM32_OTA.h"
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
namespace NanopixCore
{
static WiFiUDP udp_ota;
static IPAddress remote_ip_addr;
bool STM_OTA_UDP_Receiver::start(int udp_local_port, int udp_remote_port)
{
udp_ota.begin(udp_local_port);
STM32_OTA::begin();
_udp_local_port = udp_local_port;
_udp_remote_port = udp_remote_port;
_current_state = UploadFlash;
}
bool STM_OTA_UDP_Receiver::restart()
{
udp_ota.flush();
udp_ota.stop();
start(_udp_local_port, _udp_remote_port);
}
bool STM_OTA_UDP_Receiver::handle()
{
while (true)
{
int packet_size = udp_ota.parsePacket();
if (packet_size)
{
int len = udp_ota.read(_rx_buffer, RX_BUFFER_SIZE);
if (len == 1) // then it is command
{
remote_ip_addr = udp_ota.remoteIP();
STM32_OTA::BootloaderAckStatus res;
uint8_t command = _rx_buffer[0];
switch (command)
{
case EnterBOOT:
res = STM32_OTA::enterToBOOT();
sendLog(res == STM32_OTA::BOOTLOADER_ACK_OK ?
"Enter to BOOT mode - OK" :
"Enter to BOOT mode - FAILURE!");
break;
case StartUpload:
_current_state = UploadFlash;
STM32_OTA::startUpdate();
sendLog("STM OTA: start flash...");
break;
case StartVerify:
_current_state = VerifyFlash;
STM32_OTA::startUpdate();
sendLog("STM OTA: start verify...");
break;
case GlobalErase:
res = STM32_OTA::globalEraseEx();
if (res != STM32_OTA::BOOTLOADER_ACK_OK)
{
res = STM32_OTA::globalErase();
sendLog(res == STM32_OTA::BOOTLOADER_ACK_OK ?
"Extended global erase FAIL, standart OK" :
"Extended and standart global erase both FAILURE");
}
else
{
sendLog("Extended global erase OK");
}
break;
case ReadoutUnprotect:
res = STM32_OTA::setReadoutUnprotect();
sendLog(res == STM32_OTA::BOOTLOADER_ACK_OK ?
"Readout unprotect OK" :
"Readout unprotect FAILURE");
break;
case WriteUnprotect:
res = STM32_OTA::setWriteUnprotect();
sendLog(res == STM32_OTA::BOOTLOADER_ACK_OK ?
"Write unprotect OK" :
"Write unprotect FAILURE");
break;
case FinishUpdate:
STM32_OTA::finishUpdate();
return true;
default:
break;
}
}
else
{
////////////////////////////////
// TODO: need check packet CRC!!!!!!
////////////////////////////////
if (_rx_buffer[3] != 0) // then it is address settings packet
{
STM32_OTA::parceSettingsRecord(_rx_buffer);
sendResponce(STM32_OTA::ACK_TOKEN_BYTE); // TODO: move to stm32 ota
}
else
{
switch (_current_state)
{
case UploadFlash:
if (STM32_OTA::writeReceivedBlock(_rx_buffer) == STM32_OTA::BOOTLOADER_ACK_OK)
{
sendResponce(STM32_OTA::ACK_TOKEN_BYTE);
sendResponce(STM32_OTA::ACK_TOKEN_BYTE);
}
break;
case VerifyFlash:
if (STM32_OTA::verifyReceivedBlock(_rx_buffer) == STM32_OTA::BOOTLOADER_ACK_OK)
sendResponce(STM32_OTA::ACK_TOKEN_BYTE);
break;
}
}
}
}
else {
if (STM32_OTA::isOTA_Updating() == false)
break;
}
} // while true end
return false;
}
bool STM_OTA_UDP_Receiver::isOTA_Request()
{
return udp_ota.available();
}
void STM_OTA_UDP_Receiver::sendLog(const char* str)
{
udp_ota.beginPacket(remote_ip_addr, _udp_remote_port);
udp_ota.write(str);
udp_ota.endPacket();
}
void STM_OTA_UDP_Receiver::sendResponce(uint8_t val)
{
udp_ota.beginPacket(remote_ip_addr, _udp_remote_port);
String hex_string = String(val, HEX);
char char_var[sizeof(hex_string)];
hex_string.toCharArray(char_var, sizeof(char_var));
udp_ota.write(char_var);
udp_ota.endPacket();
}
STM_OTA_UDP_Receiver::UpdateStates STM_OTA_UDP_Receiver::_current_state;
uint8_t STM_OTA_UDP_Receiver::_rx_buffer[RX_BUFFER_SIZE];
int STM_OTA_UDP_Receiver::_udp_remote_port;
int STM_OTA_UDP_Receiver::_udp_local_port;
}
/*
Copyright (C) 2018 Nanopix
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Nanopix nor the names
of its contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#ifndef __STM_OTA_UDP_RECEIVER_H_
#define __STM_OTA_UDP_RECEIVER_H_
#include "stdint.h"
#define RX_BUFFER_SIZE 270
namespace NanopixCore {
class STM_OTA_UDP_Receiver {
public:
static bool start(int udp_local_port, int udp_remote_port);
static bool restart();
static bool handle();
static bool isOTA_Request();
static void sendLog(const char* str);
static void sendResponce(uint8_t val);
private:
enum OTA_Commands { EnterBOOT = 0, StartUpload, StartVerify, GlobalErase, FinishUpdate, ReadoutUnprotect, WriteUnprotect };
enum UpdateStates { UploadFlash = 0, VerifyFlash };
static UpdateStates _current_state;
static int _udp_remote_port, _udp_local_port;
static uint8_t _rx_buffer[RX_BUFFER_SIZE];
};