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
11 changed files with 4862 additions and 171 deletions

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)

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,17 +51,67 @@ const struct driver led_4 = {
.dev = &nrf_led_4
};
// LCD
// SPI 0
const struct spi nrf_spi_0 = {
.sck_pin = 2,
.mosi_pin = 3,
.miso_pin = 0xff,
.ss_pin = 25
.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

View File

@ -1,27 +1,21 @@
#ifndef __NRF52_SPI_H__
#define __NRF52_SPI_H__
#include <stdint.h>
#include "driver.h"
int spi_open(const struct driver *drv);
int spi_close(const struct driver *drv);
int spi_read(const struct driver *drv, char *buffer, unsigned int len);
int spi_write(const struct driver *drv, const char *buffer, unsigned int len);
struct spi {
uint8_t sck_pin;
uint8_t mosi_pin;
uint8_t miso_pin;
uint8_t ss_pin;
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 = spi_read,
.read = NULL,
.write = spi_write,
.ioctl = NULL
};

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 "nrf_gpio.h"
#include "nrf_delay.h"
@ -48,63 +8,29 @@
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#define SPI_INSTANCE 0 /**< SPI instance index. */
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */
static volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer. */
#include "board.h"
#include "driver.h"
#define TEST_STRING "Nordic"
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));
}
}
const char buf[] = "Test";
int main(void)
{
bsp_board_init(BSP_INIT_LEDS);
unsigned int cnt = 0;
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
spi_config.ss_pin = SPI_SS_PIN;
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));
drv_open(&led_1);
drv_open(&spi_0);
NRF_LOG_INFO("SPI example started.");
while (1)
{
// Reset rx buffer and transfer done flag
memset(m_rx_buf, 0, m_length);
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();
}
while(1) {
char c = (cnt++ % 2) + 0x30;
drv_write(&led_1, &c, 1);
drv_write(&spi_0, buf, sizeof(buf));
NRF_LOG_FLUSH();
bsp_board_led_invert(BSP_BOARD_LED_0);
nrf_delay_ms(200);
}
}

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

@ -1,86 +1,61 @@
#include <assert.h>
#include <stddef.h>
#include "nrf_drv_spi.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf.h"
#include "driver.h"
#include "spi.h"
#define SPI_INSTANCE 0
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);
static volatile bool spi_xfer_done;
struct spi_object {
uint8_t *rx_buf;
uint8_t *tx_buf;
uint32_t rx_len;
uint32_t tx_len;
};
uint8_t rx_buffer[1];
uint8_t tx_buffer[20];
static struct spi_object spi_obj = {
.rx_buf = rx_buffer,
.tx_buf = tx_buffer,
.rx_len = sizeof(rx_buffer),
.tx_len = sizeof(tx_buffer)
};
void spi_event_handler(nrf_drv_spi_evt_t const * p_event, void * p_context)
{
spi_xfer_done = true;
NRF_LOG_INFO("Transfer completed.");
}
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);
struct spi *this = (struct spi *)drv->dev;
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
spi_config.ss_pin = this->ss_pin;
spi_config.miso_pin = this->miso_pin;
spi_config.mosi_pin = this->mosi_pin;
spi_config.sck_pin = this->sck_pin;
APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
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)
{
assert(NULL != drv);
return 0;
}
int spi_read(const struct driver *drv, char *buffer, unsigned int len)
{
assert((NULL != drv) && (NULL != buffer));
NRF_LOG_INFO("spi read not implemented yet.");
NRF_SPI0->ENABLE = 0;
return 0;
}
int spi_write(const struct driver *drv, const char *buffer, unsigned int len)
{
if(len > spi_obj.tx_len) {
NRF_LOG_ERROR("buffer too small");
return 0;
}
memcpy(spi_obj.tx_buf, buffer, len);
assert(NULL != buffer);
// Reset transfer done flag
spi_xfer_done = false;
//FIXME: missing CS handling
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, spi_obj.tx_buf, spi_obj.tx_len, spi_obj.rx_buf, spi_obj.rx_len));
for(unsigned int i = 0; i < len; i++) {
spi_transfer(buffer[i]);
}
while(!spi_xfer_done) {
__WFE();
//FIXME: missing CS handling
return len;
}
return (int)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);
}