Skip to content

Commit 10c0345

Browse files
implemented unor4 ota class
1 parent 1623741 commit 10c0345

File tree

2 files changed

+201
-8
lines changed

2 files changed

+201
-8
lines changed

src/ota/implementation/OTAUnoR4.cpp

+163
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,167 @@
1+
/*
2+
This file is part of the ArduinoIoTCloud library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#include <AIoTC_Config.h>
12+
113
#if defined(ARDUINO_UNOR4_WIFI) && OTA_ENABLED
214
#include "OTAUnoR4.h"
315

16+
#include <Arduino_DebugUtils.h>
17+
#include "tls/utility/SHA256.h"
18+
#include "fsp_common_api.h"
19+
#include "r_flash_lp.h"
20+
#include "WiFi.h"
21+
22+
/******************************************************************************
23+
* DEFINES
24+
******************************************************************************/
25+
26+
const char UNOR4OTACloudProcess::UPDATE_FILE_NAME[] = "/update.bin";
27+
28+
static OTACloudProcessInterface::State convertUnor4ErrorToState(int error_code);
29+
30+
UNOR4OTACloudProcess::UNOR4OTACloudProcess(MessageStream *ms)
31+
: OTACloudProcessInterface(ms){
32+
33+
}
34+
35+
OTACloudProcessInterface::State UNOR4OTACloudProcess::resume(Message* msg) {
36+
return OtaBegin;
37+
}
38+
39+
OTACloudProcessInterface::State UNOR4OTACloudProcess::startOTA() {
40+
int ota_err = OTAUpdate::OTA_ERROR_NONE;
41+
42+
// Open fs for ota
43+
if((ota_err = ota.begin(UPDATE_FILE_NAME)) != OTAUpdate::OTA_ERROR_NONE) {
44+
DEBUG_VERBOSE("OTAUpdate::begin() failed with %d", ota_err);
45+
return convertUnor4ErrorToState(ota_err);
46+
}
47+
48+
return Fetch;
49+
}
50+
51+
OTACloudProcessInterface::State UNOR4OTACloudProcess::fetch() {
52+
int ota_err = OTAUpdate::OTA_ERROR_NONE;
53+
54+
int const ota_download = ota.download(this->context->url,UPDATE_FILE_NAME);
55+
if (ota_download <= 0) {
56+
DEBUG_VERBOSE("OTAUpdate::download() failed with %d", ota_download);
57+
return convertUnor4ErrorToState(ota_download);
58+
}
59+
DEBUG_VERBOSE("OTAUpdate::download() %d bytes downloaded", ota_download);
60+
61+
if ((ota_err = ota.verify()) != OTAUpdate::OTA_ERROR_NONE) {
62+
DEBUG_VERBOSE("OTAUpdate::verify() failed with %d", ota_err);
63+
return convertUnor4ErrorToState(ota_err);
64+
}
65+
66+
return FlashOTA;
67+
}
68+
69+
OTACloudProcessInterface::State UNOR4OTACloudProcess::flashOTA() {
70+
int ota_err = OTAUpdate::OTA_ERROR_NONE;
71+
72+
/* Flash new firmware */
73+
if ((ota_err = ota.update(UPDATE_FILE_NAME)) != OTAUpdate::OTA_ERROR_NONE) { // This reboots the MCU
74+
DEBUG_VERBOSE("OTAUpdate::update() failed with %d", ota_err);
75+
return convertUnor4ErrorToState(ota_err);
76+
}
77+
}
78+
79+
OTACloudProcessInterface::State UNOR4OTACloudProcess::reboot() {
80+
}
81+
82+
void UNOR4OTACloudProcess::reset() {
83+
}
84+
85+
bool UNOR4OTACloudProcess::isOtaCapable() {
86+
String const fv = WiFi.firmwareVersion();
87+
if (fv < String("0.3.0")) {
88+
return false;
89+
}
90+
return true;
91+
}
92+
93+
extern void* __ROM_Start;
94+
extern void* __etext;
95+
extern void* __data_end__;
96+
extern void* __data_start__;
97+
98+
constexpr void* UNOR4OTACloudProcess::appStartAddress() { return &__ROM_Start; }
99+
uint32_t UNOR4OTACloudProcess::appSize() {
100+
return ((&__etext - &__ROM_Start) + (&__data_end__ - &__data_start__))*sizeof(void*);
101+
}
102+
103+
bool UNOR4OTACloudProcess::appFlashOpen() {
104+
cfg.data_flash_bgo = false;
105+
cfg.p_callback = nullptr;
106+
cfg.p_context = nullptr;
107+
cfg.p_extend = nullptr;
108+
cfg.ipl = (BSP_IRQ_DISABLED);
109+
cfg.irq = FSP_INVALID_VECTOR;
110+
cfg.err_ipl = (BSP_IRQ_DISABLED);
111+
cfg.err_irq = FSP_INVALID_VECTOR;
112+
113+
fsp_err_t rv = FSP_ERR_UNSUPPORTED;
114+
115+
rv = R_FLASH_LP_Open(&ctrl,&cfg);
116+
DEBUG_VERBOSE("Flash open %X", rv);
117+
118+
return rv == FSP_SUCCESS;
119+
}
120+
121+
bool UNOR4OTACloudProcess::appFlashClose() {
122+
fsp_err_t rv = FSP_ERR_UNSUPPORTED;
123+
rv = R_FLASH_LP_Close(&ctrl);
124+
DEBUG_VERBOSE("Flash close %X", rv);
125+
126+
return rv == FSP_SUCCESS;
127+
}
128+
129+
static OTACloudProcessInterface::State convertUnor4ErrorToState(int error_code) {
130+
switch(error_code) {
131+
case -2:
132+
return OTACloudProcessInterface::NoOtaStorageFail;
133+
case -3:
134+
return OTACloudProcessInterface::OtaStorageInitFail;
135+
case -4:
136+
return OTACloudProcessInterface::OtaStorageEndFail;
137+
case -5:
138+
return OTACloudProcessInterface::UrlParseErrorFail;
139+
case -6:
140+
return OTACloudProcessInterface::ServerConnectErrorFail;
141+
case -7:
142+
return OTACloudProcessInterface::HttpHeaderErrorFail;
143+
case -8:
144+
return OTACloudProcessInterface::ParseHttpHeaderFail;
145+
case -9:
146+
return OTACloudProcessInterface::OtaHeaderLengthFail;
147+
case -10:
148+
return OTACloudProcessInterface::OtaHeaderCrcFail;
149+
case -11:
150+
return OTACloudProcessInterface::OtaHeaterMagicNumberFail;
151+
case -12:
152+
return OTACloudProcessInterface::OtaDownloadFail;
153+
case -13:
154+
return OTACloudProcessInterface::OtaHeaderTimeoutFail;
155+
case -14:
156+
return OTACloudProcessInterface::HttpResponseFail;
157+
case -25:
158+
return OTACloudProcessInterface::LibraryFail;
159+
case -26:
160+
return OTACloudProcessInterface::ModemFail;
161+
default:
162+
DEBUG_VERBOSE("Unrecognized error code %d", error_code);
163+
return OTACloudProcessInterface::Fail;
164+
}
165+
}
166+
4167
#endif // defined(ARDUINO_UNOR4_WIFI) && OTA_ENABLED

src/ota/implementation/OTAUnoR4.h

+38-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,52 @@
1+
/*
2+
This file is part of the ArduinoIoTCloud library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
111
#pragma once
212

3-
#include "src/ota/interface/OTAInterface.h"
13+
#include "ota/interface/OTAInterface.h"
414
#include "OTAUpdate.h"
515
#include "r_flash_lp.h"
616

717
class UNOR4OTACloudProcess: public OTACloudProcessInterface {
818
public:
9-
STM32H7OTACloudProcess();
19+
UNOR4OTACloudProcess(MessageStream *ms);
20+
21+
bool isOtaCapable() override;
1022
protected:
11-
// we start the download and decompress process
12-
virtual State fetch(Message* msg=nullptr);
23+
virtual OTACloudProcessInterface::State resume(Message* msg=nullptr) override;
24+
25+
// we are overriding the method of startOTA in order to download ota file on ESP32
26+
virtual OTACloudProcessInterface::State startOTA() override;
1327

14-
// when the download is completed we verify for integrity and correctness of the downloaded binary
15-
// virtual State verifyOTA(Message* msg=nullptr); // TODO this may be performed inside download
28+
// we start the download and decompress process
29+
virtual OTACloudProcessInterface::State fetch() override;
1630

1731
// whene the download is correctly finished we set the mcu to use the newly downloaded binary
18-
virtual State flashOTA(Message* msg=nullptr);
32+
virtual OTACloudProcessInterface::State flashOTA() override;
1933

2034
// we reboot the device
21-
virtual State reboot(Message* msg=nullptr);
35+
virtual OTACloudProcessInterface::State reboot() override;
36+
37+
virtual void reset() override;
38+
39+
constexpr void* appStartAddress();
40+
uint32_t appSize();
41+
42+
bool appFlashOpen();
43+
bool appFlashClose();
44+
45+
public:
46+
// used to access to flash memory for sha256 calculation
47+
flash_lp_instance_ctrl_t ctrl;
48+
flash_cfg_t cfg;
49+
50+
OTAUpdate ota;
51+
static const char UPDATE_FILE_NAME[];
2252
};

0 commit comments

Comments
 (0)