SPI clock frequency selection is unclear #12
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I had an oversight in the STM32F407 examples where the selection of the SPI frequency (at max supported of 14MHz) lacks a correlation with the STM32 clock tree.
On STM32F407, the actual SPI clock frequency must be f_PCLK (frequency of the APB clock prescaled from SYSCLK) divided by 2, 4, 8, ..., or 256, which is determined by the
BR[2:0]
bits of theSPI_CR1
register. Luckily, the stm32f4xx_hal crate automatically choose the validBR
value by approximating (rounding down) the user-declared frequency value (see code). In the examples, the 14 MHz value is translated to 42/4 = 10.5 MHz.On other MCUs, however, the supported crate might require that the user specify the frequency that matches the clock tree. Plus, on stm32f4xx_hal it is not immediately clear if the user-defined frequency will be rounded down or up, which might break the speed limit of ENC424J600.
Here are a few ways to improve:
In the STM32F407 examples, use 10.5 MHz instead of 14 MHz when initialising the SPI port. Add some comments to improve our communication with the user.
In README, add a note to clarify the maximum SPI clock frequency supported by ENC424J600, and that the
SPI_CLOCK_FREQ
const refers to this upper limit rather than the fixed requirement.Completely throw away the
SPI_CLOCK_FREQ
const. Possibly add an assertion check on the user-defined value against the hardware limit.