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

pub struct LED();

impl LED {
    pub fn new(peripherals: &Peripherals) -> Self {
        peripherals.RCU.apb2en.write(|w| {
            w.paen().set_bit()
        });

        peripherals.GPIOA.ctl0.write(|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();
        }
    }
}