Skip to content

Commit

Permalink
update nvm implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
microdev1 committed Nov 19, 2020
1 parent bc9036f commit a25b275
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions ports/esp32s2/common-hal/nvm/ByteArray.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

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

#include <string.h>

#include "py/runtime.h"
#include "nvs_flash.h"

Expand Down Expand Up @@ -53,26 +55,52 @@ static void get_nvs_handle(nvs_handle_t * nvs_handle) {
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
uint32_t start_index, uint8_t* values, uint32_t len) {
char index[9];
sprintf(index, "%i", start_index);

uint8_t buffer[len];
memcpy(buffer, values, len);

// start nvs
nvs_handle_t handle;
get_nvs_handle(&handle);
bool status = ((nvs_set_u8(handle, (const char *)index, *values) == ESP_OK) && (nvs_commit(handle) == ESP_OK));

// stage flash changes
for (uint32_t i = 0; i < len; i++) {
sprintf(index, "%i", start_index + i);
if (nvs_set_u8(handle, (const char *)index, buffer[i]) != ESP_OK) {
return false;
}
}

// commit flash changes
if (nvs_commit(handle) != ESP_OK) {
return false;
}

// close nvs
nvs_close(handle);
return status;
return true;
}

void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self,
uint32_t start_index, uint32_t len, uint8_t* values) {
char index[9];
sprintf(index, "%i", start_index);
uint8_t buffer[len];

// start nvs
nvs_handle_t handle;
get_nvs_handle(&handle);
if (nvs_get_u8(handle, (const char *)index, values) != ESP_OK) {
mp_raise_RuntimeError(translate("NVS Error"));

// get from flash
for (uint32_t i = 0; i < len; i++) {
sprintf(index, "%i", start_index + i);
if (nvs_get_u8(handle, (const char *)index, &buffer[i]) != ESP_OK) {
mp_raise_RuntimeError(translate("NVS Error"));
}
}

// set into values
memcpy(values, buffer, len);

// close nvs
nvs_close(handle);
}

0 comments on commit a25b275

Please sign in to comment.