user_main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "flash_fs.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 "mem.h"
  23. #ifdef LUA_USE_MODULES_RTCTIME
  24. #include "rtc/rtctime.h"
  25. #endif
  26. #define SIG_LUA 0
  27. #define SIG_UARTINPUT 1
  28. #define TASK_QUEUE_LEN 4
  29. static os_event_t *taskQueue;
  30. /* Important: no_init_data CAN NOT be left as zero initialised, as that
  31. * initialisation will happen after user_start_trampoline, but before
  32. * the user_init, thus clobbering our state!
  33. */
  34. static uint8_t no_init_data = 0xff;
  35. /* Note: the trampoline *must* be explicitly put into the .text segment, since
  36. * by the time it is invoked the irom has not yet been mapped. This naturally
  37. * also goes for anything the trampoline itself calls.
  38. */
  39. void TEXT_SECTION_ATTR user_start_trampoline (void)
  40. {
  41. __real__xtos_set_exception_handler (
  42. EXCCAUSE_LOAD_STORE_ERROR, load_non_32_wide_handler);
  43. #ifdef LUA_USE_MODULES_RTCTIME
  44. // Note: Keep this as close to call_user_start() as possible, since it
  45. // is where the cpu clock actually gets bumped to 80MHz.
  46. rtctime_early_startup ();
  47. #endif
  48. /* Minimal early detection of missing esp_init_data.
  49. * If it is missing, the SDK will write its own and thus we'd end up
  50. * using that unless the flash size field is incorrect. This then leads
  51. * to different esp_init_data being used depending on whether the user
  52. * flashed with the right flash size or not (and the better option would
  53. * be to flash with an *incorrect* flash size, counter-intuitively).
  54. * To avoid that mess, we read out the flash size and do a test for
  55. * esp_init_data based on that size. If it's missing, flag for later.
  56. * If the flash size was incorrect, we'll end up fixing it all up
  57. * anyway, so this ends up solving the conundrum. Only remaining issue
  58. * is lack of spare code bytes in iram, so this is deliberately quite
  59. * terse and not as readable as one might like.
  60. */
  61. SPIFlashInfo sfi;
  62. SPIRead (0, &sfi, sizeof (sfi)); // Cache read not enabled yet, safe to use
  63. if (sfi.size < 2) // Compensate for out-of-order 4mbit vs 2mbit values
  64. sfi.size ^= 1;
  65. uint32_t flash_end_addr = (256 * 1024) << sfi.size;
  66. uint32_t init_data_hdr = 0xffffffff;
  67. SPIRead (flash_end_addr - 4 * SPI_FLASH_SEC_SIZE, &init_data_hdr, sizeof (init_data_hdr));
  68. no_init_data = (init_data_hdr == 0xffffffff);
  69. call_user_start ();
  70. }
  71. void task_lua(os_event_t *e){
  72. char* lua_argv[] = { (char *)"lua", (char *)"-i", NULL };
  73. NODE_DBG("Task task_lua started.\n");
  74. switch(e->sig){
  75. case SIG_LUA:
  76. NODE_DBG("SIG_LUA received.\n");
  77. lua_main( 2, lua_argv );
  78. break;
  79. case SIG_UARTINPUT:
  80. lua_handle_input (false);
  81. break;
  82. case LUA_PROCESS_LINE_SIG:
  83. lua_handle_input (true);
  84. break;
  85. default:
  86. break;
  87. }
  88. }
  89. void task_init(void){
  90. taskQueue = (os_event_t *)os_malloc(sizeof(os_event_t) * TASK_QUEUE_LEN);
  91. system_os_task(task_lua, USER_TASK_PRIO_0, taskQueue, TASK_QUEUE_LEN);
  92. }
  93. // extern void test_spiffs();
  94. // extern int test_romfs();
  95. // extern uint16_t flash_get_sec_num();
  96. void nodemcu_init(void)
  97. {
  98. NODE_ERR("\n");
  99. // Initialize platform first for lua modules.
  100. if( platform_init() != PLATFORM_OK )
  101. {
  102. // This should never happen
  103. NODE_DBG("Can not init platform for modules.\n");
  104. return;
  105. }
  106. #if defined(FLASH_SAFE_API)
  107. if( flash_safe_get_size_byte() != flash_rom_get_size_byte()) {
  108. NODE_ERR("Self adjust flash size.\n");
  109. // Fit hardware real flash size.
  110. flash_rom_set_size_byte(flash_safe_get_size_byte());
  111. // Write out init data at real location.
  112. no_init_data = true;
  113. if( !fs_format() )
  114. {
  115. NODE_ERR( "\ni*** ERROR ***: unable to format. FS might be compromised.\n" );
  116. NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" );
  117. }
  118. else{
  119. NODE_ERR( "format done.\n" );
  120. }
  121. fs_unmount(); // mounted by format.
  122. }
  123. #endif // defined(FLASH_SAFE_API)
  124. if (no_init_data)
  125. {
  126. NODE_ERR("Restore init data.\n");
  127. // Flash init data at FLASHSIZE - 0x04000 Byte.
  128. flash_init_data_default();
  129. // Flash blank data at FLASHSIZE - 0x02000 Byte.
  130. flash_init_data_blank();
  131. // Reboot to make the new data come into effect
  132. system_restart ();
  133. }
  134. #if defined( BUILD_WOFS )
  135. romfs_init();
  136. // if( !wofs_format() )
  137. // {
  138. // NODE_ERR( "\ni*** ERROR ***: unable to erase the flash. WOFS might be compromised.\n" );
  139. // NODE_ERR( "It is advised to re-flash the NodeWifi image.\n" );
  140. // }
  141. // else
  142. // NODE_ERR( "format done.\n" );
  143. // test_romfs();
  144. #elif defined ( BUILD_SPIFFS )
  145. fs_mount();
  146. // test_spiffs();
  147. #endif
  148. // endpoint_setup();
  149. // char* lua_argv[] = { (char *)"lua", (char *)"-e", (char *)"print(collectgarbage'count');ttt={};for i=1,100 do table.insert(ttt,i*2 -1);print(i);end for k, v in pairs(ttt) do print('<'..k..' '..v..'>') end print(collectgarbage'count');", NULL };
  150. // lua_main( 3, lua_argv );
  151. // char* lua_argv[] = { (char *)"lua", (char *)"-i", NULL };
  152. // lua_main( 2, lua_argv );
  153. // char* lua_argv[] = { (char *)"lua", (char *)"-e", (char *)"pwm.setup(0,100,50) pwm.start(0) pwm.stop(0)", NULL };
  154. // lua_main( 3, lua_argv );
  155. // NODE_DBG("Flash sec num: 0x%x\n", flash_get_sec_num());
  156. task_init();
  157. system_os_post(LUA_TASK_PRIO,SIG_LUA,'s');
  158. }
  159. /******************************************************************************
  160. * FunctionName : user_init
  161. * Description : entry of user application, init user function here
  162. * Parameters : none
  163. * Returns : none
  164. *******************************************************************************/
  165. void user_init(void)
  166. {
  167. #ifdef LUA_USE_MODULES_RTCTIME
  168. rtctime_late_startup ();
  169. #endif
  170. // NODE_DBG("SDK version:%s\n", system_get_sdk_version());
  171. // system_print_meminfo();
  172. // os_printf("Heap size::%d.\n",system_get_free_heap_size());
  173. // os_delay_us(50*1000); // delay 50ms before init uart
  174. UartBautRate br = BIT_RATE_DEFAULT;
  175. uart_init (br, br, USER_TASK_PRIO_0, SIG_UARTINPUT);
  176. #ifndef NODE_DEBUG
  177. system_set_os_print(0);
  178. #endif
  179. system_init_done_cb(nodemcu_init);
  180. }