address SPI_CLKDIV_PRE requirements

* respect N-1
* avoid range overflow
This commit is contained in:
devsaurus 2015-10-04 16:57:50 +02:00
parent ac50f9c6a5
commit 4cf5f37450
1 changed files with 4 additions and 1 deletions

View File

@ -112,10 +112,13 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, unsigned databi
// SPI clock = CPU clock / clock_div // SPI clock = CPU clock / clock_div
// the divider needs to be a multiple of 2 to get a proper waveform shape // the divider needs to be a multiple of 2 to get a proper waveform shape
if ((clock_div & 0x01) != 0) { if ((clock_div & 0x01) != 0) {
// bump the divider to the nextN*2 // bump the divider to the next N*2
clock_div += 0x02; clock_div += 0x02;
} }
clock_div >>= 1; clock_div >>= 1;
// clip to maximum possible CLKDIV_PRE
clock_div = clock_div > SPI_CLKDIV_PRE ? SPI_CLKDIV_PRE : clock_div - 1;
WRITE_PERI_REG(SPI_CLOCK(spi_no), WRITE_PERI_REG(SPI_CLOCK(spi_no),
((clock_div&SPI_CLKDIV_PRE)<<SPI_CLKDIV_PRE_S)| ((clock_div&SPI_CLKDIV_PRE)<<SPI_CLKDIV_PRE_S)|
((1&SPI_CLKCNT_N)<<SPI_CLKCNT_N_S)| ((1&SPI_CLKCNT_N)<<SPI_CLKCNT_N_S)|