Skip to content

Commit 1cd1953

Browse files
implementing kvstore for stm32h7
1 parent 734d2b5 commit 1cd1953

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* This file is part of Arduino_KVStore.
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+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) \
12+
|| defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA)
13+
#include "stm32h7.h"
14+
15+
16+
STM32H7KVStore::STM32H7KVStore(): kvstore(nullptr), bd(nullptr) {}
17+
18+
bool STM32H7KVStore::begin() {
19+
return begin(false);
20+
}
21+
22+
bool STM32H7KVStore::begin(bool reformat, mbed::KVStore* store) {
23+
// bd gets allocated if a kvstore is not provided as parameter here
24+
// if either one of bd or kvstore is different from NULL it means that the kvstore
25+
// had already been called begin on
26+
if(bd != nullptr || kvstore != nullptr) {
27+
return false;
28+
}
29+
30+
if(store != nullptr) {
31+
kvstore = store;
32+
} else {
33+
auto root = mbed::BlockDevice::get_default_instance();
34+
35+
if (root->init() != QSPIF_BD_ERROR_OK) {
36+
Serial.println(F("Error: QSPI init failure."));
37+
return false;
38+
}
39+
40+
bd = new mbed::MBRBlockDevice(root, 3);
41+
int res = bd->init();
42+
if (res != QSPIF_BD_ERROR_OK && !reformat) {
43+
Serial.println(F("Error: QSPI is not properly formatted, "
44+
"run QSPIformat.ino or set reformat to true"));
45+
return false;
46+
} else if (res != QSPIF_BD_ERROR_OK && reformat) {
47+
Serial.println(F("Error: QSPI is not properly formatted, "
48+
"reformatting it according to the following scheme:"));
49+
Serial.println(F("Partition 1: WiFi firmware and certificates 1MB"));
50+
Serial.println(F("Partition 2: OTA and user data 12MB"));
51+
Serial.println(F("Partition 3: Provisioning KVStore 1MB"));
52+
53+
// clearing MBR Table
54+
root->erase(0x0, root->get_erase_size());
55+
56+
mbed::MBRBlockDevice::partition(root, 1, 0x0B, 0, 1024 * 1024);
57+
mbed::MBRBlockDevice::partition(root, 2, 0x0B, 1024 * 1024, 13 * 1024 * 1024);
58+
mbed::MBRBlockDevice::partition(root, 3, 0x0B, 13 * 1024 * 1024, 14 * 1024 * 1024);
59+
}
60+
61+
kvstore = new mbed::TDBStore(bd);
62+
}
63+
64+
return kvstore->init() == MBED_SUCCESS;
65+
}
66+
67+
bool STM32H7KVStore::end() {
68+
bool res = false;
69+
70+
if(kvstore != nullptr && bd == nullptr) {
71+
res = kvstore->deinit() == MBED_SUCCESS;
72+
kvstore = nullptr;
73+
} else if(kvstore != nullptr && bd != nullptr) {
74+
res = kvstore->deinit() == MBED_SUCCESS;
75+
76+
delete kvstore;
77+
kvstore = nullptr;
78+
79+
delete bd;
80+
bd = nullptr;
81+
}
82+
83+
return res;
84+
}
85+
86+
template<typename T=int>
87+
static inline typename KVStoreInterface::res_t fromMbedErrors(int error, T res=1) {
88+
return error == MBED_SUCCESS ? res : -error;
89+
}
90+
91+
bool STM32H7KVStore::clear() {
92+
return kvstore != nullptr ? kvstore->reset() == MBED_SUCCESS : false;
93+
}
94+
95+
typename KVStoreInterface::res_t STM32H7KVStore::remove(const key_t& key) {
96+
return kvstore != nullptr ? fromMbedErrors(kvstore->remove(key)) : -1;
97+
}
98+
99+
typename KVStoreInterface::res_t STM32H7KVStore::putBytes(const key_t& key, const uint8_t buf[], size_t len) {
100+
return kvstore != nullptr ? fromMbedErrors(kvstore->set(key, buf, len, 0), len) : -1; // TODO flags
101+
}
102+
103+
typename KVStoreInterface::res_t STM32H7KVStore::getBytes(const key_t& key, uint8_t buf[], size_t maxLen) const {
104+
if(kvstore == nullptr) {
105+
return -1;
106+
}
107+
108+
size_t actual_size = maxLen;
109+
auto res = kvstore->get(key, buf, maxLen, &actual_size);
110+
111+
return fromMbedErrors(res, actual_size);
112+
}
113+
114+
size_t STM32H7KVStore::getBytesLength(const key_t& key) const {
115+
if(kvstore == nullptr) {
116+
return 0;
117+
}
118+
119+
mbed::KVStore::info_t info;
120+
auto res = kvstore->get_info(key, &info);
121+
122+
return res == MBED_SUCCESS ? info.size : 0;
123+
}
124+
125+
bool STM32H7KVStore::exists(const key_t& key) const {
126+
return getBytesLength(key) > 0;
127+
}
128+
129+
#endif // defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA)

src/kvstore/implementation/stm32h7.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This file is part of Arduino_KVStore.
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+
#pragma once
11+
#include "../kvstore.h"
12+
#include <KVStore.h>
13+
#include <TDBStore.h>
14+
#include "QSPIFBlockDevice.h"
15+
#include "MBRBlockDevice.h"
16+
17+
class STM32H7KVStore: public KVStoreInterface {
18+
public:
19+
STM32H7KVStore();
20+
~STM32H7KVStore() { end(); }
21+
22+
bool begin() override;
23+
bool begin(bool reformat, mbed::KVStore* store = nullptr);
24+
bool end() override;
25+
bool clear() override;
26+
27+
typename KVStoreInterface::res_t remove(const key_t& key) override;
28+
bool exists(const key_t& key) const override;
29+
typename KVStoreInterface::res_t putBytes(const key_t& key, const uint8_t b[], size_t s) override;
30+
typename KVStoreInterface::res_t getBytes(const key_t& key, uint8_t b[], size_t s) const override;
31+
size_t getBytesLength(const key_t& key) const override;
32+
private:
33+
mbed::MBRBlockDevice* bd;
34+
mbed::KVStore* kvstore;
35+
};

0 commit comments

Comments
 (0)