Skip to content

Commit

Permalink
enhance: init embassy time driver in hal::init (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf authored Oct 31, 2024
1 parent 4e47c04 commit 749577a
Show file tree
Hide file tree
Showing 47 changed files with 30 additions and 52 deletions.
1 change: 0 additions & 1 deletion examples/ch32l103/src/bin/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ async fn main(_spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_96MHZ_HSE;
let p = hal::init(config);
hal::embassy::init();

let mut adc = hal::adc::Adc::new(p.ADC1, Default::default());

Expand Down
1 change: 0 additions & 1 deletion examples/ch32l103/src/bin/adc_temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ async fn main(_spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_96MHZ_HSE;
let p = hal::init(config);
hal::embassy::init();

let mut adc = hal::adc::Adc::new(p.ADC1, Default::default());

Expand Down
1 change: 0 additions & 1 deletion examples/ch32l103/src/bin/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ async fn main(spawner: Spawner) -> ! {
hal::debug::SDIPrint::enable();
let p = hal::init(Default::default());

hal::embassy::init();

// GPIO
spawner.spawn(blink(p.PB12.degrade(), 1000)).unwrap();
Expand Down
1 change: 0 additions & 1 deletion examples/ch32l103/src/bin/rcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ async fn main(_spawner: Spawner) -> ! {
let mut config = Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_96MHZ_HSE;
let p = hal::init(config);
hal::embassy::init();

println!("Clocks {:?}", hal::rcc::clocks());

Expand Down
1 change: 0 additions & 1 deletion examples/ch32v003/src/bin/embassy_blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI;
let p = hal::init(config);
hal::embassy::init();

println!("CHIP signature => {}", hal::signature::chip_id().name());
println!("Clocks {:?}", hal::rcc::clocks());
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v003/src/bin/spi-lcd-st7735-cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ fn main() -> ! {
config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSE;
let p = hal::init(config);

// hal::embassy::init();

// SPI1, remap 0
let cs = p.PC1;
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v203/src/bin/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_96MHZ_HSI;
let p = hal::init(config);
hal::embassy::init();

let mut delay = Delay;

Expand Down
1 change: 0 additions & 1 deletion examples/ch32v203/src/bin/flash_sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_144MHZ_HSI;
let p = hal::init(config);
hal::embassy::init();

// GPIO
spawner.spawn(blink(p.PB8.degrade(), 500)).unwrap();
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v203/src/bin/spi-lcd-st7735.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_144MHZ_HSI;
let p = hal::init(config);
hal::embassy::init();

// SPI1, remap 0
let cs = p.PA4;
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v203/src/bin/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use {ch32_hal as hal, panic_halt as _};
#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(spawner: Spawner) -> ! {
let p = hal::init(Default::default());
hal::embassy::init();

let mut led = Output::new(p.PB8, Level::Low, Default::default());

Expand Down
1 change: 0 additions & 1 deletion examples/ch32v305/src/bin/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ async fn main(spawner: Spawner) -> ! {
hal::debug::SDIPrint::enable();
let mut config = hal::Config::default();
let p = hal::init(config);
hal::embassy::init();

let mut delay = Delay;

Expand Down
1 change: 0 additions & 1 deletion examples/ch32v305/src/bin/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ async fn blink(pin: AnyPin, interval_ms: u64) {
#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(spawner: Spawner) -> ! {
let p = hal::init(Default::default());
hal::embassy::init();

// GPIO
spawner.spawn(blink(p.PC9.degrade(), 400)).unwrap();
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v305/src/bin/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_144MHZ_HSI;
let p = hal::init(config);
hal::embassy::init();

// GPIO
spawner.spawn(blink(p.PA15.degrade(), 500)).unwrap();
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v305/src/bin/exti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ async fn blink(pin: AnyPin) {
async fn main(spawner: Spawner) -> ! {
hal::debug::SDIPrint::enable();
let p = hal::init(Default::default());
hal::embassy::init();

let mut ei = ExtiInput::new(p.PC7, p.EXTI7, Pull::None);

Expand Down
1 change: 0 additions & 1 deletion examples/ch32v305/src/bin/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ async fn main(spawner: Spawner) -> ! {
}
}
let p = hal::init(config);
hal::embassy::init();

Timer::after_millis(100).await;

Expand Down
1 change: 0 additions & 1 deletion examples/ch32v305/src/bin/rcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ async fn main(spawner: Spawner) -> ! {
}
}
let p = hal::init(config);
hal::embassy::init();

println!("SYS: {}Hz", hal::rcc::clocks().sysclk.0);
println!("HCLK: {}Hz", hal::rcc::clocks().hclk.0);
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v305/src/bin/spi-lcd-st7735-cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ async fn main(_spawner: Spawner) -> ! {
}
}
let p = hal::init(config);
hal::embassy::init();

// SPI2
let cs = p.PB12;
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v305/src/bin/uart_echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use {ch32_hal as hal, panic_halt as _};
async fn main(spawner: Spawner) -> ! {
hal::debug::SDIPrint::enable();
let p = hal::init(Default::default());
hal::embassy::init();

// GPIO
let mut led = Output::new(p.PC9, Level::Low, Default::default());
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v305/src/bin/uart_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ async fn blink(pin: AnyPin) {
#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(spawner: Spawner) -> ! {
let p = hal::init(Default::default());
hal::embassy::init();

// GPIO
spawner.spawn(blink(p.PC9.degrade())).unwrap();
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_96MHZ_HSI;
let p = hal::init(config);
hal::embassy::init();

let delay = Delay;

Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/blinky_pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ async fn main(_spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_144MHZ_HSE;
let p = hal::init(Default::default());
hal::embassy::init();

// use remap 1, or 3
let pin = PwmPin::new_ch1::<1>(p.PA15);
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ async fn blink(mut led: Output<'static>) {
async fn main(spawner: Spawner) -> ! {
hal::debug::SDIPrint::enable();
let p = hal::init(Default::default());
hal::embassy::init();

let button = Input::new(p.PB3, Pull::Up);

Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/embassy_blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ async fn blink(pin: AnyPin, interval_ms: u64) {
#[embassy_executor::main(entry = "ch32_hal::entry")]
async fn main(spawner: Spawner) -> ! {
let p = hal::init(Default::default());
hal::embassy::init();

// GPIO
spawner.spawn(blink(p.PA15.degrade(), 1000)).unwrap();
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/exti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ async fn blink(pin: AnyPin) {
async fn main(spawner: Spawner) -> ! {
hal::debug::SDIPrint::enable();
let p = hal::init(Default::default());
hal::embassy::init();

let mut ei = ExtiInput::new(p.PB3, p.EXTI3, Pull::Up); // YD-CH32V307VCT6 USER button

Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/i2c-bmp180-async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ async fn main(_spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_144MHZ_HSE;
let p = hal::init(Default::default());
hal::embassy::init();

let i2c_sda = p.PB11;
let i2c_scl = p.PB10;
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/i2c-bmp180.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ async fn main(_spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_144MHZ_HSE;
let p = hal::init(Default::default());
hal::embassy::init();

let i2c_sda = p.PB11;
let i2c_scl = p.PB10;
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/i2c-eeprom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ async fn main(spawner: Spawner) -> ! {
// let p = hal::init(Default::default());
//let p = hal::init(Default::default());
let p = hal::init(hal::Config::default());
hal::embassy::init();

let i2c_sda = p.PB11;
let i2c_scl = p.PB10;
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/i2c-ssd1306.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ async fn main(_spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_144MHZ_HSE;
let p = hal::init(Default::default());
hal::embassy::init();

let i2c_sda = p.PB11;
let i2c_scl = p.PB10;
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/rcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ async fn main(spawner: Spawner) -> ! {
config.rcc = hal::rcc::Config::SYSCLK_FREQ_144MHZ_HSE;

let p = hal::init(config);
hal::embassy::init();

println!("SYS: {}Hz", hal::rcc::clocks().sysclk.0);
println!("HCLK: {}Hz", hal::rcc::clocks().hclk.0);
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ async fn main(_spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_96MHZ_HSI;
let p = hal::init(config);
hal::embassy::init();

println!("Hello World!");

Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/sdmmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ async fn main(_spawner: Spawner) {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_96MHZ_HSE;
let p = hal::init(config);
hal::embassy::init();

println!("clk => {}", hal::rcc::clocks().hclk.0);

Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ async fn blink(pin: AnyPin) {
async fn main(spawner: Spawner) -> ! {
hal::debug::SDIPrint::enable();
let p = hal::init(Default::default());
hal::embassy::init();

// GPIO
spawner.spawn(blink(p.PA0.degrade())).unwrap();
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ async fn blink(pin: AnyPin) {
#[embassy_executor::main(entry = "ch32_hal::entry")]
async fn main(spawner: Spawner) -> ! {
let p = hal::init(Default::default());
hal::embassy::init();

// GPIO
spawner.spawn(blink(p.PA0.degrade())).unwrap();
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/uart_echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use {ch32_hal as hal, panic_halt as _};
async fn main(spawner: Spawner) -> ! {
hal::debug::SDIPrint::enable();
let p = hal::init(Default::default());
hal::embassy::init();

// GPIO
let mut led = Output::new(p.PA4, Level::Low, Default::default());
Expand Down
1 change: 0 additions & 1 deletion examples/ch32v307/src/bin/usbfs_dfu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ async fn main(spawner: Spawner) -> ! {
..Default::default()
};
let p = hal::init(cfg);
hal::embassy::init();

/* USB DRIVER SECION */
let mut buffer: [EndpointDataBuffer; 1] = core::array::from_fn(|_| EndpointDataBuffer::default());
Expand Down
1 change: 0 additions & 1 deletion examples/ch32x035/src/bin/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ async fn main(spawner: Spawner) -> ! {
hal::debug::SDIPrint::enable();
let p = hal::init(Default::default());

hal::embassy::init();

// GPIO
spawner.spawn(blink(p.PB12.degrade(), 1000)).unwrap();
Expand Down
1 change: 0 additions & 1 deletion examples/ch32x035/src/bin/blinky_pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ async fn main(spawner: Spawner) -> ! {
hal::debug::SDIPrint::enable();
let p = hal::init(Default::default());

hal::embassy::init();

let pin = PwmPin::new_ch4(p.PB12);
let mut pwm = SimplePwm::new(
Expand Down
1 change: 0 additions & 1 deletion examples/ch32x035/src/bin/exti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use hal::println;
async fn main(_spawner: Spawner) -> ! {
hal::debug::SDIPrint::enable();
let p = hal::init(Default::default());
hal::embassy::init();

let button = p.PC3;
let mut button = ExtiInput::new(button, p.EXTI3, hal::gpio::Pull::None);
Expand Down
1 change: 0 additions & 1 deletion examples/ch32x035/src/bin/spi-lcd-st7735.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI;
let p = hal::init(config);
hal::embassy::init();

// SPI1, remap 0
let cs = p.PA4;
Expand Down
1 change: 0 additions & 1 deletion examples/ch32x035/src/bin/uart_echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI;
let p = hal::init(Default::default());
hal::embassy::init();

// Connector pinout:
// GND, VCC, PC17, PC16
Expand Down
1 change: 0 additions & 1 deletion examples/ch32x035/src/bin/uart_pms7003_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI;
let p = hal::init(config);
hal::embassy::init();

// Connector pinout:
// GND, VCC, PC17, PC16
Expand Down
1 change: 0 additions & 1 deletion examples/ch32x035/src/bin/uart_rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI;
let p = hal::init(Default::default());
hal::embassy::init();

// Connector pinout:
// GND, VCC, PC17, PC16
Expand Down
1 change: 0 additions & 1 deletion examples/ch32x035/src/bin/uart_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI;
let p = hal::init(Default::default());
hal::embassy::init();

// Connector pinout:
// GND, VCC, PC17, PC16
Expand Down
1 change: 0 additions & 1 deletion examples/ch32x035/src/bin/uart_tx_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ async fn main(spawner: Spawner) -> ! {
config.dma_interrupt_priority = interrupt::Priority::P0;
config = Default::default();
let p = hal::init(config);
hal::embassy::init();

// Connector pinout:
// GND, VCC, PC17, PC16
Expand Down
1 change: 0 additions & 1 deletion examples/ch641/src/bin/embassy_blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ async fn main(spawner: Spawner) -> ! {
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI;
let p = hal::init(config);
hal::embassy::init();

println!("CHIP signature => {}", hal::signature::chip_id().name());
println!("Clocks {:?}", hal::rcc::clocks());
Expand Down
20 changes: 17 additions & 3 deletions src/embassy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,23 @@ pub mod time_driver_impl;
#[path = "time_driver_tim.rs"]
pub mod time_driver_impl;

// This should be called after global clocks inited
pub fn init() {
crate::pac::PFIC.sctlr().modify(|w| w.set_sevonpend(true));
/// Initialize the Embassy time driver.
///
/// System global clocks must be initialized before calling this function.
///
/// # Safety
///
/// This function should be called only once.
///
/// # Implementation Notes
///
/// The WCH QingKe RISC-V core deviates from standard RISC-V specification:
/// - `WFI` instruction will not wake up from disabled interrupts
/// - Either `WFITOWFE` or `SEVONPEND` must be enabled for proper wake-up behavior
///
/// `WFITOWFE` is configured in `qingke-rt`, so no additional setup needed here
pub unsafe fn init() {
// crate::pac::PFIC.sctlr().modify(|w| w.set_sevonpend(true));

#[cfg(all(qingke_v4, not(time_driver_timer)))]
time_driver_impl::init();
Expand Down
Loading

0 comments on commit 749577a

Please sign in to comment.