@@ -264,29 +264,34 @@ static mp_obj_t rp2_flash_writeblocks(size_t n_args, const mp_obj_t *args) {
264264 offset += mp_obj_get_int (args [3 ]);
265265 }
266266
267+ #if defined(MICROPY_HW_PSRAM_CS_PIN ) && MICROPY_HW_ENABLE_PSRAM
267268 if ((uintptr_t )bufinfo .buf >= SRAM_BASE ) {
269+ #endif
270+ mp_uint_t atomic_state = begin_critical_flash_section ();
271+ flash_range_program (self -> flash_base + offset , bufinfo .buf , bufinfo .len );
272+ end_critical_flash_section (atomic_state );
273+ mp_event_handle_nowait ();
274+ #if defined(MICROPY_HW_PSRAM_CS_PIN ) && MICROPY_HW_ENABLE_PSRAM
275+ } else {
276+ size_t bytes_left = bufinfo .len ;
277+ size_t bytes_offset = 0 ;
278+ static uint8_t copy_buffer [BLOCK_SIZE_BYTES ] = {0 }
279+ ;
280+
281+ while (bytes_left ) {
282+ memcpy (copy_buffer , bufinfo .buf + bytes_offset , min_size (bytes_left , BLOCK_SIZE_BYTES ));
268283 mp_uint_t atomic_state = begin_critical_flash_section ();
269- flash_range_program (self -> flash_base + offset , bufinfo . buf , bufinfo . len );
284+ flash_range_program (self -> flash_base + offset + bytes_offset , copy_buffer , min_size ( bytes_left , BLOCK_SIZE_BYTES ) );
270285 end_critical_flash_section (atomic_state );
271- mp_event_handle_nowait ();
272- } else {
273- size_t bytes_left = bufinfo .len ;
274- size_t bytes_offset = 0 ;
275- static uint8_t copy_buffer [BLOCK_SIZE_BYTES ] = {0 };
276-
277- while (bytes_left ) {
278- memcpy (copy_buffer , bufinfo .buf + bytes_offset , min_size (bytes_left , BLOCK_SIZE_BYTES ));
279- mp_uint_t atomic_state = begin_critical_flash_section ();
280- flash_range_program (self -> flash_base + offset + bytes_offset , copy_buffer , min_size (bytes_left , BLOCK_SIZE_BYTES ));
281- end_critical_flash_section (atomic_state );
282- bytes_offset += BLOCK_SIZE_BYTES ;
283- if (bytes_left <= BLOCK_SIZE_BYTES ) {
284- break ;
285- }
286- bytes_left -= BLOCK_SIZE_BYTES ;
287- mp_event_handle_nowait ();
286+ bytes_offset += BLOCK_SIZE_BYTES ;
287+ if (bytes_left <= BLOCK_SIZE_BYTES ) {
288+ break ;
288289 }
290+ bytes_left -= BLOCK_SIZE_BYTES ;
291+ mp_event_handle_nowait ();
289292 }
293+ }
294+ #endif
290295
291296 // TODO check return value
292297 return mp_const_none ;
0 commit comments