12 months ago.

SPi frequency selection


I am not sure how to exactly select the SPI frequency and UART baudrate to transfer the data. The system I have includes an imu, Nucleo L476RG and PC. The Nucleo reads the imu data using SPI protocol and then publishes to host via USB but I have issues of dropped packets. Higher baud rates than 115200kbps don't have this issue, but this baud rate is more than enough when reading IMU data directly from sensor, when nucleo is removed from the circuit. My question is, how to properly select spi frequency and uart baudrate to transfer the data without loss? I am mostly interested in 115200kbps as other components in other part of the system use this rate, so I would like to keep it same for whole system, thus I do not know how to select appropriate SPI frequency.

1 Answer

12 months ago.

This should be simple:

Serial pc(...); 
SPI IMU(...);

main() {
  pc.baud(115200); // set UART to 115200 baud, the USB port will normally do 460800 baud quite happily.
  IMU.frequency(1*1000*1000); // set SPI to 1 MHz or whatever speed you want.

Hopefully it's obvious that for the SPI you could just do IMU.frequency(1000000); but personally I like splitting it into blocks of 1000 like that. To the compiler they are the same thing but from a human readability point of view splitting it makes it obvious it's 1 MHz rather than 100 k or 10 M, if you write it all as one number you have to stop and count the zeros which is slower and more prone to errors.

Almost all SPI devices will do at least 1 MHz, with good wiring most will do 10 MHz but that's normally about the limit for reliable operation without proper boards and connectors.