Commit 90202b4c authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

esp8266/modesp: Implement flash_read(offset, size_bytes) function.

Based on vendor API documentation, untested on real hardware.
parent ff8c4188
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <errno.h>
#include "py/nlr.h" #include "py/nlr.h"
#include "py/obj.h" #include "py/obj.h"
...@@ -569,6 +570,20 @@ STATIC mp_obj_t esp_flash_id() { ...@@ -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_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[] = { STATIC const mp_map_elem_t esp_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_esp) }, { 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[] = { ...@@ -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_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_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_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 }, { MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&esp_socket_type },
#if MODESP_INCLUDE_CONSTANTS #if MODESP_INCLUDE_CONSTANTS
......
...@@ -64,6 +64,7 @@ Q(adc) ...@@ -64,6 +64,7 @@ Q(adc)
Q(vdd33) Q(vdd33)
Q(chip_id) Q(chip_id)
Q(flash_id) Q(flash_id)
Q(flash_read)
Q(sdk_version) Q(sdk_version)
Q(mac) Q(mac)
Q(getaddrinfo) Q(getaddrinfo)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment