Commit 56789592 authored by Johny Mattsson's avatar Johny Mattsson
Browse files

Boot to (nonresponsive) Lua prompt on RTOS!

Uart driver currently disabled as it's not (yet) compatible with RTOS.

Running Lua task with excessive stack to avoid smashing it; need to work out
what's using so much stack space.

Changed some flash reading functions to not attempt to drop an entire 4k
flash page onto the stack.

Ensure the task pump doesn't attempt to retrieve from uninitialised queues.
parent 97568e98
...@@ -129,8 +129,8 @@ static int docall (lua_State *L, int narg, int clear) { ...@@ -129,8 +129,8 @@ static int docall (lua_State *L, int narg, int clear) {
static void print_version (lua_State *L) { static void print_version (lua_State *L) {
lua_pushliteral (L, "\n" NODE_VERSION " build " BUILD_DATE " powered by " LUA_RELEASE " on SDK "); lua_pushliteral (L, "\n" NODE_VERSION " build " BUILD_DATE " powered by " LUA_RELEASE " on RTOS-SDK ");
lua_pushstring (L, SDK_VERSION); lua_pushstring (L, system_get_sdk_version ());
lua_concat (L, 2); lua_concat (L, 2);
const char *msg = lua_tostring (L, -1); const char *msg = lua_tostring (L, -1);
l_message (NULL, msg); l_message (NULL, msg);
......
...@@ -29,19 +29,31 @@ static int adc_init107( lua_State *L ) ...@@ -29,19 +29,31 @@ static int adc_init107( lua_State *L )
{ {
uint8_t byte107 = luaL_checkinteger (L, 1); uint8_t byte107 = luaL_checkinteger (L, 1);
int erridx = -1;
const char *errmsg[] = {
"out of memory",
"flash read error",
"flash erase error",
"flash write error"
};
#define return_error(idx) do { erridx = idx; goto out; } while (0)
uint32 init_sector = flash_safe_get_sec_num () - 4; uint32 init_sector = flash_safe_get_sec_num () - 4;
// Note 32bit alignment so we can safely cast to uint32 for the flash api char *init_data = malloc (SPI_FLASH_SEC_SIZE);
char init_data[SPI_FLASH_SEC_SIZE] __attribute__((aligned(4))); if (!init_data)
return_error(0);
if (SPI_FLASH_RESULT_OK != flash_safe_read ( if (SPI_FLASH_RESULT_OK != flash_safe_read (
init_sector * SPI_FLASH_SEC_SIZE, init_sector * SPI_FLASH_SEC_SIZE,
(uint32 *)init_data, sizeof(init_data))) (uint32 *)init_data, sizeof(init_data)))
return luaL_error(L, "flash read error"); return_error(1);
// If it's already the correct value, we don't need to force it // If it's already the correct value, we don't need to force it
if (init_data[107] == byte107) if (init_data[107] == byte107)
{ {
free (init_data);
lua_pushboolean (L, false); lua_pushboolean (L, false);
return 1; return 1;
} }
...@@ -49,15 +61,22 @@ static int adc_init107( lua_State *L ) ...@@ -49,15 +61,22 @@ static int adc_init107( lua_State *L )
// Nope, it differs, we need to rewrite it // Nope, it differs, we need to rewrite it
init_data[107] = byte107; init_data[107] = byte107;
if (SPI_FLASH_RESULT_OK != flash_safe_erase_sector (init_sector)) if (SPI_FLASH_RESULT_OK != flash_safe_erase_sector (init_sector))
return luaL_error(L, "flash erase error"); return_error(2);
if (SPI_FLASH_RESULT_OK != flash_safe_write ( if (SPI_FLASH_RESULT_OK != flash_safe_write (
init_sector * SPI_FLASH_SEC_SIZE, init_sector * SPI_FLASH_SEC_SIZE,
(uint32 *)init_data, sizeof(init_data))) (uint32 *)init_data, sizeof(init_data)))
return luaL_error(L, "flash write error"); return_error(3);
lua_pushboolean (L, true); out:
return 1; free (init_data);
if (erridx >= 0)
return luaL_error(L, errmsg[erridx]);
else
{
lua_pushboolean (L, true);
return 1;
}
} }
// Module function map // Module function map
......
...@@ -133,7 +133,10 @@ bool flash_rom_set_size_type(uint8_t size) ...@@ -133,7 +133,10 @@ bool flash_rom_set_size_type(uint8_t size)
// Reboot required!!! // Reboot required!!!
// If you don't know what you're doing, your nodemcu may turn into stone ... // If you don't know what you're doing, your nodemcu may turn into stone ...
NODE_DBG("\nBEGIN SET FLASH HEADER\n"); NODE_DBG("\nBEGIN SET FLASH HEADER\n");
uint8_t data[SPI_FLASH_SEC_SIZE] ICACHE_STORE_ATTR; uint8_t *data = malloc (SPI_FLASH_SEC_SIZE);
if (!data)
return false;
if (SPI_FLASH_RESULT_OK == spi_flash_read(0, (uint32 *)data, SPI_FLASH_SEC_SIZE)) if (SPI_FLASH_RESULT_OK == spi_flash_read(0, (uint32 *)data, SPI_FLASH_SEC_SIZE))
{ {
((SPIFlashInfo *)(&data[0]))->size = size; ((SPIFlashInfo *)(&data[0]))->size = size;
...@@ -146,6 +149,7 @@ bool flash_rom_set_size_type(uint8_t size) ...@@ -146,6 +149,7 @@ bool flash_rom_set_size_type(uint8_t size)
NODE_DBG("\nWRITE SUCCESS, %u\n", size); NODE_DBG("\nWRITE SUCCESS, %u\n", size);
} }
} }
free (data);
NODE_DBG("\nEND SET FLASH HEADER\n"); NODE_DBG("\nEND SET FLASH HEADER\n");
return true; return true;
} }
...@@ -267,7 +271,9 @@ bool flash_rom_set_speed(uint32_t speed) ...@@ -267,7 +271,9 @@ bool flash_rom_set_speed(uint32_t speed)
// Reboot required!!! // Reboot required!!!
// If you don't know what you're doing, your nodemcu may turn into stone ... // If you don't know what you're doing, your nodemcu may turn into stone ...
NODE_DBG("\nBEGIN SET FLASH HEADER\n"); NODE_DBG("\nBEGIN SET FLASH HEADER\n");
uint8_t data[SPI_FLASH_SEC_SIZE] ICACHE_STORE_ATTR; uint8_t *data = malloc (SPI_FLASH_SEC_SIZE);
if (!data)
return false;
uint8_t speed_type = SPEED_40MHZ; uint8_t speed_type = SPEED_40MHZ;
if (speed < 26700000) if (speed < 26700000)
{ {
...@@ -297,6 +303,7 @@ bool flash_rom_set_speed(uint32_t speed) ...@@ -297,6 +303,7 @@ bool flash_rom_set_speed(uint32_t speed)
NODE_DBG("\nWRITE SUCCESS, %u\n", speed_type); NODE_DBG("\nWRITE SUCCESS, %u\n", speed_type);
} }
} }
free (data);
NODE_DBG("\nEND SET FLASH HEADER\n"); NODE_DBG("\nEND SET FLASH HEADER\n");
return true; return true;
} }
......
...@@ -97,7 +97,7 @@ static bool next_event (task_event_t *ev, task_prio_t *prio) ...@@ -97,7 +97,7 @@ static bool next_event (task_event_t *ev, task_prio_t *prio)
{ {
for (task_prio_t p = TASK_PRIORITY_COUNT; p != TASK_PRIORITY_LOW; --p) for (task_prio_t p = TASK_PRIORITY_COUNT; p != TASK_PRIORITY_LOW; --p)
{ {
if (xQueueReceive (task_Q[p-1], ev, 0) == pdTRUE) if (task_Q[p-1] && xQueueReceive (task_Q[p-1], ev, 0) == pdTRUE)
{ {
*prio = p-1; *prio = p-1;
return true; return true;
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "task/task.h" #include "task/task.h"
#include "sections.h" #include "sections.h"
#include "mem.h" #include "mem.h"
#include "freertos/task.h"
#ifdef LUA_USE_MODULES_RTCTIME #ifdef LUA_USE_MODULES_RTCTIME
#include "rtc/rtctime.h" #include "rtc/rtctime.h"
...@@ -117,6 +118,18 @@ void nodemcu_init(void) ...@@ -117,6 +118,18 @@ void nodemcu_init(void)
task_post_low(task_get_id(start_lua),'s'); task_post_low(task_get_id(start_lua),'s');
} }
static void nodemcu_main (void *param)
{
(void)param;
nodemcu_init ();
task_pump_messages ();
__builtin_unreachable ();
}
/****************************************************************************** /******************************************************************************
* FunctionName : user_init * FunctionName : user_init
* Description : entry of user application, init user function here * Description : entry of user application, init user function here
...@@ -129,14 +142,21 @@ void user_init(void) ...@@ -129,14 +142,21 @@ void user_init(void)
rtctime_late_startup (); rtctime_late_startup ();
#endif #endif
#if 0
// TODO: fix uart driver to work with RTOS SDK
UartBautRate br = BIT_RATE_DEFAULT; UartBautRate br = BIT_RATE_DEFAULT;
input_sig = task_get_id(handle_input); input_sig = task_get_id(handle_input);
uart_init (br, br, input_sig); uart_init (br, br, input_sig);
#endif
#ifndef NODE_DEBUG #ifndef NODE_DEBUG
system_set_os_print(0); system_set_os_print(0);
#endif #endif
system_init_done_cb(nodemcu_init); // FIXME! Max supported RTOS stack size is 512 words (2k bytes), but
// NodeMCU currently uses more than that. The game is on to find these
// culprits, but this gcc doesn't do the -fstack-usage option :(
xTaskCreate (
nodemcu_main, "nodemcu", 600, 0, configTIMER_TASK_PRIORITY +1, NULL);
} }
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
// FIXME // FIXME
static inline void system_set_os_print(uint8 onoff) { (void)onoff; } static inline void system_set_os_print(uint8 onoff) { (void)onoff; }
static inline void system_init_done_cb(void *cb) { (void)cb; }
bool wifi_softap_deauth(uint8 mac[6]); bool wifi_softap_deauth(uint8 mac[6]);
......
Markdown is supported
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