Merge pull request #68 from nodemcu/dev094

Add node.flashid() and node.flashsize().
This commit is contained in:
zeroday 2015-01-06 19:23:42 +08:00
commit 0a3702f885
6 changed files with 270 additions and 14 deletions

View File

@ -1,8 +1,14 @@
#ifndef __USER_CONFIG_H__ #ifndef __USER_CONFIG_H__
#define __USER_CONFIG_H__ #define __USER_CONFIG_H__
#define NODE_VERSION_MAJOR 0U
#define NODE_VERSION_MINOR 9U
#define NODE_VERSION_REVISION 4U
#define NODE_VERSION_INTERNAL 0U
#define NODE_VERSION "NodeMcu 0.9.4" #define NODE_VERSION "NodeMcu 0.9.4"
#define BUILD_DATE "build 20141230" #define BUILD_DATE "build 20150101"
// #define FLASH_512K // #define FLASH_512K
// #define FLASH_1M // #define FLASH_1M
// #define FLASH_2M // #define FLASH_2M
@ -29,6 +35,9 @@
#define NODE_ERR #define NODE_ERR
#endif /* NODE_ERROR */ #endif /* NODE_ERROR */
#define NODE_STORE_TYPEDEF_ATTR __attribute__((aligned(4),packed))
#define NODE_STORE_ATTR __attribute__((aligned(4)))
#define CLIENT_SSL_ENABLE #define CLIENT_SSL_ENABLE
#define GPIO_INTERRUPT_ENABLE #define GPIO_INTERRUPT_ENABLE

View File

@ -645,6 +645,7 @@ void ICACHE_FLASH_ATTR readline(lua_Load *load){
os_timer_setfn(&lua_timer, (os_timer_func_t *)dojob, load); os_timer_setfn(&lua_timer, (os_timer_func_t *)dojob, load);
os_timer_arm(&lua_timer, READLINE_INTERVAL, 0); // no repeat os_timer_arm(&lua_timer, READLINE_INTERVAL, 0); // no repeat
} }
continue;
} }
/* other control character or not an acsii character */ /* other control character or not an acsii character */

View File

@ -152,9 +152,15 @@ static int ICACHE_FLASH_ATTR file_check( lua_State* L )
#endif #endif
// Lua: readline() // g_read()
static int ICACHE_FLASH_ATTR file_readline( lua_State* L ) static int ICACHE_FLASH_ATTR file_g_read( lua_State* L, int n, int16_t end_char )
{ {
if(n< 0 || n>LUAL_BUFFERSIZE)
n = LUAL_BUFFERSIZE;
if(end_char < 0 || end_char >255)
end_char = EOF;
signed char ec = (signed char)end_char;
luaL_Buffer b; luaL_Buffer b;
if((FS_OPEN_OK - 1)==file_fd) if((FS_OPEN_OK - 1)==file_fd)
return luaL_error(L, "open a file first"); return luaL_error(L, "open a file first");
@ -170,7 +176,7 @@ static int ICACHE_FLASH_ATTR file_readline( lua_State* L )
break; break;
} }
p[i++] = c; p[i++] = c;
}while((c!=EOF) && (c!='\n') && (i<LUAL_BUFFERSIZE) ); }while((c!=EOF) && (c!=ec) && (i<n) );
#if 0 #if 0
if(i>0 && p[i-1] == '\n') if(i>0 && p[i-1] == '\n')
@ -187,6 +193,40 @@ static int ICACHE_FLASH_ATTR file_readline( lua_State* L )
return 1; /* read at least an `eol' */ return 1; /* read at least an `eol' */
} }
// Lua: read()
// file.read() will read all byte in file
// file.read(10) will read 10 byte from file, or EOF is reached.
// file.read('q') will read until 'q' or EOF is reached.
static int ICACHE_FLASH_ATTR file_read( lua_State* L )
{
unsigned need_len = LUAL_BUFFERSIZE;
int16_t end_char = EOF;
size_t el;
if( lua_type( L, 1 ) == LUA_TNUMBER )
{
need_len = ( unsigned )luaL_checkinteger( L, 1 );
if( need_len > LUAL_BUFFERSIZE ){
need_len = LUAL_BUFFERSIZE;
}
}
else if(lua_isstring(L, 1))
{
const char *end = luaL_checklstring( L, 1, &el );
if(el!=1){
return luaL_error( L, "wrong arg range" );
}
end_char = (int16_t)end[0];
}
return file_g_read(L, need_len, end_char);
}
// Lua: readline()
static int ICACHE_FLASH_ATTR file_readline( lua_State* L )
{
return file_g_read(L, LUAL_BUFFERSIZE, '\n');
}
// Lua: write("string") // Lua: write("string")
static int ICACHE_FLASH_ATTR file_write( lua_State* L ) static int ICACHE_FLASH_ATTR file_write( lua_State* L )
{ {
@ -233,6 +273,7 @@ const LUA_REG_TYPE file_map[] =
{ LSTRKEY( "close" ), LFUNCVAL( file_close ) }, { LSTRKEY( "close" ), LFUNCVAL( file_close ) },
{ LSTRKEY( "write" ), LFUNCVAL( file_write ) }, { LSTRKEY( "write" ), LFUNCVAL( file_write ) },
{ LSTRKEY( "writeline" ), LFUNCVAL( file_writeline ) }, { LSTRKEY( "writeline" ), LFUNCVAL( file_writeline ) },
{ LSTRKEY( "read" ), LFUNCVAL( file_read ) },
{ LSTRKEY( "readline" ), LFUNCVAL( file_readline ) }, { LSTRKEY( "readline" ), LFUNCVAL( file_readline ) },
#if defined(BUILD_WOFS) #if defined(BUILD_WOFS)
{ LSTRKEY( "format" ), LFUNCVAL( file_format ) }, { LSTRKEY( "format" ), LFUNCVAL( file_format ) },

View File

@ -11,6 +11,8 @@
#include "romfs.h" #include "romfs.h"
#include "c_string.h" #include "c_string.h"
#include "driver/uart.h" #include "driver/uart.h"
#include "spi_flash.h"
#include "flash_api.h"
// Lua: restart() // Lua: restart()
static int ICACHE_FLASH_ATTR node_restart( lua_State* L ) static int ICACHE_FLASH_ATTR node_restart( lua_State* L )
@ -30,6 +32,20 @@ static int ICACHE_FLASH_ATTR node_deepsleep( lua_State* L )
return 0; return 0;
} }
// Lua: info()
static int ICACHE_FLASH_ATTR node_info( lua_State* L )
{
lua_pushinteger(L, NODE_VERSION_MAJOR);
lua_pushinteger(L, NODE_VERSION_MINOR);
lua_pushinteger(L, NODE_VERSION_REVISION);
lua_pushinteger(L, system_get_chip_id()); // chip id
lua_pushinteger(L, spi_flash_get_id()); // flash id
lua_pushinteger(L, flash_get_size_byte() / 1024); // flash size in KB
lua_pushinteger(L, flash_get_mode());
lua_pushinteger(L, flash_get_speed());
return 8;
}
// Lua: chipid() // Lua: chipid()
static int ICACHE_FLASH_ATTR node_chipid( lua_State* L ) static int ICACHE_FLASH_ATTR node_chipid( lua_State* L )
{ {
@ -38,6 +54,31 @@ static int ICACHE_FLASH_ATTR node_chipid( lua_State* L )
return 1; 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() // Lua: heap()
static int ICACHE_FLASH_ATTR node_heap( lua_State* L ) static int ICACHE_FLASH_ATTR node_heap( lua_State* L )
{ {
@ -237,7 +278,10 @@ const LUA_REG_TYPE node_map[] =
{ {
{ LSTRKEY( "restart" ), LFUNCVAL( node_restart ) }, { LSTRKEY( "restart" ), LFUNCVAL( node_restart ) },
{ LSTRKEY( "dsleep" ), LFUNCVAL( node_deepsleep ) }, { LSTRKEY( "dsleep" ), LFUNCVAL( node_deepsleep ) },
{ LSTRKEY( "info" ), LFUNCVAL( node_info ) },
{ LSTRKEY( "chipid" ), LFUNCVAL( node_chipid ) }, { LSTRKEY( "chipid" ), LFUNCVAL( node_chipid ) },
{ LSTRKEY( "flashid" ), LFUNCVAL( node_flashid ) },
{ LSTRKEY( "flashsize" ), LFUNCVAL( node_flashsize) },
{ LSTRKEY( "heap" ), LFUNCVAL( node_heap ) }, { LSTRKEY( "heap" ), LFUNCVAL( node_heap ) },
{ LSTRKEY( "key" ), LFUNCVAL( node_key ) }, { LSTRKEY( "key" ), LFUNCVAL( node_key ) },
{ LSTRKEY( "led" ), LFUNCVAL( node_led ) }, { LSTRKEY( "led" ), LFUNCVAL( node_led ) },

View File

@ -1,14 +1,16 @@
/****************************************************************************** /******************************************************************************
* Flash api for NodeMCU * Flash api for NodeMCU
* NodeMCU Team
* 2014-12-31
*******************************************************************************/ *******************************************************************************/
#include "user_config.h" #include "user_config.h"
#include "flash_api.h" #include "flash_api.h"
#include "spi_flash.h" #include "spi_flash.h"
SPIFlashInfo *ICACHE_FLASH_ATTR SPIFlashInfo *ICACHE_FLASH_ATTR
flash_get_info() flash_get_info(void)
{ {
static SPIFlashInfo spi_flash_info; static SPIFlashInfo spi_flash_info NODE_STORE_ATTR;
static bool is_spi_flash_info_initialized = false; static bool is_spi_flash_info_initialized = false;
// Make the code more fast // Make the code more fast
if (!is_spi_flash_info_initialized) if (!is_spi_flash_info_initialized)
@ -16,11 +18,19 @@ flash_get_info()
SPIRead(0, &spi_flash_info, sizeof(spi_flash_info)); SPIRead(0, &spi_flash_info, sizeof(spi_flash_info));
is_spi_flash_info_initialized = true; is_spi_flash_info_initialized = true;
} }
return &spi_flash_info; // return (SPIFlashInfo *)(0x40200000);
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 uint32_t ICACHE_FLASH_ATTR
flash_get_size_byte() flash_get_size_byte(void)
{ {
static uint32_t flash_size = 0; static uint32_t flash_size = 0;
// Make the code more fast // Make the code more fast
@ -58,8 +68,68 @@ flash_get_size_byte()
return flash_size; 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 ...
uint8_t data[SPI_FLASH_SEC_SIZE] NODE_STORE_ATTR;
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 uint16_t ICACHE_FLASH_ATTR
flash_get_sec_num() flash_get_sec_num(void)
{ {
static uint16_t result = 0; static uint16_t result = 0;
// Make the code more fast // Make the code more fast
@ -69,3 +139,82 @@ flash_get_sec_num()
} }
return result; 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!
SPIEraseSector((flash_get_sec_num() - 4));
SPIWrite((flash_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, 0x10000 - SPI_FLASH_SEC_SIZE + (0), 128);
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;
}

View File

@ -1,7 +1,9 @@
#ifndef __FLASH_API_H__ #ifndef __FLASH_API_H__
#define __FLASH_API_H__ #define __FLASH_API_H__
#include "ets_sys.h" #include "ets_sys.h"
typedef struct __attribute__((packed)) #include "user_config.h"
typedef struct
{ {
uint8_t unknown0; uint8_t unknown0;
uint8_t unknown1; uint8_t unknown1;
@ -27,8 +29,18 @@ typedef struct __attribute__((packed))
SIZE_16MBIT = 3, SIZE_16MBIT = 3,
SIZE_32MBIT = 4, SIZE_32MBIT = 4,
} size : 4; } size : 4;
} SPIFlashInfo; } NODE_STORE_TYPEDEF_ATTR SPIFlashInfo;
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);
uint32_t flash_get_size_byte();
uint16_t flash_get_sec_num();
#endif // __FLASH_API_H__ #endif // __FLASH_API_H__