27
27
#include "common-hal/nvm/ByteArray.h"
28
28
29
29
#include "py/runtime.h"
30
-
31
30
#include "nvs_flash.h"
32
31
33
32
uint32_t common_hal_nvm_bytearray_get_length (nvm_bytearray_obj_t * self ) {
34
33
return self -> len ;
35
34
}
36
35
37
- static nvs_handle get_nvs_handle (void ) {
36
+ static void get_nvs_handle (nvs_handle_t * nvs_handle ) {
38
37
// Initialize NVS
39
38
esp_err_t err = nvs_flash_init ();
40
39
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) {
46
45
ESP_ERROR_CHECK (err );
47
46
48
47
// 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 ) {
51
49
mp_raise_RuntimeError (translate ("NVS Error" ));
52
50
}
53
- return nvs_handle ;
54
51
}
55
52
56
53
bool common_hal_nvm_bytearray_set_bytes (nvm_bytearray_obj_t * self ,
57
54
uint32_t start_index , uint8_t * values , uint32_t len ) {
58
55
char index [9 ];
59
- sprintf (index , "%i" , start_index - CIRCUITPY_INTERNAL_NVM_START_ADDR );
56
+ sprintf (index , "%i" , start_index );
60
57
// start nvs
61
- nvs_handle handle = get_nvs_handle ();
58
+ nvs_handle_t handle ;
59
+ get_nvs_handle (& handle );
62
60
bool status = ((nvs_set_u8 (handle , (const char * )index , * values ) == ESP_OK ) && (nvs_commit (handle ) == ESP_OK ));
63
61
// close nvs
64
62
nvs_close (handle );
65
63
return status ;
66
64
}
67
65
68
- // NVM memory is memory mapped so reading it is easy.
69
66
void common_hal_nvm_bytearray_get_bytes (nvm_bytearray_obj_t * self ,
70
67
uint32_t start_index , uint32_t len , uint8_t * values ) {
71
68
char index [9 ];
72
- sprintf (index , "%i" , start_index - CIRCUITPY_INTERNAL_NVM_START_ADDR );
69
+ sprintf (index , "%i" , start_index );
73
70
// start nvs
74
- nvs_handle handle = get_nvs_handle ();
71
+ nvs_handle_t handle ;
72
+ get_nvs_handle (& handle );
75
73
if (nvs_get_u8 (handle , (const char * )index , values ) != ESP_OK ) {
76
74
mp_raise_RuntimeError (translate ("NVS Error" ));
77
75
}
0 commit comments