From 56789592faf02caa4c6e8018ff41265e06b2abe5 Mon Sep 17 00:00:00 2001 From: Johny Mattsson Date: Mon, 30 May 2016 17:56:33 +1000 Subject: [PATCH] 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. --- app/lua/lua.c | 4 ++-- app/modules/adc.c | 33 ++++++++++++++++++++------ app/platform/flash_api.c | 11 +++++++-- app/task/task.c | 2 +- app/user/user_main.c | 22 ++++++++++++++++- sdk-overrides/include/user_interface.h | 1 - 6 files changed, 59 insertions(+), 14 deletions(-) diff --git a/app/lua/lua.c b/app/lua/lua.c index 173b612e..0a07d224 100644 --- a/app/lua/lua.c +++ b/app/lua/lua.c @@ -129,8 +129,8 @@ static int docall (lua_State *L, int narg, int clear) { static void print_version (lua_State *L) { - lua_pushliteral (L, "\n" NODE_VERSION " build " BUILD_DATE " powered by " LUA_RELEASE " on SDK "); - lua_pushstring (L, SDK_VERSION); + lua_pushliteral (L, "\n" NODE_VERSION " build " BUILD_DATE " powered by " LUA_RELEASE " on RTOS-SDK "); + lua_pushstring (L, system_get_sdk_version ()); lua_concat (L, 2); const char *msg = lua_tostring (L, -1); l_message (NULL, msg); diff --git a/app/modules/adc.c b/app/modules/adc.c index 06068cd8..20e0bcf2 100644 --- a/app/modules/adc.c +++ b/app/modules/adc.c @@ -29,19 +29,31 @@ static int adc_init107( lua_State *L ) { 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; - // Note 32bit alignment so we can safely cast to uint32 for the flash api - char init_data[SPI_FLASH_SEC_SIZE] __attribute__((aligned(4))); + char *init_data = malloc (SPI_FLASH_SEC_SIZE); + if (!init_data) + return_error(0); if (SPI_FLASH_RESULT_OK != flash_safe_read ( init_sector * SPI_FLASH_SEC_SIZE, (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 (init_data[107] == byte107) { + free (init_data); lua_pushboolean (L, false); return 1; } @@ -49,15 +61,22 @@ static int adc_init107( lua_State *L ) // Nope, it differs, we need to rewrite it init_data[107] = byte107; 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 ( init_sector * SPI_FLASH_SEC_SIZE, (uint32 *)init_data, sizeof(init_data))) - return luaL_error(L, "flash write error"); + return_error(3); - lua_pushboolean (L, true); - return 1; +out: + free (init_data); + if (erridx >= 0) + return luaL_error(L, errmsg[erridx]); + else + { + lua_pushboolean (L, true); + return 1; + } } // Module function map diff --git a/app/platform/flash_api.c b/app/platform/flash_api.c index cf21ae5f..f1354d45 100644 --- a/app/platform/flash_api.c +++ b/app/platform/flash_api.c @@ -133,7 +133,10 @@ bool flash_rom_set_size_type(uint8_t size) // Reboot required!!! // If you don't know what you're doing, your nodemcu may turn into stone ... 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)) { ((SPIFlashInfo *)(&data[0]))->size = size; @@ -146,6 +149,7 @@ bool flash_rom_set_size_type(uint8_t size) NODE_DBG("\nWRITE SUCCESS, %u\n", size); } } + free (data); NODE_DBG("\nEND SET FLASH HEADER\n"); return true; } @@ -267,7 +271,9 @@ bool flash_rom_set_speed(uint32_t speed) // Reboot required!!! // If you don't know what you're doing, your nodemcu may turn into stone ... 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; if (speed < 26700000) { @@ -297,6 +303,7 @@ bool flash_rom_set_speed(uint32_t speed) NODE_DBG("\nWRITE SUCCESS, %u\n", speed_type); } } + free (data); NODE_DBG("\nEND SET FLASH HEADER\n"); return true; } diff --git a/app/task/task.c b/app/task/task.c index af61b68a..3ca057e0 100644 --- a/app/task/task.c +++ b/app/task/task.c @@ -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) { - 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; return true; diff --git a/app/user/user_main.c b/app/user/user_main.c index 8e7b7aeb..2b0b44ad 100644 --- a/app/user/user_main.c +++ b/app/user/user_main.c @@ -23,6 +23,7 @@ #include "task/task.h" #include "sections.h" #include "mem.h" +#include "freertos/task.h" #ifdef LUA_USE_MODULES_RTCTIME #include "rtc/rtctime.h" @@ -117,6 +118,18 @@ void nodemcu_init(void) 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 * Description : entry of user application, init user function here @@ -129,14 +142,21 @@ void user_init(void) rtctime_late_startup (); #endif +#if 0 + // TODO: fix uart driver to work with RTOS SDK UartBautRate br = BIT_RATE_DEFAULT; input_sig = task_get_id(handle_input); uart_init (br, br, input_sig); +#endif #ifndef NODE_DEBUG system_set_os_print(0); #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); } diff --git a/sdk-overrides/include/user_interface.h b/sdk-overrides/include/user_interface.h index 25e6ee09..e195e1c9 100644 --- a/sdk-overrides/include/user_interface.h +++ b/sdk-overrides/include/user_interface.h @@ -14,7 +14,6 @@ // FIXME 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]);