wm_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*****************************************************************************
  2. *
  3. * File Name : wm_main.c
  4. *
  5. * Description: wm main
  6. *
  7. * Copyright (c) 2014 Winner Micro Electronic Design Co., Ltd.
  8. * All rights reserved.
  9. *
  10. * Author :
  11. *
  12. * Date : 2014-6-14
  13. *****************************************************************************/
  14. #include <string.h>
  15. #include "wm_irq.h"
  16. #include "tls_sys.h"
  17. #include "wm_regs.h"
  18. #include "wm_type_def.h"
  19. #include "wm_timer.h"
  20. #include "wm_irq.h"
  21. #include "wm_params.h"
  22. #include "wm_hostspi.h"
  23. #include "wm_flash.h"
  24. #include "wm_fls_gd25qxx.h"
  25. #include "wm_internal_flash.h"
  26. #include "wm_efuse.h"
  27. #include "wm_debug.h"
  28. #include "wm_netif.h"
  29. #include "wm_at_ri_init.h"
  30. #include "wm_config.h"
  31. #include "wm_osal.h"
  32. #include "wm_http_client.h"
  33. #include "wm_cpu.h"
  34. #include "wm_webserver.h"
  35. #include "wm_io.h"
  36. #include "wm_mem.h"
  37. #include "wm_wl_task.h"
  38. #include "wm_wl_timers.h"
  39. #ifdef TLS_CONFIG_HARD_CRYPTO
  40. #include "wm_crypto_hard.h"
  41. #endif
  42. #include "wm_gpio_afsel.h"
  43. #include "wm_pmu.h"
  44. #include "wm_ram_config.h"
  45. #include "wm_uart.h"
  46. #include "luat_conf_bsp.h"
  47. #include "wm_watchdog.h"
  48. #include "wm_wifi.h"
  49. #if TLS_CONFIG_ONLY_FACTORY_ATCMD
  50. #include "../../src/app/factorycmd/factory_atcmd.h"
  51. #endif
  52. #include "wm_flash_map.h"
  53. /* c librayr mutex */
  54. tls_os_sem_t *libc_sem;
  55. /*----------------------------------------------------------------------------
  56. * Standard Library multithreading interface
  57. *---------------------------------------------------------------------------*/
  58. #ifndef __MICROLIB
  59. /*--------------------------- _mutex_initialize -----------------------------*/
  60. int _mutex_initialize (u32 *mutex)
  61. {
  62. /* Allocate and initialize a system mutex. */
  63. //tls_os_sem_create(&libc_sem, 1);
  64. //mutex = (u32 *)libc_sem;
  65. return(1);
  66. }
  67. /*--------------------------- _mutex_acquire --------------------------------*/
  68. void _mutex_acquire (u32 *mutex)
  69. {
  70. //u8 err;
  71. /* Acquire a system mutex, lock stdlib resources. */
  72. tls_os_sem_acquire(libc_sem, 0);
  73. }
  74. /*--------------------------- _mutex_release --------------------------------*/
  75. void _mutex_release (u32 *mutex)
  76. {
  77. /* Release a system mutex, unlock stdlib resources. */
  78. tls_os_sem_release(libc_sem);
  79. }
  80. #endif
  81. #define TASK_START_STK_SIZE 768 /* Size of each task's stacks (# of WORDs) */
  82. /*If you want to delete main task after it works, you can open this MACRO below*/
  83. #define MAIN_TASK_DELETE_AFTER_START_FTR 1
  84. u8 *TaskStartStk = NULL;
  85. tls_os_task_t tststarthdl = NULL;
  86. #define FW_MAJOR_VER 0x1
  87. #define FW_MINOR_VER 0x0
  88. #define FW_PATCH_VER 0x10
  89. const char FirmWareVer[4] =
  90. {
  91. 'v',
  92. FW_MAJOR_VER, /* Main version */
  93. FW_MINOR_VER, /* Subversion */
  94. FW_PATCH_VER /* Internal version */
  95. };
  96. const char HwVer[6] =
  97. {
  98. 'H',
  99. 0x1,
  100. 0x0,
  101. 0x0,
  102. 0x0,
  103. 0x0
  104. };
  105. extern const char WiFiVer[];
  106. extern u8 tx_gain_group[];
  107. extern void *tls_wl_init(u8 *tx_gain, u8 *mac_addr, u8 *hwver);
  108. extern int wpa_supplicant_init(u8 *mac_addr);
  109. extern void tls_sys_auto_mode_run(void);
  110. extern void UserMain(void);
  111. extern void tls_bt_entry();
  112. void tls_mem_get_init_available_size(void);
  113. void task_start (void *data);
  114. /****************/
  115. /* main program */
  116. /****************/
  117. void vApplicationIdleHook( void )
  118. {
  119. __WAIT();
  120. return;
  121. }
  122. void wm_gpio_config()
  123. {
  124. /* must call first */
  125. wm_gpio_af_disable();
  126. wm_uart0_tx_config(WM_IO_PB_19);
  127. wm_uart0_rx_config(WM_IO_PB_20);
  128. }
  129. #if MAIN_TASK_DELETE_AFTER_START_FTR
  130. void task_start_free()
  131. {
  132. if (TaskStartStk)
  133. {
  134. tls_mem_free(TaskStartStk);
  135. TaskStartStk = NULL;
  136. }
  137. }
  138. #endif
  139. #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
  140. uint32 power_bk_reg = 0;
  141. int main(void)
  142. {
  143. u32 value = 0;
  144. /*standby reason setting in here,because pmu irq will clear it.*/
  145. if ((tls_reg_read32(HR_PMU_INTERRUPT_SRC)>>7)&0x1)
  146. {
  147. tls_sys_set_reboot_reason(REBOOT_REASON_STANDBY);
  148. }
  149. /*32K switch to use RC circuit & calibration*/
  150. tls_pmu_clk_select(0);
  151. /*Switch to DBG*/
  152. value = tls_reg_read32(HR_PMU_BK_REG);
  153. power_bk_reg = value;
  154. value &= ~(BIT(19));
  155. tls_reg_write32(HR_PMU_BK_REG, value);
  156. /*32K switch to use RC circuit & calibration*/
  157. tls_pmu_clk_select(1);
  158. /*Switch to DBG*/
  159. value = tls_reg_read32(HR_PMU_PS_CR);
  160. value &= ~(BIT(5));
  161. tls_reg_write32(HR_PMU_PS_CR, value);
  162. /*Close those not initialized clk except touchsensor/trng, uart0,sdadc,gpio,rfcfg*/
  163. value = tls_reg_read32(HR_CLK_BASE_ADDR);
  164. value &= ~0x3fffff;
  165. #ifdef LUAT_USE_WLAN
  166. value |= 0x201a02;
  167. #else
  168. value |= 0x1a02;
  169. #endif
  170. tls_reg_write32(HR_CLK_BASE_ADDR, value);
  171. #ifndef LUAT_USE_WLAN
  172. /* Close bbp clk */
  173. tls_reg_write32(HR_CLK_BBP_CLT_CTRL, 0x0F);
  174. #endif
  175. tls_sys_clk_set(CPU_CLK_80M);
  176. tls_os_init(NULL);
  177. /* before use malloc() function, must create mutex used by c_lib */
  178. tls_os_sem_create(&libc_sem, 1);
  179. /*configure wake up source begin*/
  180. csi_vic_set_wakeup_irq(SDIO_IRQn);
  181. #ifdef LUAT_USE_WLAN
  182. csi_vic_set_wakeup_irq(MAC_IRQn);
  183. csi_vic_set_wakeup_irq(SEC_IRQn);
  184. #endif
  185. csi_vic_set_wakeup_irq(DMA_Channel0_IRQn);
  186. csi_vic_set_wakeup_irq(DMA_Channel1_IRQn);
  187. csi_vic_set_wakeup_irq(DMA_Channel2_IRQn);
  188. csi_vic_set_wakeup_irq(DMA_Channel3_IRQn);
  189. csi_vic_set_wakeup_irq(DMA_Channel4_7_IRQn);
  190. csi_vic_set_wakeup_irq(DMA_BRUST_IRQn);
  191. csi_vic_set_wakeup_irq(I2C_IRQn);
  192. csi_vic_set_wakeup_irq(ADC_IRQn);
  193. csi_vic_set_wakeup_irq(SPI_LS_IRQn);
  194. #ifdef LUAT_USE_WLAN
  195. csi_vic_set_wakeup_irq(SPI_HS_IRQn);
  196. #endif
  197. csi_vic_set_wakeup_irq(GPIOA_IRQn);
  198. csi_vic_set_wakeup_irq(GPIOB_IRQn);
  199. csi_vic_set_wakeup_irq(UART0_IRQn);
  200. csi_vic_set_wakeup_irq(UART1_IRQn);
  201. csi_vic_set_wakeup_irq(UART24_IRQn);
  202. // csi_vic_set_wakeup_irq(BLE_IRQn);
  203. // csi_vic_set_wakeup_irq(BT_IRQn);
  204. csi_vic_set_wakeup_irq(PWM_IRQn);
  205. csi_vic_set_wakeup_irq(I2S_IRQn);
  206. csi_vic_set_wakeup_irq(SIDO_HOST_IRQn);
  207. csi_vic_set_wakeup_irq(SYS_TICK_IRQn);
  208. // csi_vic_set_wakeup_irq(RSA_IRQn);
  209. csi_vic_set_wakeup_irq(CRYPTION_IRQn);
  210. csi_vic_set_wakeup_irq(PMU_IRQn);
  211. csi_vic_set_wakeup_irq(TIMER_IRQn);
  212. csi_vic_set_wakeup_irq(WDG_IRQn);
  213. /*should be here because main stack will be allocated and deallocated after task delete*/
  214. tls_mem_get_init_available_size();
  215. /*configure wake up source end*/
  216. TaskStartStk = tls_mem_alloc(sizeof(u32)*TASK_START_STK_SIZE);
  217. if (TaskStartStk)
  218. {
  219. tls_os_task_create(&tststarthdl, NULL,
  220. task_start,
  221. (void *)0,
  222. (void *)TaskStartStk, /* 任务栈的起始地址 */
  223. TASK_START_STK_SIZE * sizeof(u32), /* 任务栈的大小 */
  224. 1,
  225. 0);
  226. tls_os_start_scheduler();
  227. }
  228. else
  229. {
  230. while(1);
  231. }
  232. return 0;
  233. }
  234. unsigned int tls_get_wifi_ver(void)
  235. {
  236. return (WiFiVer[0]<<16)|(WiFiVer[1]<<8)|WiFiVer[2];
  237. }
  238. unsigned int heap_size_max;
  239. unsigned int total_mem_size;
  240. void tls_mem_get_init_available_size(void)
  241. {
  242. u8 *p = NULL;
  243. total_mem_size = (unsigned int)&__heap_end - (unsigned int)&__heap_start;
  244. heap_size_max = total_mem_size;
  245. while(total_mem_size > 512)
  246. {
  247. p = malloc(total_mem_size);
  248. if (p)
  249. {
  250. free(p);
  251. p = NULL;
  252. break;
  253. }
  254. total_mem_size = total_mem_size - 512;
  255. }
  256. }
  257. void tls_pmu_chipsleep_callback(int sleeptime)
  258. {
  259. //wm_printf("c:%d\r\n", sleeptime);
  260. /*set wakeup time*/
  261. tls_pmu_timer1_start(sleeptime);
  262. /*enter chip sleep*/
  263. tls_pmu_sleep_start();
  264. }
  265. /*****************************************************************************
  266. * Function Name // task_start
  267. * Descriptor // before create multi_task, we create a task_start task
  268. * // in this example, this task display the cpu usage
  269. * Input
  270. * Output
  271. * Return
  272. ****************************************************************************/
  273. extern const u8 default_mac[];
  274. void task_start (void *data)
  275. {
  276. u8 enable = 0;
  277. u8 mac_addr[6] = {0x00, 0x25, 0x08, 0x09, 0x01, 0x0F};
  278. char unique_id [20] = {0};
  279. tls_fls_read_unique_id(unique_id);
  280. #if defined(AIR103) && defined(LUAT_USE_WLAN)
  281. // 判断实际flash大小, 为了正确读取系统擦拭能.
  282. // AIR101的固件总是2M FLASH, 脚本区直接在1M之后, 单纯这里判断没有意义, 因为info.json写了具体偏移量
  283. u8 magic[4] = {0};
  284. u8 test[] = {0xFF, 0xFF, 0xFF, 0xFF};
  285. int ret = tls_fls_read(TLS_FLASH_PARAM_DEFAULT, magic, 4);
  286. if (ret || memcmp(test, magic, 4) == 0) {
  287. // LLOGD("1M Flash");
  288. TLS_FLASH_PARAM_DEFAULT = (0x80FB000UL);
  289. TLS_FLASH_PARAM1_ADDR = (0x80FC000UL);
  290. TLS_FLASH_PARAM2_ADDR = (0x80FD000UL);
  291. TLS_FLASH_PARAM_RESTORE_ADDR = (0x80FE000UL);
  292. TLS_FLASH_OTA_FLAG_ADDR = (0x80FF000UL);
  293. TLS_FLASH_END_ADDR = (0x80FFFFFUL);
  294. }
  295. #endif
  296. #if TLS_CONFIG_CRYSTAL_24M
  297. tls_wl_hw_using_24m_crystal();
  298. #endif
  299. /* must call first to configure gpio Alternate functions according the hardware design */
  300. wm_gpio_config();
  301. tls_irq_init();
  302. #if TLS_CONFIG_HARD_CRYPTO
  303. tls_crypto_init();
  304. #endif
  305. #if (TLS_CONFIG_LS_SPI)
  306. tls_spi_init();
  307. tls_spifls_init();
  308. #endif
  309. tls_fls_init();
  310. tls_fls_sys_param_postion_init();
  311. #if defined(LUAT_USE_NIMBLE) || defined(LUAT_USE_WLAN)
  312. tls_ft_param_init();
  313. tls_param_load_factory_default();
  314. tls_param_init(); /*add param to init sysparam_lock sem*/
  315. #endif
  316. #ifdef LUAT_USE_NIMBLE
  317. // TODO 注意, 除了启用LUAT_USE_NIMBTE外
  318. // 1. 修改FreeRTOSConfig.h的configTICK_RATE_HZ为500, 并重新make lib
  319. // 2. 若修改libblehost.a相关代码,需要手工复制bin目录下的文件,拷贝到lib目录.
  320. // 读蓝牙mac, 如果是默认值,就根据unique_id读取
  321. uint8_t bt_mac[6];
  322. // 缺省mac C0:25:08:09:01:10
  323. uint8_t bt_default_mac[] = {0xC0,0x25,0x08,0x09,0x01,0x10};
  324. tls_get_bt_mac_addr(bt_mac);
  325. if (!memcmp(bt_mac, bt_default_mac, 6)) { // 看来是默认MAC, 那就改一下吧
  326. if (unique_id[1] == 0x10){
  327. memcpy(bt_mac, unique_id + 10, 6);
  328. }
  329. else {
  330. memcpy(bt_mac, unique_id + 2, 6);
  331. }
  332. tls_set_bt_mac_addr(bt_mac);
  333. }
  334. //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]);
  335. #endif
  336. /*PARAM GAIN,MAC default*/
  337. #ifdef LUAT_USE_WLAN
  338. tls_get_mac_addr(mac_addr);
  339. if (!memcmp(mac_addr, default_mac, 6)) { // 看来是默认MAC, 那就改一下吧
  340. if (unique_id[1] == 0x10){
  341. memcpy(mac_addr, unique_id + 12, 6);
  342. }
  343. else {
  344. memcpy(mac_addr, unique_id + 4, 6);
  345. }
  346. tls_set_mac_addr(mac_addr);
  347. }
  348. //printf("WIFI %02X:%02X:%02X:%02X:%02X:%02X\n", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  349. tls_get_tx_gain(&tx_gain_group[0]);
  350. TLS_DBGPRT_INFO("tx gain ");
  351. TLS_DBGPRT_DUMP((char *)(&tx_gain_group[0]), 27);
  352. if (tls_wifi_mem_cfg(WIFI_MEM_START_ADDR, 7, 7)) /*wifi tx&rx mem customized interface*/
  353. {
  354. TLS_DBGPRT_INFO("wl mem initial failured\n");
  355. }
  356. // tls_get_mac_addr(&mac_addr[0]);
  357. TLS_DBGPRT_INFO("mac addr ");
  358. TLS_DBGPRT_DUMP((char *)(&mac_addr[0]), 6);
  359. if(tls_wl_init(NULL, &mac_addr[0], NULL) == NULL)
  360. {
  361. TLS_DBGPRT_INFO("wl driver initial failured\n");
  362. }
  363. if (wpa_supplicant_init(mac_addr))
  364. {
  365. TLS_DBGPRT_INFO("supplicant initial failured\n");
  366. }
  367. /*wifi-temperature compensation,default:open*/
  368. if (tls_wifi_get_tempcomp_flag() != 0)
  369. tls_wifi_set_tempcomp_flag(0);
  370. if (tls_wifi_get_psm_chipsleep_flag() != 0)
  371. tls_wifi_set_psm_chipsleep_flag(0);
  372. tls_wifi_psm_chipsleep_cb_register((tls_wifi_psm_chipsleep_callback)tls_pmu_chipsleep_callback, NULL, NULL);
  373. tls_ethernet_init();
  374. tls_sys_init();
  375. tls_param_get(TLS_PARAM_ID_PSM, &enable, TRUE);
  376. if (enable != FALSE)
  377. {
  378. enable = FALSE;
  379. tls_param_set(TLS_PARAM_ID_PSM, &enable, TRUE);
  380. }
  381. #endif
  382. UserMain();
  383. #ifdef LUAT_USE_WLAN
  384. //tls_sys_auto_mode_run();
  385. #endif
  386. // for (;;)
  387. // {
  388. #if MAIN_TASK_DELETE_AFTER_START_FTR
  389. if (tststarthdl)
  390. {
  391. tls_os_task_del_by_task_handle(tststarthdl,task_start_free);
  392. // break;
  393. }
  394. // tls_os_time_delay(0x10000000);
  395. #else
  396. // //printf("start up\n");
  397. // extern void tls_os_disp_task_stat_info(void);
  398. // tls_os_disp_task_stat_info();
  399. // tls_os_time_delay(1000);
  400. #endif
  401. // }
  402. }