Skip to content

Commit

Permalink
fix: compile error
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Oct 27, 2023
1 parent 4b7b0d9 commit ffc95c3
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 250 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ embassy-executor = { version = "0.3.0", features = [
"executor-thread",
] }
heapless = "0.7.16"
embedded-alloc = "0.5.0"

[features]
default = []
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ Refer `Cargo.toml` and `examples` directory.
## References

- [Slappy2022/ch58x-hal](https://github.com/Slappy2022/ch58x-hal)
- [Slappy2022/ch58x-ble-rt](https://github.com/Slappy2022/ch58x-ble-rt)
2 changes: 2 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ fn main() {

fs::write(out_dir.join("libISP583.a"), include_bytes!("vendor/libISP583.a")).unwrap();

fs::write(out_dir.join("libCH58xBLE.a"), include_bytes!("vendor/libCH58xBLE.a")).unwrap();

// Put the linker script somewhere the linker can find it.
fs::write(out_dir.join("link.x"), include_bytes!("link.x")).unwrap();

Expand Down
131 changes: 0 additions & 131 deletions examples/blinky copy.rs

This file was deleted.

19 changes: 2 additions & 17 deletions examples/gpio_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,8 @@ static mut SERIAL: Option<UartTx<'static, hal::peripherals::UART1>> = None;

static mut BUTTON: Option<Input<hal::peripherals::PB22>> = None;

global_asm!(
r#"
.section .trap, "ax"
.global GPIOB
GPIOB:
addi sp, sp, -4
sw ra, 0(sp)
jal _rust_GPIOB
lw ra, 0(sp)
addi sp, sp, 4
mret
"#
);

#[allow(non_snake_case)]
#[export_name = "_rust_GPIOB"]
unsafe fn GPIOB_IRQHandler() {
#[ch32v_rt::interrupt]
unsafe fn GPIOB() {
if let Some(button) = BUTTON.as_mut() {
button.disable_interrupt();

Expand Down
116 changes: 34 additions & 82 deletions examples/hardfault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,45 @@ use embedded_hal_1::delay::DelayUs;
use hal::gpio::{AnyPin, Input, Level, Output, OutputDrive, Pull};
use hal::isp::EEPROM_BLOCK_SIZE;
use hal::rtc::{DateTime, Rtc};
use hal::serial::Uart;
use hal::sysctl::Config;
use hal::systick::SysTick;
use hal::uart::UartTx;
use hal::{pac, peripherals, Peripherals};
use {ch58x_hal as hal, panic_halt as _};

global_asm!(
r#"
.section .trap, "ax"
.global RTC
RTC:
addi sp, sp, -4
sw ra, 0(sp)
jal _rust_RTC
lw ra, 0(sp)
addi sp, sp, 4
mret
"#
);

#[allow(non_snake_case)]
#[export_name = "_rust_RTC"]
extern "C" fn RTC_IRQHandler() {
let mut rtc = Rtc;
let mut uart = Uart {};
static mut SERIAL: Option<UartTx<peripherals::UART1>> = None;
macro_rules! println {
($($arg:tt)*) => {
unsafe {
use core::fmt::Write;
use core::writeln;

writeln!(uart, "Entering IRQ...");
if let Some(uart) = SERIAL.as_mut() {
writeln!(uart, $($arg)*).unwrap();
}
}
}
}

#[ch32v_rt::interrupt]
fn RTC() {
let mut rtc = Rtc;

rtc.ack_timing();

let now = rtc.now();
writeln!(uart, "Current time: {} weekday={}", now, now.isoweekday()).unwrap();
println!("Current time: {} weekday={}", now, now.isoweekday());
// writeln!(uart, "mepc: {:08x}", riscv::register::mepc::read()).unwrap();
}

#[allow(non_snake_case)]
#[export_name = "HardFault"]
extern "C" fn HardFault_IRQHandler() {
#[ch32v_rt::interrupt]
fn HardFault() {
let pa8 = unsafe { hal::peripherals::PA8::steal() };
let mut led = Output::new(pa8, Level::Low, OutputDrive::HighDrive);

println!("in hardfault");
let mcause = riscv::register::mcause::read();
println!("mcause: {:?}", mcause);

let short = hal::sysctl::clocks().hclk.to_Hz() / 256;
let long = hal::sysctl::clocks().hclk.to_Hz() / 32;

Expand All @@ -70,49 +68,23 @@ extern "C" fn HardFault_IRQHandler() {
}
}

#[link_section = ".sbss"]
static mut BUF: [u8; 1024] = [0; 1024];

// #[riscv_rt::entry]
#[allow(non_snake_case)]
#[export_name = "main"]
extern "C" fn main() -> ! {
// LED PA8
// hal::sysctl::Config::pll_60mhz().freeze();
hal::sysctl::Config::pll_60mhz().enable_lse().freeze();

let p = Peripherals::take();
#[ch32v_rt::entry]
fn main() -> ! {
let mut config = hal::Config::default();
config.clock.use_pll_60mhz();
let p = hal::init(config);

let mut delay = SysTick::new(p.SYSTICK);

unsafe {
// prepare PA9, for uart tx
p.GPIO.pa_dir.modify(|_, w| w.pa_dir().bits((1 << 9)));
}

let mut pa8 = Output::new(p.PA8, Level::Low, OutputDrive::Standard);

let mut download_button = Input::new(p.PB22, Pull::Up);
let mut reset_button = Input::new(p.PB23, Pull::Up);

let mut uart = Uart::new(Default::default());

writeln!(uart, "\n\n\nHello World!").unwrap();
writeln!(uart, "Clocks: {}", hal::sysctl::clocks().hclk).unwrap();
writeln!(uart, "ChipID: {:02x}", hal::signature::get_chip_id());

//let boot_info = hal::isp::get_boot_info();
//writeln!(uart, "boot_info: {:02x?}", boot_info).unwrap();
let uart = UartTx::new(p.UART1, p.PA9, Default::default()).unwrap();
unsafe { SERIAL.replace(uart) };

//uart.flush();
//let ret = unsafe { hal::isp::eeprom_read(0x0, &mut BUF[..500]) };
// writeln!(uart, "ret {}", ret);
// unsafe { writeln!(uart, "read flash: {:02x?}", &BUF[..500]).unwrap() };

/// erase 1 block(4k)
uart.flush();
// let ret = hal::isp::eeprom_erase(0x0, 256);
// writeln!(uart, "erase ret {}", ret);
println!("\nMCU init ok!");

let mut rtc = Rtc;

Expand All @@ -121,38 +93,18 @@ extern "C" fn main() -> ! {
rtc.enable_timing(hal::rtc::TimingMode::_2S);
unsafe { pfic.ienr1.write(|w| w.bits(1 << 28)) }; // enable rtc irq

/*
rtc.set_datatime(DateTime {
year: 2023,
month: 10,
day: 12,
hour: 18,
minute: 45,
second: 0,
});
*/

// let buf = hal::isp::read_eeprom(0x0, 500);
// writeln!(uart, "read flash: {:02x?}", buf).unwrap();

// ISP functions

loop {
unsafe {
pa8.toggle();

// writeln!(uart, "day {:?}", rtc.counter_day()).unwrap();
// writeln!(uart, "2s {:?}", rtc.counter_2s()).unwrap();

// writeln!(uart, "tick! {}", SysTick::now()).unwrap();
delay.delay_ms(100);

while download_button.is_low() {}

if reset_button.is_low() {
writeln!(uart, "button: {} {}", download_button.is_low(), reset_button.is_low()).unwrap();
println!("button: {} {}", download_button.is_low(), reset_button.is_low());
}
unsafe { asm!("j -222224") };
unsafe { asm!("j -222224") }; // trigger hardfault
}
}
}
3 changes: 0 additions & 3 deletions examples/spi-ssd1306.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use embedded_hal_1::delay::DelayUs;
use hal::adc::Adc;
use hal::gpio::{Input, Level, Output, OutputDrive, Pull};
use hal::prelude::*;
// use hal::interrupt::Interrupt;
use hal::rtc::Rtc;
use hal::spi::{BitOrder, Spi};
use hal::systick::SysTick;
Expand Down Expand Up @@ -59,8 +58,6 @@ impl DisplaySize for DisplaySize128x32Variant1 {

#[ch32v_rt::entry]
fn main() -> ! {
// LED PA8

let mut config = hal::Config::default();
config.clock.use_pll_60mhz();
let p = hal::init(config);
Expand Down
19 changes: 2 additions & 17 deletions examples/systick_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,8 @@ fn main() -> ! {
}
}

core::arch::global_asm!(
r#"
.section .trap, "ax"
.global SysTick
SysTick:
addi sp, sp, -4
sw ra, 0(sp)
jal _rust_SysTick
lw ra, 0(sp)
addi sp, sp, 4
mret
"#
);

#[allow(non_snake_case)]
#[export_name = "_rust_SysTick"]
extern "C" fn SysTick_IRQHandler() {
#[ch32v_rt::interrupt]
fn SysTick() {
// FIXME: the usage of SWIE is unknown
let systick = unsafe { &*pac::SYSTICK::PTR };
// systick.ctlr.modify(|_, w| w.stie().clear_bit()); // disable interrupt
Expand Down

0 comments on commit ffc95c3

Please sign in to comment.