- /*
- * bsp_display.c - ST7735S 80x160 驱动
- *
- * 采用 4 线 SPI(SCK/MOSI/CS/DC + RST/BL),RGB565,像素大端发送。
- * SPI 句柄来自 CubeMX2 生成的 mx_spi2_gethandle(),引脚来自 board_config.h。
- *
- * 初始化命令序列来自一块实测可点亮的 0.96" 80x160 IPS(ST7735S);
- * 若你的模组是 ST7735R 或偏移不同,只需改 board_config.h 的 OFFSET/INVERSION。
- */
- #include "bsp_display.h"
- #include <string.h>
- #include "stm32c5xx_hal.h"
- #include "stm32c5xx_hal_gpio.h"
- #include "stm32c5xx_hal_spi.h"
- /* -------------------------------------------------------------------------- */
- /* 面板命令 */
- /* -------------------------------------------------------------------------- */
- #define ST_SWRESET 0x01u
- #define ST_SLPOUT 0x11u
- #define ST_NORON 0x13u
- #define ST_INVOFF 0x20u
- #define ST_INVON 0x21u
- #define ST_DISPOFF 0x28u
- #define ST_DISPON 0x29u
- #define ST_CASET 0x2Au
- #define ST_RASET 0x2Bu
- #define ST_RAMWR 0x2Cu
- #define ST_MADCTL 0x36u
- #define ST_COLMOD 0x3Au
- /* -------------------------------------------------------------------------- */
- /* 内部状态 */
- /* -------------------------------------------------------------------------- */
- static hal_spi_handle_t *s_spi;
- static uint16_t s_w = 80u;
- static uint16_t s_h = 160u;
- static uint16_t s_x_off = BSP_LCD_X_OFFSET;
- static uint16_t s_y_off = BSP_LCD_Y_OFFSET;
- static uint8_t s_rotation = BSP_LCD_ROTATION;
- static uint8_t s_tx_buf[BSP_LCD_CHUNK_PIXELS * 2u];
- static volatile uint32_t s_spi_ok;
- static volatile uint32_t s_spi_fail;
- static volatile uint32_t s_bytes;
- /* -------------------------------------------------------------------------- */
- /* 引脚操作(HAL2 GPIO) */
- /* -------------------------------------------------------------------------- */
- #define LCD_CS_LOW() HAL_GPIO_WritePin(BSP_LCD_CS_PORT, BSP_LCD_CS_PIN, HAL_GPIO_PIN_RESET)
- #define LCD_CS_HIGH() HAL_GPIO_WritePin(BSP_LCD_CS_PORT, BSP_LCD_CS_PIN, HAL_GPIO_PIN_SET)
- #define LCD_DC_LOW() HAL_GPIO_WritePin(BSP_LCD_DC_PORT, BSP_LCD_DC_PIN, HAL_GPIO_PIN_RESET)
- #define LCD_DC_HIGH() HAL_GPIO_WritePin(BSP_LCD_DC_PORT, BSP_LCD_DC_PIN, HAL_GPIO_PIN_SET)
- #define LCD_RST_LOW() HAL_GPIO_WritePin(BSP_LCD_RST_PORT, BSP_LCD_RST_PIN, HAL_GPIO_PIN_RESET)
- #define LCD_RST_HIGH() HAL_GPIO_WritePin(BSP_LCD_RST_PORT, BSP_LCD_RST_PIN, HAL_GPIO_PIN_SET)
- #define LCD_BL_LOW() HAL_GPIO_WritePin(BSP_LCD_BL_PORT, BSP_LCD_BL_PIN, HAL_GPIO_PIN_RESET)
- #define LCD_BL_HIGH() HAL_GPIO_WritePin(BSP_LCD_BL_PORT, BSP_LCD_BL_PIN, HAL_GPIO_PIN_SET)
- /* -------------------------------------------------------------------------- */
- /* 底层 SPI */
- /* -------------------------------------------------------------------------- */
- static bool lcd_spi_tx(const uint8_t *buf, uint32_t len)
- {
- if ((s_spi == NULL) || (len == 0u))
- {
- return false;
- }
- /* HAL2 的 count_packet 对 8 bit 数据宽度就是字节数 */
- if (HAL_SPI_Transmit(s_spi, buf, len, 100u) == HAL_OK)
- {
- s_spi_ok++;
- s_bytes += len;
- return true;
- }
- s_spi_fail++;
- return false;
- }
- static bool lcd_spi_byte(uint8_t b)
- {
- return lcd_spi_tx(&b, 1u);
- }
- static void lcd_cmd_data(uint8_t cmd, const uint8_t *data, uint16_t len)
- {
- LCD_CS_LOW();
- LCD_DC_LOW();
- (void)lcd_spi_byte(cmd);
- if ((len != 0u) && (data != NULL))
- {
- LCD_DC_HIGH();
- (void)lcd_spi_tx(data, len);
- }
- LCD_CS_HIGH();
- }
- static void lcd_cmd(uint8_t cmd)
- {
- lcd_cmd_data(cmd, NULL, 0u);
- }
- /*
- * 连续命令+数据:调用者负责 CS 已经拉低,函数内不再动 CS。
- * ST7735S 要求 CASET/RASET/RAMWR 到像素数据全程 CS 保持低,
- * 否则 RAMWR 会被 CS 上升沿提前结束,后面的像素被当成新命令 -> 雪花。
- */
- static void lcd_cmd_data_cont(uint8_t cmd, const uint8_t *data, uint16_t len)
- {
- LCD_DC_LOW();
- (void)lcd_spi_byte(cmd);
- if ((len != 0u) && (data != NULL))
- {
- LCD_DC_HIGH();
- (void)lcd_spi_tx(data, len);
- }
- }
- static void lcd_window(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
- {
- uint8_t buf[4];
- uint16_t x0 = (uint16_t)(x + s_x_off);
- uint16_t x1 = (uint16_t)(x + w - 1u + s_x_off);
- uint16_t y0 = (uint16_t)(y + s_y_off);
- uint16_t y1 = (uint16_t)(y + h - 1u + s_y_off);
- LCD_CS_LOW();
- buf[0] = (uint8_t)(x0 >> 8);
- buf[1] = (uint8_t)(x0 & 0xFFu);
- buf[2] = (uint8_t)(x1 >> 8);
- buf[3] = (uint8_t)(x1 & 0xFFu);
- lcd_cmd_data_cont(ST_CASET, buf, 4u);
- buf[0] = (uint8_t)(y0 >> 8);
- buf[1] = (uint8_t)(y0 & 0xFFu);
- buf[2] = (uint8_t)(y1 >> 8);
- buf[3] = (uint8_t)(y1 & 0xFFu);
- lcd_cmd_data_cont(ST_RASET, buf, 4u);
- LCD_DC_LOW();
- (void)lcd_spi_byte(ST_RAMWR);
- LCD_DC_HIGH();
- /* CS 保持低,紧接着由 lcd_stream_xxx() 发像素 */
- }
- static void lcd_stream_color(uint16_t color, uint32_t n)
- {
- uint32_t i;
- uint32_t chunk;
- uint8_t hi = (uint8_t)(color >> 8);
- uint8_t lo = (uint8_t)(color & 0xFFu);
- for (i = 0u; i < BSP_LCD_CHUNK_PIXELS; i++)
- {
- s_tx_buf[2u * i] = hi;
- s_tx_buf[2u * i + 1u] = lo;
- }
- while (n != 0u)
- {
- chunk = (n > BSP_LCD_CHUNK_PIXELS) ? BSP_LCD_CHUNK_PIXELS : n;
- (void)lcd_spi_tx(s_tx_buf, chunk * 2u);
- n -= chunk;
- }
- }
- static void lcd_stream_pixels(const uint16_t *pixels, uint32_t n)
- {
- uint32_t i;
- uint32_t chunk;
- while (n != 0u)
- {
- chunk = (n > BSP_LCD_CHUNK_PIXELS) ? BSP_LCD_CHUNK_PIXELS : n;
- for (i = 0u; i < chunk; i++)
- {
- s_tx_buf[2u * i] = (uint8_t)(pixels[i] >> 8);
- s_tx_buf[2u * i + 1u] = (uint8_t)(pixels[i] & 0xFFu);
- }
- (void)lcd_spi_tx(s_tx_buf, chunk * 2u);
- pixels += chunk;
- n -= chunk;
- }
- }
- /* -------------------------------------------------------------------------- */
- /* 初始化 */
- /* -------------------------------------------------------------------------- */
- struct lcd_init_cmd
- {
- uint8_t cmd;
- uint8_t len;
- uint8_t data[16];
- uint16_t delay_ms;
- };
- static const struct lcd_init_cmd s_init_cmds[] =
- {
- {ST_SLPOUT, 0u, {0}, 120u},
- {0xB1u, 3u, {0x05, 0x3C, 0x3Cu}, 0u}, /* 帧率 */
- {0xB2u, 3u, {0x05, 0x3C, 0x3Cu}, 0u},
- {0xB3u, 6u, {0x05, 0x3C, 0x3Cu, 0x05, 0x3C, 0x3Cu}, 0u},
- {0xB4u, 1u, {0x03u}, 0u}, /* 点反转 */
- {0xC0u, 3u, {0xA2u, 0x02u, 0x84u}, 0u}, /* 电源 */
- {0xC1u, 1u, {0xC5u}, 0u},
- {0xC2u, 2u, {0x0Au, 0x00u}, 0u},
- {0xC3u, 2u, {0x8Au, 0x2Au}, 0u},
- {0xC4u, 2u, {0x8Au, 0xEEu}, 0u},
- {0xC5u, 1u, {0x0Eu}, 0u}, /* VCOM */
- {ST_COLMOD, 1u, {0x05u}, 0u}, /* RGB565 */
- {0xE0u, 16u, {0x02u, 0x1Cu, 0x07u, 0x12u, 0x37u, 0x32u, 0x29u, 0x2Du,
- 0x29u, 0x25u, 0x2Bu, 0x39u, 0x00u, 0x01u, 0x03u, 0x10u}, 0u},
- {0xE1u, 16u, {0x03u, 0x1Du, 0x07u, 0x06u, 0x2Eu, 0x2Cu, 0x29u, 0x2Du,
- 0x2Eu, 0x2Eu, 0x37u, 0x3Fu, 0x00u, 0x00u, 0x02u, 0x10u}, 0u},
- };
- static const uint8_t s_madctl[4] =
- {
- 0xC8u, /* 0 竖屏 MY|MX|BGR */
- 0xA8u, /* 1 横屏 MY|MV|BGR */
- 0x08u, /* 2 竖屏翻转 BGR */
- 0x68u, /* 3 横屏翻转 MX|MV|BGR */
- };
- static void lcd_apply_rotation(void)
- {
- uint8_t madctl = s_madctl[s_rotation & 0x03u];
- lcd_cmd_data(ST_MADCTL, &madctl, 1u);
- if ((s_rotation & 0x01u) != 0u)
- {
- s_w = 160u;
- s_h = 80u;
- s_x_off = BSP_LCD_Y_OFFSET;
- s_y_off = BSP_LCD_X_OFFSET;
- }
- else
- {
- s_w = 80u;
- s_h = 160u;
- s_x_off = BSP_LCD_X_OFFSET;
- s_y_off = BSP_LCD_Y_OFFSET;
- }
- }
- bool bsp_display_init(void)
- {
- uint32_t i;
- s_spi = BSP_LCD_SPI_HANDLE();
- if (s_spi == NULL)
- {
- return false;
- }
- /* 先把控制脚拉到安全电平(引脚复用已在 CubeMX2 里配好) */
- LCD_CS_HIGH();
- LCD_DC_HIGH();
- #if BSP_LCD_USE_RESET
- LCD_RST_HIGH();
- HAL_Delay(10u);
- LCD_RST_LOW();
- HAL_Delay(20u);
- LCD_RST_HIGH();
- HAL_Delay(120u);
- #endif
- lcd_cmd(ST_SWRESET);
- HAL_Delay(120u);
- for (i = 0u; i < (sizeof(s_init_cmds) / sizeof(s_init_cmds[0])); i++)
- {
- const struct lcd_init_cmd *c = &s_init_cmds[i];
- lcd_cmd_data(c->cmd, (c->len != 0u) ? c->data : NULL, c->len);
- if (c->delay_ms != 0u)
- {
- HAL_Delay(c->delay_ms);
- }
- }
- lcd_apply_rotation();
- #if BSP_LCD_INVERSION
- lcd_cmd(ST_INVON);
- #else
- lcd_cmd(ST_INVOFF);
- #endif
- lcd_cmd(ST_NORON);
- HAL_Delay(10u);
- lcd_cmd(ST_DISPON);
- HAL_Delay(20u);
- bsp_display_clear(0x0000u);
- bsp_display_backlight(true);
- return true;
- }
- /* -------------------------------------------------------------------------- */
- /* 绘图 */
- /* -------------------------------------------------------------------------- */
- void bsp_display_fill(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color)
- {
- if ((w == 0u) || (h == 0u) || (x >= s_w) || (y >= s_h))
- {
- return;
- }
- if ((uint32_t)x + w > s_w)
- {
- w = (uint16_t)(s_w - x);
- }
- if ((uint32_t)y + h > s_h)
- {
- h = (uint16_t)(s_h - y);
- }
- lcd_window(x, y, w, h);
- lcd_stream_color(color, (uint32_t)w * h);
- LCD_CS_HIGH();
- }
- void bsp_display_blit(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint16_t *pixels)
- {
- if ((pixels == NULL) || (w == 0u) || (h == 0u))
- {
- return;
- }
- if (((uint32_t)x + w > s_w) || ((uint32_t)y + h > s_h))
- {
- return;
- }
- lcd_window(x, y, w, h);
- lcd_stream_pixels(pixels, (uint32_t)w * h);
- LCD_CS_HIGH();
- }
- void bsp_display_clear(uint16_t color)
- {
- bsp_display_fill(0u, 0u, s_w, s_h, color);
- }
- void bsp_display_backlight(bool on)
- {
- #if BSP_LCD_USE_BL
- if (on)
- {
- LCD_BL_HIGH();
- }
- else
- {
- LCD_BL_LOW();
- }
- #else
- (void)on;
- #endif
- }
- uint16_t bsp_display_width(void)
- {
- return s_w;
- }
- uint16_t bsp_display_height(void)
- {
- return s_h;
- }
- void bsp_display_get_stats(bsp_display_stats_t *st)
- {
- if (st == NULL)
- {
- return;
- }
- st->spi_ok = s_spi_ok;
- st->spi_fail = s_spi_fail;
- st->bytes = s_bytes;
- }
- void bsp_display_reset_stats(void)
- {
- s_spi_ok = 0u;
- s_spi_fail = 0u;
- s_bytes = 0u;
- }