wm_main.c 8.0 KB

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