From f8e5e2da64fbf2131d77a0834de0b91b102afdee Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Thu, 31 Jan 2019 10:50:07 +1100 Subject: Start on nRF nvm.ByteArray adafruit/circuitpython#1042 (doesn't do anything yet) --- .../nrf/boards/feather_nrf52840_express/mpconfigboard.h | 2 +- ports/nrf/common-hal/microcontroller/__init__.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ports/nrf/boards/feather_nrf52840_express/mpconfigboard.h b/ports/nrf/boards/feather_nrf52840_express/mpconfigboard.h index eb8be7608..21232d2b3 100644 --- a/ports/nrf/boards/feather_nrf52840_express/mpconfigboard.h +++ b/ports/nrf/boards/feather_nrf52840_express/mpconfigboard.h @@ -55,7 +55,7 @@ #define CIRCUITPY_AUTORELOAD_DELAY_MS 500 -// TODO #define CIRCUITPY_INTERNAL_NVM_SIZE 8192 +#define CIRCUITPY_INTERNAL_NVM_SIZE (4096) #define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE) diff --git a/ports/nrf/common-hal/microcontroller/__init__.c b/ports/nrf/common-hal/microcontroller/__init__.c index c2e0c5b41..f29b0eb95 100644 --- a/ports/nrf/common-hal/microcontroller/__init__.c +++ b/ports/nrf/common-hal/microcontroller/__init__.c @@ -24,9 +24,14 @@ * THE SOFTWARE. */ +#include "py/mphal.h" +#include "py/obj.h" +#include "py/runtime.h" + #include "common-hal/microcontroller/Pin.h" #include "common-hal/microcontroller/Processor.h" +#include "shared-bindings/nvm/ByteArray.h" #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Processor.h" @@ -63,6 +68,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, + }, + .len = CIRCUITPY_INTERNAL_NVM_SIZE, + .start_address = 0 +}; +#endif + STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_P0_00), MP_ROM_PTR(&pin_P0_00) }, -- cgit v1.2.3 From 492431a694283345109bc29f478d13463aeb5661 Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Thu, 31 Jan 2019 11:17:41 +1100 Subject: nvm.ByteArray reads & writes but no sensible erase yet adafruit/circuitpython#1042 --- .../feather_nrf52840_express/mpconfigboard.h | 1 + ports/nrf/common-hal/microcontroller/__init__.c | 2 +- ports/nrf/common-hal/nvm/ByteArray.c | 49 ++++++++++++++++++++++ ports/nrf/common-hal/nvm/ByteArray.h | 38 +++++++++++++++++ ports/nrf/common-hal/nvm/__init__.c | 27 ++++++++++++ 5 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 ports/nrf/common-hal/nvm/ByteArray.c create mode 100644 ports/nrf/common-hal/nvm/ByteArray.h create mode 100644 ports/nrf/common-hal/nvm/__init__.c diff --git a/ports/nrf/boards/feather_nrf52840_express/mpconfigboard.h b/ports/nrf/boards/feather_nrf52840_express/mpconfigboard.h index 21232d2b3..d007a1812 100644 --- a/ports/nrf/boards/feather_nrf52840_express/mpconfigboard.h +++ b/ports/nrf/boards/feather_nrf52840_express/mpconfigboard.h @@ -32,6 +32,7 @@ #define MICROPY_HW_BOARD_NAME "Adafruit Feather nRF52840 Express" #define MICROPY_HW_MCU_NAME "nRF52840" #define MICROPY_PY_SYS_PLATFORM "Feather52840Express" +#define FLASH_SIZE (0x100000) #define MICROPY_HW_NEOPIXEL (&pin_P0_16) diff --git a/ports/nrf/common-hal/microcontroller/__init__.c b/ports/nrf/common-hal/microcontroller/__init__.c index f29b0eb95..7055cb236 100644 --- a/ports/nrf/common-hal/microcontroller/__init__.c +++ b/ports/nrf/common-hal/microcontroller/__init__.c @@ -75,7 +75,7 @@ const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = { .type = &nvm_bytearray_type, }, .len = CIRCUITPY_INTERNAL_NVM_SIZE, - .start_address = 0 + .start_address = FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE }; #endif diff --git a/ports/nrf/common-hal/nvm/ByteArray.c b/ports/nrf/common-hal/nvm/ByteArray.c new file mode 100644 index 000000000..6298ce18e --- /dev/null +++ b/ports/nrf/common-hal/nvm/ByteArray.c @@ -0,0 +1,49 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Nick Moore for Adafruit Industries + * + * 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 +#include + +#include "nrf_nvmc.h" + +uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) { + return self->len; +} + +bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self, + uint32_t start_index, uint8_t* values, uint32_t len) { + printf("%ld %ld\n", start_index, len); + nrf_nvmc_page_erase(self->start_address); + nrf_nvmc_write_bytes(self->start_address + start_index, values, len); + return true; +} + +void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self, + uint32_t start_index, uint32_t len, uint8_t* values) { + memcpy(values, (uint8_t *)(self->start_address + start_index), len); +} diff --git a/ports/nrf/common-hal/nvm/ByteArray.h b/ports/nrf/common-hal/nvm/ByteArray.h new file mode 100644 index 000000000..e47a87b9e --- /dev/null +++ b/ports/nrf/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) 2019 Nick Moore for Adafruit Industries + * + * 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_NRF_COMMON_HAL_NVM_BYTEARRAY_H +#define MICROPY_INCLUDED_NRF_COMMON_HAL_NVM_BYTEARRAY_H + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint32_t start_address; + uint32_t len; +} nvm_bytearray_obj_t; + +#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_NVM_BYTEARRAY_H diff --git a/ports/nrf/common-hal/nvm/__init__.c b/ports/nrf/common-hal/nvm/__init__.c new file mode 100644 index 000000000..3cdc9d3a4 --- /dev/null +++ b/ports/nrf/common-hal/nvm/__init__.c @@ -0,0 +1,27 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Nick Moore for Adafruit Industries + * + * 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. + */ + +// No nvm module functions. -- cgit v1.2.3 From 592bd0140a22b34daf44ad6d648ee0c642ab45de Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Tue, 9 Apr 2019 10:55:15 +1000 Subject: switch CIRCUITPY_NVM on! --- ports/nrf/mpconfigport.mk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/nrf/mpconfigport.mk b/ports/nrf/mpconfigport.mk index fdeb1bbbe..83166708a 100644 --- a/ports/nrf/mpconfigport.mk +++ b/ports/nrf/mpconfigport.mk @@ -19,8 +19,8 @@ CIRCUITPY_AUDIOBUSIO = 0 # No I2CSlave implementation CIRCUITPY_I2CSLAVE = 0 -# nvm not yet implemented -CIRCUITPY_NVM = 0 +# enable NVM +CIRCUITPY_NVM = 1 # enable RTC CIRCUITPY_RTC = 1 -- cgit v1.2.3 From 8e7fee22465c556f0f5e1b83c99313a3657652f9 Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Thu, 31 Jan 2019 15:48:19 +1100 Subject: Working flash pages for nvm.ByteArray adafruit/circuitpython#1042 import microcontroller def dump(n = microcontroller.nvm): for i in range(0,len(n)): print ("%02X " % n[i], end="") if i % 16 == 15: print('') microcontroller.nvm[0:4096] = bytes([1,2,3,4,5,6,7,8]) * 512 microcontroller.nvm[4096:8192] = bytes([16,17,18,19]) * 1024 microcontroller.nvm[4090:4101] = b'thisisatest' microcontroller.nvm[100:105] = b'hello' microcontroller.nvm[8000:8007] = b'goodbye' dump() --- .../feather_nrf52840_express/mpconfigboard.h | 2 ++ ports/nrf/common-hal/nvm/ByteArray.c | 34 ++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/ports/nrf/boards/feather_nrf52840_express/mpconfigboard.h b/ports/nrf/boards/feather_nrf52840_express/mpconfigboard.h index d007a1812..e1f43e3c9 100644 --- a/ports/nrf/boards/feather_nrf52840_express/mpconfigboard.h +++ b/ports/nrf/boards/feather_nrf52840_express/mpconfigboard.h @@ -32,7 +32,9 @@ #define MICROPY_HW_BOARD_NAME "Adafruit Feather nRF52840 Express" #define MICROPY_HW_MCU_NAME "nRF52840" #define MICROPY_PY_SYS_PLATFORM "Feather52840Express" + #define FLASH_SIZE (0x100000) +#define FLASH_PAGE_SIZE (4096) #define MICROPY_HW_NEOPIXEL (&pin_P0_16) diff --git a/ports/nrf/common-hal/nvm/ByteArray.c b/ports/nrf/common-hal/nvm/ByteArray.c index 6298ce18e..7393ce0a5 100644 --- a/ports/nrf/common-hal/nvm/ByteArray.c +++ b/ports/nrf/common-hal/nvm/ByteArray.c @@ -35,11 +35,39 @@ uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) { return self->len; } +static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_t *bytes) { + // Write a whole page to flash, buffering it first and then erasing and rewriting + // it since we can only clear a whole page at a time. + // TODO (maybe) check if only clearing bits (don't need to erase) + // XXX should this suspend interrupts so erase/write is atomic? + + if (offset == 0 && len == FLASH_PAGE_SIZE) { + nrf_nvmc_page_erase(page_addr); + nrf_nvmc_write_bytes(page_addr, bytes, FLASH_PAGE_SIZE); + } else { + uint8_t buffer[FLASH_PAGE_SIZE]; + memcpy(buffer, (uint8_t *)page_addr, FLASH_PAGE_SIZE); + memcpy(buffer + offset, bytes, len); + nrf_nvmc_page_erase(page_addr); + nrf_nvmc_write_bytes(page_addr, buffer, FLASH_PAGE_SIZE); + } +} + bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self, uint32_t start_index, uint8_t* values, uint32_t len) { - printf("%ld %ld\n", start_index, len); - nrf_nvmc_page_erase(self->start_address); - nrf_nvmc_write_bytes(self->start_address + start_index, values, len); + + uint32_t address = self->start_address + start_index; + uint32_t offset = address % FLASH_PAGE_SIZE; + uint32_t page_addr = address - offset; + + while (len) { + uint32_t write_len = MIN(len, FLASH_PAGE_SIZE - offset); + write_page(page_addr, offset, write_len, values); + len -= write_len; + values += write_len; + page_addr += FLASH_PAGE_SIZE; + offset = 0; + } return true; } -- cgit v1.2.3 From cd69db777034ea7bbda1ed19da742a032767e01f Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Thu, 4 Apr 2019 15:40:12 +1100 Subject: Add a peripherals/nrf/nvm.c to wrap flash page writes safely --- ports/nrf/Makefile | 1 + ports/nrf/peripherals/nrf/nvm.c | 96 +++++++++++++++++++++++++++++++++++++++++ ports/nrf/peripherals/nrf/nvm.h | 28 ++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 ports/nrf/peripherals/nrf/nvm.c create mode 100644 ports/nrf/peripherals/nrf/nvm.h diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 3cb4b52aa..b32b25217 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -177,6 +177,7 @@ SRC_C += \ peripherals/nrf/clocks.c \ peripherals/nrf/$(MCU_CHIP)/pins.c \ peripherals/nrf/$(MCU_CHIP)/power.c \ + peripherals/nrf/nvm.c \ peripherals/nrf/timers.c \ sd_mutex.c \ supervisor/shared/memory.c diff --git a/ports/nrf/peripherals/nrf/nvm.c b/ports/nrf/peripherals/nrf/nvm.c new file mode 100644 index 000000000..f9cc1b806 --- /dev/null +++ b/ports/nrf/peripherals/nrf/nvm.c @@ -0,0 +1,96 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * Copyright (c) 2019 Nick Moore for Adafruit Industries + * + * 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 "py/obj.h" +#include "py/runtime.h" + +#include +#include + +#include "nrf_nvmc.h" + +#ifdef BLUETOOTH_SD +#include "ble_drv.h" +#include "nrf_sdm.h" + +#define FLASH_PAGE_SIZE (4096) + +STATIC void sd_flash_operation_start(void) { + sd_flash_operation_status = SD_FLASH_OPERATION_IN_PROGRESS; +} + +STATIC sd_flash_operation_status_t sd_flash_operation_wait_until_done(void) { + while (sd_flash_operation_status == SD_FLASH_OPERATION_IN_PROGRESS) { + sd_app_evt_wait(); + } + return sd_flash_operation_status; +} +#endif + +void nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data) { + #ifdef BLUETOOTH_SD + uint8_t sd_en = 0; + (void) sd_softdevice_is_enabled(&sd_en); + if (sd_en) { + uint32_t err_code; + sd_flash_operation_status_t status; + + sd_flash_operation_start(); + err_code = sd_flash_page_erase(page_addr / FLASH_PAGE_SIZE); + if (err_code != NRF_SUCCESS) { + mp_raise_OSError_msg_varg(translate("Flash erase failed to start, err 0x%04x"), err_code); + } + status = sd_flash_operation_wait_until_done(); + if (status == SD_FLASH_OPERATION_ERROR) { + mp_raise_OSError_msg(translate("Flash erase failed")); + } + + // Divide a full page into parts, because writing a full page causes an assertion failure. + // See https://devzone.nordicsemi.com/f/nordic-q-a/40088/sd_flash_write-cause-nrf_fault_id_sd_assert/ + const size_t BLOCK_PARTS = 2; + size_t words_to_write = FLASH_PAGE_SIZE / sizeof(uint32_t) / BLOCK_PARTS; + for (size_t i = 0; i < BLOCK_PARTS; i++) { + sd_flash_operation_start(); + err_code = sd_flash_write(((uint32_t *)page_addr) + i * words_to_write, + (uint32_t *)data + i * words_to_write, + words_to_write); + if (err_code != NRF_SUCCESS) { + mp_raise_OSError_msg_varg(translate("Flash write failed to start, err 0x%04x"), err_code); + } + status = sd_flash_operation_wait_until_done(); + if (status == SD_FLASH_OPERATION_ERROR) { + mp_raise_OSError_msg(translate("Flash write failed")); + } + } + + return; + } + #endif + + nrf_nvmc_page_erase(page_addr); + nrf_nvmc_write_bytes(page_addr, data, FLASH_PAGE_SIZE); +} diff --git a/ports/nrf/peripherals/nrf/nvm.h b/ports/nrf/peripherals/nrf/nvm.h new file mode 100644 index 000000000..e051bdbe5 --- /dev/null +++ b/ports/nrf/peripherals/nrf/nvm.h @@ -0,0 +1,28 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * Copyright (c) 2019 Nick Moore for Adafruit Industries + * + * 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. + */ + +void nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data); -- cgit v1.2.3 From bc9244180388860c831fb795d89fed2c09aa7a76 Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Thu, 4 Apr 2019 15:42:33 +1100 Subject: switch nvm.ByteArray to use new nrf_nvm_safe_flash_page_write adafruit/circuitpython#1610 --- ports/nrf/common-hal/nvm/ByteArray.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/ports/nrf/common-hal/nvm/ByteArray.c b/ports/nrf/common-hal/nvm/ByteArray.c index 7393ce0a5..d69abbdac 100644 --- a/ports/nrf/common-hal/nvm/ByteArray.c +++ b/ports/nrf/common-hal/nvm/ByteArray.c @@ -29,7 +29,7 @@ #include #include -#include "nrf_nvmc.h" +#include "peripherals/nrf/nvm.h" uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) { return self->len; @@ -38,18 +38,14 @@ uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) { static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_t *bytes) { // Write a whole page to flash, buffering it first and then erasing and rewriting // it since we can only clear a whole page at a time. - // TODO (maybe) check if only clearing bits (don't need to erase) - // XXX should this suspend interrupts so erase/write is atomic? if (offset == 0 && len == FLASH_PAGE_SIZE) { - nrf_nvmc_page_erase(page_addr); - nrf_nvmc_write_bytes(page_addr, bytes, FLASH_PAGE_SIZE); + nrf_nvm_safe_flash_page_write(page_addr, bytes); } else { uint8_t buffer[FLASH_PAGE_SIZE]; memcpy(buffer, (uint8_t *)page_addr, FLASH_PAGE_SIZE); memcpy(buffer + offset, bytes, len); - nrf_nvmc_page_erase(page_addr); - nrf_nvmc_write_bytes(page_addr, buffer, FLASH_PAGE_SIZE); + nrf_nvm_safe_flash_page_write(page_addr, buffer); } } -- cgit v1.2.3 From bcb87ffd6cd954e3fbdd2354cf83f9974c4d8547 Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Thu, 4 Apr 2019 15:43:40 +1100 Subject: Switch supervisor/internal_flash to use new nrf_nvm_safe_flash_page_write adafruit/circuitpython#1610 --- ports/nrf/supervisor/internal_flash.c | 88 ++++------------------------------- ports/nrf/supervisor/internal_flash.h | 1 - 2 files changed, 10 insertions(+), 79 deletions(-) diff --git a/ports/nrf/supervisor/internal_flash.c b/ports/nrf/supervisor/internal_flash.c index c18b708a3..9aaaf9b5d 100644 --- a/ports/nrf/supervisor/internal_flash.c +++ b/ports/nrf/supervisor/internal_flash.c @@ -35,7 +35,7 @@ #include "py/runtime.h" #include "lib/oofatfs/ff.h" -#include "nrf_nvmc.h" +#include "peripherals/nrf/nvm.h" #ifdef BLUETOOTH_SD #include "ble_drv.h" @@ -47,9 +47,8 @@ extern uint32_t __fatfs_flash_start_addr[]; extern uint32_t __fatfs_flash_length[]; #define NO_CACHE 0xffffffff -#define FL_PAGE_SZ 4096 -uint8_t _flash_cache[FL_PAGE_SZ] __attribute__((aligned(4))); +uint8_t _flash_cache[FLASH_PAGE_SIZE] __attribute__((aligned(4))); uint32_t _flash_page_addr = NO_CACHE; @@ -71,78 +70,11 @@ uint32_t supervisor_flash_get_block_count(void) { return ((uint32_t) __fatfs_flash_length) / FILESYSTEM_BLOCK_SIZE ; } -#ifdef BLUETOOTH_SD -STATIC void sd_flash_operation_start(void) { - sd_flash_operation_status = SD_FLASH_OPERATION_IN_PROGRESS; -} - -STATIC sd_flash_operation_status_t sd_flash_operation_wait_until_done(void) { - while (sd_flash_operation_status == SD_FLASH_OPERATION_IN_PROGRESS) { - sd_app_evt_wait(); - } - return sd_flash_operation_status; -} -#endif - -void supervisor_flash_flush(void) { - if (_flash_page_addr == NO_CACHE) return; - - // Skip if data is the same - if (memcmp(_flash_cache, (void *)_flash_page_addr, FL_PAGE_SZ) != 0) { - -#ifdef BLUETOOTH_SD - uint8_t sd_en = 0; - (void) sd_softdevice_is_enabled(&sd_en); - - if (sd_en) { - uint32_t err_code; - sd_flash_operation_status_t status; - - sd_flash_operation_start(); - err_code = sd_flash_page_erase(_flash_page_addr / FL_PAGE_SZ); - if (err_code != NRF_SUCCESS) { - mp_raise_OSError_msg_varg(translate("Flash erase failed to start, err 0x%04x"), err_code); - } - status = sd_flash_operation_wait_until_done(); - if (status == SD_FLASH_OPERATION_ERROR) { - mp_raise_OSError_msg(translate("Flash erase failed")); - } - - // Divide a full page into parts, because writing a full page causes an assertion failure. - // See https://devzone.nordicsemi.com/f/nordic-q-a/40088/sd_flash_write-cause-nrf_fault_id_sd_assert/ - const size_t BLOCK_PARTS = 2; - size_t words_to_write = FL_PAGE_SZ / sizeof(uint32_t) / BLOCK_PARTS; - for (size_t i = 0; i < BLOCK_PARTS; i++) { - sd_flash_operation_start(); - err_code = sd_flash_write(((uint32_t *)_flash_page_addr) + i * words_to_write, - (uint32_t *)_flash_cache + i * words_to_write, - words_to_write); - if (err_code != NRF_SUCCESS) { - mp_raise_OSError_msg_varg(translate("Flash write failed to start, err 0x%04x"), err_code); - } - status = sd_flash_operation_wait_until_done(); - if (status == SD_FLASH_OPERATION_ERROR) { - mp_raise_OSError_msg(translate("Flash write failed")); - } - } - } else { -#endif - nrf_nvmc_page_erase(_flash_page_addr); - nrf_nvmc_write_words(_flash_page_addr, (uint32_t *)_flash_cache, FL_PAGE_SZ / sizeof(uint32_t)); -#ifdef BLUETOOTH_SD - } -#endif - - } - _flash_page_addr = NO_CACHE; -} - -void supervisor_flash_release_cache(void) { -} - mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num_blocks) { // Must write out anything in cache before trying to read. - supervisor_flash_flush(); + nrf_nvm_safe_flash_page_write(_flash_page_addr, _flash_cache); + _flash_page_addr = NO_CACHE; + uint32_t src = lba2addr(block); memcpy(dest, (uint8_t*) src, FILESYSTEM_BLOCK_SIZE*num_blocks); return 0; // success @@ -151,21 +83,21 @@ mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t n mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32_t num_blocks) { while (num_blocks) { uint32_t const addr = lba2addr(lba); - uint32_t const page_addr = addr & ~(FL_PAGE_SZ - 1); + uint32_t const page_addr = addr & ~(FLASH_PAGE_SIZE - 1); uint32_t count = 8 - (lba % 8); // up to page boundary count = MIN(num_blocks, count); if (page_addr != _flash_page_addr) { - supervisor_flash_flush(); - + nrf_nvm_safe_flash_page_write(_flash_page_addr, _flash_cache); _flash_page_addr = page_addr; + // Copy the current contents of the entire page into the cache. - memcpy(_flash_cache, (void *)page_addr, FL_PAGE_SZ); + memcpy(_flash_cache, (void *)page_addr, FLASH_PAGE_SIZE); } // Overwrite part or all of the page cache with the src data. - memcpy(_flash_cache + (addr & (FL_PAGE_SZ - 1)), src, count * FILESYSTEM_BLOCK_SIZE); + memcpy(_flash_cache + (addr & (FLASH_PAGE_SIZE - 1)), src, count * FILESYSTEM_BLOCK_SIZE); // adjust for next run lba += count; diff --git a/ports/nrf/supervisor/internal_flash.h b/ports/nrf/supervisor/internal_flash.h index cf1dc91b5..7ea39f82e 100644 --- a/ports/nrf/supervisor/internal_flash.h +++ b/ports/nrf/supervisor/internal_flash.h @@ -32,7 +32,6 @@ #include "py/mpconfig.h" #define FLASH_PAGE_SIZE 0x1000 -#define CIRCUITPY_INTERNAL_NVM_SIZE 0 #define INTERNAL_FLASH_SYSTICK_MASK (0x1ff) // 512ms #define INTERNAL_FLASH_IDLE_TICK(tick) (((tick) & INTERNAL_FLASH_SYSTICK_MASK) == 2) -- cgit v1.2.3 From fdaff00c78e69644eddf828b9b23795db168f8d4 Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Thu, 4 Apr 2019 20:27:25 +1100 Subject: Make some space for NVM adafruit/circuitpython#1042 --- ports/nrf/boards/adafruit_nrf52840_s140_v6.ld | 1 - ports/nrf/common-hal/nvm/ByteArray.c | 13 ++++++++++--- ports/nrf/peripherals/nrf/nvm.c | 5 ++--- ports/nrf/supervisor/internal_flash.c | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/ports/nrf/boards/adafruit_nrf52840_s140_v6.ld b/ports/nrf/boards/adafruit_nrf52840_s140_v6.ld index 8a700d610..2587a19e3 100644 --- a/ports/nrf/boards/adafruit_nrf52840_s140_v6.ld +++ b/ports/nrf/boards/adafruit_nrf52840_s140_v6.ld @@ -11,7 +11,6 @@ 0x000ED000..0x000F3FFF (28KB ) Private Config Data (Bonding, Keys, etc.) 0x000AD000..0x000ECFFF (256KB) User Filesystem - 0x00026000..0x000ACFFF (540KB) Application Code (including ISR vector) 0x00001000..0x00025FFF (148KB) SoftDevice 0x00000000..0x00000FFF (4KB) Master Boot Record diff --git a/ports/nrf/common-hal/nvm/ByteArray.c b/ports/nrf/common-hal/nvm/ByteArray.c index d69abbdac..ee270f79b 100644 --- a/ports/nrf/common-hal/nvm/ByteArray.c +++ b/ports/nrf/common-hal/nvm/ByteArray.c @@ -31,8 +31,15 @@ #include "peripherals/nrf/nvm.h" +// defined in linker +extern uint32_t __fatfs_flash_start_addr[]; +extern uint32_t __fatfs_flash_length[]; + +#define NVM_START_ADDR ((uint32_t)__fatfs_flash_start_addr + \ + (uint32_t)__fatfs_flash_length - CIRCUITPY_INTERNAL_NVM_SIZE) + uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) { - return self->len; + return CIRCUITPY_INTERNAL_NVM_SIZE; } static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_t *bytes) { @@ -52,7 +59,7 @@ static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_ bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self, uint32_t start_index, uint8_t* values, uint32_t len) { - uint32_t address = self->start_address + start_index; + uint32_t address = NVM_START_ADDR + start_index; uint32_t offset = address % FLASH_PAGE_SIZE; uint32_t page_addr = address - offset; @@ -69,5 +76,5 @@ bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self, void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self, uint32_t start_index, uint32_t len, uint8_t* values) { - memcpy(values, (uint8_t *)(self->start_address + start_index), len); + memcpy(values, (uint8_t *)(NVM_START_ADDR + start_index), len); } diff --git a/ports/nrf/peripherals/nrf/nvm.c b/ports/nrf/peripherals/nrf/nvm.c index f9cc1b806..d8fddc4dc 100644 --- a/ports/nrf/peripherals/nrf/nvm.c +++ b/ports/nrf/peripherals/nrf/nvm.c @@ -25,7 +25,6 @@ * THE SOFTWARE. */ -//#include "py/obj.h" #include "py/runtime.h" #include @@ -33,12 +32,12 @@ #include "nrf_nvmc.h" +#define FLASH_PAGE_SIZE (4096) + #ifdef BLUETOOTH_SD #include "ble_drv.h" #include "nrf_sdm.h" -#define FLASH_PAGE_SIZE (4096) - STATIC void sd_flash_operation_start(void) { sd_flash_operation_status = SD_FLASH_OPERATION_IN_PROGRESS; } diff --git a/ports/nrf/supervisor/internal_flash.c b/ports/nrf/supervisor/internal_flash.c index 9aaaf9b5d..016d97076 100644 --- a/ports/nrf/supervisor/internal_flash.c +++ b/ports/nrf/supervisor/internal_flash.c @@ -67,7 +67,7 @@ uint32_t supervisor_flash_get_block_size(void) { } uint32_t supervisor_flash_get_block_count(void) { - return ((uint32_t) __fatfs_flash_length) / FILESYSTEM_BLOCK_SIZE ; + return ((uint32_t) __fatfs_flash_length - CIRCUITPY_INTERNVAL_NVM_SIZE) / FILESYSTEM_BLOCK_SIZE ; } mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num_blocks) { -- cgit v1.2.3 From 18908c21f7998b13493fe75d0020bd9ad1bd1b60 Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Tue, 9 Apr 2019 12:53:11 +1000 Subject: Fixups for adafruit/circuitpython#1042 --- ports/nrf/common-hal/microcontroller/__init__.c | 3 --- ports/nrf/peripherals/nrf/nvm.h | 6 ++++++ ports/nrf/supervisor/internal_flash.c | 18 +++++++++++++++--- ports/nrf/supervisor/internal_flash.h | 2 -- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ports/nrf/common-hal/microcontroller/__init__.c b/ports/nrf/common-hal/microcontroller/__init__.c index 7055cb236..ab7b5d5d6 100644 --- a/ports/nrf/common-hal/microcontroller/__init__.c +++ b/ports/nrf/common-hal/microcontroller/__init__.c @@ -74,12 +74,9 @@ const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = { .base = { .type = &nvm_bytearray_type, }, - .len = CIRCUITPY_INTERNAL_NVM_SIZE, - .start_address = FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE }; #endif - STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_P0_00), MP_ROM_PTR(&pin_P0_00) }, { MP_ROM_QSTR(MP_QSTR_P0_01), MP_ROM_PTR(&pin_P0_01) }, diff --git a/ports/nrf/peripherals/nrf/nvm.h b/ports/nrf/peripherals/nrf/nvm.h index e051bdbe5..4eac3d728 100644 --- a/ports/nrf/peripherals/nrf/nvm.h +++ b/ports/nrf/peripherals/nrf/nvm.h @@ -25,4 +25,10 @@ * THE SOFTWARE. */ +#define FLASH_PAGE_SIZE (4096) + +#ifndef CIRCUITPY_INTERNAL_NVM_SIZE +#define CIRCUITPY_INTERNAL_NVM_SIZE (0) +#endif + void nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data); diff --git a/ports/nrf/supervisor/internal_flash.c b/ports/nrf/supervisor/internal_flash.c index 016d97076..5713dea88 100644 --- a/ports/nrf/supervisor/internal_flash.c +++ b/ports/nrf/supervisor/internal_flash.c @@ -67,13 +67,21 @@ uint32_t supervisor_flash_get_block_size(void) { } uint32_t supervisor_flash_get_block_count(void) { - return ((uint32_t) __fatfs_flash_length - CIRCUITPY_INTERNVAL_NVM_SIZE) / FILESYSTEM_BLOCK_SIZE ; + return ((uint32_t) __fatfs_flash_length - CIRCUITPY_INTERNAL_NVM_SIZE) / FILESYSTEM_BLOCK_SIZE ; +} + +void supervisor_flash_flush(void) { + if (_flash_page_addr == NO_CACHE) return; + + // Skip if data is the same + if (memcmp(_flash_cache, (void *)_flash_page_addr, FLASH_PAGE_SIZE) != 0) { + nrf_nvm_safe_flash_page_write(_flash_page_addr, _flash_cache); + } } mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num_blocks) { // Must write out anything in cache before trying to read. - nrf_nvm_safe_flash_page_write(_flash_page_addr, _flash_cache); - _flash_page_addr = NO_CACHE; + supervisor_flash_flush(); uint32_t src = lba2addr(block); memcpy(dest, (uint8_t*) src, FILESYSTEM_BLOCK_SIZE*num_blocks); @@ -107,3 +115,7 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32 return 0; // success } + +void supervisor_flash_release_cache(void) { +} + diff --git a/ports/nrf/supervisor/internal_flash.h b/ports/nrf/supervisor/internal_flash.h index 7ea39f82e..024a53ebb 100644 --- a/ports/nrf/supervisor/internal_flash.h +++ b/ports/nrf/supervisor/internal_flash.h @@ -31,8 +31,6 @@ #include "py/mpconfig.h" -#define FLASH_PAGE_SIZE 0x1000 - #define INTERNAL_FLASH_SYSTICK_MASK (0x1ff) // 512ms #define INTERNAL_FLASH_IDLE_TICK(tick) (((tick) & INTERNAL_FLASH_SYSTICK_MASK) == 2) -- cgit v1.2.3 From 864910559bc712deed112e072f61fd482adea1a6 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 10 Apr 2019 21:41:55 -0400 Subject: fix subscripting --- ports/nrf/common-hal/microcontroller/__init__.c | 2 ++ ports/nrf/common-hal/nvm/ByteArray.c | 23 ++++++++++------------- ports/nrf/common-hal/nvm/ByteArray.h | 2 +- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/ports/nrf/common-hal/microcontroller/__init__.c b/ports/nrf/common-hal/microcontroller/__init__.c index ab7b5d5d6..92aac772a 100644 --- a/ports/nrf/common-hal/microcontroller/__init__.c +++ b/ports/nrf/common-hal/microcontroller/__init__.c @@ -74,6 +74,8 @@ const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = { .base = { .type = &nvm_bytearray_type, }, + .len = CIRCUITPY_INTERNAL_NVM_SIZE, + .start_address = (uint8_t*) (FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE), }; #endif diff --git a/ports/nrf/common-hal/nvm/ByteArray.c b/ports/nrf/common-hal/nvm/ByteArray.c index ee270f79b..4373e2502 100644 --- a/ports/nrf/common-hal/nvm/ByteArray.c +++ b/ports/nrf/common-hal/nvm/ByteArray.c @@ -35,37 +35,34 @@ extern uint32_t __fatfs_flash_start_addr[]; extern uint32_t __fatfs_flash_length[]; -#define NVM_START_ADDR ((uint32_t)__fatfs_flash_start_addr + \ - (uint32_t)__fatfs_flash_length - CIRCUITPY_INTERNAL_NVM_SIZE) - uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) { - return CIRCUITPY_INTERNAL_NVM_SIZE; + return self->len; } -static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_t *bytes) { - // Write a whole page to flash, buffering it first and then erasing and rewriting +static void write_page(uint8_t* page_addr, uint32_t offset, uint32_t len, uint8_t *bytes) { + // Write a whole page to flash, buffering it first and then erasing and rewriting // it since we can only clear a whole page at a time. if (offset == 0 && len == FLASH_PAGE_SIZE) { - nrf_nvm_safe_flash_page_write(page_addr, bytes); + nrf_nvm_safe_flash_page_write((uint32_t) page_addr, bytes); } else { uint8_t buffer[FLASH_PAGE_SIZE]; - memcpy(buffer, (uint8_t *)page_addr, FLASH_PAGE_SIZE); + memcpy(buffer, page_addr, FLASH_PAGE_SIZE); memcpy(buffer + offset, bytes, len); - nrf_nvm_safe_flash_page_write(page_addr, buffer); + nrf_nvm_safe_flash_page_write((uint32_t) page_addr, buffer); } } bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self, uint32_t start_index, uint8_t* values, uint32_t len) { - uint32_t address = NVM_START_ADDR + start_index; + uint32_t address = ((uint32_t) self->start_address) + start_index; uint32_t offset = address % FLASH_PAGE_SIZE; uint32_t page_addr = address - offset; while (len) { uint32_t write_len = MIN(len, FLASH_PAGE_SIZE - offset); - write_page(page_addr, offset, write_len, values); + write_page((uint8_t*) page_addr, offset, write_len, values); len -= write_len; values += write_len; page_addr += FLASH_PAGE_SIZE; @@ -75,6 +72,6 @@ bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self, } void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self, - uint32_t start_index, uint32_t len, uint8_t* values) { - memcpy(values, (uint8_t *)(NVM_START_ADDR + start_index), len); + uint32_t start_index, uint32_t len, uint8_t* values) { + memcpy(values, self->start_address + start_index, len); } diff --git a/ports/nrf/common-hal/nvm/ByteArray.h b/ports/nrf/common-hal/nvm/ByteArray.h index e47a87b9e..c048d5577 100644 --- a/ports/nrf/common-hal/nvm/ByteArray.h +++ b/ports/nrf/common-hal/nvm/ByteArray.h @@ -31,7 +31,7 @@ typedef struct { mp_obj_base_t base; - uint32_t start_address; + uint8_t* start_address; uint32_t len; } nvm_bytearray_obj_t; -- cgit v1.2.3