Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define ST7789_CLK 21
- #define ST7789_DATA 22
- #define ST7789_RST 27 // active low
- #define ST7789_CS 26 // active low
- #define ST7789_DC 25 // 1=data, 0=cmd
- #define DATA_MODE gpio_set_level(ST7789_DC, 1);
- #define CMD_MODE gpio_set_level(ST7789_DC, 0);
- // TFT LCD 76x284
- // 4-wire SPI with DCX
- #include <driver/gpio.h>
- #include <spi_bus.h>
- inline void wait(const uint16_t ms) { vTaskDelay(pdMS_TO_TICKS(ms)); }
- void st7789_init(spi_bus_device_handle_t dev);
- void st7789_setAddrWindow(spi_bus_device_handle_t dev, uint16_t x, uint16_t y,
- uint16_t w, uint16_t h);
- void app_main(void) {
- gpio_reset_pin(ST7789_DC);
- gpio_reset_pin(ST7789_RST);
- gpio_set_direction(ST7789_RST, GPIO_MODE_OUTPUT);
- gpio_set_direction(ST7789_DC, GPIO_MODE_OUTPUT);
- // hw reset
- gpio_set_level(ST7789_RST, 1);
- wait(1);
- gpio_set_level(ST7789_RST, 0);
- wait(1);
- gpio_set_level(ST7789_RST, 1);
- spi_bus_handle_t bus_hdl = {0};
- spi_bus_device_handle_t dev = {0};
- spi_config_t bus_cfg = {.mosi_io_num = ST7789_DATA,
- .miso_io_num = -1,
- .sclk_io_num = ST7789_CLK,
- .max_transfer_sz = 1};
- spi_device_config_t st7789_cfg = {
- .cs_io_num = ST7789_CS, .mode = 0, .clock_speed_hz = SPI_MASTER_FREQ_10M};
- bus_hdl = spi_bus_create(SPI3_HOST, &bus_cfg);
- if (!bus_hdl) {
- printf("SPI bus error: Cannot create SPI bus\n");
- exit(-1);
- }
- dev = spi_bus_device_create(bus_hdl, &st7789_cfg);
- if (!dev) {
- printf("SPI bus error: Cannot create SPI device\n");
- exit(-1);
- }
- // <---perform SPI here--->
- st7789_init(dev);
- CMD_MODE
- spi_bus_transfer_byte(dev, 0x2C, NULL); // memory write
- DATA_MODE
- for (uint8_t i = 0; i < 25; i++) {
- for (uint8_t j = 0; j < 25; j++) {
- // fill 25x25 area with 0xAAAA
- spi_bus_transfer_byte(dev, 0xAA, NULL);
- spi_bus_transfer_byte(dev, 0xAA, NULL);
- }
- }
- CMD_MODE
- // <---endregion--->
- spi_bus_device_delete(&dev);
- spi_bus_delete(&bus_hdl);
- }
- void st7789_init(spi_bus_device_handle_t dev) {
- spi_bus_transfer_byte(dev, 0x01, NULL); // sw reset
- wait(10);
- spi_bus_transfer_byte(dev, 0x26, NULL); // set gamma
- DATA_MODE
- spi_bus_transfer_byte(dev, 0x01, NULL); // gamma 2.2
- CMD_MODE
- spi_bus_transfer_byte(dev, 0x36, NULL); // memory data access control
- DATA_MODE
- spi_bus_transfer_byte(dev, 0x00, NULL); // 000000_xxb - normal mode
- CMD_MODE
- spi_bus_transfer_byte(dev, 0x3A, NULL); // pixel format
- DATA_MODE
- spi_bus_transfer_byte(dev, 0x26, NULL); // 0_101_0_101 - 65K@16bit
- CMD_MODE
- spi_bus_transfer_byte(dev, 0xB0, NULL); // RAM control
- DATA_MODE
- spi_bus_transfer_byte(dev, 0x00, NULL); // RAM access from MCU
- spi_bus_transfer_byte(dev, 0xCA, NULL); // RAM LE+MSB
- CMD_MODE
- spi_bus_transfer_byte(dev, 0xB2, NULL); // porch settings
- DATA_MODE
- spi_bus_transfer_byte(dev, 0x0C, NULL);
- spi_bus_transfer_byte(dev, 0x0C, NULL);
- spi_bus_transfer_byte(dev, 0x00, NULL);
- spi_bus_transfer_byte(dev, 0x33, NULL);
- spi_bus_transfer_byte(dev, 0x33, NULL);
- CMD_MODE
- spi_bus_transfer_byte(dev, 0xB7, NULL); // gate control
- DATA_MODE
- spi_bus_transfer_byte(dev, 0x45, NULL); // 13.65-10.43V
- CMD_MODE
- spi_bus_transfer_byte(dev, 0x2B, NULL); // VCOM
- DATA_MODE
- spi_bus_transfer_byte(dev, 0x2B, NULL); // 1.175V
- CMD_MODE
- spi_bus_transfer_byte(dev, 0xC0, NULL); // LCM
- DATA_MODE
- spi_bus_transfer_byte(dev, 0x2C, NULL); // XBGR, XMX, XMH
- CMD_MODE
- spi_bus_transfer_byte(dev, 0xC2, NULL); // VDV and VRH
- DATA_MODE
- spi_bus_transfer_byte(dev, 0x01, NULL); // VDV and VRH from CMD wire
- spi_bus_transfer_byte(dev, 0xFF, NULL); // always FFh
- CMD_MODE
- spi_bus_transfer_byte(dev, 0xC3, NULL); // VRH
- DATA_MODE
- spi_bus_transfer_byte(dev, 0x11, NULL); // 4.4+V
- CMD_MODE
- spi_bus_transfer_byte(dev, 0xC4, NULL); // VDV
- DATA_MODE
- spi_bus_transfer_byte(dev, 0x20, NULL); // 0V
- CMD_MODE
- spi_bus_transfer_byte(dev, 0xC6, NULL); // framerate control
- DATA_MODE
- spi_bus_transfer_byte(dev, 0x0F, NULL); // 60Hz
- CMD_MODE
- spi_bus_transfer_byte(dev, 0xD0, NULL); // power control
- DATA_MODE
- spi_bus_transfer_byte(dev, 0xA4, NULL); // always A4h
- spi_bus_transfer_byte(dev, 0xA1, NULL); // 6.8V, -4.8V, 2.3V
- CMD_MODE
- spi_bus_transfer_byte(dev, 0xE0, NULL); // positive voltage gamma control
- DATA_MODE
- spi_bus_transfer_byte(dev, 0xD0, NULL);
- spi_bus_transfer_byte(dev, 0x00, NULL);
- spi_bus_transfer_byte(dev, 0x05, NULL);
- spi_bus_transfer_byte(dev, 0x0E, NULL);
- spi_bus_transfer_byte(dev, 0x15, NULL);
- spi_bus_transfer_byte(dev, 0x0D, NULL);
- spi_bus_transfer_byte(dev, 0x37, NULL);
- spi_bus_transfer_byte(dev, 0x43, NULL);
- spi_bus_transfer_byte(dev, 0x47, NULL);
- spi_bus_transfer_byte(dev, 0x09, NULL);
- spi_bus_transfer_byte(dev, 0x15, NULL);
- spi_bus_transfer_byte(dev, 0x12, NULL);
- spi_bus_transfer_byte(dev, 0x16, NULL);
- spi_bus_transfer_byte(dev, 0x19, NULL);
- CMD_MODE
- spi_bus_transfer_byte(dev, 0xE0, NULL); // negative voltage gamma control
- DATA_MODE
- spi_bus_transfer_byte(dev, 0xD0, NULL);
- spi_bus_transfer_byte(dev, 0x00, NULL);
- spi_bus_transfer_byte(dev, 0x05, NULL);
- spi_bus_transfer_byte(dev, 0x0D, NULL);
- spi_bus_transfer_byte(dev, 0x0C, NULL);
- spi_bus_transfer_byte(dev, 0x06, NULL);
- spi_bus_transfer_byte(dev, 0x2D, NULL);
- spi_bus_transfer_byte(dev, 0x44, NULL);
- spi_bus_transfer_byte(dev, 0x40, NULL);
- spi_bus_transfer_byte(dev, 0x0E, NULL);
- spi_bus_transfer_byte(dev, 0x1C, NULL);
- spi_bus_transfer_byte(dev, 0x18, NULL);
- spi_bus_transfer_byte(dev, 0x16, NULL);
- spi_bus_transfer_byte(dev, 0x19, NULL);
- CMD_MODE
- spi_bus_transfer_byte(dev, 0x11, NULL); // sleep out
- wait(5);
- spi_bus_transfer_byte(dev, 0x29, NULL); // display on
- spi_bus_transfer_byte(dev, 0x13, NULL); // normal display on
- spi_bus_transfer_byte(dev, 0x21, NULL); // inv on
- /*spi_bus_transfer_byte(dev, 0x2A, NULL); // column addr set
- DATA_MODE
- spi_bus_transfer_byte(dev, 0x00, NULL); // XS[15:8]
- spi_bus_transfer_byte(dev, 0x00, NULL); // XS[7:0]
- spi_bus_transfer_byte(dev, 0x00, NULL); // XE[15:8]
- spi_bus_transfer_byte(dev, 0x00, NULL); // XE[7:0]
- CMD_MODE
- spi_bus_transfer_byte(dev, 0x2B, NULL); // row addr set
- DATA_MODE
- spi_bus_transfer_byte(dev, 0x00, NULL); // YS[15:8]
- spi_bus_transfer_byte(dev, 0x00, NULL); // YS[7:0]
- spi_bus_transfer_byte(dev, 0x00, NULL); // YE[15:8]
- spi_bus_transfer_byte(dev, 0x00, NULL); // YE[7:0]
- CMD_MODE*/
- st7789_setAddrWindow(dev, 0, 0, 76, 284);
- }
- void st7789_setAddrWindow(spi_bus_device_handle_t dev, uint16_t x, uint16_t y,
- uint16_t w, uint16_t h) {
- const uint16_t xs = x;
- const uint16_t xe = x + w;
- const uint16_t ys = y;
- const uint16_t ye = y + h;
- CMD_MODE
- spi_bus_transfer_byte(dev, 0x2A, NULL); // column addr set
- DATA_MODE
- spi_bus_transfer_byte(dev, xs >> 8, NULL); // XS[15:8]
- spi_bus_transfer_byte(dev, xs & 0xff, NULL); // XS[7:0]
- spi_bus_transfer_byte(dev, xe >> 8, NULL); // XE[15:8]
- spi_bus_transfer_byte(dev, xe & 0xff, NULL); // XE[7:0]
- CMD_MODE
- spi_bus_transfer_byte(dev, 0x2B, NULL); // row addr set
- DATA_MODE
- spi_bus_transfer_byte(dev, ys >> 8, NULL); // YS[15:8]
- spi_bus_transfer_byte(dev, ys & 0xff, NULL); // YS[7:0]
- spi_bus_transfer_byte(dev, ye >> 8, NULL); // Ye[15:8]
- spi_bus_transfer_byte(dev, ye & 0xff, NULL); // Ye[7:0]
- CMD_MODE
- }
Advertisement
Add Comment
Please, Sign In to add comment