wm_main.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 "wm_io.h"
  47. #include "wm_gpio.h"
  48. // #define SYS_MEM_DEBUG 0
  49. /* c librayr mutex */
  50. tls_os_sem_t *libc_sem;
  51. /*----------------------------------------------------------------------------
  52. * Standard Library multithreading interface
  53. *---------------------------------------------------------------------------*/
  54. #ifndef __MICROLIB
  55. /*--------------------------- _mutex_initialize -----------------------------*/
  56. int _mutex_initialize (u32 *mutex)
  57. {
  58. /* Allocate and initialize a system mutex. */
  59. //tls_os_sem_create(&libc_sem, 1);
  60. //mutex = (u32 *)libc_sem;
  61. return(1);
  62. }
  63. /*--------------------------- _mutex_acquire --------------------------------*/
  64. void _mutex_acquire (u32 *mutex)
  65. {
  66. //u8 err;
  67. /* Acquire a system mutex, lock stdlib resources. */
  68. tls_os_sem_acquire(libc_sem, 0);
  69. }
  70. /*--------------------------- _mutex_release --------------------------------*/
  71. void _mutex_release (u32 *mutex)
  72. {
  73. /* Release a system mutex, unlock stdlib resources. */
  74. tls_os_sem_release(libc_sem);
  75. }
  76. #endif
  77. #define TASK_START_STK_SIZE 640 /* Size of each task's stacks (# of WORDs) */
  78. /*If you want to delete main task after it works, you can open this MACRO below*/
  79. #define MAIN_TASK_DELETE_AFTER_START_FTR 1
  80. u8 *TaskStartStk = NULL;
  81. tls_os_task_t tststarthdl = NULL;
  82. #define FW_MAJOR_VER 0x1
  83. #define FW_MINOR_VER 0x0
  84. #define FW_PATCH_VER 0x2
  85. const char FirmWareVer[4] =
  86. {
  87. 'v',
  88. FW_MAJOR_VER, /* Main version */
  89. FW_MINOR_VER, /* Subversion */
  90. FW_PATCH_VER /* Internal version */
  91. };
  92. const char HwVer[6] =
  93. {
  94. 'H',
  95. 0x1,
  96. 0x0,
  97. 0x0,
  98. 0x0,
  99. 0x0
  100. };
  101. extern const char WiFiVer[];
  102. extern u8 tx_gain_group[];
  103. extern void *tls_wl_init(u8 *tx_gain, u8 *mac_addr, u8 *hwver);
  104. extern int wpa_supplicant_init(u8 *mac_addr);
  105. extern void tls_sys_auto_mode_run(void);
  106. extern void UserMain(void);
  107. extern void tls_bt_entry();
  108. void task_start (void *data);
  109. /****************/
  110. /* main program */
  111. /****************/
  112. void vApplicationIdleHook( void )
  113. {
  114. __WAIT();
  115. return;
  116. }
  117. void wm_gpio_config()
  118. {
  119. /* must call first */
  120. wm_gpio_af_disable();
  121. wm_uart0_tx_config(WM_IO_PB_19);
  122. wm_uart0_rx_config(WM_IO_PB_20);
  123. //wm_uart1_rx_config(WM_IO_PB_07);
  124. //wm_uart1_tx_config(WM_IO_PB_06);
  125. #if (0)
  126. wm_spi_ck_config(WM_IO_PB_01);
  127. wm_spi_cs_config(WM_IO_PB_04);
  128. wm_spi_do_config(WM_IO_PB_05);
  129. wm_spi_di_config(WM_IO_PB_00);
  130. #endif
  131. }
  132. #if MAIN_TASK_DELETE_AFTER_START_FTR
  133. void task_start_free()
  134. {
  135. if (TaskStartStk)
  136. {
  137. tls_mem_free(TaskStartStk);
  138. TaskStartStk = NULL;
  139. }
  140. }
  141. #endif
  142. // int main(void)
  143. // {
  144. // tls_sys_clk_set(CPU_CLK_80M);
  145. // while (1);
  146. // }
  147. int main(void)
  148. {
  149. u32 value = 0;
  150. /*32K switch to use RC circuit & calibration*/
  151. tls_pmu_clk_select(1);
  152. /*Switch to DBG*/
  153. value = tls_reg_read32(HR_PMU_BK_REG);
  154. value &= ~(BIT(19));
  155. tls_reg_write32(HR_PMU_BK_REG, value);
  156. value = tls_reg_read32(HR_PMU_PS_CR);
  157. value &= ~(BIT(5));
  158. tls_reg_write32(HR_PMU_PS_CR, value);
  159. //value = tls_reg_read32(HR_PMU_WLAN_STTS);
  160. //value &= ~(0xE);
  161. //tls_reg_write32(HR_PMU_WLAN_STTS, value);
  162. /*Close those not initialized clk except uart0,sdadc,gpio,rfcfg*/
  163. value = tls_reg_read32(HR_CLK_BASE_ADDR);
  164. value &= ~0x3fffff;
  165. value |= 0x1a02;
  166. tls_reg_write32(HR_CLK_BASE_ADDR, value);
  167. /* Close bbp clk */
  168. tls_reg_write32(HR_CLK_BBP_CLT_CTRL, 0x0F);
  169. tls_sys_clk_set(CPU_CLK_80M);
  170. tls_os_init(NULL);
  171. /* before use malloc() function, must create mutex used by c_lib */
  172. tls_os_sem_create(&libc_sem, 1);
  173. /*configure wake up source begin*/
  174. csi_vic_set_wakeup_irq(SDIO_IRQn);
  175. // csi_vic_set_wakeup_irq(MAC_IRQn);
  176. // csi_vic_set_wakeup_irq(SEC_IRQn);
  177. csi_vic_set_wakeup_irq(DMA_Channel0_IRQn);
  178. csi_vic_set_wakeup_irq(DMA_Channel1_IRQn);
  179. csi_vic_set_wakeup_irq(DMA_Channel2_IRQn);
  180. csi_vic_set_wakeup_irq(DMA_Channel3_IRQn);
  181. csi_vic_set_wakeup_irq(DMA_Channel4_7_IRQn);
  182. csi_vic_set_wakeup_irq(DMA_BRUST_IRQn);
  183. csi_vic_set_wakeup_irq(I2C_IRQn);
  184. csi_vic_set_wakeup_irq(ADC_IRQn);
  185. csi_vic_set_wakeup_irq(SPI_LS_IRQn);
  186. // csi_vic_set_wakeup_irq(SPI_HS_IRQn);
  187. csi_vic_set_wakeup_irq(GPIOA_IRQn);
  188. csi_vic_set_wakeup_irq(GPIOB_IRQn);
  189. csi_vic_set_wakeup_irq(UART0_IRQn);
  190. csi_vic_set_wakeup_irq(UART1_IRQn);
  191. csi_vic_set_wakeup_irq(UART24_IRQn);
  192. // csi_vic_set_wakeup_irq(BLE_IRQn);
  193. // csi_vic_set_wakeup_irq(BT_IRQn);
  194. csi_vic_set_wakeup_irq(PWM_IRQn);
  195. csi_vic_set_wakeup_irq(I2S_IRQn);
  196. csi_vic_set_wakeup_irq(SIDO_HOST_IRQn);
  197. csi_vic_set_wakeup_irq(SYS_TICK_IRQn);
  198. // csi_vic_set_wakeup_irq(RSA_IRQn);
  199. csi_vic_set_wakeup_irq(CRYPTION_IRQn);
  200. csi_vic_set_wakeup_irq(PMU_IRQn);
  201. csi_vic_set_wakeup_irq(TIMER_IRQn);
  202. csi_vic_set_wakeup_irq(WDG_IRQn);
  203. /*configure wake up source end*/
  204. TaskStartStk = tls_mem_alloc(sizeof(u32)*TASK_START_STK_SIZE);
  205. if (TaskStartStk)
  206. {
  207. tls_os_task_create(&tststarthdl, NULL,
  208. task_start,
  209. (void *)0,
  210. (void *)TaskStartStk, /* 任务栈的起始地址 */
  211. TASK_START_STK_SIZE * sizeof(u32), /* 任务栈的大小 */
  212. 1,
  213. 0);
  214. tls_os_start_scheduler();
  215. }
  216. else
  217. {
  218. while(1);
  219. }
  220. return 0;
  221. }
  222. unsigned int tls_get_wifi_ver(void)
  223. {
  224. return (WiFiVer[0]<<16)|(WiFiVer[1]<<8)|WiFiVer[2];
  225. }
  226. unsigned int total_mem_size;
  227. void tls_mem_get_init_available_size(void)
  228. {
  229. u8 *p = NULL;
  230. total_mem_size = (unsigned int)&__heap_end - (unsigned int)&__heap_start;
  231. while(total_mem_size > 512)
  232. {
  233. p = malloc(total_mem_size);
  234. if (p)
  235. {
  236. free(p);
  237. p = NULL;
  238. break;
  239. }
  240. total_mem_size = total_mem_size - 512;
  241. }
  242. }
  243. /*****************************************************************************
  244. * Function Name // task_start
  245. * Descriptor // before create multi_task, we create a task_start task
  246. * // in this example, this task display the cpu usage
  247. * Input
  248. * Output
  249. * Return
  250. ****************************************************************************/
  251. void task_start (void *data)
  252. {
  253. //u8 enable = 0;
  254. //u8 mac_addr[6] = {0x00, 0x25, 0x08, 0x09, 0x01, 0x0F};
  255. #if TLS_CONFIG_CRYSTAL_24M
  256. tls_wl_hw_using_24m_crystal();
  257. #endif
  258. //tls_mem_get_init_available_size();
  259. /* must call first to configure gpio Alternate functions according the hardware design */
  260. wm_gpio_config();
  261. tls_irq_init();
  262. #if TLS_CONFIG_HARD_CRYPTO
  263. tls_crypto_init();
  264. #endif
  265. #if (TLS_CONFIG_LS_SPI)
  266. tls_spi_init();
  267. tls_spifls_init();
  268. #endif
  269. tls_fls_init();
  270. tls_fls_sys_param_postion_init();
  271. /*PARAM GAIN,MAC default*/
  272. //tls_ft_param_init();
  273. //tls_crypto_init();
  274. // 仅调试内存大小时使用
  275. #if 0
  276. tls_mem_get_init_available_size();
  277. printf("sys memory max %d kb\n", total_mem_size / 1024);
  278. printf("heap memory max %d kb\n", ((unsigned int)&__heap_end - (unsigned int)&__heap_start) / 1024);
  279. #endif
  280. UserMain();
  281. // for (;;)
  282. // {
  283. #if MAIN_TASK_DELETE_AFTER_START_FTR
  284. if (tststarthdl)
  285. {
  286. tls_os_task_del_by_task_handle(tststarthdl,task_start_free);
  287. // break;
  288. }
  289. #endif
  290. // #if 1
  291. // tls_os_time_delay(0x10000000);
  292. // #else
  293. // //printf("start up\n");
  294. // extern void tls_os_disp_task_stat_info(void);
  295. // tls_os_disp_task_stat_info();
  296. // tls_os_time_delay(1000);
  297. // #endif
  298. // }
  299. }