diff --git a/pio/st7789_lcd/st7789_lcd.c b/pio/st7789_lcd/st7789_lcd.c index 26862c5be..f6724262b 100644 --- a/pio/st7789_lcd/st7789_lcd.c +++ b/pio/st7789_lcd/st7789_lcd.c @@ -28,6 +28,8 @@ #define PIN_RESET 4 #define PIN_BL 5 +#define SPI_CLOCK_POLARITY 0 // default to 0, some displays require polarity 1 + #define SERIAL_CLK_DIV 1.f #ifndef M_PI @@ -90,7 +92,7 @@ int main() { PIO pio = pio0; uint sm = 0; uint offset = pio_add_program(pio, &st7789_lcd_program); - st7789_lcd_program_init(pio, sm, offset, PIN_DIN, PIN_CLK, SERIAL_CLK_DIV); + st7789_lcd_program_init(pio, sm, offset, SPI_CLOCK_POLARITY, PIN_DIN, PIN_CLK, SERIAL_CLK_DIV); gpio_init(PIN_CS); gpio_init(PIN_DC); diff --git a/pio/st7789_lcd/st7789_lcd.pio b/pio/st7789_lcd/st7789_lcd.pio index aa35c683f..165fc4b5c 100644 --- a/pio/st7789_lcd/st7789_lcd.pio +++ b/pio/st7789_lcd/st7789_lcd.pio @@ -22,7 +22,7 @@ // but we are using a threshold of 8 here (consume 1 byte from each FIFO entry // and discard the remainder) to make things easier for software on the other side -static inline void st7789_lcd_program_init(PIO pio, uint sm, uint offset, uint data_pin, uint clk_pin, float clk_div) { +static inline void st7789_lcd_program_init(PIO pio, uint sm, uint offset, bool cpol, uint data_pin, uint clk_pin, float clk_div) { pio_gpio_init(pio, data_pin); pio_gpio_init(pio, clk_pin); pio_sm_set_consecutive_pindirs(pio, sm, data_pin, 1, true); @@ -33,6 +33,11 @@ static inline void st7789_lcd_program_init(PIO pio, uint sm, uint offset, uint d sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX); sm_config_set_clkdiv(&c, clk_div); sm_config_set_out_shift(&c, false, true, 8); + + // The pin muxes can be configured to invert the output (among other things), + // and this is a cheesy way to allow changing polarity + gpio_set_outover(clk_pin, cpol ? GPIO_OVERRIDE_INVERT : GPIO_OVERRIDE_NORMAL); + pio_sm_init(pio, sm, offset, &c); pio_sm_set_enabled(pio, sm, true); }