main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "wm_include.h"
  2. #include "wm_gpio_afsel.h"
  3. #include "wm_psram.h"
  4. #include "wm_internal_flash.h"
  5. #ifdef __LUATOS__
  6. #include "string.h"
  7. #include "luat_fs.h"
  8. #include "bget.h"
  9. #include "luat_base.h"
  10. #include "luat_msgbus.h"
  11. #include "luat_pm.h"
  12. #include <string.h>
  13. #include "wm_irq.h"
  14. #include "tls_sys.h"
  15. #include "wm_ram_config.h"
  16. #include "wm_internal_flash.h"
  17. #include "wm_psram.h"
  18. #include "wm_efuse.h"
  19. #include "FreeRTOS.h"
  20. #define LUAT_LOG_TAG "main"
  21. #include "luat_log.h"
  22. #ifndef LUAT_HEAP_SIZE
  23. #ifdef LUAT_USE_NIMBLE
  24. #define LUAT_HEAP_SIZE (128+16)*1024
  25. #else
  26. #define LUAT_HEAP_SIZE (128+48)*1024
  27. #endif
  28. #endif
  29. static void luat_start(void *sdata){
  30. // 毕竟sram还是快很多的, 优先psram吧
  31. bpool((void*)0x20028000, 128*1024);
  32. #ifdef LUAT_USE_PSRAM
  33. char test[] = {0xAA, 0xBB, 0xCC, 0xDD};
  34. char* psram_ptr = (void*)0x30000000;
  35. memcpy(psram_ptr, test, 4);
  36. if (memcmp(psram_ptr, test, 4)) {
  37. LLOGW("psram is enable, but can't access!!");
  38. }
  39. else {
  40. memset(psram_ptr, 0, 1024);
  41. // 存在psram, 加入到内存次, 就不使用系统额外的内存了.
  42. bpool(psram_ptr, 4*1024*1024); // 如果是8M内存, 改成 8也可以.
  43. luat_main();
  44. return;
  45. }
  46. #else
  47. #if (LUAT_HEAP_SIZE > 128*1024)
  48. char* heap_ext = malloc(LUAT_HEAP_SIZE - 128*1024);
  49. if (heap_ext) {
  50. bpool((void*)heap_ext, LUAT_HEAP_SIZE - 128*1024);
  51. }
  52. #endif
  53. #endif
  54. luat_main();
  55. }
  56. #ifdef LUAT_USE_LVGL
  57. #include "lvgl.h"
  58. // static uint8_t lvgl_called = 0;
  59. static uint32_t lvgl_tick_cnt;
  60. static int luat_lvgl_cb(lua_State *L, void* ptr) {
  61. if (lvgl_tick_cnt) lvgl_tick_cnt--;
  62. lv_task_handler();
  63. // lvgl_called = 0;
  64. return 0;
  65. }
  66. static void lvgl_timer_cb(void *ptmr, void *parg) {
  67. // if (lvgl_called)
  68. // return;
  69. if (lvgl_tick_cnt < 10)
  70. {
  71. lvgl_tick_cnt++;
  72. rtos_msg_t msg = {0};
  73. msg.handler = luat_lvgl_cb;
  74. luat_msgbus_put(&msg, 0);
  75. }
  76. // lvgl_called = 1;
  77. }
  78. // #define LVGL_TASK_SIZE 512
  79. // static OS_STK __attribute__((aligned(4))) LVGLTaskStk[LVGL_TASK_SIZE] = {0};
  80. #endif
  81. #define TASK_START_STK_SIZE 4096
  82. static OS_STK __attribute__((aligned(4))) TaskStartStk[TASK_START_STK_SIZE] = {0};
  83. #endif
  84. uint32_t rst_sta = 0;
  85. #ifdef __LUATOS__
  86. extern unsigned int TLS_FLASH_PARAM_DEFAULT ;
  87. extern unsigned int TLS_FLASH_PARAM1_ADDR ;
  88. extern unsigned int TLS_FLASH_PARAM2_ADDR ;
  89. extern unsigned int TLS_FLASH_PARAM_RESTORE_ADDR ;
  90. extern unsigned int TLS_FLASH_OTA_FLAG_ADDR ;
  91. extern unsigned int TLS_FLASH_END_ADDR ;
  92. #endif
  93. #include "luat_rtos.h"
  94. #include "luat_server.h"
  95. // static tls_os_queue_t *shell_queue = NULL;
  96. // luat_rtos_queue_t queue = {
  97. // .userdata = shell_queue,
  98. // };
  99. // static luat_rtos_queue_t queue;
  100. // static void prvnes_Task(void* params){
  101. // int* tag;
  102. // while (1)
  103. // {
  104. // // ret = tls_os_queue_receive(shell_queue, (void **) &tag, 0, 0);
  105. // int ret = luat_queue_recv(&queue, tag, 0, 0);
  106. // printf("this is a test task!!!!\r\n");
  107. // tls_os_time_delay(1000);
  108. // }
  109. // }
  110. void UserMain(void){
  111. char unique_id [18] = {0};
  112. tls_uart_options_t opt;
  113. opt.baudrate = UART_BAUDRATE_B921600;
  114. opt.charlength = TLS_UART_CHSIZE_8BIT;
  115. opt.flow_ctrl = TLS_UART_FLOW_CTRL_NONE;
  116. opt.paritytype = TLS_UART_PMODE_DISABLED;
  117. opt.stopbits = TLS_UART_ONE_STOPBITS;
  118. tls_uart_port_init(0, &opt, 0);
  119. // 读取开机原因
  120. rst_sta = tls_reg_read32(HR_CLK_RST_STA);
  121. tls_reg_write32(HR_CLK_RST_STA, 0xFF);
  122. // luat_queue_create(&queue, 256, 0);
  123. // luat_thread_t nes_thread = {
  124. // .name = "nes",
  125. // .priority = 1,
  126. // .stack_size = 16*1024,
  127. // .entry = prvnes_Task,
  128. // };
  129. // luat_thread_start(&nes_thread);
  130. #ifdef LUAT_USE_SHELL
  131. luat_shell_poweron(0);
  132. #endif
  133. #ifdef __LUATOS__
  134. tls_fls_read_unique_id(unique_id);
  135. if (unique_id[1] == 0x10){
  136. printf("I/main auth ok %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X %s\n",
  137. unique_id[0], unique_id[1], unique_id[2], unique_id[3], unique_id[4],
  138. unique_id[5], unique_id[6], unique_id[7], unique_id[8], unique_id[9],
  139. unique_id[10], unique_id[11], unique_id[12], unique_id[13], unique_id[14],unique_id[15],
  140. luat_os_bsp());
  141. }else{
  142. printf("I/main auth ok %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X %s\n",
  143. unique_id[0], unique_id[1], unique_id[2], unique_id[3], unique_id[4],
  144. unique_id[5], unique_id[6], unique_id[7], unique_id[8], unique_id[9],
  145. luat_os_bsp());
  146. }
  147. #endif
  148. #ifdef AIR103
  149. TLS_FLASH_PARAM_DEFAULT = (0x80FB000UL);
  150. TLS_FLASH_PARAM1_ADDR = (0x80FC000UL);
  151. TLS_FLASH_PARAM2_ADDR = (0x80FD000UL);
  152. TLS_FLASH_PARAM_RESTORE_ADDR = (0x80FE000UL);
  153. TLS_FLASH_OTA_FLAG_ADDR = (0x80FF000UL);
  154. TLS_FLASH_END_ADDR = (0x80FFFFFUL);
  155. #endif
  156. #ifdef LUAT_USE_NIMBLE
  157. // TODO 注意, 除了启用LUAT_USE_NIMBTE外
  158. // 1. 修改FreeRTOSConfig.h的configTICK_RATE_HZ为500, 并重新make lib
  159. // 2. 若修改libblehost.a相关代码,需要手工复制bin目录下的文件,拷贝到lib目录.
  160. tls_ft_param_init();
  161. tls_param_load_factory_default();
  162. tls_param_init(); /*add param to init sysparam_lock sem*/
  163. // 读蓝牙mac, 如果是默认值,就根据unique_id读取
  164. uint8_t bt_mac[6];
  165. // 缺省mac C0:25:08:09:01:10
  166. uint8_t bt_default_mac[] = {0xC0,0x25,0x08,0x09,0x01,0x10};
  167. tls_get_bt_mac_addr(bt_mac);
  168. if (!memcmp(bt_mac, bt_default_mac, 6)) { // 看来是默认MAC, 那就改一下吧
  169. if (unique_id[1] == 0x10){
  170. memcpy(bt_mac, unique_id + 10, 6);
  171. }
  172. else {
  173. memcpy(bt_mac, unique_id + 2, 6);
  174. }
  175. tls_set_bt_mac_addr(bt_mac);
  176. }
  177. 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]);
  178. #endif
  179. // 如要使用psram,启用以下代码,并重新编译sdk
  180. #ifdef LUAT_USE_PSRAM
  181. // 首先, 初始化psram相关引脚
  182. #ifndef LUAT_USE_PSRAM_PORT
  183. #ifdef AIR101
  184. // air101只能是0, 与SPI和UART3冲突, PB0~PB5
  185. #define LUAT_USE_PSRAM_PORT 0
  186. #else
  187. // air103可以是0或1
  188. // 1的话, PB2~PB5, PA15, PB27, 依然占用SPI0,但改用SPI1
  189. #define LUAT_USE_PSRAM_PORT 1
  190. #endif
  191. #endif
  192. wm_psram_config(LUAT_USE_PSRAM_PORT);
  193. // 然后初始化psram的寄存器
  194. psram_init(PSRAM_QPI);
  195. //uint8_t* psram_ptr = (uint8_t*)(PSRAM_ADDR_START);
  196. #endif
  197. #ifdef __LUATOS__
  198. #ifdef LUAT_USE_LVGL
  199. lv_init();
  200. static tls_os_timer_t *os_timer = NULL;
  201. tls_os_timer_create(&os_timer, lvgl_timer_cb, NULL, 10/(1000 / configTICK_RATE_HZ), 1, NULL);
  202. tls_os_timer_start(os_timer);
  203. #endif
  204. // int a = 1;
  205. // while (a++)
  206. // {
  207. // // tls_os_queue_send(shell_queue, (void *)user_data, 0);
  208. // luat_queue_send(&queue, a, 0, 0);
  209. // tls_os_time_delay(1000);
  210. // }
  211. luat_server_start();
  212. tls_os_task_create(NULL, NULL,
  213. luat_start,
  214. NULL,
  215. (void *)TaskStartStk, /* task's stack start address */
  216. TASK_START_STK_SIZE * sizeof(u32), /* task's stack size, unit:byte */
  217. 31,
  218. 0);
  219. #else
  220. printf("hello word\n");
  221. while (1);
  222. #endif
  223. }
  224. #ifndef __LUATOS__
  225. // void vApplicationTickHook( void ) {}
  226. void bpool(void *buffer, long len) {}
  227. #endif