Skip to content

Commit

Permalink
Constrain divisor range to 2~256.
Browse files Browse the repository at this point in the history
  • Loading branch information
ezshinoda committed Sep 29, 2018
1 parent d557203 commit 8723416
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/main/drivers/bus_spi_ll.c
Expand Up @@ -27,6 +27,7 @@
#if defined(USE_SPI)

#include "common/utils.h"
#include "common/maths.h"

#include "drivers/bus.h"
#include "drivers/bus_spi.h"
Expand Down Expand Up @@ -226,8 +227,10 @@ void spiSetDivisor(SPI_TypeDef *instance, uint16_t divisor)
}
#endif

divisor = constrain(divisor, 2, 256);

LL_SPI_Disable(instance);
LL_SPI_SetBaudRatePrescaler(instance, divisor ? (ffs(divisor | 0x100) - 2) << SPI_CR1_BR_Pos : 0);
LL_SPI_SetBaudRatePrescaler(instance, (ffs(divisor) - 2) << SPI_CR1_BR_Pos);
LL_SPI_Enable(instance);
}
#endif
5 changes: 4 additions & 1 deletion src/main/drivers/bus_spi_stdperiph.c
Expand Up @@ -26,6 +26,7 @@

#ifdef USE_SPI

#include "common/maths.h"
#include "drivers/bus.h"
#include "drivers/bus_spi.h"
#include "drivers/bus_spi_impl.h"
Expand Down Expand Up @@ -186,10 +187,12 @@ void spiSetDivisor(SPI_TypeDef *instance, uint16_t divisor)
}
#endif

divisor = constrain(divisor, 2, 256);

SPI_Cmd(instance, DISABLE);

const uint16_t tempRegister = (instance->CR1 & ~BR_BITS);
instance->CR1 = tempRegister | (divisor ? ((ffs(divisor | 0x100) - 2) << 3) : 0);
instance->CR1 = tempRegister | ((ffs(divisor) - 2) << 3);

SPI_Cmd(instance, ENABLE);

Expand Down

0 comments on commit 8723416

Please sign in to comment.