user_main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. static task_handle_t input_sig;
  30. static uint8 input_sig_flag = 0;
  31. /* Contents of esp_init_data_default.bin */
  32. extern const uint32_t init_data[];
  33. extern const uint32_t init_data_end[];
  34. __asm__(
  35. /* Place in .text for same reason as user_start_trampoline */
  36. ".section \".text\"\n"
  37. ".align 4\n"
  38. "init_data:\n"
  39. ".incbin \"" ESP_INIT_DATA_DEFAULT "\"\n"
  40. "init_data_end:\n"
  41. ".previous\n"
  42. );
  43. /* Note: the trampoline *must* be explicitly put into the .text segment, since
  44. * by the time it is invoked the irom has not yet been mapped. This naturally
  45. * also goes for anything the trampoline itself calls.
  46. */
  47. void TEXT_SECTION_ATTR user_start_trampoline (void)
  48. {
  49. __real__xtos_set_exception_handler (
  50. EXCCAUSE_LOAD_STORE_ERROR, load_non_32_wide_handler);
  51. #ifdef LUA_USE_MODULES_RTCTIME
  52. // Note: Keep this as close to call_user_start() as possible, since it
  53. // is where the cpu clock actually gets bumped to 80MHz.
  54. rtctime_early_startup ();
  55. #endif
  56. /* Re-implementation of default init data deployment. The SDK does not
  57. * appear to be laying down its own version of init data anymore, so
  58. * we have to do it again. To see whether we need to, we read out
  59. * the flash size and do a test for esp_init_data based on that size.
  60. * If it's missing, we need to initialize it *right now* before the SDK
  61. * starts up and gets stuck at "rf_cal[0] !=0x05,is 0xFF".
  62. * If the size byte is wrong, then we'll end up fixing up the init data
  63. * again on the next boot, after we've corrected the size byte.
  64. * Only remaining issue is lack of spare code bytes in iram, so this
  65. * is deliberately quite terse and not as readable as one might like.
  66. */
  67. SPIFlashInfo sfi;
  68. SPIRead (0, (uint32_t *)(&sfi), sizeof (sfi)); // Cache read not enabled yet, safe to use
  69. if (sfi.size < 2) // Compensate for out-of-order 4mbit vs 2mbit values
  70. sfi.size ^= 1;
  71. uint32_t flash_end_addr = (256 * 1024) << sfi.size;
  72. uint32_t init_data_hdr = 0xffffffff;
  73. uint32_t init_data_addr = flash_end_addr - 4 * SPI_FLASH_SEC_SIZE;
  74. SPIRead (init_data_addr, &init_data_hdr, sizeof (init_data_hdr));
  75. if (init_data_hdr == 0xffffffff)
  76. {
  77. SPIEraseSector (init_data_addr);
  78. SPIWrite (init_data_addr, init_data, 4 * (init_data_end - init_data));
  79. }
  80. call_user_start ();
  81. }
  82. // +================== New task interface ==================+
  83. static void start_lua(task_param_t param, uint8 priority) {
  84. char* lua_argv[] = { (char *)"lua", (char *)"-i", NULL };
  85. NODE_DBG("Task task_lua started.\n");
  86. lua_main( 2, lua_argv );
  87. // Only enable UART interrupts once we've successfully started up,
  88. // otherwise the task queue might fill up with input events and prevent
  89. // the start_lua task from being posted.
  90. ETS_UART_INTR_ENABLE();
  91. }
  92. static void handle_input(task_param_t flag, uint8 priority) {
  93. (void)priority;
  94. if (flag & 0x8000) {
  95. input_sig_flag = flag & 0x4000 ? 1 : 0;
  96. }
  97. lua_handle_input (flag & 0x01);
  98. }
  99. bool user_process_input(bool force) {
  100. return task_post_low(input_sig, force);
  101. }
  102. void nodemcu_init(void)
  103. {
  104. NODE_ERR("\n");
  105. // Initialize platform first for lua modules.
  106. if( platform_init() != PLATFORM_OK )
  107. {
  108. // This should never happen
  109. NODE_DBG("Can not init platform for modules.\n");
  110. return;
  111. }
  112. #if defined(FLASH_SAFE_API)
  113. if( flash_safe_get_size_byte() != flash_rom_get_size_byte()) {
  114. NODE_ERR("Self adjust flash size.\n");
  115. // Fit hardware real flash size.
  116. flash_rom_set_size_byte(flash_safe_get_size_byte());
  117. system_restart ();
  118. // Don't post the start_lua task, we're about to reboot...
  119. return;
  120. }
  121. #endif // defined(FLASH_SAFE_API)
  122. #if defined ( CLIENT_SSL_ENABLE ) && defined ( SSL_BUFFER_SIZE )
  123. espconn_secure_set_size(ESPCONN_CLIENT, SSL_BUFFER_SIZE);
  124. #endif
  125. #ifdef BUILD_SPIFFS
  126. if (!vfs_mount("/FLASH", 0)) {
  127. // Failed to mount -- try reformat
  128. dbg_printf("Formatting file system. Please wait...\n");
  129. if (!vfs_format()) {
  130. NODE_ERR( "\n*** ERROR ***: unable to format. FS might be compromised.\n" );
  131. NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" );
  132. }
  133. // Note that fs_format leaves the file system mounted
  134. }
  135. // test_spiffs();
  136. #endif
  137. // endpoint_setup();
  138. if (!task_post_low(task_get_id(start_lua),'s'))
  139. NODE_ERR("Failed to post the start_lua task!\n");
  140. }
  141. #ifdef LUA_USE_MODULES_WIFI
  142. #include "../modules/wifi_common.h"
  143. void user_rf_pre_init(void)
  144. {
  145. //set WiFi hostname before RF initialization (adds ~479 us to boot time)
  146. wifi_change_default_host_name();
  147. }
  148. #endif
  149. /******************************************************************************
  150. * FunctionName : user_rf_cal_sector_set
  151. * Description : SDK just reversed 4 sectors, used for rf init data and paramters.
  152. * We add this function to force users to set rf cal sector, since
  153. * we don't know which sector is free in user's application.
  154. * sector map for last several sectors : ABCCC
  155. * A : rf cal
  156. * B : rf init data
  157. * C : sdk parameters
  158. * Parameters : none
  159. * Returns : rf cal sector
  160. *******************************************************************************/
  161. uint32
  162. user_rf_cal_sector_set(void)
  163. {
  164. enum ext_flash_size_map size_map = system_get_flash_size_map();
  165. uint32 rf_cal_sec = 0;
  166. switch (size_map) {
  167. case FLASH_SIZE_4M_MAP_256_256:
  168. rf_cal_sec = 128 - 5;
  169. break;
  170. case FLASH_SIZE_8M_MAP_512_512:
  171. rf_cal_sec = 256 - 5;
  172. break;
  173. case FLASH_SIZE_16M_MAP_512_512:
  174. case FLASH_SIZE_16M_MAP_1024_1024:
  175. rf_cal_sec = 512 - 5;
  176. break;
  177. case FLASH_SIZE_32M_MAP_512_512:
  178. case FLASH_SIZE_32M_MAP_1024_1024:
  179. case FLASH_SIZE_32M_MAP_2048_2048:
  180. rf_cal_sec = 1024 - 5;
  181. break;
  182. case FLASH_SIZE_64M_MAP:
  183. rf_cal_sec = 2048 - 5;
  184. break;
  185. case FLASH_SIZE_128M_MAP:
  186. rf_cal_sec = 4096 - 5;
  187. break;
  188. default:
  189. rf_cal_sec = 0;
  190. break;
  191. }
  192. return rf_cal_sec;
  193. }
  194. /******************************************************************************
  195. * FunctionName : user_init
  196. * Description : entry of user application, init user function here
  197. * Parameters : none
  198. * Returns : none
  199. *******************************************************************************/
  200. void user_init(void)
  201. {
  202. #ifdef LUA_USE_MODULES_RTCTIME
  203. rtctime_late_startup ();
  204. #endif
  205. UartBautRate br = BIT_RATE_DEFAULT;
  206. input_sig = task_get_id(handle_input);
  207. uart_init (br, br, input_sig, &input_sig_flag);
  208. #ifndef NODE_DEBUG
  209. system_set_os_print(0);
  210. #endif
  211. system_init_done_cb(nodemcu_init);
  212. }