小安派CAM-U--从helloword工程从零开始搭建连接wifi

1.找到例程helloword

2.学习常用外设的配置,因为没有CAM-U的,找其他板子的外设教程也一样

【新提醒】小安派S1教程合集 - 物联网开发者社区-安信可论坛 - Powered by Discuz! (ai-thinker.com)

3.添加外设到main,自行根据上面的教程添加外设

4.关键点:如何加入rtos,加入lwip库,配网流程

4.1借鉴AiPi-Cam工程设置,复制所需的部件宏到helloword

复制后如图,进行编译make,会提示不少报错,添加库文件,添加wifi回调函数,一些关键句柄,可借鉴代码如下,最中编译成功

/* *****************************************************************************

 Copyright (C) : 2000-2023, unit

 ******************************************************************************
 File Name     : main.c
 Version       : ver 1.0
 Author        : phh
 Created       : 2023/11/20
 Last Modified :
 Description   :
 Function List :
                 main
 History       :
 1.Date        : 2023/11/20
   Author      : phh
   Modification: Created file

***************************************************************************** */

/* ----------------------------------------------*
 * header files                                  *
 *---------------------------------------------- */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.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 "bflb_mtimer.h"
#include "board.h"
#include "bflb_gpio.h"
#include "Bsp_Flash.h"

#include "log.h"


#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */

/* ----------------------------------------------*
 * macro                                         *
 *---------------------------------------------- */
#define DBG_TAG "MAIN"
#define WIFI_STACK_SIZE  (1536)
#define TASK_PRIORITY_FW (16)

/* ----------------------------------------------*
 * type definition                               *
 *---------------------------------------------- */

/* ----------------------------------------------*
 * module-wide global variables                  *
 *---------------------------------------------- */
static uint8_t write_buf[16];
static uint8_t read_buf[16];
static wifi_conf_t conf =
{
    .country_code = "CN",
};
static TaskHandle_t wifi_fw_task;

/* ----------------------------------------------*
 * modul global variables                        *
 *---------------------------------------------- */

/* ----------------------------------------------*
 * internal routine prototypes                   *
 *---------------------------------------------- */

/* ----------------------------------------------*
 * routines' implementations                     *
 *---------------------------------------------- */


int wifi_start_firmware_task(void)
{
    LOG_I("Starting wifi ...\r\n");

    /* enable wifi clock */

    GLB_PER_Clock_UnGate(GLB_AHB_CLOCK_IP_WIFI_PHY | GLB_AHB_CLOCK_IP_WIFI_MAC_PHY | GLB_AHB_CLOCK_IP_WIFI_PLATFORM);
    GLB_AHB_MCU_Software_Reset(GLB_AHB_MCU_SW_WIFI);

    /* set ble controller EM Size */

    GLB_Set_EM_Sel(GLB_WRAM160KB_EM0KB);

    if (0 != rfparam_init(0, NULL, 0))
    {
        LOG_I("PHY RF init failed!\r\n");
        return 0;
    }

    LOG_I("PHY RF init success!\r\n");

    /* Enable wifi irq */

    extern void interrupt0_handler(void);
    bflb_irq_attach(WIFI_IRQn, (irq_callback)interrupt0_handler, NULL);
    bflb_irq_enable(WIFI_IRQn);

    xTaskCreate(wifi_main, (char*)"fw", WIFI_STACK_SIZE, NULL, TASK_PRIORITY_FW, &wifi_fw_task);

    return 0;
}


void wifi_event_handler(uint32_t code)
{
    switch (code)
    {
    case CODE_WIFI_ON_INIT_DONE:
    {
        LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_INIT_DONE\r\n", __func__);
        wifi_mgmr_init(&conf);
    }
    break;
    case CODE_WIFI_ON_MGMR_DONE:
    {
        LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_MGMR_DONE\r\n", __func__);
		//wifi_sta_connect(char *ssid, char *key, char *bssid, char *akm_str, uint8_t pmf_cfg, uint16_t freq1,  freq2,  use_dhcp)
		wifi_sta_connect("huawei88", "12345678", NULL, NULL, 0, 0, 0, 1);

	}
    break;
    case CODE_WIFI_ON_SCAN_DONE:
    {
        LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_SCAN_DONE\r\n", __func__);
        wifi_mgmr_sta_scanlist();
    }
    break;
    case CODE_WIFI_ON_CONNECTED:
    {
        LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_CONNECTED\r\n", __func__);

    }
    break;
    case CODE_WIFI_ON_GOT_IP:
    {
        LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_GOT_IP\r\n", __func__);
    }
    break;
    case CODE_WIFI_ON_DISCONNECT:
    {
        LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_DISCONNECT\r\n", __func__);
    }
    break;
    case CODE_WIFI_ON_AP_STARTED:
    {
        LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_AP_STARTED\r\n", __func__);
    }
    break;
    case CODE_WIFI_ON_AP_STOPPED:
    {
        LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_AP_STOPPED\r\n", __func__);
    }
    break;
    case CODE_WIFI_ON_AP_STA_ADD:
    {
        LOG_I("[APP] [EVT] [AP] [ADD] %lld\r\n", xTaskGetTickCount());
    }
    break;
    case CODE_WIFI_ON_AP_STA_DEL:
    {
        LOG_I("[APP] [EVT] [AP] [DEL] %lld\r\n", xTaskGetTickCount());
    }
    break;
    default:
    {
        LOG_I("[APP] [EVT] Unknown code %u \r\n", code);
    }
    }
}



int main(void)
{
    board_init();
    //bl库使用方式,先获取句柄
    struct bflb_device_s* gpio;
    gpio = bflb_device_get_by_name("gpio");
    //直接配置初始化gpio34,输出,浮空,驱动等级0
    bflb_gpio_init(gpio, GPIO_PIN_34, GPIO_OUTPUT|GPIO_FLOAT|GPIO_SMT_DIS|GPIO_DRV_0);

    struct bflb_device_s* uart1;

    Bsp_Uart1_Init(  );
    uart1 = bflb_device_get_by_name("uart1");

    memset(write_buf, 0, sizeof(write_buf));
    memset(read_buf, 0, sizeof(read_buf));
    //清空缓存数组

    strcpy(write_buf,"HelloWorld!");

	Bsp_Write_Flash( write_buf, sizeof(write_buf) );
	Bsp_Read_Flash( read_buf,sizeof(read_buf) );

    printf("read_buf:%s\r\n",read_buf);
	
	//tcp,wifi初始化
    tcpip_init(NULL, NULL);
    wifi_start_firmware_task();


	//启动系统
    vTaskStartScheduler();

    while (1)
    {
    }


    while (1)
    {
        if ( 1 )
        {
            LOG_F("uart1 bflb_gpio_set\r\n");
            bflb_uart_put(uart1,"hello",5);
            bflb_gpio_set(gpio, GPIO_PIN_34);
            //这是裸机延时,带阻塞
            bflb_mtimer_delay_ms(5000);

        }
        if ( 1 )
        {

            LOG_I("uart1 bflb_gpio_reset\r\n");
            bflb_uart_put(uart1,"hello",5);
            bflb_gpio_reset(gpio, GPIO_PIN_34);
            //这是逻辑延时,不知道是否带阻塞
            bflb_mtimer_delay_ms(5000);
        }



    }
}


#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */

4.2关键点2:如何连接wifi,借鉴例程中wifi_tcp(资料说明严重不足,无力吐槽)

从别的例程发现,例程启用了shell,并且把函数调用变成命令

配网流程总结如下:

1.初始化

    tcpip_init(NULL, NULL);
    wifi_start_firmware_task();

2.在回调函数中进行wifi连接

void wifi_event_handler(uint32_t code)

    case CODE_WIFI_ON_MGMR_DONE:
    {
        LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_MGMR_DONE\r\n", __func__);
        //wifi_sta_connect(char *ssid, char *key, char *bssid, char *akm_str, uint8_t pmf_cfg, uint16_t freq1,  freq2,  use_dhcp)
        wifi_sta_connect("huawei88", "12345678", NULL, NULL, 0, 0, 0, 1);

    }
    break;

3.重新编译,烧录,打印现象

4.3配网成功后,后续加入TCP,HTTP,MQTT等

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值