Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpu/sam0_common: mitigate rounding errors of SPI baud rate calculation #13537

Merged
merged 2 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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