user_main.c 7.3 KB

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