杰理AC63串口收发实例

在event.h文件中预定义串口消息

#define DEVICE_EVENT_FROM_MY_UART      (('M' << 24) | ('Y' << 16) | ('U' << 8) | '\0')

在app_spp_and_le.c文件里对SYS_DEVICE_EVENT做处理,添加收到DEVICE_EVENT_FROM_MY_UART消息时的处理函数my_rx_handler();

    case SYS_DEVICE_EVENT:
        if ((u32)event->arg == DEVICE_EVENT_FROM_MY_UART) {
   
            my_rx_handler();
        } else if ((u32)event->arg == DEVICE_EVENT_FROM_POWER) {
   
            return app_power_event_handler(&event->u.dev, spple_set_soft_poweroff);
        }

自己新建一个uart_test文件,定义以下串口接收中断回调函数和数据包处理函数

//发出消息通知系统调用my_rx_handler();
static void my_uart_isr_cb(void *ut_bus, u32 status)
{
   
    struct sys_event e;
    /* printf("{##%d}"); */

    if (status == UT_RX || status == UT_RX_OT) {
   
        e.type = SYS_DEVICE_EVENT;
        e.arg  = (void *)DEVICE_EVENT_FROM_MY_UART;
        e.u.dev.event = 0;
        e.u.dev.value = 0;
        sys_event_notify(&e);
    }
}

 //串口接收数据包处理函数
 //这是收到一帧数据后由串口回调函数通知系统调用的的处理,不是单个字节的串口中断处理
void my_rx_handler(void)
{
   
    u8 len = 0;
    len = uart_bus->read(my_ubuff, -1, 0);
    printf("Total length : %d \r\n",len);
    printf(u8 i=0;i<len;i++)
    {
   
        printf("%c",my_ubuff[i]);
    }
}

定义以下串口初始化函数

const uart_bus_t *uart_bus;//定义一个串口结构体
static u8 devBuffer_static[UART_DB_SIZE] __attribute__((aligned(4)));       //dev DMA memory
u8 my_ubuff[200] = {
   0};
int my_uart_init(void)
{
   
    struct uart_platform_data_t u_arg = {
   0};
    u_arg.tx_pin = UART_DB_TX_PIN;
    u_arg.rx_pin = UART_DB_RX_PIN;
    u_arg.rx_cbuf = devBuffer_static;
    u_arg.rx_cbuf_size = UART_DB_SIZE;
    u_arg.frame_length = UART_DB_SIZE;
    u_arg.rx_timeout = 6;  //ms,兼容波特率较低
    u_arg.isr_cbfun = my_uart_isr_cb;//中断回调,也可以在中断回调里直接收数据
    u_arg.baud = UART_BAUD_RATE;
    u_arg.is_9bit = 0;

    uart_bus = uart_dev_open(&u_arg);

    if (uart_bus != NULL) {
   
        printf("uart_dev_open() success\n");
//        __this->udev = uart_bus;
        return 0;
    }
    return -1;
}

到此处,串口已经OK了,可以写几个发送函数测试一下。当然也可以使用电脑串口助手测试AC63串口接收。

static void my_uart_putbyte(char a)
{
   
    if (uart_bus != NULL) {
   
        uart_bus->putbyte(a);
    }
}

static void my_uart_write(char *buf, u16 len)
{
   
    if (uart_bus != NULL) {
   
        uart_bus->write(buf, len);
    }
}

void send_test(void)
{
   
    my_uart_write("123456789\r\n", strlen("123456789\r\n"));
}

void uart_test(void)
{
   
    sys_timer_add(NULL, send_test, 3000);

}

串口的底层操作,大家不必关心了,杰理SDK已经给做的很好的。当然也可以直接在串口回调函数里处理数据包,不必发出消息通知系统再调用数据处理,这一点杰理还是很灵活的,由开发者自己根据实际情况选用。

相关推荐

  1. AC63串口收发实例

    2024-01-11 10:48:03       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-11 10:48:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-11 10:48:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-11 10:48:03       82 阅读
  4. Python语言-面向对象

    2024-01-11 10:48:03       91 阅读

热门阅读

  1. IDEA异常退出重启后项目启动失败解决

    2024-01-11 10:48:03       52 阅读
  2. Leetcode160 两个链表是否相交

    2024-01-11 10:48:03       44 阅读
  3. Saas整合ELK Stack

    2024-01-11 10:48:03       58 阅读
  4. 【leetcode283】移动零

    2024-01-11 10:48:03       56 阅读
  5. C++ 捕获所有异常并拿到错误原因的方法

    2024-01-11 10:48:03       61 阅读
  6. SQL_DQL_执行顺序

    2024-01-11 10:48:03       49 阅读
  7. springboot常用扩展点

    2024-01-11 10:48:03       53 阅读
  8. 在Oracle SQL Plus中抑制SQL命令的输出

    2024-01-11 10:48:03       54 阅读
  9. GBASE南大通用GBase 8a 安装部署

    2024-01-11 10:48:03       45 阅读
  10. Spring的@Configuration注解和@Component 注解的关系

    2024-01-11 10:48:03       44 阅读
  11. zustand状态管理工具(react)

    2024-01-11 10:48:03       54 阅读
  12. fpga目前就业形势咋样?

    2024-01-11 10:48:03       65 阅读