Skip to content

Commit

Permalink
mpfs/mpfs_corespi.c: Round up divider to prevent overlock of SPI
Browse files Browse the repository at this point in the history
The divider should be rounded to the next full integer to ensure that
the resulting SPI frequency is <= target frequency, i.e. the SPI is
not overclocked.
  • Loading branch information
pussuw authored and t-salminen committed Dec 14, 2023
1 parent 4131728 commit c0f2b11
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions arch/risc-v/src/mpfs/mpfs_corespi.c
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,9 @@ static uint32_t mpfs_spi_setfrequency(struct spi_dev_s *dev,

priv->frequency = frequency;

/* Formula is SPICLK = PCLK/(2*(CFG_CLK + 1)) */
/* Formula is SPICLK = PCLK/(2*(CFG_CLK + 1)) (result is rounded up) */

divider = ((MPFS_FPGA_PERIPHERAL_CLK / frequency) >> 1) - 1;
divider = (MPFS_FPGA_PERIPHERAL_CLK / frequency) >> 1;
priv->actual = MPFS_FPGA_PERIPHERAL_CLK / ((divider + 1) << 1);

DEBUGASSERT(divider < 256u);
Expand Down

0 comments on commit c0f2b11

Please sign in to comment.