Skip to content

Circuitpython nickzoic 1042 nrf nvm bytearray 3 #1803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ports/nrf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion ports/nrf/boards/adafruit_nrf52840_s140_v6.ld
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion ports/nrf/boards/feather_nrf52840_express/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#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)

#define MICROPY_HW_LED_STATUS (&pin_P1_15)
Expand All @@ -55,7 +58,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)

Expand Down
13 changes: 13 additions & 0 deletions ports/nrf/common-hal/microcontroller/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -63,6 +68,14 @@ 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,
},
};
#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) },
Expand Down
80 changes: 80 additions & 0 deletions ports/nrf/common-hal/nvm/ByteArray.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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 <stdio.h>
#include <string.h>

#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 CIRCUITPY_INTERNAL_NVM_SIZE;
}

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.

if (offset == 0 && len == 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_nvm_safe_flash_page_write(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 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;
}

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);
}
36 changes: 36 additions & 0 deletions ports/nrf/common-hal/nvm/ByteArray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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;
} nvm_bytearray_obj_t;

#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_NVM_BYTEARRAY_H
27 changes: 27 additions & 0 deletions ports/nrf/common-hal/nvm/__init__.c
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions ports/nrf/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
95 changes: 95 additions & 0 deletions ports/nrf/peripherals/nrf/nvm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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/runtime.h"

#include <stdio.h>
#include <string.h>

#include "nrf_nvmc.h"

#define FLASH_PAGE_SIZE (4096)

#ifdef BLUETOOTH_SD
#include "ble_drv.h"
#include "nrf_sdm.h"

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);
}
34 changes: 34 additions & 0 deletions ports/nrf/peripherals/nrf/nvm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.
*/

#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);
Loading