atsamx7x-rust icon indicating copy to clipboard operation
atsamx7x-rust copied to clipboard

Wrong Clock Frequency for USART Peripheral Running in SPI Mode

Open martinmortsell opened this issue 1 year ago • 1 comments

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 avatar Oct 05 '22 11:10 martinmortsell

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.

martinmortsell avatar Oct 27 '22 12:10 martinmortsell