diff --git a/esp8266/modesp.c b/esp8266/modesp.c index 4e46b7ae1d..6b4f16310f 100644 --- a/esp8266/modesp.c +++ b/esp8266/modesp.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "py/nlr.h" #include "py/obj.h" @@ -569,6 +570,20 @@ STATIC mp_obj_t esp_flash_id() { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_id_obj, esp_flash_id); +STATIC mp_obj_t esp_flash_read(mp_obj_t offset_in, mp_obj_t len_in) { + mp_int_t offset = mp_obj_get_int(offset_in); + mp_int_t len = mp_obj_get_int(len_in); + byte *buf = m_new(byte, len); + // We know that allocation will be 4-byte aligned for sure + SpiFlashOpResult res = spi_flash_read(offset, (uint32_t*)buf, len); + if (res == SPI_FLASH_RESULT_OK) { + return mp_obj_new_bytes(buf, len); + } + m_del(byte, buf, len); + nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(res == SPI_FLASH_RESULT_TIMEOUT ? ETIMEDOUT : EIO))); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_flash_read_obj, esp_flash_read); + STATIC const mp_map_elem_t esp_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_esp) }, @@ -579,6 +594,7 @@ STATIC const mp_map_elem_t esp_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_sleep_type), (mp_obj_t)&esp_sleep_type_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_deepsleep), (mp_obj_t)&esp_deepsleep_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_flash_id), (mp_obj_t)&esp_flash_id_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_flash_read), (mp_obj_t)&esp_flash_read_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&esp_socket_type }, #if MODESP_INCLUDE_CONSTANTS diff --git a/esp8266/qstrdefsport.h b/esp8266/qstrdefsport.h index c894cf84ad..03f14f6b81 100644 --- a/esp8266/qstrdefsport.h +++ b/esp8266/qstrdefsport.h @@ -64,6 +64,7 @@ Q(adc) Q(vdd33) Q(chip_id) Q(flash_id) +Q(flash_read) Q(sdk_version) Q(mac) Q(getaddrinfo)