From 0032d35fe83f1394c8b7913c7d09ba5bd0d0a5ff Mon Sep 17 00:00:00 2001 From: Juergen Fitschen Date: Tue, 3 Mar 2020 12:24:42 +0100 Subject: [PATCH 1/2] cpu/sam0_common: mitigate rounding errors of baud rate calculation Instead of always rounding up, the driver now rounds to the nearest integer. This reduces the absolut rounding error when setting SPI baud rates. --- cpu/sam0_common/periph/spi.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cpu/sam0_common/periph/spi.c b/cpu/sam0_common/periph/spi.c index f76e2a028d..ff6e858620 100644 --- a/cpu/sam0_common/periph/spi.c +++ b/cpu/sam0_common/periph/spi.c @@ -103,8 +103,11 @@ int spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t clk) /* configure bus clock, in synchronous mode its calculated from * BAUD.reg = (f_ref / (2 * f_bus) - 1) - * with f_ref := CLOCK_CORECLOCK as defined by the board */ - const uint8_t baud = (sam0_gclk_freq(spi_config[bus].gclk_src) / (2 * clk) - 1); + * with f_ref := CLOCK_CORECLOCK as defined by the board + * to mitigate the rounding error due to integer arithmetic, the + * equation is modified to + * BAUD.reg = ((f_ref + f_bus) / (2 * f_bus) - 1) */ + const uint8_t baud = ((sam0_gclk_freq(spi_config[bus].gclk_src) + clk) / (2 * clk) - 1); /* configure device to be master and set mode and pads, * From 8fbfb13c84e79213b0319ecd21fb8ac650cd1aef Mon Sep 17 00:00:00 2001 From: Juergen Fitschen Date: Tue, 3 Mar 2020 12:29:18 +0100 Subject: [PATCH 2/2] Revert "boards/samr30-xpro: fix AT86RF212B communication" This reverts commit a9faac7218e86f9c44a34dabec1d509c434bdc36. The changed rounding behavior sets the SPI baud rate to 4MHz, which is a suitable rate for the AT86RF212B. --- boards/samr30-xpro/include/board.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boards/samr30-xpro/include/board.h b/boards/samr30-xpro/include/board.h index 9067d5215e..d6eb0fc9aa 100644 --- a/boards/samr30-xpro/include/board.h +++ b/boards/samr30-xpro/include/board.h @@ -36,7 +36,7 @@ extern "C" { #define AT86RF2XX_PARAM_INT GPIO_PIN(PB, 0) #define AT86RF2XX_PARAM_SLEEP GPIO_PIN(PA, 20) #define AT86RF2XX_PARAM_RESET GPIO_PIN(PB, 15) -#define AT86RF2XX_PARAM_SPI_CLK SPI_CLK_1MHZ +#define AT86RF2XX_PARAM_SPI_CLK SPI_CLK_5MHZ /** @}*/ /**