短笛君 发表于 2026-11-6 00:04:24

【瑞萨BLE/WIFI模块测评】DA16200 WiFi 模块连接网络

!(https://www.eefocus.com/forum/data/attachment/forum/202611/05/234953opnmleuz4a4i4plm.jpg)

本次测评的是DA16200 来自瑞萨电子的wifi模块

其主打的是低功耗与安全性
官网的特点描述如下
本期测评主要围绕入网展开

* 低功耗 Wi-Fi
* 可为大多数应用提供一年以上的电池寿命
* 卓越的续航时间
* Wi-Fi 层的高安全性
* 上层堆栈的高安全性
* 即使持续连接 Wi-Fi,也可保持较长的电池续航时间
* 完全卸载,可在芯片上运行整个堆栈
* 无需任何外部 CPU 或 MCU 即可运行
* 可与外部 MCU 一起运行(可选)
* 多种睡眠模式,适用于不同的应用
* 可以 SoC 或模块形式提供
* 在芯片上处理模拟或数字传感器信息

本次代码框架主要参考社区中的WIFI AT版本驱动 通过串口发送AT指令来控制模块连接wifi进行包发送 这也是目前wifi模块的主流用法


!(https://www.eefocus.com/forum/data/attachment/forum/202611/05/235101qvjfea6u6g7zeiv4.png)

## 接线

| RA4M2       | DA16200   |
| ------------- | ----------- |
| P411 / TXD0 | RXD       |
| P410 / RXD0 | TXD       |
| P608      | RESET   |
| 3.3V / GND| VCC / GND |

TX/RX 必须交叉。P608 是低有效复位,运行时保持高:

```c
/*
   * PMOD2 pin 8 is connected to the DA16200 RESET signal through P608.
   * Keep it high so the active-low reset input remains released.
   */
    fsp_err_t err = R_IOPORT_PinCfg(&g_ioport_ctrl,
                                    BSP_IO_PORT_06_PIN_08,
                                    IOPORT_CFG_PORT_DIRECTION_OUTPUT | IOPORT_CFG_PORT_OUTPUT_HIGH);
```

## 初始化顺序

模组初始化需要一定时间 先释放模块复位,再打开串口,最后等 AT 固件启动。顺序反了会出现"第一条 AT 没反应"的假故障。

```c
/*
   * PMOD2 pin 8 is connected to the DA16200 RESET signal through P608.
   * Keep it high so the active-low reset input remains released.
   */
    fsp_err_t err = R_IOPORT_PinCfg(&g_ioport_ctrl,
                                    BSP_IO_PORT_06_PIN_08,
                                    IOPORT_CFG_PORT_DIRECTION_OUTPUT | IOPORT_CFG_PORT_OUTPUT_HIGH);
    if (FSP_SUCCESS != err)
    {
      g_wifi_last_error = err;
      return err;
    }
```

```c
err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg);
    if (FSP_SUCCESS != err)
    {
      g_wifi_last_error = err;
      return err;
    }

    g_wifi_uart_open = true;

    /* Allow the DA16200 AT firmware to finish booting before the first command,
   * in 100 ms steps so the status screen keeps ticking. */
    for (uint32_t elapsed_ms = 0U; elapsed_ms < WIFI_MODULE_BOOT_DELAY_MS; elapsed_ms += 100U)
    {
      wifi_transport_poll(elapsed_ms);
      R_BSP_SoftwareDelay(100U, BSP_DELAY_UNITS_MILLISECONDS);
    }
```

## 模块握手

```c
bool wifi_transport_test(void)
{
    return wifi_transport_command(g_wifi_at_test_command,
                                  (uint32_t) (sizeof(g_wifi_at_test_command) - 1U),
                                  WIFI_AT_RESPONSE_TIMEOUT_MS,
                                  NULL,
                                  false);
}
```

串口上就是一来一回,代码只认末尾的 OK 行:

```
>> AT
<< \r\nOK\r\n
```

## wifi扫描

```c
bool wifi_transport_scan(void)
{
    g_wifi_scan_ok = wifi_transport_command(g_wifi_scan_command,
                                          (uint32_t) (sizeof(g_wifi_scan_command) - 1U),
                                          WIFI_SCAN_RESPONSE_TIMEOUT_MS,
                                          "+WFSCAN:",
                                          false);

    return g_wifi_scan_ok;
}
```

应答是一串以 `+WFSCAN:` 开头的行,每行一个热点,最后以 OK 收尾。把 `+WFSCAN:` 作为必需 token 传进去,可以区分"回了列表"和"只回了个 OK"——后者说明命令被接受但没扫到东西。


## wifi入网

先无条件退网,保证重复调试的结果可复现:

```c
bool wifi_transport_join(void)
{
    /* Make repeated debug runs deterministic even if the module stayed connected. */
    (void) wifi_transport_command(g_wifi_disconnect_command,
                                  (uint32_t) (sizeof(g_wifi_disconnect_command) - 1U),
                                  WIFI_AT_RESPONSE_TIMEOUT_MS,
                                  NULL,
                                  false);
    R_BSP_SoftwareDelay(500U, BSP_DELAY_UNITS_MILLISECONDS);

    g_wifi_join_ok = wifi_transport_command(g_wifi_join_command,
                                          (uint32_t) (sizeof(g_wifi_join_command) - 1U),
                                          WIFI_JOIN_RESPONSE_TIMEOUT_MS,
                                          "+WFJAP:1,",
                                          true);

    if (g_wifi_join_ok)
    {
      wifi_join_ip_extract();
      /* Allow DHCP and routing information to settle after association. */
      R_BSP_SoftwareDelay(WIFI_NETWORK_SETTLE_DELAY_MS,
                            BSP_DELAY_UNITS_MILLISECONDS);
    }

    return g_wifi_join_ok;
}
```

`+WFJAP:1,` 的 1 是关联成功,失败是 `+WFJAP:0,`,已在第 ③ 条判断里当失败处理,所以不用干等 30 秒。退网后的 500 ms 是给模块收尾的,省掉这一步偶尔会连不上——模块还停在旧连接状态。

## 取 IP

应答形如:

```
+WFJAP:1,"MyHome",192.168.1.23,...
```

跳过引号里的 SSID,停在 IP 前的逗号,再按数字和点收取:

```c
static void wifi_join_ip_extract(void)
{
    char const success_token[] = "+WFJAP:1,";
    int32_t const token_position = wifi_response_find(success_token);
    uint32_t ip_length = 0U;

    g_wifi_ip_address = '\0';

    if (token_position < 0)
    {
      return;
    }

    uint32_t response_index = (uint32_t) token_position +
                              (uint32_t) (sizeof(success_token) - 1U);

    /* Skip the quoted SSID and stop at the comma before the IPv4 address. */
    while ((response_index < g_wifi_at_response_length) &&
         (g_wifi_at_response != ','))
    {
      response_index++;
    }

    if (response_index < g_wifi_at_response_length)
    {
      response_index++;
    }

    while ((response_index < g_wifi_at_response_length) &&
         (ip_length < (sizeof(g_wifi_ip_address) - 1U)))
    {
      char const data = g_wifi_at_response;

      if (!(((data >= '0') && (data <= '9')) || (data == '.')))
      {
            break;
      }

      g_wifi_ip_address = data;
      ip_length++;
      response_index++;
    }

    g_wifi_ip_address = '\0';
}
```

不用 `sscanf` 有两个原因:堆是 0,不想为了一行解析把 `scanf` 拉进来;另外按"跳过 SSID 到 逗号"的方式走,SSID 里带逗号也不会错位。

实测效果如下
模组上电后会先被MCU初始化
!(https://www.eefocus.com/forum/data/attachment/forum/202611/06/000151udvx4qinv4qzqnnr.jpg)

等待模块初始化成功

!(https://www.eefocus.com/forum/data/attachment/forum/202611/06/000212ms7xbjfsd2lihssb.jpg)
模块回复OK后进入下一步 会按照先前写入的ssid连接

!(https://www.eefocus.com/forum/data/attachment/forum/202611/06/000305qv136okw8kv0v4wi.jpg)

成功连接网络后会根据获取到的ip进行测试
!(https://www.eefocus.com/forum/data/attachment/forum/202611/06/000331f9ukykwc3qdhdqhu.jpg)
测试成功后会显示测试OK
!(https://www.eefocus.com/forum/data/attachment/forum/202611/06/000410w8xcb5ico4y08tk8.jpg)




页: [1]
查看完整版本: 【瑞萨BLE/WIFI模块测评】DA16200 WiFi 模块连接网络