user_main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 TASK_QUEUE_LEN 4
  28. os_event_t *taskQueue;
  29. /* Note: the trampoline *must* be explicitly put into the .text segment, since
  30. * by the time it is invoked the irom has not yet been mapped. This naturally
  31. * also goes for anything the trampoline itself calls.
  32. */
  33. void TEXT_SECTION_ATTR user_start_trampoline (void)
  34. {
  35. __real__xtos_set_exception_handler (
  36. EXCCAUSE_LOAD_STORE_ERROR, load_non_32_wide_handler);
  37. #ifdef LUA_USE_MODULES_RTCTIME
  38. // Note: Keep this as close to call_user_start() as possible, since it
  39. // is where the cpu clock actually gets bumped to 80MHz.
  40. rtctime_early_startup ();
  41. #endif
  42. call_user_start ();
  43. }
  44. void task_lua(os_event_t *e){
  45. char* lua_argv[] = { (char *)"lua", (char *)"-i", NULL };
  46. NODE_DBG("Task task_lua started.\n");
  47. switch(e->sig){
  48. case SIG_LUA:
  49. NODE_DBG("SIG_LUA received.\n");
  50. lua_main( 2, lua_argv );
  51. break;
  52. default:
  53. break;
  54. }
  55. }
  56. void task_init(void){
  57. taskQueue = (os_event_t *)os_malloc(sizeof(os_event_t) * TASK_QUEUE_LEN);
  58. system_os_task(task_lua, USER_TASK_PRIO_0, taskQueue, TASK_QUEUE_LEN);
  59. }
  60. // extern void test_spiffs();
  61. // extern int test_romfs();
  62. // extern uint16_t flash_get_sec_num();
  63. void nodemcu_init(void)
  64. {
  65. NODE_ERR("\n");
  66. // Initialize platform first for lua modules.
  67. if( platform_init() != PLATFORM_OK )
  68. {
  69. // This should never happen
  70. NODE_DBG("Can not init platform for modules.\n");
  71. return;
  72. }
  73. #if defined(FLASH_SAFE_API)
  74. if( flash_safe_get_size_byte() != flash_rom_get_size_byte()) {
  75. NODE_ERR("Self adjust flash size.\n");
  76. // Fit hardware real flash size.
  77. flash_rom_set_size_byte(flash_safe_get_size_byte());
  78. // Flash init data at FLASHSIZE - 0x04000 Byte.
  79. flash_init_data_default();
  80. // Flash blank data at FLASHSIZE - 0x02000 Byte.
  81. flash_init_data_blank();
  82. if( !fs_format() )
  83. {
  84. NODE_ERR( "\ni*** ERROR ***: unable to format. FS might be compromised.\n" );
  85. NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" );
  86. }
  87. else{
  88. NODE_ERR( "format done.\n" );
  89. }
  90. fs_unmount(); // mounted by format.
  91. }
  92. #endif // defined(FLASH_SAFE_API)
  93. if( !flash_init_data_written() ){
  94. NODE_ERR("Restore init data.\n");
  95. // Flash init data at FLASHSIZE - 0x04000 Byte.
  96. flash_init_data_default();
  97. // Flash blank data at FLASHSIZE - 0x02000 Byte.
  98. flash_init_data_blank();
  99. }
  100. #if defined( BUILD_WOFS )
  101. romfs_init();
  102. // if( !wofs_format() )
  103. // {
  104. // NODE_ERR( "\ni*** ERROR ***: unable to erase the flash. WOFS might be compromised.\n" );
  105. // NODE_ERR( "It is advised to re-flash the NodeWifi image.\n" );
  106. // }
  107. // else
  108. // NODE_ERR( "format done.\n" );
  109. // test_romfs();
  110. #elif defined ( BUILD_SPIFFS )
  111. fs_mount();
  112. // test_spiffs();
  113. #endif
  114. // endpoint_setup();
  115. // 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 };
  116. // lua_main( 3, lua_argv );
  117. // char* lua_argv[] = { (char *)"lua", (char *)"-i", NULL };
  118. // lua_main( 2, lua_argv );
  119. // char* lua_argv[] = { (char *)"lua", (char *)"-e", (char *)"pwm.setup(0,100,50) pwm.start(0) pwm.stop(0)", NULL };
  120. // lua_main( 3, lua_argv );
  121. // NODE_DBG("Flash sec num: 0x%x\n", flash_get_sec_num());
  122. task_init();
  123. system_os_post(USER_TASK_PRIO_0,SIG_LUA,'s');
  124. }
  125. /******************************************************************************
  126. * FunctionName : user_init
  127. * Description : entry of user application, init user function here
  128. * Parameters : none
  129. * Returns : none
  130. *******************************************************************************/
  131. void user_init(void)
  132. {
  133. #ifdef LUA_USE_MODULES_RTCTIME
  134. rtctime_late_startup ();
  135. #endif
  136. // NODE_DBG("SDK version:%s\n", system_get_sdk_version());
  137. // system_print_meminfo();
  138. // os_printf("Heap size::%d.\n",system_get_free_heap_size());
  139. // os_delay_us(50*1000); // delay 50ms before init uart
  140. #ifdef DEVELOP_VERSION
  141. uart_init(BIT_RATE_74880, BIT_RATE_74880);
  142. #else
  143. uart_init(BIT_RATE_9600, BIT_RATE_9600);
  144. #endif
  145. // uart_init(BIT_RATE_115200, BIT_RATE_115200);
  146. #ifndef NODE_DEBUG
  147. system_set_os_print(0);
  148. #endif
  149. system_init_done_cb(nodemcu_init);
  150. }