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

@ -116,6 +116,9 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, unsigned databi
clock_div += 0x02;
}
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),
((clock_div&SPI_CLKDIV_PRE)<<SPI_CLKDIV_PRE_S)|
((1&SPI_CLKCNT_N)<<SPI_CLKCNT_N_S)|