Skip to content

Contributed by 黎明将至!, organized by Ai-Thinker

Ai-WB2-32S UART RX Timeout Interrupt (Receive Idle Interrupt)

Preface

This article introduces the use of the UART receive interrupt. You can click the link to view the flashing process under Linux Ubuntu. Chip reference manual, UART section link**测试完记着要把bl_uart.c和bl_uart.h文件还原,要不然别的demo就无法编译了!

1. Detailed Procedure

Step 1. Enter the directory ~/Ai-Thinker-WB2/applications/peripherals/uart/uart

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

Step 2. Run ls to view the files in the directory

ls

Step 3. Write the main.c file

gedit main.c

Change the code to the following:

Click to expand full code
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
);
}

Step 4. Enter the ~/Ai-Thinker-WB2/components/platform/hosal/bl602_hal directory

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

Step 5. Modify the bl_uart.c file

gedit bl_uart.c

Click to expand full code
c
#
include

<hosal_uart.h>

Click to expand full code
c
extern

hosal_uart_dev_t
 uart_dev_log;

修改中断服务函数

Click to expand full code
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));
        }
    }
}

Step 6. Modify the bl_uart.c file

gedit bl_uart.h

Click to expand full code
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;

Step 7. Compile

Enter the directory ~/Ai-Thinker-WB2/applications/peripherals/uart cd ~/Ai-Thinker-WB2/applications/peripherals/uart

Compile make -j8 (the blogger's computer has an 8-core CPU, so 8 cores were used for compilation; here the number of cores should be no more than your own computer's core count)

Run make flash p=/dev/ttyUSB0 b=115200

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

After connecting the module to the host, open the serial port debugging assistant, select the corresponding serial port, baud rate, etc., open the serial port, and press the reset key

2. Usage Notes

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

Original link: <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