Skip to content

Commit

Permalink
demo: add pwm demo for ch32v307
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed May 4, 2024
1 parent 477cc37 commit 658a6bd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions examples/ch32v307/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[build]
target = "riscv32imac-unknown-none-elf"
target = "riscv32imafc-unknown-none-elf"

[target.riscv32imac-unknown-none-elf]
[target.riscv32imafc-unknown-none-elf]
runner = "wlink flash --enable-sdi-print --watch-serial"
# runner = "wlink -v flash --no-erase --dry-run"
# runner = "probe-rs run --chip CH32V307"
Expand Down
18 changes: 18 additions & 0 deletions examples/ch32v307/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,21 @@
## Boards

### YD-CH32V307VCT6

8MHz XTAL, 32.768KHz XTAL

- PA15, Red LED, active high
- PB4, Blue LED, active high
- PB3, User button, active low

FT24C32, with pull up

- SCL: PB10
- SDA: PB11

W26Q16

- CS: PA2
- DO(IO1): PA6
- CLK: PA5
- DI(IO0): PA7
49 changes: 49 additions & 0 deletions examples/ch32v307/src/bin/blinky_pwm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

use embassy_executor::Spawner;
use embassy_time::Timer;
use hal::println;
use hal::time::Hertz;
use hal::timer::low_level::CountingMode;
use hal::timer::simple_pwm::{PwmPin, SimplePwm};
use {ch32_hal as hal, panic_halt as _};

#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(_spawner: Spawner) -> ! {
hal::debug::SDIPrint::enable();
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);
let mut pwm = SimplePwm::new(
p.TIM2,
Some(pin),
None,
None,
None,
Hertz::khz(1),
CountingMode::default(),
);
let ch = hal::timer::Channel::Ch1;

let max_duty = pwm.get_max_duty();
println!("max duty: {}", max_duty);
pwm.set_duty(ch, 7000);
pwm.enable(ch);

loop {
for i in 0..100 {
pwm.set_duty(ch, i * 80);
Timer::after_millis(5).await;
}
for i in (0..100).rev() {
pwm.set_duty(ch, i * 80);
Timer::after_millis(5).await;
}
}
}

0 comments on commit 658a6bd

Please sign in to comment.