user_main.c 4.9 KB

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