From ffc95c38a630c3b804a9d5b8995d97e527d755df Mon Sep 17 00:00:00 2001 From: Andelf Date: Fri, 27 Oct 2023 20:39:34 +0800 Subject: [PATCH] fix: compile error --- Cargo.toml | 1 + README.md | 1 + build.rs | 2 + examples/blinky copy.rs | 131 ---------------------------------------- examples/gpio_int.rs | 19 +----- examples/hardfault.rs | 116 +++++++++++------------------------ examples/spi-ssd1306.rs | 3 - examples/systick_int.rs | 19 +----- 8 files changed, 42 insertions(+), 250 deletions(-) delete mode 100644 examples/blinky copy.rs diff --git a/Cargo.toml b/Cargo.toml index f51d3cd..e919199 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ embassy-executor = { version = "0.3.0", features = [ "executor-thread", ] } heapless = "0.7.16" +embedded-alloc = "0.5.0" [features] default = [] diff --git a/README.md b/README.md index 1744ae5..51145c5 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/build.rs b/build.rs index 5e22116..0b13e39 100644 --- a/build.rs +++ b/build.rs @@ -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(); diff --git a/examples/blinky copy.rs b/examples/blinky copy.rs deleted file mode 100644 index ac52cd8..0000000 --- a/examples/blinky copy.rs +++ /dev/null @@ -1,131 +0,0 @@ -#![no_std] -#![no_main] - -use core::arch::{asm, global_asm}; -use core::fmt::Write; -use core::writeln; - -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::sysctl::Config; -use hal::systick::SysTick; -use hal::uart::Uart; -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 {}; - - writeln!(uart, "Entering IRQ..."); - - rtc.ack_timing(); - - let now = rtc.now(); - writeln!(uart, "Current time: {} weekday={}", now, now.isoweekday()).unwrap(); - // writeln!(uart, "mepc: {:08x}", riscv::register::mepc::read()).unwrap(); -} - -#[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(); - - 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(); - - //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); - - let mut rtc = Rtc; - - let pfic = unsafe { &*pac::PFIC::PTR }; - - 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(1000); - - while download_button.is_low() {} - - if reset_button.is_low() { - writeln!(uart, "button: {} {}", download_button.is_low(), reset_button.is_low()).unwrap(); - } - } - } -} diff --git a/examples/gpio_int.rs b/examples/gpio_int.rs index 9d3d70d..8cf2c8e 100644 --- a/examples/gpio_int.rs +++ b/examples/gpio_int.rs @@ -21,23 +21,8 @@ static mut SERIAL: Option> = None; static mut BUTTON: Option> = 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(); diff --git a/examples/hardfault.rs b/examples/hardfault.rs index 73446fb..53637c6 100644 --- a/examples/hardfault.rs +++ b/examples/hardfault.rs @@ -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> = 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; @@ -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; @@ -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 } } } diff --git a/examples/spi-ssd1306.rs b/examples/spi-ssd1306.rs index ab10050..be86da3 100644 --- a/examples/spi-ssd1306.rs +++ b/examples/spi-ssd1306.rs @@ -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; @@ -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); diff --git a/examples/systick_int.rs b/examples/systick_int.rs index 8b15e5d..fa4636c 100644 --- a/examples/systick_int.rs +++ b/examples/systick_int.rs @@ -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