aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32u0
diff options
context:
space:
mode:
authorWillaWillNot <[email protected]>2025-11-22 17:22:55 -0500
committerWillaWillNot <[email protected]>2025-11-22 17:23:16 -0500
commit2589d3539903356c524b38f04f740b1735a80207 (patch)
treeaa6439513efaf28982ae7fb3a087d00140c13d6f /examples/stm32u0
parent764c921a573e42f510a74d61f31302b8609bbd6c (diff)
parenteb4e4100acbe03ee1d3726c948f91b6927a18125 (diff)
Synchronize with main branch
Diffstat (limited to 'examples/stm32u0')
-rw-r--r--examples/stm32u0/.cargo/config.toml4
-rw-r--r--examples/stm32u0/Cargo.toml6
-rw-r--r--examples/stm32u0/src/bin/lcd.rs412
3 files changed, 416 insertions, 6 deletions
diff --git a/examples/stm32u0/.cargo/config.toml b/examples/stm32u0/.cargo/config.toml
index 688347084..e9212cacb 100644
--- a/examples/stm32u0/.cargo/config.toml
+++ b/examples/stm32u0/.cargo/config.toml
@@ -1,6 +1,6 @@
1[target.'cfg(all(target_arch = "arm", target_os = "none"))'] 1[target.'cfg(all(target_arch = "arm", target_os = "none"))']
2# replace stm32u083rctx with your chip as listed in `probe-rs chip list` 2# replace stm32u083mctx with your chip as listed in `probe-rs chip list`
3runner = "probe-rs run --chip stm32u083rctx" 3runner = "probe-rs run --chip stm32u083mctx"
4 4
5[build] 5[build]
6target = "thumbv6m-none-eabi" 6target = "thumbv6m-none-eabi"
diff --git a/examples/stm32u0/Cargo.toml b/examples/stm32u0/Cargo.toml
index 9f5227e3f..8cc894cb3 100644
--- a/examples/stm32u0/Cargo.toml
+++ b/examples/stm32u0/Cargo.toml
@@ -6,8 +6,8 @@ license = "MIT OR Apache-2.0"
6publish = false 6publish = false
7 7
8[dependencies] 8[dependencies]
9# Change stm32u083rc to your chip name, if necessary. 9# Change stm32u083mc to your chip name, if necessary.
10embassy-stm32 = { version = "0.4.0", path = "../../embassy-stm32", features = [ "defmt", "time-driver-any", "stm32u083rc", "memory-x", "unstable-pac", "exti", "chrono"] } 10embassy-stm32 = { version = "0.4.0", path = "../../embassy-stm32", features = [ "defmt", "time-driver-any", "stm32u083mc", "memory-x", "unstable-pac", "exti", "chrono"] }
11embassy-sync = { version = "0.7.2", path = "../../embassy-sync", features = ["defmt"] } 11embassy-sync = { version = "0.7.2", path = "../../embassy-sync", features = ["defmt"] }
12embassy-executor = { version = "0.9.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt"] } 12embassy-executor = { version = "0.9.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt"] }
13embassy-time = { version = "0.5.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } 13embassy-time = { version = "0.5.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
@@ -19,9 +19,7 @@ defmt-rtt = "1.0.0"
19 19
20cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } 20cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
21cortex-m-rt = "0.7.0" 21cortex-m-rt = "0.7.0"
22embedded-hal = "0.2.6"
23panic-probe = { version = "1.0.0", features = ["print-defmt"] } 22panic-probe = { version = "1.0.0", features = ["print-defmt"] }
24heapless = { version = "0.8", default-features = false }
25 23
26micromath = "2.0.0" 24micromath = "2.0.0"
27chrono = { version = "0.4.38", default-features = false } 25chrono = { version = "0.4.38", default-features = false }
diff --git a/examples/stm32u0/src/bin/lcd.rs b/examples/stm32u0/src/bin/lcd.rs
new file mode 100644
index 000000000..2b34d4ef1
--- /dev/null
+++ b/examples/stm32u0/src/bin/lcd.rs
@@ -0,0 +1,412 @@
1#![no_std]
2#![no_main]
3
4use defmt::*;
5use embassy_executor::Spawner;
6use embassy_stm32::lcd::{Bias, BlinkFreq, BlinkSelector, Config, Duty, Lcd, LcdPin};
7use embassy_stm32::peripherals::LCD;
8use embassy_stm32::time::Hertz;
9use embassy_time::Duration;
10use {defmt_rtt as _, panic_probe as _};
11
12#[embassy_executor::main]
13async fn main(_spawner: Spawner) {
14 let mut config = embassy_stm32::Config::default();
15 // The RTC clock = the LCD clock and must be running
16 {
17 use embassy_stm32::rcc::*;
18 config.rcc.sys = Sysclk::PLL1_R;
19 config.rcc.hsi = true;
20 config.rcc.pll = Some(Pll {
21 source: PllSource::HSI, // 16 MHz
22 prediv: PllPreDiv::DIV1,
23 mul: PllMul::MUL7, // 16 * 7 = 112 MHz
24 divp: None,
25 divq: None,
26 divr: Some(PllRDiv::DIV2), // 112 / 2 = 56 MHz
27 });
28 config.rcc.ls = LsConfig::default_lsi();
29 }
30
31 let p = embassy_stm32::init(config);
32 info!("Hello World!");
33
34 let mut config = Config::default();
35 config.bias = Bias::Third;
36 config.duty = Duty::Quarter;
37 config.target_fps = Hertz(100);
38
39 let mut lcd = Lcd::new(
40 p.LCD,
41 config,
42 p.PC3,
43 [
44 LcdPin::new_com(p.PA8),
45 LcdPin::new_com(p.PA9),
46 LcdPin::new_com(p.PA10),
47 LcdPin::new_seg(p.PB1),
48 LcdPin::new_com(p.PB9),
49 LcdPin::new_seg(p.PB11),
50 LcdPin::new_seg(p.PB14),
51 LcdPin::new_seg(p.PB15),
52 LcdPin::new_seg(p.PC4),
53 LcdPin::new_seg(p.PC5),
54 LcdPin::new_seg(p.PC6),
55 LcdPin::new_seg(p.PC8),
56 LcdPin::new_seg(p.PC9),
57 LcdPin::new_seg(p.PC10),
58 LcdPin::new_seg(p.PC11),
59 LcdPin::new_seg(p.PD8),
60 LcdPin::new_seg(p.PD9),
61 LcdPin::new_seg(p.PD12),
62 LcdPin::new_seg(p.PD13),
63 LcdPin::new_seg(p.PD0),
64 LcdPin::new_seg(p.PD1),
65 LcdPin::new_seg(p.PD3),
66 LcdPin::new_seg(p.PD4),
67 LcdPin::new_seg(p.PD5),
68 LcdPin::new_seg(p.PD6),
69 LcdPin::new_seg(p.PE7),
70 LcdPin::new_seg(p.PE8),
71 LcdPin::new_seg(p.PE9),
72 ],
73 );
74
75 lcd.set_blink(BlinkSelector::All, BlinkFreq::Hz4);
76 {
77 let mut buffer = DisplayBuffer::new();
78 for i in 0..4 {
79 buffer.write_colon(i);
80 buffer.write(&mut lcd);
81 embassy_time::Timer::after_millis(200).await;
82 buffer.write_dot(i);
83 buffer.write(&mut lcd);
84 embassy_time::Timer::after_millis(200).await;
85 }
86 for i in 0..4 {
87 buffer.write_bar(i);
88 buffer.write(&mut lcd);
89 embassy_time::Timer::after_millis(200).await;
90 }
91 }
92
93 embassy_time::Timer::after_millis(1000).await;
94
95 lcd.set_blink(BlinkSelector::None, BlinkFreq::Hz4);
96
97 const MESSAGE: &str = "Hello embassy people. Hope you like this LCD demo :} ";
98 loop {
99 print_message(MESSAGE, &mut lcd, Duration::from_millis(250)).await;
100 print_message(characters::ALL_CHARS, &mut lcd, Duration::from_millis(500)).await;
101 }
102}
103
104async fn print_message(message: &str, lcd: &mut Lcd<'_, LCD>, delay: Duration) {
105 let mut display_buffer = DisplayBuffer::new();
106
107 let mut char_buffer = [' '; 6];
108 for char in message.chars() {
109 char_buffer.copy_within(1.., 0);
110 char_buffer[5] = char;
111
112 display_buffer.clear();
113 for (i, char) in char_buffer.iter().enumerate() {
114 display_buffer.write_char(i, *char);
115 }
116 display_buffer.write(lcd);
117
118 embassy_time::Timer::after(delay).await;
119 }
120}
121
122/// Display layout for the U0-DK
123mod display_layout {
124 // Character layout. There are 6 characters, left-to-right
125 // T
126 // ─────────
127 // │ N │
128 // │ │ │ │ │
129 // TL │ └┐ │ ┌┘ │ TR
130 // │NW│ │ │NE│
131 // │ │ │
132 // W─── ───E
133 // │ │ │
134 // │SW│ │ │SE│
135 // BL │ ┌┘ │ └┐ │ BR
136 // │ │ │ │ │
137 // │ S │
138 // ─────────
139 // B
140
141 pub const CHAR_N_COM: u8 = 3;
142 pub const CHAR_N_SEG: [u8; 6] = [39, 37, 35, 48, 26, 33];
143 pub const CHAR_NW_COM: u8 = 3;
144 pub const CHAR_NW_SEG: [u8; 6] = [49, 38, 36, 34, 27, 24];
145 pub const CHAR_W_COM: u8 = 0;
146 pub const CHAR_W_SEG: [u8; 6] = CHAR_NW_SEG;
147 pub const CHAR_SW_COM: u8 = 2;
148 pub const CHAR_SW_SEG: [u8; 6] = CHAR_NW_SEG;
149 pub const CHAR_S_COM: u8 = 2;
150 pub const CHAR_S_SEG: [u8; 6] = [22, 6, 46, 11, 15, 29];
151 pub const CHAR_SE_COM: u8 = 3;
152 pub const CHAR_SE_SEG: [u8; 6] = CHAR_S_SEG;
153 pub const CHAR_E_COM: u8 = 0;
154 pub const CHAR_E_SEG: [u8; 6] = [23, 45, 47, 14, 28, 32];
155 pub const CHAR_NE_COM: u8 = 2;
156 pub const CHAR_NE_SEG: [u8; 6] = CHAR_N_SEG;
157 pub const CHAR_T_COM: u8 = 1;
158 pub const CHAR_T_SEG: [u8; 6] = CHAR_N_SEG;
159 pub const CHAR_TL_COM: u8 = 1;
160 pub const CHAR_TL_SEG: [u8; 6] = CHAR_NW_SEG;
161 pub const CHAR_BL_COM: u8 = 0;
162 pub const CHAR_BL_SEG: [u8; 6] = CHAR_S_SEG;
163 pub const CHAR_B_COM: u8 = 1;
164 pub const CHAR_B_SEG: [u8; 6] = CHAR_S_SEG;
165 pub const CHAR_BR_COM: u8 = 1;
166 pub const CHAR_BR_SEG: [u8; 6] = CHAR_E_SEG;
167 pub const CHAR_TR_COM: u8 = 0;
168 pub const CHAR_TR_SEG: [u8; 6] = CHAR_N_SEG;
169
170 pub const COLON_COM: u8 = 2;
171 pub const COLON_SEG: [u8; 4] = [23, 45, 47, 14];
172 pub const DOT_COM: u8 = 3;
173 pub const DOT_SEG: [u8; 4] = COLON_SEG;
174 /// COM + SEG, bar from top to bottom
175 pub const BAR: [(u8, u8); 4] = [(2, 28), (3, 28), (2, 32), (3, 32)];
176}
177
178mod characters {
179 use super::CharSegment::{self, *};
180
181 pub const CHAR_0: &[CharSegment] = &[T, TL, BL, B, BR, TR, NW, SE];
182 pub const CHAR_1: &[CharSegment] = &[NE, TR, BR];
183 pub const CHAR_2: &[CharSegment] = &[T, BL, B, TR, E, W];
184 pub const CHAR_3: &[CharSegment] = &[T, B, BR, TR, E];
185 pub const CHAR_4: &[CharSegment] = &[TL, BR, TR, E, W];
186 pub const CHAR_5: &[CharSegment] = &[T, TL, B, BR, E, W];
187 pub const CHAR_6: &[CharSegment] = &[T, TL, BL, B, BR, E, W];
188 pub const CHAR_7: &[CharSegment] = &[T, NE, S];
189 pub const CHAR_8: &[CharSegment] = &[T, TL, BL, B, BR, TR, E, W];
190 pub const CHAR_9: &[CharSegment] = &[T, TL, BR, TR, E, W];
191
192 pub const CHAR_COLON: &[CharSegment] = &[N, S];
193 pub const CHAR_SEMICOLON: &[CharSegment] = &[N, SW];
194 pub const CHAR_EQUALS: &[CharSegment] = &[E, W, B];
195 pub const CHAR_SLASH: &[CharSegment] = &[SW, NE];
196 pub const CHAR_BACKSLASH: &[CharSegment] = &[SE, NW];
197 pub const CHAR_PLUS: &[CharSegment] = &[N, E, S, W];
198 pub const CHAR_STAR: &[CharSegment] = &[NE, N, NW, SE, S, SW];
199 pub const CHAR_QUOTE: &[CharSegment] = &[N];
200 pub const CHAR_BACKTICK: &[CharSegment] = &[NW];
201 pub const CHAR_DASH: &[CharSegment] = &[W, E];
202 pub const CHAR_COMMA: &[CharSegment] = &[SW];
203 pub const CHAR_DOT: &[CharSegment] = &[S];
204 pub const CHAR_CURLYOPEN: &[CharSegment] = &[T, NW, W, SW, B];
205 pub const CHAR_CURLYCLOSE: &[CharSegment] = &[T, NE, E, SE, B];
206 pub const CHAR_AMPERSAND: &[CharSegment] = &[T, NE, NW, W, BL, B, SE];
207
208 pub const CHAR_A: &[CharSegment] = &[T, TL, TR, E, W, BL, BR];
209 pub const CHAR_B: &[CharSegment] = &[T, TR, BR, B, N, S, E];
210 pub const CHAR_C: &[CharSegment] = &[T, TL, BL, B];
211 pub const CHAR_D: &[CharSegment] = &[T, TR, BR, B, N, S];
212 pub const CHAR_E: &[CharSegment] = &[T, TL, BL, B, W];
213 pub const CHAR_F: &[CharSegment] = &[T, TL, BL, W];
214 pub const CHAR_G: &[CharSegment] = &[T, TL, BL, B, BR, E];
215 pub const CHAR_H: &[CharSegment] = &[TL, BL, E, W, TR, BR];
216 pub const CHAR_I: &[CharSegment] = &[T, N, S, B];
217 pub const CHAR_J: &[CharSegment] = &[TR, BR, B, BL];
218 pub const CHAR_K: &[CharSegment] = &[TL, BL, W, NE, SE];
219 pub const CHAR_L: &[CharSegment] = &[TL, BL, B];
220 pub const CHAR_M: &[CharSegment] = &[BL, TL, NW, NE, TR, BR];
221 pub const CHAR_N: &[CharSegment] = &[BL, TL, NW, SE, BR, TR];
222 pub const CHAR_O: &[CharSegment] = &[T, TL, BL, B, BR, TR];
223 pub const CHAR_P: &[CharSegment] = &[BL, TL, T, TR, E, W];
224 pub const CHAR_Q: &[CharSegment] = &[T, TL, BL, B, BR, TR, SE];
225 pub const CHAR_R: &[CharSegment] = &[BL, TL, T, TR, E, W, SE];
226 pub const CHAR_S: &[CharSegment] = &[T, NW, E, BR, B];
227 pub const CHAR_T: &[CharSegment] = &[T, N, S];
228 pub const CHAR_U: &[CharSegment] = &[TL, BL, B, BR, TR];
229 pub const CHAR_V: &[CharSegment] = &[TL, BL, SW, NE];
230 pub const CHAR_W: &[CharSegment] = &[TL, BL, SW, SE, BR, TR];
231 pub const CHAR_X: &[CharSegment] = &[NE, NW, SE, SW];
232 pub const CHAR_Y: &[CharSegment] = &[NE, NW, S];
233 pub const CHAR_Z: &[CharSegment] = &[T, NE, SW, B];
234
235 pub const CHAR_UNKNOWN: &[CharSegment] = &[N, NW, W, SW, S, SE, E, NE, T, TL, BL, B, BR, TR];
236
237 pub const ALL_CHARS: &str =
238 "0 1 2 3 4 5 6 7 8 9 : ; = / \\ + * ' ` - , . { } & A B C D E F G H I J K L M N O P Q R S T U V W X Y Z � ";
239
240 pub fn get_char_segments(val: char) -> &'static [CharSegment] {
241 match val {
242 val if val.is_whitespace() => &[],
243
244 '0' => CHAR_0,
245 '1' => CHAR_1,
246 '2' => CHAR_2,
247 '3' => CHAR_3,
248 '4' => CHAR_4,
249 '5' => CHAR_5,
250 '6' => CHAR_6,
251 '7' => CHAR_7,
252 '8' => CHAR_8,
253 '9' => CHAR_9,
254
255 ':' => CHAR_COLON,
256 ';' => CHAR_SEMICOLON,
257 '=' => CHAR_EQUALS,
258 '/' => CHAR_SLASH,
259 '\\' => CHAR_BACKSLASH,
260 '+' => CHAR_PLUS,
261 '*' => CHAR_STAR,
262 '\'' => CHAR_QUOTE,
263 '`' => CHAR_BACKTICK,
264 '-' => CHAR_DASH,
265 ',' => CHAR_COMMA,
266 '.' => CHAR_DOT,
267 '{' => CHAR_CURLYOPEN,
268 '}' => CHAR_CURLYCLOSE,
269 '&' => CHAR_AMPERSAND,
270
271 'A' | 'a' => CHAR_A,
272 'B' | 'b' => CHAR_B,
273 'C' | 'c' => CHAR_C,
274 'D' | 'd' => CHAR_D,
275 'E' | 'e' => CHAR_E,
276 'F' | 'f' => CHAR_F,
277 'G' | 'g' => CHAR_G,
278 'H' | 'h' => CHAR_H,
279 'I' | 'i' => CHAR_I,
280 'J' | 'j' => CHAR_J,
281 'K' | 'k' => CHAR_K,
282 'L' | 'l' => CHAR_L,
283 'M' | 'm' => CHAR_M,
284 'N' | 'n' => CHAR_N,
285 'O' | 'o' => CHAR_O,
286 'P' | 'p' => CHAR_P,
287 'Q' | 'q' => CHAR_Q,
288 'R' | 'r' => CHAR_R,
289 'S' | 's' => CHAR_S,
290 'T' | 't' => CHAR_T,
291 'U' | 'u' => CHAR_U,
292 'V' | 'v' => CHAR_V,
293 'W' | 'w' => CHAR_W,
294 'X' | 'x' => CHAR_X,
295 'Y' | 'y' => CHAR_Y,
296 'Z' | 'z' => CHAR_Z,
297
298 _ => CHAR_UNKNOWN,
299 }
300 }
301}
302
303pub struct DisplayBuffer {
304 pixels: [u64; 4],
305}
306
307impl DisplayBuffer {
308 pub const fn new() -> Self {
309 Self { pixels: [0; 4] }
310 }
311
312 pub fn clear(&mut self) {
313 *self = Self::new();
314 }
315
316 fn write_char_segment(&mut self, index: usize, value: CharSegment) {
317 defmt::assert!(index < 6);
318 let (com, segments) = value.get_com_seg();
319 self.pixels[com as usize] |= 1 << segments[index];
320 }
321
322 pub fn write_char(&mut self, index: usize, val: char) {
323 let segments = characters::get_char_segments(val);
324
325 for segment in segments {
326 self.write_char_segment(index, *segment);
327 }
328 }
329
330 pub fn write(&self, lcd: &mut Lcd<'_, LCD>) {
331 lcd.write_com_segments(0, self.pixels[0]);
332 lcd.write_com_segments(1, self.pixels[1]);
333 lcd.write_com_segments(2, self.pixels[2]);
334 lcd.write_com_segments(3, self.pixels[3]);
335 lcd.submit_frame();
336 }
337
338 pub fn write_colon(&mut self, index: usize) {
339 defmt::assert!(index < 4);
340 self.pixels[display_layout::COLON_COM as usize] |= 1 << display_layout::COLON_SEG[index];
341 }
342
343 pub fn write_dot(&mut self, index: usize) {
344 defmt::assert!(index < 4);
345 self.pixels[display_layout::DOT_COM as usize] |= 1 << display_layout::DOT_SEG[index];
346 }
347
348 pub fn write_bar(&mut self, index: usize) {
349 defmt::assert!(index < 4);
350 let (bar_com, bar_seg) = display_layout::BAR[index];
351 self.pixels[bar_com as usize] |= 1 << bar_seg;
352 }
353}
354
355impl Default for DisplayBuffer {
356 fn default() -> Self {
357 Self::new()
358 }
359}
360
361#[derive(Debug, Clone, Copy)]
362enum CharSegment {
363 /// North
364 N,
365 /// North west
366 NW,
367 /// West
368 W,
369 /// South west
370 SW,
371 /// South
372 S,
373 /// South East
374 SE,
375 /// East
376 E,
377 /// North East
378 NE,
379 /// Top
380 T,
381 /// Top left
382 TL,
383 /// Bottom left
384 BL,
385 /// Bottom
386 B,
387 /// Bottom right
388 BR,
389 /// Top right
390 TR,
391}
392
393impl CharSegment {
394 fn get_com_seg(&self) -> (u8, [u8; 6]) {
395 match self {
396 CharSegment::N => (display_layout::CHAR_N_COM, display_layout::CHAR_N_SEG),
397 CharSegment::NW => (display_layout::CHAR_NW_COM, display_layout::CHAR_NW_SEG),
398 CharSegment::W => (display_layout::CHAR_W_COM, display_layout::CHAR_W_SEG),
399 CharSegment::SW => (display_layout::CHAR_SW_COM, display_layout::CHAR_SW_SEG),
400 CharSegment::S => (display_layout::CHAR_S_COM, display_layout::CHAR_S_SEG),
401 CharSegment::SE => (display_layout::CHAR_SE_COM, display_layout::CHAR_SE_SEG),
402 CharSegment::E => (display_layout::CHAR_E_COM, display_layout::CHAR_E_SEG),
403 CharSegment::NE => (display_layout::CHAR_NE_COM, display_layout::CHAR_NE_SEG),
404 CharSegment::T => (display_layout::CHAR_T_COM, display_layout::CHAR_T_SEG),
405 CharSegment::TL => (display_layout::CHAR_TL_COM, display_layout::CHAR_TL_SEG),
406 CharSegment::BL => (display_layout::CHAR_BL_COM, display_layout::CHAR_BL_SEG),
407 CharSegment::B => (display_layout::CHAR_B_COM, display_layout::CHAR_B_SEG),
408 CharSegment::BR => (display_layout::CHAR_BR_COM, display_layout::CHAR_BR_SEG),
409 CharSegment::TR => (display_layout::CHAR_TR_COM, display_layout::CHAR_TR_SEG),
410 }
411 }
412}