main.c 7.1 KB

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