首页 > 其他分享 >PSoc62™开发板之串口通信

PSoc62™开发板之串口通信

时间:2024-01-02 23:34:34浏览次数:44  
标签:rt RT rx 开发板 device 串口 msg PSoc62 CONFIG

实验目的

1.使用串口和PC机通信:接收和发送信息

2.接收GNSS模块定位信息

实验准备

  • PSoc62™开发板
  • CH340 USB转TTL模块
  • ATGM332D GNSS模块
  • 公母头杜邦线x4

板载资源

板载有多少uart

创建工程例程,在libraries/HAL_Drivers/uart_config.h中查看BSP支持的uart数量及对应的GPIO,可以看到一共有7组uart

/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-07-08     Rbb666       first version
 */

#ifndef __UART_CONFIG_H__
#define __UART_CONFIG_H__

#include <rtthread.h>
#include "board.h"

#ifdef __cplusplus
extern "C"
{
#endif

#ifdef BSP_USING_UART0
    /* UART0 device driver structure */
    cy_stc_sysint_t UART0_SCB_IRQ_cfg =
        {
            .intrSrc = (IRQn_Type)scb_0_interrupt_IRQn,
            .intrPriority = (7u),
    };
#endif
#ifdef BSP_USING_UART1
    /* UART1 device driver structure */
    cy_stc_sysint_t UART1_SCB_IRQ_cfg =
        {
            .intrSrc = (IRQn_Type)scb_1_interrupt_IRQn,
            .intrPriority = (7u),
    };
#endif
#ifdef BSP_USING_UART2
    /* UART2 device driver structure */
    cy_stc_sysint_t UART2_SCB_IRQ_cfg =
        {
            .intrSrc = (IRQn_Type)scb_2_interrupt_IRQn,
            .intrPriority = (7u),
    };
#endif
#ifdef BSP_USING_UART3
    /* UART3 device driver structure */
    cy_stc_sysint_t UART3_SCB_IRQ_cfg =
        {
            .intrSrc = (IRQn_Type)scb_3_interrupt_IRQn,
            .intrPriority = (7u),
    };
#endif
#ifdef BSP_USING_UART4
    /* UART4 device driver structure */
    cy_stc_sysint_t UART4_SCB_IRQ_cfg =
        {
            .intrSrc = (IRQn_Type)scb_4_interrupt_IRQn,
            .intrPriority = (7u),
    };
#endif
#ifdef BSP_USING_UART5
    /* UART5 device driver structure */
    cy_stc_sysint_t UART5_SCB_IRQ_cfg =
        {
            .intrSrc = (IRQn_Type)scb_5_interrupt_IRQn,
            .intrPriority = (7u),
    };
#endif

#ifdef BSP_USING_UART6
    /* UART6 device driver structure */
    cy_stc_sysint_t UART6_SCB_IRQ_cfg =
        {
            .intrSrc = (IRQn_Type)scb_6_interrupt_IRQn,
            .intrPriority = (7u),
    };
#endif
#if defined(BSP_USING_UART0)
#ifndef UART0_CONFIG
#define UART0_CONFIG                            \
    {                                           \
        .name = "uart0",                        \
        .tx_pin = P0_3,                         \
        .rx_pin = P0_2,                         \
        .usart_x = SCB0,                        \
        .intrSrc = scb_0_interrupt_IRQn,        \
        .userIsr = uart_isr_callback(uart0),    \
        .UART_SCB_IRQ_cfg = &UART0_SCB_IRQ_cfg, \
    }
    void uart0_isr_callback(void);
#endif /* UART0_CONFIG */
#endif /* BSP_USING_UART0 */

#if defined(BSP_USING_UART1)
#ifndef UART1_CONFIG
#define UART1_CONFIG                            \
    {                                           \
        .name = "uart1",                        \
        .tx_pin = P10_1,                        \
        .rx_pin = P10_0,                        \
        .usart_x = SCB1,                        \
        .intrSrc = scb_1_interrupt_IRQn,        \
        .userIsr = uart_isr_callback(uart1),    \
        .UART_SCB_IRQ_cfg = &UART1_SCB_IRQ_cfg, \
    }
    void uart1_isr_callback(void);
#endif /* UART1_CONFIG */
#endif /* BSP_USING_UART1 */

#if defined(BSP_USING_UART2)
#ifndef UART2_CONFIG
#if defined(SOC_CY8C6244LQI_S4D92)
#define UART2_CONFIG                            \
    {                                           \
        .name = "uart2",                        \
        .tx_pin = P3_1,                         \
        .rx_pin = P3_0,                         \
        .usart_x = SCB2,                        \
        .intrSrc = scb_2_interrupt_IRQn,        \
        .userIsr = uart_isr_callback(uart2),    \
        .UART_SCB_IRQ_cfg = &UART2_SCB_IRQ_cfg, \
    }
#else
#define UART2_CONFIG                            \
    {                                           \
        .name = "uart2",                        \
        .tx_pin = P9_1,                         \
        .rx_pin = P9_0,                         \
        .usart_x = SCB2,                        \
        .intrSrc = scb_2_interrupt_IRQn,        \
        .userIsr = uart_isr_callback(uart2),    \
        .UART_SCB_IRQ_cfg = &UART2_SCB_IRQ_cfg, \
    }
#endif /* SOC_CY8C6244LQI_S4D92 */
    void uart2_isr_callback(void);
#endif /* UART2_CONFIG */
#endif /* BSP_USING_UART2 */

#if defined(BSP_USING_UART3)
#ifndef UART3_CONFIG
#define UART3_CONFIG                            \
    {                                           \
        .name = "uart3",                        \
        .tx_pin = P6_1,                         \
        .rx_pin = P6_0,                         \
        .usart_x = SCB3,                        \
        .intrSrc = scb_3_interrupt_IRQn,        \
        .userIsr = uart_isr_callback(uart3),    \
        .UART_SCB_IRQ_cfg = &UART3_SCB_IRQ_cfg, \
    }
    void uart3_isr_callback(void);
#endif /* UART3_CONFIG */
#endif /* BSP_USING_UART3 */

#if defined(BSP_USING_UART4)
#ifndef UART4_CONFIG
#define UART4_CONFIG                            \
    {                                           \
        .name = "uart4",                        \
        .tx_pin = P7_1,                         \
        .rx_pin = P7_0,                         \
        .usart_x = SCB4,                        \
        .intrSrc = scb_4_interrupt_IRQn,        \
        .userIsr = uart_isr_callback(uart4),    \
        .UART_SCB_IRQ_cfg = &UART4_SCB_IRQ_cfg, \
    }
    void uart4_isr_callback(void);
#endif /* UART4_CONFIG */
#endif /* BSP_USING_UART4 */

#if defined(BSP_USING_UART5)
#ifndef UART5_CONFIG
#define UART5_CONFIG                            \
    {                                           \
        .name = "uart5",                        \
        .tx_pin = P5_1,                         \
        .rx_pin = P5_0,                         \
        .usart_x = SCB5,                        \
        .intrSrc = scb_5_interrupt_IRQn,        \
        .userIsr = uart_isr_callback(uart5),    \
        .UART_SCB_IRQ_cfg = &UART5_SCB_IRQ_cfg, \
    }
    void uart5_isr_callback(void);
#endif /* UART5_CONFIG */
#endif /* BSP_USING_UART5 */

#if defined(BSP_USING_UART6)
#ifndef UART6_CONFIG
#define UART6_CONFIG                            \
    {                                           \
        .name = "uart6",                        \
        .tx_pin = P6_5,                         \
        .rx_pin = P6_4,                         \
        .usart_x = SCB6,                        \
        .intrSrc = scb_6_interrupt_IRQn,        \
        .userIsr = uart_isr_callback(uart6),    \
        .UART_SCB_IRQ_cfg = &UART6_SCB_IRQ_cfg, \
    }
    void uart6_isr_callback(void);
#endif /* UART6_CONFIG */
#endif /* BSP_USING_UART6 */
#ifdef __cplusplus
}
#endif

#endif

uart0-uart6和GPIO对应关系表

uart2是比较特殊的,两组不同的GPIO都可以使用它

分组 RXD TXD
uart0 P0.2 P0.3
uart1 P10.0 P10.1
uart2 P3.0 / P9.0 P3.1 / P9.1
uart3 P6.0 P6.1
uart4 P7.0 P7.1
uart5 P5.0 P5.1
uart6 P6.4 P6.5

电路图

串口IO

这里以uart0为例,P0.2对应MCU_ARD_PWM11P0.3对应MCU_ARD_PWM12

串口引脚图.png

模块连接图

右侧排母从上往下第6、7引脚分别对应uart0 TXD、RXD

串口模块电路.png

状态指示灯

板子有两个自主控制的绿色LED,通过上拉方式接入GPIO,MCU_USER_LED1对应P0.0,本次实验使用它来指示uart0的数据接收状态

程序设计

和PC机通信

通过CH340 USB转TTL模块连接PC和uart0,实物连接图如下

串口和PC通信.jpg

编码实现

#include <rtthread.h>
#include <rtdevice.h>
#include "drv_gpio.h"

#define LED_PIN     GET_PIN(0, 1)
#define UART_DEVICE_NAME       "uart0"
#define BSP_UART4_RX_BUFSIZE 1024

static rt_device_t serial;

struct rx_msg
{
    rt_device_t dev;
    rt_size_t size;
};

static struct rt_messagequeue rx_mq;

static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
    rt_pin_write(LED_PIN, PIN_LOW);
    struct rx_msg msg;
    rt_err_t result;
    msg.dev = dev;
    msg.size = size;

    result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
    if (result == -RT_EFULL)
    {
        rt_kprintf("message queue full!\n");
    }
    return result;
}

static void serial_thread_entry(void *parameter)
{
    struct rx_msg msg;
    rt_err_t result;
    rt_uint32_t rx_length;
    static char rx_buffer[BSP_UART4_RX_BUFSIZE + 1];
    while (1)
    {
        rt_memset(&msg, 0, sizeof(msg));
        result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
//        if (result == RT_EOK)
        {
            rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
            rx_buffer[rx_length] = '\0';
            rt_device_write(serial, 0, rx_buffer, rx_length);
            rt_pin_write(LED_PIN, PIN_HIGH);
//            rt_kprintf("%s\n",rx_buffer);
        }
    }
}

static int uart0_setup(int argc, char *argv[])
{
    rt_err_t ret = RT_EOK;
    char uart_name[RT_NAME_MAX];
    static char msg_pool[256];
    char str[] = "uart0 setup successfully!\r\n";

    if (argc == 2)
    {
        rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
    }
    else
    {
        rt_strncpy(uart_name, UART_DEVICE_NAME, RT_NAME_MAX);
    }

    serial = rt_device_find(uart_name);
    if (!serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return RT_ERROR;
    }

    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
    config.baud_rate = BAUD_RATE_9600;
    config.data_bits = DATA_BITS_8;
    config.stop_bits = STOP_BITS_1;
    config.bufsz     = 1024;
    config.parity    = PARITY_NONE;
    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);

    rt_mq_init(&rx_mq, "rx_mq", msg_pool, sizeof(struct rx_msg), sizeof(msg_pool), RT_IPC_FLAG_FIFO);
    rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
    rt_device_set_rx_indicate(serial, uart_input);
    rt_device_write(serial, 0, str, (sizeof(str) - 1));
    rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
    if (thread != RT_NULL)
    {
        rt_thread_startup(thread);
    }
    else
    {
        ret = RT_ERROR;
    }

    return ret;
}

MSH_CMD_EXPORT(uart0_setup, uart0 setup example);


int main(void)
{
    rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);

    while (1)
    {
        rt_thread_mdelay(500);
    }
}

打开串口调试工具,打开对应的COM口,串口设置和上面的代码保持一致:波特率115200、数据位8位、停止位为1位、奇偶校验位为无

串口调试工具设置.png

在RT-Thread Studio初始化uart0,使用msh终端输入uart0_setup命令

uart0设置.png

初始化成功,可以在串口调试工具看到以下信息

uart0初始化成功.png

自定义串口消息,点击发送按钮,可以看到发出去的消息被开发板转发了回来,而LED也随即闪了一下

uart0发送接收数据.png

接收GPS模块定位数据

通过uart0连接ATGM332D GNSS模块,实物连接图如下

串口连接GNSS模块.jpg

编码实现uart0串口通信,注意GNSS模块的波特率默认为9600,其它跟上面的example保持一致

#include <rtthread.h>
#include <rtdevice.h>
#include "drv_gpio.h"

#define LED_PIN     GET_PIN(0, 1)
#define UART_DEVICE_NAME       "uart0"
#define BSP_UART4_RX_BUFSIZE 1024

static rt_device_t serial;

struct rx_msg
{
    rt_device_t dev;
    rt_size_t size;
};

static struct rt_messagequeue rx_mq;

static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
    rt_pin_write(LED_PIN, PIN_LOW);
    struct rx_msg msg;
    rt_err_t result;
    msg.dev = dev;
    msg.size = size;

    result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
    if (result == -RT_EFULL)
    {
        rt_kprintf("message queue full!\n");
    }
    return result;
}

static void serial_thread_entry(void *parameter)
{
    struct rx_msg msg;
    rt_err_t result;
    rt_uint32_t rx_length;
    static char rx_buffer[BSP_UART4_RX_BUFSIZE + 1];
    while (1)
    {
        rt_memset(&msg, 0, sizeof(msg));
        result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
//        if (result == RT_EOK)
        {
            rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
            rx_buffer[rx_length] = '\0';
            rt_device_write(serial, 0, rx_buffer, rx_length);
            rt_pin_write(LED_PIN, PIN_HIGH);
            rt_kprintf("%s\n",rx_buffer);
        }
    }
}

static int uart0_setup(int argc, char *argv[])
{
    rt_err_t ret = RT_EOK;
    char uart_name[RT_NAME_MAX];
    static char msg_pool[256];
    char str[] = "uart0 setup successfully!\r\n";

    if (argc == 2)
    {
        rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
    }
    else
    {
        rt_strncpy(uart_name, UART_DEVICE_NAME, RT_NAME_MAX);
    }

    serial = rt_device_find(uart_name);
    if (!serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return RT_ERROR;
    }

    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
    config.baud_rate = BAUD_RATE_9600;
    config.data_bits = DATA_BITS_8;
    config.stop_bits = STOP_BITS_1;
    config.bufsz     = 1024;
    config.parity    = PARITY_NONE;
    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);

    rt_mq_init(&rx_mq, "rx_mq", msg_pool, sizeof(struct rx_msg), sizeof(msg_pool), RT_IPC_FLAG_FIFO);
    rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
    rt_device_set_rx_indicate(serial, uart_input);
    rt_device_write(serial, 0, str, (sizeof(str) - 1));
    rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
    if (thread != RT_NULL)
    {
        rt_thread_startup(thread);
    }
    else
    {
        ret = RT_ERROR;
    }

    return ret;
}

MSH_CMD_EXPORT(uart0_setup, uart0 setup example);


int main(void)
{
    rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);

    while (1)
    {
        rt_thread_mdelay(500);
    }
}

在msh终端输入uart0_setup后GNSS数据读取结果如下

GNSS数据读取.png

运行效果如下,可以看到GNSS报文以1Hz的频率打印,LED灯在不停地闪烁

实验效果

标签:rt,RT,rx,开发板,device,串口,msg,PSoc62,CONFIG
From: https://blog.51cto.com/hywing/9073632

相关文章

  • STM32MP157D-DK1开发板Qt镜像构建
    上篇介绍了STM32MP57-DK1开发板官方系统的烧录。那个系统包含Linux系统的基础功能,如果要进行Qt开发,还需要重新构建带有Qt功能的镜像本篇就来介绍如何构建带有Qt功能的系统镜像,并在开发板中烧录构建的镜像。1Distribution包的构建STM32微处理器平台的STM32MPU嵌入式软件发行版支持......
  • 2K1000开发板虚拟机 ubuntu 更换下载源
    Ubuntu系统软件的下载安装我们通常使用命令“apt-get”,该命令可以实现软件自动下载,安装,配置。该命令采用客户端/服务器的模式,我们的Ubuntu系统作为客户端,当需要下载软件的时候就向服务器发起请求,因此我们需要配置下服务器的地址,也就是更换ubuntu系统的下载源,首先打开......
  • 迅为RK3568开发板-实时系统烧写(Preemption 系统/ Xenomai系统烧写)
    目前Linux系统提供的完整升级固件update.img只有Buildroot系统。所以本章节烧写完整升级固件update.img的方法也只针对Buildroot系统。演示烧写过程使用的镜像地址是“iTOP-3568开发板\01_【iTOP-RK3568开发板】基础资料【V1.7版本】\14_实时系统配套资料\03_Preempti......
  • 1-1-06 USB串口工具安装
    1.1概述    串口是最常用的一种调试工具,开发过程中我们经常会使用串口输出一些调试信息,在LINUX下也会用串口控制台控制LINUX系统。目前的串口,大部分都是USB转串口。米联客硬件中一共使用过三款串口工具,分别是CP2104CH9102F(PIN脚兼容CP2014)CH304,以下给出每种串口工具的安......
  • 30 RS485串口程序收发环路设计
    软件版本:VIVADO2021.1操作系统:WIN1064bit硬件平台:适用XILINXA7/K7/Z7/ZU/KU系列FPGA登录米联客(MiLianKe)FPGA社区-www.uisrc.com观看免费视频课程、在线答疑解惑!1概述在前面的课程中,我们已经学习了UART串口程序的设计,在工业场合为了提高串口的抗干扰能力,以及传输距离,RS4......
  • 11 UART串口接收驱动设计
    软件版本:VIVADO2021.1操作系统:WIN1064bit硬件平台:适用XILINXA7/K7/Z7/ZU/KU系列FPGA登录米联客(MiLianKe)FPGA社区-www.uisrc.com观看免费视频课程、在线答疑解惑!1概述UART串口通信是应用非常广泛的一种串行异步通信方式,常用的异步串口包括RS232\RS482\RS485。RS232的逻......
  • 12 串口程序收发环路设计
    软件版本:VIVADO2021.1操作系统:WIN1064bit硬件平台:适用XILINXA7/K7/Z7/ZU/KU系列FPGA登录米联客(MiLianKe)FPGA社区-www.uisrc.com观看免费视频课程、在线答疑解惑!1概述前面两课,我们完成了我们发送程序的测试,成功给我们的PC主机发送了我们"HELLOFPGA"的信息,我们主机显示......
  • 10 UART串口发送驱动设计
    软件版本:VIVADO2021.1操作系统:WIN1064bit硬件平台:适用XILINXA7/K7/Z7/ZU/KU系列FPGA登录米联客(MiLianKe)FPGA社区-www.uisrc.com观看免费视频课程、在线答疑解惑!1概述UART串口通信是应用非常广泛的一种串行异步通信方式,常用的异步串口包括RS232\RS482\RS485。RS232的......
  • MT8766安卓核心板/开发板_MTK联发科4G安卓手机主板方案定制开发
    MT8766采用台积电12nmFinFET制程工艺,4*A53架构,Android9.0操作系统,搭载2.0GHz的ArmNEON引擎。提供了支持最新OpenOS及其要求苛刻的应用程序所需的处理能力,专为具有全球蜂窝连接的高移动性和功能强大的平板设备而设计。该处理器还配备600MHz主频的超强IMGGE8300GPU,可流......
  • (转载)为什么UART串口通信要16倍过采样数据
    增强抗干扰是原因之一标准UART可以选16倍采样,也可以选64倍采样,个人觉得应该是方便分频设计. 标准UART的RXD前端有一个"1到0跳变检测器",当其连续接受到8个RXD上的地电平时,该检测器就认为RXD线出现了起始位,进入接受数据状态.在接受状态,接受控制器对数据位7,8,9三个脉冲采......