Change controller to ch32v203f6p6
and use CAN for communication
This commit is contained in:
parent
a764ab89ec
commit
5912a547ba
3 changed files with 120 additions and 52 deletions
29
Cargo.lock
generated
29
Cargo.lock
generated
|
|
@ -32,6 +32,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"ch32-hal",
|
||||
"embassy-executor",
|
||||
"embassy-time",
|
||||
"embedded-hal 1.0.0",
|
||||
"fugit",
|
||||
"panic-halt",
|
||||
|
|
@ -42,7 +43,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "ch32-hal"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/ch32-rs/ch32-hal#2f176c867c6f1c152e6cec6954ebc2758b637fd1"
|
||||
source = "git+https://github.com/paval-shlyk/ch32-hal?rev=ace32859924ab0d5e411521a8f392df01ce262ff#ace32859924ab0d5e411521a8f392df01ce262ff"
|
||||
dependencies = [
|
||||
"ch32-metapac",
|
||||
"critical-section",
|
||||
|
|
@ -52,6 +53,7 @@ dependencies = [
|
|||
"embassy-time",
|
||||
"embassy-time-driver",
|
||||
"embassy-usb-driver",
|
||||
"embedded-can",
|
||||
"embedded-hal 0.2.7",
|
||||
"embedded-hal 1.0.0",
|
||||
"embedded-hal-async",
|
||||
|
|
@ -83,9 +85,9 @@ checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"
|
|||
|
||||
[[package]]
|
||||
name = "darling"
|
||||
version = "0.20.10"
|
||||
version = "0.20.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989"
|
||||
checksum = "fc7f46116c46ff9ab3eb1597a45688b6715c6e628b5c133e288e709a29bcb4ee"
|
||||
dependencies = [
|
||||
"darling_core",
|
||||
"darling_macro",
|
||||
|
|
@ -93,9 +95,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "darling_core"
|
||||
version = "0.20.10"
|
||||
version = "0.20.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5"
|
||||
checksum = "0d00b9596d185e565c2207a0b01f8bd1a135483d02d9b7b0a54b11da8d53412e"
|
||||
dependencies = [
|
||||
"fnv",
|
||||
"ident_case",
|
||||
|
|
@ -107,9 +109,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "darling_macro"
|
||||
version = "0.20.10"
|
||||
version = "0.20.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
|
||||
checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead"
|
||||
dependencies = [
|
||||
"darling_core",
|
||||
"quote",
|
||||
|
|
@ -218,6 +220,15 @@ version = "0.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4fc247028eae04174b6635104a35b1ed336aabef4654f5e87a8f32327d231970"
|
||||
|
||||
[[package]]
|
||||
name = "embedded-can"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e9d2e857f87ac832df68fa498d18ddc679175cf3d2e4aa893988e5601baf9438"
|
||||
dependencies = [
|
||||
"nb 1.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "embedded-hal"
|
||||
version = "0.2.7"
|
||||
|
|
@ -467,9 +478,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.94"
|
||||
version = "1.0.95"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84"
|
||||
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
|
|
|||
17
Cargo.toml
17
Cargo.toml
|
|
@ -10,10 +10,12 @@ bench = false
|
|||
|
||||
[dependencies]
|
||||
panic-halt = "1.0.0"
|
||||
ch32-hal = {git = "https://github.com/ch32-rs/ch32-hal", features = [
|
||||
"ch32v003f4p6",
|
||||
|
||||
# https://github.com/ch32-rs/ch32-hal/pull/75
|
||||
ch32-hal = {git = "https://github.com/paval-shlyk/ch32-hal", rev="ace32859924ab0d5e411521a8f392df01ce262ff", features = [
|
||||
"ch32v203f6p6",
|
||||
"embassy",
|
||||
"time-driver-tim2",
|
||||
"time-driver-tim1",
|
||||
]}
|
||||
qingke-rt = { version = "0.5.0", features = ["highcode"] }
|
||||
qingke = "0.5.0"
|
||||
|
|
@ -23,10 +25,17 @@ embassy-executor = {version = "0.6.0", features = [
|
|||
"integrated-timers",
|
||||
"arch-spin",
|
||||
"executor-thread",
|
||||
"task-arena-size-320", # see https://docs.embassy.dev/embassy-executor/git/cortex-m/index.html#task-arena-size
|
||||
|
||||
# see https://docs.embassy.dev/embassy-executor/git/cortex-m/index.html#task-arena-size
|
||||
#"task-arena-size-64",
|
||||
"task-arena-size-128"
|
||||
#"task-arena-size-192",
|
||||
#"task-arena-size-256",
|
||||
]}
|
||||
fugit = "0.3.7"
|
||||
|
||||
embassy-time = { version = "0.3.0" }
|
||||
|
||||
[profile.release]
|
||||
strip = true
|
||||
opt-level = "z"
|
||||
|
|
|
|||
126
src/main.rs
126
src/main.rs
|
|
@ -4,67 +4,115 @@
|
|||
#![feature(impl_trait_in_assoc_type)]
|
||||
|
||||
use ch32_hal as hal;
|
||||
use hal::bind_interrupts;
|
||||
use hal::println;
|
||||
use hal::time::Hertz;
|
||||
use hal::timer::low_level::CountingMode;
|
||||
use hal::timer::simple_pwm::{PwmPin, SimplePwm};
|
||||
use hal::usart;
|
||||
use hal::can::{Can, CanFifo, CanFilter, CanFrame, CanMode, StandardId};
|
||||
use embassy_executor::Spawner;
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
USART1 => usart::InterruptHandler<hal::peripherals::USART1>;
|
||||
});
|
||||
|
||||
const READ_BUF_SIZE: usize = 128;
|
||||
use embassy_time::{Duration, Ticker};
|
||||
|
||||
#[embassy_executor::main(entry = "qingke_rt::entry")]
|
||||
async fn main(_spawner: Spawner) -> ! {
|
||||
hal::debug::SDIPrint::enable();
|
||||
let config = hal::Config::default();
|
||||
let p = hal::init(config);
|
||||
let p = hal::init(hal::Config::default());
|
||||
|
||||
let mut uart_conf = usart::Config::default();
|
||||
uart_conf.baudrate = 19200;
|
||||
let uart = usart::Uart::new(p.USART1, p.PD6, p.PD5, Irqs, p.DMA1_CH4, p.DMA1_CH5, uart_conf).unwrap(); // TX = PD5, RX = PD6
|
||||
let (mut tx, mut rx) = uart.split();
|
||||
println!("setting up can");
|
||||
let can = Can::new(p.CAN1, p.PA11, p.PA12, CanFifo::Fifo1, CanMode::Normal, 500_000).expect("Valid");
|
||||
println!("setting up can filter");
|
||||
let mut filter = CanFilter::new_id_list();
|
||||
filter
|
||||
.get(0)
|
||||
.unwrap()
|
||||
.set(StandardId::new(0x580 | 0x42).unwrap().into(), Default::default());
|
||||
|
||||
let pin_cw = PwmPin::new_ch3::<0>(p.PC3);
|
||||
let pin_ww = PwmPin::new_ch4::<0>(p.PC4);
|
||||
let mut pwm = SimplePwm::new(
|
||||
p.TIM1,
|
||||
println!("adding can filter");
|
||||
can.add_filter(CanFilter::accept_all());
|
||||
|
||||
println!("setting up pwm");
|
||||
|
||||
let pin_r = PwmPin::new_ch2::<0>(p.PA1);
|
||||
let pin_g = PwmPin::new_ch3::<0>(p.PA2);
|
||||
let pin_b = PwmPin::new_ch4::<0>(p.PA3);
|
||||
let mut pwm_rgb = SimplePwm::new(
|
||||
p.TIM2,
|
||||
None,
|
||||
None,
|
||||
Some(pin_cw),
|
||||
Some(pin_ww),
|
||||
Some(pin_r),
|
||||
Some(pin_g),
|
||||
Some(pin_b),
|
||||
Hertz::khz(1),
|
||||
CountingMode::default(),
|
||||
);
|
||||
let (ch_cw, ch_ww) = (hal::timer::Channel::Ch3, hal::timer::Channel::Ch4);
|
||||
|
||||
let _max_duty = pwm.get_max_duty();
|
||||
pwm.set_duty(ch_ww, 100);
|
||||
pwm.enable(ch_ww);
|
||||
let (ch_r, ch_g, ch_b) = (hal::timer::Channel::Ch2, hal::timer::Channel::Ch3, hal::timer::Channel::Ch4);
|
||||
|
||||
pwm_rgb.set_duty(ch_r, 0);
|
||||
pwm_rgb.enable(ch_r);
|
||||
pwm_rgb.set_duty(ch_g, 0);
|
||||
pwm_rgb.enable(ch_g);
|
||||
pwm_rgb.set_duty(ch_b, 0);
|
||||
pwm_rgb.enable(ch_b);
|
||||
|
||||
pwm.set_duty(ch_cw, 50);
|
||||
pwm.enable(ch_cw);
|
||||
let pin_cw = PwmPin::new_ch1::<0>(p.PA6);
|
||||
let pin_ww = PwmPin::new_ch2::<0>(p.PA7);
|
||||
let mut pwm_w = SimplePwm::new(
|
||||
p.TIM3,
|
||||
Some(pin_cw),
|
||||
Some(pin_ww),
|
||||
None,
|
||||
None,
|
||||
Hertz::khz(1),
|
||||
CountingMode::default(),
|
||||
);
|
||||
let (ch_cw, ch_ww) = (hal::timer::Channel::Ch1, hal::timer::Channel::Ch2);
|
||||
|
||||
let _ = tx.write(b"ready!\r\n").await;
|
||||
let _max_duty = pwm_w.get_max_duty();
|
||||
pwm_w.set_duty(ch_ww, 0);
|
||||
pwm_w.enable(ch_ww);
|
||||
|
||||
pwm_w.set_duty(ch_cw, 0);
|
||||
pwm_w.enable(ch_cw);
|
||||
|
||||
let mut ticker = Ticker::every(Duration::from_millis(25));
|
||||
//let mut ticker = Ticker::every(Duration::from_millis(250));
|
||||
println!("entering main loop");
|
||||
let mut rbuf: [u8; READ_BUF_SIZE] = [0u8; READ_BUF_SIZE];
|
||||
let msg: [u8; 8] = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF];
|
||||
loop {
|
||||
println!("wfd");
|
||||
let r = rx.read_until_idle(&mut rbuf).await;
|
||||
match r {
|
||||
Ok(len) => {
|
||||
println!("got {} bytes", len);
|
||||
for i in 0..len {
|
||||
println!(" {}: {:#x}", i, rbuf[i]);
|
||||
}
|
||||
}
|
||||
Err(e) => println!("RX Error: {:?}", e),
|
||||
/*
|
||||
let frame = CanFrame::new(StandardId::new(0x42).unwrap(), &msg).unwrap();
|
||||
match can.transmit(&frame) {
|
||||
Ok(_) => println!("Sent CAN message {:?}", msg),
|
||||
Err(_) => println!("Error sending CAN message"),
|
||||
};
|
||||
let _ = can.transmit(&frame);
|
||||
|
||||
println!("-");
|
||||
match can.try_recv() {
|
||||
Err(_) => println!("Receive error"),
|
||||
Ok(recv_msg) => println!("Received: {:?}", recv_msg),
|
||||
}
|
||||
|
||||
println!(".");
|
||||
*/
|
||||
|
||||
for x in 0..100 {
|
||||
pwm_w.set_duty(ch_cw, x*8);
|
||||
ticker.next().await;
|
||||
}
|
||||
for x in 0..100 {
|
||||
pwm_w.set_duty(ch_cw, (100-x)*8);
|
||||
ticker.next().await;
|
||||
}
|
||||
for x in 0..100 {
|
||||
pwm_w.set_duty(ch_ww, x*8);
|
||||
ticker.next().await;
|
||||
}
|
||||
for x in 0..100 {
|
||||
pwm_w.set_duty(ch_ww, (100-x)*8);
|
||||
ticker.next().await;
|
||||
}
|
||||
|
||||
ticker.next().await;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue