summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Shawcroft <scott@adafruit.com>2020-11-24 14:45:35 -0800
committerGitHub <noreply@github.com>2020-11-24 14:45:35 -0800
commit669f17e5bfdbf4a253e438f94f5f66ec612f9360 (patch)
tree170ef30dcb5ad8030dd3fc0ef6b9cc319a084a2c
parentd0e75e635ab0433217de28aa8f59b3a9e8f057c4 (diff)
parentbbe13490b55bceed477b28c7ed4256db69cca84b (diff)
Merge pull request #3678 from microDev1/nvm-s2
ESP32S2: Support for NVM
-rw-r--r--locale/circuitpython.pot6
-rw-r--r--ports/esp32s2/Makefile2
-rw-r--r--ports/esp32s2/common-hal/microcontroller/__init__.c12
-rw-r--r--ports/esp32s2/common-hal/nvm/ByteArray.c97
-rw-r--r--ports/esp32s2/common-hal/nvm/ByteArray.h38
-rw-r--r--ports/esp32s2/common-hal/nvm/__init__.c1
-rw-r--r--ports/esp32s2/mpconfigport.h7
-rw-r--r--ports/esp32s2/mpconfigport.mk2
8 files changed, 161 insertions, 4 deletions
diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot
index c184d6931..e8a9e412f 100644
--- a/locale/circuitpython.pot
+++ b/locale/circuitpython.pot
@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2020-11-23 10:10-0600\n"
+"POT-Creation-Date: 2020-11-11 16:30+0530\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -1262,6 +1262,10 @@ msgstr ""
msgid "Must use a multiple of 6 rgb pins, not %d"
msgstr ""
+#: ports/esp32s2/common-hal/nvm/ByteArray.c
+msgid "NVS Error"
+msgstr ""
+
#: py/parse.c
msgid "Name too long"
msgstr ""
diff --git a/ports/esp32s2/Makefile b/ports/esp32s2/Makefile
index 931e4d063..0b5aa448a 100644
--- a/ports/esp32s2/Makefile
+++ b/ports/esp32s2/Makefile
@@ -104,6 +104,8 @@ INC += -isystem esp-idf/components/soc/soc/include
INC += -isystem esp-idf/components/soc/soc/esp32s2/include
INC += -isystem esp-idf/components/heap/include
INC += -isystem esp-idf/components/esp_system/include
+INC += -isystem esp-idf/components/spi_flash/include
+INC += -isystem esp-idf/components/nvs_flash/include
INC += -I$(BUILD)/esp-idf/config
CFLAGS += -DHAVE_CONFIG_H \
diff --git a/ports/esp32s2/common-hal/microcontroller/__init__.c b/ports/esp32s2/common-hal/microcontroller/__init__.c
index dba12b88b..05f26c3fe 100644
--- a/ports/esp32s2/common-hal/microcontroller/__init__.c
+++ b/ports/esp32s2/common-hal/microcontroller/__init__.c
@@ -35,6 +35,7 @@
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/Processor.h"
+#include "shared-bindings/nvm/ByteArray.h"
#include "supervisor/filesystem.h"
#include "supervisor/shared/safe_mode.h"
@@ -85,6 +86,17 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
},
};
+#if CIRCUITPY_INTERNAL_NVM_SIZE > 0
+// The singleton nvm.ByteArray object.
+const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
+ .base = {
+ .type = &nvm_bytearray_type,
+ },
+ .start_address = (uint8_t*) CIRCUITPY_INTERNAL_NVM_START_ADDR,
+ .len = CIRCUITPY_INTERNAL_NVM_SIZE,
+};
+#endif
+
#if CIRCUITPY_WATCHDOG
// The singleton watchdog.WatchDogTimer object.
watchdog_watchdogtimer_obj_t common_hal_mcu_watchdogtimer_obj = {
diff --git a/ports/esp32s2/common-hal/nvm/ByteArray.c b/ports/esp32s2/common-hal/nvm/ByteArray.c
new file mode 100644
index 000000000..71321e7e6
--- /dev/null
+++ b/ports/esp32s2/common-hal/nvm/ByteArray.c
@@ -0,0 +1,97 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 microDev
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "common-hal/nvm/ByteArray.h"
+
+#include "py/runtime.h"
+#include "nvs_flash.h"
+
+uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
+ return self->len;
+}
+
+static void get_nvs_handle(nvs_handle_t * nvs_handle) {
+ // Initialize NVS
+ esp_err_t err = nvs_flash_init();
+ if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
+ // NVS partition was truncated and needs to be erased
+ // Retry nvs_flash_init
+ ESP_ERROR_CHECK(nvs_flash_erase());
+ err = nvs_flash_init();
+ }
+ ESP_ERROR_CHECK(err);
+
+ // Open NVS handle
+ if (nvs_open("CPY", NVS_READWRITE, nvs_handle) != ESP_OK) {
+ mp_raise_RuntimeError(translate("NVS Error"));
+ }
+}
+
+bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
+ uint32_t start_index, uint8_t* values, uint32_t len) {
+ char index[9];
+
+ // start nvs
+ nvs_handle_t handle;
+ get_nvs_handle(&handle);
+
+ // stage flash changes
+ for (uint32_t i = 0; i < len; i++) {
+ sprintf(index, "%i", start_index + i);
+ if (nvs_set_u8(handle, (const char *)index, values[i]) != ESP_OK) {
+ return false;
+ }
+ }
+
+ // commit flash changes
+ if (nvs_commit(handle) != ESP_OK) {
+ return false;
+ }
+
+ // close nvs
+ nvs_close(handle);
+ return true;
+}
+
+void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self,
+ uint32_t start_index, uint32_t len, uint8_t* values) {
+ char index[9];
+
+ // start nvs
+ nvs_handle_t handle;
+ get_nvs_handle(&handle);
+
+ // get from flash
+ for (uint32_t i = 0; i < len; i++) {
+ sprintf(index, "%i", start_index + i);
+ if (nvs_get_u8(handle, (const char *)index, &values[i]) != ESP_OK) {
+ mp_raise_RuntimeError(translate("NVS Error"));
+ }
+ }
+
+ // close nvs
+ nvs_close(handle);
+}
diff --git a/ports/esp32s2/common-hal/nvm/ByteArray.h b/ports/esp32s2/common-hal/nvm/ByteArray.h
new file mode 100644
index 000000000..44b63d724
--- /dev/null
+++ b/ports/esp32s2/common-hal/nvm/ByteArray.h
@@ -0,0 +1,38 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 microDev
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_NVM_BYTEARRAY_H
+#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_NVM_BYTEARRAY_H
+
+#include "py/obj.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ uint8_t* start_address;
+ uint32_t len;
+} nvm_bytearray_obj_t;
+
+#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_NVM_BYTEARRAY_H
diff --git a/ports/esp32s2/common-hal/nvm/__init__.c b/ports/esp32s2/common-hal/nvm/__init__.c
new file mode 100644
index 000000000..1b702a158
--- /dev/null
+++ b/ports/esp32s2/common-hal/nvm/__init__.c
@@ -0,0 +1 @@
+// No nvm module functions.
diff --git a/ports/esp32s2/mpconfigport.h b/ports/esp32s2/mpconfigport.h
index cec8dd35d..db7393d8e 100644
--- a/ports/esp32s2/mpconfigport.h
+++ b/ports/esp32s2/mpconfigport.h
@@ -28,7 +28,6 @@
#ifndef ESP32S2_MPCONFIGPORT_H__
#define ESP32S2_MPCONFIGPORT_H__
-#define CIRCUITPY_INTERNAL_NVM_SIZE (0)
#define MICROPY_NLR_THUMB (0)
#define MICROPY_PY_UJSON (1)
@@ -36,11 +35,15 @@
#include "py/circuitpy_mpconfig.h"
-
#define MICROPY_PORT_ROOT_POINTERS \
CIRCUITPY_COMMON_ROOT_POINTERS
#define MICROPY_NLR_SETJMP (1)
#define CIRCUITPY_DEFAULT_STACK_SIZE 0x6000
+#define CIRCUITPY_INTERNAL_NVM_START_ADDR (0x9000)
+
+#ifndef CIRCUITPY_INTERNAL_NVM_SIZE
+#define CIRCUITPY_INTERNAL_NVM_SIZE (20 * 1024)
+#endif
#endif // __INCLUDED_ESP32S2_MPCONFIGPORT_H
diff --git a/ports/esp32s2/mpconfigport.mk b/ports/esp32s2/mpconfigport.mk
index 93f222cb3..8ba5b0f99 100644
--- a/ports/esp32s2/mpconfigport.mk
+++ b/ports/esp32s2/mpconfigport.mk
@@ -21,7 +21,7 @@ CIRCUITPY_COUNTIO = 1
CIRCUITPY_FREQUENCYIO = 1
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_ROTARYIO = 1
-CIRCUITPY_NVM = 0
+CIRCUITPY_NVM = 1
# We don't have enough endpoints to include MIDI.
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_WIFI = 1