aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 19f2b7a5ae51a751d90d254d32b13dc5fd889735 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#![no_std]
#![no_main]

mod blink;
mod led;
mod log;
mod usb;

use gd32vf103xx_hal::{
    afio::AfioExt,
    eclic::{self, EclicExt},
    exti::Exti,
    gpio::GpioExt,
    pac::{self, Peripherals},
    rcu::RcuExt,
    serial::Serial,
    time::{Hertz, MegaHertz},
    timer::Timer,
};
use riscv::{
    asm::wfi,
    interrupt,
};
use riscv_rt::entry;

use led::LED;

// global_asm!(include_str!("boot.S"));

#[entry]
fn main(_hartid: usize) -> ! {
    let p = Peripherals::take().expect("couldn't take peripherals");

    pac::ECLIC::reset();
    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 afio = p.AFIO.constrain(&mut rcu);
    let gpioa = p.GPIOA.split(&mut rcu);

    // TODO: figure out how to use the usb serial on the start board.
    //
    // this version has to be wired up physically.
    let serial = Serial::new(
        p.USART1,
        (gpioa.pa2, gpioa.pa3),
        Default::default(),
        &mut afio,
        &mut rcu,
    );
    let (tx, _rx) = serial.split();
    log::init(tx);

    let timer = Timer::<pac::TIMER6>::timer6(p.TIMER6, Hertz(5), &mut rcu);
    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 mut serial = SerialPort::new(&bus_alloc);

    unsafe { interrupt::enable() };

    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 {
            log!("!");
            unsafe { wfi() };
        }
        logln!("");
    }
}

#[export_name="ExceptionHandler"]
fn exception_handler(_frame: &riscv_rt::TrapFrame) -> ! {
    spin();
}

#[export_name="DefaultHandler"]
fn default_handler() -> ! {
    spin();
}

#[export_name="MachineTimer"]
fn machine_timer() {
    spin();
}

#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
    interrupt::free(|_cs| {
        log!("!!! panic ");
        if let Some(loc) = info.location() {
            log!("in {}:{} ", loc.file(), loc.line());
        }
        if let Some(msg) = info.payload().downcast_ref::<&str>() {
            log!("⇒ {} ", msg);
        }
        logln!("!!!");
    });
    spin();
}

fn spin() -> ! {
    loop { unsafe { wfi() } };
}