Add st7789 lcd driver
This commit is contained in:
parent
7a5a5930aa
commit
67a28afd24
2
Makefile
2
Makefile
@ -8,6 +8,7 @@ TARGET_FILE ?= $(APPLICATION).elf
|
||||
CC = $(CROSS_COMPILE)gcc
|
||||
CPP = $(CROSS_COMPILE)cpp
|
||||
OBJCOPY = $(CROSS_COMPILE)objcopy
|
||||
SIZE = $(CROSS_COMPILE)size
|
||||
|
||||
SRC_DIR = src
|
||||
OBJ_DIR = obj/$(PLATFORM)
|
||||
@ -56,6 +57,7 @@ $(TARGET): $(OBJS) $(THIS_MAKEFILE)
|
||||
@mkdir -p $(dir $@)
|
||||
$(CC) $(CC_FLAGS) $(LD_FLAGS) $(OBJS) $(addprefix -l,$(LIBS)) -o $@
|
||||
ln -sf $(shell pwd)/$@ $(shell pwd)/bin/firmware.elf
|
||||
$(SIZE) -x $@
|
||||
|
||||
$(TARGET_HEX): $(TARGET) $(THIS_MAKEFILE)
|
||||
$(OBJCOPY) -O ihex $(TARGET) $(TARGET_HEX)
|
||||
|
4430
include/application/sdk_config.h
Normal file
4430
include/application/sdk_config.h
Normal file
File diff suppressed because it is too large
Load Diff
12
include/framebuffer.h
Normal file
12
include/framebuffer.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef __INCLUDE_FRAMEBUFFER_H__
|
||||
#define __INCLUDE_FRAMEBUFFER_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void fb_draw_pixel(uint16_t *image, uint16_t x, uint16_t y, uint16_t color);
|
||||
void fb_draw_line(uint16_t *image, int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
|
||||
|
||||
void fb_load_image(uint16_t *dst, uint16_t *src, uint32_t length);
|
||||
void fb_set_image(uint16_t *dst, uint16_t value, uint32_t length);
|
||||
|
||||
#endif
|
@ -5,6 +5,7 @@
|
||||
|
||||
#include "gpio.h"
|
||||
#include "spi.h"
|
||||
#include "st7789.h"
|
||||
|
||||
// LED 1
|
||||
const struct gpio nrf_led_1 = {
|
||||
@ -50,6 +51,8 @@ const struct driver led_4 = {
|
||||
.dev = &nrf_led_4
|
||||
};
|
||||
|
||||
// LCD
|
||||
// SPI 0
|
||||
const struct spi nrf_spi_0 = {
|
||||
.sck_pin = 2,
|
||||
.mosi_pin = 3,
|
||||
@ -60,5 +63,55 @@ const struct driver spi_0 = {
|
||||
.fp = &spi_fp,
|
||||
.dev = &nrf_spi_0
|
||||
};
|
||||
const struct gpio nrf_dc_pin = {
|
||||
.pin = 18,
|
||||
.dir = OUT
|
||||
};
|
||||
const struct driver dc_pin = {
|
||||
.name = "DC",
|
||||
.fp = &gpio_fp,
|
||||
.dev = &nrf_dc_pin
|
||||
};
|
||||
const struct gpio nrf_bl_pin = {
|
||||
.pin = 23,
|
||||
.dir = OUT
|
||||
};
|
||||
const struct driver bl_pin = {
|
||||
.name = "BACKLIGHT",
|
||||
.fp = &gpio_fp,
|
||||
.dev = &nrf_bl_pin
|
||||
};
|
||||
const struct gpio nrf_rst_pin = {
|
||||
.pin = 26,
|
||||
.dir = OUT
|
||||
};
|
||||
const struct driver rst_pin = {
|
||||
.name = "RESET",
|
||||
.fp = &gpio_fp,
|
||||
.dev = &nrf_rst_pin
|
||||
};
|
||||
const struct gpio nrf_select_pin = {
|
||||
.pin = 25,
|
||||
.dir = OUT
|
||||
};
|
||||
const struct driver select_pin = {
|
||||
.name = "SELECT",
|
||||
.fp = &gpio_fp,
|
||||
.dev = &nrf_select_pin
|
||||
};
|
||||
struct st7789 nrf_lcd = {
|
||||
.spi = &spi_0,
|
||||
.dc = &dc_pin,
|
||||
.bl = &bl_pin,
|
||||
.rst = &rst_pin,
|
||||
.select = &select_pin,
|
||||
.height = 240,
|
||||
.width = 240,
|
||||
};
|
||||
const struct driver lcd = {
|
||||
.name = "LCD",
|
||||
.fp = &st7789_fp,
|
||||
.dev = &nrf_lcd
|
||||
};
|
||||
|
||||
#endif
|
||||
|
30
include/st7789.h
Normal file
30
include/st7789.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef __ST7789_H__
|
||||
#define __ST7789_H__
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#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
|
@ -1,30 +1,33 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "app_util_platform.h"
|
||||
#include "board.h"
|
||||
#include "nrf_gpio.h"
|
||||
#include "nrf_delay.h"
|
||||
#include "boards.h"
|
||||
#include "app_error.h"
|
||||
#include <string.h>
|
||||
#include "nrf_log.h"
|
||||
#include "nrf_log_ctrl.h"
|
||||
#include "nrf_log_default_backends.h"
|
||||
|
||||
#include "board.h"
|
||||
#include "driver.h"
|
||||
#include "delay.h"
|
||||
|
||||
const char buffer[] = "Hello world!\r\n";
|
||||
int main(void)
|
||||
{
|
||||
unsigned int i = 0;
|
||||
unsigned int cnt = 0;
|
||||
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
|
||||
NRF_LOG_DEFAULT_BACKENDS_INIT();
|
||||
|
||||
drv_open(&led_1);
|
||||
drv_write(&led_1, "1", 1);
|
||||
drv_open(&spi_0);
|
||||
drv_open(&lcd);
|
||||
|
||||
// APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
|
||||
// NRF_LOG_DEFAULT_BACKENDS_INIT();
|
||||
|
||||
// NRF_LOG_INFO("SPI example started.");
|
||||
NRF_LOG_INFO("SPI example started.");
|
||||
|
||||
while(1) {
|
||||
drv_write(&spi_0, buffer, sizeof(buffer));
|
||||
// NRF_LOG_FLUSH();
|
||||
char c = (i++ % 2) + 0x30;
|
||||
char c = (cnt++ % 2) + 0x30;
|
||||
drv_write(&led_1, &c, 1);
|
||||
delay_ms(200);
|
||||
|
||||
NRF_LOG_FLUSH();
|
||||
|
||||
nrf_delay_ms(200);
|
||||
}
|
||||
}
|
||||
|
76
src/framebuffer.c
Normal file
76
src/framebuffer.c
Normal file
@ -0,0 +1,76 @@
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "framebuffer.h"
|
||||
|
||||
void fb_draw_pixel(uint16_t *image, uint16_t x, uint16_t y, uint16_t color)
|
||||
{
|
||||
assert(image != NULL);
|
||||
|
||||
image[x + 240 * y] = color;
|
||||
}
|
||||
|
||||
#ifndef _swap_int16_t
|
||||
#define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; }
|
||||
#endif
|
||||
|
||||
void fb_draw_line(uint16_t *image, int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
|
||||
{
|
||||
assert(image != NULL);
|
||||
|
||||
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
|
||||
if(steep) {
|
||||
_swap_int16_t(x0, y0);
|
||||
_swap_int16_t(x1, y1);
|
||||
}
|
||||
if(x0 > x1) {
|
||||
_swap_int16_t(x0, x1);
|
||||
_swap_int16_t(y0, y1);
|
||||
}
|
||||
|
||||
int16_t dx, dy;
|
||||
dx = x1 - x0;
|
||||
dy = abs(y1 - y0);
|
||||
|
||||
int16_t err = dx / 2;
|
||||
int16_t ystep;
|
||||
|
||||
if(y0 < y1) {
|
||||
ystep = 1;
|
||||
} else {
|
||||
ystep = -1;
|
||||
}
|
||||
|
||||
for(; x0 <= x1; x0++) {
|
||||
if(steep) {
|
||||
fb_draw_pixel(image, y0, x0, color);
|
||||
} else {
|
||||
fb_draw_pixel(image, x0, y0, color);
|
||||
}
|
||||
err -= dy;
|
||||
if (err < 0) {
|
||||
y0 += ystep;
|
||||
err += dx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void fb_load_image(uint16_t *dst, uint16_t *src, uint32_t length)
|
||||
{
|
||||
assert(NULL != dst);
|
||||
assert(NULL != src);
|
||||
|
||||
for(uint32_t i = 0; i < length; i++) {
|
||||
dst[i] = src[i];
|
||||
}
|
||||
}
|
||||
|
||||
void fb_set_image(uint16_t *dst, uint16_t value, uint32_t length)
|
||||
{
|
||||
assert(NULL != dst);
|
||||
|
||||
for (uint32_t i = 0; i < length; i++) {
|
||||
dst[i] = value;
|
||||
}
|
||||
}
|
@ -58,3 +58,4 @@ static inline int spi_transfer(uint32_t t)
|
||||
}
|
||||
r = NRF_SPI0->RXD; // in
|
||||
return (int)r;
|
||||
}
|
||||
|
192
src/st7789.c
Normal file
192
src/st7789.c
Normal file
@ -0,0 +1,192 @@
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user