diff --git a/include/st7789.h b/include/st7789.h deleted file mode 100644 index 3eb474c..0000000 --- a/include/st7789.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef __ST7789_H__ -#define __ST7789_H__ - -#include - -#include "driver.h" - -int st7789_open(const struct driver *drv); -int st7789_close(const struct driver *drv); -int st7789_write(const struct driver *drv, const char *buffer, unsigned int len); - -struct st7789 { - const struct driver *spi; - const struct driver *dc; - const struct driver *bl; - const struct driver *rst; - const struct driver *select; - unsigned int height; - unsigned int width; -}; - -static const struct driver_fp st7789_fp = { - .open = st7789_open, - .close = st7789_close, - .read = NULL, - .write = st7789_write, - .ioctl = NULL, -}; - -#endif diff --git a/src/application/st7789_lcd/main.c b/src/application/st7789_lcd/main.c deleted file mode 100644 index 7e7937e..0000000 --- a/src/application/st7789_lcd/main.c +++ /dev/null @@ -1,33 +0,0 @@ -#include "app_util_platform.h" -#include "nrf_gpio.h" -#include "nrf_delay.h" -#include "boards.h" -#include "app_error.h" -#include -#include "nrf_log.h" -#include "nrf_log_ctrl.h" -#include "nrf_log_default_backends.h" - -#include "board.h" -#include "driver.h" - -int main(void) -{ - unsigned int cnt = 0; - APP_ERROR_CHECK(NRF_LOG_INIT(NULL)); - NRF_LOG_DEFAULT_BACKENDS_INIT(); - - drv_open(&led_1); - drv_open(&lcd); - - NRF_LOG_INFO("SPI example started."); - - while(1) { - char c = (cnt++ % 2) + 0x30; - drv_write(&led_1, &c, 1); - - NRF_LOG_FLUSH(); - - nrf_delay_ms(200); - } -} diff --git a/src/application/st7789_lcd/main.cc b/src/application/st7789_lcd/main.cc new file mode 100644 index 0000000..5ddb003 --- /dev/null +++ b/src/application/st7789_lcd/main.cc @@ -0,0 +1,19 @@ +#include "delay.h" + +#include "platform/hal.h" +#include "st7789.h" + +hal::Gpio lcd_reset(26); +hal::Gpio lcd_data_command(18); +hal::Gpio lcd_backlight(23); +hal::Gpio lcd_chip_select(25); +hal::Spi lcd_spi(0, 2, 3, 4, 25); + +St7789 lcd(lcd_spi, lcd_reset, lcd_data_command, lcd_backlight, lcd_chip_select); + +int main(void) +{ + while(true) { + delay_ms(200); + } +} diff --git a/src/st7789.c b/src/st7789.c deleted file mode 100644 index b704aea..0000000 --- a/src/st7789.c +++ /dev/null @@ -1,192 +0,0 @@ -#include -#include -#include -#include - -#include "delay.h" -#include "st7789.h" - -static void send_cmd(const struct driver *drv, uint8_t cmd); -static void send_data_8bit(const struct driver *drv, uint8_t data); - -static void lcd_init(const struct driver *drv); -static void lcd_set_windows(const struct driver *drv, uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end); - -static void lcd_clear(const struct driver *drv, uint16_t Color) -{ - unsigned int i,j; - struct st7789 *this = (struct st7789*)(drv->dev); - - lcd_set_windows(drv, 0, 0, 240, 240); - drv_write(this->dc, "1", 1); - for(i = 0; i < 240; i++) { - for(j = 0; j < 240; j++){ - char c = (Color >> 8) & 0xff; - drv_write(this->spi, &c, 1); - c = Color & 0xff; - drv_write(this->spi, &c, 1); - } - } -} - -int st7789_open(const struct driver *drv) -{ - assert(drv != NULL); - struct st7789 *this = (struct st7789*)(drv->dev); - drv_open(this->rst); - drv_write(this->rst, "1", 1); - drv_open(this->dc); - drv_write(this->dc, "1", 1); - drv_open(this->bl); - drv_write(this->bl, "1", 1); - - drv_open(this->select); - drv_write(this->select, "0", 1); - - drv_open(this->spi); - // hard reset - drv_write(this->rst, "0", 1); - delay_ms(100); - drv_write(this->rst, "1", 1); - delay_ms(100); - - lcd_init(drv); - - lcd_clear(drv, 0x0000); - - return 0; -} - -int st7789_close(const struct driver *drv) -{ - assert(drv != NULL); - struct st7789 *this = (struct st7789*)(drv->dev); - drv_close(this->rst); - drv_close(this->dc); - drv_close(this->bl); - drv_close(this->spi); - return 0; -} - -int st7789_write(const struct driver *drv, const char *buffer, unsigned int len) -{ - assert(drv != NULL); - struct st7789 *this = (struct st7789*)(drv->dev); - uint16_t *image = (uint16_t *)buffer; - lcd_set_windows(drv, 0, 0, this->width, this->height); - drv_write(this->dc, "1", 1); - for (uint16_t i = 0; i < this->height; i++) { - drv_write(this->spi, (const char *)&image[i * this->width], len); - } - return len; -} - -static void send_cmd(const struct driver *drv, uint8_t cmd) -{ - assert(drv != NULL); - struct st7789 *this = (struct st7789*)(drv->dev); - drv_write(this->dc, "0", 1); - drv_write(this->spi, (const char* )&cmd, 1); -} - -static void send_data_8bit(const struct driver *drv, uint8_t data) -{ - assert(drv != NULL); - struct st7789 *this = (struct st7789*)(drv->dev); - drv_write(this->dc, "1", 1); - drv_write(this->spi, (const char* )&data, 1); -} - -static void lcd_init(const struct driver *drv) -{ - send_cmd(drv, 0x36); - send_data_8bit(drv, 0x00); - - send_cmd(drv, 0x3A); - send_data_8bit(drv, 0x05); - - send_cmd(drv, 0xB2); - send_data_8bit(drv, 0x0C); - send_data_8bit(drv, 0x0C); - send_data_8bit(drv, 0x00); - send_data_8bit(drv, 0x33); - send_data_8bit(drv, 0x33); - - send_cmd(drv, 0xB7); //Gate Control - send_data_8bit(drv, 0x35); - - send_cmd(drv, 0xBB); //VCOM Setting - send_data_8bit(drv, 0x19); - - send_cmd(drv, 0xC0); //LCM Control - send_data_8bit(drv, 0x2C); - - send_cmd(drv, 0xC2); //VDV and VRH Command Enable - send_data_8bit(drv, 0x01); - send_cmd(drv, 0xC3); //VRH Set - send_data_8bit(drv, 0x12); - send_cmd(drv, 0xC4); //VDV Set - send_data_8bit(drv, 0x20); - - send_cmd(drv, 0xC6); //Frame Rate Control in Normal Mode - send_data_8bit(drv, 0x0F); - - send_cmd(drv, 0xD0); // Power Control 1 - send_data_8bit(drv, 0xA4); - send_data_8bit(drv, 0xA1); - - send_cmd(drv, 0xE0); //Positive Voltage Gamma Control - send_data_8bit(drv, 0xD0); - send_data_8bit(drv, 0x04); - send_data_8bit(drv, 0x0D); - send_data_8bit(drv, 0x11); - send_data_8bit(drv, 0x13); - send_data_8bit(drv, 0x2B); - send_data_8bit(drv, 0x3F); - send_data_8bit(drv, 0x54); - send_data_8bit(drv, 0x4C); - send_data_8bit(drv, 0x18); - send_data_8bit(drv, 0x0D); - send_data_8bit(drv, 0x0B); - send_data_8bit(drv, 0x1F); - send_data_8bit(drv, 0x23); - - send_cmd(drv, 0xE1); //Negative Voltage Gamma Control - send_data_8bit(drv, 0xD0); - send_data_8bit(drv, 0x04); - send_data_8bit(drv, 0x0C); - send_data_8bit(drv, 0x11); - send_data_8bit(drv, 0x13); - send_data_8bit(drv, 0x2C); - send_data_8bit(drv, 0x3F); - send_data_8bit(drv, 0x44); - send_data_8bit(drv, 0x51); - send_data_8bit(drv, 0x2F); - send_data_8bit(drv, 0x1F); - send_data_8bit(drv, 0x1F); - send_data_8bit(drv, 0x20); - send_data_8bit(drv, 0x23); - - send_cmd(drv, 0x21); //Display Inversion On - send_cmd(drv, 0x11); //Sleep Out - send_cmd(drv, 0x29); //Display On -} - -static void lcd_set_windows(const struct driver *drv, uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end) -{ - //set the X coordinates - send_cmd(drv, 0x2A); - send_data_8bit(drv, (x_start >> 8) & 0xFF); - send_data_8bit(drv, x_start & 0xFF); - send_data_8bit(drv, ((x_end - 1) >> 8) & 0xFF); - send_data_8bit(drv, (x_end - 1) & 0xFF); - - //set the Y coordinates - send_cmd(drv, 0x2B); - send_data_8bit(drv, (y_start >> 8) & 0xFF); - send_data_8bit(drv, y_start & 0xFF); - send_data_8bit(drv, ((y_end - 1) >> 8) & 0xFF); - send_data_8bit(drv, (y_end - 1) & 0xFF); - - send_cmd(drv, 0X2C); -} diff --git a/src/st7789.cc b/src/st7789.cc new file mode 100644 index 0000000..9468d14 --- /dev/null +++ b/src/st7789.cc @@ -0,0 +1,152 @@ +#include + +#include "delay.h" +#include "st7789.h" + +St7789::St7789(interfaces::SpiInterface & spi_if, + interfaces::GpioInterface & rst, + interfaces::GpioInterface & dc, + interfaces::GpioInterface & bl, + interfaces::GpioInterface & cs) + : spi(spi_if) + , reset(rst) + , data_command(dc) + , backlight(bl) + , select(cs) +{ + this->reset.set(); + this->data_command.set(); + this->backlight.set(); + this->select.clear(); + + // hard reset + this->reset.clear(); + delay_ms(100); + this->reset.set(); + delay_ms(100); + + this->init(); + this->clear(0x0000); +} + +void St7789::init() +{ + send_cmd(0x36); + send_data(0x00); + + send_cmd(0x3A); + send_data(0x05); + + send_cmd(0xB2); + send_data(0x0C); + send_data(0x0C); + send_data(0x00); + send_data(0x33); + send_data(0x33); + + send_cmd(0xB7); //Gate Control + send_data(0x35); + + send_cmd(0xBB); //VCOM Setting + send_data(0x19); + + send_cmd(0xC0); //LCM Control + send_data(0x2C); + + send_cmd(0xC2); //VDV and VRH Command Enable + send_data(0x01); + send_cmd(0xC3); //VRH Set + send_data(0x12); + send_cmd(0xC4); //VDV Set + send_data(0x20); + + send_cmd(0xC6); //Frame Rate Control in Normal Mode + send_data(0x0F); + + send_cmd(0xD0); // Power Control 1 + send_data(0xA4); + send_data(0xA1); + + send_cmd(0xE0); //Positive Voltage Gamma Control + send_data(0xD0); + send_data(0x04); + send_data(0x0D); + send_data(0x11); + send_data(0x13); + send_data(0x2B); + send_data(0x3F); + send_data(0x54); + send_data(0x4C); + send_data(0x18); + send_data(0x0D); + send_data(0x0B); + send_data(0x1F); + send_data(0x23); + + send_cmd(0xE1); //Negative Voltage Gamma Control + send_data(0xD0); + send_data(0x04); + send_data(0x0C); + send_data(0x11); + send_data(0x13); + send_data(0x2C); + send_data(0x3F); + send_data(0x44); + send_data(0x51); + send_data(0x2F); + send_data(0x1F); + send_data(0x1F); + send_data(0x20); + send_data(0x23); + + send_cmd(0x21); //Display Inversion On + send_cmd(0x11); //Sleep Out + send_cmd(0x29); //Display On +} + +void St7789::send_cmd(uint8_t cmd) +{ + this->data_command.clear(); + this->spi.send(&cmd, 1); +} + +void St7789::send_data(uint8_t data) +{ + this->data_command.set(); + this->spi.send(&data, 1); +} + +void St7789::clear(uint16_t Color) +{ + unsigned int i,j; + + set_windows(0, 0, 240, 240); + this->data_command.set(); + for(i = 0; i < 240; i++) { + for(j = 0; j < 240; j++){ + uint8_t c = (Color >> 8) & 0xff; + this->spi.send(&c, 1); + c = Color & 0xff; + this->spi.send(&c, 1); + } + } +} + +void St7789::set_windows(uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end) +{ + //set the X coordinates + send_cmd(0x2A); + send_data((x_start >> 8) & 0xFF); + send_data(x_start & 0xFF); + send_data(((x_end - 1) >> 8) & 0xFF); + send_data((x_end - 1) & 0xFF); + + //set the Y coordinates + send_cmd(0x2B); + send_data((y_start >> 8) & 0xFF); + send_data(y_start & 0xFF); + send_data(((y_end - 1) >> 8) & 0xFF); + send_data((y_end - 1) & 0xFF); + + send_cmd(0X2C); +} diff --git a/src/st7789.h b/src/st7789.h new file mode 100644 index 0000000..ba97bfd --- /dev/null +++ b/src/st7789.h @@ -0,0 +1,31 @@ +#ifndef __ST7789_H__ +#define __ST7789_H__ + +#include +#include "gpio_interface.h" +#include "spi_interface.h" + +class St7789 +{ +public: + St7789(interfaces::SpiInterface &, + interfaces::GpioInterface &, + interfaces::GpioInterface &, + interfaces::GpioInterface &, + interfaces::GpioInterface &); + +private: + interfaces::SpiInterface & spi; + interfaces::GpioInterface & reset; + interfaces::GpioInterface & data_command; + interfaces::GpioInterface & backlight; + interfaces::GpioInterface & select; + + void init(); + void send_cmd(uint8_t); + void send_data(uint8_t); + void clear(uint16_t Color); + void set_windows(uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end); +}; + +#endif