user_main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "c_stdio.h"
  16. #include "vfs.h"
  17. #include "flash_api.h"
  18. #include "user_interface.h"
  19. #include "user_exceptions.h"
  20. #include "user_modules.h"
  21. #include "ets_sys.h"
  22. #include "driver/uart.h"
  23. #include "task/task.h"
  24. #include "mem.h"
  25. #include "espconn.h"
  26. #ifdef LUA_USE_MODULES_RTCTIME
  27. #include "rtc/rtctime.h"
  28. #endif
  29. #define SIG_LUA 0
  30. #define SIG_UARTINPUT 1
  31. #define TASK_QUEUE_LEN 4
  32. static os_event_t *taskQueue;
  33. /* Note: the trampoline *must* be explicitly put into the .text segment, since
  34. * by the time it is invoked the irom has not yet been mapped. This naturally
  35. * also goes for anything the trampoline itself calls.
  36. */
  37. void TEXT_SECTION_ATTR user_start_trampoline (void)
  38. {
  39. __real__xtos_set_exception_handler (
  40. EXCCAUSE_LOAD_STORE_ERROR, load_non_32_wide_handler);
  41. #ifdef LUA_USE_MODULES_RTCTIME
  42. // Note: Keep this as close to call_user_start() as possible, since it
  43. // is where the cpu clock actually gets bumped to 80MHz.
  44. rtctime_early_startup ();
  45. #endif
  46. call_user_start ();
  47. }
  48. // +================== New task interface ==================+
  49. static void start_lua(task_param_t param, uint8 priority) {
  50. char* lua_argv[] = { (char *)"lua", (char *)"-i", NULL };
  51. NODE_DBG("Task task_lua started.\n");
  52. lua_main( 2, lua_argv );
  53. }
  54. static void handle_input(task_param_t flag, uint8 priority) {
  55. // c_printf("HANDLE_INPUT: %u %u\n", flag, priority); REMOVE
  56. lua_handle_input (flag);
  57. }
  58. static task_handle_t input_sig;
  59. task_handle_t user_get_input_sig(void) {
  60. return input_sig;
  61. }
  62. bool user_process_input(bool force) {
  63. return task_post_low(input_sig, force);
  64. }
  65. void nodemcu_init(void)
  66. {
  67. NODE_ERR("\n");
  68. // Initialize platform first for lua modules.
  69. if( platform_init() != PLATFORM_OK )
  70. {
  71. // This should never happen
  72. NODE_DBG("Can not init platform for modules.\n");
  73. return;
  74. }
  75. #if defined(FLASH_SAFE_API)
  76. if( flash_safe_get_size_byte() != flash_rom_get_size_byte()) {
  77. NODE_ERR("Self adjust flash size.\n");
  78. // Fit hardware real flash size.
  79. flash_rom_set_size_byte(flash_safe_get_size_byte());
  80. // Reboot to get SDK to use (or write) init data at new location
  81. system_restart ();
  82. // Don't post the start_lua task, we're about to reboot...
  83. return;
  84. }
  85. #endif // defined(FLASH_SAFE_API)
  86. #if defined ( CLIENT_SSL_ENABLE ) && defined ( SSL_BUFFER_SIZE )
  87. espconn_secure_set_size(ESPCONN_CLIENT, SSL_BUFFER_SIZE);
  88. #endif
  89. #ifdef BUILD_SPIFFS
  90. if (!vfs_mount("/FLASH", 0)) {
  91. // Failed to mount -- try reformat
  92. c_printf("Formatting file system. Please wait...\n");
  93. if (!vfs_format()) {
  94. NODE_ERR( "\n*** ERROR ***: unable to format. FS might be compromised.\n" );
  95. NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" );
  96. }
  97. // Note that fs_format leaves the file system mounted
  98. }
  99. // test_spiffs();
  100. #endif
  101. // endpoint_setup();
  102. task_post_low(task_get_id(start_lua),'s');
  103. }
  104. #ifdef LUA_USE_MODULES_WIFI
  105. #include "../modules/wifi_common.h"
  106. void user_rf_pre_init(void)
  107. {
  108. //set WiFi hostname before RF initialization (adds ~479 us to boot time)
  109. wifi_change_default_host_name();
  110. }
  111. #endif
  112. /******************************************************************************
  113. * FunctionName : user_rf_cal_sector_set
  114. * Description : SDK just reversed 4 sectors, used for rf init data and paramters.
  115. * We add this function to force users to set rf cal sector, since
  116. * we don't know which sector is free in user's application.
  117. * sector map for last several sectors : ABCCC
  118. * A : rf cal
  119. * B : rf init data
  120. * C : sdk parameters
  121. * Parameters : none
  122. * Returns : rf cal sector
  123. *******************************************************************************/
  124. uint32
  125. user_rf_cal_sector_set(void)
  126. {
  127. enum flash_size_map size_map = system_get_flash_size_map();
  128. uint32 rf_cal_sec = 0;
  129. switch (size_map) {
  130. case FLASH_SIZE_4M_MAP_256_256:
  131. rf_cal_sec = 128 - 5;
  132. break;
  133. case FLASH_SIZE_8M_MAP_512_512:
  134. rf_cal_sec = 256 - 5;
  135. break;
  136. case FLASH_SIZE_16M_MAP_512_512:
  137. case FLASH_SIZE_16M_MAP_1024_1024:
  138. rf_cal_sec = 512 - 5;
  139. break;
  140. case FLASH_SIZE_32M_MAP_512_512:
  141. case FLASH_SIZE_32M_MAP_1024_1024:
  142. rf_cal_sec = 1024 - 5;
  143. break;
  144. default:
  145. rf_cal_sec = 0;
  146. break;
  147. }
  148. return rf_cal_sec;
  149. }
  150. /******************************************************************************
  151. * FunctionName : user_init
  152. * Description : entry of user application, init user function here
  153. * Parameters : none
  154. * Returns : none
  155. *******************************************************************************/
  156. void user_init(void)
  157. {
  158. #ifdef LUA_USE_MODULES_RTCTIME
  159. rtctime_late_startup ();
  160. #endif
  161. UartBautRate br = BIT_RATE_DEFAULT;
  162. input_sig = task_get_id(handle_input);
  163. uart_init (br, br, input_sig);
  164. #ifndef NODE_DEBUG
  165. system_set_os_print(0);
  166. #endif
  167. system_init_done_cb(nodemcu_init);
  168. }