user_main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 \".rodata.dram\"\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. // enable operations on >4MB flash chip
  69. extern SpiFlashChip * flashchip;
  70. uint32 orig_chip_size = flashchip->chip_size;
  71. flashchip->chip_size = FLASH_SIZE_16MBYTE;
  72. SPIRead (0, (uint32_t *)(&sfi), sizeof (sfi)); // Cache read not enabled yet, safe to use
  73. // handle all size entries
  74. switch (sfi.size) {
  75. case 0: sfi.size = 1; break; // SIZE_4MBIT
  76. case 1: sfi.size = 0; break; // SIZE_2MBIT
  77. case 5: sfi.size = 3; break; // SIZE_16MBIT_8M_8M
  78. case 6: // fall-through
  79. case 7: sfi.size = 4; break; // SIZE_32MBIT_8M_8M, SIZE_32MBIT_16M_16M
  80. case 8: sfi.size = 5; break; // SIZE_64MBIT
  81. case 9: sfi.size = 6; break; // SIZE_128MBIT
  82. default: break;
  83. }
  84. uint32_t flash_end_addr = (256 * 1024) << sfi.size;
  85. uint32_t init_data_hdr = 0xffffffff;
  86. uint32_t init_data_addr = flash_end_addr - 4 * SPI_FLASH_SEC_SIZE;
  87. SPIRead (init_data_addr, &init_data_hdr, sizeof (init_data_hdr));
  88. if (init_data_hdr == 0xffffffff)
  89. {
  90. SPIEraseSector (init_data_addr);
  91. SPIWrite (init_data_addr, init_data, 4 * (init_data_end - init_data));
  92. }
  93. // revert temporary setting
  94. flashchip->chip_size = orig_chip_size;
  95. call_user_start ();
  96. }
  97. // +================== New task interface ==================+
  98. static void start_lua(task_param_t param, uint8 priority) {
  99. char* lua_argv[] = { (char *)"lua", (char *)"-i", NULL };
  100. NODE_DBG("Task task_lua started.\n");
  101. lua_main( 2, lua_argv );
  102. // Only enable UART interrupts once we've successfully started up,
  103. // otherwise the task queue might fill up with input events and prevent
  104. // the start_lua task from being posted.
  105. ETS_UART_INTR_ENABLE();
  106. }
  107. static void handle_input(task_param_t flag, uint8 priority) {
  108. (void)priority;
  109. if (flag & 0x8000) {
  110. input_sig_flag = flag & 0x4000 ? 1 : 0;
  111. }
  112. lua_handle_input (flag & 0x01);
  113. }
  114. bool user_process_input(bool force) {
  115. return task_post_low(input_sig, force);
  116. }
  117. void nodemcu_init(void)
  118. {
  119. NODE_ERR("\n");
  120. // Initialize platform first for lua modules.
  121. if( platform_init() != PLATFORM_OK )
  122. {
  123. // This should never happen
  124. NODE_DBG("Can not init platform for modules.\n");
  125. return;
  126. }
  127. if( flash_detect_size_byte() != flash_rom_get_size_byte() ) {
  128. NODE_ERR("Self adjust flash size.\n");
  129. // Fit hardware real flash size.
  130. flash_rom_set_size_byte(flash_detect_size_byte());
  131. system_restart ();
  132. // Don't post the start_lua task, we're about to reboot...
  133. return;
  134. }
  135. #if defined ( CLIENT_SSL_ENABLE ) && defined ( SSL_BUFFER_SIZE )
  136. espconn_secure_set_size(ESPCONN_CLIENT, SSL_BUFFER_SIZE);
  137. #endif
  138. #ifdef BUILD_SPIFFS
  139. if (!vfs_mount("/FLASH", 0)) {
  140. // Failed to mount -- try reformat
  141. dbg_printf("Formatting file system. Please wait...\n");
  142. if (!vfs_format()) {
  143. NODE_ERR( "\n*** ERROR ***: unable to format. FS might be compromised.\n" );
  144. NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" );
  145. }
  146. // Note that fs_format leaves the file system mounted
  147. }
  148. // test_spiffs();
  149. #endif
  150. // endpoint_setup();
  151. if (!task_post_low(task_get_id(start_lua),'s'))
  152. NODE_ERR("Failed to post the start_lua task!\n");
  153. }
  154. #ifdef LUA_USE_MODULES_WIFI
  155. #include "../modules/wifi_common.h"
  156. void user_rf_pre_init(void)
  157. {
  158. //set WiFi hostname before RF initialization (adds ~479 us to boot time)
  159. wifi_change_default_host_name();
  160. }
  161. #endif
  162. /******************************************************************************
  163. * FunctionName : user_rf_cal_sector_set
  164. * Description : SDK just reversed 4 sectors, used for rf init data and paramters.
  165. * We add this function to force users to set rf cal sector, since
  166. * we don't know which sector is free in user's application.
  167. * sector map for last several sectors : ABCCC
  168. * A : rf cal
  169. * B : rf init data
  170. * C : sdk parameters
  171. * Parameters : none
  172. * Returns : rf cal sector
  173. *******************************************************************************/
  174. uint32
  175. user_rf_cal_sector_set(void)
  176. {
  177. enum flash_size_map size_map = system_get_flash_size_map();
  178. uint32 rf_cal_sec = 0;
  179. switch (size_map) {
  180. case FLASH_SIZE_4M_MAP_256_256:
  181. rf_cal_sec = 128 - 5;
  182. break;
  183. case FLASH_SIZE_8M_MAP_512_512:
  184. rf_cal_sec = 256 - 5;
  185. break;
  186. case FLASH_SIZE_16M_MAP_512_512:
  187. case FLASH_SIZE_16M_MAP_1024_1024:
  188. rf_cal_sec = 512 - 5;
  189. break;
  190. case FLASH_SIZE_32M_MAP_512_512:
  191. case FLASH_SIZE_32M_MAP_1024_1024:
  192. case FLASH_SIZE_32M_MAP_2048_2048:
  193. rf_cal_sec = 1024 - 5;
  194. break;
  195. case FLASH_SIZE_64M_MAP_1024_1024:
  196. rf_cal_sec = 2048 - 5;
  197. break;
  198. case FLASH_SIZE_128M_MAP_1024_1024:
  199. rf_cal_sec = 4096 - 5;
  200. break;
  201. default:
  202. rf_cal_sec = 0;
  203. break;
  204. }
  205. return rf_cal_sec;
  206. }
  207. /******************************************************************************
  208. * FunctionName : user_init
  209. * Description : entry of user application, init user function here
  210. * Parameters : none
  211. * Returns : none
  212. *******************************************************************************/
  213. void user_init(void)
  214. {
  215. #ifdef LUA_USE_MODULES_RTCTIME
  216. rtctime_late_startup ();
  217. #endif
  218. UartBautRate br = BIT_RATE_DEFAULT;
  219. input_sig = task_get_id(handle_input);
  220. uart_init (br, br, input_sig, &input_sig_flag);
  221. #ifndef NODE_DEBUG
  222. system_set_os_print(0);
  223. #endif
  224. system_init_done_cb(nodemcu_init);
  225. }