~ruther/simple-clock

ref: b92738752d8b6cc66a2b634b6528a7f5d3f2a315 simple-clock/source/src/main.rs -rw-r--r-- 11.1 KiB
b9273875 — František Boháček feat(src): add meaningful message to panic in Calendar::days_in_month 1 year, 8 months ago
                                                                                
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#![no_std]
#![no_main]
#![feature(alloc_error_handler)]
#![feature(variant_count)]

extern crate alloc;

pub mod brightness_manager;
pub mod button;
pub mod calendar;
pub mod clock_app;
pub mod clock_display;
pub mod clock_display_viewer;
pub mod clock_state;
pub mod count_down;
pub mod display;
pub mod linear_interpolation;
pub mod number_digits;
pub mod seven_segments;

use alloc::boxed::Box;
use button::{ActiveHigh, Button, ButtonState};
use calendar::Calendar;
use clock_app::{ClockApp, ClockInterrupt};
use clock_display::{ClockDisplay, DisplayPart};
use clock_display_viewer::{ClockDisplayViewer, DisplayView};
use clock_state::ClockState;
use core::{alloc::Layout, cell::RefCell, convert::Infallible, panic::PanicInfo};
use cortex_m::asm::wfi;
use cortex_m_rt::entry;
use count_down::{CountDown, CountDowner};
use critical_section::Mutex;
use display::Display;
use embedded_alloc::Heap;
use embedded_hal::digital::v2::OutputPin;
use fugit::MicrosDurationU32;
use stm32f1xx_hal::{
    afio::MAPR,
    gpio::{Cr, Floating, Input, Pin},
    pac,
    pac::interrupt,
    prelude::*,
    rcc::Clocks,
    rtc::{
        RestoredOrNewRtc::{New, Restored},
        Rtc,
    },
    time::MonoTimer,
    timer::{Event, SysDelay, Tim1NoRemap, Tim2NoRemap, Tim3NoRemap, TimerExt},
};

use defmt_rtt as _;

#[global_allocator]
static HEAP: Heap = Heap::empty();

static APP: Mutex<RefCell<Option<ClockApp>>> = Mutex::new(RefCell::new(Option::None));

#[interrupt]
fn RTC() {
    critical_section::with(|cs| {
        let mut app = APP.borrow_ref_mut(cs);
        let app = app.as_mut().unwrap();

        app.interrupt(ClockInterrupt::Rtc);
    });
}

#[interrupt]
fn TIM4() {
    critical_section::with(|cs| {
        let mut app = APP.borrow_ref_mut(cs);
        let app = app.as_mut().unwrap();

        app.interrupt(ClockInterrupt::DisplayTimer);
    });
}

/// Puts given pins into open drain,
/// then makes PWM out of digit pins,
/// lastly, timer 4 is constructed for refreshing the display,
/// and ClockDisplayViewer is created.
fn init_segment_display(
    pb10: Pin<'B', 10, Input<Floating>>,
    pb2: Pin<'B', 2, Input<Floating>>,
    pb8: Pin<'B', 8, Input<Floating>>,
    pb6: Pin<'B', 6, Input<Floating>>,
    pb9: Pin<'B', 9, Input<Floating>>,
    pb3: Pin<'B', 3, Input<Floating>>,
    pb4: Pin<'B', 4, Input<Floating>>,
    pb7: Pin<'B', 7, Input<Floating>>,
    pa6: Pin<'A', 6, Input<Floating>>,
    pa3: Pin<'A', 3, Input<Floating>>,
    pa7: Pin<'A', 7, Input<Floating>>,
    pa8: Pin<'A', 8, Input<Floating>>,
    pa9: Pin<'A', 9, Input<Floating>>,
    pa2: Pin<'A', 2, Input<Floating>>,
    pa10: Pin<'A', 10, Input<Floating>>,
    pa1: Pin<'A', 1, Input<Floating>>,
    tim1: pac::TIM1,
    tim2: pac::TIM2,
    tim3: pac::TIM3,
    tim4: pac::TIM4,
    gpioa_crl: &mut Cr<'A', false>,
    gpioa_crh: &mut Cr<'A', true>,
    gpiob_crl: &mut Cr<'B', false>,
    gpiob_crh: &mut Cr<'B', true>,
    afio_mapr: &mut MAPR,
    clocks: &Clocks,
) -> ClockDisplayViewer {
    let a = pb10.into_open_drain_output(gpiob_crh);
    let b = pb2.into_open_drain_output(gpiob_crl);
    let c = pb8.into_open_drain_output(gpiob_crh);
    let d = pb6.into_open_drain_output(gpiob_crl);
    let e = pb9.into_open_drain_output(gpiob_crh);
    let f = pb3.into_open_drain_output(gpiob_crl);
    let g = pb4.into_open_drain_output(gpiob_crl);
    let dpp = pb7.into_open_drain_output(gpiob_crl);

    let dig1 = pa6.into_alternate_open_drain(gpioa_crl);
    let dig2 = pa3.into_alternate_open_drain(gpioa_crl);
    let dig3 = pa7.into_alternate_open_drain(gpioa_crl);
    let dig4 = pa8.into_alternate_open_drain(gpioa_crh);
    let dig5 = pa9.into_alternate_open_drain(gpioa_crh);
    let dig6 = pa2.into_alternate_open_drain(gpioa_crl);
    let dig7 = pa10.into_alternate_open_drain(gpioa_crh);
    let dig8 = pa1.into_alternate_open_drain(gpioa_crl);

    let pwm_freq = 2.kHz();
    let pins1 = (dig4, dig5, dig7);
    let pwm1 = tim1.pwm_hz::<Tim1NoRemap, _, _>(pins1, afio_mapr, pwm_freq, &clocks);

    let pins2 = (dig8, dig6, dig2);
    let pwm2 = tim2.pwm_hz::<Tim2NoRemap, _, _>(pins2, afio_mapr, pwm_freq, &clocks);

    let pins3 = (dig1, dig3);
    let pwm3 = tim3.pwm_hz::<Tim3NoRemap, _, _>(pins3, afio_mapr, pwm_freq, &clocks);

    let mut tim4 = tim4.counter_us(&clocks);
    tim4.listen(Event::Update);
    tim4.start(10.micros()).unwrap();

    let countdown: Box<dyn CountDown<Time = MicrosDurationU32> + Send> =
        Box::new(CountDowner::new(tim4));

    let (dig4, dig5, dig7) = pwm1.split();
    let (dig8, dig6, dig2) = pwm2.split();
    let (dig1, dig3) = pwm3.split();

    let display = Display::<8>::new(
        [
            Box::new(a),
            Box::new(b),
            Box::new(c),
            Box::new(d),
            Box::new(e),
            Box::new(f),
            Box::new(g),
            Box::new(dpp),
        ],
        [
            Box::new(dig1),
            Box::new(dig2),
            Box::new(dig3),
            Box::new(dig4),
            Box::new(dig5),
            Box::new(dig6),
            Box::new(dig7),
            Box::new(dig8),
        ],
        countdown,
    );

    let display = ClockDisplay::new(display);
    ClockDisplayViewer::new(display)
}

fn init_buttons(
    pb15: Pin<'B', 15, Input<Floating>>,
    pb14: Pin<'B', 14, Input<Floating>>,
    pb13: Pin<'B', 13, Input<Floating>>,
    pc13: Pin<'C', 13, Input<Floating>>,
    gpiob_crh: &mut Cr<'B', true>,
    gpioc_crh: &mut Cr<'C', true>,
) -> [Button<ActiveHigh>; 4] {
    let btn1 = Button::<ActiveHigh>::new(Box::new(pb15.into_pull_down_input(gpiob_crh)));
    let btn2 = Button::<ActiveHigh>::new(Box::new(pb14.into_pull_down_input(gpiob_crh)));
    let btn3 = Button::<ActiveHigh>::new(Box::new(pb13.into_pull_down_input(gpiob_crh)));
    let btn4 = Button::<ActiveHigh>::new(Box::new(pc13.into_pull_down_input(gpioc_crh)));

    [btn1, btn2, btn3, btn4]
}

fn init_leds<'a>(
    pb12: Pin<'B', 12, Input<Floating>>,
    pb11: Pin<'B', 11, Input<Floating>>,
    pb1: Pin<'B', 1, Input<Floating>>,
    pb0: Pin<'B', 0, Input<Floating>>,
    gpiob_crl: &mut Cr<'B', false>,
    gpiob_crh: &mut Cr<'B', true>,
) -> [Box<dyn OutputPin<Error = Infallible> + Send>; 4] {
    let led1 = pb12.into_open_drain_output(gpiob_crh);
    let led2 = pb11.into_open_drain_output(gpiob_crh);
    let led3 = pb1.into_open_drain_output(gpiob_crl);
    let led4 = pb0.into_open_drain_output(gpiob_crl);

    [
        Box::new(led1),
        Box::new(led2),
        Box::new(led3),
        Box::new(led4),
    ]
}

fn init_heap() {
    use core::mem::MaybeUninit;
    const HEAP_SIZE: usize = 512;
    static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
    unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
}

#[entry]
fn main() -> ! {
    init_heap();

    let cp = cortex_m::Peripherals::take().unwrap();
    let dp = pac::Peripherals::take().unwrap();

    let mut pwr = dp.PWR;
    let mut flash = dp.FLASH.constrain();
    let rcc = dp.RCC.constrain();
    let mut backup_domain = rcc.bkp.constrain(dp.BKP, &mut pwr);

    let clocks = rcc
        .cfgr
        .use_hse(8.MHz())
        .sysclk(24.MHz())
        .pclk1(24.MHz())
        .pclk2(24.MHz())
        .freeze(&mut flash.acr);

    let mut gpiob = dp.GPIOB.split();
    let mut gpioa = dp.GPIOA.split();
    let mut gpioc = dp.GPIOC.split();
    let mut afio = dp.AFIO.constrain();
    let (_, pb3, pb4) = afio.mapr.disable_jtag(gpioa.pa15, gpiob.pb3, gpiob.pb4);

    let leds = init_leds(
        gpiob.pb12,
        gpiob.pb11,
        gpiob.pb1,
        gpiob.pb0,
        &mut gpiob.crl,
        &mut gpiob.crh,
    );
    let btns = init_buttons(
        gpiob.pb15,
        gpiob.pb14,
        gpiob.pb13,
        gpioc.pc13,
        &mut gpiob.crh,
        &mut gpioc.crh,
    );

    let mut display = init_segment_display(
        gpiob.pb10,
        gpiob.pb2,
        gpiob.pb8,
        gpiob.pb6,
        gpiob.pb9,
        pb3,
        pb4,
        gpiob.pb7,
        gpioa.pa6,
        gpioa.pa3,
        gpioa.pa7,
        gpioa.pa8,
        gpioa.pa9,
        gpioa.pa2,
        gpioa.pa10,
        gpioa.pa1,
        dp.TIM1,
        dp.TIM2,
        dp.TIM3,
        dp.TIM4,
        &mut gpioa.crl,
        &mut gpioa.crh,
        &mut gpiob.crl,
        &mut gpiob.crh,
        &mut afio.mapr,
        &clocks,
    );
    display.set_current_view(DisplayView::ClockView);

    let mut rtc = match Rtc::restore_or_new(dp.RTC, &mut backup_domain) {
        Restored(rtc) => rtc,
        New(rtc) => rtc,
    };
    rtc.listen_seconds();

    // Initialize the state inside of a critical section,
    // to make sure that if a second will elapse during state initialization,
    // an interrupt will be called afterwards.
    critical_section::with(|cs| {
        let current_time = rtc.current_time();
        unsafe {
            // RTC interrupt cannot be called prior APP being Some,
            // otherwise a panic would result.
            cortex_m::peripheral::NVIC::unmask(interrupt::RTC);
        }

        let state = ClockState::new(
            Calendar::from_ticks(2023, current_time),
            MonoTimer::new(cp.DWT, cp.DCB, clocks),
        );

        let app = ClockApp::new(rtc, display, state);
        APP.borrow(cs).replace(Some(app));
    });

    unsafe {
        cortex_m::peripheral::NVIC::unmask(interrupt::TIM4);
    }

    let delay = cp.SYST.delay(&clocks);
    main_loop(delay, btns, leds)
}

fn main_loop(
    mut delay: SysDelay,
    mut btns: [Button<ActiveHigh>; 4],
    mut leds: [Box<dyn OutputPin<Error = Infallible> + Send>; 4],
) -> ! {
    loop {
        for (i, btn) in btns.iter_mut().enumerate() {
            btn.update();

            if btn.is_pressed() {
                leds[i].set_low().unwrap();
            } else {
                leds[i].set_high().unwrap();
            }

            let state = btn.state();
            if state != ButtonState::Off {
                critical_section::with(|cs| {
                    let mut app = APP.borrow_ref_mut(cs);
                    let app = app.as_mut().unwrap();

                    app.handle_button(i, state);
                });
            }
        }

        delay.delay_ms(50u16);
    }
}

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
    defmt::error!("{}", defmt::Display2Format(info));
    defmt::flush();

    cortex_m::peripheral::NVIC::mask(interrupt::RTC);
    critical_section::with(|cs| {
        let mut app = APP.borrow_ref_mut(cs);
        let app = app.as_mut().unwrap();
        let display = app.display();
        display.hide_all();
        let _ = display
            .clock_display()
            .show_text(DisplayPart::MainDisplay, "Erro");
        display.clock_display().hide(DisplayPart::SideDisplay1);
        display.clock_display().hide(DisplayPart::SideDisplay2);
    });

    loop {
        wfi();
    }
}

#[alloc_error_handler]
fn oom(_: Layout) -> ! {
    cortex_m::peripheral::NVIC::mask(interrupt::RTC);
    critical_section::with(|cs| {
        let mut app = APP.borrow_ref_mut(cs);
        let app = app.as_mut().unwrap();
        let display = app.display();
        display.hide_all();
        let _ = display
            .clock_display()
            .show_text(DisplayPart::MainDisplay, "oom");
    });

    loop {
        wfi();
    }
}
Do not follow this link