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"
  11. struct HdfWifiProductData g_hdfWifiProductData;
  12. /* 声明芯片驱动 */
  13. HDF_WIFI_CHIP_DECLARE(hisi);
  14. int32_t Hi3881Deinit(struct HdfWifiChipData *chipData);
  15. int32_t Hi3881Init(struct HdfWifiChipData *chipData, const struct HdfConfigWifiChip *chipConfig);
  16. HDF_WIFI_CHIP_DRIVER(hisi, Hi3881Init, Hi3881Deinit);
  17. struct HdfWifiChipData *g_wifiChips[] = {
  18. HDF_WIFI_CHIP(hisi),
  19. };
  20. /* 声明芯片驱动 */
  21. static const struct HdfConfigWifiBoard *HdfWifiGetBoardConfig(void)
  22. {
  23. struct HdfConfigWifiRoot *rootConfig = HdfWifiGetModuleConfigRoot();
  24. return &rootConfig->wifiConfig.board;
  25. }
  26. int32_t HdfWifiGetBusIdx(void)
  27. {
  28. const struct HdfConfigWifiBoard *board = HdfWifiGetBoardConfig();
  29. if (board != NULL) {
  30. return board->busIdx;
  31. }
  32. return -1;
  33. }
  34. static void HdfWifiChipReset(void)
  35. {
  36. const struct HdfConfigWifiBoard *board = HdfWifiGetBoardConfig();
  37. uint16_t gpio;
  38. int32_t ret;
  39. if (board != NULL && board->busType == BUS_SDIO) {
  40. /* set dir input */
  41. gpio = board->reset[0] * board->gpioArgs[1] + board->reset[1];
  42. ret = GpioSetDir(gpio, 1);
  43. if (ret != HDF_SUCCESS) {
  44. HDF_LOGE("%s: set dir fail! ret:%d\n", __func__, ret);
  45. return;
  46. }
  47. /* power off */
  48. ret = GpioWrite(gpio, 0);
  49. if (ret != HDF_SUCCESS) {
  50. HDF_LOGE("%s: val:0 write fail! ret:%d\n", __func__, ret);
  51. return;
  52. }
  53. OsalMSleep(0x10);
  54. /* power on */
  55. ret = GpioWrite(gpio, 1);
  56. if (ret != HDF_SUCCESS) {
  57. HDF_LOGE("%s: val:1 write fail! ret:%d\n", __func__, ret);
  58. return;
  59. }
  60. OsalMSleep(0x20);
  61. }
  62. return;
  63. }
  64. static int32_t HdfWifiProductInit(struct HdfDeviceObject *device)
  65. {
  66. int32_t ret;
  67. ret = HdfParseWifiConfig(device->property);
  68. if (ret != HDF_SUCCESS) {
  69. HDF_LOGE("%s:ParseWifiConfig error ret=%d", __func__, ret);
  70. return ret;
  71. }
  72. /* reset chip firstly */
  73. HdfWifiChipReset();
  74. return HDF_SUCCESS;
  75. }
  76. /* WifiModule初始化 */
  77. static int16_t HdfWifiModuleInit(const struct HdfConfigWifiModuleConfig *config)
  78. {
  79. if (config == NULL) {
  80. return HDF_FAILURE;
  81. }
  82. /* 创建Module */
  83. struct WifiModule *module = WifiModuleCreate(config);
  84. if (module == NULL) {
  85. return HDF_FAILURE;
  86. }
  87. g_hdfWifiProductData.module = module;
  88. HDF_LOGD("%s:hdf wifi module init succ", __func__);
  89. return HDF_SUCCESS;
  90. }
  91. /* 通过芯片的名称获取相应的芯片驱动 */
  92. static struct HdfWifiChipData *HdfWifiFindChipDriverByName(const char *chipName)
  93. {
  94. if (chipName == NULL) {
  95. return NULL;
  96. }
  97. bool found = FALSE;
  98. struct HdfWifiChipData *chipDriver = NULL;
  99. int32_t chipNum = sizeof(g_wifiChips) / sizeof(g_wifiChips[0]);
  100. /* 循环遍历所配置的芯片,返回匹配的芯片驱动 */
  101. for (int32_t i = 0; i < chipNum; i++) {
  102. chipDriver = g_wifiChips[i];
  103. if (chipDriver != NULL && !strcmp(chipDriver->name, chipName)) {
  104. found = TRUE;
  105. HDF_LOGI("%s: find chip %s", __func__, chipName);
  106. break;
  107. }
  108. }
  109. return found ? chipDriver : NULL;
  110. }
  111. /* 芯片初始化 */
  112. static int16_t HdfWifiChipInit(struct WifiModule *module, const struct HdfConfigWifiChip *chipConfig)
  113. {
  114. int32_t ret;
  115. if (module == NULL) {
  116. HDF_LOGE("%s: module is NULL", __func__);
  117. return HDF_FAILURE;
  118. }
  119. /* 通过芯片的名称获取相应的芯片驱动 */
  120. struct HdfWifiChipData *chipDriver = HdfWifiFindChipDriverByName(chipConfig->chipName);
  121. if (chipDriver == NULL) {
  122. HDF_LOGI("%s: chip driver %s not supported", __func__, chipConfig->chipName);
  123. return HDF_DEV_ERR_OP;
  124. }
  125. if (chipDriver->init == NULL) {
  126. HDF_LOGI("%s: chip driver %s not 'init' api", __func__, chipConfig->chipName);
  127. return HDF_DEV_ERR_OP;
  128. }
  129. /* 初始化芯片 */
  130. ret = chipDriver->init(chipDriver, chipConfig);
  131. if (ret != HDF_SUCCESS) {
  132. HDF_LOGE("%s:init chip %s error ret=%d", __func__, chipConfig->chipName, ret);
  133. return HDF_DEV_ERR_OP;
  134. }
  135. /* 将芯片和feature进行绑定 */
  136. for (uint32_t i = 0; i < HDF_WIFI_FEATURE_NUM; i++) {
  137. if ((module->feList->fe[i] != NULL) && (chipConfig->featureMap & (1 << i))) {
  138. HDF_LOGI("%s:reg chip to feature %d", __func__, i);
  139. module->feList->fe[i]->chip = chipDriver;
  140. }
  141. }
  142. HDF_LOGI("%s:init chip %s success!", __func__, chipConfig->chipName);
  143. return HDF_SUCCESS;
  144. }
  145. /* WLAN平台初始化 */
  146. static int16_t HdfWifiPlatformInit(const struct HdfConfigWifiModuleConfig *config)
  147. {
  148. /* 根据配置进行module初始化 */
  149. int32_t ret = HdfWifiModuleInit(config);
  150. if (ret != HDF_SUCCESS) {
  151. HDF_LOGE("%s:HdfWifiModuleInit error ret=%d", __func__, ret);
  152. return ret;
  153. }
  154. return ret;
  155. }
  156. struct HdfWifiProductData *HdfWifiGetProduct(void)
  157. {
  158. return &g_hdfWifiProductData;
  159. }
  160. static int32_t HdfWifiDriverInit(struct HdfDeviceObject *device)
  161. {
  162. int32_t ret;
  163. HDF_LOGD("%s:start..", __func__);
  164. if (device == NULL) {
  165. return HDF_FAILURE;
  166. }
  167. /* 配置分析(初始化WLAN配置) */
  168. ret = HdfWifiProductInit(device);
  169. if (ret != HDF_SUCCESS) {
  170. HDF_LOGE("%s:init produt cfg error ret=%d", __func__, ret);
  171. return ret;
  172. }
  173. /* 获取全量配置的结构体对象 */
  174. struct HdfConfigWifiRoot *rootConfig = HdfWifiGetModuleConfigRoot();
  175. struct HdfWifiProductData *wifiData = HdfWifiGetProduct();
  176. const struct HdfConfigWifiModuleConfig *moduleConfig = &rootConfig->wifiConfig.moduleConfig;
  177. /* WIFI平台的初始化(Module初始化) */
  178. ret = HdfWifiPlatformInit(moduleConfig);
  179. if (ret) {
  180. HDF_LOGE("%s:init platform error ret=%d", __func__, ret);
  181. return ret;
  182. }
  183. /* 循环遍历所配置的芯片,并初始化有相应硬件的芯片 */
  184. for (int16_t i = 0; i < rootConfig->wifiConfig.deviceList.chipSize; i++) {
  185. const struct HdfConfigWifiChip *chipConfig = &rootConfig->wifiConfig.deviceList.chip[i];
  186. ret = HdfWifiChipInit(wifiData->module, chipConfig);
  187. if (ret != HDF_SUCCESS) {
  188. HDF_LOGE("%s:init chip %d error ret=%d, feature %d is unavailable!", __func__, i, ret,
  189. chipConfig->featureMap);
  190. } else {
  191. wifiData->device = device;
  192. HDF_LOGE("%s:init chip %d success!", __func__, i);
  193. break;
  194. }
  195. }
  196. HDF_LOGD("%s:success..", __func__);
  197. return ret;
  198. }
  199. int32_t HdfWifiDriverDispatch(struct HdfDeviceIoClient *client, int id, struct HdfSBuf *data, struct HdfSBuf *reply)
  200. {
  201. if (client == NULL) {
  202. return HDF_ERR_INVALID_PARAM;
  203. }
  204. return WifiCmdProcess(client->device, id, data, reply);
  205. }
  206. static int HdfWifiDriverBind(struct HdfDeviceObject *dev)
  207. {
  208. if (dev == NULL) {
  209. return HDF_FAILURE;
  210. }
  211. static struct IDeviceIoService wifiService = {
  212. .object.objectId = 1,
  213. .Dispatch = HdfWifiDriverDispatch,
  214. };
  215. dev->service = &wifiService;
  216. return HDF_SUCCESS;
  217. }
  218. static void HdfWifiDriverRelease(struct HdfDeviceObject *object)
  219. {
  220. (void)object;
  221. }
  222. struct HdfDriverEntry g_hdfWifiEntry = {
  223. .moduleVersion = 1,
  224. .Bind = HdfWifiDriverBind,
  225. .Init = HdfWifiDriverInit,
  226. .Release = HdfWifiDriverRelease,
  227. .moduleName = "HDF_WIFI"
  228. };
  229. HDF_INIT(g_hdfWifiEntry);