Skip to content

Commit 7889b99

Browse files
committed
Fix flash write error handling; clean up safe mode message printing
1 parent 887f64e commit 7889b99

File tree

7 files changed

+78
-43
lines changed

7 files changed

+78
-43
lines changed

main.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,7 @@ bool run_code_py(safe_mode_t safe_mode) {
264264
rgb_status_animation_t animation;
265265
prep_rgb_status_animation(&result, found_main, safe_mode, &animation);
266266
while (true) {
267-
#ifdef MICROPY_VM_HOOK_LOOP
268-
MICROPY_VM_HOOK_LOOP
269-
#endif
267+
RUN_BACKGROUND_TASKS;
270268
if (reload_requested) {
271269
reload_requested = false;
272270
return true;

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/runtime.h"
2728
#include "common-hal/nvm/ByteArray.h"
2829

2930
#include <stdio.h>
@@ -43,16 +44,20 @@ uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
4344
}
4445

4546
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+
// Write a whole page to flash, buffering it first and then erasing and rewriting
4748
// it since we can only clear a whole page at a time.
4849

50+
bool status;
4951
if (offset == 0 && len == FLASH_PAGE_SIZE) {
50-
nrf_nvm_safe_flash_page_write(page_addr, bytes);
52+
status = nrf_nvm_safe_flash_page_write(page_addr, bytes);
5153
} else {
5254
uint8_t buffer[FLASH_PAGE_SIZE];
5355
memcpy(buffer, (uint8_t *)page_addr, FLASH_PAGE_SIZE);
5456
memcpy(buffer + offset, bytes, len);
55-
nrf_nvm_safe_flash_page_write(page_addr, buffer);
57+
status = nrf_nvm_safe_flash_page_write(page_addr, buffer);
58+
}
59+
if (!status) {
60+
mp_raise_OSError_msg(translate("Flash write failed"));
5661
}
5762
}
5863

ports/nrf/peripherals/nrf/nvm.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ STATIC sd_flash_operation_status_t sd_flash_operation_wait_until_done(void) {
5050
}
5151
#endif
5252

53-
void nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data) {
53+
bool nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data) {
5454
#ifdef BLUETOOTH_SD
5555
uint8_t sd_en = 0;
5656
(void) sd_softdevice_is_enabled(&sd_en);
@@ -61,11 +61,11 @@ void nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data) {
6161
sd_flash_operation_start();
6262
err_code = sd_flash_page_erase(page_addr / FLASH_PAGE_SIZE);
6363
if (err_code != NRF_SUCCESS) {
64-
mp_raise_OSError_msg_varg(translate("Flash erase failed to start, err 0x%04x"), err_code);
64+
return false;
6565
}
6666
status = sd_flash_operation_wait_until_done();
6767
if (status == SD_FLASH_OPERATION_ERROR) {
68-
mp_raise_OSError_msg(translate("Flash erase failed"));
68+
return false;
6969
}
7070

7171
// Divide a full page into parts, because writing a full page causes an assertion failure.
@@ -78,18 +78,19 @@ void nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data) {
7878
(uint32_t *)data + i * words_to_write,
7979
words_to_write);
8080
if (err_code != NRF_SUCCESS) {
81-
mp_raise_OSError_msg_varg(translate("Flash write failed to start, err 0x%04x"), err_code);
81+
return false;
8282
}
8383
status = sd_flash_operation_wait_until_done();
8484
if (status == SD_FLASH_OPERATION_ERROR) {
85-
mp_raise_OSError_msg(translate("Flash write failed"));
85+
return false;
8686
}
8787
}
8888

89-
return;
89+
return true;
9090
}
9191
#endif
9292

9393
nrf_nvmc_page_erase(page_addr);
9494
nrf_nvmc_write_bytes(page_addr, data, FLASH_PAGE_SIZE);
95-
}
95+
return true;
96+
}

ports/nrf/peripherals/nrf/nvm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
#define CIRCUITPY_INTERNAL_NVM_SIZE (0)
3232
#endif
3333

34-
void nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data);
34+
bool nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data);

ports/nrf/supervisor/internal_flash.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "py/obj.h"
3535
#include "py/runtime.h"
3636
#include "lib/oofatfs/ff.h"
37+
#include "supervisor/shared/safe_mode.h"
3738

3839
#include "peripherals/nrf/nvm.h"
3940

@@ -75,7 +76,9 @@ void supervisor_flash_flush(void) {
7576

7677
// Skip if data is the same
7778
if (memcmp(_flash_cache, (void *)_flash_page_addr, FLASH_PAGE_SIZE) != 0) {
78-
nrf_nvm_safe_flash_page_write(_flash_page_addr, _flash_cache);
79+
if (!nrf_nvm_safe_flash_page_write(_flash_page_addr, _flash_cache)) {
80+
reset_into_safe_mode(FLASH_WRITE_FAIL);
81+
}
7982
}
8083
}
8184

@@ -120,4 +123,3 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32
120123

121124
void supervisor_flash_release_cache(void) {
122125
}
123-

supervisor/shared/safe_mode.c

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -94,44 +94,72 @@ void __attribute__((noinline,)) reset_into_safe_mode(safe_mode_t reason) {
9494
reset_cpu();
9595
}
9696

97+
98+
99+
#define FILE_AN_ISSUE translate("\r\nPlease file an issue with the contents of your CIRCUITPY drive at \nhttps://github.com/adafruit/circuitpython/issues\r\n")
100+
97101
void print_safe_mode_message(safe_mode_t reason) {
98102
if (reason == NO_SAFE_MODE) {
99103
return;
100104
}
101105
serial_write("\r\n");
102-
// Output a user safe mode string if its set.
106+
// Output a user safe mode string if it's set.
103107
#ifdef BOARD_USER_SAFE_MODE
104108
if (reason == USER_SAFE_MODE) {
105109
serial_write_compressed(translate("You requested starting safe mode by "));
106110
serial_write(BOARD_USER_SAFE_MODE_ACTION);
107-
serial_write("\r\n");
108-
serial_write_compressed(translate("To exit, please reset the board without "));
111+
serial_write_compressed(translate("\r\nTo exit, please reset the board without "));
109112
serial_write(BOARD_USER_SAFE_MODE_ACTION);
110113
serial_write("\r\n");
111114
} else
112115
#endif
113-
if (reason == MANUAL_SAFE_MODE) {
114-
serial_write_compressed(translate("The reset button was pressed while booting CircuitPython. Press again to exit safe mode.\n"));
115-
} else if (reason == PROGRAMMATIC_SAFE_MODE) {
116-
serial_write_compressed(translate("The `microcontroller` module was used to boot into safe mode. Press reset to exit safe mode.\n"));
117-
} else {
118-
serial_write_compressed(translate("You are running in safe mode which means something unanticipated happened.\n"));
119-
if (reason == HARD_CRASH || reason == MICROPY_NLR_JUMP_FAIL || reason == MICROPY_FATAL_ERROR || reason == GC_ALLOC_OUTSIDE_VM) {
120-
serial_write_compressed(translate("Looks like our core CircuitPython code crashed hard. Whoops!\nPlease file an issue at https://github.com/adafruit/circuitpython/issues\n with the contents of your CIRCUITPY drive and this message:\n"));
121-
if (reason == HARD_CRASH) {
122-
serial_write_compressed(translate("Crash into the HardFault_Handler.\n"));
123-
} else if (reason == MICROPY_NLR_JUMP_FAIL) {
124-
serial_write_compressed(translate("MicroPython NLR jump failed. Likely memory corruption.\n"));
125-
} else if (reason == MICROPY_FATAL_ERROR) {
126-
serial_write_compressed(translate("MicroPython fatal error.\n"));
127-
} else if (reason == GC_ALLOC_OUTSIDE_VM) {
128-
serial_write_compressed(translate("Attempted heap allocation when MicroPython VM not running.\n"));
129-
}
130-
} else if (reason == BROWNOUT) {
131-
serial_write_compressed(translate("The microcontroller's power dipped. Please make sure your power supply provides\nenough power for the whole circuit and press reset (after ejecting CIRCUITPY).\n"));
132-
} else if (reason == HEAP_OVERWRITTEN) {
133-
serial_write_compressed(translate("The CircuitPython heap was corrupted because the stack was too small.\nPlease increase stack size limits and press reset (after ejecting CIRCUITPY).\nIf you didn't change the stack, then file an issue here with the contents of your CIRCUITPY drive:\n"));
134-
serial_write("https://github.com/adafruit/circuitpython/issues\r\n");
116+
switch (reason) {
117+
case MANUAL_SAFE_MODE:
118+
serial_write_compressed(translate("CircuitPython is in safe mode because you pressed the reset button during boot. Press again to exit safe mode.\r\n"));
119+
return;
120+
case PROGRAMMATIC_SAFE_MODE:
121+
serial_write_compressed(translate("The `microcontroller` module was used to boot into safe mode. Press reset to exit safe mode.\r\n"));
122+
return;
123+
default:
124+
break;
135125
}
136-
}
126+
127+
serial_write_compressed(translate("You are in safe mode: something unanticipated happened.\r\n"));
128+
switch (reason) {
129+
case BROWNOUT:
130+
serial_write_compressed(translate("The microcontroller's power dipped. Make sure your power supply provides\r\nenough power for the whole circuit and press reset (after ejecting CIRCUITPY).\r\n"));
131+
return;
132+
case HEAP_OVERWRITTEN:
133+
serial_write_compressed(translate("The CircuitPython heap was corrupted because the stack was too small.\r\nPlease increase the stack size if you know how, or if not:"));
134+
serial_write_compressed(FILE_AN_ISSUE);
135+
return;
136+
default:
137+
break;
138+
}
139+
140+
serial_write_compressed(translate("CircuitPython core code crashed hard. Whoops!\r\n"));
141+
switch (reason) {
142+
case HARD_CRASH:
143+
serial_write_compressed(translate("Crash into the HardFault_Handler."));
144+
return;
145+
case MICROPY_NLR_JUMP_FAIL:
146+
serial_write_compressed(translate("MicroPython NLR jump failed. Likely memory corruption."));
147+
return;
148+
case MICROPY_FATAL_ERROR:
149+
serial_write_compressed(translate("MicroPython fatal error."));
150+
break;
151+
case GC_ALLOC_OUTSIDE_VM:
152+
serial_write_compressed(translate("Attempted heap allocation when MicroPython VM not running."));
153+
break;
154+
case NORDIC_SOFT_DEVICE_ASSERT:
155+
serial_write_compressed(translate("Nordic Soft Device failure assertion."));
156+
break;
157+
case FLASH_WRITE_FAIL:
158+
serial_write_compressed(translate("Failed to write internal flash."));
159+
break;
160+
default:
161+
serial_write_compressed(translate("Unknown reason."));
162+
break;
163+
}
164+
serial_write_compressed(FILE_AN_ISSUE);
137165
}

supervisor/shared/safe_mode.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ typedef enum {
3838
MICROPY_FATAL_ERROR,
3939
GC_ALLOC_OUTSIDE_VM,
4040
PROGRAMMATIC_SAFE_MODE,
41-
NORDIC_SOFT_DEVICE_ASSERT
41+
NORDIC_SOFT_DEVICE_ASSERT,
42+
FLASH_WRITE_FAIL,
4243
} safe_mode_t;
4344

4445
safe_mode_t wait_for_safe_mode_reset(void);

0 commit comments

Comments
 (0)