From 86e0b2553288bf40a22e1e91d161c075295dd4a7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 10 Aug 2018 16:39:47 +1000 Subject: [PATCH] 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. --- ports/stm32/spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/stm32/spi.c b/ports/stm32/spi.c index 7e9864bbce..1aa8d666fe 100644 --- a/ports/stm32/spi.c +++ b/ports/stm32/spi.c @@ -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(); } #endif - prescale = spi_clock / baudrate; + prescale = (spi_clock + baudrate - 1) / baudrate; } if (prescale <= 2) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; } else if (prescale <= 4) { init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; }