Compare commits

..

3 Commits

Author SHA1 Message Date
Thomas Klaehn
67a28afd24 Add st7789 lcd driver 2020-03-29 14:59:38 +02:00
Thomas Klaehn
7a5a5930aa Add application skeleton for st7789_lcd app 2020-03-29 14:51:51 +02:00
Thomas Klaehn
07301042f9 Add nrf52 spi driver abstraction 2020-03-29 14:51:13 +02:00
14 changed files with 9385 additions and 2 deletions

3
.vscode/tasks.json vendored
View File

@ -8,7 +8,8 @@
// "PLATFORM": "posix",
// "APPLICATION": "blinky",
// "APPLICATION": "button",
"APPLICATION": "spi",
// "APPLICATION": "spi",
"APPLICATION": "st7789_lcd",
},
},
"tasks": [

View File

@ -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)

View File

@ -0,0 +1,56 @@
CPU = cortex-m4
CROSS_COMPILE ?= arm-none-eabi-
NRF_PATH := nrf5sdk/
INCLUDES += $(NRF_PATH)components/
INCLUDES += $(NRF_PATH)modules/nrfx/mdk/
INCLUDES += $(NRF_PATH)components/libraries/strerror/
INCLUDES += $(NRF_PATH)components/softdevice/s132/headers/nrf52/
INCLUDES += $(NRF_PATH)components/toolchain/cmsis/include/
INCLUDES += $(NRF_PATH)components/libraries/util/
INCLUDES += $(NRF_PATH)components/libraries/balloc/
INCLUDES += $(NRF_PATH)components/libraries/ringbuf/
INCLUDES += $(NRF_PATH)modules/nrfx/hal/
INCLUDES += $(NRF_PATH)components/libraries/bsp/
INCLUDES += $(NRF_PATH)components/libraries/log/
INCLUDES += $(NRF_PATH)modules/nrfx/
INCLUDES += $(NRF_PATH)modules/nrfx/drivers/include/
INCLUDES += $(NRF_PATH)components/libraries/experimental_section_vars/
INCLUDES += $(NRF_PATH)components/softdevice/s132/headers/
INCLUDES += $(NRF_PATH)components/libraries/delay/
INCLUDES += $(NRF_PATH)integration/nrfx/
INCLUDES += $(NRF_PATH)integration/nrfx/legacy/
INCLUDES += $(NRF_PATH)components/libraries/atomic/
INCLUDES += $(NRF_PATH)components/boards/
INCLUDES += $(NRF_PATH)components/libraries/memobj/
INCLUDES += $(NRF_PATH)components/softdevice/common/
INCLUDES += $(NRF_PATH)external/fprintf/
INCLUDES += $(NRF_PATH)components/libraries/log/src/
NRF_C_SRCS += $(NRF_PATH)components/boards/boards.c
NRF_C_SRCS += $(NRF_PATH)modules/nrfx/mdk/system_nrf52.c
NRF_C_SRCS += $(NRF_PATH)components/libraries/util/app_error.c
NRF_C_SRCS += $(NRF_PATH)components/libraries/util/app_error_handler_gcc.c
NRF_C_SRCS += $(NRF_PATH)components/libraries/util/app_error_weak.c
NRF_C_SRCS += $(NRF_PATH)modules/nrfx/drivers/src/nrfx_spim.c
NRF_C_SRCS += $(NRF_PATH)components/libraries/log/src/nrf_log_frontend.c
NRF_C_SRCS += $(NRF_PATH)components/libraries/atomic/nrf_atomic.c
NRF_C_SRCS += $(NRF_PATH)components/libraries/log/src/nrf_log_default_backends.c
NRF_C_SRCS += $(NRF_PATH)components/libraries/log/src/nrf_log_backend_uart.c
NRF_C_SRCS += $(NRF_PATH)integration/nrfx/legacy/nrf_drv_uart.c
NRF_C_SRCS += $(NRF_PATH)integration/nrfx/legacy/nrf_drv_spi.c
NRF_C_SRCS += $(NRF_PATH)modules/nrfx/drivers/src/nrfx_uarte.c
NRF_C_SRCS += $(NRF_PATH)modules/nrfx/drivers/src/prs/nrfx_prs.c
NRF_C_SRCS += $(NRF_PATH)components/libraries/util/app_util_platform.c
NRF_C_SRCS += $(NRF_PATH)modules/nrfx/drivers/src/nrfx_uart.c
NRF_C_SRCS += $(NRF_PATH)components/libraries/log/src/nrf_log_backend_serial.c
NRF_C_SRCS += $(NRF_PATH)components/libraries/memobj/nrf_memobj.c
NRF_C_SRCS += $(NRF_PATH)components/libraries/balloc/nrf_balloc.c
NRF_C_SRCS += $(NRF_PATH)components/libraries/log/src/nrf_log_str_formatter.c
NRF_C_SRCS += $(NRF_PATH)external/fprintf/nrf_fprintf.c
NRF_C_SRCS += $(NRF_PATH)external/fprintf/nrf_fprintf_format.c
NRF_C_SRCS += $(NRF_PATH)components/libraries/ringbuf/nrf_ringbuf.c
NRF_C_SRCS += $(NRF_PATH)components/libraries/strerror/nrf_strerror.c
include config/build/$(PLATFORM)/Makefile.$(PLATFORM)

View File

@ -0,0 +1,64 @@
/* Linker script to configure memory regions. */
SEARCH_DIR(.)
GROUP(-lgcc -lc -lnosys)
MEMORY
{
FLASH (rx) : ORIGIN = 0x0, LENGTH = 0x80000
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x10000
}
SECTIONS
{
}
SECTIONS
{
. = ALIGN(4);
.mem_section_dummy_ram :
{
}
.log_dynamic_data :
{
PROVIDE(__start_log_dynamic_data = .);
KEEP(*(SORT(.log_dynamic_data*)))
PROVIDE(__stop_log_dynamic_data = .);
} > RAM
.log_filter_data :
{
PROVIDE(__start_log_filter_data = .);
KEEP(*(SORT(.log_filter_data*)))
PROVIDE(__stop_log_filter_data = .);
} > RAM
} INSERT AFTER .data;
SECTIONS
{
.mem_section_dummy_rom :
{
}
.log_const_data :
{
PROVIDE(__start_log_const_data = .);
KEEP(*(SORT(.log_const_data*)))
PROVIDE(__stop_log_const_data = .);
} > FLASH
.log_backends :
{
PROVIDE(__start_log_backends = .);
KEEP(*(SORT(.log_backends*)))
PROVIDE(__stop_log_backends = .);
} > FLASH
.nrf_balloc :
{
PROVIDE(__start_nrf_balloc = .);
KEEP(*(.nrf_balloc))
PROVIDE(__stop_nrf_balloc = .);
} > FLASH
} INSERT AFTER .text
INCLUDE "nrf_common.ld"

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

12
include/framebuffer.h Normal file
View 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

View File

@ -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

View File

@ -1,3 +1,6 @@
#ifndef __NRF52_SPI_H__
#define __NRF52_SPI_H__
int spi_open(const struct driver *drv);
int spi_close(const struct driver *drv);
@ -16,3 +19,5 @@ static const struct driver_fp spi_fp = {
.write = spi_write,
.ioctl = NULL
};
#endif

30
include/st7789.h Normal file
View 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

View File

@ -0,0 +1,33 @@
#include "app_util_platform.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"
int main(void)
{
unsigned int cnt = 0;
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
drv_open(&led_1);
drv_open(&lcd);
NRF_LOG_INFO("SPI example started.");
while(1) {
char c = (cnt++ % 2) + 0x30;
drv_write(&led_1, &c, 1);
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

@ -8,7 +8,6 @@
static inline int spi_transfer(uint32_t t);
int spi_open(const struct driver *drv)
{
assert(NULL != drv);

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