wm_main.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #if defined(LUAT_USE_WLAN) || defined(LUAT_USE_NIMBLE) || defined(LUAT_USE_RTC)
  166. value |= 0x201a02;
  167. #else
  168. value |= 0x1a02;
  169. #endif
  170. tls_reg_write32(HR_CLK_BASE_ADDR, value);
  171. #if !defined(LUAT_USE_WLAN) && !defined(LUAT_USE_NIMBLE)
  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. #if (defined(LUAT_USE_WLAN) || defined(LUAT_USE_NIMBLE))
  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. csi_vic_set_wakeup_irq(SPI_HS_IRQn);
  195. csi_vic_set_wakeup_irq(GPIOA_IRQn);
  196. csi_vic_set_wakeup_irq(GPIOB_IRQn);
  197. csi_vic_set_wakeup_irq(UART0_IRQn);
  198. csi_vic_set_wakeup_irq(UART1_IRQn);
  199. csi_vic_set_wakeup_irq(UART24_IRQn);
  200. #if (defined(LUAT_USE_NIMBLE))
  201. csi_vic_set_wakeup_irq(BLE_IRQn);
  202. csi_vic_set_wakeup_irq(BT_IRQn);
  203. #endif
  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. #if (defined(LUAT_USE_WLAN) || defined(LUAT_USE_RSA))
  209. csi_vic_set_wakeup_irq(RSA_IRQn);
  210. #endif
  211. csi_vic_set_wakeup_irq(CRYPTION_IRQn);
  212. csi_vic_set_wakeup_irq(PMU_IRQn);
  213. csi_vic_set_wakeup_irq(TIMER_IRQn);
  214. csi_vic_set_wakeup_irq(WDG_IRQn);
  215. /*should be here because main stack will be allocated and deallocated after task delete*/
  216. tls_mem_get_init_available_size();
  217. /*configure wake up source end*/
  218. TaskStartStk = tls_mem_alloc(sizeof(u32)*TASK_START_STK_SIZE);
  219. if (TaskStartStk)
  220. {
  221. tls_os_task_create(&tststarthdl, NULL,
  222. task_start,
  223. (void *)0,
  224. (void *)TaskStartStk, /* 任务栈的起始地址 */
  225. TASK_START_STK_SIZE * sizeof(u32), /* 任务栈的大小 */
  226. 1,
  227. 0);
  228. tls_os_start_scheduler();
  229. }
  230. else
  231. {
  232. while(1);
  233. }
  234. return 0;
  235. }
  236. unsigned int tls_get_wifi_ver(void)
  237. {
  238. return (WiFiVer[0]<<16)|(WiFiVer[1]<<8)|WiFiVer[2];
  239. }
  240. unsigned int heap_size_max;
  241. unsigned int total_mem_size;
  242. unsigned int min_free_size;
  243. void tls_mem_get_init_available_size(void)
  244. {
  245. u8 *p = NULL;
  246. total_mem_size = (unsigned int)&__heap_end - (unsigned int)&__heap_start;
  247. heap_size_max = total_mem_size;
  248. while(total_mem_size > 512)
  249. {
  250. p = malloc(total_mem_size);
  251. if (p)
  252. {
  253. free(p);
  254. p = NULL;
  255. break;
  256. }
  257. total_mem_size = total_mem_size - 512;
  258. }
  259. min_free_size = total_mem_size;
  260. }
  261. void tls_pmu_chipsleep_callback(int sleeptime)
  262. {
  263. //wm_printf("c:%d\r\n", sleeptime);
  264. /*set wakeup time*/
  265. tls_pmu_timer1_start(sleeptime);
  266. /*enter chip sleep*/
  267. tls_pmu_sleep_start();
  268. }
  269. /*****************************************************************************
  270. * Function Name // task_start
  271. * Descriptor // before create multi_task, we create a task_start task
  272. * // in this example, this task display the cpu usage
  273. * Input
  274. * Output
  275. * Return
  276. ****************************************************************************/
  277. void task_start (void *data)
  278. {
  279. u8 enable = 0;
  280. u8 mac_addr[6];
  281. #if TLS_CONFIG_CRYSTAL_24M
  282. tls_wl_hw_using_24m_crystal();
  283. #endif
  284. /* must call first to configure gpio Alternate functions according the hardware design */
  285. wm_gpio_config();
  286. tls_irq_init();
  287. #if TLS_CONFIG_HARD_CRYPTO
  288. tls_crypto_init();
  289. #endif
  290. #if (TLS_CONFIG_LS_SPI)
  291. tls_spi_init();
  292. //tls_spifls_init();
  293. #endif
  294. tls_fls_init();
  295. tls_fls_sys_param_postion_init();
  296. #ifdef LUAT_CONF_LOG_UART1
  297. luat_log_set_uart_port(1);
  298. #endif
  299. /*PARAM GAIN,MAC default*/
  300. #if defined(LUAT_USE_NIMBLE) || defined(LUAT_USE_WLAN)
  301. tls_ft_param_init();
  302. tls_param_load_factory_default();
  303. tls_param_init(); /*add param to init sysparam_lock sem*/
  304. extern void sys_mac_init();
  305. sys_mac_init();
  306. #endif
  307. UserMain();
  308. #ifdef LUAT_USE_WLAN
  309. //tls_sys_auto_mode_run();
  310. #endif
  311. // for (;;)
  312. // {
  313. #if MAIN_TASK_DELETE_AFTER_START_FTR
  314. if (tststarthdl)
  315. {
  316. tls_os_task_del_by_task_handle(tststarthdl,task_start_free);
  317. // break;
  318. }
  319. // tls_os_time_delay(0x10000000);
  320. #else
  321. // //printf("start up\n");
  322. // extern void tls_os_disp_task_stat_info(void);
  323. // tls_os_disp_task_stat_info();
  324. // tls_os_time_delay(1000);
  325. #endif
  326. // }
  327. }