update nvm implementation

This commit is contained in:
microDev 2020-11-20 00:22:00 +05:30
parent bc9036f353
commit a25b27520d
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
1 changed files with 34 additions and 6 deletions

View File

@ -26,6 +26,8 @@
#include "common-hal/nvm/ByteArray.h"
#include <string.h>
#include "py/runtime.h"
#include "nvs_flash.h"
@ -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) {
// 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);
}