From a93e62c44431b223a27fa8ad237b9c168d995b25 Mon Sep 17 00:00:00 2001 From: HuangRui Date: Wed, 31 Dec 2014 08:08:31 +0800 Subject: [PATCH] Add node.flashid() and node.flashsize(). --- app/modules/node.c | 29 +++++++ app/platform/flash_api.c | 167 ++++++++++++++++++++++++++++++++++++++- app/platform/flash_api.h | 14 +++- 3 files changed, 205 insertions(+), 5 deletions(-) diff --git a/app/modules/node.c b/app/modules/node.c index 94b2f7a1..3c7a41b2 100644 --- a/app/modules/node.c +++ b/app/modules/node.c @@ -11,6 +11,8 @@ #include "romfs.h" #include "c_string.h" #include "driver/uart.h" +#include "spi_flash.h" +#include "flash_api.h" // Lua: restart() static int ICACHE_FLASH_ATTR node_restart( lua_State* L ) @@ -38,6 +40,31 @@ static int ICACHE_FLASH_ATTR node_chipid( lua_State* L ) return 1; } +// Lua: flashid() +static int ICACHE_FLASH_ATTR node_flashid( lua_State* L ) +{ + uint32_t id = spi_flash_get_id(); + lua_pushinteger( L, id ); + return 1; +} + +// Lua: flashsize() +static int ICACHE_FLASH_ATTR node_flashsize( lua_State* L ) +{ + //uint32_t sz = 0; + //if(lua_type(L, 1) == LUA_TNUMBER) + //{ + // sz = luaL_checkinteger(L, 1); + // if(sz > 0) + // { + // flash_set_size_byte(sz); + // } + //} + uint32_t sz = flash_get_size_byte(); + lua_pushinteger( L, sz ); + return 1; +} + // Lua: heap() static int ICACHE_FLASH_ATTR node_heap( lua_State* L ) { @@ -238,6 +265,8 @@ const LUA_REG_TYPE node_map[] = { LSTRKEY( "restart" ), LFUNCVAL( node_restart ) }, { LSTRKEY( "dsleep" ), LFUNCVAL( node_deepsleep ) }, { LSTRKEY( "chipid" ), LFUNCVAL( node_chipid ) }, + { LSTRKEY( "flashid" ), LFUNCVAL( node_flashid ) }, + { LSTRKEY( "flashsize" ), LFUNCVAL( node_flashsize) }, { LSTRKEY( "heap" ), LFUNCVAL( node_heap ) }, { LSTRKEY( "key" ), LFUNCVAL( node_key ) }, { LSTRKEY( "led" ), LFUNCVAL( node_led ) }, diff --git a/app/platform/flash_api.c b/app/platform/flash_api.c index a99a77df..1620c7c5 100644 --- a/app/platform/flash_api.c +++ b/app/platform/flash_api.c @@ -1,12 +1,16 @@ /****************************************************************************** * Flash api for NodeMCU + * NodeMCU Team + * 2014-12-31 *******************************************************************************/ #include "user_config.h" #include "flash_api.h" #include "spi_flash.h" +#include "c_stdio.h" + SPIFlashInfo *ICACHE_FLASH_ATTR -flash_get_info() +flash_get_info(void) { static SPIFlashInfo spi_flash_info; static bool is_spi_flash_info_initialized = false; @@ -19,8 +23,15 @@ flash_get_info() return &spi_flash_info; } +uint8_t ICACHE_FLASH_ATTR +flash_get_size(void) +{ + SPIFlashInfo *p_spi_flash_info = flash_get_info(); + return p_spi_flash_info->size; +} + uint32_t ICACHE_FLASH_ATTR -flash_get_size_byte() +flash_get_size_byte(void) { static uint32_t flash_size = 0; // Make the code more fast @@ -58,8 +69,69 @@ flash_get_size_byte() return flash_size; } +bool ICACHE_FLASH_ATTR +flash_set_size(uint8_t size) +{ + // Dangerous, here are dinosaur infested!!!!! + // Reboot required!!! + // If you don't know what you're doing, your nodemcu may turn into stone ... + c_printf("\nSet size!!! %d\n", size); + uint8_t data[SPI_FLASH_SEC_SIZE]; + SPIRead(0, data, sizeof(data)); + SPIFlashInfo *p_spi_flash_info = (SPIFlashInfo *)(data); + p_spi_flash_info->size = size; + SPIEraseSector(0); + SPIWrite(data, 0, sizeof(data)); + //p_spi_flash_info = flash_get_info(); + //p_spi_flash_info->size = size; + return true; +} + +bool ICACHE_FLASH_ATTR +flash_set_size_byte(uint32_t size) +{ + // Dangerous, here are dinosaur infested!!!!! + // Reboot required!!! + // If you don't know what you're doing, your nodemcu may turn into stone ... + bool result = true; + uint32_t flash_size = 0; + switch (size) + { + case 256 * 1024: + // 2Mbit, 256kByte + flash_size = SIZE_2MBIT; + flash_set_size(flash_size); + break; + case 512 * 1024: + // 4Mbit, 512kByte + flash_size = SIZE_4MBIT; + flash_set_size(flash_size); + break; + case 1 * 1024 * 1024: + // 8Mbit, 1MByte + flash_size = SIZE_8MBIT; + flash_set_size(flash_size); + break; + case 2 * 1024 * 1024: + // 16Mbit, 2MByte + flash_size = SIZE_16MBIT; + flash_set_size(flash_size); + break; + case 4 * 1024 * 1024: + // 32Mbit, 4MByte + flash_size = SIZE_32MBIT; + flash_set_size(flash_size); + break; + default: + // Unknown flash size. + result = false; + break; + } + return result; +} + uint16_t ICACHE_FLASH_ATTR -flash_get_sec_num() +flash_get_sec_num(void) { static uint16_t result = 0; // Make the code more fast @@ -69,3 +141,92 @@ flash_get_sec_num() } return result; } + +uint8_t ICACHE_FLASH_ATTR +flash_get_mode(void) +{ + SPIFlashInfo *p_spi_flash_info = flash_get_info(); + switch (p_spi_flash_info->mode) + { + // Reserved for future use + case MODE_QIO: + break; + case MODE_QOUT: + break; + case MODE_DIO: + break; + case MODE_DOUT: + break; + } + return p_spi_flash_info->mode; +} + +uint32_t ICACHE_FLASH_ATTR +flash_get_speed(void) +{ + uint32_t speed = 0; + SPIFlashInfo *p_spi_flash_info = flash_get_info(); + switch (p_spi_flash_info->speed) + { + case SPEED_40MHZ: + // 40MHz + speed = 40000000; + break; + case SPEED_26MHZ: + //26.7MHz + speed = 26700000; + break; + case SPEED_20MHZ: + // 20MHz + speed = 20000000; + break; + case SPEED_80MHZ: + //80MHz + speed = 80000000; + break; + } + return speed; +} + +bool ICACHE_FLASH_ATTR +flash_init_data_default(void) +{ + // FLASH SEC - 4 + // Dangerous, here are dinosaur infested!!!!! + // Reboot required!!! + // It will init system data to default! + uint8_t flash_init_data[128] = + { + 0x05, 0x00, 0x04, 0x02, 0x05, 0x05, 0x05, 0x02, 0x05, 0x00, 0x04, 0x05, 0x05, 0x04, 0x05, 0x05, + 0x04, 0xFE, 0xFD, 0xFF, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xE0, 0xE1, 0x0A, 0xFF, 0xFF, 0xF8, 0x00, + 0xF8, 0xF8, 0x52, 0x4E, 0x4A, 0x44, 0x40, 0x38, 0x00, 0x00, 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xE1, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x93, 0x43, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + SPIEraseSector((flash_get_sec_num() - 4)); + SPIWrite((flash_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, flash_init_data, sizeof(flash_init_data)); + return true; +} + +bool ICACHE_FLASH_ATTR +flash_init_data_blank(void) +{ + // FLASH SEC - 2 + // Dangerous, here are dinosaur infested!!!!! + // Reboot required!!! + // It will init system config to blank! + SPIEraseSector((flash_get_sec_num() - 2)); + SPIEraseSector((flash_get_sec_num() - 1)); + return true; +} + +bool ICACHE_FLASH_ATTR +flash_self_destruct(void) +{ + // Erase your flash. Good bye! + SPIEraseChip(); + return true; +} \ No newline at end of file diff --git a/app/platform/flash_api.h b/app/platform/flash_api.h index 8ff2b455..5642562f 100644 --- a/app/platform/flash_api.h +++ b/app/platform/flash_api.h @@ -29,6 +29,16 @@ typedef struct __attribute__((packed)) } size : 4; } SPIFlashInfo; -uint32_t flash_get_size_byte(); -uint16_t flash_get_sec_num(); +SPIFlashInfo *flash_get_info(void); +uint8_t flash_get_size(void); +uint32_t flash_get_size_byte(void); +bool flash_set_size(uint8_t); +bool flash_set_size_byte(uint32_t); +uint16_t flash_get_sec_num(void); +uint8_t flash_get_mode(void); +uint32_t flash_get_speed(void); +bool flash_init_data_default(void); +bool flash_init_data_blank(void); +bool flash_self_destruct(void); + #endif // __FLASH_API_H__