Kaynağa Gözat

display: Revert to simple EPD display example code.

Mathias Gottschlag 5 yıl önce
ebeveyn
işleme
e9f89f739f

+ 0
- 88
display/firmware/src/board/led.rs Dosyayı Görüntüle

@@ -1,88 +0,0 @@
1
-//! User LEDs
2
-
3
-use mkl25z4_hal::gpio;
4
-use embedded_hal::digital::v2::OutputPin;
5
-
6
-pub struct Led {
7
-    pub red: gpio::gpiob::PB18<gpio::Output<gpio::PushPull>>,
8
-    pub green: gpio::gpiob::PB19<gpio::Output<gpio::PushPull>>,
9
-    pub blue: gpio::gpiod::PD1<gpio::Output<gpio::PushPull>>,
10
-}
11
-
12
-impl Led {
13
-    /// Sets the LED's color
14
-    pub fn set_color(&mut self, red: bool, green: bool, blue: bool) {
15
-        if red {
16
-            self.red.set_low().ok();
17
-        } else {
18
-            self.red.set_high().ok();
19
-        }
20
-        if green {
21
-            self.green.set_low().ok();
22
-        } else {
23
-            self.green.set_high().ok();
24
-        }
25
-        if blue {
26
-            self.blue.set_low().ok();
27
-        } else {
28
-            self.blue.set_high().ok();
29
-        }
30
-        // NOTE(safe) atomic write
31
-        /*unsafe {
32
-            if red {
33
-                (*GPIOB::ptr()).pcor.write(|w| w.ptco().bits(1 << 18));
34
-            } else {
35
-                (*GPIOB::ptr()).psor.write(|w| w.ptso().bits(1 << 18));
36
-            }
37
-            if green {
38
-                (*GPIOB::ptr()).pcor.write(|w| w.ptco().bits(1 << 19));
39
-            } else {
40
-                (*GPIOB::ptr()).psor.write(|w| w.ptso().bits(1 << 19));
41
-            }
42
-            if blue {
43
-                (*GPIOD::ptr()).pcor.write(|w| w.ptco().bits(1 << 1));
44
-            } else {
45
-                (*GPIOD::ptr()).psor.write(|w| w.ptso().bits(1 << 1));
46
-            }
47
-        }*/    }
48
-}
49
-
50
-//impl Led {
51
-//    /// Turns off the LED
52
-//    pub fn off(&self) {
53
-//        // NOTE(safe) atomic write
54
-//        unsafe { (*GPIOG.get()).bsrr.write(|w| w.bits(1 << (self.i + 16))) }
55
-//    }
56
-//
57
-//    /// Turns on the LED
58
-//    pub fn on(&self) {
59
-//        // NOTE(safe) atomic write
60
-//        unsafe { (*GPIOG.get()).bsrr.write(|w| w.bits(1 << self.i)) }
61
-//    }
62
-//}
63
-
64
-/*/// Initializes all the user LEDs
65
-pub fn init(portb: &PORTB, gpiob: &GPIOB, portd: &PORTD, gpiod: &GPIOD, sim: &SIM) {
66
-    // Power up peripherals
67
-    //rcc.ahb1enr.modify(|_, w| w.gpiogen().set_bit());
68
-    sim.scgc5.modify(|_, w| w.portb().set_bit().portd().set_bit());
69
-
70
-    // Configure pins as outputs
71
-    portb.pcr[18].modify(|_, w| w.mux().bits(1));
72
-    portb.pcr[19].modify(|_, w| w.mux().bits(1));
73
-    portd.pcr[1].modify(|_, w| w.mux().bits(1));
74
-    gpiod.pcor.write(|w| unsafe { w.ptco().bits(1 << 1) });
75
-    gpiob.pcor.write(|w| unsafe { w.ptco().bits((1 << 18) | (1 << 19)) });
76
-    gpiod.pddr.write(|w| unsafe { w.pdd().bits(1 << 1) });
77
-    gpiob.pddr.write(|w| unsafe { w.pdd().bits((1 << 18) | (1 << 19)) });
78
-    /*gpiog
79
-        .moder
80
-        .modify(
81
-            |_, w| {
82
-                w.moder13()
83
-                    .bits(1)
84
-                    .moder14()
85
-                    .bits(1)
86
-            },
87
-        );*/
88
-}*/

+ 0
- 1
display/firmware/src/board/mod.rs Dosyayı Görüntüle

@@ -1 +0,0 @@
1
-pub mod led;

+ 75
- 347
display/firmware/src/main.rs Dosyayı Görüntüle

@@ -1,27 +1,25 @@
1 1
 #![no_main]
2 2
 #![no_std]
3 3
 
4
-mod board;
5 4
 mod pins;
6 5
 mod assets {
7 6
     include!(concat!(env!("OUT_DIR"), "/assets.rs"));
8 7
 }
9 8
 
10
-
11 9
 use cortex_m_semihosting::{debug, hprintln};
12
-use mkl25z4_hal as hal;
13
-use panic_semihosting as _;
14
-use mkl25z4_hal::gpio::GpioExt;
15
-use mkl25z4_hal::spi::{Phase, Polarity, Spi};
16
-use mkl25z4_hal::time::{NonCopyableMonoTimer, U32Ext};
17
-use mkl25z4_hal::timer::{Timer, TimerInterrupt};
18
-use embedded_hal::digital::v2::OutputPin;
19
-use embedded_epd::{gui, Display};
20 10
 use embedded_epd as epd;
21
-use nb::block;
11
+use embedded_epd::{gui, Display};
12
+use embedded_hal::blocking::delay::DelayMs;
13
+use embedded_hal::digital::v2::OutputPin;
22 14
 use embedded_hal::timer::CountDown;
23
-
24
-use board::led;
15
+use epd_waveshare::{epd4in2::*, prelude::*};
16
+use mkl25z4_hal as hal;
17
+use mkl25z4_hal::clocks::ClockConfiguration;
18
+use mkl25z4_hal::time::NonCopyableMonoTimer;
19
+use mkl25z4_hal::time::U32Ext;
20
+use mkl25z4_hal::timer::Timer;
21
+use nb::block;
22
+use panic_semihosting as _;
25 23
 
26 24
 struct EPDTimer<Timer> {
27 25
     timer: Timer,
@@ -47,116 +45,53 @@ where
47 45
 
48 46
 #[rtfm::app(device = mkl25z4_hal::mkl25z4, peripherals = true)]
49 47
 const APP: () = {
50
-    struct Resources {
51
-    }
48
+    struct Resources {}
52 49
 
53 50
     #[init]
54 51
     fn init(ctx: init::Context) {
55 52
         let mut sim = ctx.device.SIM;
56 53
         mkl25z4_hal::watchdog::disable(&mut sim);
57
-        let clocks = mkl25z4_hal::clocks::init();
58
-
59
-        let mut gpiob = ctx.device.GPIOB.split(&mut sim);
60
-        let mut gpioc = ctx.device.GPIOC.split(&mut sim);
61
-        let mut gpiod = ctx.device.GPIOD.split(&mut sim);
62
-
63
-        let mut led = led::Led {
64
-            red: gpiob.pb18.into_push_pull_output(&mut gpiob.pddr),
65
-            green: gpiob.pb19.into_push_pull_output(&mut gpiob.pddr),
66
-            blue: gpiod.pd1.into_push_pull_output(&mut gpiod.pddr),
67
-        };
68
-        //led::init(&p.device.PORTB, &p.device.GPIOB, &p.device.PORTD, &p.device.GPIOD, &p.device.SIM);
69
-        led.set_color(true, true, true);
70
-
71
-        /*for _ in 0..2 {
72
-            time.delay_ms(1000);
73
-            led.set_color(false, false, true);
74
-            time.delay_ms(1000);
75
-            led.set_color(false, true, false);
76
-        }*/
77
-
78
-        let mosi = gpioc.pc6.into_alternate(&mut gpioc.pddr);
79
-        let miso = gpioc.pc7.into_alternate(&mut gpioc.pddr);
80
-        let sck = gpioc.pc5.into_alternate(&mut gpioc.pddr);
81
-        let mode = hal::spi::Mode {
82
-            polarity: Polarity::IdleLow,
83
-            phase: Phase::CaptureOnFirstTransition,
84
-        };
85
-        let spi0 = Spi::spi0(ctx.device.SPI0, mosi, miso, sck, mode, &mut sim);
86
-
87
-        // Initialize the display.
88
-        let busy = gpioc.pc3.into_floating_input(&mut gpioc.pddr);
89
-        let mut reset = gpioc.pc2.into_push_pull_output(&mut gpioc.pddr);
90
-        reset.set_high().unwrap();
91
-        let mut data_cmd = gpioc.pc1.into_push_pull_output(&mut gpioc.pddr);
92
-        data_cmd.set_high().unwrap();
93
-        let mut cs = gpioc.pc0.into_push_pull_output(&mut gpioc.pddr);
94
-        cs.set_high().unwrap();
95
-
96
-        let t = EPDTimer {
97
-            timer: Timer::pit(ctx.device.PIT, hal::time::Hertz(1), clocks, &mut sim),
98
-        };
99
-
100
-        let mut display = epd::gdew042z15::GDEW042Z15::new(spi0, busy, reset, data_cmd, cs, t);
101
-        block!(display.init()).ok();
102
-        block!(display.start_frame()).ok();
103
-        led.set_color(false, false, false);
104
-        let layout = gui::Layout::new(
105
-            400,
106
-            300,
107
-            gui::VerticalSplit::expand_bottom(
108
-                24,
109
-                gui::Fill::new(epd::Color::White),
110
-                gui::VerticalSplit::expand_bottom(
111
-                    1,
112
-                    gui::Fill::new(epd::Color::Black),
113
-                    gui::HorizontalSplit::expand_right(
114
-                        48,
115
-                        gui::Fill::new(epd::Color::White),
116
-                        //gui::Image::new(&assets::ALARM_CLOCK_36),
117
-                        gui::HorizontalSplit::expand_right(
118
-                            1,
119
-                            gui::Fill::new(epd::Color::Black),
120
-                            gui::HorizontalSplit::expand_left(
121
-                                49,
122
-                                gui::VerticalSplit::expand_bottom(
123
-                                    132,
124
-                                    gui::Align::new(
125
-                                        gui::HorizontalAlign::Center,
126
-                                        gui::VerticalAlign::Top,
127
-                                        gui::Text::new("12:34", &assets::ROBOTO_100),
128
-                                    ),
129
-                                    gui::Align::new(
130
-                                        gui::HorizontalAlign::Center,
131
-                                        gui::VerticalAlign::Top,
132
-                                        gui::HorizontalSplit::expand_right(
133
-                                            36,
134
-                                            gui::Image::new(&assets::ALARM_CLOCK_36),
135
-                                            gui::Text::new("Do. 8:00", &assets::ROBOTO_30),
136
-                                        ),
137
-                                    ),
138
-                                ),
139
-                                gui::Fill::new(epd::Color::White),
140
-                            ),
141
-                        ),
142
-                    ),
143
-                ),
144
-            ),
54
+        let clocks = ClockConfiguration::new()
55
+            .use_irc()
56
+            .core_clock(24.mhz())
57
+            .bus_clock(24.mhz())
58
+            .apply(&mut sim, ctx.device.OSC0, ctx.device.MCG);
59
+
60
+        let mut pins = pins::Pins::configure(
61
+            &mut sim,
62
+            clocks,
63
+            ctx.device.GPIOA,
64
+            ctx.device.GPIOB,
65
+            ctx.device.GPIOC,
66
+            ctx.device.GPIOD,
67
+            ctx.device.GPIOE,
68
+            ctx.device.SPI0,
69
+            ctx.device.SPI1,
145 70
         );
146
-        let mut row_buffer = [0u8; 400 / 8];
147
-        layout.render(&mut display, &mut row_buffer);
148
-        display.end_frame();
149
-        led.set_color(true, true, false);
150
-
151
-        let (_, _, _, _, _, mut t) = display.destroy();
152
-
153
-        /*let pit_ = display.time.deactivate(&mut p.device.SIM);*/
154
-        t.start(epd::Hertz(1));
155
-        t.timer.enable_interrupt();
156
-        let mut nvic = ctx.core.NVIC;
157
-        nvic.enable(mkl25z4::Interrupt::PIT);
158 71
 
159
-        led.set_color(true, true, true);
72
+        // Enable power.
73
+        pins.display_pwr.set_low().ok();
74
+        pins.display_rst.set_high().unwrap();
75
+        pins.display_dc.set_high().unwrap();
76
+        pins.display_cs.set_high().unwrap();
77
+
78
+        let mut timer = NonCopyableMonoTimer::new(ctx.device.PIT, clocks, &mut sim);
79
+
80
+        let mut epd = EPD4in2::new(
81
+            &mut pins.display_bme_spi,
82
+            pins.display_cs,
83
+            pins.display_busy,
84
+            pins.display_dc,
85
+            pins.display_rst,
86
+            &mut timer,
87
+        )
88
+        .unwrap();
89
+
90
+        epd.set_background_color(Color::Black);
91
+        let checkerboard = Checkerboard::new(400, 300, true);
92
+        epd.update_frame_stream(&mut pins.display_bme_spi, checkerboard)
93
+            .unwrap();
94
+        epd.display_frame(&mut pins.display_bme_spi).unwrap();
160 95
     }
161 96
 
162 97
     #[idle]
@@ -172,241 +107,34 @@ const APP: () = {
172 107
     }
173 108
 };
174 109
 
175
-/*#![no_main]
176
-#![no_std]
177
-
178
-extern crate cortex_m;
179
-extern crate cortex_m_rt as rt;
180
-//extern crate cortex_m_rtfm as rtfm;
181
-extern crate embedded_epd as epd;
182
-extern crate embedded_hal;
183
-extern crate mkl25z4;
184
-extern crate mkl25z4_hal as hal;
185
-//extern crate panic_halt;
186
-extern crate panic_semihosting;
187
-extern crate void;
188
-#[macro_use(block)]
189
-extern crate nb;
190
-extern crate bme280;
191
-
192
-mod board;
193
-mod assets {
194
-    include!(concat!(env!("OUT_DIR"), "/assets.rs"));
110
+struct Checkerboard {
111
+    width: usize,
112
+    height: usize,
113
+    index: usize,
114
+    buffer: [u8; 1],
195 115
 }
196 116
 
197
-use bme280::BME280;
198
-use board::led;
199
-use embedded_hal::prelude::*;
200
-use embedded_hal::timer::CountDown;
201
-use epd::{gui, Display};
202
-use hal::gpio::GpioExt;
203
-use hal::i2c::BlockingI2c;
204
-use hal::spi::{Phase, Polarity, Spi};
205
-use hal::time::{NonCopyableMonoTimer, U32Ext};
206
-use hal::timer::{Timer, TimerInterrupt};
207
-use rt::pre_init;
208
-use rtfm::{app, Threshold};
209
-
210
-/*use core::sync::atomic;
211
-use core::sync::atomic::Ordering;*/
212
-
213
-struct EPDTimer<Timer> {
214
-    timer: Timer,
215
-}
216
-
217
-impl<Timer> CountDown for EPDTimer<Timer>
218
-where
219
-    Timer: CountDown<Time = hal::time::Hertz>,
220
-{
221
-    type Time = epd::Hertz;
222
-
223
-    fn start<T>(&mut self, timeout: T)
224
-    where
225
-        T: Into<epd::Hertz>,
226
-    {
227
-        self.timer.start(hal::time::Hertz(timeout.into().0))
228
-    }
229
-
230
-    fn wait(&mut self) -> nb::Result<(), void::Void> {
231
-        self.timer.wait()
232
-    }
233
-}
234
-
235
-#[pre_init]
236
-unsafe fn disable_watchdog() {
237
-    use mkl25z4::SIM;
238
-    (*SIM::ptr()).copc.write(|w| w.bits(0));
239
-}
240
-
241
-app! {
242
-    device: mkl25z4,
243
-
244
-    resources: {
245
-        static ON: bool = false;
246
-        static pit: hal::timer::Timer<mkl25z4::PIT>;
247
-        static rgb_led: led::Led;
248
-    },
249
-
250
-    tasks: {
251
-        PIT: {
252
-            path: pit_handler,
253
-            resources: [pit, rgb_led, ON],
254
-        },
255
-    },
256
-}
257
-
258
-fn init(mut p: init::Peripherals, _r: init::Resources) -> init::LateResources {
259
-    mkl25z4_hal::watchdog::disable(&mut p.device.SIM);
260
-    let clocks = mkl25z4_hal::clocks::init();
261
-    //let time = hal::time::MonoTimer::new(p.device.PIT, clocks, &mut p.device.SIM);
262
-
263
-    let mut gpiob = p.device.GPIOB.split(&mut p.device.SIM);
264
-    let mut gpioc = p.device.GPIOC.split(&mut p.device.SIM);
265
-    let mut gpiod = p.device.GPIOD.split(&mut p.device.SIM);
266
-
267
-    let mut led = led::Led {
268
-        red: gpiob.pb18.into_push_pull_output(&mut gpiob.pddr),
269
-        green: gpiob.pb19.into_push_pull_output(&mut gpiob.pddr),
270
-        blue: gpiod.pd1.into_push_pull_output(&mut gpiod.pddr),
271
-    };
272
-    //led::init(&p.device.PORTB, &p.device.GPIOB, &p.device.PORTD, &p.device.GPIOD, &p.device.SIM);
273
-    led.set_color(true, true, true);
274
-
275
-    let timer = NonCopyableMonoTimer::new(p.device.PIT, clocks, &mut p.device.SIM);
276
-    let scl = gpiob.pb2.into_alternate(&mut gpiob.pddr);
277
-    let sda = gpiob.pb3.into_alternate(&mut gpiob.pddr);
278
-    let i2c0 = BlockingI2c::i2c0(
279
-        p.device.I2C0,
280
-        sda,
281
-        scl,
282
-        100000u32.hz(),
283
-        timer,
284
-        clocks,
285
-        &mut p.device.SIM,
286
-        1000000,
287
-        10,
288
-        1000000,
289
-        1000000,
290
-    );
291
-    let mut bme280 = BME280::new_primary(i2c0);
292
-    let (_, _, _, timer) = i2c0.free(&mut p.device.SIM);
293
-    let pit_ = timer.free(&mut p.device.SIM);
294
-
295
-    /*for _ in 0..2 {
296
-        time.delay_ms(1000);
297
-        led.set_color(false, false, true);
298
-        time.delay_ms(1000);
299
-        led.set_color(false, true, false);
300
-    }*/
301
-
302
-    let mosi = gpioc.pc6.into_alternate(&mut gpioc.pddr);
303
-    let miso = gpioc.pc7.into_alternate(&mut gpioc.pddr);
304
-    let sck = gpioc.pc5.into_alternate(&mut gpioc.pddr);
305
-    let mode = hal::spi::Mode {
306
-        polarity: Polarity::IdleLow,
307
-        phase: Phase::CaptureOnFirstTransition,
308
-    };
309
-    let spi0 = Spi::spi0(p.device.SPI0, mosi, miso, sck, mode, &mut p.device.SIM);
310
-
311
-    // Initialize the display.
312
-    let busy = gpioc.pc3.into_floating_input(&mut gpioc.pddr);
313
-    let mut reset = gpioc.pc2.into_push_pull_output(&mut gpioc.pddr);
314
-    reset.set_high();
315
-    let mut data_cmd = gpioc.pc1.into_push_pull_output(&mut gpioc.pddr);
316
-    data_cmd.set_high();
317
-    let mut cs = gpioc.pc0.into_push_pull_output(&mut gpioc.pddr);
318
-    cs.set_high();
319
-
320
-    let t = EPDTimer {
321
-        timer: Timer::pit(pit_, hal::time::Hertz(1), clocks, &mut p.device.SIM),
322
-    };
323
-
324
-    let mut display = epd::gdew042z15::GDEW042Z15::new(spi0, busy, reset, data_cmd, cs, t);
325
-    block!(display.init()).ok();
326
-    block!(display.start_frame()).ok();
327
-    led.set_color(false, false, false);
328
-    let layout = gui::Layout::new(
329
-        400,
330
-        300,
331
-        gui::VerticalSplit::expand_bottom(
332
-            24,
333
-            gui::Fill::new(epd::Color::White),
334
-            gui::VerticalSplit::expand_bottom(
335
-                1,
336
-                gui::Fill::new(epd::Color::Black),
337
-                gui::HorizontalSplit::expand_right(
338
-                    48,
339
-                    gui::Fill::new(epd::Color::White),
340
-                    //gui::Image::new(&assets::ALARM_CLOCK_36),
341
-                    gui::HorizontalSplit::expand_right(
342
-                        1,
343
-                        gui::Fill::new(epd::Color::Black),
344
-                        gui::HorizontalSplit::expand_left(
345
-                            49,
346
-                            gui::VerticalSplit::expand_bottom(
347
-                                132,
348
-                                gui::Align::new(
349
-                                    gui::HorizontalAlign::Center,
350
-                                    gui::VerticalAlign::Top,
351
-                                    gui::Text::new("12:34", &assets::ROBOTO_100),
352
-                                ),
353
-                                gui::Align::new(
354
-                                    gui::HorizontalAlign::Center,
355
-                                    gui::VerticalAlign::Top,
356
-                                    gui::HorizontalSplit::expand_right(
357
-                                        36,
358
-                                        gui::Image::new(&assets::ALARM_CLOCK_36),
359
-                                        gui::Text::new("Do. 8:00", &assets::ROBOTO_30),
360
-                                    ),
361
-                                ),
362
-                            ),
363
-                            gui::Fill::new(epd::Color::White),
364
-                        ),
365
-                    ),
366
-                ),
367
-            ),
368
-        ),
369
-    );
370
-    let mut row_buffer = [0u8; 400 / 8];
371
-    layout.render(&mut display, &mut row_buffer);
372
-    display.end_frame();
373
-    led.set_color(true, true, false);
374
-
375
-    let (_, _, _, _, _, mut t) = display.destroy();
376
-
377
-    /*let pit_ = display.time.deactivate(&mut p.device.SIM);*/
378
-    t.start(epd::Hertz(1));
379
-    t.timer.enable_interrupt();
380
-    p.core.NVIC.enable(mkl25z4::Interrupt::PIT);
381
-
382
-    led.set_color(true, true, true);
383
-
384
-    init::LateResources {
385
-        pit: t.timer,
386
-        rgb_led: led,
117
+impl Checkerboard {
118
+    fn new(width: usize, height: usize, start_black: bool) -> Self {
119
+        Self {
120
+            width: width / 8,
121
+            height,
122
+            index: 0,
123
+            buffer: [if start_black { 0x55 } else { 0xaa }],
124
+        }
387 125
     }
388 126
 }
389 127
 
390
-fn idle() -> ! {
391
-    // Sleep
392
-    loop {
393
-        // wfi needs to be disabled for GDB
394
-        /*// add some side effect to prevent this from turning into a UDF instruction
395
-        // see rust-lang/rust#28728 for details
396
-        atomic::compiler_fence(Ordering::SeqCst);*/
397
-        rtfm::wfi();
128
+impl DisplayStream for Checkerboard {
129
+    fn next<'a>(&'a mut self) -> Option<&'a [u8]> {
130
+        if self.index != self.width * self.height {
131
+            self.index += 1;
132
+            if (self.index - 1) % self.width == 0 {
133
+                self.buffer[0] = !self.buffer[0];
134
+            }
135
+            Some(&self.buffer)
136
+        } else {
137
+            None
138
+        }
398 139
     }
399 140
 }
400
-
401
-// TASKS
402
-
403
-// Toggle the state of the LED
404
-fn pit_handler(_t: &mut Threshold, mut r: PIT::Resources) {
405
-    if *r.ON {
406
-        r.rgb_led.set_color(false, false, true);
407
-    } else {
408
-        r.rgb_led.set_color(true, false, false);
409
-    }
410
-    r.pit.wait().ok();
411
-    *r.ON = !*r.ON;
412
-}*/

Loading…
İptal
Kaydet