Compare commits

...

4 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
Thomas Klaehn
437ba90bc2 Add nrf spi driver 2020-03-29 12:12:42 +02:00
15 changed files with 9486 additions and 85 deletions

3
.vscode/tasks.json vendored
View File

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

View File

@ -8,6 +8,7 @@ TARGET_FILE ?= $(APPLICATION).elf
CC = $(CROSS_COMPILE)gcc CC = $(CROSS_COMPILE)gcc
CPP = $(CROSS_COMPILE)cpp CPP = $(CROSS_COMPILE)cpp
OBJCOPY = $(CROSS_COMPILE)objcopy OBJCOPY = $(CROSS_COMPILE)objcopy
SIZE = $(CROSS_COMPILE)size
SRC_DIR = src SRC_DIR = src
OBJ_DIR = obj/$(PLATFORM) OBJ_DIR = obj/$(PLATFORM)
@ -56,6 +57,7 @@ $(TARGET): $(OBJS) $(THIS_MAKEFILE)
@mkdir -p $(dir $@) @mkdir -p $(dir $@)
$(CC) $(CC_FLAGS) $(LD_FLAGS) $(OBJS) $(addprefix -l,$(LIBS)) -o $@ $(CC) $(CC_FLAGS) $(LD_FLAGS) $(OBJS) $(addprefix -l,$(LIBS)) -o $@
ln -sf $(shell pwd)/$@ $(shell pwd)/bin/firmware.elf ln -sf $(shell pwd)/$@ $(shell pwd)/bin/firmware.elf
$(SIZE) -x $@
$(TARGET_HEX): $(TARGET) $(THIS_MAKEFILE) $(TARGET_HEX): $(TARGET) $(THIS_MAKEFILE)
$(OBJCOPY) -O ihex $(TARGET) $(TARGET_HEX) $(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

@ -4,6 +4,8 @@
#include "driver.h" #include "driver.h"
#include "gpio.h" #include "gpio.h"
#include "spi.h"
#include "st7789.h"
// LED 1 // LED 1
const struct gpio nrf_led_1 = { const struct gpio nrf_led_1 = {
@ -49,4 +51,67 @@ const struct driver led_4 = {
.dev = &nrf_led_4 .dev = &nrf_led_4
}; };
// LCD
// SPI 0
const struct spi nrf_spi_0 = {
.sck_pin = 2,
.mosi_pin = 3,
.miso_pin = 4
};
const struct driver spi_0 = {
.name = "SPI0",
.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 #endif

23
include/spi.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef __NRF52_SPI_H__
#define __NRF52_SPI_H__
int spi_open(const struct driver *drv);
int spi_close(const struct driver *drv);
int spi_write(const struct driver *drv, const char *buffer, unsigned int len);
struct spi {
unsigned int sck_pin;
unsigned int mosi_pin;
unsigned int miso_pin;
};
static const struct driver_fp spi_fp = {
.open = spi_open,
.close = spi_close,
.read = NULL,
.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

@ -1,43 +1,3 @@
/**
* Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "nrf_drv_spi.h"
#include "app_util_platform.h" #include "app_util_platform.h"
#include "nrf_gpio.h" #include "nrf_gpio.h"
#include "nrf_delay.h" #include "nrf_delay.h"
@ -48,63 +8,29 @@
#include "nrf_log_ctrl.h" #include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h" #include "nrf_log_default_backends.h"
#define SPI_INSTANCE 0 /**< SPI instance index. */ #include "board.h"
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */ #include "driver.h"
static volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer. */
#define TEST_STRING "Nordic" const char buf[] = "Test";
static uint8_t m_tx_buf[] = TEST_STRING; /**< TX buffer. */
static uint8_t m_rx_buf[sizeof(TEST_STRING) + 1]; /**< RX buffer. */
static const uint8_t m_length = sizeof(m_tx_buf); /**< Transfer length. */
/**
* @brief SPI user event handler.
* @param event
*/
void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
void * p_context)
{
spi_xfer_done = true;
NRF_LOG_INFO("Transfer completed.");
if (m_rx_buf[0] != 0)
{
NRF_LOG_INFO(" Received:");
NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
}
}
int main(void) int main(void)
{ {
bsp_board_init(BSP_INIT_LEDS); unsigned int cnt = 0;
APP_ERROR_CHECK(NRF_LOG_INIT(NULL)); APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT(); NRF_LOG_DEFAULT_BACKENDS_INIT();
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG; drv_open(&led_1);
spi_config.ss_pin = SPI_SS_PIN; drv_open(&spi_0);
spi_config.miso_pin = SPI_MISO_PIN;
spi_config.mosi_pin = SPI_MOSI_PIN;
spi_config.sck_pin = SPI_SCK_PIN;
APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
NRF_LOG_INFO("SPI example started."); NRF_LOG_INFO("SPI example started.");
while (1) while(1) {
{ char c = (cnt++ % 2) + 0x30;
// Reset rx buffer and transfer done flag drv_write(&led_1, &c, 1);
memset(m_rx_buf, 0, m_length); drv_write(&spi_0, buf, sizeof(buf));
spi_xfer_done = false;
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
while (!spi_xfer_done)
{
__WFE();
}
NRF_LOG_FLUSH(); NRF_LOG_FLUSH();
bsp_board_led_invert(BSP_BOARD_LED_0);
nrf_delay_ms(200); nrf_delay_ms(200);
} }
} }

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

61
src/platform/nrf52/spi.c Normal file
View File

@ -0,0 +1,61 @@
#include <assert.h>
#include <stddef.h>
#include "nrf.h"
#include "driver.h"
#include "spi.h"
static inline int spi_transfer(uint32_t t);
int spi_open(const struct driver *drv)
{
assert(NULL != drv);
struct spi *this = (struct spi *)drv->dev;
NRF_SPI0->ENABLE = 0;
NRF_SPI0->PSELSCK = this->sck_pin;
NRF_SPI0->PSELMOSI = this->mosi_pin;
NRF_SPI0->PSELMISO = this->miso_pin;
NRF_SPI0->FREQUENCY = SPI_FREQUENCY_FREQUENCY_M8;
NRF_SPI0->CONFIG = (0x03 << 1); //Sample on trailing edge of clock, shift serial data on leading edge, SCK polarity Active low
NRF_SPI0->EVENTS_READY = 0;
NRF_SPI0->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos);
return 0;
}
int spi_close(const struct driver *drv)
{
NRF_SPI0->ENABLE = 0;
return 0;
}
int spi_write(const struct driver *drv, const char *buffer, unsigned int len)
{
assert(NULL != buffer);
//FIXME: missing CS handling
for(unsigned int i = 0; i < len; i++) {
spi_transfer(buffer[i]);
}
//FIXME: missing CS handling
return len;
}
static inline int spi_transfer(uint32_t t)
{
volatile uint32_t r;
NRF_SPI0->EVENTS_READY = 0; // ready
NRF_SPI0->TXD = t; // out
while(NRF_SPI0->EVENTS_READY == 0) {
}
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);
}