Skip to content

Commit f0dc0ff

Browse files
authored
Upgrade example deps and push v00x to use 48Mhz + 3Mbps baud rate (#30)
* update deps for v003 examples. * update deps for v103 examples. * update v00x examples and validate against OSC Dev V006 board. validated at 48MHz / 3Mbps baud.
1 parent 6012fe5 commit f0dc0ff

13 files changed

Lines changed: 79 additions & 39 deletions

File tree

‎examples/ch32/v003/Cargo.lock‎

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎examples/ch32/v003/Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ codegen-units = 1
1919
panic = "abort"
2020

2121
[patch.crates-io]
22-
qingke = { git = "https://github.com/OpenServoCore/qingke", branch = "main" }
23-
qingke-rt = { git = "https://github.com/OpenServoCore/qingke", branch = "main" }
22+
qingke = { git = "https://github.com/ch32-rs/qingke", branch = "main" }
23+
qingke-rt = { git = "https://github.com/ch32-rs/qingke", branch = "main" }

‎examples/ch32/v003/app/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tinyboot-ch32 = { path = "../../../../ch32", default-features = false }
88
embedded-io = "0.7"
99
critical-section = "1"
1010
ch32-hal = { git = "https://github.com/ch32-rs/ch32-hal", default-features = false }
11-
qingke-rt = { version = "0.6", features = ["v2"] }
11+
qingke-rt = { version = "0.7", features = ["v2"] }
1212
defmt = "1.0"
1313
defmt-rtt = "1.1"
1414

‎examples/ch32/v00x/Cargo.lock‎

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎examples/ch32/v00x/Cargo.toml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ codegen-units = 1
2020
panic = "abort"
2121

2222
[patch.crates-io]
23-
qingke = { git = "https://github.com/OpenServoCore/qingke", branch = "main" }
24-
qingke-rt = { git = "https://github.com/OpenServoCore/qingke", branch = "main" }
23+
qingke = { git = "https://github.com/ch32-rs/qingke", branch = "main" }
24+
qingke-rt = { git = "https://github.com/ch32-rs/qingke", branch = "main" }
25+

‎examples/ch32/v00x/app/Cargo.toml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ edition.workspace = true
77
tinyboot-ch32 = { path = "../../../../ch32", default-features = false }
88
embedded-io = "0.7"
99
critical-section = "1"
10-
ch32-hal = { git = "https://github.com/ch32-rs/ch32-hal", default-features = false }
11-
qingke-rt = { version = "0.6", features = ["v2"] }
10+
# See https://github.com/ch32-rs/ch32-hal/pull/169
11+
ch32-hal = { git = "https://github.com/ch32-rs/ch32-hal", branch = "fix/v00x-48mhz", default-features = false }
12+
qingke-rt = { version = "0.7", features = ["v2"] }
1213
defmt = "1.0"
1314
defmt-rtt = "1.1"
1415

‎examples/ch32/v00x/app/build.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ fn main() {
1111
match (system_flash, user_flash) {
1212
(true, false) => "system-flash",
1313
(false, true) => "user-flash",
14-
_ => panic!("Enable exactly one flash mode: `system-flash`, `user-flash`, or `standalone`"),
14+
_ => panic!(
15+
"Enable exactly one flash mode: `system-flash`, `user-flash`, or `standalone`"
16+
),
1517
}
1618
};
1719

‎examples/ch32/v00x/app/src/main.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ static LED: Shared<Output<'static>> = Mutex::new(RefCell::new(None));
3434

3535
#[qingke_rt::entry]
3636
fn main() -> ! {
37-
let p = ch32_hal::init(Default::default());
37+
let mut config = ch32_hal::Config::default();
38+
config.rcc = ch32_hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI;
39+
let p = ch32_hal::init(config);
3840

3941
// LED blink via TIM2 interrupt (2 Hz toggle = 1 Hz blink)
4042
critical_section::with(|cs| {
@@ -54,7 +56,7 @@ fn main() -> ! {
5456
// 2: TX=PD0, RX=PD1
5557
// 3: TX=PC0, RX=PC1
5658
let mut uart_config = usart::Config::default();
57-
uart_config.baudrate = 115200;
59+
uart_config.baudrate = 3_000_000;
5860
let uart = Uart::new_blocking::<3>(p.USART1, p.PC1, p.PC0, uart_config).unwrap();
5961
let (tx, rx) = uart.split();
6062
let mut rx = transport::Rx(rx);

‎examples/ch32/v00x/app/src/transport.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
//! embedded_io adapters for ch32-hal blocking UART with optional RS-485 DE/RE.
2+
//!
3+
//! RX bypasses ch32-hal's `blocking_read` and polls `STATR.RXNE` / `DATAR`
4+
//! directly — at 48 MHz with a 3 Mbps line, each byte arrives every ~160
5+
//! cycles and the per-byte overhead of `check_rx_flags` + the `embedded_io`
6+
//! adapter chain is enough to overrun.
27
38
use ch32_hal::gpio::{Level, Output};
49
use ch32_hal::mode::Blocking;
10+
use ch32_hal::pac;
511
use ch32_hal::usart::{Instance, UartRx, UartTx};
612
use core::convert::Infallible;
713
use embedded_io::{ErrorType, Read, Write};
@@ -17,8 +23,12 @@ impl<T: Instance> Read for Rx<'_, T> {
1723
if buf.is_empty() {
1824
return Ok(0);
1925
}
20-
let _ = self.0.blocking_read(&mut buf[..1]);
21-
Ok(1)
26+
let regs = pac::USART1;
27+
for slot in buf.iter_mut() {
28+
while !regs.statr().read().rxne() {}
29+
*slot = regs.datar().read().dr() as u8;
30+
}
31+
Ok(buf.len())
2232
}
2333
}
2434

‎examples/ch32/v00x/boot/src/main.rs‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,30 @@ use tinyboot_ch32_rt as _;
1313
tinyboot_ch32::boot::boot_version!();
1414

1515
use tinyboot_ch32::boot::prelude::*;
16+
use tinyboot_ch32::pac::{
17+
FLASH, RCC,
18+
rcc::vals::{Hpre, Pllsrc, Sw},
19+
};
20+
21+
fn init_48mhz_hsi_pll() {
22+
FLASH.actlr().modify(|w| w.set_latency(2));
23+
24+
RCC.cfgr0().modify(|w| {
25+
w.set_pllsrc(Pllsrc::HSI);
26+
w.set_hpre(Hpre::DIV1);
27+
});
28+
29+
RCC.ctlr().modify(|w| w.set_pllon(true));
30+
while !RCC.ctlr().read().pllrdy() {}
31+
32+
RCC.cfgr0().modify(|w| w.set_sw(Sw::PLL));
33+
while RCC.cfgr0().read().sws() != Sw::PLL {}
34+
}
1635

1736
#[unsafe(export_name = "main")]
1837
fn main() -> ! {
38+
init_48mhz_hsi_pll();
39+
1940
// USART1 transport. Remap options (CH32V00x):
2041
// Remap0 (default): TX=PD5, RX=PD6
2142
// Remap1: TX=PD6, RX=PD5
@@ -35,11 +56,14 @@ fn main() -> ! {
3556
// tx_en: Some(TxEnConfig { pin: Pin::PC2, tx_level: Level::High }),
3657
let transport = Usart::new(&UsartConfig {
3758
duplex: Duplex::Full,
38-
baud: BaudRate::B115200,
39-
pclk: 8_000_000,
59+
baud: BaudRate::B3000000,
60+
pclk: 48_000_000,
4061
mapping: UsartMapping::Usart1Remap3,
4162
rx_pull: Pull::None,
42-
tx_en: Some(TxEnConfig { pin: Pin::PC2, tx_level: Level::High }),
63+
tx_en: Some(TxEnConfig {
64+
pin: Pin::PC2,
65+
tx_level: Level::High,
66+
}),
4367
});
4468
tinyboot_ch32::boot::run(transport, BootCtl::new());
4569
}

0 commit comments

Comments
 (0)