aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2022-08-04 07:26:35 -0400
committerBrian Cully <bjc@kublai.com>2022-08-04 07:26:35 -0400
commit8f9bc49bb40e2972cf0c0bf1b61eb8127a9397df (patch)
tree8a80aed807578319cfcf3d24cd62fd5ceef261e2
parentb55fdd210ad34847ce8638e92fb311721853fbd8 (diff)
downloadluchie-8f9bc49bb40e2972cf0c0bf1b61eb8127a9397df.tar.gz
luchie-8f9bc49bb40e2972cf0c0bf1b61eb8127a9397df.zip
Move LED to separate module.
-rw-r--r--src/led.rs43
-rwxr-xr-xsrc/main.rs45
2 files changed, 54 insertions, 34 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();
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 8d95d00..aee0767 100755
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,9 +1,13 @@
#![no_std]
#![no_main]
+mod led;
+
use core::arch::global_asm;
use gd32vf103_pac::Peripherals;
+use led::LED;
+
global_asm!(include_str!("boot.S"));
#[no_mangle]
@@ -11,44 +15,17 @@ fn main(_hartid: usize) -> ! {
// let peripherals = Peripherals::take().unwrap();
let peripherals = unsafe { Peripherals::steal() };
- let rcu = peripherals.RCU;
- // enable RCU_GPIOA
- rcu.apb2en.write(|w| {
- w.paen().set_bit()
- });
-
- let gpio = peripherals.GPIOA;
- // led is gpioa pin 7 output push/pull 50mhz
- gpio.ctl0.write(|w| unsafe {
- // output mode, push-pull
- w.ctl7().bits(0b00);
- // 50 mhz output rate
- w.md7().bits(0b11);
- w
- });
-
- let mut led_on = false;
+ let led = LED::new(&peripherals);
loop {
- if led_on {
- gpio.bc.write(|w| w.cr7().set_bit());
- if gpio.octl.read().octl7().bit() {
- panic!("not set");
- }
- } else {
- gpio.bop.write(|w| w.bop7().set_bit());
- if !gpio.octl.read().octl7().bit() {
- panic!("set");
- }
- }
- led_on = !led_on;
-
- let mut a = 0;
- for _ in 1..100_000 {
- a = gpio.octl.read().bits();
- }
+ led.toggle();
+ delay();
}
}
+fn delay() {
+ for _ in 1..100_000 {}
+}
+
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
if let Some(loc) = info.location() {