From 5062ac0818f07bfe07714039c46018172a79ee76 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Wed, 3 Aug 2022 12:22:32 -0400 Subject: Blinking achieved. --- Cargo.lock | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + device.lds | 2 +- src/main.rs | 53 +++++++++++++++++++++++++-- 4 files changed, 170 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 60e46fa..f03a28c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,124 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "bare-metal" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3" +dependencies = [ + "rustc_version", +] + +[[package]] +name = "bit_field" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcb6dd1c2376d2e096796e234a70e17e94cc2d5d54ff8ce42b28cef1d0d359a4" + +[[package]] +name = "gd32vf103-pac" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ffd430737a49edbd1c0241b58cabe79a7ac7ebe208f67dc838c1c981eb60e83" +dependencies = [ + "bare-metal", + "riscv", + "vcell", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "luchie" version = "0.1.0" +dependencies = [ + "gd32vf103-pac", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "regex" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" + +[[package]] +name = "riscv" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2f0b705d428e9d0f78e2bb73093887ee58a83c9688de3faedbb4c0631c4618e" +dependencies = [ + "bare-metal", + "bit_field", + "riscv-target", +] + +[[package]] +name = "riscv-target" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88aa938cda42a0cf62a20cfe8d139ff1af20c2e681212b5b34adb5a58333f222" +dependencies = [ + "lazy_static", + "regex", +] + +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver", +] + +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + +[[package]] +name = "vcell" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77439c1b53d2303b20d9459b1ade71a83c716e3f9c34f3228c00e6f185d6c002" diff --git a/Cargo.toml b/Cargo.toml index 9a0078f..16741da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ lto = true codegen-units = 1 [dependencies] +gd32vf103-pac = "0.4.0" [[bin]] name = "luchie" diff --git a/device.lds b/device.lds index ec4729d..d4ca9f1 100644 --- a/device.lds +++ b/device.lds @@ -50,5 +50,5 @@ SECTIONS . = ALIGN(2); PROVIDE(_memory_start = ORIGIN(RAM)); PROVIDE(_memory_end = _memory_start + LENGTH(RAM)); - PROVIDE(_stack_top = . + _memory_end); + PROVIDE(_stack_top = _memory_end); } diff --git a/src/main.rs b/src/main.rs index 0f77639..c0d41d3 100755 --- a/src/main.rs +++ b/src/main.rs @@ -4,10 +4,49 @@ #![no_main] use core::arch::asm; +use gd32vf103_pac::Peripherals; #[no_mangle] fn main(_hartid: usize) -> ! { - loop {} + // 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; + 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(); + } + } } #[naked] @@ -27,12 +66,20 @@ unsafe extern "C" fn _start() -> ! { "csrr a0, mhartid", "la sp, _stack_top", - "j main", + "tail main", options(noreturn) ) } #[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { +fn panic(info: &core::panic::PanicInfo) -> ! { + if let Some(loc) = info.location() { + let _file = loc.file(); + let _line = loc.line(); + if let Some(_msg) = info.payload().downcast_ref::<&str>() { + loop {} + } + loop {} + } loop {} } -- cgit v1.2.3