Skip to content

Commit c0ce820

Browse files
authored
Merge pull request adafruit#1803 from nickzoic/circuitpython-nickzoic-1042-nrf-nvm-bytearray-3
Circuitpython nickzoic 1042 nrf nvm bytearray 3
2 parents 7822d01 + 9c42a72 commit c0ce820

File tree

13 files changed

+309
-79
lines changed

13 files changed

+309
-79
lines changed

ports/nrf/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ SRC_C += \
177177
peripherals/nrf/clocks.c \
178178
peripherals/nrf/$(MCU_CHIP)/pins.c \
179179
peripherals/nrf/$(MCU_CHIP)/power.c \
180+
peripherals/nrf/nvm.c \
180181
peripherals/nrf/timers.c \
181182
sd_mutex.c \
182183
supervisor/shared/memory.c

ports/nrf/boards/adafruit_nrf52840_s140_v6.ld

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
0x000ED000..0x000F3FFF (28KB ) Private Config Data (Bonding, Keys, etc.)
1313
0x000AD000..0x000ECFFF (256KB) User Filesystem
14-
1514
0x00026000..0x000ACFFF (540KB) Application Code (including ISR vector)
1615
0x00001000..0x00025FFF (148KB) SoftDevice
1716
0x00000000..0x00000FFF (4KB) Master Boot Record

ports/nrf/boards/feather_nrf52840_express/mpconfigboard.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#define MICROPY_HW_MCU_NAME "nRF52840"
3434
#define MICROPY_PY_SYS_PLATFORM "Feather52840Express"
3535

36+
#define FLASH_SIZE (0x100000)
37+
#define FLASH_PAGE_SIZE (4096)
38+
3639
#define MICROPY_HW_NEOPIXEL (&pin_P0_16)
3740

3841
#define MICROPY_HW_LED_STATUS (&pin_P1_15)
@@ -55,7 +58,7 @@
5558

5659
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
5760

58-
// TODO #define CIRCUITPY_INTERNAL_NVM_SIZE 8192
61+
#define CIRCUITPY_INTERNAL_NVM_SIZE (4096)
5962

6063
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)
6164

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/mphal.h"
28+
#include "py/obj.h"
29+
#include "py/runtime.h"
30+
2731
#include "common-hal/microcontroller/Pin.h"
2832
#include "common-hal/microcontroller/Processor.h"
2933

34+
#include "shared-bindings/nvm/ByteArray.h"
3035
#include "shared-bindings/microcontroller/__init__.h"
3136
#include "shared-bindings/microcontroller/Pin.h"
3237
#include "shared-bindings/microcontroller/Processor.h"
@@ -63,6 +68,14 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
6368
},
6469
};
6570

71+
#if CIRCUITPY_INTERNAL_NVM_SIZE > 0
72+
// The singleton nvm.ByteArray object.
73+
const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
74+
.base = {
75+
.type = &nvm_bytearray_type,
76+
},
77+
};
78+
#endif
6679

6780
STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = {
6881
{ MP_ROM_QSTR(MP_QSTR_P0_00), MP_ROM_PTR(&pin_P0_00) },

ports/nrf/common-hal/nvm/ByteArray.c

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) 2019 Nick Moore for Adafruit Industries
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 <stdio.h>
30+
#include <string.h>
31+
32+
#include "peripherals/nrf/nvm.h"
33+
34+
// defined in linker
35+
extern uint32_t __fatfs_flash_start_addr[];
36+
extern uint32_t __fatfs_flash_length[];
37+
38+
#define NVM_START_ADDR ((uint32_t)__fatfs_flash_start_addr + \
39+
(uint32_t)__fatfs_flash_length - CIRCUITPY_INTERNAL_NVM_SIZE)
40+
41+
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
42+
return CIRCUITPY_INTERNAL_NVM_SIZE;
43+
}
44+
45+
static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_t *bytes) {
46+
// Write a whole page to flash, buffering it first and then erasing and rewriting
47+
// it since we can only clear a whole page at a time.
48+
49+
if (offset == 0 && len == FLASH_PAGE_SIZE) {
50+
nrf_nvm_safe_flash_page_write(page_addr, bytes);
51+
} else {
52+
uint8_t buffer[FLASH_PAGE_SIZE];
53+
memcpy(buffer, (uint8_t *)page_addr, FLASH_PAGE_SIZE);
54+
memcpy(buffer + offset, bytes, len);
55+
nrf_nvm_safe_flash_page_write(page_addr, buffer);
56+
}
57+
}
58+
59+
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
60+
uint32_t start_index, uint8_t* values, uint32_t len) {
61+
62+
uint32_t address = NVM_START_ADDR + start_index;
63+
uint32_t offset = address % FLASH_PAGE_SIZE;
64+
uint32_t page_addr = address - offset;
65+
66+
while (len) {
67+
uint32_t write_len = MIN(len, FLASH_PAGE_SIZE - offset);
68+
write_page(page_addr, offset, write_len, values);
69+
len -= write_len;
70+
values += write_len;
71+
page_addr += FLASH_PAGE_SIZE;
72+
offset = 0;
73+
}
74+
return true;
75+
}
76+
77+
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self,
78+
uint32_t start_index, uint32_t len, uint8_t* values) {
79+
memcpy(values, (uint8_t *)(NVM_START_ADDR + start_index), len);
80+
}

ports/nrf/common-hal/nvm/ByteArray.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Nick Moore for Adafruit Industries
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_NRF_COMMON_HAL_NVM_BYTEARRAY_H
28+
#define MICROPY_INCLUDED_NRF_COMMON_HAL_NVM_BYTEARRAY_H
29+
30+
#include "py/obj.h"
31+
32+
typedef struct {
33+
mp_obj_base_t base;
34+
} nvm_bytearray_obj_t;
35+
36+
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_NVM_BYTEARRAY_H

ports/nrf/common-hal/nvm/__init__.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Nick Moore for Adafruit Industries
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+
// No nvm module functions.

ports/nrf/mpconfigport.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ CIRCUITPY_AUDIOBUSIO = 0
1919
# No I2CSlave implementation
2020
CIRCUITPY_I2CSLAVE = 0
2121

22-
# nvm not yet implemented
23-
CIRCUITPY_NVM = 0
22+
# enable NVM
23+
CIRCUITPY_NVM = 1
2424

2525
# enable RTC
2626
CIRCUITPY_RTC = 1

ports/nrf/peripherals/nrf/nvm.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
7+
* Copyright (c) 2019 Nick Moore for Adafruit Industries
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "py/runtime.h"
29+
30+
#include <stdio.h>
31+
#include <string.h>
32+
33+
#include "nrf_nvmc.h"
34+
35+
#define FLASH_PAGE_SIZE (4096)
36+
37+
#ifdef BLUETOOTH_SD
38+
#include "ble_drv.h"
39+
#include "nrf_sdm.h"
40+
41+
STATIC void sd_flash_operation_start(void) {
42+
sd_flash_operation_status = SD_FLASH_OPERATION_IN_PROGRESS;
43+
}
44+
45+
STATIC sd_flash_operation_status_t sd_flash_operation_wait_until_done(void) {
46+
while (sd_flash_operation_status == SD_FLASH_OPERATION_IN_PROGRESS) {
47+
sd_app_evt_wait();
48+
}
49+
return sd_flash_operation_status;
50+
}
51+
#endif
52+
53+
void nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data) {
54+
#ifdef BLUETOOTH_SD
55+
uint8_t sd_en = 0;
56+
(void) sd_softdevice_is_enabled(&sd_en);
57+
if (sd_en) {
58+
uint32_t err_code;
59+
sd_flash_operation_status_t status;
60+
61+
sd_flash_operation_start();
62+
err_code = sd_flash_page_erase(page_addr / FLASH_PAGE_SIZE);
63+
if (err_code != NRF_SUCCESS) {
64+
mp_raise_OSError_msg_varg(translate("Flash erase failed to start, err 0x%04x"), err_code);
65+
}
66+
status = sd_flash_operation_wait_until_done();
67+
if (status == SD_FLASH_OPERATION_ERROR) {
68+
mp_raise_OSError_msg(translate("Flash erase failed"));
69+
}
70+
71+
// Divide a full page into parts, because writing a full page causes an assertion failure.
72+
// See https://devzone.nordicsemi.com/f/nordic-q-a/40088/sd_flash_write-cause-nrf_fault_id_sd_assert/
73+
const size_t BLOCK_PARTS = 2;
74+
size_t words_to_write = FLASH_PAGE_SIZE / sizeof(uint32_t) / BLOCK_PARTS;
75+
for (size_t i = 0; i < BLOCK_PARTS; i++) {
76+
sd_flash_operation_start();
77+
err_code = sd_flash_write(((uint32_t *)page_addr) + i * words_to_write,
78+
(uint32_t *)data + i * words_to_write,
79+
words_to_write);
80+
if (err_code != NRF_SUCCESS) {
81+
mp_raise_OSError_msg_varg(translate("Flash write failed to start, err 0x%04x"), err_code);
82+
}
83+
status = sd_flash_operation_wait_until_done();
84+
if (status == SD_FLASH_OPERATION_ERROR) {
85+
mp_raise_OSError_msg(translate("Flash write failed"));
86+
}
87+
}
88+
89+
return;
90+
}
91+
#endif
92+
93+
nrf_nvmc_page_erase(page_addr);
94+
nrf_nvmc_write_bytes(page_addr, data, FLASH_PAGE_SIZE);
95+
}

ports/nrf/peripherals/nrf/nvm.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
7+
* Copyright (c) 2019 Nick Moore for Adafruit Industries
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#define FLASH_PAGE_SIZE (4096)
29+
30+
#ifndef CIRCUITPY_INTERNAL_NVM_SIZE
31+
#define CIRCUITPY_INTERNAL_NVM_SIZE (0)
32+
#endif
33+
34+
void nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data);

0 commit comments

Comments
 (0)