@@ -535,16 +535,34 @@ STATIC mp_obj_t esp_flash_id() {
535535}
536536STATIC MP_DEFINE_CONST_FUN_OBJ_0 (esp_flash_id_obj , esp_flash_id );
537537
538- STATIC mp_obj_t esp_flash_read (mp_obj_t offset_in , mp_obj_t len_in ) {
538+ STATIC mp_obj_t esp_flash_read (mp_obj_t offset_in , mp_obj_t len_or_buf_in ) {
539539 mp_int_t offset = mp_obj_get_int (offset_in );
540- mp_int_t len = mp_obj_get_int (len_in );
541- byte * buf = m_new (byte , len );
540+
541+ mp_int_t len ;
542+ byte * buf ;
543+ bool alloc_buf = MP_OBJ_IS_INT (len_or_buf_in );
544+
545+ if (alloc_buf ) {
546+ len = mp_obj_get_int (len_or_buf_in );
547+ buf = m_new (byte , len );
548+ } else {
549+ mp_buffer_info_t bufinfo ;
550+ mp_get_buffer_raise (len_or_buf_in , & bufinfo , MP_BUFFER_WRITE );
551+ len = bufinfo .len ;
552+ buf = bufinfo .buf ;
553+ }
554+
542555 // We know that allocation will be 4-byte aligned for sure
543556 SpiFlashOpResult res = spi_flash_read (offset , (uint32_t * )buf , len );
544557 if (res == SPI_FLASH_RESULT_OK ) {
545- return mp_obj_new_bytes (buf , len );
558+ if (alloc_buf ) {
559+ return mp_obj_new_bytes (buf , len );
560+ }
561+ return mp_const_none ;
562+ }
563+ if (alloc_buf ) {
564+ m_del (byte , buf , len );
546565 }
547- m_del (byte , buf , len );
548566 nlr_raise (mp_obj_new_exception_arg1 (& mp_type_OSError , MP_OBJ_NEW_SMALL_INT (res == SPI_FLASH_RESULT_TIMEOUT ? ETIMEDOUT : EIO )));
549567}
550568STATIC MP_DEFINE_CONST_FUN_OBJ_2 (esp_flash_read_obj , esp_flash_read );
0 commit comments