Add st7789 lcd driver

This commit is contained in:
Thomas Klaehn
2020-03-29 10:25:17 +02:00
parent 7a5a5930aa
commit 67a28afd24
9 changed files with 4815 additions and 16 deletions

View File

@@ -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
View 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;
}
}

View File

@@ -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
View 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);
}