Skip to content

Commit

Permalink
Merge pull request #13537 from jue89/feature/sam0-spi-frequency
Browse files Browse the repository at this point in the history
cpu/sam0_common: mitigate rounding errors of SPI baud rate calculation
  • Loading branch information
benpicco authored Mar 4, 2020
2 parents 0c2c5f2 + 8fbfb13 commit 681678e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion boards/samr30-xpro/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
/** @}*/

/**
Expand Down
7 changes: 5 additions & 2 deletions cpu/sam0_common/periph/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
*
Expand Down

0 comments on commit 681678e

Please sign in to comment.