atsams-rs / atsamx7x-rust

Rust HAL, PACs, and examples for the Microship SAM E70/S70/V70/V71
Apache License 2.0
24 stars 11 forks source link

Wrong Clock Frequency for USART Peripheral Running in SPI Mode #35

Open martinmortsell opened 1 year ago

martinmortsell commented 1 year ago

I'm using the Usart peripheral in SPI Mode and it seems like the SCK is running at exactly 8 times the frequency I set it to. E.g.

let spi_host_handle = handles.spi_host.configure(
    &usart_spi,
    &hclk,
    SpiConfig {
        bitrate: (2_000_000).bps(),
        mode: MODE_1,
    },                                                                                                                                                      │
).unwrap();                                 

yields a bitrate of 8MHz. I haven't explored this behaviour fully, but it seems to me that the cause is:

        const MIN_OVERSAMPLE: u32 = 8;
        let oversample_factor = MIN_OVERSAMPLE * if oversample { 2 } else { 1 };

in /usart/mod.rs Compare 46.6.1.1 which is the current behaviour to 46.6.1.3 which is the desired behaviour in SPI Mode.

martinmortsell commented 1 year ago

Having looked at the datasheet the baudrate calculation needs to be done separately for asynchronous modes and synchronous modes. (And for ISO7816, should that ever be implemented.) Furthermore, Baudrate Configuration should be omitted completely for SPI Client as the clock speed is determined by an external chip in that case.