ch32-hal icon indicating copy to clipboard operation
ch32-hal copied to clipboard

USART transmit is clipped to 500kbaud

Open silibattlebot opened this issue 1 year ago • 1 comments

CH32V003F4U6, nanoCHV32003 v1.0

If the user tries to set uart_config.baudrate above 500_000, the USART still only transmits at 500kbaud. Datasheet and usart/mod.rs indicate 3 Mbps should be possible. It may be a clocking problem with my usage of hal::Config::default().

#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

use embassy_executor::Spawner;
use embassy_time::Timer;
use hal::gpio::{AnyPin, Level, Output, Pin};
//use hal::println;
use ch32_hal::usart::UartTx;
use {ch32_hal as hal, panic_halt as _};

use heapless::String;

#[embassy_executor::task]
async fn blink(pin: AnyPin, interval_ms: u64) {
    let mut led = Output::new(pin, Level::Low, Default::default());

    loop {
        led.set_high();
        Timer::after_millis(interval_ms).await;
        led.set_low();
        Timer::after_millis(interval_ms).await;
    }
}

#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(spawner: Spawner) -> ! {
    let p = hal::init(hal::Config::default());
    hal::embassy::init();


    let mut uart_config: ch32_hal::usart::Config = Default::default();

    uart_config.baudrate = 500000;

    let mut uart = UartTx::new_blocking(p.USART1, p.PD0, uart_config).unwrap();

    // Adjust the LED GPIO according to your board
    spawner.spawn(blink(p.PD6.degrade(), 100)).unwrap();

    loop {
        Timer::after_millis(100).await;
        uart.blocking_write(b"Hello world!\r\n").unwrap();
    }
}

silibattlebot avatar Jul 14 '24 01:07 silibattlebot

Hi, default hal config of CH32V003F4U6 uses HSI which has a base frequency of 24Mhz and a divider of 3. Thereby USART block is triggered by 8Mhz clock. Oversampling is set to default of 16. Thereby highest baudrate can be 8Mhz / 16 = 500k.

joao404 avatar May 13 '25 21:05 joao404