WLAN开发实例

本例程提供WLAN模块初始化过程的完整使用流程。示例如下:

1、根据硬件,修改配置参数


1. /* 根据硬件参数,通过wifi_config.hcs配置相关参数,以下是配置chip的示例 */
2. hisi :& deviceList {
3. C1 :: Chip {
4. chipName = "hisi";   /* WLAN芯片的名称 */
5. chipId = 0;          /* WLAN芯片的id */
6. featureMap = 0xFFFF; /* WLAN模块特性map */
7. powerType = 0;       /* 电源类型 */
8. irqNo = 0;           /* 中断号 */
9. bus {
10. busType = 0;       /* 总线类型:0-sdio 1-usb 2-spi etc. */
11. funcNum = [1];     /* 功能号 */
12. vendorId = 0x0296; /* 厂商id */
13. deviceId = 0x5347; /* 设备id */
14. timeout = 1000;    /* 读/写数据的超时时间 */
15. blockSize = 512;   /* 读/写数据的块大小 */
16. }
17. }
18. }
19. board {
20. boardName = "3518EV300";  /* 单板名称 */
21. busType = 0;              /* 总线类型 */
22. busIdx = 1;               /* 总线索引 */
23. reset   = [5,0];          /* GPIO组号和偏移 */
24. gpioArgs = [10,10,10];    /* GPIO组数, GPIO每组个数, GPIO中断数 */
25. busRegs {
26. cclk_out = [0x112C0048, 0x1A04];  /* 时钟线寄存器配置 */
27. ccmd     = [0x112C004C, 0x1004];  /* 命令传输线寄存器配 */
28. cdata0    = [0x112C0064, 0x1004]; /* 数据0线线寄存器配 */
29. cdata1    = [0x112C0058, 0x1004]; /* 数据1线寄存器配 */
30. cdata2    = [0x112C005C, 0x1004]; /* 数据2线寄存器配 */
31. cdata3    = [0x112C0060, 0x1004]; /* 数据3线寄存器配 */
32. }
33. }

2、适配挂接


1. /* WLAN初始化流程 */
2. #include "gpio_if.h"
3. #include "hdf_wifi_core.h"
4. #include "hdf_device_desc.h"
5. #include "hdf_wifi_product.h"
6. #include "hdf_log.h"
7. #include "hdf_wifi_cmd.h"
8. #include "osal_mem.h"
9. #include "osal_time.h"
10. #include "hdf_wifi_config.h"

12. struct HdfWifiProductData g_hdfWifiProductData;

14. /* 声明芯片驱动 */
15. HDF_WIFI_CHIP_DECLARE(hisi);

17. int32_t Hi3881Deinit(struct HdfWifiChipData *chipData);
18. int32_t Hi3881Init(struct HdfWifiChipData *chipData, const struct HdfConfigWifiChip *chipConfig);
19. HDF_WIFI_CHIP_DRIVER(hisi, Hi3881Init, Hi3881Deinit);

21. struct HdfWifiChipData *g_wifiChips[] = {
22. HDF_WIFI_CHIP(hisi),
23. };

25. /* 声明芯片驱动 */
26. static const struct HdfConfigWifiBoard *HdfWifiGetBoardConfig(void)
27. {
28. struct HdfConfigWifiRoot *rootConfig = HdfWifiGetModuleConfigRoot();

30. return &rootConfig->wifiConfig.board;
31. }

33. int32_t HdfWifiGetBusIdx(void)
34. {
35. const struct HdfConfigWifiBoard *board = HdfWifiGetBoardConfig();
36. if (board != NULL) {
37. return board->busIdx;
38. }

40. return -1;
41. }

43. static void HdfWifiChipReset(void)
44. {
45. const struct HdfConfigWifiBoard *board = HdfWifiGetBoardConfig();
46. uint16_t gpio;
47. int32_t ret;

49. if (board != NULL && board->busType == BUS_SDIO) {
50. /* set dir input */
51. gpio = board->reset[0] *  board->gpioArgs[1] + board->reset[1];
52. ret = GpioSetDir(gpio, 1);
53. if (ret != HDF_SUCCESS) {
54. HDF_LOGE("%s: set dir fail! ret:%d\n", __func__, ret);
55. return;
56. }
57. /* power off */
58. ret = GpioWrite(gpio, 0);
59. if (ret != HDF_SUCCESS) {
60. HDF_LOGE("%s: val:0 write fail! ret:%d\n", __func__, ret);
61. return;
62. }
63. OsalMSleep(0x10);
64. /* power on */
65. ret = GpioWrite(gpio, 1);
66. if (ret != HDF_SUCCESS) {
67. HDF_LOGE("%s: val:1 write fail! ret:%d\n", __func__, ret);
68. return;
69. }
70. OsalMSleep(0x20);
71. }

73. return;
74. }

76. static int32_t HdfWifiProductInit(struct HdfDeviceObject *device)
77. {
78. int32_t ret;

80. ret = HdfParseWifiConfig(device->property);
81. if (ret != HDF_SUCCESS) {
82. HDF_LOGE("%s:ParseWifiConfig error ret=%d", __func__, ret);
83. return ret;
84. }
85. /* reset chip firstly */
86. HdfWifiChipReset();
87. return HDF_SUCCESS;
88. }

90. /* WifiModule初始化 */
91. static int16_t HdfWifiModuleInit(const struct HdfConfigWifiModuleConfig *config)
92. {
93. if (config == NULL) {
94. return HDF_FAILURE;
95. }
96. /* 创建Module */
97. struct WifiModule *module = WifiModuleCreate(config);
98. if (module == NULL) {
99. return HDF_FAILURE;
100. }
101. g_hdfWifiProductData.module = module;
102. HDF_LOGD("%s:hdf wifi module init succ", __func__);

104. return HDF_SUCCESS;
105. }

107. /* 通过芯片的名称获取相应的芯片驱动 */
108. static struct HdfWifiChipData *HdfWifiFindChipDriverByName(const char *chipName)
109. {
110. if (chipName == NULL) {
111. return NULL;
112. }

114. bool found = FALSE;
115. struct HdfWifiChipData *chipDriver = NULL;
116. int32_t chipNum = sizeof(g_wifiChips) / sizeof(g_wifiChips[0]);

118. /* 循环遍历所配置的芯片,返回匹配的芯片驱动 */
119. for (int32_t i = 0; i < chipNum; i++) {
120. chipDriver = g_wifiChips[i];
121. if (chipDriver != NULL && !strcmp(chipDriver->name, chipName)) {
122. found = TRUE;
123. HDF_LOGI("%s: find chip %s", __func__, chipName);
124. break;
125. }
126. }

128. return found ? chipDriver : NULL;
129. }

131. /* 芯片初始化 */
132. static int16_t HdfWifiChipInit(struct WifiModule *module, const struct HdfConfigWifiChip *chipConfig)
133. {
134. int32_t ret;

136. if (module == NULL) {
137. HDF_LOGE("%s: module is NULL", __func__);
138. return HDF_FAILURE;
139. }

141. /* 通过芯片的名称获取相应的芯片驱动 */
142. struct HdfWifiChipData *chipDriver = HdfWifiFindChipDriverByName(chipConfig->chipName);
143. if (chipDriver == NULL) {
144. HDF_LOGI("%s: chip driver %s not supported", __func__, chipConfig->chipName);
145. return HDF_DEV_ERR_OP;
146. }

148. if (chipDriver->init == NULL) {
149. HDF_LOGI("%s: chip driver %s not 'init' api", __func__, chipConfig->chipName);
150. return HDF_DEV_ERR_OP;
151. }

153. /* 初始化芯片 */
154. ret = chipDriver->init(chipDriver, chipConfig);
155. if (ret != HDF_SUCCESS) {
156. HDF_LOGE("%s:init chip %s error ret=%d", __func__, chipConfig->chipName, ret);
157. return HDF_DEV_ERR_OP;
158. }

160. /* 将芯片和feature进行绑定 */
161. for (uint32_t i = 0; i < HDF_WIFI_FEATURE_NUM; i++) {
162. if ((module->feList->fe[i] != NULL) && (chipConfig->featureMap & (1 << i))) {
163. HDF_LOGI("%s:reg chip to feature %d", __func__, i);
164. module->feList->fe[i]->chip = chipDriver;
165. }
166. }
167. HDF_LOGI("%s:init chip %s success!", __func__, chipConfig->chipName);
168. return HDF_SUCCESS;
169. }

171. /* WLAN平台初始化 */
172. static int16_t HdfWifiPlatformInit(const struct HdfConfigWifiModuleConfig *config)
173. {
174. /* 根据配置进行module初始化 */
175. int32_t ret = HdfWifiModuleInit(config);
176. if (ret != HDF_SUCCESS) {
177. HDF_LOGE("%s:HdfWifiModuleInit error ret=%d", __func__, ret);
178. return ret;
179. }

181. return ret;
182. }

184. struct HdfWifiProductData *HdfWifiGetProduct(void)
185. {
186. return &g_hdfWifiProductData;
187. }

189. static int32_t HdfWifiDriverInit(struct HdfDeviceObject *device)
190. {
191. int32_t ret;
192. HDF_LOGD("%s:start..", __func__);
193. if (device == NULL) {
194. return HDF_FAILURE;
195. }
196. /* 配置分析(初始化WLAN配置) */
197. ret = HdfWifiProductInit(device);
198. if (ret != HDF_SUCCESS) {
199. HDF_LOGE("%s:init produt cfg error ret=%d", __func__, ret);
200. return ret;
201. }
202. /* 获取全量配置的结构体对象 */
203. struct HdfConfigWifiRoot *rootConfig = HdfWifiGetModuleConfigRoot();
204. struct HdfWifiProductData *wifiData = HdfWifiGetProduct();
205. const struct HdfConfigWifiModuleConfig *moduleConfig = &rootConfig->wifiConfig.moduleConfig;
206. /* WIFI平台的初始化(Module初始化) */
207. ret = HdfWifiPlatformInit(moduleConfig);
208. if (ret) {
209. HDF_LOGE("%s:init platform error ret=%d", __func__, ret);
210. return ret;
211. }

213. /* 循环遍历所配置的芯片,并初始化有相应硬件的芯片 */
214. for (int16_t i = 0; i < rootConfig->wifiConfig.deviceList.chipSize; i++) {
215. const struct HdfConfigWifiChip *chipConfig = &rootConfig->wifiConfig.deviceList.chip[i];
216. ret = HdfWifiChipInit(wifiData->module, chipConfig);
217. if (ret != HDF_SUCCESS) {
218. HDF_LOGE("%s:init chip %d error ret=%d, feature %d is unavailable!", __func__, i, ret,
219. chipConfig->featureMap);
220. } else {
221. wifiData->device = device;
222. HDF_LOGE("%s:init chip %d success!", __func__, i);
223. break;
224. }
225. }

227. HDF_LOGD("%s:success..", __func__);
228. return ret;
229. }

231. int32_t HdfWifiDriverDispatch(struct HdfDeviceIoClient *client, int id, struct HdfSBuf *data, struct HdfSBuf *reply)
232. {
233. if (client == NULL) {
234. return HDF_ERR_INVALID_PARAM;
235. }
236. return WifiCmdProcess(client->device, id, data, reply);
237. }

239. static int HdfWifiDriverBind(struct HdfDeviceObject *dev)
240. {
241. if (dev == NULL) {
242. return HDF_FAILURE;
243. }
244. static struct IDeviceIoService wifiService = {
245. .object.objectId = 1,
246. .Dispatch = HdfWifiDriverDispatch,
247. };
248. dev->service = &wifiService;
249. return HDF_SUCCESS;
250. }

252. static void HdfWifiDriverRelease(struct HdfDeviceObject *object)
253. {
254. (void)object;
255. }

257. struct HdfDriverEntry g_hdfWifiEntry = {
258. .moduleVersion = 1,
259. .Bind = HdfWifiDriverBind,
260. .Init = HdfWifiDriverInit,
261. .Release = HdfWifiDriverRelease,
262. .moduleName = "HDF_WIFI"
263. };

265. HDF_INIT(g_hdfWifiEntry);