如何使用rust语言编写一个RMT使用示例?
Posted: Thu Oct 17, 2024 5:36 pm
示例代码
日志输出:
仓库地址:https://github.com/banchen21/esp32c3_idf_rmt_demo
红外设备:
Code: Select all
use std::{thread, time::Duration};
use anyhow::Result;
use esp_idf_hal::rmt::RmtChannel;
use esp_idf_hal::{
prelude::Peripherals,
rmt::{
config::CarrierConfig, FixedLengthSignal, PinState, Pulse, PulseTicks, RxRmtConfig,
RxRmtDriver, TxRmtConfig, TxRmtDriver,
},
};
use esp_idf_svc::eventloop::EspSystemEventLoop;
use log::{debug, error, info};
fn main() -> Result<()> {
esp_idf_svc::sys::link_patches();
esp_idf_svc::log::EspLogger::initialize_default();
let peripherals = Peripherals::take().unwrap();
let sysloop = EspSystemEventLoop::take()?;
let rx_config = RxRmtConfig::new()
.clock_divider(80)
.carrier(Some(CarrierConfig::new()));
let rx_channel = peripherals.rmt.channel1;
let rx_pin = peripherals.pins.gpio4;
let mut rx = match RxRmtDriver::new(rx_channel, rx_pin, &rx_config, 64) {
Ok(rx) => {
info!("RMT接收器初始化成功");
rx
}
Err(e) => {
error!("RMT接收器初始化失败: {:?}", e);
return Ok(());
}
};
rx.start()?;
// Prepare the config. 中文:准备配置
let tx_config = TxRmtConfig::new().clock_divider(80);
// Retrieve the output pin and channel from peripherals. 中文:获取输出引脚和通道
let tx_channel = peripherals.rmt.channel0;
let tx_pin = peripherals.pins.gpio11;
// Create an RMT transmitter. 中文:创建一个 RMT 传输器
let mut tx = // 初始化RMT发送器
match TxRmtDriver::new(tx_channel, tx_pin, &tx_config) {
Ok(mut tx) => {
info!("RMT发送器初始化成功");
tx
},
Err(e) => {
error!("RMT发送器初始化失败: {:?}", e);
return Ok(());
}
};
thread::spawn(move || {
loop {
// Receive the signal. 中文:接收信号
let buf = &mut [(Pulse::zero(), Pulse::zero())];
while let Ok(signal) = rx.receive(buf, 10) {
info!("Received signal: {:?}", signal);
}
}
});
thread::spawn(move || {
loop {
// Prepare signal pulse signal to be sent. 中文:准备要发送的信号脉冲
let low = Pulse::new(PinState::Low, PulseTicks::new(10).unwrap());
let high = Pulse::new(PinState::High, PulseTicks::new(10).unwrap());
let mut signal = FixedLengthSignal::<2>::new();
signal.set(0, &(low, high)).unwrap();
signal.set(1, &(high, low)).unwrap();
// Transmit the signal. 中文:发送信号
tx.start(signal).unwrap();
}
});
loop {
thread::sleep(Duration::from_secs(1));
}
}
Code: Select all
[2024-10-17T17:08:55Z INFO ] Serial port: '/dev/ttyACM0'
[2024-10-17T17:08:55Z INFO ] Connecting...
[2024-10-17T17:08:55Z INFO ] Using flash stub
Chip type: esp32c3 (revision v0.4)
Crystal frequency: 40 MHz
Flash size: 4MB
Features: WiFi, BLE
MAC address: dc:da:0c:f9:24:4c
App/part. size: 549,792/4,128,768 bytes, 13.32%
[2024-10-17T17:08:56Z INFO ] Segment at address '0x0' has not changed, skipping write
[2024-10-17T17:08:56Z INFO ] Segment at address '0x8000' has not changed, skipping write
[00:00:23] [========================================] 265/265 0x10000 [2024-10-17T17:09:23Z INFO ] Flashing has completed!
Commands:
CTRL+R Reset chip
CTRL+C Exit
I (30) boot: ESP-IDF v5.1.2-342-gbcf1645e44 2nd stage bootloader
I (30) boot: compile time Dec 12 2023 10:50:58
I (31) boot: chip revision: v0.4
I (34) boot.esp32c3: SPI Speed : 40MHz
I (39) boot.esp32c3: SPI Mode : DIO
I (44) boot.esp32c3: SPI Flash Size : 4MB
I (49) boot: Enabling RNG early entropy source...
I (54) boot: Partition Table:
I (58) boot: ## Label Usage Type ST Offset Length
I (65) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (72) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (80) boot: 2 factory factory app 00 00 00010000 003f0000
I (87) boot: End of partition table
I (92) esp_image: segment 0: paddr=00010020 vaddr=3c060020 size=2354ch (144716) map
I (132) esp_image: segment 1: paddr=00033574 vaddr=3fc8ba00 size=0123ch ( 4668) load
I (134) esp_image: segment 2: paddr=000347b8 vaddr=40380000 size=0b844h ( 47172) load
I (150) esp_image: segment 3: paddr=00040004 vaddr=00000000 size=00014h ( 20)
I (150) esp_image: segment 4: paddr=00040020 vaddr=42000020 size=56354h (353108) map
I (237) boot: Loaded app from partition at offset 0x10000
I (237) boot: Disabling RNG early entropy source...
I (248) cpu_start: Unicore app
I (257) cpu_start: Pro cpu start user code
I (257) cpu_start: cpu freq: 160000000 Hz
I (257) cpu_start: Application information:
I (260) cpu_start: Project name: libespidf
I (265) cpu_start: App version: 65855c2-dirty
I (271) cpu_start: Compile time: Oct 17 2024 20:32:08
I (277) cpu_start: ELF file SHA256: 000000000...
I (282) cpu_start: ESP-IDF: v5.2.2
I (287) cpu_start: Min chip rev: v0.3
I (292) cpu_start: Max chip rev: v1.99
I (297) cpu_start: Chip rev: v0.4
I (302) heap_init: Initializing. RAM available for dynamic allocation:
I (309) heap_init: At 3FC8DC00 len 00032400 (201 KiB): RAM
I (315) heap_init: At 3FCC0000 len 0001C710 (113 KiB): Retention RAM
I (322) heap_init: At 3FCDC710 len 00002950 (10 KiB): Retention RAM
I (329) heap_init: At 50000010 len 00001FD8 (7 KiB): RTCRAM
I (336) spi_flash: detected chip: generic
I (340) spi_flash: flash io: dio
W (344) rmt(legacy): legacy driver is deprecated, please migrate to `driver/rmt_tx.h` and/or `driver/rmt_rx.h`
W (354) timer_group: legacy driver is deprecated, please migrate to `driver/gptimer.h`
I (363) sleep: Configure to isolate all GPIO pins in sleep state
I (370) sleep: Enable automatic switching of GPIO sleep configuration
I (377) main_task: Started on CPU0
I (377) main_task: Calling app_main()
E (377) rmt(legacy): rmt_set_gpio(546): RMT CHANNEL ERR
E (387) rmt(legacy): rmt_config(692): set gpio for RMT driver failed
E (397) idf_hi: RMT接收器初始化失败: ESP_ERR_INVALID_ARG (error code 258)
I (407) esp_idf_svc::eventloop: System event loop dropped
I (407) main_task: Returned from app_main()
红外设备: