Skip to content

Commit 3dbeed3

Browse files
committed
Add Class to handle QSPI flash operations
1 parent c3fc164 commit 3dbeed3

File tree

6 files changed

+215
-311
lines changed

6 files changed

+215
-311
lines changed

src/flashFormatter/C33FlashFormatter.cpp

Lines changed: 4 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -7,121 +7,16 @@
77
*/
88
#if defined(ARDUINO_PORTENTA_C33)
99
#include "C33FlashFormatter.h"
10-
#define BD_ERROR_OK 0
1110
#include "certificates.h"
1211

13-
C33FlashFormatter::C33FlashFormatter():
14-
_root(BlockDevice::get_default_instance()),
15-
_sys_bd(_root, 1),
16-
_sys_fs("sys"),
17-
_user_bd(_root, 2),
18-
_kvStore_bd(_root, 3) {
19-
}
20-
21-
bool C33FlashFormatter::checkPartition()
22-
{
23-
if (_root->init() != BD_ERROR_OK)
24-
{
25-
return false;
26-
}
27-
28-
if(!checkCACertificatesPartition())
29-
{
30-
return false;
31-
}
32-
33-
if (_user_bd.init() != BD_ERROR_OK)
34-
{
35-
return false;
36-
}
37-
38-
_user_bd.deinit();
39-
40-
if (_kvStore_bd.init() != BD_ERROR_OK)
41-
{
42-
return false;
43-
}
44-
45-
_kvStore_bd.deinit();
46-
_root->deinit();
47-
48-
return true;
49-
}
50-
51-
bool C33FlashFormatter::formatPartition() {
52-
MBRBlockDevice::partition(_root, 1, 0x0B, 0, 5 * 1024 * 1024);
53-
MBRBlockDevice::partition(_root, 2, 0x0B, 5 * 1024 * 1024, 15 * 1024 * 1024);
54-
MBRBlockDevice::partition(_root, 3, 0x0B, 15 * 1024 * 1024, 16 * 1024 * 1024);
55-
56-
if(!flashCACertificates())
57-
{
58-
return false;
59-
}
60-
61-
_user_data_fs = new LittleFileSystem("user");
62-
int err = _user_data_fs->reformat(&_user_bd);
63-
if (err) {
64-
return false;
65-
}
66-
_user_data_fs->unmount();
67-
_root->deinit();
68-
return true;
69-
}
70-
71-
bool C33FlashFormatter::checkCACertificatesPartition()
12+
bool C33FlashFormatter::checkWiFiData()
7213
{
73-
/*Inspired by the CertificateUploader.ino example for Portenta C33*/
74-
if (_sys_bd.init() != BD_ERROR_OK || _sys_fs.mount(&_sys_bd) != FR_OK)
75-
{
76-
return false;
77-
}
78-
79-
DIR *dir;
80-
struct dirent *ent;
81-
82-
if ((dir = opendir("/sys")) == NULL) {
83-
return false;
84-
}
85-
86-
bool foundCert = false;
87-
while ((ent = readdir (dir)) != NULL) {
88-
String fullname = "/sys/" + String(ent->d_name);
89-
if (fullname == "/sys/cacert.pem") {
90-
foundCert = true;
91-
break;
92-
}
93-
}
94-
closedir (dir);
95-
96-
_sys_fs.unmount();
97-
_sys_bd.deinit();
98-
return foundCert;
14+
return checkFile("/wlan", "cacert.pem");
9915
}
10016

101-
bool C33FlashFormatter::flashCACertificates()
17+
bool C33FlashFormatter::restoreWifiData()
10218
{
103-
int err = _sys_fs.reformat(&_sys_bd);
104-
if (err) {
105-
return false;
106-
}
107-
108-
int chunck_size = 128;
109-
int byte_count = 0;
110-
FILE* fp = fopen("/sys/cacert.pem", "wb");
111-
while (byte_count < cacert_pem_len) {
112-
if(byte_count + chunck_size > cacert_pem_len)
113-
chunck_size = cacert_pem_len - byte_count;
114-
int ret = fwrite(&cacert_pem[byte_count], chunck_size, 1 ,fp);
115-
if (ret != 1) {
116-
return false;
117-
}
118-
byte_count += chunck_size;
119-
}
120-
fclose(fp);
121-
122-
_sys_fs.unmount();
123-
124-
return true;
19+
return writeFile("/wlan", "cacert.pem", cacert_pem, cacert_pem_len, 128);
12520
}
12621

12722
#endif

src/flashFormatter/C33FlashFormatter.h

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,10 @@
66
file, You can obtain one at http://mozilla.org/MPL/2.0/.
77
*/
88
#pragma once
9-
#include "FlashFormatterBase.h"
10-
#include "BlockDevice.h"
11-
#include "MBRBlockDevice.h"
12-
#include "LittleFileSystem.h"
13-
#include "FATFileSystem.h"
9+
#include "FlashFormatterQSPI.h"
1410

15-
class C33FlashFormatter : public FlashFormatterBase {
16-
public:
17-
C33FlashFormatter();
18-
protected:
19-
bool checkPartition() override;
20-
bool formatPartition() override;
11+
class C33FlashFormatter : public FlashFormatterQSPI {
2112
private:
22-
bool checkCACertificatesPartition();
23-
bool flashCACertificates();
24-
BlockDevice* _root;
25-
MBRBlockDevice _sys_bd;
26-
MBRBlockDevice _user_bd;
27-
FATFileSystem _sys_fs;
28-
FileSystem * _user_data_fs;
29-
MBRBlockDevice _kvStore_bd;
13+
bool checkWiFiData() override;
14+
bool restoreWifiData() override;
3015
};
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
Copyright (c) 2025 Arduino SA
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) \
9+
|| defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA) || defined(ARDUINO_PORTENTA_C33)
10+
#include "FlashFormatterQSPI.h"
11+
12+
FlashFormatterQSPI::FlashFormatterQSPI():
13+
_root(BlockDevice::get_default_instance()),
14+
_wifiData(_root, 1),
15+
_wifiFS("wlan"),
16+
_otaData(_root, 2),
17+
_otaFS("fs"),
18+
_kvstoreData(_root, 3)
19+
{
20+
}
21+
22+
bool FlashFormatterQSPI::checkPartition()
23+
{
24+
if (_root->init() != BD_ERROR_OK) {
25+
return false;
26+
}
27+
28+
if (_wifiData.init() != BD_ERROR_OK || _wifiFS.mount(&_wifiData) != 0) {
29+
return false;
30+
}
31+
32+
if (!checkWiFiData()) {
33+
return false;
34+
}
35+
36+
_wifiFS.unmount();
37+
_wifiData.deinit();
38+
39+
if (_otaData.init() != BD_ERROR_OK || _otaFS.mount(&_otaData) != 0) {
40+
return false;
41+
}
42+
43+
if (_otaData.size() < 5 * 1024 * 1024) {
44+
return false;
45+
}
46+
47+
_otaFS.unmount();
48+
_otaData.deinit();
49+
50+
if (_kvstoreData.init() != BD_ERROR_OK) {
51+
return false;
52+
}
53+
54+
_kvstoreData.deinit();
55+
_root->deinit();
56+
return true;
57+
}
58+
59+
bool FlashFormatterQSPI::formatPartition() {
60+
_root->erase(0x0, _root->get_erase_size());
61+
MBRBlockDevice::partition(_root, 1, 0x0B, 0, 1 * 1024 * 1024);
62+
MBRBlockDevice::partition(_root, 2, 0x0B, 1 * 1024 * 1024, 6 * 1024 * 1024);
63+
MBRBlockDevice::partition(_root, 3, 0x0B, 6 * 1024 * 1024, 7 * 1024 * 1024);
64+
65+
if (_wifiFS.reformat(&_wifiData) != 0) {
66+
return false;
67+
}
68+
69+
if (_otaFS.reformat(&_otaData) != 0) {
70+
return false;
71+
}
72+
73+
if (!restoreWifiData()) {
74+
return false;
75+
}
76+
77+
_wifiFS.unmount();
78+
_otaFS.unmount();
79+
_root->deinit();
80+
81+
return true;
82+
}
83+
84+
bool FlashFormatterQSPI::checkFile(const char* partition, const char* filename)
85+
{
86+
DIR *dir;
87+
struct dirent *ent;
88+
89+
if ((dir = opendir(partition)) == NULL) {
90+
return false;
91+
}
92+
93+
bool foundFile = false;
94+
while ((ent = readdir (dir)) != NULL) {
95+
String fullname = String(partition) + "/" + String(ent->d_name);
96+
if (fullname == String(partition) + "/" + String(filename)) {
97+
foundFile = true;
98+
break;
99+
}
100+
}
101+
closedir (dir);
102+
return foundFile;
103+
}
104+
105+
bool FlashFormatterQSPI::writeFile(const char* partition, const char* filename, const uint8_t* data, size_t size, size_t maxChunkSize)
106+
{
107+
String fullname = String(partition) + "/" + String(filename);
108+
FILE* fp = fopen(fullname.c_str(), "wb");
109+
if (!fp) {
110+
return false;
111+
}
112+
113+
size_t chunkSize = maxChunkSize;
114+
size_t byteCount = 0;
115+
while (byteCount < size) {
116+
if (byteCount + chunkSize > size)
117+
chunkSize = size - byteCount;
118+
int ret = fwrite(&data[byteCount], chunkSize, 1, fp);
119+
if (ret != 1) {
120+
fclose(fp);
121+
return false;
122+
}
123+
byteCount += chunkSize;
124+
}
125+
size_t written = ftell(fp);
126+
fclose(fp);
127+
128+
return written == size;
129+
}
130+
131+
bool FlashFormatterQSPI::writeFlash(const uint8_t* data, size_t size, size_t offset, size_t maxChunkSize)
132+
{
133+
size_t chunkSize = maxChunkSize;
134+
size_t byteCount = 0;
135+
while (byteCount < size) {
136+
if(byteCount + chunkSize > size)
137+
chunkSize = size - byteCount;
138+
int ret = _root->program(data, offset + byteCount, chunkSize);
139+
if (ret != 0) {
140+
return false;
141+
}
142+
byteCount += chunkSize;
143+
}
144+
return true;
145+
}
146+
147+
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright (c) 2025 Arduino SA
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
#pragma once
9+
#include "FlashFormatterBase.h"
10+
#include <BlockDevice.h>
11+
#include <MBRBlockDevice.h>
12+
#include <LittleFileSystem.h>
13+
#include <FATFileSystem.h>
14+
15+
#if !defined(ARDUINO_PORTENTA_C33)
16+
using namespace mbed;
17+
#endif
18+
19+
class FlashFormatterQSPI : public FlashFormatterBase {
20+
public:
21+
FlashFormatterQSPI();
22+
bool checkPartition() override;
23+
bool formatPartition() override;
24+
25+
protected:
26+
bool checkFile(const char* partition, const char* filename);
27+
bool writeFile(const char* partition, const char* filename, const uint8_t* data, size_t size, size_t maxChunkSize);
28+
bool writeFlash(const uint8_t* data, size_t size, size_t offset, size_t maxChunkSize);
29+
30+
private:
31+
virtual bool checkWiFiData() { return true; };
32+
virtual bool restoreWifiData() { return true; };
33+
BlockDevice* _root;
34+
MBRBlockDevice _wifiData;
35+
FATFileSystem _wifiFS;
36+
MBRBlockDevice _otaData;
37+
FATFileSystem _otaFS;
38+
MBRBlockDevice _kvstoreData;
39+
};
40+

0 commit comments

Comments
 (0)