小安派新建工程,连接WiFi

[复制链接]
查看1866 | 回复14 | 2023-9-29 09:26:06 | 显示全部楼层 |阅读模式

BL模组的SDK文件的examples里面的例程都是用指令开启某个功能的,在实际使用过程中应该都是模组自动化执行逻辑,人为干预越少越好

首先新建一个属于自己的工程文件夹

我是将BL的SDK放在了自己间的一个文件夹里面,自己的工程文件夹和SDK同级目录,如下 SMART_HOME文件夹是我的工程文件夹 image1.png

根据 Project_basic工程结构建立文件夹,当然也可以根据自己的习惯随便建立,只要最后在 CMakeLists.txt文件里面包含就可以,build文件夹无需自己建立,在后面执行 make时会自动生成,将 Project_basic文件夹内的 CMakeLists.txt、flash_prog_cfg.ini、Makefile、proj.conf四个文件复制到自己的工程: image2.png 我这边直接将 wifi和config文件一起复制过来了,主要是WiFi的初始化和对FreeRtos的移植,完成后需要将 flash_prog_cfg.ini文件里面最后部分修改成自己的工程名称,我的工程名称是 SMART_HOME

[FW]
filedir = ./build/build_out/SMART_HOME_$(CHIPNAME).bin
address = 0x10000

修改 CMakeLists.txt文件引用WiFi的头文件

# *************************************************************#
# 工程源文件配置
# Engineering Source File Configuration
# *************************************************************#

# ############################ 保持默认 #######################
# #########################  Keep default ####################
cmake_minimum_required(VERSION 3.15)

include(proj.conf)

find_package(bouffalo_sdk REQUIRED HINTS $ENV{BL_SDK_BASE})

# #############################################################

# 搜集所有的C文件( Collect source files)
file(GLOB_RECURSE sources "${CMAKE_CURRENT_SOURCE_DIR}/components/*.c")

# 添加头文件的引用路径(Add .h include directories)
sdk_add_include_directories(main config config)
sdk_add_include_directories(main config components/wifi)
# sdk_add_include_directories(main config components/uart)
# sdk_add_include_directories(main config components/freertos)

# 把C文件添加到工程里(Add .c file to the project)
target_sources(app PRIVATE ${sources})

# 设置main.c 的文件(Set the document source for main. c)
# 注意:不能和file(GLOB_RECURSE sources "${CMAKE_CURRENT_SOURCE_DIR}/components/*.c") 冲突(Note: Cannot conflict with “file(GLOB_RECURSE sources "${CMAKE_CURRENT_SOURCE_DIR}/components/*.c")”)
sdk_set_main_file(main/main.c)

# 设置工程名称,这个配置决定了编译出来的文件名。(Set the project name, which determines the compiled file name)
get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
project(${PROJECT_NAME})

修改 main文件,新建 连接WiFi的线程,直接贴代码吧

#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "mem.h"

#include <lwip/tcpip.h>
#include <lwip/sockets.h>
#include <lwip/netdb.h>

// #include "bl_fw_api.h"
// #include "wifi_mgmr_ext.h"
// #include "wifi_mgmr.h"

// #include "bflb_irq.h"
// #include "bflb_uart.h"

#include "bl616_glb.h"
#include "rfparam_adapter.h"

#include "board.h"
// #include "shell.h"

#include "wifi_event.h"

#define DBG_TAG "MAIN"
#include "log.h"


static TaskHandle_t wifi_test_task;

extern uint32_t sta_ConnectStatus;


static void wifi_test(void *pvParameters)
{
    uint8_t ret = 0;

    static char *ssid = "qop";                  // TODO: 请在这里填上自己 WiFi 的名字和密码
    static char *password = "pzypzy888888";     // TODO: 请在这里填上自己 WiFi 的名字和密码
    ret = wifi_connect(ssid, password);
    LOG_I("ret = %d\r\n", ret);

    while (1)
    {
        LOG_I("[wifi_test] ================= \r\n");
        vTaskDelay(10000);
    }
}


int main(void)
{
    board_init();

    tcpip_init(NULL, NULL);
    wifi_start_firmware_task();

    xTaskCreate(wifi_test, "wifi_test", 1024, NULL, 15, &wifi_test_task);

    vTaskStartScheduler();

    while (1) {
    }
}

编译

首次打开终端,进入aithinker_Ai-M6X_SDK

. install.sh
. export.sh

然后进入我们的工程文件夹执行 make image3.png 编译成功: image4.png

下载进板子后连接板子串口,使用串口助手或者PUTTY看串口日志,最后出现 CODE_WIFI_ON_GOT_IP说明WiFi连接成功。

[2023-09-29 09:13:09.246]# RECV ASCII>

  ____               __  __      _       _       _   
 |  _ \             / _|/ _|    | |     | |     | |  
 | |_) | ___  _   _| |_| |_ __ _| | ___ | | __ _| |__  
 |  _ < / _ \| | | |  _|  _/ _` | |/ _ \| |/ _` | '_ \ 
 | |_) | (_) | |_| | | | || (_| | | (_) | | (_| | |_) |
 |____/ \___/ \__,_|_| |_| \__,_|_|\___/|_|\__,_|_.__/ 

Build:08:24:39,Sep 29 2023
Copyright (c) 2022 Bouffalolab team
=========== flash cfg ==============
jedec id   0xC84017
mid            0xC8
iomode         0x04
clk delay      0x01
clk invert     0x01
read reg cmd0  0x05
read reg cmd1  0x35
write reg cmd0 0x01
write reg cmd1 0x31
qe write len   0x01
cread support  0x01
cread code     0x20
burst wrap cmd 0x77
=====================================
dynamic memory init success, ocram heap size = 251 Kbyte 
sig1:ffffffff
sig2:0000f32f
cgen1:9f7ffffd
lwip init done
sys_mbox_new done!
sys_mutex_new done!
tcpip thread init done!
[I][WIFI EVENT] Starting wifi ...[I][rfparam] xtal value 40000000
[I][rfparam] pwr_mode is bf
Empty slot:0
No written slot found
[I][rfparam] no pwr_offset in efuse
[I][rfparam] tlv wlan pwr_offset[14]: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,
[I][rfparam] wlan pwr_offset[14]: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,
Empty slot:0
No written slot found
[I][rfparam] no lp pwr_offset in efuse
[I][rfparam] tlv wlan lp pwr_offset[14]: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,
[I][rfparam] wlan lp pwr_offset[14]: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,


[2023-09-29 09:13:09.316]# RECV ASCII>

Empty slot:0
No written slot found
[I][rfparam] no bz pwr_offset in efuse
[I][rfparam] tlv bz pwr_offset[5]: 0,0,0,0,0,
[I][rfparam] bz pwr_offset[5]: 0,0,0,0,0,
[I][rfparam] pwr_11b[4]: 20,20,20,20,
[I][rfparam] pwr_11g[8]: 18,18,18,18,18,18,16,16,
[I][rfparam] pwr_11n_ht20[8]: 18,18,18,18,18,16,15,15,
[I][rfparam] pwr_11n_ht40[8]: 18,18,18,18,18,16,15,14,
[I][rfparam] pwr_11ac_vht20[10]: 18,18,18,18,18,16,15,15,15,14,
[I][rfparam] pwr_11ac_vht40[10]: 18,18,18,18,18,16,15,14,14,13,
[I][rfparam] pwr_11ac_vht80[10]: 18,18,18,18,18,15,14,13,13,12,
[I][rfparam] pwr_11ax_he20[12]: 18,18,18,18,18,16,15,15,15,14,13,13,
[I][rfparam] pwr_11ax_he40[12]: 18,18,18,18,18,16,15,14,14,13,12,12,
[I][rfparam] pwr_11ax_he80[12]: 18,18,18,18,18,15,14,13,13,12,11,11,
[I][rfparam] pwr_11ax_he160[12]: 18,18,18,18,18,15,14,13,12,11,10,10,
[I][rfparam] capcode mode is MF
Empty slot:1
[I][rfparam] efuse capcode_in 32,capcode_out 32
[I][rfparam] capcode_in 32,capcode_out 32
[I][rfparam] tcal.en_tcal = 0
[I][rfparam] tcal.linear_or_follow = 1
[I][rfparam] tcal.Tchannels[5]: 2412,2427,2442,2457,2472,
[I][rfparam] tcal.Tchannel_os[5]: 180,168,163,160,157,
[I][rfparam] tcal.Tchannel_os_low[5]: 199,186,170,165,160,
[I][rfparam] tcal.Troom_os = -1
[I][rfparam] pwr_ble = 13 
[I][rfparam] pwr_bt[3]: 10,8,8,
[I][rfparam] pwr_zigbee = 13 
[I][rfparam] country_code = 86 


[2023-09-29 09:13:09.486]# RECV ASCII>
[I][WIFI EVENT] PHY RF init success!get sta mac: b4:0e:cf:39:cb:d1

get ap mac: b4:0e:cf:39:cb:d1

[I][WIFI EVENT] [APP] [EVT] wifi_event_handlr, CODE_WIFI_ON_INIT_DONEwifi_mgmr_set_country_code:code  CN
[I][WIFI EVENT] [APP] [EVT] wifi_event_handler, CODE_WIFI_ON_MGMR_DONEstart connecting ... 
[I][WIFI EVENT] Wating wifi connetExec key_mgmt WPA-PSK WPA-PSK-SHA256 SAE
Exec pairwise TKIP CCMP GCMP GCMP-256 CCMP-256
Exec group TKIP CCMP GCMP GCMP-256 CCMP-256
Exec ieee80211w 0
Exec scan_ssid 1
Exec ssid "qop"
Exec psk "pzypzy888888"


[2023-09-29 09:13:10.406]# RECV ASCII>
WPA network 0: created and configuredStarting AP scan for specific SSID: qop, ssid_len:3


[2023-09-29 09:13:13.316]# RECV ASCII>
?[WPA] CTRL-EVENT-BSS-ADDED 0 D2:23:A4:B6:EA:D0
?[WPA] CTRL-EVENT-SCAN-RESULTS 
?[WPA] Trying to associate with D2:23:A4:B6:EA:D0 (SSID='qop' freq=2412 MHz)
--- OPT rxu_mgmt_ind is detected
[FU]Set timer,state is 5,ongoing++
[FU]Tx cfm ongoing--, value is 0
[FL]RECV AUTH,sn:693
[FU]RECV AUTH,sn:693
[FU]Rx handler,state:5,fctl:0xb0
[FU]Clear timer in auth handler
[FU]Set timer,state is 8,ongoing++
[FU]Tx cfm ongoing--, value is 0
[FL]RECV ASSOCRSP,sn:694
[FU]RECV ASSOCRSP,sn:694
[FU]Rx handler,state:8,fctl:0x10
[FU]Clear timer in assoc rsp handler
[FU] rcv eapol 888e
?[WPA] Associated with D2:23:A4:B6:EA:D0
?[WPA] CTRL-EVENT-SUBNET-STATUS-UPDATE status=0
[FU] rcv eapol 888e
?[WPA] WPA: Key negotiation completed with D2:23:A4:B6:EA:D0 [PTK=CCMP GTK=CCMP]
?[WPA] CTRL-EVENT-CONNECTED - Connection to D2:23:A4:B6:EA:D0 completed [id=0 id_str=]
=================================================================
[AT][RX] Connection Status
[AT][RX]   status_code 0
[AT][RX]   reason_code 0
[AT][RX]   status detail: sm connect ind ok
[AT][RX]   MAC D2:23:A4:B6:EA:D0
[AT][RX]   vif_idx 1
[AT][RX]   ap_idx 0
[AT][RX]   ch_idx 0
[AT][RX]   qos 1
[AT][RX]   aid 6
[AT][RX]   band 2.4G
[AT][RX]   channel 1
=================================================================
[I][WIFI EVENT] [APP] [EVT] wifi_event_handler, CODE_WIFI_ON_CONNECTED==========================  Keyram Dump =========================
[id]   MAC Address     Key Len  VLan ID  Default        Ke

[2023-09-29 09:13:13.396]# RECV ASCII>
y Hexdump(16Bytes MAX)
[00] FF:FF:FF:FF:FF:FF  64-Bit    00        0
[01] FF:FF:FF:FF:FF:FF  64-Bit    00        0
[02] FF:FF:FF:FF:FF:FF  64-Bit    00        0
[03] FF:FF:FF:FF:FF:FF  64-Bit    00        0
[04] FF:FF:FF:FF:FF:FF  64-Bit    00        0
[05] FF:FF:FF:FF:FF:FF  64-Bit    01        0
[06] FF:FF:FF:FF:FF:FF  64-Bit    00        0
[07] FF:FF:FF:FF:FF:FF  64-Bit    00        0
[08] D2:23:A4:B6:EA:D0  64-Bit    01        0
[09] FF:FF:FF:FF:FF:FF  64-Bit    00        0
[10] FF:FF:FF:FF:FF:FF  64-Bit    00        0
[11] FF:FF:FF:FF:FF:FF  64-Bit    00        0
==========================  MAC Address =========================
    MAC B4:0E:CF:39:CB:D1 Mask 00:00:00:00:00:00
  BSSID D2:23:A4:B6:EA:D0 Mask 00:00:00:00:00:00
start dhcping ... 
wpa_supplicant_ctrl_iface_msg_cb: cmd send succeded, txt is CTRL-EVENT-CONNECTED - Connection to D2:23:A4:B6:EA:D0 completed [id=0 id_str=]
[I][WIFI EVENT] Wating wifi connet OK IP:192.168.43.180
 MASK: 255.255.255.0
 Gateway: 192.168.43.1


[2023-09-29 09:13:13.486]# RECV ASCII>
[I][WIFI EVENT] Wating wifi connet OK

[2023-09-29 09:13:13.586]# RECV ASCII>
[I][WIFI EVENT] Wating wifi connet OK

[2023-09-29 09:13:13.686]# RECV ASCII>
[I][WIFI EVENT] Wating wifi connet OK

[2023-09-29 09:13:13.786]# RECV ASCII>
[I][WIFI EVENT] Wating wifi connet OK

[2023-09-29 09:13:13.886]# RECV ASCII>
[I][WIFI EVENT] Wating wifi connet OK

[2023-09-29 09:13:13.976]# RECV ASCII>
[I][WIFI EVENT] [APP] [EVT] wifi_event_handler, CODE_WIFI_ON_GOT_IP[0] wl1: MAC=b4:0e:cf:39:cb:d1 ip=192.168.43.180/24 UP,CONNECTED
[I][MAIN] ret = 0
[I][MAIN] [wifi_test] =================

避坑

如果移植FreeRtos使用的是 aithinker_Ai_M6X_SDK\examples\freertos\FreeRTOSConfig.h,需要修改两个地方

58行

#define configMAX_PRIORITIES                    (7)

修改成

#define configMAX_PRIORITIES                    (32)

86行

#define configTIMER_TASK_STACK_DEPTH            (configMINIMAL_STACK_SIZE)
修改成

#define configTIMER_TASK_STACK_DEPTH            (1024)

==========================================

本帖被以下淘专辑推荐:

回复

使用道具 举报

王乐乐 | 2023-9-29 10:20:42 | 显示全部楼层
打卡
回复

使用道具 举报

496199544 | 2023-9-29 10:28:13 来自手机 | 显示全部楼层
学习
回复

使用道具 举报

鱼《》《》 | 2023-9-29 12:49:41 | 显示全部楼层
不错
回复

使用道具 举报

WangChong | 2023-9-29 16:14:04 | 显示全部楼层
学习了
回复

使用道具 举报

WangChong | 2023-9-29 16:23:52 | 显示全部楼层
学习了
回复

使用道具 举报

ifwz1729 | 2023-9-29 17:32:34 | 显示全部楼层
学废了
知足常乐
回复

使用道具 举报

lsrly | 2023-9-29 17:56:37 | 显示全部楼层
学习
好好学习,努力挣钱,专心
回复

使用道具 举报

小浪先生 | 2023-10-7 22:01:47 | 显示全部楼层
怎么啥也没有
回复 支持 反对

使用道具 举报

Ject | 2023-10-9 22:53:31 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则