1 /*
2 * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 /*****************************************************************************
17 *
18 * File Name : wm_main.c
19 *
20 * Description: wm main
21 *
22 * Copyright (c) 2014 Winner Micro Electronic Design Co., Ltd.
23 * All rights reserved.
24 *
25 * Date : 2014-6-14
26 *****************************************************************************/
27 #include <string.h>
28 #include "wm_irq.h"
29 #include "wm_regs.h"
30 #include "wm_type_def.h"
31 #include "wm_timer.h"
32 #include "wm_irq.h"
33 #include "wm_params.h"
34 #include "wm_hostspi.h"
35 #include "wm_flash.h"
36 #include "wm_fls_gd25qxx.h"
37 #include "wm_internal_flash.h"
38 #include "wm_efuse.h"
39 #include "wm_debug.h"
40 #include "wm_config.h"
41 #include "wm_osal.h"
42 #include "wm_cpu.h"
43 #include "wm_io.h"
44 #include "wm_mem.h"
45 #include "wm_wl_task.h"
46 #include "wm_wl_timers.h"
47 #ifdef TLS_CONFIG_HARD_CRYPTO
48 #include "wm_crypto_hard.h"
49 #endif
50 #include "wm_gpio_afsel.h"
51 #include "wm_pmu.h"
52 #include "wm_ram_config.h"
53 #include "wm_uart.h"
54 #include "los_task.h"
55
56 #define TASK_START_STK_SIZE 1024 /* Size of each task's stacks (# of WORDs) */
57 /* If you want to delete main task after it works, you can open this MACRO below */
58 #define MAIN_TASK_DELETE_AFTER_START_FTR 0
59 #if !TLS_OS_LITEOS
60 u8 *TaskStartStk = NULL;
61 #endif
62 tls_os_task_t tststarthdl = NULL;
63
64 #define FW_MAJOR_VER 0x1
65 #define FW_MINOR_VER 0x0
66 #define FW_PATCH_VER 0x4
67
68 const char FirmWareVer[4] = {
69 'v',
70 FW_MAJOR_VER, /* Main version */
71 FW_MINOR_VER, /* Subversion */
72 FW_PATCH_VER /* Internal version */
73 };
74
75 const char HwVer[6] = {
76 'H',
77 0x1,
78 0x0,
79 0x0,
80 0x0,
81 0x0
82 };
83
84 extern const char WiFiVer[];
85 extern u8 tx_gain_group[];
86 extern void *tls_wl_init(u8 *tx_gain, u8 *mac_addr, u8 *hwver);
87 extern int wpa_supplicant_init(u8 *mac_addr);
88 extern void tls_sys_auto_mode_run(void);
89 extern void UserMain(void);
90
91 void task_start (void *data);
92
93 /* main program */
94
vApplicationIdleHook(void)95 void vApplicationIdleHook(void)
96 {
97 __WAIT();
98 return;
99 }
100
wm_gpio_config(void)101 void wm_gpio_config(void)
102 {
103 /* must call first */
104 wm_gpio_af_disable();
105
106 wm_uart0_tx_config(WM_IO_PB_19);
107 wm_uart0_rx_config(WM_IO_PB_20);
108
109 #if (TLS_CONFIG_LS_SPI)
110 wm_spi_cs_config(WM_IO_PB_04);
111 wm_spi_ck_config(WM_IO_PB_02);
112 wm_spi_di_config(WM_IO_PB_03);
113 wm_spi_do_config(WM_IO_PB_05);
114 #endif
115 }
main(void)116 int main(void)
117 {
118 u32 value = 0;
119 /* 32K switch to use RC circuit & calibration */
120 tls_pmu_clk_select(0);
121 /* Switch to DBG */
122 value = tls_reg_read32(HR_PMU_BK_REG);
123 value &= ~(BIT(19)); // 19:byte alignment
124 tls_reg_write32(HR_PMU_BK_REG, value);
125 value = tls_reg_read32(HR_PMU_PS_CR);
126 value &= ~(BIT(5)); // 5:byte alignment
127 tls_reg_write32(HR_PMU_PS_CR, value);
128
129 tls_sys_clk_set(CPU_CLK_80M);
130 tls_os_init(NULL);
131
132 /* configure wake up source begin */
133 csi_vic_set_wakeup_irq(SDIO_IRQn);
134 csi_vic_set_wakeup_irq(MAC_IRQn);
135 csi_vic_set_wakeup_irq(SEC_IRQn);
136 csi_vic_set_wakeup_irq(DMA_Channel0_IRQn);
137 csi_vic_set_wakeup_irq(DMA_Channel1_IRQn);
138 csi_vic_set_wakeup_irq(DMA_Channel2_IRQn);
139 csi_vic_set_wakeup_irq(DMA_Channel3_IRQn);
140 csi_vic_set_wakeup_irq(DMA_Channel4_7_IRQn);
141 csi_vic_set_wakeup_irq(DMA_BRUST_IRQn);
142 csi_vic_set_wakeup_irq(I2C_IRQn);
143 csi_vic_set_wakeup_irq(ADC_IRQn);
144 csi_vic_set_wakeup_irq(SPI_LS_IRQn);
145 csi_vic_set_wakeup_irq(SPI_HS_IRQn);
146 csi_vic_set_wakeup_irq(GPIOA_IRQn);
147 csi_vic_set_wakeup_irq(GPIOB_IRQn);
148 csi_vic_set_wakeup_irq(UART0_IRQn);
149 csi_vic_set_wakeup_irq(UART1_IRQn);
150 csi_vic_set_wakeup_irq(UART24_IRQn);
151 csi_vic_set_wakeup_irq(BLE_IRQn);
152 csi_vic_set_wakeup_irq(BT_IRQn);
153 csi_vic_set_wakeup_irq(PWM_IRQn);
154 csi_vic_set_wakeup_irq(I2S_IRQn);
155 csi_vic_set_wakeup_irq(SIDO_HOST_IRQn);
156 csi_vic_set_wakeup_irq(SYS_TICK_IRQn);
157 csi_vic_set_wakeup_irq(RSA_IRQn);
158 csi_vic_set_wakeup_irq(CRYPTION_IRQn);
159 csi_vic_set_wakeup_irq(PMU_IRQn);
160 csi_vic_set_wakeup_irq(TIMER_IRQn);
161 csi_vic_set_wakeup_irq(WDG_IRQn);
162 /* configure wake up source end */
163 #if TLS_OS_LITEOS
164 tls_os_task_create(&tststarthdl, "firstThr",
165 task_start,
166 (void *)0,
167 (void *)NULL,
168 TASK_START_STK_SIZE * sizeof(u32), /* 任务栈的大小 */
169 1,
170 0);
171 tls_os_start_scheduler();
172 #else
173 TaskStartStk = tls_mem_alloc(sizeof(u32)*TASK_START_STK_SIZE);
174 if (TaskStartStk) {
175 tls_os_task_create(&tststarthdl, NULL,
176 task_start,
177 (void *)0,
178 (void *)TaskStartStk, /* 任务栈的起始地址 */
179 TASK_START_STK_SIZE * sizeof(u32), /* 任务栈的大小 */
180 1,
181 0);
182 tls_os_start_scheduler();
183 } else {
184 while (1);
185 }
186 #endif
187 return 0;
188 }
189
tls_get_wifi_ver(void)190 unsigned int tls_get_wifi_ver(void)
191 {
192 return (WiFiVer[0]<<16)|(WiFiVer[1]<<8)|WiFiVer[2]; // 16:byte alignment, 8:byte alignment, 2:byte alignment
193 }
194
disp_version_info(void)195 void disp_version_info(void)
196 {
197 TLS_DBGPRT_INFO("\n\n");
198 TLS_DBGPRT_INFO("****************************************************************\n");
199 TLS_DBGPRT_INFO("* *\n");
200 TLS_DBGPRT_INFO("* Copyright (C) 2014 WinnerMicro Co. Ltd. *\n");
201 TLS_DBGPRT_INFO("* All rights reserved. *\n");
202 TLS_DBGPRT_INFO("* WinnerMicro Firmware Version: %x.%x.%X *\n",
203 FirmWareVer[1], FirmWareVer[2], FirmWareVer[3]); // 2:array element, 3:array element
204 TLS_DBGPRT_INFO("* WinnerMicro Hardware Version: %x.%x.%x.%x.%x *\n",
205 HwVer[1], HwVer[2], HwVer[3], // 2:array element, 3:array element
206 HwVer[4], HwVer[5]); // 4:array element, 5:array element
207 TLS_DBGPRT_INFO("* *\n");
208 TLS_DBGPRT_INFO("* WinnerMicro Wi-Fi Lib Version: %x.%x.%x *\n",
209 WiFiVer[0], WiFiVer[1], WiFiVer[2]); // 2:array element
210 TLS_DBGPRT_INFO("****************************************************************\n");
211 }
212
tls_pmu_chipsleep_callback(int sleeptime)213 void tls_pmu_chipsleep_callback(int sleeptime)
214 {
215 /* set wakeup time */
216 tls_pmu_timer1_start(sleeptime);
217 /* enter chip sleep */
218 tls_pmu_sleep_start();
219 }
220
221 /*****************************************************************************
222 * Function Name // task_start
223 * Descriptor // before create multi_task, we create a task_start task
224 * // in this example, this task display the cpu usage
225 * Input
226 * Output
227 * Return
228 ****************************************************************************/
task_start(void * data)229 void task_start (void *data)
230 {
231 #if defined(LOSCFG_KERNEL_TEST_FULL) || defined(LOSCFG_KERNEL_TEST)
232 /* nothing to do when kernel test is running */
233 #else
234 u8 enable = 0;
235
236 #if TLS_CONFIG_CRYSTAL_24M
237 tls_wl_hw_using_24m_crystal();
238 #endif
239
240 /* must call first to configure gpio Alternate functions according the hardware design */
241 wm_gpio_config();
242
243 tls_irq_init();
244
245 #if TLS_CONFIG_HARD_CRYPTO
246 tls_crypto_init();
247 #endif
248
249 #if (TLS_CONFIG_LS_SPI)
250 tls_spi_init();
251 tls_spifls_init();
252 #endif
253
254 tls_fls_init();
255 tls_fls_sys_param_postion_init();
256
257 /* PARAM GAIN,MAC default */
258 tls_ft_param_init();
259 tls_param_load_factory_default();
260 tls_param_init(); /* add param to init sysparam_lock sem */
261
262 tls_wifi_netif_event_init();
263
264 tls_param_get(TLS_PARAM_ID_PSM, &enable, TRUE);
265 if (enable != TRUE) {
266 enable = TRUE;
267 tls_param_set(TLS_PARAM_ID_PSM, &enable, TRUE);
268 }
269 #endif
270
271 #if TLS_CONFIG_HOSTIF
272 tls_hostif_init();
273 #endif
274
275 #if TLS_CONFIG_UART
276 tls_uart_init();
277 #endif
278 UserMain();
279
280 extern void OHOS_SystemInit(void);
281 OHOS_SystemInit();
282
283 for (;;) {
284 #if 1
285 tls_os_time_delay(0x10000000);
286 #else
287 extern void tls_os_disp_task_stat_info(void);
288 tls_os_disp_task_stat_info();
289 tls_os_time_delay(1000); // 1000:time unit
290 #endif
291 }
292 }