aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: c0d41d3d87e06a0214401d070238599879ee7445 (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
#![feature(naked_functions)]

#![no_std]
#![no_main]

use core::arch::asm;
use gd32vf103_pac::Peripherals;

#[no_mangle]
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;
    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]
#[link_section = ".text.init"]
#[export_name = "_start"]
unsafe extern "C" fn _start() -> ! {
    // At boot, the hart is in privilege mode M, mstatus fields MIE
    // and MPRV are 0, the pc is set to a reset vector, the mcause
    // register specifies why the reset occurred.
    // c.f.: [^super::rism2] § 3.4 (Reset)
    asm!(
        ".option push",
        ".option norelax",
        "la gp, __global_pointer$",
        ".option pop",

        "csrr a0, mhartid",
        "la sp, _stack_top",

        "tail main",
        options(noreturn)
    )
}

#[panic_handler]
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 {}
}