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
图三
将代码改为下面代码

/*
 * @Author: xuhongv@yeah.net
 * @Date: 2022-10-03 15:02:19
 * @LastEditors: xuhongv@yeah.net xuhongv@yeah.net
 * @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
图五图六

#include <hosal_uart.h>

图七

extern hosal_uart_dev_t uart_dev_log;
MY_UART_DATA my_uart_data;(修改,需要添加)

修改中断服务函数
图八

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.h文件

gedit bl_uart.h
图九
图十

#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;

extern 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;否则就会无法出中断;
图十九
**测试完记着要把bl_uart.c和bl_uart.h文件还原,要不然别的demo就无法编译了!

总结

以上就是本期分享的内容,目的为了介绍一下uart接收中断的使用,更多资料可从安信可官网上获取。

官方官网:https://www.ai-thinker.com
开发资料:https://docs.ai-thinker.com/
官方论坛:http://bbs.ai-thinker.com
技术支持:support@aithinker.com

以上代码编译使用gedit是为了方便大众使用,因此没有使用vs做例子。
**测试完记着要把bl_uart.c和bl_uart.h文件还原,要不然别的demo就无法编译了!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值