最近学习zephyr。 想在正点原子stm32f103zet6运行。
问下腾讯元宝以及,我的以往了解的知识,完成如下功能。
我有正点原子stm32f103zet6开发板, 想在zephyr基于waveshare_open103z 开发板,实现串口1,PA9,PA10 实现 printf(“hello world”), 以及led PB5的闪烁。 要怎么做
元宝回答
在 Zephyr 里,waveshare_open103z这块板子默认 USART1 已经绑定到了 zephyr,console,且 PA9/PA10 就是 STM32F103 的 USART1 默认引脚,所以串口基本不用大改;主要是把 PB5 的 LED 通过设备树 overlay 加进去,再写个同时闪灯 + 打印的 main.c就行。
下面按最小可用步骤来。
一、确认串口 USART1(PA9/PA10)现状
waveshare_open103z.dts里已经有:
chosen{zephyr,console=&usart1;zephyr,shell-uart=&usart1;};&usart1{pinctrl-0=<&usart1_tx_pa9&usart1_rx_pa10>;pinctrl-names="default";current-speed=<115200>;status="okay";};TX = PA9,RX = PA10(STM32F1 默认复用,pinctrl 在 stm32f1-pinctrl.dtsi里定义好了)
zephyr,console指向 usart1⇒ printk()/ printf(newlib)都会从这里输出
结论:串口这边你什么都不用改,只要保证 prj.conf开了串口控制台即可。
二、用 Overlay 添加 PB5 LED
板上默认 LED 可能不是 PB5,因此建议在应用层用 app.overlay追加一个 LED 节点,不影响官方板级 dts。
在项目根目录创建 app.overlay, 和prj.conf 同级目录
/{leds{compatible="gpio-leds";led_pb5:led_pb5{gpios=<&gpiob5GPIO_ACTIVE_LOW>;label="User PB5 LED";};};aliases{led0=&led_pb5;/* blinky / gpio_sample 常用 led0 */};};&gpiob 5⇒ PB5
GPIO_ACTIVE_LOW:看你硬件,正点原子板载 LED 通常是 低电平点亮,如果不是就改成 GPIO_ACTIVE_HIGH
起别名叫 led0,后面 C 代码直接用 DEVICE_DT_ALIAS(led0)或 DT_ALIAS(led0)。
三、prj.conf 配置(串口 + GPIO)
在项目中创建 / 修改 prj.conf:
# 串口/console CONFIG_SERIAL=y CONFIG_UART_STM32=y CONFIG_CONSOLE=y CONFIG_UART_CONSOLE=y CONFIG_PRINTK=y CONFIG_STDOUT_CONSOLE=y#GPIO(LED 要用)CONFIG_GPIO=y # 可选:用 printf 需要 libc CONFIG_NEWLIB_LIBC=yZephyr 里 推荐用 printk(),它直接走 console(USART1),不需要重定向;
如果你一定要用 printf(),就开 CONFIG_NEWLIB_LIBC=y或 CONFIG_MINIMAL_LIBC=y。
编译代码
cd~/zephyrproject/zephyr/west build-p always-b waveshare_open103z samples/basic/blinky编译完成后打印结果:
Parsing/data/workspace/zephyrproject/zephyr/Kconfig Loaded configuration '/data/workspace/zephyrproject/zephyr/boards/waveshare/open103z/waveshare_open103z_defconfig' Merged configuration '/data/workspace/zephyrproject/zephyr/samples/basic/blinky/prj.conf' Configuration saved to '/data/workspace/zephyrproject/zephyr/build/zephyr/.config' Kconfig header saved to '/data/workspace/zephyrproject/zephyr/build/zephyr/include/generated/zephyr/autoconf.h'--Found GnuLd:/root/zephyr-sdk-1.0.1/gnu/arm-zephyr-eabi/arm-zephyr-eabi/bin/ld.bfd(found version"2.43.1")--The C compiler identification is GNU14.3.0--The CXX compiler identification is GNU14.3.0--The ASM compiler identification is GNU--Found assembler:/root/zephyr-sdk-1.0.1/gnu/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc--Using ccache:/usr/bin/ccache--Found gen_kobject_list:/data/workspace/zephyrproject/zephyr/scripts/build/gen_kobject_list.py--Configuringdone(8.3s)--Generatingdone(0.1s)--Build files have been written to:/data/workspace/zephyrproject/zephyr/build--west build:building application[1/153]Preparing syscall dependency handling[2/153]Generating include/generated/zephyr/version.h--Zephyr version:4.4.99(/data/workspace/zephyrproject/zephyr),build:v4.4.0-2726-g3c2422272499[153/153]Linking C executable zephyr/zephyr.elf Memory region Used Size Region Size%age Used FLASH:15208B512KB2.90%RAM:4136B64KB6.31%SRAM0:0B64KB0.00%IDT_LIST:0B32KB0.00%Generating files from/data/workspace/zephyrproject/zephyr/build/zephyr/zephyr.elfforboard:waveshare_open103z/stm32f103xe编译后的 zephyr.elf 和 zephyr.hex 文件在 zephyr/build/zephyr/zephyr.hex ,
可以用JLINK 或 cmsis-dap 下载到开发板,
观察现象看到LED0闪烁,串口打印
LED state:ON LED state:OFF