user_main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /******************************************************************************
  2. * Copyright 2013-2014 Espressif Systems (Wuxi)
  3. *
  4. * FileName: user_main.c
  5. *
  6. * Description: entry file of user application
  7. *
  8. * Modification history:
  9. * 2014/1/1, v1.0 create this file.
  10. *******************************************************************************/
  11. #include "lua.h"
  12. #include "platform.h"
  13. #include "c_string.h"
  14. #include "c_stdlib.h"
  15. #include "flash_fs.h"
  16. #include "flash_api.h"
  17. #include "user_interface.h"
  18. #include "user_exceptions.h"
  19. #include "user_modules.h"
  20. #include "ets_sys.h"
  21. #include "driver/uart.h"
  22. #include "task/task.h"
  23. #include "mem.h"
  24. #ifdef LUA_USE_MODULES_RTCTIME
  25. #include "rtc/rtctime.h"
  26. #endif
  27. #define SIG_LUA 0
  28. #define SIG_UARTINPUT 1
  29. #define TASK_QUEUE_LEN 4
  30. static os_event_t *taskQueue;
  31. /* Note: the trampoline *must* be explicitly put into the .text segment, since
  32. * by the time it is invoked the irom has not yet been mapped. This naturally
  33. * also goes for anything the trampoline itself calls.
  34. */
  35. void TEXT_SECTION_ATTR user_start_trampoline (void)
  36. {
  37. __real__xtos_set_exception_handler (
  38. EXCCAUSE_LOAD_STORE_ERROR, load_non_32_wide_handler);
  39. #ifdef LUA_USE_MODULES_RTCTIME
  40. // Note: Keep this as close to call_user_start() as possible, since it
  41. // is where the cpu clock actually gets bumped to 80MHz.
  42. rtctime_early_startup ();
  43. #endif
  44. call_user_start ();
  45. }
  46. // +================== New task interface ==================+
  47. static void start_lua(task_param_t param, uint8 priority) {
  48. char* lua_argv[] = { (char *)"lua", (char *)"-i", NULL };
  49. NODE_DBG("Task task_lua started.\n");
  50. lua_main( 2, lua_argv );
  51. }
  52. static void handle_input(task_param_t flag, uint8 priority) {
  53. // c_printf("HANDLE_INPUT: %u %u\n", flag, priority); REMOVE
  54. lua_handle_input (flag);
  55. }
  56. static task_handle_t input_sig;
  57. task_handle_t user_get_input_sig(void) {
  58. return input_sig;
  59. }
  60. bool user_process_input(bool force) {
  61. return task_post_low(input_sig, force);
  62. }
  63. void nodemcu_init(void)
  64. {
  65. NODE_ERR("\n");
  66. // Initialize platform first for lua modules.
  67. if( platform_init() != PLATFORM_OK )
  68. {
  69. // This should never happen
  70. NODE_DBG("Can not init platform for modules.\n");
  71. return;
  72. }
  73. #if defined(FLASH_SAFE_API)
  74. if( flash_safe_get_size_byte() != flash_rom_get_size_byte()) {
  75. NODE_ERR("Self adjust flash size.\n");
  76. // Fit hardware real flash size.
  77. flash_rom_set_size_byte(flash_safe_get_size_byte());
  78. // Reboot to get SDK to use (or write) init data at new location
  79. system_restart ();
  80. // Don't post the start_lua task, we're about to reboot...
  81. return;
  82. }
  83. #endif // defined(FLASH_SAFE_API)
  84. #if defined ( BUILD_SPIFFS )
  85. if (!fs_mount()) {
  86. // Failed to mount -- try reformat
  87. c_printf("Formatting file system.\n");
  88. if (!fs_format()) {
  89. NODE_ERR( "\n*** ERROR ***: unable to format. FS might be compromised.\n" );
  90. NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" );
  91. }
  92. // Note that fs_format leaves the file system mounted
  93. }
  94. // test_spiffs();
  95. #endif
  96. // endpoint_setup();
  97. task_post_low(task_get_id(start_lua),'s');
  98. }
  99. #ifdef LUA_USE_MODULES_WIFI
  100. #include "../modules/wifi_common.h"
  101. void user_rf_pre_init(void)
  102. {
  103. //set WiFi hostname before RF initialization (adds ~479 us to boot time)
  104. wifi_change_default_host_name();
  105. }
  106. #endif
  107. /******************************************************************************
  108. * FunctionName : user_init
  109. * Description : entry of user application, init user function here
  110. * Parameters : none
  111. * Returns : none
  112. *******************************************************************************/
  113. void user_init(void)
  114. {
  115. #ifdef LUA_USE_MODULES_RTCTIME
  116. rtctime_late_startup ();
  117. #endif
  118. UartBautRate br = BIT_RATE_DEFAULT;
  119. input_sig = task_get_id(handle_input);
  120. uart_init (br, br, input_sig);
  121. #ifndef NODE_DEBUG
  122. system_set_os_print(0);
  123. #endif
  124. system_init_done_cb(nodemcu_init);
  125. }