2014-12-22 12:35:05 +01: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 "c_string.h"
|
|
|
|
#include "c_stdlib.h"
|
2016-09-05 20:17:13 +02:00
|
|
|
#include "c_stdio.h"
|
|
|
|
#include "vfs.h"
|
2016-03-24 00:06:09 +01:00
|
|
|
#include "flash_api.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
#include "user_interface.h"
|
2015-06-22 10:43:01 +02:00
|
|
|
#include "user_exceptions.h"
|
2015-06-25 07:32:21 +02:00
|
|
|
#include "user_modules.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
|
|
#include "ets_sys.h"
|
|
|
|
#include "driver/uart.h"
|
Add New Tasking I/F and rework GPIO, UART, etc to support it
As with the last commit this rolls up the follwowing, but include the various
review comments on the PR.
- **Documentation changes**. I've added the taks FAQ as a stub new Extension
developer FAQ, and split the old FAQ into a Lua Developer FAQ and a Hardware
FAQ.
- **Tasking I/F**. New `app/task/Makefile`, `app/task/task.c`,
`app/include/task/task.h` and `app/Makefile` as per previous commit. Cascade
changes to `app/driver/uart.c`, `app/include/driver/uart.h`,
`app/user/user_main.c` and `app/modules/node.c`
- **GPIO Rework** to `app/modules/gpio.c` and `pin_map.[hc]`, `platform.[hc]`
in `app/platform`
- **Other Optimisations** Move the `platform_*_exists()` from
`app/platform/common.c` to static inline declarations in `platform.h` as
this generates faster, smaller code. Move lgc.a routines out of iram0.
2016-02-17 18:13:17 +01:00
|
|
|
#include "task/task.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
#include "mem.h"
|
2016-09-04 22:45:46 +02:00
|
|
|
#include "espconn.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
|
2015-06-25 07:32:21 +02:00
|
|
|
#ifdef LUA_USE_MODULES_RTCTIME
|
|
|
|
#include "rtc/rtctime.h"
|
|
|
|
#endif
|
|
|
|
|
2016-10-03 10:19:20 +02:00
|
|
|
static task_handle_t input_sig;
|
2016-10-18 03:56:38 +02:00
|
|
|
static uint8 input_sig_flag = 0;
|
2015-11-12 02:57:07 +01:00
|
|
|
|
2016-10-04 19:11:34 +02:00
|
|
|
/* Contents of esp_init_data_default.bin */
|
|
|
|
extern const uint32_t init_data[];
|
|
|
|
extern const uint32_t init_data_end[];
|
|
|
|
__asm__(
|
|
|
|
/* Place in .text for same reason as user_start_trampoline */
|
2016-12-04 20:19:51 +01:00
|
|
|
".section \".rodata.dram\"\n"
|
2016-10-04 19:11:34 +02:00
|
|
|
".align 4\n"
|
|
|
|
"init_data:\n"
|
|
|
|
".incbin \"" ESP_INIT_DATA_DEFAULT "\"\n"
|
|
|
|
"init_data_end:\n"
|
|
|
|
".previous\n"
|
|
|
|
);
|
2015-11-12 02:57:07 +01:00
|
|
|
|
2015-06-23 07:38:52 +02:00
|
|
|
/* Note: the trampoline *must* be explicitly put into the .text segment, since
|
|
|
|
* by the time it is invoked the irom has not yet been mapped. This naturally
|
|
|
|
* also goes for anything the trampoline itself calls.
|
|
|
|
*/
|
2015-07-06 05:45:35 +02:00
|
|
|
void TEXT_SECTION_ATTR user_start_trampoline (void)
|
2015-06-23 07:38:52 +02:00
|
|
|
{
|
|
|
|
__real__xtos_set_exception_handler (
|
|
|
|
EXCCAUSE_LOAD_STORE_ERROR, load_non_32_wide_handler);
|
|
|
|
|
2015-06-25 07:32:21 +02:00
|
|
|
#ifdef LUA_USE_MODULES_RTCTIME
|
|
|
|
// Note: Keep this as close to call_user_start() as possible, since it
|
|
|
|
// is where the cpu clock actually gets bumped to 80MHz.
|
2015-07-06 05:45:35 +02:00
|
|
|
rtctime_early_startup ();
|
2015-06-25 07:32:21 +02:00
|
|
|
#endif
|
2015-11-12 02:57:07 +01:00
|
|
|
|
2016-10-04 03:04:09 +02:00
|
|
|
/* Re-implementation of default init data deployment. The SDK does not
|
|
|
|
* appear to be laying down its own version of init data anymore, so
|
|
|
|
* we have to do it again. To see whether we need to, we read out
|
|
|
|
* the flash size and do a test for esp_init_data based on that size.
|
|
|
|
* If it's missing, we need to initialize it *right now* before the SDK
|
|
|
|
* starts up and gets stuck at "rf_cal[0] !=0x05,is 0xFF".
|
|
|
|
* If the size byte is wrong, then we'll end up fixing up the init data
|
|
|
|
* again on the next boot, after we've corrected the size byte.
|
|
|
|
* Only remaining issue is lack of spare code bytes in iram, so this
|
|
|
|
* is deliberately quite terse and not as readable as one might like.
|
|
|
|
*/
|
|
|
|
SPIFlashInfo sfi;
|
|
|
|
SPIRead (0, (uint32_t *)(&sfi), sizeof (sfi)); // Cache read not enabled yet, safe to use
|
|
|
|
if (sfi.size < 2) // Compensate for out-of-order 4mbit vs 2mbit values
|
|
|
|
sfi.size ^= 1;
|
|
|
|
uint32_t flash_end_addr = (256 * 1024) << sfi.size;
|
|
|
|
uint32_t init_data_hdr = 0xffffffff;
|
|
|
|
uint32_t init_data_addr = flash_end_addr - 4 * SPI_FLASH_SEC_SIZE;
|
|
|
|
SPIRead (init_data_addr, &init_data_hdr, sizeof (init_data_hdr));
|
|
|
|
if (init_data_hdr == 0xffffffff)
|
|
|
|
{
|
|
|
|
SPIEraseSector (init_data_addr);
|
2016-10-04 19:11:34 +02:00
|
|
|
SPIWrite (init_data_addr, init_data, 4 * (init_data_end - init_data));
|
2016-10-04 03:04:09 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 07:38:52 +02:00
|
|
|
call_user_start ();
|
|
|
|
}
|
|
|
|
|
Add New Tasking I/F and rework GPIO, UART, etc to support it
As with the last commit this rolls up the follwowing, but include the various
review comments on the PR.
- **Documentation changes**. I've added the taks FAQ as a stub new Extension
developer FAQ, and split the old FAQ into a Lua Developer FAQ and a Hardware
FAQ.
- **Tasking I/F**. New `app/task/Makefile`, `app/task/task.c`,
`app/include/task/task.h` and `app/Makefile` as per previous commit. Cascade
changes to `app/driver/uart.c`, `app/include/driver/uart.h`,
`app/user/user_main.c` and `app/modules/node.c`
- **GPIO Rework** to `app/modules/gpio.c` and `pin_map.[hc]`, `platform.[hc]`
in `app/platform`
- **Other Optimisations** Move the `platform_*_exists()` from
`app/platform/common.c` to static inline declarations in `platform.h` as
this generates faster, smaller code. Move lgc.a routines out of iram0.
2016-02-17 18:13:17 +01:00
|
|
|
// +================== New task interface ==================+
|
|
|
|
static void start_lua(task_param_t param, uint8 priority) {
|
|
|
|
char* lua_argv[] = { (char *)"lua", (char *)"-i", NULL };
|
|
|
|
NODE_DBG("Task task_lua started.\n");
|
|
|
|
lua_main( 2, lua_argv );
|
2016-10-03 10:19:20 +02:00
|
|
|
// Only enable UART interrupts once we've successfully started up,
|
|
|
|
// otherwise the task queue might fill up with input events and prevent
|
|
|
|
// the start_lua task from being posted.
|
|
|
|
ETS_UART_INTR_ENABLE();
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
Add New Tasking I/F and rework GPIO, UART, etc to support it
As with the last commit this rolls up the follwowing, but include the various
review comments on the PR.
- **Documentation changes**. I've added the taks FAQ as a stub new Extension
developer FAQ, and split the old FAQ into a Lua Developer FAQ and a Hardware
FAQ.
- **Tasking I/F**. New `app/task/Makefile`, `app/task/task.c`,
`app/include/task/task.h` and `app/Makefile` as per previous commit. Cascade
changes to `app/driver/uart.c`, `app/include/driver/uart.h`,
`app/user/user_main.c` and `app/modules/node.c`
- **GPIO Rework** to `app/modules/gpio.c` and `pin_map.[hc]`, `platform.[hc]`
in `app/platform`
- **Other Optimisations** Move the `platform_*_exists()` from
`app/platform/common.c` to static inline declarations in `platform.h` as
this generates faster, smaller code. Move lgc.a routines out of iram0.
2016-02-17 18:13:17 +01:00
|
|
|
static void handle_input(task_param_t flag, uint8 priority) {
|
2016-10-03 10:19:20 +02:00
|
|
|
(void)priority;
|
2016-10-18 03:56:38 +02:00
|
|
|
if (flag & 0x8000) {
|
|
|
|
input_sig_flag = flag & 0x4000 ? 1 : 0;
|
|
|
|
}
|
|
|
|
lua_handle_input (flag & 0x01);
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
Add New Tasking I/F and rework GPIO, UART, etc to support it
As with the last commit this rolls up the follwowing, but include the various
review comments on the PR.
- **Documentation changes**. I've added the taks FAQ as a stub new Extension
developer FAQ, and split the old FAQ into a Lua Developer FAQ and a Hardware
FAQ.
- **Tasking I/F**. New `app/task/Makefile`, `app/task/task.c`,
`app/include/task/task.h` and `app/Makefile` as per previous commit. Cascade
changes to `app/driver/uart.c`, `app/include/driver/uart.h`,
`app/user/user_main.c` and `app/modules/node.c`
- **GPIO Rework** to `app/modules/gpio.c` and `pin_map.[hc]`, `platform.[hc]`
in `app/platform`
- **Other Optimisations** Move the `platform_*_exists()` from
`app/platform/common.c` to static inline declarations in `platform.h` as
this generates faster, smaller code. Move lgc.a routines out of iram0.
2016-02-17 18:13:17 +01:00
|
|
|
bool user_process_input(bool force) {
|
|
|
|
return task_post_low(input_sig, force);
|
|
|
|
}
|
2014-12-30 19:47:44 +01:00
|
|
|
|
2015-01-23 06:25:54 +01:00
|
|
|
void nodemcu_init(void)
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
NODE_ERR("\n");
|
2015-10-30 04:25:02 +01:00
|
|
|
// Initialize platform first for lua modules.
|
2014-12-22 12:35:05 +01:00
|
|
|
if( platform_init() != PLATFORM_OK )
|
|
|
|
{
|
|
|
|
// This should never happen
|
|
|
|
NODE_DBG("Can not init platform for modules.\n");
|
|
|
|
return;
|
|
|
|
}
|
2015-03-15 22:40:43 +01:00
|
|
|
|
2016-12-26 14:17:57 +01:00
|
|
|
if( flash_safe_get_size_byte() <= FLASH_SIZE_4MBYTE ) {
|
|
|
|
if( flash_safe_get_size_byte() != flash_rom_get_size_byte() ) {
|
|
|
|
NODE_ERR("Self adjust flash size.\n");
|
|
|
|
// Fit hardware real flash size.
|
|
|
|
flash_rom_set_size_byte(flash_safe_get_size_byte());
|
|
|
|
|
|
|
|
system_restart ();
|
|
|
|
// Don't post the start_lua task, we're about to reboot...
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if( (flash_rom_get_size_byte() < FLASH_SIZE_1MBYTE) ||
|
|
|
|
(flash_rom_get_size_byte() > FLASH_SIZE_4MBYTE) ) {
|
|
|
|
NODE_ERR("Locking flash size for SDK to 1MByte.\n");
|
|
|
|
// SDK/ROM can't handle flash size > 4MByte, ensure a minimum of 1MByte for firmware image
|
|
|
|
flash_rom_set_size_byte(FLASH_SIZE_1MBYTE);
|
2015-11-12 02:57:07 +01:00
|
|
|
|
|
|
|
system_restart ();
|
2016-05-18 21:45:15 +02:00
|
|
|
// Don't post the start_lua task, we're about to reboot...
|
|
|
|
return;
|
2015-01-05 16:57:07 +01:00
|
|
|
}
|
|
|
|
|
2016-09-04 22:45:46 +02:00
|
|
|
#if defined ( CLIENT_SSL_ENABLE ) && defined ( SSL_BUFFER_SIZE )
|
|
|
|
espconn_secure_set_size(ESPCONN_CLIENT, SSL_BUFFER_SIZE);
|
|
|
|
#endif
|
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
#ifdef BUILD_SPIFFS
|
|
|
|
if (!vfs_mount("/FLASH", 0)) {
|
2016-06-05 23:10:58 +02:00
|
|
|
// Failed to mount -- try reformat
|
2016-10-27 08:38:47 +02:00
|
|
|
dbg_printf("Formatting file system. Please wait...\n");
|
2016-09-05 20:17:13 +02:00
|
|
|
if (!vfs_format()) {
|
2016-06-05 23:10:58 +02:00
|
|
|
NODE_ERR( "\n*** 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
|
|
|
|
}
|
2014-12-22 12:35:05 +01:00
|
|
|
// test_spiffs();
|
|
|
|
#endif
|
|
|
|
// endpoint_setup();
|
|
|
|
|
2016-10-03 10:19:20 +02:00
|
|
|
if (!task_post_low(task_get_id(start_lua),'s'))
|
|
|
|
NODE_ERR("Failed to post the start_lua task!\n");
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
2015-01-23 06:25:54 +01:00
|
|
|
|
2016-05-27 13:25:42 +02:00
|
|
|
#ifdef LUA_USE_MODULES_WIFI
|
|
|
|
#include "../modules/wifi_common.h"
|
|
|
|
|
|
|
|
void user_rf_pre_init(void)
|
|
|
|
{
|
2016-06-05 13:38:12 +02:00
|
|
|
//set WiFi hostname before RF initialization (adds ~479 us to boot time)
|
2016-05-27 13:25:42 +02:00
|
|
|
wifi_change_default_host_name();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-07-22 23:18:47 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : user_rf_cal_sector_set
|
|
|
|
* Description : SDK just reversed 4 sectors, used for rf init data and paramters.
|
|
|
|
* We add this function to force users to set rf cal sector, since
|
|
|
|
* we don't know which sector is free in user's application.
|
|
|
|
* sector map for last several sectors : ABCCC
|
|
|
|
* A : rf cal
|
|
|
|
* B : rf init data
|
|
|
|
* C : sdk parameters
|
|
|
|
* Parameters : none
|
|
|
|
* Returns : rf cal sector
|
|
|
|
*******************************************************************************/
|
|
|
|
uint32
|
|
|
|
user_rf_cal_sector_set(void)
|
|
|
|
{
|
2016-10-13 09:51:48 +02:00
|
|
|
enum ext_flash_size_map size_map = system_get_flash_size_map();
|
2016-07-22 23:18:47 +02:00
|
|
|
uint32 rf_cal_sec = 0;
|
|
|
|
|
|
|
|
switch (size_map) {
|
|
|
|
case FLASH_SIZE_4M_MAP_256_256:
|
|
|
|
rf_cal_sec = 128 - 5;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FLASH_SIZE_8M_MAP_512_512:
|
|
|
|
rf_cal_sec = 256 - 5;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FLASH_SIZE_16M_MAP_512_512:
|
|
|
|
case FLASH_SIZE_16M_MAP_1024_1024:
|
|
|
|
rf_cal_sec = 512 - 5;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FLASH_SIZE_32M_MAP_512_512:
|
|
|
|
case FLASH_SIZE_32M_MAP_1024_1024:
|
2016-10-13 09:51:48 +02:00
|
|
|
case FLASH_SIZE_32M_MAP_2048_2048:
|
2016-07-22 23:18:47 +02:00
|
|
|
rf_cal_sec = 1024 - 5;
|
|
|
|
break;
|
|
|
|
|
2016-10-13 09:51:48 +02:00
|
|
|
case FLASH_SIZE_64M_MAP:
|
|
|
|
rf_cal_sec = 2048 - 5;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FLASH_SIZE_128M_MAP:
|
|
|
|
rf_cal_sec = 4096 - 5;
|
|
|
|
break;
|
|
|
|
|
2016-07-22 23:18:47 +02:00
|
|
|
default:
|
|
|
|
rf_cal_sec = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rf_cal_sec;
|
|
|
|
}
|
|
|
|
|
2015-01-23 06:25:54 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* FunctionName : user_init
|
|
|
|
* Description : entry of user application, init user function here
|
|
|
|
* Parameters : none
|
|
|
|
* Returns : none
|
|
|
|
*******************************************************************************/
|
|
|
|
void user_init(void)
|
|
|
|
{
|
2015-07-06 05:45:35 +02:00
|
|
|
#ifdef LUA_USE_MODULES_RTCTIME
|
|
|
|
rtctime_late_startup ();
|
|
|
|
#endif
|
2015-01-23 06:25:54 +01:00
|
|
|
|
2015-10-30 04:25:02 +01:00
|
|
|
UartBautRate br = BIT_RATE_DEFAULT;
|
|
|
|
|
Add New Tasking I/F and rework GPIO, UART, etc to support it
As with the last commit this rolls up the follwowing, but include the various
review comments on the PR.
- **Documentation changes**. I've added the taks FAQ as a stub new Extension
developer FAQ, and split the old FAQ into a Lua Developer FAQ and a Hardware
FAQ.
- **Tasking I/F**. New `app/task/Makefile`, `app/task/task.c`,
`app/include/task/task.h` and `app/Makefile` as per previous commit. Cascade
changes to `app/driver/uart.c`, `app/include/driver/uart.h`,
`app/user/user_main.c` and `app/modules/node.c`
- **GPIO Rework** to `app/modules/gpio.c` and `pin_map.[hc]`, `platform.[hc]`
in `app/platform`
- **Other Optimisations** Move the `platform_*_exists()` from
`app/platform/common.c` to static inline declarations in `platform.h` as
this generates faster, smaller code. Move lgc.a routines out of iram0.
2016-02-17 18:13:17 +01:00
|
|
|
input_sig = task_get_id(handle_input);
|
2016-10-18 03:56:38 +02:00
|
|
|
uart_init (br, br, input_sig, &input_sig_flag);
|
2015-10-30 04:25:02 +01:00
|
|
|
|
2016-03-20 17:54:16 +01:00
|
|
|
#ifndef NODE_DEBUG
|
2015-01-23 06:25:54 +01:00
|
|
|
system_set_os_print(0);
|
2016-03-20 17:54:16 +01:00
|
|
|
#endif
|
2015-10-08 05:14:51 +02:00
|
|
|
|
2015-01-23 06:25:54 +01:00
|
|
|
system_init_done_cb(nodemcu_init);
|
|
|
|
}
|