Skip to content

Instantly share code, notes, and snippets.

@andelf
Created May 23, 2024 13:14
Show Gist options
  • Save andelf/c132020143a37b5da9cb8f6432010006 to your computer and use it in GitHub Desktop.
Save andelf/c132020143a37b5da9cb8f6432010006 to your computer and use it in GitHub Desktop.
Blinky an LED like Heartbeat
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use ch32_hal as hal;
use hal::delay::Delay;
use hal::println;
use hal::time::Hertz;
use hal::timer::complementary_pwm::{ComplementaryPwm, ComplementaryPwmPin};
use hal::timer::low_level::CountingMode;
use hal::timer::simple_pwm::PwmPin;
#[qingke_rt::entry]
fn main() -> ! {
hal::debug::SDIPrint::enable();
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI;
let p = hal::init(config);
// println!("mtvec: {:#x}", qingke::register::mtvec::read().bits());
let pin1 = ComplementaryPwmPin::new_ch2::<2>(p.PA2);
let pin2 = PwmPin::new_ch4::<2>(p.PC4);
let mut pwm = ComplementaryPwm::new(
p.TIM1,
None,
None,
None,
Some(pin1),
None,
None,
Some(pin2),
Hertz::khz(1),
CountingMode::default(),
);
let ch1 = hal::timer::Channel::Ch2;
let ch2 = hal::timer::Channel::Ch4;
let _max_duty = pwm.get_max_duty();
println!("max duty: {}", _max_duty);
pwm.set_duty(ch1, 2000);
pwm.enable(ch1);
pwm.set_duty(ch2, 2000);
pwm.enable(ch2);
// The values are converted from a normal EEG graph. Not accurate. :)
let vals: &[u16] = &[
101, 101, 104, 104, 106, 103, 107, 106, 105, 104, 109, 112, 119, 122, 121, 123, 125, 125, 129, 128, 127, 122,
121, 123, 115, 120, 113, 119, 111, 112, 109, 107, 104, 107, 102, 107, 105, 98, 102, 102, 99, 98, 97, 100, 101,
96, 93, 92, 91, 81, 77, 76, 72, 64, 42, 38, 37, 36, 32, 17, 11, 10, 9, 16, 10, 9, 1, 4, 0, 8, 80, 79, 78, 77,
72, 71, 112, 111, 110, 109, 108, 107, 106, 105, 120, 103, 100, 99, 98, 97, 162, 161, 124, 123, 160, 159, 156,
155, 154, 224, 223, 222, 221, 214, 213, 212, 204, 203, 255, 254, 253, 252, 251, 250, 249, 247, 246, 245, 220,
219, 218, 217, 282, 281, 264, 263, 256, 243, 242, 241, 240, 239, 238, 237, 236, 235, 234, 233, 295, 294, 280,
279, 278, 277, 276, 275, 274, 268, 267, 264, 263, 348, 347, 374, 373, 372, 371, 367, 366, 365, 368, 367, 346,
343, 470, 454, 453, 452, 451, 450, 449, 474, 456, 455, 454, 453, 452, 451, 450, 449, 448, 447, 446, 445, 444,
443, 432, 431, 430, 428, 427, 426, 425, 424, 423, 422, 421, 326, 325, 324, 298, 297, 295, 294, 272, 271, 270,
269, 295, 294, 267, 200, 199, 256, 255, 254, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 237, 236, 177,
176, 175, 167, 166, 165, 164, 163, 165, 164, 163, 162, 161, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114,
113, 110, 109, 108, 107, 106, 105, 88, 87, 86, 85, 84, 53, 65, 64, 61, 64, 63, 75, 79, 74, 72, 77, 86, 85, 81,
80, 94, 92, 91, 95, 101, 95, 94, 93, 106, 106, 106, 106, 104, 104, 104, 104, 104, 104, 104, 104, 103, 105, 103,
105, 103, 105, 103, 103, 103, 103, 106, 106, 105, 106, 106, 106, 106, 106, 104, 104, 103, 103, 109, 108, 101,
105, 117, 113, 117, 116, 111, 110, 119, 113, 120, 136, 135, 124, 129, 128, 153, 154, 156, 159, 158, 162, 167,
177, 176, 175, 174, 177, 178, 185, 180, 183, 186, 187, 185, 186, 184, 182, 182, 179, 175, 179, 177, 176, 174,
173, 171, 170, 171, 169, 166, 164, 170, 165, 163, 164, 160, 157, 157, 153, 155, 151, 148, 147, 149, 151, 145,
149, 144, 142, 144, 143, 137, 136, 139, 138, 137, 138, 135, 134, 129, 127, 126, 129, 128, 125, 130, 127, 123,
122, 116, 118, 116, 114, 122, 112, 120, 113, 110, 113, 108, 116, 112, 109, 109, 109, 109, 108, 106, 107, 107,
107, 106, 106, 106, 109, 109, 109, 109, 109, 109, 107, 109, 107, 109, 107, 109, 109, 109, 109, 108, 106, 109,
108, 106, 109, 108, 106, 109, 108, 106, 109, 108, 106, 109, 108, 106, 109, 108, 106, 109, 108, 106, 109, 108,
106, 109, 108, 106, 109, 108, 106, 109, 108, 106, 109, 108, 106, 109, 108, 106, 109, 108, 106, 109, 109, 107,
109, 107, 109, 107, 109, 107, 109, 107, 109, 107, 109, 107, 109, 107, 109, 107, 109, 107, 109, 107, 109, 107,
109, 107, 109, 107, 109, 107, 109, 107, 109, 107, 109, 107, 109, 107, 109, 107, 109, 107, 109, 109, 109, 109,
106, 109, 108, 106, 109, 106, 109, 106, 109, 108, 106, 111, 109, 108, 106, 112, 111, 106, 113, 112, 111, 110,
109, 106, 109, 106, 116, 118, 120, 126, 121, 122, 123, 116, 115, 121, 116, 113, 119, 115, 112, 116, 116, 110,
112, 109, 108, 107, 104, 103, 101,
];
pwm.set_duty(ch2, _max_duty); // inverse, close channel
loop {
for &i in vals {
let b = (i * 100).saturating_sub(1900);
pwm.set_duty(ch1, b);
Delay.delay_us(1400);
}
}
}
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
let _ = hal::println!("\n\n\n{}", info);
loop {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment