embassy-rs / embassy

Modern embedded framework, using Rust and async.
https://embassy.dev
Apache License 2.0
5.52k stars 768 forks source link

STM32 USB serial example fails to compile on L476RG #2174

Closed mbq closed 11 months ago

mbq commented 12 months ago

The code tries to use USB-synced HSI48: https://github.com/embassy-rs/embassy/blob/0b015bd727547d1eade5cd12c8b6a1b77483e6db/examples/stm32l4/src/bin/usb_serial.rs#L26

which triggers not-found errors on both hsi48 field and Hsi48Config struct in HAL, presumably because this board lacks hardware support. I have copied the CubeMX solution, namely to use PLLSAI1_Q, and it seems to work, though I don't know if this is an optimal one:

   config.rcc.clk48_src = Clk48Src::PLLSAI1_Q;
   config.rcc.pllsai1 = Some(Pll {
        source: PLLSource::HSI,
        prediv: PllPreDiv::DIV1,
        mul: PllMul::MUL12,
        divp: None,
        divq: Some(PllQDiv::DIV4), // 16/1*12/4=48
        divr: None,
    });
    config.vbus_detection = false; //VBUS is not connected on my board
Dirbaio commented 11 months ago

L476RG doesn't have HSI48 nor CRS (needed for sync_from_usb).

The solution is to use PLL, yes. However HSI is not accurate enough to meet USB specs, you should use HSE with some accurate clock source. (HSI48 with CRS can sync to USB to get accuracy, but the regular HSI can't)