1 /*
2 * Copyright (c) 2006-2022, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2018-08-30 ZeroFree the first version
9 */
10
11 #include <rtthread.h>
12 #include <rtdevice.h>
13
14 #ifdef RT_USING_WIFI
15
16 #include <drv_common.h>
17 #include <dev_wlan_mgnt.h>
18 #include <dev_wlan_prot.h>
19 #include <dev_wlan_cfg.h>
20 #include <string.h>
21 #include <fal.h>
22
23 #define DBG_ENABLE
24 #define DBG_SECTION_NAME "WLAN"
25 #define DBG_COLOR
26 #define DBG_LEVEL DBG_LOG
27 #include <rtdbg.h>
28
29 #define NVRAM_GENERATED_MAC_ADDRESS "macaddr=02:0A:F7:fe:86:1c"
30 #define WIFI_IMAGE_PARTITION_NAME "wifi_image"
31 #define WIFI_INIT_THREAD_STACK_SIZE (1024 * 4)
32 #define WIFI_INIT_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
33 #define WIFI_INIT_WAIT_TIME (rt_tick_from_millisecond(1000))
34
35 extern int wifi_hw_init(void);
36 static rt_bool_t init_flag = 0;
37 static const struct fal_partition *partition = RT_NULL;
38
39 rt_align(64)
40 static const char wifi_nvram_image[] =
41 // # The following parameter values are just placeholders, need to be updated.
42 "manfid=0x2d0" "\x00"
43 "prodid=0x0726" "\x00"
44 "vendid=0x14e4" "\x00"
45 "devid=0x43e2" "\x00"
46 "boardtype=0x0726" "\x00"
47 "boardrev=0x1101" "\x00"
48 "boardnum=22" "\x00"
49 "xtalfreq=26000" "\x00"
50 "sromrev=11" "\x00"
51 "boardflags=0x00404201" "\x00"
52 "boardflags3=0x08000000" "\x00"
53 NVRAM_GENERATED_MAC_ADDRESS "\x00"
54 "nocrc=1" "\x00"
55 "ag0=255" "\x00"
56 "aa2g=1" "\x00"
57 "ccode=ALL"
58 "\x00"
59 "swdiv_en=1" "\x00"
60 "swdiv_gpio=2" "\x00"
61
62 "pa0itssit=0x20" "\x00"
63 "extpagain2g=0" "\x00"
64 "pa2ga0=-215,5267,-656" "\x00"
65 "AvVmid_c0=0x0,0xc8" "\x00"
66 "cckpwroffset0=5" "\x00"
67 "maxp2ga0=80" "\x00"
68 "txpwrbckof=6" "\x00"
69 "cckbw202gpo=0x6666" "\x00"
70 "legofdmbw202gpo=0xaaaaaaaa" "\x00"
71 "mcsbw202gpo=0xbbbbbbbb" "\x00"
72 "propbw202gpo=0xdd" "\x00"
73 "ofdmdigfilttype=18" "\x00"
74 "ofdmdigfilttypebe=18" "\x00"
75 "papdmode=1" "\x00"
76 "papdvalidtest=1" "\x00"
77 "pacalidx2g=32" "\x00"
78 "papdepsoffset=-36" "\x00"
79 "papdendidx=61" "\x00"
80 "wl0id=0x431b" "\x00"
81 "deadman_to=0xffffffff" "\x00"
82 "muxenab=0x11" "\x00"
83 "spurconfig=0x3" "\x00"
84 "\x00\x00";
85
wiced_platform_resource_size(int resource)86 int wiced_platform_resource_size(int resource)
87 {
88 int size = 0;
89
90 /* Download firmware */
91 if (resource == 0)
92 {
93 size = 355159;
94 }
95 else if (resource == 1)
96 {
97 size = sizeof(wifi_nvram_image);
98 }
99
100 return size;
101 }
102
wiced_platform_resource_read(int resource,uint32_t offset,void * buffer,uint32_t buffer_size)103 int wiced_platform_resource_read(int resource, uint32_t offset, void* buffer, uint32_t buffer_size)
104 {
105 if (resource == 0)
106 {
107 /* read RF firmware from partition */
108 fal_partition_read(partition, offset, buffer, buffer_size);
109 }
110 else if (resource == 1)
111 {
112 memcpy(buffer, &wifi_nvram_image[offset], buffer_size);
113 }
114
115 return buffer_size;
116 }
117
118 /**
119 * wait milliseconds for wifi low level initialize complete
120 *
121 * time_ms: timeout in milliseconds
122 */
rt_hw_wlan_wait_init_done(rt_uint32_t time_ms)123 int rt_hw_wlan_wait_init_done(rt_uint32_t time_ms)
124 {
125 rt_uint32_t time_cnt = 0;
126
127 /* wait wifi low level initialize complete */
128 while (time_cnt <= (time_ms / 100))
129 {
130 time_cnt++;
131 rt_thread_mdelay(100);
132
133 if (init_flag == 1)
134 {
135 break;
136 }
137 }
138
139 if (time_cnt > (time_ms / 100))
140 {
141 return -RT_ETIMEOUT;
142 }
143
144 return RT_EOK;
145 }
146
147
wifi_init_thread_entry(void * parameter)148 static void wifi_init_thread_entry(void *parameter)
149 {
150 /* initialize fal */
151 fal_init();
152 partition = fal_partition_find(WIFI_IMAGE_PARTITION_NAME);
153
154 if (partition == RT_NULL)
155 {
156 LOG_E("%s partition is not exist, please check your configuration!", WIFI_IMAGE_PARTITION_NAME);
157 return;
158 }
159
160 /* initialize low level wifi(ap6212) library */
161 wifi_hw_init();
162
163 /* waiting for sdio bus stability */
164 rt_thread_delay(WIFI_INIT_WAIT_TIME);
165
166 /* set wifi work mode */
167 rt_wlan_set_mode(RT_WLAN_DEVICE_STA_NAME, RT_WLAN_STATION);
168 /* NEED TODO !!! */
169 /* rt_wlan_set_mode(RT_WLAN_DEVICE_AP_NAME, RT_WLAN_AP); */
170
171 init_flag = 1;
172 }
173
rt_hw_wlan_init(void)174 int rt_hw_wlan_init(void)
175 {
176 if (init_flag == 1)
177 {
178 return RT_EOK;
179 }
180
181 rt_thread_t tid = RT_NULL;
182
183 tid = rt_thread_create("wifi_init", wifi_init_thread_entry, RT_NULL, WIFI_INIT_THREAD_STACK_SIZE, WIFI_INIT_THREAD_PRIORITY, 20);
184
185 if (tid)
186 {
187 rt_thread_startup(tid);
188 }
189 else
190 {
191 LOG_E("Create wifi initialization thread fail!");
192 return -RT_ERROR;
193 }
194
195 return RT_EOK;
196 }
197 INIT_APP_EXPORT(rt_hw_wlan_init);
198
ap6212_init(void)199 static int ap6212_init(void)
200 {
201 #define AP6212_WL_REG_ON GET_PIN(C, 13)
202
203 /* enable the WLAN REG pin */
204 rt_pin_mode(AP6212_WL_REG_ON, PIN_MODE_OUTPUT);
205 rt_pin_write(AP6212_WL_REG_ON, PIN_HIGH);
206
207 return 0;
208 }
209 INIT_DEVICE_EXPORT(ap6212_init);
210
211 #endif /* RT_USING_WIFI */
212