Skip to content

Commit a25b275

Browse files
committed
update nvm implementation
1 parent bc9036f commit a25b275

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

ports/esp32s2/common-hal/nvm/ByteArray.c

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include "common-hal/nvm/ByteArray.h"
2828

29+
#include <string.h>
30+
2931
#include "py/runtime.h"
3032
#include "nvs_flash.h"
3133

@@ -53,26 +55,52 @@ static void get_nvs_handle(nvs_handle_t * nvs_handle) {
5355
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
5456
uint32_t start_index, uint8_t* values, uint32_t len) {
5557
char index[9];
56-
sprintf(index, "%i", start_index);
58+
59+
uint8_t buffer[len];
60+
memcpy(buffer, values, len);
61+
5762
// start nvs
5863
nvs_handle_t handle;
5964
get_nvs_handle(&handle);
60-
bool status = ((nvs_set_u8(handle, (const char *)index, *values) == ESP_OK) && (nvs_commit(handle) == ESP_OK));
65+
66+
// stage flash changes
67+
for (uint32_t i = 0; i < len; i++) {
68+
sprintf(index, "%i", start_index + i);
69+
if (nvs_set_u8(handle, (const char *)index, buffer[i]) != ESP_OK) {
70+
return false;
71+
}
72+
}
73+
74+
// commit flash changes
75+
if (nvs_commit(handle) != ESP_OK) {
76+
return false;
77+
}
78+
6179
// close nvs
6280
nvs_close(handle);
63-
return status;
81+
return true;
6482
}
6583

6684
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self,
6785
uint32_t start_index, uint32_t len, uint8_t* values) {
6886
char index[9];
69-
sprintf(index, "%i", start_index);
87+
uint8_t buffer[len];
88+
7089
// start nvs
7190
nvs_handle_t handle;
7291
get_nvs_handle(&handle);
73-
if (nvs_get_u8(handle, (const char *)index, values) != ESP_OK) {
74-
mp_raise_RuntimeError(translate("NVS Error"));
92+
93+
// get from flash
94+
for (uint32_t i = 0; i < len; i++) {
95+
sprintf(index, "%i", start_index + i);
96+
if (nvs_get_u8(handle, (const char *)index, &buffer[i]) != ESP_OK) {
97+
mp_raise_RuntimeError(translate("NVS Error"));
98+
}
7599
}
100+
101+
// set into values
102+
memcpy(values, buffer, len);
103+
76104
// close nvs
77105
nvs_close(handle);
78106
}

0 commit comments

Comments
 (0)