1、st7796的引脚分配:
我这里使用PMOD进行转接,主要是他的IO集中,好接线。

分配引脚如下:
CS --------P205
DC --------P015
RST -------P014
BL --------P008
SCK -------P204
MOSI -----P203
VCC -------3.3
GND-------GND
2、打rasc配置SPI0

3、打开stack添加spi

4、配置DC、CS、RST、BLK


配置他们为输出模式。
5、保存后,生成工程。
6、向工程中添加st7789.h 代码如下:
#ifndef ST7796_H
#define ST7796_H
#include "hal_data.h"
#include <stdint.h>
#include <stdbool.h>
#define ST7796_WIDTH 320
#define ST7796_HEIGHT 480
#define BLACK 0x0000
#define WHITE 0xFFFF
#define RED 0xF800
#define GREEN 0x07E0
#define BLUE 0x001F
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define GRAY 0x8410
void st7796_init(void);
void st7796_fill_screen(uint16_t color);
void st7796_draw_pixel(uint16_t x, uint16_t y, uint16_t color);
void st7796_draw_rectangle(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t color);
void st7796_set_backlight(bool on);
#endif
7、添加st7796.c代码如下:
#include "st7796.h"
#include "hal_data.h"
extern spi_instance_ctrl_t g_spi0_ctrl;
#define CS_SET R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_02_PIN_05, BSP_IO_LEVEL_HIGH)
#define CS_CLR R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_02_PIN_05, BSP_IO_LEVEL_LOW)
#define DC_SET R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_15, BSP_IO_LEVEL_HIGH)
#define DC_CLR R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_15, BSP_IO_LEVEL_LOW)
#define RST_SET R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_14, BSP_IO_LEVEL_HIGH)
#define RST_CLR R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_14, BSP_IO_LEVEL_LOW)
#define BL_SET R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_08, BSP_IO_LEVEL_HIGH)
#define BL_CLR R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_08, BSP_IO_LEVEL_LOW)
bool g_transfer_complete;
void spi0_callback(spi_callback_args_t *p_args)
{
if(p_args->event == SPI_EVENT_TRANSFER_COMPLETE)
{
g_transfer_complete = true;
}
}
static bool SPIWaitgtc(void)
{
unsigned short wTimeout = 100;
while(!g_transfer_complete && wTimeout)
{
R_BSP_SoftwareDelay(1u, BSP_DELAY_UNITS_MICROSECONDS);
wTimeout--;
}
g_transfer_complete = false;
if(g_transfer_complete==false && wTimeout==0)
return false;
return true;
}
static void st7796_send_command(uint8_t cmd)
{
DC_CLR;
CS_CLR;
R_SPI_Write(&g_spi0_ctrl, &cmd, 1, SPI_BIT_WIDTH_8_BITS);
SPIWaitgtc();
CS_SET;
}
static void st7796_send_data(uint8_t *data, uint32_t len)
{
DC_SET;
CS_CLR;
R_SPI_Write(&g_spi0_ctrl, data, len, SPI_BIT_WIDTH_8_BITS);
SPIWaitgtc();
CS_SET;
}
static void st7796_send_data16(uint16_t data)
{
uint8_t buf[2];
buf[0] = (data >> 8) & 0xFF;
buf[1] = data & 0xFF;
st7796_send_data(buf, 2);
}
static void st7796_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
st7796_send_command(0x2A);
st7796_send_data16(x0);
st7796_send_data16(x1);
st7796_send_command(0x2B);
st7796_send_data16(y0);
st7796_send_data16(y1);
st7796_send_command(0x2C);
}
void st7796_init(void)
{
fsp_err_t err = FSP_SUCCESS;
err = R_SPI_Open(&g_spi0_ctrl,&g_spi0_cfg);
assert(FSP_SUCCESS == err);
R_BSP_SoftwareDelay(500u, BSP_DELAY_UNITS_MILLISECONDS);
CS_SET;
DC_SET;
RST_SET;
BL_CLR;
RST_CLR;
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
RST_SET;
R_BSP_SoftwareDelay(120, BSP_DELAY_UNITS_MILLISECONDS);
st7796_send_command(0x11);
R_BSP_SoftwareDelay(120, BSP_DELAY_UNITS_MILLISECONDS);
st7796_send_command(0x36);
st7796_send_data((uint8_t[]){0x00}, 1);
st7796_send_command(0x3A);
st7796_send_data((uint8_t[]){0x55}, 1);
st7796_send_command(0xB2);
st7796_send_data((uint8_t[]){0x0C, 0x0C, 0x00, 0x33, 0x33}, 5);
st7796_send_command(0xB7);
st7796_send_data((uint8_t[]){0x35}, 1);
st7796_send_command(0xBB);
st7796_send_data((uint8_t[]){0x19}, 1);
st7796_send_command(0xC0);
st7796_send_data((uint8_t[]){0x2C}, 1);
st7796_send_command(0xC2);
st7796_send_data((uint8_t[]){0x01}, 1);
st7796_send_command(0xC3);
st7796_send_data((uint8_t[]){0x12}, 1);
st7796_send_command(0xC4);
st7796_send_data((uint8_t[]){0x20}, 1);
st7796_send_command(0xC6);
st7796_send_data((uint8_t[]){0x0F}, 1);
st7796_send_command(0xD0);
st7796_send_data((uint8_t[]){0xA4, 0xA1}, 2);
st7796_send_command(0xE0);
st7796_send_data((uint8_t[]){0xF0, 0x09, 0x0B, 0x06, 0x04, 0x15, 0x2F, 0x54,
0x42, 0x3C, 0x17, 0x14, 0x18, 0x1B}, 14);
st7796_send_command(0xE1);
st7796_send_data((uint8_t[]){0xE0, 0x09, 0x0B, 0x06, 0x04, 0x03, 0x2B, 0x43,
0x42, 0x3B, 0x16, 0x14, 0x17, 0x1B}, 14);
st7796_send_command(0x21);
st7796_send_command(0x29);
BL_SET;
st7796_fill_screen(BLACK);
}
void st7796_fill_screen(uint16_t color)
{
st7796_set_window(0, 0, ST7796_WIDTH - 1, ST7796_HEIGHT - 1);
DC_SET;
CS_CLR;
uint32_t pixel_count = ST7796_WIDTH * ST7796_HEIGHT;
uint8_t color_high = (color >> 8) & 0xFF;
uint8_t color_low = color & 0xFF;
for (uint32_t i = 0; i < pixel_count; i++)
{
R_SPI_Write(&g_spi0_ctrl, &color_high, 1, SPI_BIT_WIDTH_8_BITS);
SPIWaitgtc();
R_SPI_Write(&g_spi0_ctrl, &color_low, 1, SPI_BIT_WIDTH_8_BITS);
SPIWaitgtc();
}
CS_SET;
}
void st7796_draw_pixel(uint16_t x, uint16_t y, uint16_t color)
{
if (x >= ST7796_WIDTH || y >= ST7796_HEIGHT)
return;
st7796_set_window(x, y, x, y);
st7796_send_data16(color);
}
void st7796_draw_rectangle(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t color)
{
if (x >= ST7796_WIDTH || y >= ST7796_HEIGHT)
return;
uint16_t x1 = x + width - 1;
uint16_t y1 = y + height - 1;
if (x1 >= ST7796_WIDTH) x1 = ST7796_WIDTH - 1;
if (y1 >= ST7796_HEIGHT) y1 = ST7796_HEIGHT - 1;
st7796_set_window(x, y, x1, y1);
DC_SET;
CS_CLR;
uint32_t pixel_count = (x1 - x + 1) * (y1 - y + 1);
uint8_t color_high = (color >> 8) & 0xFF;
uint8_t color_low = color & 0xFF;
for (uint32_t i = 0; i < pixel_count; i++)
{
R_SPI_Write(&g_spi0_ctrl, &color_high, 1, SPI_BIT_WIDTH_8_BITS);
SPIWaitgtc();
R_SPI_Write(&g_spi0_ctrl, &color_low, 1, SPI_BIT_WIDTH_8_BITS);
SPIWaitgtc();
}
CS_SET;
}
void st7796_set_backlight(bool on)
{
if (on)
BL_SET;
else
BL_CLR;
}
8、在hal_enty中添加测试函数如下:
void hal_entry(void)
{
st7796_init();
st7796_draw_rectangle(0, 0, ST7796_WIDTH, ST7796_HEIGHT / 4, RED);
st7796_draw_rectangle(0, ST7796_HEIGHT / 4, ST7796_WIDTH, ST7796_HEIGHT / 4, GREEN);
st7796_draw_rectangle(0, ST7796_HEIGHT / 2, ST7796_WIDTH, ST7796_HEIGHT / 4, BLUE);
st7796_draw_rectangle(0, ST7796_HEIGHT * 3 / 4, ST7796_WIDTH, ST7796_HEIGHT / 4, YELLOW);
#if BSP_TZ_SECURE_BUILD
R_BSP_NonSecureEnter();
#endif
}
9、设置下载参数如下:

【实现效果】
