diff --git a/README.md b/README.md index 5580c61c..e3d9a358 100644 --- a/README.md +++ b/README.md @@ -405,7 +405,7 @@ All other pins can be assigned to any available GPIO: Also refer to the initialization sequence eg in [u8g_graphics_test.lua](lua_examples/u8glib/u8g_graphics_test.lua): ```lua -spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0) +spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 8) ``` @@ -488,7 +488,7 @@ All other pins can be assigned to any available GPIO: Also refer to the initialization sequence eg in [GraphicsTest.lua](lua_examples/ucglib/GraphicsRest.lua): ```lua -spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0) +spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 8) ``` #####Library usage diff --git a/app/driver/spi.c b/app/driver/spi.c index c2d673b9..3167fa05 100644 --- a/app/driver/spi.c +++ b/app/driver/spi.c @@ -64,7 +64,7 @@ void spi_lcd_9bit_write(uint8 spi_no,uint8 high_bit,uint8 low_8bit) * Description : SPI master initial function for common byte units transmission * Parameters : uint8 spi_no - SPI module number, Only "SPI" and "HSPI" are valid *******************************************************************************/ -void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, unsigned databits, uint32_t clock) +void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, unsigned databits, uint32_t clock_div) { uint32 regvalue; @@ -109,12 +109,21 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, unsigned databi //clear Daul or Quad lines transmission mode CLEAR_PERI_REG_MASK(SPI_CTRL(spi_no), SPI_QIO_MODE|SPI_DIO_MODE|SPI_DOUT_MODE|SPI_QOUT_MODE); - // SPI clock=CPU clock/8 + // 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 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), - ((1&SPI_CLKDIV_PRE)<