#include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/gpio.h" #include "driver/spi_master.h" #include "esp_log.h" #include "st7735.h" static const char *TAG = "st7735"; #define LCD_W 80 #define LCD_H 160 /* T-Dongle C5: LCD shares SPI data lines with the SD card (MOSI=2,MISO=7,SCK=6) * but has its own CS. RS/D-C, backlight, reset are GPIO-only. */ #define LCD_PIN_CS 10 #define LCD_PIN_RS 3 /* D/C: 0=command, 1=data */ #define LCD_PIN_BL 0 /* backlight (active high) */ #define LCD_HOST SPI2_HOST #define LCD_FG_DEF 0x07E0 /* green */ #define LCD_BG_DEF 0x0000 /* black */ static spi_device_handle_t s_dev = NULL; static uint16_t s_fg = LCD_FG_DEF; static uint16_t s_bg = LCD_BG_DEF; static uint16_t s_buf[1024]; static void rs(bool data) { gpio_set_level(LCD_PIN_RS, data ? 1 : 0); } /* Transmit n bytes (data phase). RS already set by caller. */ static esp_err_t tx_bytes(const uint8_t *d, int n) { if (n <= 0) return ESP_OK; spi_transaction_t t; memset(&t, 0, sizeof(t)); t.flags = 0; t.length = (uint32_t)(n * 8); t.tx_buffer = (void *)d; return spi_device_polling_transmit(s_dev, &t); } static void cmd(uint8_t c) { rs(false); tx_bytes(&c, 1); } static void dat(uint8_t v) { rs(true); tx_bytes(&v, 1); } /* Render one 5x7 glyph (5 columns x 7 rows, LSB=top) at (x, y). */ static void draw_glyph(int x, int y, const uint8_t g[5]) { if (x >= LCD_W || y >= LCD_H) return; for (int r = 0; r < 7; r++) { for (int c = 0; c < 5; c++) { int px = x + c, py = y + r; if (px < 0 || py < 0 || px >= LCD_W || py >= LCD_H) continue; uint16_t col = (g[c] >> r) & 1 ? s_fg : s_bg; st7735_fill_rect(px, py, 1, 1, col); } } } /* 5x7 font: indexed by (c - 0x20); each glyph = 5 columns, LSB=top. */ static const uint8_t FONT[][5] = { /* 0x20 space */ {0x00,0x00,0x00,0x00,0x00}, /* 0x21 '!' */ {0x00,0x00,0x5F,0x00,0x00}, /* 0x22 '"' */ {0x00,0x07,0x00,0x07,0x00}, /* 0x23 '#' */ {0x14,0x7F,0x14,0x7F,0x14}, /* 0x24 '$' */ {0x24,0x2A,0x7F,0x2A,0x12}, /* 0x25 '%' */ {0x23,0x13,0x08,0x64,0x62}, /* 0x26 '&' */ {0x36,0x49,0x55,0x22,0x50}, /* 0x27 ''' */ {0x00,0x05,0x03,0x00,0x00}, /* 0x28 '(' */ {0x00,0x1C,0x22,0x41,0x00}, /* 0x29 ')' */ {0x00,0x41,0x22,0x1C,0x00}, /* 0x2A '*' */ {0x14,0x08,0x3E,0x08,0x14}, /* 0x2B '+' */ {0x08,0x08,0x3E,0x08,0x08}, /* 0x2C ',' */ {0x00,0x50,0x30,0x00,0x00}, /* 0x2D '-' */ {0x08,0x08,0x08,0x08,0x08}, /* 0x2E '.' */ {0x00,0x60,0x60,0x00,0x00}, /* 0x2F '/' */ {0x20,0x10,0x08,0x04,0x02}, /* 0x30 '0' */ {0x3E,0x51,0x49,0x45,0x3E}, /* 0x31 '1' */ {0x00,0x42,0x7F,0x40,0x00}, /* 0x32 '2' */ {0x42,0x61,0x51,0x49,0x46}, /* 0x33 '3' */ {0x21,0x41,0x45,0x4B,0x31}, /* 0x34 '4' */ {0x18,0x14,0x12,0x7F,0x10}, /* 0x35 '5' */ {0x27,0x45,0x45,0x45,0x39}, /* 0x36 '6' */ {0x3C,0x4A,0x49,0x49,0x30}, /* 0x37 '7' */ {0x01,0x71,0x09,0x05,0x03}, /* 0x38 '8' */ {0x36,0x49,0x49,0x49,0x36}, /* 0x39 '9' */ {0x06,0x49,0x49,0x29,0x1E}, /* 0x3A ':' */ {0x00,0x36,0x36,0x00,0x00}, /* 0x3B ';' */ {0x00,0x56,0x36,0x00,0x00}, /* 0x3C '<' */ {0x00,0x08,0x14,0x22,0x41}, /* 0x3D '=' */ {0x14,0x14,0x14,0x14,0x14}, /* 0x3E '>' */ {0x41,0x22,0x14,0x08,0x00}, /* 0x3F '?' */ {0x02,0x01,0x51,0x09,0x06}, /* 0x40 '@' */ {0x32,0x49,0x79,0x41,0x3E}, /* 0x41 'A' */ {0x7C,0x12,0x11,0x12,0x7C}, /* 0x42 'B' */ {0x7F,0x49,0x49,0x49,0x36}, /* 0x43 'C' */ {0x3E,0x41,0x41,0x41,0x22}, /* 0x44 'D' */ {0x7F,0x41,0x41,0x22,0x1C}, /* 0x45 'E' */ {0x7F,0x49,0x49,0x49,0x41}, /* 0x46 'F' */ {0x7F,0x09,0x09,0x09,0x01}, /* 0x47 'G' */ {0x3E,0x41,0x41,0x51,0x32}, /* 0x48 'H' */ {0x7F,0x08,0x08,0x08,0x7F}, /* 0x49 'I' */ {0x00,0x41,0x7F,0x41,0x00}, /* 0x4A 'J' */ {0x20,0x40,0x41,0x3F,0x01}, /* 0x4B 'K' */ {0x7F,0x08,0x14,0x22,0x41}, /* 0x4C 'L' */ {0x7F,0x40,0x40,0x40,0x40}, /* 0x4D 'M' */ {0x7F,0x02,0x04,0x02,0x7F}, /* 0x4E 'N' */ {0x7F,0x04,0x08,0x10,0x7F}, /* 0x4F 'O' */ {0x3E,0x41,0x41,0x41,0x3E}, /* 0x50 'P' */ {0x7F,0x09,0x09,0x09,0x06}, /* 0x51 'Q' */ {0x3E,0x41,0x51,0x21,0x5E}, /* 0x52 'R' */ {0x7F,0x09,0x19,0x29,0x46}, /* 0x53 'S' */ {0x46,0x49,0x49,0x49,0x31}, /* 0x54 'T' */ {0x01,0x01,0x7F,0x01,0x01}, /* 0x55 'U' */ {0x3F,0x40,0x40,0x40,0x3F}, /* 0x56 'V' */ {0x1F,0x20,0x40,0x20,0x1F}, /* 0x57 'W' */ {0x7F,0x20,0x18,0x20,0x7F}, /* 0x58 'X' */ {0x63,0x14,0x08,0x14,0x63}, /* 0x59 'Y' */ {0x03,0x04,0x78,0x04,0x03}, /* 0x5A 'Z' */ {0x61,0x51,0x49,0x45,0x43}, /* 0x5B '[' */ {0x00,0x00,0x7F,0x41,0x41}, /* 0x5C '\' */ {0x02,0x04,0x08,0x10,0x20}, /* 0x5D ']' */ {0x41,0x41,0x7F,0x00,0x00}, /* 0x5E '^' */ {0x04,0x02,0x01,0x02,0x04}, /* 0x5F '_' */ {0x40,0x40,0x40,0x40,0x40}, /* 0x60 '`' */ {0x00,0x01,0x02,0x04,0x00}, /* 0x61 'a' */ {0x20,0x54,0x54,0x54,0x78}, /* 0x62 'b' */ {0x7F,0x44,0x44,0x44,0x3C}, /* 0x63 'c' */ {0x38,0x44,0x44,0x44,0x28}, /* 0x64 'd' */ {0x7C,0x54,0x54,0x54,0x18}, /* 0x65 'e' */ {0x38,0x54,0x54,0x54,0x18}, /* 0x66 'f' */ {0x08,0x7E,0x09,0x01,0x02}, /* 0x67 'g' */ {0x0C,0x54,0x54,0x54,0x38}, /* 0x68 'h' */ {0x7F,0x08,0x04,0x04,0x78}, /* 0x69 'i' */ {0x00,0x44,0x7D,0x40,0x00}, /* 0x6A 'j' */ {0x20,0x40,0x44,0x3D,0x00}, /* 0x6B 'k' */ {0x7F,0x10,0x28,0x44,0x00}, /* 0x6C 'l' */ {0x00,0x41,0x7F,0x40,0x00}, /* 0x6D 'm' */ {0x7C,0x04,0x18,0x04,0x78}, /* 0x6E 'n' */ {0x7C,0x08,0x04,0x04,0x78}, /* 0x6F 'o' */ {0x38,0x44,0x44,0x44,0x38}, /* 0x70 'p' */ {0x7C,0x14,0x14,0x14,0x08}, /* 0x71 'q' */ {0x08,0x14,0x14,0x14,0x7C}, /* 0x72 'r' */ {0x7C,0x08,0x04,0x04,0x08}, /* 0x73 's' */ {0x48,0x54,0x54,0x54,0x20}, /* 0x74 't' */ {0x04,0x3F,0x44,0x40,0x20}, /* 0x75 'u' */ {0x3C,0x40,0x40,0x20,0x7C}, /* 0x76 'v' */ {0x1C,0x20,0x40,0x20,0x1C}, /* 0x77 'w' */ {0x3C,0x40,0x30,0x40,0x3C}, /* 0x78 'x' */ {0x44,0x28,0x10,0x28,0x44}, /* 0x79 'y' */ {0x0C,0x50,0x50,0x50,0x3C}, /* 0x7A 'z' */ {0x44,0x64,0x54,0x4C,0x44}, /* 0x7B '{' */ {0x00,0x08,0x36,0x41,0x00}, /* 0x7C '|' */ {0x00,0x00,0x7F,0x00,0x00}, /* 0x7D '}' */ {0x00,0x41,0x36,0x08,0x00}, /* 0x7E '~' */ {0x08,0x04,0x08,0x10,0x08}, }; #define FONT_N (sizeof(FONT) / sizeof(FONT[0])) uint16_t st7735_rgb565(int r, int g, int b) { r >>= 3; g >>= 2; b >>= 3; return (uint16_t)((r << 11) | (g << 5) | b); } static void write_area_pixels(int x0, int y0, int x1, int y1, const uint16_t *px) { /* px is (x1-x0+1)*(y1-y0+1) 16-bit pixels, row-major from (x0,y0). */ cmd(0x2A); dat((x0 >> 8) & 0xFF); dat(x0 & 0xFF); dat((x1 >> 8) & 0xFF); dat(x1 & 0xFF); cmd(0x2B); dat((y0 >> 8) & 0xFF); dat(y0 & 0xFF); dat((y1 >> 8) & 0xFF); dat(y1 & 0xFF); cmd(0x2C); rs(true); int n = (x1 - x0 + 1) * (y1 - y0 + 1); /* chunk to respect the shared-bus max_transfer_sz */ int off = 0; while (off < n) { int chunk = n - off; if (chunk > 512) chunk = 512; tx_bytes((const uint8_t *)(px + off), chunk * 2); off += chunk; } } void st7735_fill_rect(int x, int y, int w, int h, uint16_t color) { if (x < 0) { w += x; x = 0; } if (y < 0) { h += y; y = 0; } if (x >= LCD_W || y >= LCD_H || w <= 0 || h <= 0) return; if (x + w > LCD_W) w = LCD_W - x; if (y + h > LCD_H) h = LCD_H - y; int n = w * h; if (n > (int)sizeof(s_buf) / 2) n = (int)sizeof(s_buf) / 2; for (int i = 0; i < n; i++) s_buf[i] = color; write_area_pixels(x, y, x + w - 1, y + h - 1, s_buf); } void st7735_clear(uint16_t color) { st7735_fill_rect(0, 0, LCD_W, LCD_H, color); } void st7735_set_fg(uint16_t c) { s_fg = c; } void st7735_set_bg(uint16_t c) { s_bg = c; } void st7735_text(int x, int y, const char *s) { for (; *s; s++) { uint8_t c = (uint8_t)*s; if (c >= 0x20 && (c - 0x20) < FONT_N) { draw_glyph(x, y, FONT[c - 0x20]); x += 6; } else if (c == '\n') { x = 0; y += 8; } else { x += 6; } } } bool st7735_ready(void) { return s_dev != NULL; } esp_err_t st7735_init(void) { /* GPIO: RS + backlight (CS is SPI-managed via spics_io_num). */ gpio_set_direction(LCD_PIN_RS, GPIO_MODE_OUTPUT); gpio_set_level(LCD_PIN_RS, 0); gpio_set_direction(LCD_PIN_BL, GPIO_MODE_OUTPUT); gpio_set_level(LCD_PIN_BL, 1); /* backlight on */ /* Ensure the shared SPI2_HOST bus is initialized (cits_store may have done * it already; tolerate ESP_ERR_INVALID_STATE). */ spi_bus_config_t bus = { .mosi_io_num = 2, .miso_io_num = 7, .sclk_io_num = 6, .quadwp_io_num = -1, .quadhd_io_num = -1, .max_transfer_sz = 4096, }; esp_err_t e = spi_bus_initialize(LCD_HOST, &bus, SPI_DMA_CH_AUTO); if (e != ESP_OK && e != ESP_ERR_INVALID_STATE) { ESP_LOGE(TAG, "spi_bus_initialize: %s", esp_err_to_name(e)); return e; } spi_device_interface_config_t cfg = { .clock_speed_hz = 24 * 1000 * 1000, .mode = 0, .spics_io_num = LCD_PIN_CS, /* SPI-managed CS (GPIO10) */ .flags = SPI_DEVICE_NO_DUMMY, .command_bits = 0, .address_bits = 0, }; e = spi_bus_add_device(LCD_HOST, &cfg, &s_dev); if (e != ESP_OK) { ESP_LOGE(TAG, "spi_device_add_transmit: %s", esp_err_to_name(e)); return e; } vTaskDelay(pdMS_TO_TICKS(120)); /* power-on */ cmd(0x12); vTaskDelay(pdMS_TO_TICKS(50)); /* SWRESET */ cmd(0x11); vTaskDelay(pdMS_TO_TICKS(120)); /* SLPOUT */ cmd(0x36); dat(0x08); /* MADCTL */ cmd(0x3A); dat(0x05); /* COLMOD 16-bit */ cmd(0x2A); dat(0x00); dat(0x00); dat(0x00); dat(0x4F); /* CASET 0-79 */ cmd(0x2B); dat(0x00); dat(0x00); dat(0x00); dat(0x9F); /* RASET 0-159 */ cmd(0x2C); /* RAMWR */ cmd(0x13); /* normal display on */ cmd(0x29); /* DISPON */ st7735_clear(LCD_BG_DEF); ESP_LOGI(TAG, "ST7735 80x160 ready"); return ESP_OK; }