diff options
author | Brian Cully <bjc@kublai.com> | 2022-08-27 19:35:56 -0400 |
---|---|---|
committer | Brian Cully <bjc@kublai.com> | 2022-08-27 19:35:56 -0400 |
commit | 0f80c612d59c855a99073e583db339fe6a42b883 (patch) | |
tree | 7594496b532f6aaa75b7793efd561503f5f948a1 /src/main.rs | |
parent | 7846e30a25dfbbb2794035894d4b9e5c0f1ef41d (diff) | |
download | luchie-0f80c612d59c855a99073e583db339fe6a42b883.tar.gz luchie-0f80c612d59c855a99073e583db339fe6a42b883.zip |
this is also broken, but at least it ain't mine
Diffstat (limited to 'src/main.rs')
-rwxr-xr-x | src/main.rs | 126 |
1 files changed, 94 insertions, 32 deletions
diff --git a/src/main.rs b/src/main.rs index 19f2b7a..dce005a 100755 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +/* #![no_std] #![no_main] @@ -9,7 +10,6 @@ mod usb; use gd32vf103xx_hal::{ afio::AfioExt, eclic::{self, EclicExt}, - exti::Exti, gpio::GpioExt, pac::{self, Peripherals}, rcu::RcuExt, @@ -22,10 +22,12 @@ use riscv::{ interrupt, }; use riscv_rt::entry; +use usb_device::prelude::*; use led::LED; +use usb::{USB, UsbBus}; -// global_asm!(include_str!("boot.S")); +static mut EP_MEMORY: [u32; 1024] = [0; 1024]; #[entry] fn main(_hartid: usize) -> ! { @@ -35,7 +37,11 @@ fn main(_hartid: usize) -> ! { pac::ECLIC::set_threshold_level(eclic::Level::L0); pac::ECLIC::set_level_priority_bits(eclic::LevelPriorityBits::L3P1); - let mut rcu = p.RCU.configure().sysclk(MegaHertz(48)).freeze(); + let mut rcu = p.RCU + .configure() + .ext_hf_clock(MegaHertz(8)) + .sysclk(MegaHertz(96)) + .freeze(); let mut afio = p.AFIO.constrain(&mut rcu); let gpioa = p.GPIOA.split(&mut rcu); @@ -56,18 +62,24 @@ fn main(_hartid: usize) -> ! { let led = LED::new(gpioa.pa7); let mut blink = blink::Task::new(timer, Hertz(5), led); - logln!("+++ busabus"); - let mut exti = Exti::new(p.EXTI); - let bus = usb::Bus::new(p.USBFS_GLOBAL, p.USBFS_DEVICE, p.USBFS_PWRCLK, &mut rcu, &mut exti); - bus.init(); - // let bus_alloc = UsbBusAllocator::new(bus); - // logln!("+++ devadev"); - // let mut usb_dev = UsbDeviceBuilder::new(&bus_alloc, UsbVidPid(0xdead, 0xbeef)) - // .manufacturer("luchie") - // .product("pad") - // .serial_number("test") - // .build(); - // logln!("+++ superserious"); + let usb = USB { + usb_global: p.USBFS_GLOBAL, + usb_device: p.USBFS_DEVICE, + usb_pwrclk: p.USBFS_PWRCLK, + pin_dm: gpioa.pa11, + pin_dp: gpioa.pa12, + hclk: rcu.clocks.hclk() + }; + + let usb_bus = UsbBus::new(usb, unsafe { &mut EP_MEMORY }); + + let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) + .manufacturer("Fake company") + .product("Enumeration test") + .serial_number("TEST") + .device_class(0) + .build(); + //let mut serial = SerialPort::new(&bus_alloc); unsafe { interrupt::enable() }; @@ -75,25 +87,20 @@ fn main(_hartid: usize) -> ! { loop { let mut can_sleep = true; - log!("yo"); if let Ok(_) = blink.poll() { can_sleep = false; } - log!(" mtv"); - // was: usb_dev.poll(&mut [&mut serial]) - // if usb_dev.poll(&mut []) { - // can_sleep = false; - // } - log!(" raps"); - - if can_sleep { + if usb_dev.poll(&mut []) { + logln!("usb"); + can_sleep = false; + } + + if can_sleep && false { log!("!"); unsafe { wfi() }; } - logln!(""); } } - #[export_name="ExceptionHandler"] fn exception_handler(_frame: &riscv_rt::TrapFrame) -> ! { spin(); @@ -108,19 +115,74 @@ fn default_handler() -> ! { fn machine_timer() { spin(); } +*/ + +#![no_std] +#![no_main] + +mod usb; + +use riscv::asm::wfi; +use riscv_rt::entry; +use gd32vf103xx_hal::prelude::*; +use gd32vf103xx_hal::pac; + +use usb::{USB, UsbBus}; +use usb_device::prelude::*; +use usbd_serial::SerialPort; + +static mut EP_MEMORY: [u32; 1024] = [0; 1024]; + +#[entry] +fn main() -> ! { + let dp = pac::Peripherals::take().unwrap(); + + // Configure clocks + let mut rcu = dp.RCU.configure() + .ext_hf_clock(8.mhz()) + .sysclk(96.mhz()) + .freeze(); + + assert!(rcu.clocks.usbclk_valid()); + + let gpioa = dp.GPIOA.split(&mut rcu); + let usb = USB { + usb_global: dp.USBFS_GLOBAL, + usb_device: dp.USBFS_DEVICE, + usb_pwrclk: dp.USBFS_PWRCLK, + pin_dm: gpioa.pa11, + pin_dp: gpioa.pa12, + hclk: rcu.clocks.hclk() + }; + + let usb_bus = UsbBus::new(usb, unsafe { &mut EP_MEMORY }); + + let mut serial = SerialPort::new(&usb_bus); + let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) + .manufacturer("Fake company") + .product("Enumeration test") + .serial_number("TEST") + .device_class(0) + .build(); + + loop { + if usb_dev.poll(&mut [&mut serial]) { + } + } +} #[panic_handler] fn panic(info: &core::panic::PanicInfo) -> ! { - interrupt::free(|_cs| { - log!("!!! panic "); +// interrupt::free(|_cs| { +// log!("!!! panic "); if let Some(loc) = info.location() { - log!("in {}:{} ", loc.file(), loc.line()); +// log!("in {}:{} ", loc.file(), loc.line()); } if let Some(msg) = info.payload().downcast_ref::<&str>() { - log!("⇒ {} ", msg); +// log!("⇒ {} ", msg); } - logln!("!!!"); - }); +// logln!("!!!"); +// }); spin(); } |