diff --git a/src/platform/stm32/platform.c b/src/platform/stm32/platform.c index dca9aa33..982035b3 100755 --- a/src/platform/stm32/platform.c +++ b/src/platform/stm32/platform.c @@ -490,13 +490,17 @@ static void spis_init() RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); } -#define SPI_GET_BASE_CLK( id ) ( ( id ) == 2 ? ( HCLK / PCLK2_DIV ) : ( HCLK / PCLK1_DIV ) ) +#define SPI_GET_BASE_CLK( id ) ( ( id ) == 1 ? ( HCLK / PCLK2_DIV ) : ( HCLK / PCLK1_DIV ) ) u32 platform_spi_setup( unsigned id, int mode, u32 clock, unsigned cpol, unsigned cpha, unsigned databits ) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; - u8 prescaler_idx = intlog2( ( unsigned ) ( SPI_GET_BASE_CLK( id ) / clock ) ) - 1; + u8 prescaler_idx = intlog2( ( unsigned ) ( SPI_GET_BASE_CLK( id ) / clock ) ) - 2; + if ( prescaler_idx < 0 ) + prescaler_idx = 0; + if ( prescaler_idx > 7 ) + prescaler_idx = 7; /* Configure SPI pins */ GPIO_InitStructure.GPIO_Pin = spi_gpio_pins[ id ];