@@ -95,12 +95,20 @@ STATIC mp_obj_t rp2_flash_writeblocks(size_t n_args, const mp_obj_t *args) {
95
95
mp_buffer_info_t bufinfo ;
96
96
mp_get_buffer_raise (args [2 ], & bufinfo , MP_BUFFER_READ );
97
97
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 ();
98
100
flash_range_erase (self -> flash_base + offset , bufinfo .len );
101
+ MICROPY_END_ATOMIC_SECTION (atomic_state );
102
+ MICROPY_EVENT_POLL_HOOK
99
103
// TODO check return value
100
104
} else {
101
105
offset += mp_obj_get_int (args [3 ]);
102
106
}
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 ();
103
109
flash_range_program (self -> flash_base + offset , bufinfo .buf , bufinfo .len );
110
+ MICROPY_END_ATOMIC_SECTION (atomic_state );
111
+ MICROPY_EVENT_POLL_HOOK
104
112
// TODO check return value
105
113
return mp_const_none ;
106
114
}
@@ -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_
122
130
return MP_OBJ_NEW_SMALL_INT (BLOCK_SIZE_BYTES );
123
131
case MP_BLOCKDEV_IOCTL_BLOCK_ERASE : {
124
132
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 ();
125
135
flash_range_erase (self -> flash_base + offset , BLOCK_SIZE_BYTES );
136
+ MICROPY_END_ATOMIC_SECTION (atomic_state );
126
137
// TODO check return value
127
138
return MP_OBJ_NEW_SMALL_INT (0 );
128
139
}
0 commit comments