2016-09-16 10:10:18 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright 2013-2014 Espressif Systems (Wuxi)
|
|
|
|
*
|
|
|
|
* FileName: user_main.c
|
|
|
|
*
|
|
|
|
* Description: entry file of user application
|
|
|
|
*
|
|
|
|
* Modification history:
|
|
|
|
* 2014/1/1, v1.0 create this file.
|
|
|
|
*******************************************************************************/
|
|
|
|
#include "lua.h"
|
|
|
|
#include "platform.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2016-09-21 09:51:50 +02:00
|
|
|
#include "vfs.h"
|
2016-09-16 10:10:18 +02:00
|
|
|
#include "sdkconfig.h"
|
2016-09-21 09:51:50 +02:00
|
|
|
#include "esp_system.h"
|
2016-09-27 10:42:08 +02:00
|
|
|
#include "esp_event.h"
|
|
|
|
#include "nvs_flash.h"
|
2016-09-22 06:56:40 +02:00
|
|
|
#include "flash_api.h"
|
2016-09-16 10:10:18 +02:00
|
|
|
|
2016-09-20 05:35:56 +02:00
|
|
|
#include "driver/console.h"
|
2016-09-16 10:10:18 +02:00
|
|
|
#include "task/task.h"
|
|
|
|
#include "sections.h"
|
2016-09-27 10:42:08 +02:00
|
|
|
#include "nodemcu_esp_event.h"
|
2016-09-16 10:10:18 +02:00
|
|
|
|
2016-09-20 05:35:56 +02:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2016-09-27 10:42:08 +02:00
|
|
|
#include "freertos/queue.h"
|
2016-09-16 10:10:18 +02:00
|
|
|
|
|
|
|
#define SIG_LUA 0
|
|
|
|
#define SIG_UARTINPUT 1
|
|
|
|
|
|
|
|
|
2016-09-27 10:42:08 +02:00
|
|
|
static task_handle_t esp_event_task;
|
|
|
|
static QueueHandle_t esp_event_queue;
|
|
|
|
|
|
|
|
// We provide our own esp_event_send which hooks into the NodeMCU task
|
|
|
|
// task framework, and ensures all events are handled in the same context
|
|
|
|
// as the LVM, making life as easy as possible for us.
|
|
|
|
esp_err_t esp_event_send (system_event_t *event)
|
|
|
|
{
|
|
|
|
if (!event)
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
|
|
|
|
if (!esp_event_task || !esp_event_queue)
|
|
|
|
return ESP_ERR_INVALID_STATE; // too early!
|
|
|
|
|
|
|
|
portBASE_TYPE ret = xQueueSendToBack (esp_event_queue, event, 0);
|
|
|
|
if (ret != pdPASS)
|
|
|
|
{
|
|
|
|
NODE_ERR("failed to queue esp event %d", event->event_id);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the task_post() fails, it only means the event gets delayed, hence
|
|
|
|
// we claim OK regardless.
|
|
|
|
task_post_medium (esp_event_task, 0);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void handle_esp_event (task_param_t param, task_prio_t prio)
|
|
|
|
{
|
|
|
|
(void)param;
|
|
|
|
(void)prio;
|
|
|
|
|
|
|
|
system_event_t evt;
|
|
|
|
while (xQueueReceive (esp_event_queue, &evt, 0) == pdPASS)
|
|
|
|
{
|
|
|
|
esp_err_t ret = esp_event_process_default (&evt);
|
|
|
|
if (ret != ESP_OK)
|
|
|
|
NODE_ERR("default event handler failed for %d", evt.event_id);
|
|
|
|
|
|
|
|
nodemcu_esp_event_reg_t *evregs;
|
2016-09-29 10:21:18 +02:00
|
|
|
for (evregs = &esp_event_cb_table; evregs->callback; ++evregs)
|
2016-09-27 10:42:08 +02:00
|
|
|
{
|
|
|
|
if (evregs->event_id == evt.event_id)
|
|
|
|
evregs->callback (&evt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-16 10:10:18 +02:00
|
|
|
// +================== New task interface ==================+
|
2016-10-03 04:05:17 +02:00
|
|
|
static void start_lua ()
|
|
|
|
{
|
2016-09-16 10:10:18 +02:00
|
|
|
char* lua_argv[] = { (char *)"lua", (char *)"-i", NULL };
|
|
|
|
NODE_DBG("Task task_lua started.\n");
|
|
|
|
lua_main( 2, lua_argv );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_input(task_param_t flag, task_prio_t priority) {
|
|
|
|
(void)priority;
|
|
|
|
lua_handle_input (flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
static task_handle_t input_task;
|
|
|
|
|
|
|
|
task_handle_t user_get_input_sig(void) {
|
|
|
|
return input_task;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool user_process_input(bool force) {
|
|
|
|
return task_post_low(input_task, force);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nodemcu_init(void)
|
|
|
|
{
|
|
|
|
NODE_ERR("\n");
|
|
|
|
// Initialize platform first for lua modules.
|
|
|
|
if( platform_init() != PLATFORM_OK )
|
|
|
|
{
|
|
|
|
// This should never happen
|
|
|
|
NODE_DBG("Can not init platform for modules.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-21 09:51:50 +02:00
|
|
|
if (flash_safe_get_size_byte() != flash_rom_get_size_byte()) {
|
2016-09-22 06:56:40 +02:00
|
|
|
NODE_ERR("Incorrect flash size reported, adjusting...\n");
|
2016-09-16 10:10:18 +02:00
|
|
|
// Fit hardware real flash size.
|
|
|
|
flash_rom_set_size_byte(flash_safe_get_size_byte());
|
|
|
|
|
|
|
|
// Reboot to get SDK to use (or write) init data at new location
|
2016-11-27 03:28:48 +01:00
|
|
|
esp_restart ();
|
2016-09-16 10:10:18 +02:00
|
|
|
|
|
|
|
// Don't post the start_lua task, we're about to reboot...
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-21 09:51:50 +02:00
|
|
|
#if defined ( CONFIG_BUILD_SPIFFS )
|
2016-09-23 09:39:06 +02:00
|
|
|
// This can take a while, so be nice and provide some feedback while waiting
|
|
|
|
printf ("Mounting flash filesystem...\n");
|
2016-09-21 09:51:50 +02:00
|
|
|
if (!vfs_mount("/FLASH", 0)) {
|
|
|
|
// Failed to mount -- try reformat
|
|
|
|
NODE_ERR("Formatting file system. Please wait...\n");
|
2016-09-22 09:18:22 +02:00
|
|
|
if (!vfs_format()) {
|
2016-09-21 09:51:50 +02:00
|
|
|
NODE_ERR( "*** ERROR ***: unable to format. FS might be compromised.\n" );
|
|
|
|
NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" );
|
|
|
|
}
|
|
|
|
// Note that fs_format leaves the file system mounted
|
|
|
|
}
|
2016-09-16 10:10:18 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-27 10:42:08 +02:00
|
|
|
void app_main (void)
|
2016-09-16 10:10:18 +02:00
|
|
|
{
|
Fix net module data loss & RTOS task unsafety (#2829)
To avoid races between the lwIP callbacks (lwIP RTOS task) and the Lua
handlers (LVM RTOS task), the data flow and ownership has been simplified
and cleaned up.
lwIP callbacks now have no visibility of the userdata struct. They are
limited to creating small event objects and task_post()ing them over
to the LVM "thread", passing ownership in doing so. The shared identifier
then becomes the struct netconn*.
On the LVM side, we keep a linked list of active userdata objects. This
allows us to retrieve the correct userdata when we get an event with
a netconn pointer. Because this list is only ever used within the LVM
task, no locking is necessary.
The old approach of stashing a userdata pointer into the 'socket' field
on the netconn has been removed entirely, as this was both not
thread/RTOS-task safe, and also interfered with the IDFs internal use
of the socket field (even when using only the netconn layer). As an
added benefit, this removed the need for all the SYS_ARCH_PROTECT()
locking stuff.
The need to track receive events before the corresponding userdata object
has been established has been removed by virtue of not reordering the
"accept" and the "recv" events any more (previously accepts were posted
with medium priority, while the receives where high priority, leading
to the observed reordering and associated headaches).
The workaround for IDF issue 784 has been removed as it is now not needed
and is in fact directly harmful as it results in a double-free. Yay for
getting rid of old workarounds!
DNS resolution code paths were merged for the two instances of "socket"
initiated resolves (connect/dns functions).
Also fixed an instance of using a stack variable for receiving the resolved
IP address, with said variable going out of scope before the DNS resolution
necessarily completed (hello, memory corruption!).
Where possible, moved to use the Lua allocator rather than plain malloc.
Finally, the NodeMCU task posting mechanism got a polish and an adjustment.
Given all the Bad(tm) that tends to happen if something fails task posting,
I went through a couple of iterations on how to avoid that. Alas, the
preferred solution of blocking non-LVM RTOS tasks until a slot is free
turned out to not be viable, as this easily resulted in deadlocks with the
lwIP stack. After much deliberation I settled on increasing the number of
available queue slots for the task_post() mechanism, but in the interest
of user control also now made it user configurable via Kconfig.
2019-07-14 23:20:20 +02:00
|
|
|
task_init();
|
|
|
|
|
2016-09-27 10:42:08 +02:00
|
|
|
esp_event_queue =
|
|
|
|
xQueueCreate (CONFIG_SYSTEM_EVENT_QUEUE_SIZE, sizeof (system_event_t));
|
|
|
|
esp_event_task = task_get_id (handle_esp_event);
|
2016-09-16 10:10:18 +02:00
|
|
|
|
2016-09-27 10:42:08 +02:00
|
|
|
input_task = task_get_id (handle_input);
|
2016-09-16 10:10:18 +02:00
|
|
|
|
2016-09-27 10:42:08 +02:00
|
|
|
ConsoleSetup_t cfg;
|
|
|
|
cfg.bit_rate = CONFIG_CONSOLE_BIT_RATE;
|
|
|
|
cfg.data_bits = CONSOLE_NUM_BITS_8;
|
|
|
|
cfg.parity = CONSOLE_PARITY_NONE;
|
|
|
|
cfg.stop_bits = CONSOLE_STOP_BITS_1;
|
2017-04-06 21:09:22 +02:00
|
|
|
cfg.auto_baud =
|
|
|
|
#ifdef CONFIG_CONSOLE_BIT_RATE_AUTO
|
|
|
|
true;
|
|
|
|
#else
|
|
|
|
false;
|
|
|
|
#endif
|
2016-09-16 10:10:18 +02:00
|
|
|
|
2016-09-27 10:42:08 +02:00
|
|
|
console_init (&cfg, input_task);
|
2018-10-07 21:40:49 +02:00
|
|
|
setvbuf(stdout, NULL, _IONBF, 0);
|
2016-09-16 10:10:18 +02:00
|
|
|
|
2016-09-27 10:42:08 +02:00
|
|
|
nodemcu_init ();
|
2016-09-20 05:35:56 +02:00
|
|
|
|
2016-09-28 09:57:00 +02:00
|
|
|
nvs_flash_init ();
|
2016-09-27 10:42:08 +02:00
|
|
|
tcpip_adapter_init ();
|
2016-09-16 10:10:18 +02:00
|
|
|
|
2016-10-03 04:05:17 +02:00
|
|
|
start_lua ();
|
2016-09-27 10:42:08 +02:00
|
|
|
task_pump_messages ();
|
|
|
|
__builtin_unreachable ();
|
2016-09-16 10:10:18 +02:00
|
|
|
}
|