Skip to content

Commit ec04262

Browse files
[openthread] update flash impl to use isolated nv
1 parent f027717 commit ec04262

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

libopenthread/platform/flash.c

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,63 @@
1-
#include <storage/nonvolatile_storage.h>
2-
#include <libtock-sync/storage/nonvolatile_storage.h>
1+
#include <storage/isolated_nonvolatile_storage.h>
2+
#include <libtock-sync/storage/isolated_nonvolatile_storage.h>
33

44
#include <openthread/platform/flash.h>
55

66
#include <stdio.h>
77

8-
const uint32_t SWAP_SIZE = 1024;
8+
// TODO: The kernel currently has a 512 byte buffer
9+
// for isolated nonvolatile storage. This means that we can only
10+
// allow 512 bytes. 2x 512B swaps seems to be enough for openthread
11+
// but we may need to revist this in the future.
12+
const uint32_t SWAP_SIZE = 512;
913
const uint8_t SWAP_NUM = 2;
1014

11-
uint32_t otPlatFlashGetSwapSize(otInstance *aInstance){
15+
uint32_t otPlatFlashGetSwapSize(otInstance *aInstance) {
1216
OT_UNUSED_VARIABLE(aInstance);
1317
return SWAP_SIZE;
1418
}
1519

16-
void otPlatFlashInit(otInstance *aInstance) {
20+
void otPlatFlashInit(otInstance *aInstance) {
1721
OT_UNUSED_VARIABLE(aInstance);
18-
}
1922

20-
void otPlatFlashErase(otInstance *aInstance, uint8_t aSwapIndex) {
21-
OT_UNUSED_VARIABLE(aInstance);
23+
// Confirm we have enough space for the 2 swaps.
24+
uint64_t num_bytes;
25+
libtocksync_isolated_nonvolatile_storage_get_number_bytes(&num_bytes);
2226

23-
uint32_t num_bytes;
24-
libtock_nonvolatile_storage_get_number_bytes(&num_bytes);
27+
assert(num_bytes >= SWAP_SIZE * SWAP_NUM);
28+
}
2529

26-
// Write all zeroes to flash
27-
uint8_t zeroes[SWAP_SIZE];
30+
void otPlatFlashErase(otInstance *aInstance, uint8_t aSwapIndex) {
31+
uint8_t overwrite[SWAP_SIZE];
2832

29-
otPlatFlashWrite(aInstance, aSwapIndex, 0, zeroes, num_bytes);
33+
// Fill the swap area with 0xFF to erase it.
34+
for (uint32_t i = 0; i < SWAP_SIZE; i++) {
35+
overwrite[i] = 0xFF;
36+
}
3037

38+
otPlatFlashWrite(aInstance, aSwapIndex, 0, overwrite, SWAP_SIZE);
3139
}
3240

3341
void otPlatFlashWrite(otInstance *aInstance, uint8_t aSwapIndex, uint32_t aOffset,
3442
const void *aData, uint32_t aSize) {
3543
OT_UNUSED_VARIABLE(aInstance);
36-
OT_UNUSED_VARIABLE(aSwapIndex);
37-
int ret;
3844

3945
assert(aSwapIndex < SWAP_NUM);
46+
assert(aOffset + aSize <= SWAP_SIZE);
4047
uint32_t offset = aSwapIndex ? SWAP_SIZE : 0;
4148
offset += aOffset;
4249

43-
int _written;
44-
ret = libtocksync_nonvolatile_storage_write(offset, aSize, (uint8_t*)aData, aSize, &_written);
45-
if (ret != RETURNCODE_SUCCESS) {
46-
return;
47-
}
50+
libtocksync_isolated_nonvolatile_storage_write(offset, (uint8_t*)aData, aSize);
4851
}
4952

5053
void otPlatFlashRead(otInstance *aInstance, uint8_t aSwapIndex, uint32_t aOffset, void *aData,
5154
uint32_t aSize) {
5255
OT_UNUSED_VARIABLE(aInstance);
53-
OT_UNUSED_VARIABLE(aSwapIndex);
54-
int ret;
55-
56+
5657
assert(aSwapIndex < SWAP_NUM);
58+
assert(aOffset + aSize <= SWAP_SIZE);
5759
uint32_t offset = aSwapIndex ? SWAP_SIZE : 0;
5860
offset += aOffset;
59-
60-
int _read;
61-
ret = libtocksync_nonvolatile_storage_read(offset, aSize, (uint8_t*)aData, aSize, &_read);
62-
if (ret != RETURNCODE_SUCCESS) {
63-
return;
64-
}
61+
62+
libtocksync_isolated_nonvolatile_storage_read(offset, (uint8_t*)aData, aSize);
6563
}

0 commit comments

Comments
 (0)