aboutsummaryrefslogtreecommitdiffstats
path: root/src/led.rs
blob: 951a32e271790601f49f3254f0a1a07620430862 (plain)
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
use gd32vf103_pac::Peripherals;

#[derive(Clone, Copy)]
pub struct LED();

impl LED {
    pub fn new() -> Self {
        let peripherals = unsafe { Peripherals::steal() };
        peripherals.RCU.apb2en.modify(|_, w| {
            w.paen().set_bit()
        });

        peripherals.GPIOA.ctl0.modify(|_, w| unsafe {
            // output mode, push-pull
            w.ctl7().bits(0b00);
            // 50 mhz output rate
            w.md7().bits(0b11);
            w
        });
        Self()
    }

    pub fn is_on(&self) -> bool {
        let gpio = unsafe { Peripherals::steal() }.GPIOA;
        gpio.octl.read().octl7().bit()
    }

    pub fn on(&self) {
        let gpio = unsafe { Peripherals::steal() }.GPIOA;
        gpio.bop.write(|w| w.bop7().set_bit());
    }

    pub fn off(&self) {
        let gpio = unsafe { Peripherals::steal() }.GPIOA;
        gpio.bc.write(|w| w.cr7().set_bit());
    }

    pub fn toggle(&self) {
        if self.is_on() {
            self.off();
        } else {
            self.on();
        }
    }
}