RP2040 C SDK开发串口的使用

news2024/9/25 9:31:35

RP2040 C SDK开发串口的使用


  • 📍环境搭建部署篇《RP2040 VSCode C/C++开发环境快速部署》
  • 🔖RP2040 有硬件串口资源有2个。
  • 🌿参考RP2040 C SDK Hardware APIS:https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#group_hardware_uart
  • 🌿串口0默认引脚定义:
// --- UART ---
#ifndef PICO_DEFAULT_UART
#define PICO_DEFAULT_UART 0
#endif
#ifndef PICO_DEFAULT_UART_TX_PIN
#define PICO_DEFAULT_UART_TX_PIN 0
#endif
#ifndef PICO_DEFAULT_UART_RX_PIN
#define PICO_DEFAULT_UART_RX_PIN 1
#endif
  • 🌿串口0初始化:
 int main() {

    // Set the GPIO pin mux to the UART - pin 0 is TX, 1 is RX; note use of UART_FUNCSEL_NUM for the general
    // case where the func sel used for UART depends on the pin number
    // Do this before calling uart_init to avoid losing data
    gpio_set_function(0, UART_FUNCSEL_NUM(uart0, 0));
    gpio_set_function(1, UART_FUNCSEL_NUM(uart0, 1));

    // Initialise UART 0
    uart_init(uart0, 115200);
    uart_puts(uart0, "Hello world!");
}

  • 🌿如果调用了stdio_init_all();则默认使用串口0(0,1),波特率默认115200;作为标准输出函数。
bool stdio_init_all(void) {
    // todo add explicit custom, or registered although you can call stdio_enable_driver explicitly anyway
    // These are well known ones

    bool rc = false;
#if LIB_PICO_STDIO_UART
    stdio_uart_init();
    rc = true;
#endif

#if LIB_PICO_STDIO_SEMIHOSTING
    stdio_semihosting_init();
    rc = true;
#endif

#if LIB_PICO_STDIO_USB
    rc |= stdio_usb_init();
#endif
    return rc;
}
//最终调用的串口初始化函数
void stdio_uart_init_full(struct uart_inst *uart, uint baud_rate, int tx_pin, int rx_pin) {
    uart_instance = uart;
    if (tx_pin >= 0) gpio_set_function((uint)tx_pin, GPIO_FUNC_UART);
    if (rx_pin >= 0) gpio_set_function((uint)rx_pin, GPIO_FUNC_UART);
    uart_init(uart_instance, baud_rate);
    stdio_set_driver_enabled(&stdio_uart, true);
}

📗串口0和串口1测试例程

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/uart.h"
#include "hardware/gpio.h"
#include "hardware/gpio.h"
#include "hardware/divider.h"




// UART defines
// By default the stdout UART is `uart0`, so we will use the second one
#define UART_ID uart1
#define BAUD_RATE 115200  //

// Use pins 4 and 5 for UART1
// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments
#define UART_TX_PIN 4
#define UART_RX_PIN 5

// GPIO defines
// Example uses GPIO 2
#define GPIO 2
static const uint pin = 25;

int main()
{
    stdio_init_all();//串口0,115200,支持printf打印

    // Set up our UART
    uart_init(UART_ID, BAUD_RATE);//串口1,115200
    uart_set_hw_flow(UART_ID, false, false);//关闭硬件流控
    // Set the TX and RX pins by using the function select on the GPIO
    // Set datasheet for more information on function select
    gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
    gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);


    // GPIO initialisation.
    // We will make this GPIO an input, and pull it up by default
    gpio_init(GPIO);
    gpio_set_dir(GPIO, GPIO_OUT);
    gpio_pull_up(GPIO);
    gpio_init(pin);
    gpio_set_dir(pin, GPIO_OUT);
while (true) {

    // Example of using the HW divider. The pico_divider library provides a more user friendly set of APIs
    // over the divider (and support for 64 bit divides), and of course by default regular C language integer
    // divisions are redirected thru that library, meaning you can just use C level `/` and `%` operators and
    // gain the benefits of the fast hardware divider.
    int32_t dividend = 123456;
    int32_t divisor = -321;
    // This is the recommended signed fast divider for general use.
    divmod_result_t result = hw_divider_divmod_s32(dividend, divisor);
    printf("%d/%d = %d remainder %d\n", dividend, divisor, to_quotient_s32(result), to_remainder_s32(result));
    // This is the recommended unsigned fast divider for general use.
    int32_t udividend = 123456;
    int32_t udivisor = 321;
    divmod_result_t uresult = hw_divider_divmod_u32(udividend, udivisor);
    printf("%d/%d = %d remainder %d\n", udividend, udivisor, to_quotient_u32(uresult), to_remainder_u32(uresult));
    puts("Hello, world!1234 from uart0");
     sleep_ms(1000);
     gpio_set_mask(1ul<<GPIO);
     gpio_put(pin, true);
        sleep_ms(250);
        gpio_clr_mask(1ul<<GPIO);
        gpio_put(pin, false);
        sleep_ms(250);
 uart_puts(uart1,"Hello, from UART1!");
 //uart_puts(uart0, "Hello world!");
}

  //  return 0;
}

📘串口中断测试例程

  • 🌿中断号:
enum irq_num_rp2040 { TIMER_IRQ_0 = 0, TIMER_IRQ_1 = 1, TIMER_IRQ_2 = 2, TIMER_IRQ_3 = 3, PWM_IRQ_WRAP = 4, USBCTRL_IRQ = 5, XIP_IRQ = 6, PIO0_IRQ_0 = 7, PIO0_IRQ_1 = 8, PIO1_IRQ_0 = 9, PIO1_IRQ_1 = 10, DMA_IRQ_0 = 11, DMA_IRQ_1 = 12, IO_IRQ_BANK0 = 13, IO_IRQ_QSPI = 14, SIO_IRQ_PROC0 = 15, SIO_IRQ_PROC1 = 16, CLOCKS_IRQ = 17, SPI0_IRQ = 18, SPI1_IRQ = 19, UART0_IRQ = 20, UART1_IRQ = 21, ADC_IRQ_FIFO = 22, I2C0_IRQ = 23, I2C1_IRQ = 24, RTC_IRQ = 25, IRQ_COUNT } RP2040
Interrupt numbers on RP2040 (used as typedef irq_num_t)


📑通过开启串口1接收中断,将串口1(0,1)接收到的数据,通过串口0(4,5)转发.

  • 📝测试代码:
/*
 烧录命令:openocd -f interface/cmsis-dap.cfg -f target/rp2040.cfg  -c  "program USART_Test.elf verify reset exit"

 jlink  openocd -f interface/jlink.cfg -f target/rp2040.cfg  -c  "program USART_Test.elf verify reset exit"
 * @FilePath: \USART_Test\USART_Test.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/uart.h"
#include "hardware/gpio.h"
#include "hardware/divider.h"
// #include "hardware/clocks.h"

// 检查是否定义了 LIB_PICO_STDIO_UART 宏
#if defined(LIB_PICO_STDIO_UART)
    // 如果定义了,取消这个宏
    #undef LIB_PICO_STDIO_UART
  #define LIB_PICO_STDIO_USB 1
#endif


// UART defines
// By default the stdout UART is `uart0`, so we will use the second one
#define UART_ID uart1
#define BAUD_RATE 115200  //串口波特率

// Use pins 4 and 5 for UART1
// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments
#define UART_TX_PIN 4
#define UART_RX_PIN 5

// GPIO defines
// Example uses GPIO 2
#define GPIO 2
static const uint pin = 25;

void uart1ISR(void)
{
while(uart_is_readable(UART_ID))
	{
		uint8_t ch = uart_getc(UART_ID);
		if(uart_is_writable(UART_ID))
		{
			uart_putc(uart0, ch);//转发数据
		}
  }
}

int main()
{
    stdio_init_all();//串口0,115200
    // Set the TX and RX pins by using the function select on the GPIO
    // Set datasheet for more information on function select
    gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
    gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
    // Set up our UART
    uart_init(UART_ID, BAUD_RATE);//串口1,115200
    uart_set_hw_flow(UART_ID, false, false);//关闭硬件流控
    irq_set_exclusive_handler(UART1_IRQ, uart1ISR);//配置中断回调
	irq_set_enabled(UART1_IRQ, true);//开启串口中断
    uart_set_irq_enables(uart1, true, false); //开启中断,接收中断,关闭发送中断
    irq_set_priority (21, 1); //设置中断优先级
    uart_set_fifo_enabled(uart1, true);//开启缓存
    hw_write_masked(&uart_get_hw(uart1)->ifls, 0b100 << UART_UARTIFLS_RXIFLSEL_LSB,
					UART_UARTIFLS_RXIFLSEL_BITS); //开启接收中断
 
    uart_is_enabled(UART_ID);

while (true) {
}

  • 测试结果
    在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2084017.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

安卓APK重签名并查看MD5值-2024最新版

重签名 命令行运行&#xff1a; apksigner sign --ks your_keystore.jks --out output.apk input.apk在这个命令中&#xff1a; –ks 或 --keystore 参数后面是你的keystore文件路径。 your_keystore.jks 是你的keystore文件。 –out 参数后面是输出的签名后的APK文件名。 out…

发布npm包到GitLab教程

之前在研究如何搭建UI组件库&#xff08;内部使用&#xff09;&#xff0c;其中重要的一步就是发布npm包到GitLab。中间踩了很多坑&#xff0c;在这里记录一下整个流程方便大家快速上手。不足之处欢迎指出&#x1f64f; 1. 获取Token 在gitlab中打开access tokens申请页面&am…

鲲鹏服务器之ARM探知

什么叫arm架构 ARM架构过去称作进阶精简指令集机器&#xff08;Advanced RISC Machine&#xff0c;更早称作&#xff1a;Acorn RISC Machine&#xff09;&#xff0c;是一个32位精简指令集&#xff08;RISC&#xff09;处理器架构&#xff0c;其广泛地使用在许多嵌入式系统设计…

CSS系列之详解overflow(四)

一、什么是溢出 CSS 的 overflow 属性用于控制元素内容溢出时的表现方式。当元素的内容超出其指定的尺寸范围时&#xff0c;就会出现溢出现象。比如&#xff0c;一个元素的高度设置是 80px&#xff0c;但内容高度不只是 80px&#xff0c;内容此时就叫做溢出了。 那需要注意的…

【QT学习】1-2 Liunx环境下QT5.12.9软件安装1——VMware17.0.0虚拟机安装

注意&#xff1a;如果电脑已经安装低版本的VMware&#xff0c;千万不要卸载&#xff0c;直接覆盖安装&#xff0c;更新到新的安装版本 1.点击.exe文件&#xff0c;右键以管理员身份运行&#xff0c;点击下一步&#xff0c;下一步 2.选择软件安装位置后&#xff0c;点击下一步。…

Datawhale X 李宏毅苹果书 AI夏令营(深度学习进阶)task3

批量归一化 其实归一化简单一点理解就类似于我们学过的数学中的每个数值减去平均值除以标准差。 神经网络中的批量归一化&#xff08;Batch Normalization&#xff0c;BN&#xff09;就是其中一个“把山铲平”的想法。不要小看优化这个问题&#xff0c;有时候就算误差表面是凸…

面试基本内容

1.类加载器 类加载器加载过程: 加载:&#xff08;将字节码文件加载到运行时数据区的方法区中/元空间&#xff09; 链接:&#xff08;验证:检查字节码文件是否合法—>准备:静态类变量赋值为默认值&#xff0c;不会实例变量分配初始化—>解析:将常量池引用&#xff0c;转化…

Java | Leetcode Java题解之第382题链表随机节点

题目&#xff1a; 题解&#xff1a; class Solution {ListNode head;Random random;public Solution(ListNode head) {this.head head;random new Random();}public int getRandom() {int i 1, ans 0;for (ListNode node head; node ! null; node node.next) {if (rando…

14.神经网络的基本骨架 - nn.Module 的使用

神经网络的基本骨架 - nn.Module 的使用 Pytorch官网左侧&#xff1a;Python API&#xff08;相当于package&#xff0c;提供了一些不同的工具&#xff09; 关于神经网络的工具主要在torch.nn里 网站地址&#xff1a;torch.nn — PyTorch 1.8.1 documentation Containers C…

【Linux】CodeServer:云IDE部署

Code-server 是一个开源项目&#xff0c;它允许你在任何地方通过浏览器访问 Visual Studio Code&#xff08;VS Code&#xff09;编辑器。这意味着你可以在远程服务器或云端运行 VS Code&#xff0c;并通过浏览器进行编码、调试和开发&#xff0c;而不需要在本地安装 VS Code。…

EtherCAT 转 ModbusTCP 网关

设备简介 本产品是 EtherCAT 和 Modbus TCP 网关&#xff0c;使用数据映射方式工作。 本产品在 EtherCAT 侧作为 EtherCAT 从站&#xff0c;接 TwinCAT 、 CodeSYS 、 PLC等&#xff1b;在 ModbusTCP 侧做为 ModbusTCP 主站&#xff08; Client &#xff09;或从站…

<数据集>无人机识别数据集<目标检测>

数据集格式&#xff1a;VOCYOLO格式 图片数量&#xff1a;9229张 标注数量(xml文件个数)&#xff1a;9229 标注数量(txt文件个数)&#xff1a;9229 标注类别数&#xff1a;1 标注类别名称&#xff1a;[UAV] 使用标注工具&#xff1a;labelImg 标注规则&#xff1a;对类别…

我如何解决 java.lang.ClassNotFoundException:javax.xml.bind.DatatypeConverter

优质博文&#xff1a;IT-BLOG-CN 问题 我如何解决java.lang.ClassNotFoundException&#xff1a;javax.xml.bind.DatatypeConverter 2024-08-25T02:31:25.46202:00 ERROR 21868 --- [fintonic-oauth] [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet…

springAI框架学习总结

springAI 1.springAI基本介绍 springAI是一个AI工程应用框架&#xff0c;其目标是将 Spring 生态系统设计原则&#xff08;例如可移植性和模块化设计&#xff09;应用于 AI 领域&#xff0c;并推广使用 POJO 作为 AI 领域应用程序的构建块。 2.特性 灵活的AIP支持chat,text…

Matlab R2022b使用Camera Calibrator工具箱张正友标定法进行相机标定附带标定前后对比代码

打开Camera Calibrator 在这添加你拍摄的图片 根据你每个方块的实际边长填写&#xff0c;我是15mm。 通俗一点&#xff0c;要k3就选3 Coefficients&#xff0c;否则为0&#xff1b;要p1、p2就选Tangential Distortion。然后进行计算。 可以点击右侧误差高的选中图像进行移…

【计算机网络】计算机网络的分层结构

为什么要分层&#xff1f;为什么要制定协议&#xff1f; 计算机网络功能复杂→采用分层结构&#xff0c;将诸多功能合理地划分在不同层次→对等层之间制定协议&#xff0c;以实现功能。

探索Scratch编程:重温《西游记-大战蜘蛛精》

小虎鲸Scratch资源站-免费Scratch作品源码,素材,教程分享平台! 在编程教育的浪潮中&#xff0c;Scratch以其简单易用的特点&#xff0c;成为了孩子们学习编程的热门选择。今天&#xff0c;我们很高兴向大家介绍一款精彩的Scratch教学案例作品——《西游记-大战蜘蛛精》。这不仅…

【JAVA入门】Day27 - 集合体系结构综述

【JAVA入门】Day27 - 集合体系结构综述 文章目录 【JAVA入门】Day27 - 集合体系结构综述一、单列集合体系结构1.1 Collection 集合的基本方法1.2 Collection 集合的遍历方式1.2.1 迭代器遍历1.2.2 增强 for 遍历1.2.3 利用 Lambda 表达式进行遍历 1.3 List 集合的基本方法1.4 L…

pyhton __init__.py

文章目录 包和模块__init__.py概述导入包和使用模块控制导入行为 包和模块 在这样一个工程中&#xff0c;pkg是包(package)&#xff0c;module1.py和module2.py是模块(module)&#xff0c;在模块中还有定义的方法、变量等&#xff0c;可以统称为功能。 import可以导入包&…

Node-RED解析巴法云/小米的传感器数据

在前面的博文&#xff08;Node-RED订阅巴法云的数据并展示-CSDN博客&#xff09;中提到过&#xff0c;Node-RED对JSON格式的数据很友好&#xff0c;直接可以解析。不过巴法云默认的格式是小米所采用的格式&#xff0c;即&#xff1a;#温度#湿度#开关#。采用这种格式的好处就是巴…