Skip to content

Commit bc9036f

Browse files
committed
use pointer to get nvs handle
1 parent 0686cde commit bc9036f

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@
2727
#include "common-hal/nvm/ByteArray.h"
2828

2929
#include "py/runtime.h"
30-
3130
#include "nvs_flash.h"
3231

3332
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
3433
return self->len;
3534
}
3635

37-
static nvs_handle get_nvs_handle(void) {
36+
static void get_nvs_handle(nvs_handle_t * nvs_handle) {
3837
// Initialize NVS
3938
esp_err_t err = nvs_flash_init();
4039
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) {
4645
ESP_ERROR_CHECK(err);
4746

4847
// Open NVS handle
49-
nvs_handle nvs_handle;
50-
if (nvs_open("CPY", NVS_READWRITE, &nvs_handle) != ESP_OK) {
48+
if (nvs_open("CPY", NVS_READWRITE, nvs_handle) != ESP_OK) {
5149
mp_raise_RuntimeError(translate("NVS Error"));
5250
}
53-
return nvs_handle;
5451
}
5552

5653
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
5754
uint32_t start_index, uint8_t* values, uint32_t len) {
5855
char index[9];
59-
sprintf(index, "%i", start_index - CIRCUITPY_INTERNAL_NVM_START_ADDR);
56+
sprintf(index, "%i", start_index);
6057
// start nvs
61-
nvs_handle handle = get_nvs_handle();
58+
nvs_handle_t handle;
59+
get_nvs_handle(&handle);
6260
bool status = ((nvs_set_u8(handle, (const char *)index, *values) == ESP_OK) && (nvs_commit(handle) == ESP_OK));
6361
// close nvs
6462
nvs_close(handle);
6563
return status;
6664
}
6765

68-
// NVM memory is memory mapped so reading it is easy.
6966
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self,
7067
uint32_t start_index, uint32_t len, uint8_t* values) {
7168
char index[9];
72-
sprintf(index, "%i", start_index - CIRCUITPY_INTERNAL_NVM_START_ADDR);
69+
sprintf(index, "%i", start_index);
7370
// start nvs
74-
nvs_handle handle = get_nvs_handle();
71+
nvs_handle_t handle;
72+
get_nvs_handle(&handle);
7573
if (nvs_get_u8(handle, (const char *)index, values) != ESP_OK) {
7674
mp_raise_RuntimeError(translate("NVS Error"));
7775
}

0 commit comments

Comments
 (0)