stm32/spi: Round up prescaler calc to never exceed requested baudrate.

Requesting a baudrate of X should never configure the peripheral to have a
baudrate greater than X because connected hardware may not be able to
handle higher speeds.  This patch makes sure to round the prescaler up so
that the actual baudrate is rounded down.
This commit is contained in:
Damien George 2018-08-10 16:39:47 +10:00
parent ca0d78cebb
commit 86e0b25532
1 changed files with 1 additions and 1 deletions

View File

@ -214,7 +214,7 @@ STATIC void spi_set_params(const spi_t *spi_obj, uint32_t prescale, int32_t baud
spi_clock = HAL_RCC_GetPCLK2Freq(); spi_clock = HAL_RCC_GetPCLK2Freq();
} }
#endif #endif
prescale = spi_clock / baudrate; prescale = (spi_clock + baudrate - 1) / baudrate;
} }
if (prescale <= 2) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; } if (prescale <= 2) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; }
else if (prescale <= 4) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; } else if (prescale <= 4) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; }