发帖
3 0 0

Ai-WB2的SPI测试

WildboarG
论坛元老

17

主题

146

回帖

3286

积分

论坛元老

积分
3286
Ai-WB2系列 32 3 昨天 21:34

WB2 SPI测试


Static Badge Static Badge

闲言碎语

最近水逆,被老板叼难,头都整大了,有没有老哥有工作推荐.

关于hosal库


测试wb2的spi发现一直溢出,用的是 WB2-12Fkit开发板,跑园长的demo点亮ws2812卡在了spi初始化,发现论坛也有大佬卡在了初始化.

使用printf大法一点点跟踪,发现具体卡在了GPIO的初始化这个函数

static void hosal_spi_gpio_init(hosal_spi_dev_t *arg)
{

    if (!arg) {
        blog_error("arg err.\r\n");
        return;
    }

    GLB_GPIOgeng huan_Type gpiopins[4];
    gpiopins[0] = 22;  //这里注释掉就可以初始化完成
    gpiopins[1] = arg->config.pin_clk;
    gpiopins[2] = arg->config.pin_mosi;
    gpiopins[3] = arg->config.pin_miso;
    GLB_GPIO_Func_Init(GPIO_FUN_SPI,gpiopins,sizeof(gpiopins)/sizeof(gpiopins[0]));

    if (arg->config.mode == 0) {
        GLB_Set_SPI_0_ACT_MOD_Sel(GLB_SPI_PAD_ACT_AS_MASTER);
    } else {
        GLB_Set_SPI_0_ACT_MOD_Sel(GLB_SPI_PAD_ACT_AS_SLAVE);
    }

    return;
}

不明白为什么将SPI的GPIO除了用到的引脚还初始化22号引脚,看手册上12F不能用这个引脚,把它给注释掉了,然后只初始化CLK MOSI MISO这三个引脚,然后就能正常完初始化.但是发送数据依旧报溢出的问题.

不死心,自己写一个spi发送的demo依然溢出.

更换SDK

折腾了两天解决不了,然后尝试换博流的SDK,看文档说对bl602/bl604支持SPI但未测试,心凉一半.
写了发送1234...的demo用编译烧录,又又出错了,再次搬出printf大法跟踪,定位到 board.c 文件的这里

void board_spi0_gpio_init()
{
    // struct bflb_device_s *gpio;

    // gpio = bflb_device_get_by_name("gpio");
    // bflb_gpio_init(gpio, GPIO_PIN_18, GPIO_FUNC_SPI0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
    // bflb_gpio_init(gpio, GPIO_PIN_19, GPIO_FUNC_SPI0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
    // bflb_gpio_init(gpio, GPIO_PIN_20, GPIO_FUNC_SPI0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
}

使用SPI的引脚引脚需要自己实现初始化(它这里给注释掉了,表示需要按照实际的引脚来用).

查手册,看支持的SPI引脚不少,选了IO3 IO4 IO5

再次烧录测试,能够成功发送数据,接受方也表示没毛病.

但是神奇的事情发生了,再次用 Ai-Thinker-WB2sdk烧录程序,怎么都烧录不进去,用 bouffalo的sdk就可以正常烧录,有大佬知道这是什么原因吗?

然后尝试点亮WS2812灯珠的示例:

#include "bflb_mtimer.h"
#include "bflb_spi.h"
#include "board.h"
#include "bflb_gpio.h"
#include <stdio.h>
#define SPI_MASTER_CASE    0
#define SPI_SLAVE_CASE     1

#define SPI_CASE_SELECT    SPI_MASTER_CASE

// SPI数据为0的时序
#define SPI_NEO0           ((uint8_t)0b11000000)
// SPI数据为1的时序
#define SPI_NEO1           ((uint8_t)0b11111100)

#define BITS_PER_LED_COLOR (sizeof(color_t) * 8)

#define BUFF_LEN           (8 * 1024)

uint32_t tx_buff[BUFF_LEN / 4];
uint32_t rx_buff[BUFF_LEN / 4];

struct bflb_device_s *spi0;

typedef struct {
    uint8_t r;
    uint8_t g;
    uint8_t b;
} color_t;

typedef struct {
    uint16_t led_counts;      // LED数量
    uint8_t *color_datas;     // 24位颜色数据
    uint16_t color_data_size; // 颜色数据长度
    color_t *led_colors;      // LED颜色
    bool inited;              // 是否初始化,1表示已经初始化,0则表示未初始化
} ws2812b_t;

ws2812b_t ws2812b = {
    .inited = false,
    .led_counts = 1,
};

color_t RED = { 255, 0, 0 };
color_t GREEN = { 0, 255, 0 };
color_t BLUE = { 0, 0, 255 };

void ws2812b_init(ws2812b_t *ws2812b)
{
    if (ws2812b == NULL) {
        return;
    }
    // 分配24位颜色数据内存
    ws2812b->color_datas = (uint8_t *)malloc(
        ws2812b->led_counts * BITS_PER_LED_COLOR + 32);
    // 分配LED颜色数据内存
    ws2812b->led_colors = (color_t *)malloc(
        sizeof(color_t) * ws2812b->led_counts);
    if (ws2812b->color_datas && ws2812b->led_colors) {
        ws2812b->inited = true;
        ws2812b->color_data_size = ws2812b->led_counts * BITS_PER_LED_COLOR; //+32
    } else {
        ws2812b->inited = false;
    }
}
// 生成24位颜色数据序列
void __build_led_color_data(ws2812b_t *ws2812b)
{
    for (int i = 0; i < ws2812b->led_counts; i++) {
        uint8_t m = 0b10000000;
        for (int b = 0; b < 8; b++) {
            ws2812b->color_datas[BITS_PER_LED_COLOR * i + b] =
                ws2812b->led_colors[i].g & m ? SPI_NEO1 : SPI_NEO0;
            m >>= 1u;
        }

        m = 0b10000000;
        for (int b = 0; b < 8; b++) {
            ws2812b->color_datas[BITS_PER_LED_COLOR * i + b + 8] =
                ws2812b->led_colors[i].r & m ? SPI_NEO1 : SPI_NEO0;
            m >>= 1u;
        }

        m = 0b10000000;
        for (int b = 0; b < 8; b++) {
            ws2812b->color_datas[BITS_PER_LED_COLOR * i + b + 16] =
                ws2812b->led_colors[i].b & m ? SPI_NEO1 : SPI_NEO0;
            m >>= 1u;
        }
    }
}

void ws2812b_set_color(ws2812b_t *ws2812b, uint16_t index, color_t color)
{
    if (index >= ws2812b->led_counts && ws2812b->inited) {
        return;
    }
    ws2812b->led_colors[index] = color;
}

void ws2812b_show(ws2812b_t *ws2812b)
{
    if (!ws2812b->inited) {
        return;
    }
    __build_led_color_data(ws2812b);
    //hosal_spi_send(&spi, ws2812b->color_datas,ws2812b->color_data_size,1000);
    printf("countsize=%d\r\n", ws2812b->color_data_size);
    for (int i = 0; i < ws2812b->color_data_size; i++) {
        bflb_spi_poll_send(spi0, ws2812b->color_datas[i]);
    }
}
void board_spi0_gpiomy_init()
{
    struct bflb_device_s *gpio;

    gpio = bflb_device_get_by_name("gpio");
    bflb_gpio_init(gpio, GPIO_PIN_3, GPIO_FUNC_SPI0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
    bflb_gpio_init(gpio, GPIO_PIN_4, GPIO_FUNC_SPI0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
    bflb_gpio_init(gpio, GPIO_PIN_5, GPIO_FUNC_SPI0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
}
/* poll test func */
/* main */
int main(void)
{
    board_init();
    board_spi0_gpiomy_init();

    struct bflb_spi_config_s spi_cfg = {
#if (SPI_CASE_SELECT == SPI_MASTER_CASE)
        .freq = 8 * 1000 * 1000,
        .role = SPI_ROLE_MASTER,
#else
        .freq = 32 * 1000 * 1000,
        .role = SPI_ROLE_SLAVE,
#endif
        .mode = SPI_MODE3,
        .data_width = SPI_DATA_WIDTH_8BIT,
        .bit_order = SPI_BIT_MSB,
        .byte_order = SPI_BYTE_LSB,
        .tx_fifo_threshold = 0,
        .rx_fifo_threshold = 0,
    };

    spi0 = bflb_device_get_by_name("spi0");
    bflb_spi_init(spi0, &spi_cfg);
    //uint32_t mess = 0;
    printf("start\r\n");
    // bflb_spi_poll_send(spi0, mess);
    ws2812b_init(&ws2812b);
    printf("ws2812b task start...\r\n");
    while (1) {
        //      bflb_mtimer_delay_ms(2000); /* delay for slave device prepare ok */
        //      bflb_spi_poll_send(spi0, mess);
        //      mess++;
        for (size_t i = 0; i < ws2812b.led_counts; i++) {
            ws2812b_set_color(&ws2812b, i, RED);
        }
        ws2812b_show(&ws2812b);

        bflb_mtimer_delay_ms(1000);

        for (size_t i = 0; i < ws2812b.led_counts; i++) {
            ws2812b_set_color(&ws2812b, i, GREEN);
        }
        ws2812b_show(&ws2812b);

        bflb_mtimer_delay_ms(1000);

        for (size_t i = 0; i < ws2812b.led_counts; i++) {
            ws2812b_set_color(&ws2812b, i, BLUE);
        }
        ws2812b_show(&ws2812b);

        bflb_mtimer_delay_ms(1000);
    }
}

编译烧录

make CHIP=BL602  BOARD=bl602dk
make flash CHIP=BL602  COMX=/dev/ttyUSB0

嘿,你猜怎么着,WB2-12F-kit上边的灯珠颗RGB5050灯珠,用单线协议没用,直接用高低电平控制的,只能用用逻辑分析仪检测一下控制逻辑是否正确.

mmexport1741698808608.png

mmexport1741698815197.png

已经解析出来WS2812协议了

ok

其他


其实我之前画过WB2-12f的测试版,要驱动IIC用的,上边刚好有一颗WS2812灯珠接在io5上.想把上边程序下载上去验证,但一直无法烧录(用 Ai-Thinker-WB2SDK就可以正常烧录程序),简直是倒反天罡.我就说水逆吧.

还有个问题,我没明白博流sdk 的spi如何为功能引脚指定GPIO引脚.求评论区大佬解答.

──── 0人觉得很赞 ────

使用道具 举报

你是使用开发板进行烧录的话   可以使用1.7.4版本的烧录软件,长按开发板上的烧录按钮同时再按复位按键让模块强制进入烧录模式,1.7.4版本的烧录软件下载地址:https://aithinker111.oss-cn-beij ... /v1.7.4-release.zip
5 小时前
123456 发表于 2025-3-12 08:43
你是使用开发板进行烧录的话   可以使用1.7.4版本的烧录软件,长按开发板上的烧录按钮同时再按复位按键让模 ...

用ai-thinker-wb2 的sdk 烧录wb2-12f-kit 按键烧录按键+复位 完全不起作用,用1.7.4烧录是可以烧录进去的   
make flash
****** Please SET BL60X_SDK_PATH ******
****** Trying SDK PATH [/home/wildboarg/github/Ai-Thinker-WB2]
Your configuration chipname is Ai-Thinker Ai-WB2 Wi-Fi&BLE Module
CC build_out/bl602_std/bl602_std/StdDriver/Src/bl602_glb.o
AR build_out/bl602_std/libbl602_std.a
CC build_out/hosal/bl602_hal/hosal_spi.o
AR build_out/hosal/libhosal.a
LD build_out/demo_iic.elf
Generating BIN File to /home/wildboarg/github/demo_iic/build_out/demo_iic.bin
Building Finish. To flash build output. run 'make flash' or:
c  ...
cd /home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool && env SDK_APP_BIN=/home/wildboarg/github/demo_iic/build_out/demo_iic.bin SDK_BOARD=evb SDK_NAME=demo_iic SDK_MEDIA_BIN= SDK_ROMFS_DIR= SDK_DTS= SDK_XTAL= BL_FLASH_TOOL_INPUT_PATH_cfg2_bin_input=/home/wildboarg/github/demo_iic/build_out/demo_iic.bin ./bflb_iot_tool-ubuntu --chipname=BL602 --baudrate=921600 --port=/dev/ttyUSB0 --pt=/home/wildboarg/github/demo_iic/img_conf/partition_cfg_2M.toml --dts=/home/wildboarg/github/demo_iic/img_conf/bl_factory_params_IoTKitA_40M.dts --firmware=/home/wildboarg/github/demo_iic/build_out/demo_iic.bin
['./bflb_iot_tool-ubuntu', '--chipname=BL602', '--baudrate=921600', '--port=/dev/ttyUSB0', '--pt=/home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/partition/partition_cfg_2M.toml', '--dts=/home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/device_tree/04-bl_factory_params_IoTKitA_40M-20220625.dts', '--boot2=/home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/builtin_imgs/boot2_isp_bl602_v6.5.1/boot2_iap_release.bin', '--firmware=/home/wildboarg/github/demo_iic/build_out/demo_iic.bin']
[10:05:43.285] -
[10:05:43.285] - ==================================================
[10:05:43.285] - Chip name is BL602
[10:05:43.285] - Serial port is /dev/ttyUSB0
[10:05:43.285] - Baudrate is 921600
[10:05:43.285] - Firmware is /home/wildboarg/github/demo_iic/build_out/demo_iic.bin
[10:05:43.285] - Partition Table is /home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/partition/partition_cfg_2M.toml
[10:05:43.285] - Device Tree is /home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/device_tree/04-bl_factory_params_IoTKitA_40M-20220625.dts
[10:05:43.285] - Boot2 is /home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/builtin_imgs/boot2_isp_bl602_v6.5.1/boot2_iap_release.bin
[10:05:43.285] - MFG is None
[10:05:43.285] - Media is None
[10:05:43.285] - Romfs Dir is None
[10:05:43.285] - ==================================================
[10:05:43.285] - ========= Interface is Uart =========
[10:05:43.286] - eflash loader bin is eflash_loader_40m.bin
[10:05:43.287] - ========= chip flash id: ef4015 =========
[10:05:43.288] - create partition.bin, pt_new is True
[10:05:43.291] - fw_boot_head_gen xtal: 40M
[10:05:43.291] - Create bootheader using /home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/img_create_iot/efuse_bootheader_cfg.ini
[10:05:43.291] - Updating data according to </home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/img_create_iot/efuse_bootheader_cfg.ini[BOOTHEADER_CFG]>
[10:05:43.292] - Created file len:176
[10:05:43.293] - Create efuse using /home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/img_create_iot/efuse_bootheader_cfg.ini
[10:05:43.293] - Updating data according to </home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/img_create_iot/efuse_bootheader_cfg.ini[EFUSE_CFG]>
[10:05:43.293] - Created file len:128
[10:05:43.294] - ========= sp image create =========
[10:05:43.294] - Image hash is b'60fa5a43cce1faf36a8567e05fa84f3da4ad1dc252b9424a8748b14236417f78'
[10:05:43.294] - Hash ignored
[10:05:43.294] - Header crc ignored
[10:05:43.294] - Write flash img
[10:05:43.296] - fw_boot_head_gen xtal: 40M
[10:05:43.296] - Create bootheader using /home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/img_create_iot/efuse_bootheader_cfg.ini
[10:05:43.296] - Updating data according to </home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/img_create_iot/efuse_bootheader_cfg.ini[BOOTHEADER_CFG]>
[10:05:43.297] - Created file len:176
[10:05:43.297] - Create efuse using /home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/img_create_iot/efuse_bootheader_cfg.ini
[10:05:43.298] - Updating data according to </home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/img_create_iot/efuse_bootheader_cfg.ini[EFUSE_CFG]>
[10:05:43.298] - Created file len:128
[10:05:43.299] - ========= sp image create =========
[10:05:43.299] - Image hash is b'87a0cbc0501253fa156d4807957bd8d85057dc6547dafeb246c84a996a74aa2b'
[10:05:43.299] - Header crc: b'31f7ae38'
[10:05:43.299] - Write flash img
[10:05:43.300] - FW Header is 176, 3920 still needed
[10:05:43.300] - FW OTA bin header is Done. Len is 4096
[10:05:43.304] - BL60X_OTA_Ver1.0
[10:05:43.307] - FW OTA bin is Done. Len is 87616
[10:05:43.321] - OTA XZ file len = 46992
[10:05:43.321] - Partiton len = 557056
[10:05:43.321] - BL60X_OTA_Ver1.0
[10:05:43.324] - FW OTA xz is Done
[10:05:43.324] - ========= eflash loader config =========
[10:05:43.327] - =========/home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/device_tree/04-bl_factory_params_IoTKitA_40M-20220625.dts ——> /home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/device_tree/ro_params.dtb=========
[10:05:43.374] - Version: eflash_loader_v2.4.3
[10:05:43.374] - Program Start
[10:05:43.374] - ========= eflash loader cmd arguments =========
[10:05:43.374] - /home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/eflash_loader/eflash_loader_cfg.ini
[10:05:43.374] - Config file: /home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/eflash_loader/eflash_loader_cfg.ini
[10:05:43.374] - serial port is /dev/ttyUSB0
[10:05:43.374] - cpu_reset=False
[10:05:43.374] - chiptype: bl602
[10:05:43.374] - ========= Interface is uart =========
[10:05:43.374] - com speed: 921600
[10:05:43.374] - isp mode speed: 921600
[10:05:43.374] - Eflash load helper file: /home/wildboarg/github/Ai-Thinker-WB2/tools/flash_tool/chips/bl602/eflash_loader/eflash_loader_40m.bin
[10:05:43.374] - ========= load eflash_loader.bin =========
[10:05:43.374] - Load eflash_loader.bin via uart
[10:05:43.374] - ========= image load =========
[10:05:43.377] - Please Press Reset Key!
问题解决了吧~
您需要登录后才可以回帖 立即登录
高级模式
返回
统计信息
  • 会员数: 28064 个
  • 话题数: 39617 篇