树莓派PICO原生开发-串口
得益于 pico sdk,我们可以直接使用 printf 等函数来在串口上输出,如同运行在电脑上的普通程序那样。代码如下:
#include "pico/stdlib.h" #include <cstdio> int main() { stdio_init_all(); puts("Hello, world!"); while (true) { tight_loop_contents(); } }
pico/stdlib.h 大概是原生SDK吧
特殊地,pico sdk 还帮我们实现了基于 usb 协议的串口。要打开这个功能,我们只需修改 CMakelists.txt,加入这一行:
pico_enable_stdio_usb([项目名] 1)
此时,stdio_init_all() 就会帮我们初始化 usb 串口。
按照文档,usb 串口不能与 swd 调试一同使用。
按照文档,usb 串口不能与 swd 调试一同使用。
stdio_init_all() 函数的作用是初始化所有串口信道,包括 uart 和 usb。开发者也可以通过 stdio_uart_init() 和 stdio_usb_init() 来自行初始化。
而 scanf() 也是可用的,于是我们可以从电脑向 RP2040 发送数据:
但是有 reddit 回答说 pico sdk 并没有官方支持 scanf()。更保险的做法是使用 getchar_timeout_us()。
while(true) { printf("input name: "); scanf("%s", name); printf("\nHello, %s\n\n", name); }

浙公网安备 33010602011771号