-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmt.rs
127 lines (108 loc) · 3.6 KB
/
rmt.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#![no_std]
#![no_main]
use esp_backtrace as _;
use hal::{
clock::ClockControl,
peripherals::Peripherals,
prelude::*,
rmt::{Channel0, PulseCode, TxChannel, TxChannelConfig, TxChannelCreator},
timer::TimerGroup,
Delay, Rmt, Rtc, IO,
};
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let mut system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
// Disable the RTC and TIMG watchdog timers
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
let timer_group0 = TimerGroup::new(
peripherals.TIMG0,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt0 = timer_group0.wdt;
rtc.swd.disable();
rtc.rwdt.disable();
wdt0.disable();
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
// Initialize the RMT peripheral.
let rmt = Rmt::new(
peripherals.RMT,
// 80 MHz is the default RMT clock frequency, according to:
// https://github.com/esp-rs/esp-hal/blob/0c47ceda3afbc71dc2f540589811257eab51199f/esp-hal-common/src/soc/esp32c3/mod.rs#L28
// It appears that arbitrary frequencies are not supported here; only integer divisors of the base frequency:
// https://github.com/esp-rs/esp-hal/blob/0c47ceda3afbc71dc2f540589811257eab51199f/esp-hal-common/src/rmt.rs#L234
80u32.MHz(),
&mut system.peripheral_clock_control,
&clocks,
)
.unwrap();
let mut delay = Delay::new(&clocks);
let config = TxChannelConfig {
clk_divider: 1,
..TxChannelConfig::default()
};
// Our LED is hooked up to pin 7.
let mut channel = rmt.channel0.configure(io.pins.gpio7, config).unwrap();
loop {
channel = color(channel, 64, 0, 0);
delay.delay_ms(500u32);
channel = color(channel, 0, 64, 0);
delay.delay_ms(500u32);
channel = color(channel, 0, 0, 64);
delay.delay_ms(500u32);
}
}
// The WS2812 spec sheet has two different durations.
// The shorter duration is 0.40µs. That's 32 cycles at 80MHz.
const SHORT: u16 = 32;
// The shorter duration is 0.85µs. That's 68 cycles at 80MHz.
const LONG: u16 = 68;
// We send a "one" bit by setting the pin high for a long time and low for
// a short time.
const ONE: PulseCode = PulseCode {
level1: true,
length1: LONG,
level2: false,
length2: SHORT,
};
// We send a "zero" bit by setting the pin high for a short time and low for
// a long time.
const ZERO: PulseCode = PulseCode {
level1: true,
length1: SHORT,
level2: false,
length2: LONG,
};
// We send a "reset" code by setting the pin low for 50µs. That's 4000 cycles
// at 80MHz.
const RESET: PulseCode = PulseCode {
level1: false,
length1: 0,
level2: false,
length2: 4000,
};
// Tell the led to change color.
fn color<const CH: u8>(ch: Channel0<CH>, r: u8, g: u8, b: u8) -> Channel0<CH> {
// We need to send 25 pulses: 24 bits and the reset code.
let mut buf = [0u32; 25];
// According to the spec sheet, the order of bytes is GRB.
write_byte(&mut buf[0..8], g);
write_byte(&mut buf[8..16], r);
write_byte(&mut buf[16..24], b);
buf[24] = RESET.into();
ch.transmit(&buf).wait().unwrap()
}
// Convert a byte into a pulse code. Store the result in the buffer `out`, which
// must be 8 bytes long.
fn write_byte(out: &mut [u32], mut b: u8) {
let one: u32 = ONE.into();
let zero: u32 = ZERO.into();
for sig in out {
// Highest order bits get sent first.
let bit = b & 0b1000_0000;
*sig = if bit != 0 { one } else { zero };
b <<= 1;
}
}