Skip to content

由 黎明将至! 贡献,Ai-Thinker 进行整理

Ai-WB2-32S uart rx超时中断(接收空闲中断)

前言

本期文档介绍一下uart接收中断的使用,在Linux Ubuntu下烧录的过程可以点击链接查看 芯片参考手册uart部分链接**测试完记着要把bl_uart.c和bl_uart.h文件还原,要不然别的demo就无法编译了!

一、具体流程

第1步 进入目录~/Ai-Thinker-WB2/applications/peripherals/uart/uart

cd ~/Ai-Thinker-WB2/applications/peripherals/uart/uart

第2步 输入ls查看目录下的文件

ls

第3步 编写main.c文件

gedit main.c

将代码改为下面代码

📜 点击展开完整代码
c
/*
 * @Author: [email]xuhongv@yeah.net[/email]
 * @Date: 2022-10-03 15:02:19
 * @LastEditors: [email]xuhongv@yeah.net[/email] [email]xuhongv@yeah.net[/email]
 * @LastEditTime: 2022-10-20 17:42:45
 * @FilePath: \bl_iot_sdk_for_aithinker\applications\get-started\helloworld\helloworld\main.c
 * @Description: Uart
 */

#
include

<stdio.h>

#
include

<string.h>

#
include

<FreeRTOS.h>

#
include

<task.h>

#
include

<blog.h>

#
include

"bl_sys.h"

#
include

<stdio.h>

#
include

<cli.h>

#
include

<hosal_uart.h>

#
include

<blog.h>

#
include

"bl_uart.h"

//create a serial port structure variable.

hosal_uart_dev_t
 uart_dev_log = {
    .config = {
        .uart_id =
1
,
        .tx_pin =
16
,
// TXD GPIO

        .rx_pin =
7
,
// RXD GPIO

        .cts_pin =
255
,
        .rts_pin =
255
,
        .baud_rate =
115200
,
        .data_width = HOSAL_DATA_WIDTH_8BIT,
        .parity = HOSAL_NO_PARITY,
        .stop_bits = HOSAL_STOP_BITS_1,
        .mode = HOSAL_UART_MODE_INT,
    },
};

//functions used to create tasks.

void

TaskUart
(
void
 *param)

{

/* Uart init device */

    hosal_uart_init(&uart_dev_log);
    blog_info(
"Uart Demo Start"
);

while
 (
1
)
    {

// hosal_uart_send(&uart_dev_log, "test\r\n", 6);      //test whether to exit the interrupt.

if
(my_uart_data.uart_rx_flag >
0
)
//if data is received, execute the following program.

        {
            my_uart_data.uart_rx_flag =
0
;
//clear receiving flag.

memset
(my_uart_data.uart_tx_data,
0
,
sizeof
(my_uart_data.uart_tx_data));

//process the received data and write it to the send buffer.

sprintf
((
char
 *)(my_uart_data.uart_tx_data),
"receive: %s\r\n"
, my_uart_data.uart_rx_data);

//send processed data

            hosal_uart_send(&uart_dev_log, my_uart_data.uart_tx_data,
sizeof
(my_uart_data.uart_tx_data));

memset
(my_uart_data.uart_rx_data,
0
,
sizeof
(my_uart_data.uart_rx_data));
        }

//delay of 500ms.

        vTaskDelay(
500
);
    }
}

/**
 * @brief main
 *
 */

void

main
(
void
)

{

//create task

    xTaskCreate(TaskUart,
"TaskUart"
,
1024
,
NULL
,
15
,
NULL
);
}

第4步 进入~/Ai-Thinker-WB2/components/platform/hosal/bl602_hal目录

cd ~/Ai-Thinker-WB2/components/platform/hosal/bl602_hal/

第5步 修改bl_uart.c文件

gedit bl_uart.c

📜 点击展开完整代码
c
#
include

<hosal_uart.h>

📜 点击展开完整代码
c
extern

hosal_uart_dev_t
 uart_dev_log;

修改中断服务函数

📜 点击展开完整代码
c
void

UART1_IRQHandler
(
void
)

{

cb_uart_notify_t
 cb;

void
 *arg;

uint32_t
 tmpVal =
0
;

uint32_t
 maskVal =
0
;

uint32_t
 UARTx = uartAddr[
1
];

    tmpVal = BL_RD_REG(UARTx,UART_INT_STS);
    maskVal = BL_RD_REG(UARTx,UART_INT_MASK);

if
 (BL_IS_REG_BIT_SET(tmpVal,UART_URX_RTO_INT) && !BL_IS_REG_BIT_SET(maskVal,UART_CR_URX_RTO_MASK)){
        BL_WR_REG(UARTx,UART_INT_CLEAR,
0x12
);

// *((uint32_t *)0x4000A128) = 0x12;

/*Receive Data ready*/

        cb = g_uart_notify_arg[
1
].rx_cb;
        arg = g_uart_notify_arg[
1
].rx_cb_arg;

if
 (cb) {

/*notify up layer*/

            cb(arg);
            my_uart_data.uart_rx_flag = hosal_uart_receive(&uart_dev_log,
                my_uart_data.uart_rx_data,
sizeof
(my_uart_data.uart_rx_data));
        }
    }
}

第6步 修改bl_uart.c文件

gedit bl_uart.h

📜 点击展开完整代码
c
#
define
 DATALINE        64

typedef

struct
 {

uint8_t
 uart_rx_data[DATALINE];

uint8_t
 uart_rx_flag;

uint8_t
 uart_tx_data[DATALINE];
}MY_UART_DATA;

MY_UART_DATA my_uart_data;

第7步 编译

进入目录~/Ai-Thinker-WB2/applications/peripherals/uart cd ~/Ai-Thinker-WB2/applications/peripherals/uart

编译 make -j8(博主的电脑内核是8核的,因此就是用了8核编译,这里的内核数最大写自己电脑的内核数)

第8步 烧录到模组(此时需要模组连接到虚拟机,具体过程可以点击此链接

执行 make flash p=/dev/ttyUSB0 b=115200

根据提示按下模组的复位键

第9步 通过调试助手向模组发送数据(这里要将模组连接到主机,具体过程可以点击次连接进行参考)

将模组连接到主机之后,打开串口调试助手,选择对应的串口、波特率等,打开串口,按下复位键

二、使用注意

清除接收中断标志位,既要清除urx_end_int,也要清除urx_rto_int;否则就会无法出中断;

原链接:<https://blog.csdn.net/qq_54193285/article/details/135933164?spm=1001.2014.3001.5501>

**测试完记着要把bl_uart.c和bl_uart.h文件还原,要不然别的demo就无法编译了!

Released under the MIT License. Build Time 2026-09-11 14:52:23