Skip to content

Commit 75c92c2

Browse files
implementing kvstore for portenta c33
1 parent 3828e22 commit 75c92c2

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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_C33)
12+
#include "portentac33.h"
13+
14+
PortentaC33KVStore::PortentaC33KVStore(): kvstore(nullptr), bd(nullptr) {}
15+
16+
bool PortentaC33KVStore::begin() {
17+
return begin(false);
18+
}
19+
20+
bool PortentaC33KVStore::begin(bool reformat, mbed::KVStore* store) {
21+
// bd gets allocated if a kvstore is not provided as parameter here
22+
// if either one of bd or kvstore is different from NULL it means that the kvstore
23+
// had already been called begin on
24+
if(bd != nullptr || kvstore != nullptr) {
25+
return false;
26+
}
27+
28+
if(store != nullptr) {
29+
kvstore = store;
30+
} else {
31+
auto bd = new MBRBlockDevice(BlockDevice::get_default_instance(), 3);
32+
33+
kvstore = new TDBStore(bd);
34+
}
35+
36+
return kvstore->init() == KVSTORE_SUCCESS;
37+
}
38+
39+
bool PortentaC33KVStore::end() {
40+
bool res = false;
41+
42+
if(kvstore != nullptr && bd == nullptr) {
43+
res = kvstore->deinit() == KVSTORE_SUCCESS;
44+
kvstore = nullptr;
45+
} else if(kvstore != nullptr && bd != nullptr) {
46+
res = kvstore->deinit() == KVSTORE_SUCCESS;
47+
48+
delete kvstore;
49+
kvstore = nullptr;
50+
51+
delete bd;
52+
bd = nullptr;
53+
}
54+
55+
return res;
56+
}
57+
58+
template<typename T=int>
59+
static inline typename KVStoreInterface::res_t fromMbedErrors(int error, T res=1) {
60+
return error == KVSTORE_SUCCESS ? res : -error;
61+
}
62+
63+
bool PortentaC33KVStore::clear() {
64+
return kvstore != nullptr ? kvstore->reset() : false;
65+
}
66+
67+
typename KVStoreInterface::res_t PortentaC33KVStore::remove(const key_t& key) {
68+
return kvstore != nullptr ? fromMbedErrors(kvstore->remove(key)) : -1;
69+
}
70+
71+
typename KVStoreInterface::res_t PortentaC33KVStore::putBytes(const key_t& key, const uint8_t buf[], size_t len) {
72+
return kvstore != nullptr ? fromMbedErrors(kvstore->set(key, buf, len, 0), len) : -1; // TODO flags
73+
}
74+
75+
typename KVStoreInterface::res_t PortentaC33KVStore::getBytes(const key_t& key, uint8_t buf[], size_t maxLen) const {
76+
if(kvstore == nullptr) {
77+
return -1;
78+
}
79+
80+
size_t actual_size = maxLen;
81+
auto res = kvstore->get(key, buf, maxLen, &actual_size);
82+
83+
return fromMbedErrors(res, actual_size);
84+
}
85+
86+
size_t PortentaC33KVStore::getBytesLength(const key_t& key) const {
87+
if(kvstore == nullptr) {
88+
return 0;
89+
}
90+
91+
mbed::KVStore::info_t info;
92+
auto res = kvstore->get_info(key, &info);
93+
94+
return res == KVSTORE_SUCCESS ? info.size : 0;
95+
}
96+
97+
bool PortentaC33KVStore::exists(const key_t& key) const {
98+
return getBytesLength(key) > 0;
99+
}
100+
#endif // defined(ARDUINO_PORTENTA_C33)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 "QSPIFlashBlockDevice.h"
15+
#include "MBRBlockDevice.h"
16+
17+
#define QSPIF_BD_ERROR_OK 0
18+
19+
class PortentaC33KVStore: public KVStoreInterface {
20+
public:
21+
PortentaC33KVStore();
22+
~PortentaC33KVStore() { end(); }
23+
24+
bool begin() override;
25+
bool begin(bool reformat, mbed::KVStore* store = nullptr);
26+
bool end() override;
27+
bool clear() override;
28+
29+
typename KVStoreInterface::res_t remove(const key_t& key) override;
30+
bool exists(const key_t& key) const override;
31+
typename KVStoreInterface::res_t putBytes(const key_t& key, const uint8_t b[], size_t s) override;
32+
typename KVStoreInterface::res_t getBytes(const key_t& key, uint8_t b[], size_t s) const override;
33+
size_t getBytesLength(const key_t& key) const override;
34+
private:
35+
MBRBlockDevice* bd;
36+
mbed::KVStore* kvstore;
37+
};

0 commit comments

Comments
 (0)