diff options
Diffstat (limited to 'src/led.rs')
-rw-r--r-- | src/led.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/led.rs b/src/led.rs new file mode 100644 index 0000000..d8d9912 --- /dev/null +++ b/src/led.rs @@ -0,0 +1,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(); + } + } +} |