user_main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <string.h>
  14. #include <stdlib.h>
  15. #include "flash_fs.h"
  16. #include "flash_api.h"
  17. #include "user_interface.h"
  18. #include "user_modules.h"
  19. #include "rom.h"
  20. #include "ets_sys.h"
  21. #include "driver/uart.h"
  22. #include "task/task.h"
  23. #include "sections.h"
  24. #include "mem.h"
  25. #include "freertos/task.h"
  26. #ifdef LUA_USE_MODULES_RTCTIME
  27. #include "rtc/rtctime.h"
  28. #endif
  29. #define SIG_LUA 0
  30. #define SIG_UARTINPUT 1
  31. #ifdef __ESP32__
  32. void system_soft_wdt_feed (void)
  33. {
  34. // FIXME this shouldn't go here, and it needs to tickle hardware watchdog
  35. }
  36. #endif
  37. extern void call_user_start (void);
  38. #if defined(__ESP8266__)
  39. /* Note: the trampoline *must* be explicitly put into the .text segment, since
  40. * by the time it is invoked the irom has not yet been mapped. This naturally
  41. * also goes for anything the trampoline itself calls.
  42. */
  43. void TEXT_SECTION_ATTR user_start_trampoline (void)
  44. {
  45. #ifdef LUA_USE_MODULES_RTCTIME
  46. // Note: Keep this as close to call_user_start() as possible, since it
  47. // is where the cpu clock actually gets bumped to 80MHz.
  48. rtctime_early_startup ();
  49. #endif
  50. /* The first thing call_user_start() does is switch the interrupt vector
  51. * base, which enables our 8/16bit load handler on the ESP8266. */
  52. call_user_start ();
  53. }
  54. #endif
  55. // +================== New task interface ==================+
  56. static void start_lua(task_param_t param, task_prio_t prio) {
  57. (void)param;
  58. (void)prio;
  59. char* lua_argv[] = { (char *)"lua", (char *)"-i", NULL };
  60. NODE_DBG("Task task_lua started.\n");
  61. lua_main( 2, lua_argv );
  62. }
  63. static void handle_input(task_param_t flag, task_prio_t priority) {
  64. (void)priority;
  65. lua_handle_input (flag);
  66. }
  67. static task_handle_t input_task;
  68. task_handle_t user_get_input_sig(void) {
  69. return input_task;
  70. }
  71. bool user_process_input(bool force) {
  72. return task_post_low(input_task, force);
  73. }
  74. void nodemcu_init(void)
  75. {
  76. NODE_ERR("\n");
  77. // Initialize platform first for lua modules.
  78. if( platform_init() != PLATFORM_OK )
  79. {
  80. // This should never happen
  81. NODE_DBG("Can not init platform for modules.\n");
  82. return;
  83. }
  84. // FIXME: this breaks horribly on the ESP32 at the moment; possibly all flash writes?
  85. #if defined(FLASH_SAFE_API) && defined(__ESP8266__)
  86. if( flash_safe_get_size_byte() != flash_rom_get_size_byte()) {
  87. NODE_ERR("Self adjust flash size.\n");
  88. // Fit hardware real flash size.
  89. flash_rom_set_size_byte(flash_safe_get_size_byte());
  90. if( !fs_format() )
  91. {
  92. NODE_ERR( "\ni*** ERROR ***: unable to format. FS might be compromised.\n" );
  93. NODE_ERR( "It is advised to re-flash the NodeMCU image.\n" );
  94. }
  95. else{
  96. NODE_ERR( "format done.\n" );
  97. }
  98. fs_unmount(); // mounted by format.
  99. // Reboot to get SDK to use (or write) init data at new location
  100. system_restart ();
  101. // Don't post the start_lua task, we're about to reboot...
  102. return;
  103. }
  104. #endif // defined(FLASH_SAFE_API)
  105. #if defined ( BUILD_SPIFFS )
  106. fs_mount();
  107. // test_spiffs();
  108. #endif
  109. task_post_low(task_get_id(start_lua), 0);
  110. }
  111. static void nodemcu_main (void *param)
  112. {
  113. (void)param;
  114. nodemcu_init ();
  115. task_pump_messages ();
  116. __builtin_unreachable ();
  117. }
  118. /******************************************************************************
  119. * FunctionName : user_init
  120. * Description : entry of user application, init user function here
  121. * Parameters : none
  122. * Returns : none
  123. *******************************************************************************/
  124. void user_init(void)
  125. {
  126. #ifdef LUA_USE_MODULES_RTCTIME
  127. rtctime_late_startup ();
  128. #endif
  129. input_task = task_get_id (handle_input);
  130. UART_ConfigTypeDef cfg;
  131. cfg.baud_rate = BIT_RATE_DEFAULT;
  132. cfg.data_bits = UART_WordLength_8b;
  133. cfg.parity = USART_Parity_None;
  134. cfg.stop_bits = USART_StopBits_1;
  135. cfg.flow_ctrl = USART_HardwareFlowControl_None;
  136. cfg.UART_RxFlowThresh = 120;
  137. cfg.UART_InverseMask = UART_None_Inverse;
  138. uart_init_uart0_console (&cfg, input_task);
  139. #ifndef NODE_DEBUG
  140. system_set_os_print(0);
  141. #endif
  142. // FIXME! Max supported RTOS stack size is 512 words (2k bytes), but
  143. // NodeMCU currently uses more than that. The game is on to find these
  144. // culprits, but this gcc doesn't do the -fstack-usage option :(
  145. xTaskCreate (
  146. nodemcu_main, "nodemcu", 1536, 0, configTIMER_TASK_PRIORITY +1, NULL);
  147. }