From 4cf5f37450333301d9592632011adf2f75255473 Mon Sep 17 00:00:00 2001 From: devsaurus Date: Sun, 4 Oct 2015 16:57:50 +0200 Subject: [PATCH] address SPI_CLKDIV_PRE requirements * respect N-1 * avoid range overflow --- app/driver/spi.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/driver/spi.c b/app/driver/spi.c index 0195205d..3167fa05 100644 --- a/app/driver/spi.c +++ b/app/driver/spi.c @@ -112,10 +112,13 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, unsigned databi // SPI clock = CPU clock / clock_div // the divider needs to be a multiple of 2 to get a proper waveform shape 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 >>= 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)<