use pointer to get nvs handle
This commit is contained in:
parent
0686cde226
commit
bc9036f353
@ -27,14 +27,13 @@
|
|||||||
#include "common-hal/nvm/ByteArray.h"
|
#include "common-hal/nvm/ByteArray.h"
|
||||||
|
|
||||||
#include "py/runtime.h"
|
#include "py/runtime.h"
|
||||||
|
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
|
|
||||||
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
|
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
|
||||||
return self->len;
|
return self->len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static nvs_handle get_nvs_handle(void) {
|
static void get_nvs_handle(nvs_handle_t * nvs_handle) {
|
||||||
// Initialize NVS
|
// Initialize NVS
|
||||||
esp_err_t err = nvs_flash_init();
|
esp_err_t err = nvs_flash_init();
|
||||||
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||||
@ -46,32 +45,31 @@ static nvs_handle get_nvs_handle(void) {
|
|||||||
ESP_ERROR_CHECK(err);
|
ESP_ERROR_CHECK(err);
|
||||||
|
|
||||||
// Open NVS handle
|
// Open NVS handle
|
||||||
nvs_handle nvs_handle;
|
if (nvs_open("CPY", NVS_READWRITE, nvs_handle) != ESP_OK) {
|
||||||
if (nvs_open("CPY", NVS_READWRITE, &nvs_handle) != ESP_OK) {
|
|
||||||
mp_raise_RuntimeError(translate("NVS Error"));
|
mp_raise_RuntimeError(translate("NVS Error"));
|
||||||
}
|
}
|
||||||
return nvs_handle;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
|
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
|
||||||
uint32_t start_index, uint8_t* values, uint32_t len) {
|
uint32_t start_index, uint8_t* values, uint32_t len) {
|
||||||
char index[9];
|
char index[9];
|
||||||
sprintf(index, "%i", start_index - CIRCUITPY_INTERNAL_NVM_START_ADDR);
|
sprintf(index, "%i", start_index);
|
||||||
// start nvs
|
// start nvs
|
||||||
nvs_handle handle = get_nvs_handle();
|
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));
|
bool status = ((nvs_set_u8(handle, (const char *)index, *values) == ESP_OK) && (nvs_commit(handle) == ESP_OK));
|
||||||
// close nvs
|
// close nvs
|
||||||
nvs_close(handle);
|
nvs_close(handle);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NVM memory is memory mapped so reading it is easy.
|
|
||||||
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self,
|
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self,
|
||||||
uint32_t start_index, uint32_t len, uint8_t* values) {
|
uint32_t start_index, uint32_t len, uint8_t* values) {
|
||||||
char index[9];
|
char index[9];
|
||||||
sprintf(index, "%i", start_index - CIRCUITPY_INTERNAL_NVM_START_ADDR);
|
sprintf(index, "%i", start_index);
|
||||||
// start nvs
|
// start nvs
|
||||||
nvs_handle handle = get_nvs_handle();
|
nvs_handle_t handle;
|
||||||
|
get_nvs_handle(&handle);
|
||||||
if (nvs_get_u8(handle, (const char *)index, values) != ESP_OK) {
|
if (nvs_get_u8(handle, (const char *)index, values) != ESP_OK) {
|
||||||
mp_raise_RuntimeError(translate("NVS Error"));
|
mp_raise_RuntimeError(translate("NVS Error"));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user