Skip to content

Commit 35ef6c6

Browse files
committed
nvm implementation for esp32s2
1 parent ddb3590 commit 35ef6c6

File tree

8 files changed

+147
-5
lines changed

8 files changed

+147
-5
lines changed

locale/circuitpython.pot

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-11-04 21:18+0530\n"
11+
"POT-Creation-Date: 2020-11-11 16:30+0530\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -296,7 +296,8 @@ msgstr ""
296296
msgid "All I2C peripherals are in use"
297297
msgstr ""
298298

299-
#: ports/esp32s2/peripherals/pcnt_handler.c
299+
#: ports/esp32s2/common-hal/countio/Counter.c
300+
#: ports/esp32s2/common-hal/rotaryio/IncrementalEncoder.c
300301
msgid "All PCNT units in use"
301302
msgstr ""
302303

@@ -1248,6 +1249,10 @@ msgstr ""
12481249
msgid "Must use a multiple of 6 rgb pins, not %d"
12491250
msgstr ""
12501251

1252+
#: ports/esp32s2/common-hal/nvm/ByteArray.c
1253+
msgid "NVS Error"
1254+
msgstr ""
1255+
12511256
#: py/parse.c
12521257
msgid "Name too long"
12531258
msgstr ""
@@ -3201,6 +3206,7 @@ msgstr ""
32013206
msgid "pow() with 3 arguments requires integers"
32023207
msgstr ""
32033208

3209+
#: ports/esp32s2/boards/adafruit_magtag_2.9_grayscale/mpconfigboard.h
32043210
#: ports/esp32s2/boards/adafruit_metro_esp32s2/mpconfigboard.h
32053211
#: ports/esp32s2/boards/electroniccats_bastwifi/mpconfigboard.h
32063212
#: ports/esp32s2/boards/espressif_kaluga_1/mpconfigboard.h

ports/esp32s2/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ INC += -isystem esp-idf/components/soc/soc/include
104104
INC += -isystem esp-idf/components/soc/soc/esp32s2/include
105105
INC += -isystem esp-idf/components/heap/include
106106
INC += -isystem esp-idf/components/esp_system/include
107+
INC += -isystem esp-idf/components/spi_flash/include
108+
INC += -isystem esp-idf/components/nvs_flash/include
107109
INC += -I$(BUILD)/esp-idf/config
108110

109111
CFLAGS += -DHAVE_CONFIG_H \

ports/esp32s2/common-hal/microcontroller/__init__.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "shared-bindings/microcontroller/__init__.h"
3636
#include "shared-bindings/microcontroller/Pin.h"
3737
#include "shared-bindings/microcontroller/Processor.h"
38+
#include "shared-bindings/nvm/ByteArray.h"
3839

3940
#include "supervisor/filesystem.h"
4041
#include "supervisor/shared/safe_mode.h"
@@ -85,6 +86,17 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
8586
},
8687
};
8788

89+
#if CIRCUITPY_INTERNAL_NVM_SIZE > 0
90+
// The singleton nvm.ByteArray object.
91+
const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
92+
.base = {
93+
.type = &nvm_bytearray_type,
94+
},
95+
.start_address = (uint8_t*) CIRCUITPY_INTERNAL_NVM_START_ADDR,
96+
.len = CIRCUITPY_INTERNAL_NVM_SIZE,
97+
};
98+
#endif
99+
88100
// This maps MCU pin names to pin objects.
89101
STATIC const mp_rom_map_elem_t mcu_pin_global_dict_table[] = {
90102
{ MP_ROM_QSTR(MP_QSTR_GPIO0), MP_ROM_PTR(&pin_GPIO0) },
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "common-hal/nvm/ByteArray.h"
28+
29+
#include "py/runtime.h"
30+
31+
#include "nvs_flash.h"
32+
33+
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
34+
return self->len;
35+
}
36+
37+
static nvs_handle get_nvs_handle(void) {
38+
// Initialize NVS
39+
esp_err_t err = nvs_flash_init();
40+
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
41+
// NVS partition was truncated and needs to be erased
42+
// Retry nvs_flash_init
43+
ESP_ERROR_CHECK(nvs_flash_erase());
44+
err = nvs_flash_init();
45+
}
46+
ESP_ERROR_CHECK(err);
47+
48+
// Open NVS handle
49+
nvs_handle nvs_handle;
50+
if (nvs_open("CPY", NVS_READWRITE, &nvs_handle) != ESP_OK) {
51+
mp_raise_RuntimeError(translate("NVS Error"));
52+
}
53+
return nvs_handle;
54+
}
55+
56+
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
57+
uint32_t start_index, uint8_t* values, uint32_t len) {
58+
char index[9];
59+
sprintf(index, "%i", start_index);
60+
// start nvs
61+
nvs_handle handle = get_nvs_handle();
62+
bool status = ((nvs_set_u8(handle, (const char *)index, *values) == ESP_OK) && (nvs_commit(handle) == ESP_OK));
63+
// close nvs
64+
nvs_close(handle);
65+
return status;
66+
}
67+
68+
// NVM memory is memory mapped so reading it is easy.
69+
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self,
70+
uint32_t start_index, uint32_t len, uint8_t* values) {
71+
char index[9];
72+
sprintf(index, "%i", start_index);
73+
// start nvs
74+
nvs_handle handle = get_nvs_handle();
75+
if (nvs_get_u8(handle, (const char *)index, values) != ESP_OK) {
76+
mp_raise_RuntimeError(translate("NVS Error"));
77+
}
78+
// close nvs
79+
nvs_close(handle);
80+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_NVM_BYTEARRAY_H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_NVM_BYTEARRAY_H
29+
30+
#include "py/obj.h"
31+
32+
typedef struct {
33+
mp_obj_base_t base;
34+
uint8_t* start_address;
35+
uint32_t len;
36+
} nvm_bytearray_obj_t;
37+
38+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_NVM_BYTEARRAY_H
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// No nvm module functions.

ports/esp32s2/mpconfigport.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@
2828
#ifndef ESP32S2_MPCONFIGPORT_H__
2929
#define ESP32S2_MPCONFIGPORT_H__
3030

31-
#define CIRCUITPY_INTERNAL_NVM_SIZE (0)
3231
#define MICROPY_NLR_THUMB (0)
3332

3433
#define MICROPY_PY_UJSON (1)
3534
#define MICROPY_USE_INTERNAL_PRINTF (0)
3635

3736
#include "py/circuitpy_mpconfig.h"
3837

39-
4038
#define MICROPY_PORT_ROOT_POINTERS \
4139
CIRCUITPY_COMMON_ROOT_POINTERS
4240
#define MICROPY_NLR_SETJMP (1)
4341
#define CIRCUITPY_DEFAULT_STACK_SIZE 0x6000
4442

43+
#define CIRCUITPY_INTERNAL_NVM_START_ADDR (0x9000)
44+
45+
#ifndef CIRCUITPY_INTERNAL_NVM_SIZE
46+
#define CIRCUITPY_INTERNAL_NVM_SIZE (20000)
47+
#endif
4548

4649
#endif // __INCLUDED_ESP32S2_MPCONFIGPORT_H

ports/esp32s2/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ CIRCUITPY_COUNTIO = 1
2121
CIRCUITPY_FREQUENCYIO = 0
2222
CIRCUITPY_I2CPERIPHERAL = 0
2323
CIRCUITPY_ROTARYIO = 1
24-
CIRCUITPY_NVM = 0
24+
CIRCUITPY_NVM = 1
2525
# We don't have enough endpoints to include MIDI.
2626
CIRCUITPY_USB_MIDI = 0
2727
CIRCUITPY_WIFI = 1

0 commit comments

Comments
 (0)