wm_main.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. /* c librayr mutex */
  53. tls_os_sem_t *libc_sem;
  54. /*----------------------------------------------------------------------------
  55. * Standard Library multithreading interface
  56. *---------------------------------------------------------------------------*/
  57. #ifndef __MICROLIB
  58. /*--------------------------- _mutex_initialize -----------------------------*/
  59. int _mutex_initialize (u32 *mutex)
  60. {
  61. /* Allocate and initialize a system mutex. */
  62. //tls_os_sem_create(&libc_sem, 1);
  63. //mutex = (u32 *)libc_sem;
  64. return(1);
  65. }
  66. /*--------------------------- _mutex_acquire --------------------------------*/
  67. void _mutex_acquire (u32 *mutex)
  68. {
  69. //u8 err;
  70. /* Acquire a system mutex, lock stdlib resources. */
  71. tls_os_sem_acquire(libc_sem, 0);
  72. }
  73. /*--------------------------- _mutex_release --------------------------------*/
  74. void _mutex_release (u32 *mutex)
  75. {
  76. /* Release a system mutex, unlock stdlib resources. */
  77. tls_os_sem_release(libc_sem);
  78. }
  79. #endif
  80. #define TASK_START_STK_SIZE 768 /* Size of each task's stacks (# of WORDs) */
  81. /*If you want to delete main task after it works, you can open this MACRO below*/
  82. #define MAIN_TASK_DELETE_AFTER_START_FTR 1
  83. u8 *TaskStartStk = NULL;
  84. tls_os_task_t tststarthdl = NULL;
  85. #define FW_MAJOR_VER 0x1
  86. #define FW_MINOR_VER 0x0
  87. #define FW_PATCH_VER 0x10
  88. const char FirmWareVer[4] =
  89. {
  90. 'v',
  91. FW_MAJOR_VER, /* Main version */
  92. FW_MINOR_VER, /* Subversion */
  93. FW_PATCH_VER /* Internal version */
  94. };
  95. const char HwVer[6] =
  96. {
  97. 'H',
  98. 0x1,
  99. 0x0,
  100. 0x0,
  101. 0x0,
  102. 0x0
  103. };
  104. extern const char WiFiVer[];
  105. extern u8 tx_gain_group[];
  106. extern void *tls_wl_init(u8 *tx_gain, u8 *mac_addr, u8 *hwver);
  107. extern int wpa_supplicant_init(u8 *mac_addr);
  108. extern void tls_sys_auto_mode_run(void);
  109. extern void UserMain(void);
  110. extern void tls_bt_entry();
  111. void tls_mem_get_init_available_size(void);
  112. void task_start (void *data);
  113. /****************/
  114. /* main program */
  115. /****************/
  116. void vApplicationIdleHook( void )
  117. {
  118. __WAIT();
  119. return;
  120. }
  121. void wm_gpio_config()
  122. {
  123. /* must call first */
  124. wm_gpio_af_disable();
  125. wm_uart0_tx_config(WM_IO_PB_19);
  126. wm_uart0_rx_config(WM_IO_PB_20);
  127. }
  128. #if MAIN_TASK_DELETE_AFTER_START_FTR
  129. void task_start_free()
  130. {
  131. if (TaskStartStk)
  132. {
  133. tls_mem_free(TaskStartStk);
  134. TaskStartStk = NULL;
  135. }
  136. }
  137. #endif
  138. #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
  139. uint32 power_bk_reg = 0;
  140. int main(void)
  141. {
  142. u32 value = 0;
  143. /*standby reason setting in here,because pmu irq will clear it.*/
  144. if ((tls_reg_read32(HR_PMU_INTERRUPT_SRC)>>7)&0x1)
  145. {
  146. tls_sys_set_reboot_reason(REBOOT_REASON_STANDBY);
  147. }
  148. /*32K switch to use RC circuit & calibration*/
  149. tls_pmu_clk_select(0);
  150. /*Switch to DBG*/
  151. value = tls_reg_read32(HR_PMU_BK_REG);
  152. power_bk_reg = value;
  153. value &= ~(BIT(19));
  154. tls_reg_write32(HR_PMU_BK_REG, value);
  155. /*32K switch to use RC circuit & calibration*/
  156. tls_pmu_clk_select(1);
  157. /*Switch to DBG*/
  158. value = tls_reg_read32(HR_PMU_PS_CR);
  159. value &= ~(BIT(5));
  160. tls_reg_write32(HR_PMU_PS_CR, value);
  161. /*Close those not initialized clk except touchsensor/trng, uart0,sdadc,gpio,rfcfg*/
  162. value = tls_reg_read32(HR_CLK_BASE_ADDR);
  163. value &= ~0x3fffff;
  164. #ifdef LUAT_USE_WLAN
  165. value |= 0x201a02;
  166. #else
  167. value |= 0x1a02;
  168. #endif
  169. tls_reg_write32(HR_CLK_BASE_ADDR, value);
  170. #ifndef LUAT_USE_WLAN
  171. /* Close bbp clk */
  172. tls_reg_write32(HR_CLK_BBP_CLT_CTRL, 0x0F);
  173. #endif
  174. tls_sys_clk_set(CPU_CLK_80M);
  175. tls_os_init(NULL);
  176. /* before use malloc() function, must create mutex used by c_lib */
  177. tls_os_sem_create(&libc_sem, 1);
  178. /*configure wake up source begin*/
  179. csi_vic_set_wakeup_irq(SDIO_IRQn);
  180. #ifdef LUAT_USE_WLAN
  181. csi_vic_set_wakeup_irq(MAC_IRQn);
  182. csi_vic_set_wakeup_irq(SEC_IRQn);
  183. #endif
  184. csi_vic_set_wakeup_irq(DMA_Channel0_IRQn);
  185. csi_vic_set_wakeup_irq(DMA_Channel1_IRQn);
  186. csi_vic_set_wakeup_irq(DMA_Channel2_IRQn);
  187. csi_vic_set_wakeup_irq(DMA_Channel3_IRQn);
  188. csi_vic_set_wakeup_irq(DMA_Channel4_7_IRQn);
  189. csi_vic_set_wakeup_irq(DMA_BRUST_IRQn);
  190. csi_vic_set_wakeup_irq(I2C_IRQn);
  191. csi_vic_set_wakeup_irq(ADC_IRQn);
  192. csi_vic_set_wakeup_irq(SPI_LS_IRQn);
  193. #ifdef LUAT_USE_WLAN
  194. csi_vic_set_wakeup_irq(SPI_HS_IRQn);
  195. #endif
  196. csi_vic_set_wakeup_irq(GPIOA_IRQn);
  197. csi_vic_set_wakeup_irq(GPIOB_IRQn);
  198. csi_vic_set_wakeup_irq(UART0_IRQn);
  199. csi_vic_set_wakeup_irq(UART1_IRQn);
  200. csi_vic_set_wakeup_irq(UART24_IRQn);
  201. // csi_vic_set_wakeup_irq(BLE_IRQn);
  202. // csi_vic_set_wakeup_irq(BT_IRQn);
  203. csi_vic_set_wakeup_irq(PWM_IRQn);
  204. csi_vic_set_wakeup_irq(I2S_IRQn);
  205. csi_vic_set_wakeup_irq(SIDO_HOST_IRQn);
  206. csi_vic_set_wakeup_irq(SYS_TICK_IRQn);
  207. // csi_vic_set_wakeup_irq(RSA_IRQn);
  208. csi_vic_set_wakeup_irq(CRYPTION_IRQn);
  209. csi_vic_set_wakeup_irq(PMU_IRQn);
  210. csi_vic_set_wakeup_irq(TIMER_IRQn);
  211. csi_vic_set_wakeup_irq(WDG_IRQn);
  212. /*should be here because main stack will be allocated and deallocated after task delete*/
  213. // tls_mem_get_init_available_size();
  214. /*configure wake up source end*/
  215. TaskStartStk = tls_mem_alloc(sizeof(u32)*TASK_START_STK_SIZE);
  216. if (TaskStartStk)
  217. {
  218. tls_os_task_create(&tststarthdl, NULL,
  219. task_start,
  220. (void *)0,
  221. (void *)TaskStartStk, /* 任务栈的起始地址 */
  222. TASK_START_STK_SIZE * sizeof(u32), /* 任务栈的大小 */
  223. 1,
  224. 0);
  225. tls_os_start_scheduler();
  226. }
  227. else
  228. {
  229. while(1);
  230. }
  231. return 0;
  232. }
  233. unsigned int tls_get_wifi_ver(void)
  234. {
  235. return (WiFiVer[0]<<16)|(WiFiVer[1]<<8)|WiFiVer[2];
  236. }
  237. unsigned int heap_size_max;
  238. unsigned int total_mem_size;
  239. void tls_mem_get_init_available_size(void)
  240. {
  241. u8 *p = NULL;
  242. total_mem_size = (unsigned int)&__heap_end - (unsigned int)&__heap_start;
  243. heap_size_max = total_mem_size;
  244. while(total_mem_size > 512)
  245. {
  246. p = malloc(total_mem_size);
  247. if (p)
  248. {
  249. free(p);
  250. p = NULL;
  251. break;
  252. }
  253. total_mem_size = total_mem_size - 512;
  254. }
  255. }
  256. void tls_pmu_chipsleep_callback(int sleeptime)
  257. {
  258. //wm_printf("c:%d\r\n", sleeptime);
  259. /*set wakeup time*/
  260. tls_pmu_timer1_start(sleeptime);
  261. /*enter chip sleep*/
  262. tls_pmu_sleep_start();
  263. }
  264. /*****************************************************************************
  265. * Function Name // task_start
  266. * Descriptor // before create multi_task, we create a task_start task
  267. * // in this example, this task display the cpu usage
  268. * Input
  269. * Output
  270. * Return
  271. ****************************************************************************/
  272. void task_start (void *data)
  273. {
  274. u8 enable = 0;
  275. u8 mac_addr[6] = {0x00, 0x25, 0x08, 0x09, 0x01, 0x0F};
  276. #if TLS_CONFIG_CRYSTAL_24M
  277. tls_wl_hw_using_24m_crystal();
  278. #endif
  279. /* must call first to configure gpio Alternate functions according the hardware design */
  280. wm_gpio_config();
  281. tls_irq_init();
  282. #if TLS_CONFIG_HARD_CRYPTO
  283. tls_crypto_init();
  284. #endif
  285. #if (TLS_CONFIG_LS_SPI)
  286. tls_spi_init();
  287. tls_spifls_init();
  288. #endif
  289. tls_fls_init();
  290. tls_fls_sys_param_postion_init();
  291. /*PARAM GAIN,MAC default*/
  292. #ifdef LUAT_USE_WLAN
  293. tls_ft_param_init();
  294. tls_param_load_factory_default();
  295. tls_param_init(); /*add param to init sysparam_lock sem*/
  296. tls_get_tx_gain(&tx_gain_group[0]);
  297. TLS_DBGPRT_INFO("tx gain ");
  298. TLS_DBGPRT_DUMP((char *)(&tx_gain_group[0]), 27);
  299. if (tls_wifi_mem_cfg(WIFI_MEM_START_ADDR, 7, 3)) /*wifi tx&rx mem customized interface*/
  300. {
  301. TLS_DBGPRT_INFO("wl mem initial failured\n");
  302. }
  303. tls_get_mac_addr(&mac_addr[0]);
  304. TLS_DBGPRT_INFO("mac addr ");
  305. TLS_DBGPRT_DUMP((char *)(&mac_addr[0]), 6);
  306. if(tls_wl_init(NULL, &mac_addr[0], NULL) == NULL)
  307. {
  308. TLS_DBGPRT_INFO("wl driver initial failured\n");
  309. }
  310. if (wpa_supplicant_init(mac_addr))
  311. {
  312. TLS_DBGPRT_INFO("supplicant initial failured\n");
  313. }
  314. /*wifi-temperature compensation,default:open*/
  315. tls_wifi_set_tempcomp_flag(0);
  316. tls_wifi_set_psm_chipsleep_flag(0);
  317. tls_wifi_psm_chipsleep_cb_register((tls_wifi_psm_chipsleep_callback)tls_pmu_chipsleep_callback, NULL, NULL);
  318. tls_ethernet_init();
  319. tls_sys_init();
  320. tls_param_get(TLS_PARAM_ID_PSM, &enable, TRUE);
  321. if (enable != TRUE)
  322. {
  323. enable = TRUE;
  324. tls_param_set(TLS_PARAM_ID_PSM, &enable, TRUE);
  325. }
  326. #endif
  327. UserMain();
  328. #ifdef LUAT_USE_WLAN
  329. tls_sys_auto_mode_run();
  330. #endif
  331. // for (;;)
  332. // {
  333. #if MAIN_TASK_DELETE_AFTER_START_FTR
  334. if (tststarthdl)
  335. {
  336. tls_os_task_del_by_task_handle(tststarthdl,task_start_free);
  337. // break;
  338. }
  339. // tls_os_time_delay(0x10000000);
  340. #else
  341. // //printf("start up\n");
  342. // extern void tls_os_disp_task_stat_info(void);
  343. // tls_os_disp_task_stat_info();
  344. // tls_os_time_delay(1000);
  345. #endif
  346. // }
  347. }