Skip to content

Commit 669f17e

Browse files
authored
Merge pull request adafruit#3678 from microDev1/nvm-s2
ESP32S2: Support for NVM
2 parents d0e75e6 + bbe1349 commit 669f17e

File tree

8 files changed

+161
-4
lines changed

8 files changed

+161
-4
lines changed

locale/circuitpython.pot

Lines changed: 5 additions & 1 deletion
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-23 10:10-0600\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 <LL@li.org>\n"
@@ -1262,6 +1262,10 @@ msgstr ""
12621262
msgid "Must use a multiple of 6 rgb pins, not %d"
12631263
msgstr ""
12641264

1265+
#: ports/esp32s2/common-hal/nvm/ByteArray.c
1266+
msgid "NVS Error"
1267+
msgstr ""
1268+
12651269
#: py/parse.c
12661270
msgid "Name too long"
12671271
msgstr ""

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
#if CIRCUITPY_WATCHDOG
89101
// The singleton watchdog.WatchDogTimer object.
90102
watchdog_watchdogtimer_obj_t common_hal_mcu_watchdogtimer_obj = {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
#include "nvs_flash.h"
31+
32+
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
33+
return self->len;
34+
}
35+
36+
static void get_nvs_handle(nvs_handle_t * nvs_handle) {
37+
// Initialize NVS
38+
esp_err_t err = nvs_flash_init();
39+
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
40+
// NVS partition was truncated and needs to be erased
41+
// Retry nvs_flash_init
42+
ESP_ERROR_CHECK(nvs_flash_erase());
43+
err = nvs_flash_init();
44+
}
45+
ESP_ERROR_CHECK(err);
46+
47+
// Open NVS handle
48+
if (nvs_open("CPY", NVS_READWRITE, nvs_handle) != ESP_OK) {
49+
mp_raise_RuntimeError(translate("NVS Error"));
50+
}
51+
}
52+
53+
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
54+
uint32_t start_index, uint8_t* values, uint32_t len) {
55+
char index[9];
56+
57+
// start nvs
58+
nvs_handle_t handle;
59+
get_nvs_handle(&handle);
60+
61+
// stage flash changes
62+
for (uint32_t i = 0; i < len; i++) {
63+
sprintf(index, "%i", start_index + i);
64+
if (nvs_set_u8(handle, (const char *)index, values[i]) != ESP_OK) {
65+
return false;
66+
}
67+
}
68+
69+
// commit flash changes
70+
if (nvs_commit(handle) != ESP_OK) {
71+
return false;
72+
}
73+
74+
// close nvs
75+
nvs_close(handle);
76+
return true;
77+
}
78+
79+
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self,
80+
uint32_t start_index, uint32_t len, uint8_t* values) {
81+
char index[9];
82+
83+
// start nvs
84+
nvs_handle_t handle;
85+
get_nvs_handle(&handle);
86+
87+
// get from flash
88+
for (uint32_t i = 0; i < len; i++) {
89+
sprintf(index, "%i", start_index + i);
90+
if (nvs_get_u8(handle, (const char *)index, &values[i]) != ESP_OK) {
91+
mp_raise_RuntimeError(translate("NVS Error"));
92+
}
93+
}
94+
95+
// close nvs
96+
nvs_close(handle);
97+
}
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 (20 * 1024)
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 = 1
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy