Skip to content

Commit c6b3c77

Browse files
authored
Merge pull request #477 from pennam/stm32h7-ota
STM32H7 OTA code cleanup
2 parents b93768d + 8143741 commit c6b3c77

File tree

2 files changed

+43
-71
lines changed

2 files changed

+43
-71
lines changed

src/ota/implementation/OTASTM32H7.cpp

+30-46
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,15 @@
1111
#include "AIoTC_Config.h"
1212
#if defined(BOARD_STM32H7) && OTA_ENABLED
1313
#include "OTASTM32H7.h"
14-
15-
#include "utility/watchdog/Watchdog.h"
1614
#include <STM32H747_System.h>
1715

18-
static bool findProgramLength(DIR * dir, uint32_t & program_length);
19-
20-
const char STM32H7OTACloudProcess::UPDATE_FILE_NAME[] = "/fs/UPDATE.BIN";
21-
2216
STM32H7OTACloudProcess::STM32H7OTACloudProcess(MessageStream *ms, Client* client)
2317
: OTADefaultCloudProcessInterface(ms, client)
2418
, decompressed(nullptr)
2519
, _bd_raw_qspi(nullptr)
26-
, _program_length(0)
2720
, _bd(nullptr)
28-
, _fs(nullptr) {
21+
, _fs(nullptr)
22+
, _filename("/" + String(STM32H747OTA::FOLDER) + "/" + String(STM32H747OTA::NAME)) {
2923

3024
}
3125

@@ -44,7 +38,6 @@ OTACloudProcessInterface::State STM32H7OTACloudProcess::resume(Message* msg) {
4438

4539
void STM32H7OTACloudProcess::update() {
4640
OTADefaultCloudProcessInterface::update();
47-
watchdog_reset(); // FIXME this should npot be performed here
4841
}
4942

5043
int STM32H7OTACloudProcess::writeFlash(uint8_t* const buffer, size_t len) {
@@ -65,10 +58,13 @@ OTACloudProcessInterface::State STM32H7OTACloudProcess::startOTA() {
6558
}
6659

6760
// this could be useless, since we are writing over it
68-
remove(UPDATE_FILE_NAME);
61+
remove(_filename.c_str());
6962

70-
decompressed = fopen(UPDATE_FILE_NAME, "wb");
63+
decompressed = fopen(_filename.c_str(), "wb");
7164

65+
if(decompressed == nullptr) {
66+
return ErrorOpenUpdateFileFail;
67+
}
7268
// start the download if the setup for ota storage is successful
7369
return OTADefaultCloudProcessInterface::startOTA();
7470
}
@@ -78,18 +74,20 @@ OTACloudProcessInterface::State STM32H7OTACloudProcess::flashOTA() {
7874
fclose(decompressed);
7975
decompressed = nullptr;
8076

77+
uint32_t updateLength = 0;
78+
8179
/* Schedule the firmware update. */
82-
if(!storageOpen()) {
80+
if(!findProgramLength(updateLength)) {
8381
return OtaStorageOpenFail;
8482
}
8583

8684
storageClean();
8785

8886
// this sets the registries in RTC to load the firmware from the storage selected at the next reboot
89-
STM32H747::writeBackupRegister(RTCBackup::DR0, 0x07AA);
90-
STM32H747::writeBackupRegister(RTCBackup::DR1, storage);
91-
STM32H747::writeBackupRegister(RTCBackup::DR2, data_offset);
92-
STM32H747::writeBackupRegister(RTCBackup::DR3, _program_length);
87+
STM32H747::writeBackupRegister(RTCBackup::DR0, STM32H747OTA::MAGIC);
88+
STM32H747::writeBackupRegister(RTCBackup::DR1, STM32H747OTA::STORAGE_TYPE);
89+
STM32H747::writeBackupRegister(RTCBackup::DR2, STM32H747OTA::PARTITION);
90+
STM32H747::writeBackupRegister(RTCBackup::DR3, updateLength);
9391

9492
return Reboot;
9593
}
@@ -106,7 +104,7 @@ OTACloudProcessInterface::State STM32H7OTACloudProcess::reboot() {
106104
void STM32H7OTACloudProcess::reset() {
107105
OTADefaultCloudProcessInterface::reset();
108106

109-
remove(UPDATE_FILE_NAME);
107+
remove(_filename.c_str());
110108

111109
storageClean();
112110
}
@@ -156,14 +154,9 @@ bool STM32H7OTACloudProcess::storageInit() {
156154
}
157155
}
158156

159-
if (storage == portenta::QSPI_FLASH_FATFS) {
160-
_fs = new mbed::FATFileSystem("fs");
161-
err_mount = _fs->mount(_bd_raw_qspi);
162-
} else if (storage == portenta::QSPI_FLASH_FATFS_MBR) {
163-
_bd = new mbed::MBRBlockDevice(_bd_raw_qspi, data_offset);
164-
_fs = new mbed::FATFileSystem("fs");
165-
err_mount = _fs->mount(_bd);
166-
}
157+
_bd = new mbed::MBRBlockDevice(_bd_raw_qspi, STM32H747OTA::PARTITION);
158+
_fs = new mbed::FATFileSystem(STM32H747OTA::FOLDER);
159+
err_mount = _fs->mount(_bd);
167160

168161
if (!err_mount) {
169162
return true;
@@ -172,43 +165,34 @@ bool STM32H7OTACloudProcess::storageInit() {
172165
return false;
173166
}
174167

175-
bool STM32H7OTACloudProcess::storageOpen() {
168+
bool STM32H7OTACloudProcess::findProgramLength(uint32_t & program_length) {
176169
DIR * dir = NULL;
177-
if ((dir = opendir("/fs")) != NULL)
178-
{
179-
if (findProgramLength(dir, _program_length))
180-
{
181-
closedir(dir);
182-
return true;
183-
}
184-
closedir(dir);
185-
}
170+
struct dirent * entry = NULL;
171+
String dirName = "/" + String(STM32H747OTA::FOLDER);
172+
bool found = false;
186173

187-
return false;
188-
}
174+
if ((dir = opendir(dirName.c_str())) == NULL) {
175+
return false;
176+
}
189177

190-
bool findProgramLength(DIR * dir, uint32_t & program_length) {
191-
struct dirent * entry = NULL;
192178
while ((entry = readdir(dir)) != NULL) {
193-
if (strcmp(entry->d_name, "UPDATE.BIN") == 0) { // FIXME use constants
179+
if (strcmp(entry->d_name, STM32H747OTA::NAME) == 0) {
194180
struct stat stat_buf;
195-
stat("/fs/UPDATE.BIN", &stat_buf);
181+
stat(_filename.c_str(), &stat_buf);
196182
program_length = stat_buf.st_size;
197-
return true;
183+
found = true;
198184
}
199185
}
200-
201-
return false;
186+
closedir(dir);
187+
return found;
202188
}
203189

204-
// extern uint32_t __stext = ~0;
205190
extern uint32_t __etext;
206191
extern uint32_t _sdata;
207192
extern uint32_t _edata;
208193

209194
void* STM32H7OTACloudProcess::appStartAddress() {
210195
return (void*)0x8040000;
211-
// return &__stext;
212196
}
213197

214198
uint32_t STM32H7OTACloudProcess::appSize() {

src/ota/implementation/OTASTM32H7.h

+13-25
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,17 @@
2121
#include "WiFi.h" /* WiFi from ArduinoCore-mbed */
2222
#include <SocketHelpers.h>
2323

24-
#define APOTA_QSPI_FLASH_FLAG (1 << 2)
25-
#define APOTA_SDCARD_FLAG (1 << 3)
26-
#define APOTA_RAW_FLAG (1 << 4)
27-
#define APOTA_FATFS_FLAG (1 << 5)
28-
#define APOTA_LITTLEFS_FLAG (1 << 6)
29-
#define APOTA_MBR_FLAG (1 << 7)
30-
31-
namespace portenta {
32-
enum StorageType {
33-
QSPI_FLASH_FATFS = APOTA_QSPI_FLASH_FLAG | APOTA_FATFS_FLAG,
34-
QSPI_FLASH_FATFS_MBR = APOTA_QSPI_FLASH_FLAG | APOTA_FATFS_FLAG | APOTA_MBR_FLAG,
35-
SD_FATFS = APOTA_SDCARD_FLAG | APOTA_FATFS_FLAG,
36-
SD_FATFS_MBR = APOTA_SDCARD_FLAG | APOTA_FATFS_FLAG | APOTA_MBR_FLAG,
37-
};
24+
namespace STM32H747OTA {
25+
/* External QSPI flash + MBR + FatFs */
26+
static const uint32_t constexpr STORAGE_TYPE = ((1 << 2) | (1 << 5) | (1 << 7));
27+
/* Default OTA partition */
28+
static const uint32_t constexpr PARTITION = 2;
29+
/* OTA Magic number */
30+
static const uint32_t constexpr MAGIC = 0x07AA;
31+
/* OTA download folder name */
32+
static const char constexpr FOLDER[] = "ota";
33+
/* OTA update filename */
34+
static const char constexpr NAME[] = "UPDATE.BIN";
3835
}
3936

4037
class STM32H7OTACloudProcess: public OTADefaultCloudProcessInterface {
@@ -67,23 +64,14 @@ class STM32H7OTACloudProcess: public OTADefaultCloudProcessInterface {
6764
bool appFlashClose() { return true; };
6865
private:
6966
bool storageInit();
70-
bool storageOpen();
71-
67+
bool findProgramLength(uint32_t & program_length);
7268
void storageClean();
7369

7470
FILE* decompressed;
75-
// static const char UPDATE_FILE_NAME[];
7671
mbed::BlockDevice* _bd_raw_qspi;
77-
uint32_t _program_length;
7872

7973
mbed::BlockDevice* _bd;
8074
mbed::FATFileSystem* _fs;
8175

82-
mbed::MBRBlockDevice* cert_bd_qspi;
83-
mbed::FATFileSystem* cert_fs_qspi;
84-
85-
const portenta::StorageType storage=portenta::QSPI_FLASH_FATFS_MBR;
86-
const uint32_t data_offset=2;
87-
88-
static const char UPDATE_FILE_NAME[];
76+
String _filename;
8977
};

0 commit comments

Comments
 (0)