main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "wm_include.h"
  2. #include "wm_gpio_afsel.h"
  3. #include "wm_psram.h"
  4. #include "wm_internal_flash.h"
  5. #include "wm_rtc.h"
  6. #include "wm_osal.h"
  7. #include "wm_watchdog.h"
  8. #ifdef __LUATOS__
  9. #include "string.h"
  10. #include "luat_fs.h"
  11. #include "bget.h"
  12. #include "luat_base.h"
  13. #include "luat_msgbus.h"
  14. #include "luat_pm.h"
  15. #include "luat_rtc.h"
  16. #include <string.h>
  17. #include "wm_irq.h"
  18. #include "tls_sys.h"
  19. #include "wm_ram_config.h"
  20. #include "wm_internal_flash.h"
  21. #include "wm_psram.h"
  22. #include "wm_efuse.h"
  23. #include "wm_regs.h"
  24. #include "wm_wifi.h"
  25. #include "FreeRTOS.h"
  26. #define LUAT_LOG_TAG "main"
  27. #include "luat_log.h"
  28. void luat_heap_init(void);
  29. static void luat_start(void *sdata){
  30. luat_heap_init();
  31. luat_main();
  32. }
  33. #ifdef LUAT_USE_LVGL
  34. #include "lvgl.h"
  35. // static uint8_t lvgl_called = 0;
  36. static uint32_t lvgl_tick_cnt;
  37. static int luat_lvgl_cb(lua_State *L, void* ptr) {
  38. if (lvgl_tick_cnt) lvgl_tick_cnt--;
  39. lv_task_handler();
  40. // lvgl_called = 0;
  41. return 0;
  42. }
  43. static void lvgl_timer_cb(void *ptmr, void *parg) {
  44. // if (lvgl_called)
  45. // return;
  46. if (lvgl_tick_cnt < 10)
  47. {
  48. lvgl_tick_cnt++;
  49. rtos_msg_t msg = {0};
  50. msg.handler = luat_lvgl_cb;
  51. luat_msgbus_put(&msg, 0);
  52. }
  53. // lvgl_called = 1;
  54. }
  55. // #define LVGL_TASK_SIZE 512
  56. // static OS_STK __attribute__((aligned(4))) LVGLTaskStk[LVGL_TASK_SIZE] = {0};
  57. #endif
  58. #define TASK_START_STK_SIZE (3*1024) // 实际*4, 即12k
  59. static OS_STK __attribute__((aligned(4))) TaskStartStk[TASK_START_STK_SIZE] = {0};
  60. #endif
  61. // uint32_t rst_sta = 0;
  62. #ifdef __LUATOS__
  63. extern unsigned int TLS_FLASH_PARAM_DEFAULT ;
  64. extern unsigned int TLS_FLASH_PARAM1_ADDR ;
  65. extern unsigned int TLS_FLASH_PARAM2_ADDR ;
  66. extern unsigned int TLS_FLASH_PARAM_RESTORE_ADDR ;
  67. extern unsigned int TLS_FLASH_OTA_FLAG_ADDR ;
  68. extern unsigned int TLS_FLASH_END_ADDR ;
  69. #endif
  70. static void check_stack(void* ptr) {
  71. while (1) {
  72. vTaskDelay(1000);
  73. tls_os_disp_task_stat_info();
  74. }
  75. }
  76. static const const char* reason[] = {
  77. "power or reset",
  78. "by charge", // 不可能
  79. "wakeup by rtc",
  80. "reset by software",
  81. "unkown",
  82. "reset by key",
  83. "reboot by exception",
  84. "reboot by tool",
  85. "reset by watchdog",
  86. "reset by pad",
  87. "by charge" // 不可能
  88. };
  89. extern int luat_pm_get_poweron_reason(void);
  90. extern int power_bk_reg;
  91. #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
  92. void UserMain(void){
  93. char unique_id [18] = {0};
  94. tls_uart_options_t opt = {0};
  95. opt.baudrate = UART_BAUDRATE_B921600;
  96. opt.charlength = TLS_UART_CHSIZE_8BIT;
  97. opt.flow_ctrl = TLS_UART_FLOW_CTRL_NONE;
  98. opt.paritytype = TLS_UART_PMODE_DISABLED;
  99. opt.stopbits = TLS_UART_ONE_STOPBITS;
  100. tls_uart_port_init(0, &opt, 0);
  101. LLOGD("poweron: %s", reason[luat_pm_get_poweron_reason()]);
  102. // printf("Bit 8 -- %d\n", CHECK_BIT(power_bk_reg, 8));
  103. // printf("Bit 5 -- %d\n", CHECK_BIT(power_bk_reg, 5));
  104. // printf("Bit 2 -- %d\n", CHECK_BIT(power_bk_reg, 2));
  105. //printf("bsp reboot_reason %d\n", tls_sys_get_reboot_reason());
  106. struct tm tt = {0};
  107. luat_rtc_get(&tt);
  108. printf("main get %d-%d-%d %d:%d:%d\n", tt.tm_year + 1900, tt.tm_mon + 1, tt.tm_mday, tt.tm_hour, tt.tm_min, tt.tm_sec);
  109. // uint32_t rtc_ctrl1 = tls_reg_read32(HR_PMU_RTC_CTRL1);
  110. // printf("rtc_ctrl1 %ld\n", rtc_ctrl1);
  111. // 如果RTC计数少于1, 那肯定是第一次开机, 启动RTC并设置到1970年.
  112. // uint32_t rtc_ctrl2 = tls_reg_read32(HR_PMU_RTC_CTRL2);
  113. if (tt.tm_year == 0) {
  114. tt.tm_mday = 1;
  115. tt.tm_mon = 0;
  116. tt.tm_year = 71;
  117. luat_rtc_set(&tt);
  118. }
  119. else {
  120. // 只需要确保RTC启用
  121. int ctrl2 = tls_reg_read32(HR_PMU_RTC_CTRL2); /* enable */
  122. ctrl2 |= (1 << 16);
  123. tls_reg_write32(HR_PMU_RTC_CTRL2, ctrl2);
  124. }
  125. // 完全禁用jtag
  126. //u32 value = tls_reg_read32(HR_CLK_SEL_CTL);
  127. // printf("HR_CLK_SEL_CTL %08X\n", value);
  128. //value = value & 0x7FFF;
  129. // value = value & 0x7F00;
  130. //tls_reg_write32(HR_CLK_SEL_CTL, value);
  131. // tls_reg_read32(HR_CLK_SEL_CTL);
  132. // printf("HR_CLK_SEL_CTL %08X\n", value);
  133. // 读取开机原因
  134. // rst_sta = tls_reg_read32(HR_CLK_RST_STA);
  135. // tls_reg_write32(HR_CLK_RST_STA, 0xFF);
  136. #ifdef LUAT_USE_SHELL
  137. luat_shell_poweron(0);
  138. #endif
  139. #ifdef __LUATOS__
  140. extern void luat_mcu_tick64_init(void);
  141. luat_mcu_tick64_init();
  142. tls_fls_read_unique_id(unique_id);
  143. if (unique_id[1] == 0x10){
  144. printf("I/main auth ok %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X %s\n",
  145. unique_id[0], unique_id[1], unique_id[2], unique_id[3], unique_id[4],
  146. unique_id[5], unique_id[6], unique_id[7], unique_id[8], unique_id[9],
  147. unique_id[10], unique_id[11], unique_id[12], unique_id[13], unique_id[14],
  148. unique_id[15],unique_id[16],unique_id[17],
  149. luat_os_bsp());
  150. }else{
  151. printf("I/main auth ok %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X %s\n",
  152. unique_id[0], unique_id[1], unique_id[2], unique_id[3], unique_id[4],
  153. unique_id[5], unique_id[6], unique_id[7], unique_id[8], unique_id[9],
  154. luat_os_bsp());
  155. }
  156. #endif
  157. #ifdef AIR103
  158. TLS_FLASH_PARAM_DEFAULT = (0x80FB000UL);
  159. TLS_FLASH_PARAM1_ADDR = (0x80FC000UL);
  160. TLS_FLASH_PARAM2_ADDR = (0x80FD000UL);
  161. TLS_FLASH_PARAM_RESTORE_ADDR = (0x80FE000UL);
  162. TLS_FLASH_OTA_FLAG_ADDR = (0x80FF000UL);
  163. TLS_FLASH_END_ADDR = (0x80FFFFFUL);
  164. #endif
  165. #ifdef LUAT_USE_NIMBLE
  166. // TODO 注意, 除了启用LUAT_USE_NIMBTE外
  167. // 1. 修改FreeRTOSConfig.h的configTICK_RATE_HZ为500, 并重新make lib
  168. // 2. 若修改libblehost.a相关代码,需要手工复制bin目录下的文件,拷贝到lib目录.
  169. tls_ft_param_init();
  170. tls_param_load_factory_default();
  171. tls_param_init(); /*add param to init sysparam_lock sem*/
  172. // 读蓝牙mac, 如果是默认值,就根据unique_id读取
  173. uint8_t bt_mac[6];
  174. // 缺省mac C0:25:08:09:01:10
  175. uint8_t bt_default_mac[] = {0xC0,0x25,0x08,0x09,0x01,0x10};
  176. tls_get_bt_mac_addr(bt_mac);
  177. if (!memcmp(bt_mac, bt_default_mac, 6)) { // 看来是默认MAC, 那就改一下吧
  178. if (unique_id[1] == 0x10){
  179. memcpy(bt_mac, unique_id + 10, 6);
  180. }
  181. else {
  182. memcpy(bt_mac, unique_id + 2, 6);
  183. }
  184. tls_set_bt_mac_addr(bt_mac);
  185. }
  186. LLOGD("BLE_4.2 %02X:%02X:%02X:%02X:%02X:%02X", bt_mac[0], bt_mac[1], bt_mac[2], bt_mac[3], bt_mac[4], bt_mac[5]);
  187. #endif
  188. // 如要使用psram,启用以下代码,并重新编译sdk
  189. #ifdef LUAT_USE_PSRAM
  190. // 首先, 初始化psram相关引脚
  191. #ifndef LUAT_USE_PSRAM_PORT
  192. #ifdef AIR101
  193. // air101只能是0, 与SPI和UART3冲突, PB0~PB5
  194. #define LUAT_USE_PSRAM_PORT 0
  195. #else
  196. // air103可以是0或1
  197. // 1的话, PB2~PB5, PA15, PB27, 依然占用SPI0,但改用SPI1
  198. #define LUAT_USE_PSRAM_PORT 1
  199. #endif
  200. #endif
  201. printf("psram init\n");
  202. wm_psram_config(LUAT_USE_PSRAM_PORT);
  203. // 然后初始化psram的寄存器
  204. psram_init(PSRAM_QPI);
  205. //uint8_t* psram_ptr = (uint8_t*)(PSRAM_ADDR_START);
  206. #endif
  207. #ifdef __LUATOS__
  208. #ifdef LUAT_USE_LVGL
  209. lv_init();
  210. static tls_os_timer_t *os_timer = NULL;
  211. tls_os_timer_create(&os_timer, lvgl_timer_cb, NULL, 10/(1000 / configTICK_RATE_HZ), 1, NULL);
  212. tls_os_timer_start(os_timer);
  213. #endif
  214. tls_os_task_create(NULL, "luatos",
  215. luat_start,
  216. NULL,
  217. (void *)TaskStartStk, /* task's stack start address */
  218. TASK_START_STK_SIZE * sizeof(u32), /* task's stack size, unit:byte */
  219. 21,
  220. 0);
  221. // tls_os_task_create(NULL, "cstack", check_stack, NULL, NULL, 2048, 10, 0);
  222. #else
  223. printf("hello word\n");
  224. while (1);
  225. #endif
  226. }
  227. #ifndef __LUATOS__
  228. // void vApplicationTickHook( void ) {}
  229. void bpool(void *buffer, long len) {}
  230. #endif