硬件: Renesas FPB-RA6E2 + 板載 LED 和按鍵
目標: 實現按鍵控制 LED 的開關狀態,并通過 UART 輸出按鍵事件日志,驗證 GPIO 和串口通信功能。
1. 硬件連接與引腳定義
包含 GPIO 輸入輸出功能與 UART 數據傳輸引腳。
| 功能 | 物理引腳 (Pin) | 信號定義 | 接線說明 |
|---|---|---|---|
| 按鍵輸入 | P304 | Digital In (SW0) | 板載按鍵無需接線 |
| LED 輸出 | P207 | Digital Out (LED0) | 板載 LED 無需接線 |
| UART 發送 | P411 | UART TX | 連接到調試器 |
| UART 接收 | P410 | UART RX | 連接到調試器 |
2. 軟件環境配置
2.1 Device Tree Overlay (app.overlay)
必須明確指定 GPIO 和 UART 的物理引腳綁定,并開啟外設狀態。
chosen {
zephyr,console = &uart0; /* in externalzephyrboardsrenesasfpb_ra6e2fpb_ra6e2.dts:22 */
zephyr,shell-uart = &uart0; /* in externalzephyrboardsrenesasfpb_ra6e2fpb_ra6e2.dts:23 */
};
/* GPIO 配置 */
leds {
compatible = "gpio-leds";
led1: led1 {
gpios = < &ioport2 7 GPIO_ACTIVE_HIGH >;
label = "LED1";
};
led2: led2 {
gpios = < &ioport2 6 GPIO_ACTIVE_HIGH >;
label = "LED2";
};
};
buttons {
compatible = "gpio-keys";
button0: s1 {
gpios = < &ioport3 4 (GPIO_PULL_UP | GPIO_ACTIVE_LOW) >;
label = "Push button switch 1";
zephyr,code = < INPUT_KEY_0 >;
};
};
/* UART 配置 */
&uart0 {
status = "okay";
pinctrl-0 = < &uart0_default >;
pinctrl-names = "default";
current-speed = < 115200 >; /* 波特率設置 */
};
};
};2.2 Kconfig 配置 (prj.conf)
確保啟用了 GPIO 和 UART 驅動支持。
CONFIG_GPIO=y
CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y
3. 代碼邏輯分析
3.1 主程序代碼(main.c)
/*
* Copyright (c) 2016 Open-RnD Sp. z o.o.
* Copyright (c) 2020 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*
* NOTE: If you are looking into an implementation of button events with
* debouncing, check out `input` subsystem and `samples/subsys/input/input_dump`
* example instead.
*/
#include < zephyr/kernel.h >
#include < zephyr/device.h >
#include < zephyr/drivers/gpio.h >
#include < zephyr/sys/util.h >
#include < zephyr/sys/printk.h >
#include < inttypes.h >
#define SLEEP_TIME_MS 1
/*
* Get button configuration from the devicetree sw0 alias. This is mandatory.
*/
#define SW0_NODE DT_ALIAS(sw0)
#if !DT_NODE_HAS_STATUS_OKAY(SW0_NODE)
#error "Unsupported board: sw0 devicetree alias is not defined"
#endif
static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(SW0_NODE, gpios,
{0});
static struct gpio_callback button_cb_data;
/*
* The led0 devicetree alias is optional. If present, we'll use it
* to turn on the LED whenever the button is pressed.
*/
static struct gpio_dt_spec led = GPIO_DT_SPEC_GET_OR(DT_ALIAS(led0), gpios,
{0});
void button_pressed(const struct device *dev, struct gpio_callback *cb,
uint32_t pins)
{
printk("Button pressed at %" PRIu32 "n", k_cycle_get_32());
}
int main(void)
{
int ret;
if (!gpio_is_ready_dt(&button)) {
printk("Error: button device %s is not readyn",
button.port- >name);
return 0;
}
ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
if (ret != 0) {
printk("Error %d: failed to configure %s pin %dn",
ret, button.port- >name, button.pin);
return 0;
}
ret = gpio_pin_interrupt_configure_dt(&button,
GPIO_INT_EDGE_TO_ACTIVE);
if (ret != 0) {
printk("Error %d: failed to configure interrupt on %s pin %dn",
ret, button.port- >name, button.pin);
return 0;
}
gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin));
gpio_add_callback(button.port, &button_cb_data);
printk("Set up button at %s pin %dn", button.port- >name, button.pin);
if (led.port && !gpio_is_ready_dt(&led)) {
printk("Error %d: LED device %s is not ready; ignoring itn",
ret, led.port- >name);
led.port = NULL;
}
if (led.port) {
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT);
if (ret != 0) {
printk("Error %d: failed to configure LED device %s pin %dn",
ret, led.port- >name, led.pin);
led.port = NULL;
} else {
printk("Set up LED at %s pin %dn", led.port- >name, led.pin);
}
}
printk("Press the buttonn");
if (led.port) {
while (1) {
/* If we have an LED, match its state to the button's. */
int val = gpio_pin_get_dt(&button);
if (val >= 0) {
gpio_pin_set_dt(&led, val);
}
k_msleep(SLEEP_TIME_MS);
}
}
return 0;
}
3.2 核心流程
程序采用中斷驅動的方式,實現了按鍵事件捕獲與 LED 狀態切換,同時通過 UART 輸出日志。
- 初始化階段
- 配置 GPIO 引腳:P002 設置為輸入模式,啟用內部上拉電阻;P003 設置為輸出模式。
- 配置 UART:波特率 115200,8N1 格式。
- 注冊按鍵中斷回調函數。
- 運行時邏輯
- 按鍵按下時觸發中斷,切換 LED 狀態(開/關)。
- 在中斷回調中,記錄按鍵事件并通過 UART 輸出日志。
- 主循環保持空閑,等待中斷觸發。
3.1 關鍵 API 使用
以下是代碼中使用的關鍵 API:
- GPIO 初始化與控制
gpio_pin_configure(gpio_dev, PIN_BTN, GPIO_INPUT | GPIO_PULL_UP); gpio_pin_configure(gpio_dev, PIN_LED, GPIO_OUTPUT); - UART 數據發送
printk(str, args...); - 中斷注冊與處理
gpio_pin_interrupt_configure(gpio_dev, PIN_BTN, GPIO_INT_EDGE_TO_ACTIVE); gpio_init_callback(&btn_cb, button_pressed, BIT(PIN_BTN)); gpio_add_callback(gpio_dev, &btn_cb);
4. 實驗現象與數據分析
4.1 串口日志 (UART Output)
終端應顯示如下數據流:
Button pressed at 91714454
Button pressed at 228051750
Button pressed at 342154106
Button pressed at 431864898
Button pressed at 514931258
...
4.2 板載 LED 行為
- 初始狀態:LED 關閉。
- 每次按鍵按下,LED 狀態翻轉(開/關)。
- 視覺效果:LED 應與按鍵動作同步,無明顯延遲。
5. 測評總結
本程序成功演示了 Renesas RA6E2 在 Zephyr RTOS 下的 GPIO 和 UART 外設控制。通過按鍵事件驅動 LED 狀態切換,并實時記錄日志到 UART,直觀地展示了嵌入式系統中“輸入-處理-輸出”的基本工作流程。代碼結構清晰,適配了最新的驅動 API,適用于初學者學習和開發者快速驗證硬件功能。
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
單片機
+關注
關注
6076文章
45495瀏覽量
670323 -
瑞薩
+關注
關注
37文章
22481瀏覽量
90865 -
Zephyr
+關注
關注
0文章
58瀏覽量
6579
發布評論請先 登錄
相關推薦
熱點推薦
當RA MCU遇見Zephyr系列(3)——在Vs code中配置Zephyr集成開發環境
RA生態工作室關注我們上一篇文章介紹了如何在VScode中使用瑞薩官方插件為RA芯片創建項目與項目調試,相信大家對RA在VScode中的開發
【RA-Eco-RA6M4開發板評測】——2.串口打印
3_ON;delay_ms(200);LED3_OFF;
printf(\"歡迎試用瑞薩電子RA-Eco-RA6M4開發板評測\\\\r\\
發表于 07-16 13:20
【瑞薩RA6E2】zephyr系統適配、點亮led及uart輸出測試
就行了。如下:
到這里板卡基本也就適配完成,但是由于ek_ra6e2板子的串口、led、按鍵等外設引腳和RA-Eco-RA6E2-64PIN-V1.0 開發板存在差異,所以我們要
發表于 11-16 17:57
【瑞薩FPB-RA6E2試用】基礎功能使用1
,首先從點燈開始。
zephyr的LED工程
在配置好環境以后,需要驗證配置的環境是否正常,生成的代碼是否能夠在開發板上成功跑通。在零基礎的情況下,參考瑞薩
發表于 12-28 21:35
【瑞薩FPB-RA6E2試用】【原創】基于【瑞薩FPB-RA6E2】Windows 下Zephyr RTOS自定義項目開發全流程(創建-編譯-燒錄-串口調試)
【原創】基于【瑞薩FPB-RA6E2】Windows 下Zephyr RTOS自定義項目開發全流程(創建 · 編譯 · 燒錄 · 串口調試)
《整體全流程演示》。
使用的硬件:
發表于 01-01 15:35
【瑞薩FPB-RA6E2試用】【瑞薩RA × Zephyr開發板評測】+以按鍵控制LED燈
安裝過程中常會出現安裝失敗的情況,為此需要多次重復才能確保完成。
在完成安裝后,即可進行瑞薩工程的創建。
具體其操作為,點擊界面左側下方的“Create Renesas RA Project”。隨后,將
發表于 01-05 17:04
【瑞薩FPB-RA6E2試用】GPIO-Zephyr RTOS 閃爍程序及設備樹理解
://docs.zephyrproject.org/latest/samples/basic/blinky/README.html
5.3 項目代碼
*附件:fpb_ra6e2_basic_blinky.zip
感謝電子發燒友平臺,感謝瑞
發表于 01-13 14:37
【瑞薩RA × Zephyr開發板評測】基于PWM的電機轉速控制
。
Zephyr對于瑞薩RAE62 開發板的支持很友好,很多例程直接下載進去即可運行,比如LED,UART,DAC等,
配置外設階段需要通過
發表于 01-16 01:19
【瑞薩FPB-RA6E2試用】環境配置及基礎任務代碼示例
【瑞薩RA × Zephyr開發板評測】 開發板基礎功能測試與代碼移植
本報告兩篇開發內容主要以入門級的外設為標準進行測試,在入門級測試需求
發表于 01-24 20:47
【瑞薩RA × Zephyr評測】LED、按鍵和UART
評論