Skip to content

Commit cd69db7

Browse files
committed
Add a peripherals/nrf/nvm.c to wrap flash page writes safely
1 parent 8e7fee2 commit cd69db7

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
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/peripherals/nrf/nvm.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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/obj.h"
29+
#include "py/runtime.h"
30+
31+
#include <stdio.h>
32+
#include <string.h>
33+
34+
#include "nrf_nvmc.h"
35+
36+
#ifdef BLUETOOTH_SD
37+
#include "ble_drv.h"
38+
#include "nrf_sdm.h"
39+
40+
#define FLASH_PAGE_SIZE (4096)
41+
42+
STATIC void sd_flash_operation_start(void) {
43+
sd_flash_operation_status = SD_FLASH_OPERATION_IN_PROGRESS;
44+
}
45+
46+
STATIC sd_flash_operation_status_t sd_flash_operation_wait_until_done(void) {
47+
while (sd_flash_operation_status == SD_FLASH_OPERATION_IN_PROGRESS) {
48+
sd_app_evt_wait();
49+
}
50+
return sd_flash_operation_status;
51+
}
52+
#endif
53+
54+
void nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data) {
55+
#ifdef BLUETOOTH_SD
56+
uint8_t sd_en = 0;
57+
(void) sd_softdevice_is_enabled(&sd_en);
58+
if (sd_en) {
59+
uint32_t err_code;
60+
sd_flash_operation_status_t status;
61+
62+
sd_flash_operation_start();
63+
err_code = sd_flash_page_erase(page_addr / FLASH_PAGE_SIZE);
64+
if (err_code != NRF_SUCCESS) {
65+
mp_raise_OSError_msg_varg(translate("Flash erase failed to start, err 0x%04x"), err_code);
66+
}
67+
status = sd_flash_operation_wait_until_done();
68+
if (status == SD_FLASH_OPERATION_ERROR) {
69+
mp_raise_OSError_msg(translate("Flash erase failed"));
70+
}
71+
72+
// Divide a full page into parts, because writing a full page causes an assertion failure.
73+
// See https://devzone.nordicsemi.com/f/nordic-q-a/40088/sd_flash_write-cause-nrf_fault_id_sd_assert/
74+
const size_t BLOCK_PARTS = 2;
75+
size_t words_to_write = FLASH_PAGE_SIZE / sizeof(uint32_t) / BLOCK_PARTS;
76+
for (size_t i = 0; i < BLOCK_PARTS; i++) {
77+
sd_flash_operation_start();
78+
err_code = sd_flash_write(((uint32_t *)page_addr) + i * words_to_write,
79+
(uint32_t *)data + i * words_to_write,
80+
words_to_write);
81+
if (err_code != NRF_SUCCESS) {
82+
mp_raise_OSError_msg_varg(translate("Flash write failed to start, err 0x%04x"), err_code);
83+
}
84+
status = sd_flash_operation_wait_until_done();
85+
if (status == SD_FLASH_OPERATION_ERROR) {
86+
mp_raise_OSError_msg(translate("Flash write failed"));
87+
}
88+
}
89+
90+
return;
91+
}
92+
#endif
93+
94+
nrf_nvmc_page_erase(page_addr);
95+
nrf_nvmc_write_bytes(page_addr, data, FLASH_PAGE_SIZE);
96+
}

ports/nrf/peripherals/nrf/nvm.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
void nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data);

0 commit comments

Comments
 (0)