8000 rp2/rp2_flash.c: Disable IRQs before calling flash_erase/program. · micropython/micropython@d27403c · GitHub
[go: up one dir, main page]

Skip to content

Commit d27403c

Browse files
committed
rp2/rp2_flash.c: Disable IRQs before calling flash_erase/program.
* Flash erase/program functions disable the XIP bit.
1 parent 44818d1 commit d27403c

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

ports/rp2/rp2_flash.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,20 @@ STATIC mp_obj_t rp2_flash_writeblocks(size_t n_args, const mp_obj_t *args) {
9595
mp_buffer_info_t bufinfo;
9696
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
9797
if (n_args == 3) {
98+
// Flash erase/program must run in an atomic section because the XIP bit gets disabled.
99+
mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
98100
flash_range_erase(self->flash_base + offset, bufinfo.len);
101+
MICROPY_END_ATOMIC_SECTION(atomic_state);
102+
MICROPY_EVENT_POLL_HOOK
99103
// TODO check return value
100104
} else {
101105
offset += mp_obj_get_int(args[3]);
102106
}
107+
// Flash erase/program must run in an atomic section because the XIP bit gets disabled.
108+
mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
103109
flash_range_program(self->flash_base + offset, bufinfo.buf, bufinfo.len);
110+
MICROPY_END_ATOMIC_SECTION(atomic_state);
111+
MICROPY_EVENT_POLL_HOOK
104112
// TODO check return value
105113
return mp_const_none;
106114
}
@@ -122,7 +130,10 @@ STATIC mp_obj_t rp2_flash_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_
122130
return MP_OBJ_NEW_SMALL_INT(BLOCK_SIZE_BYTES);
123131
case MP_BLOCKDEV_IOCTL_BLOCK_ERASE: {
124132
uint32_t offset = mp_obj_get_int(arg_in) * BLOCK_SIZE_BYTES;
133+
// Flash erase/program must run in an atomic section because the XIP bit gets disabled.
134+
mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
125135
flash_range_erase(self->flash_base + offset, BLOCK_SIZE_BYTES);
136+
MICROPY_END_ATOMIC_SECTION(atomic_state);
126137
// TODO check return value
127138
return MP_OBJ_NEW_SMALL_INT(0);
128139
}

0 commit comments

Comments
 (0)
0