1
0
Fork 0

boards: Add an SPI and ST7789 driver

This code is somewhat hacky and it (currently) hard coded for PineTime
but it's brings the demo together nicely... so let's capture progress
here!
pull/1/head
Daniel Thompson 2020-01-25 19:28:16 +00:00
parent 660aa3247c
commit 0125b3e49b
2 changed files with 335 additions and 1 deletions

View File

@ -23,6 +23,7 @@
*/
#include "boards.h"
#include "nrf_spi.h"
#include "nrf_pwm.h"
#include "nrf_wdt.h"
#include "app_scheduler.h"
@ -40,6 +41,12 @@
void neopixel_teardown(void);
#endif
#ifdef ST7789_SPI_DISPLAY
void st7789_init(void);
void st7789_state(int state);
void st7789_teardown(void);
#endif
//------------- IMPLEMENTATION -------------//
void button_init(uint32_t pin)
{
@ -102,10 +109,19 @@ void board_init(void)
// Configure Systick for led blinky
NVIC_SetPriority(SysTick_IRQn, 7);
SysTick_Config(SystemCoreClock/1000);
// Bring up the display (if there is one)
#ifdef ST7789_SPI_DISPLAY
st7789_init();
#endif
}
void board_teardown(void)
{
#ifdef ST7789_SPI_DISPLAY
st7789_teardown();
#endif
// Disable systick, turn off LEDs
SysTick->CTRL = 0;
@ -116,7 +132,7 @@ void board_teardown(void)
#if defined(LED_NEOPIXEL) || defined(LED_RGB_RED_PIN)
neopixel_teardown();
#endif
// Button
// Stop RTC1 used by app_timer
@ -337,6 +353,7 @@ void led_state(uint32_t state)
#else // LEDS_NUMBER > 0
void led_state(uint32_t state)
{
st7789_state(state);
}
#endif
@ -481,6 +498,321 @@ void neopixel_write (uint8_t *pixels)
}
#endif
#ifdef ST7789_SPI_DISPLAY
#define SPIx NRF_SPI0
#define SPI_MODE NRF_SPI_MODE_3
#define SPI_SCK 2
#define SPI_MOSI 3
#define DISP_SS 25
#define DISP_DC 18
#define DISP_RESET 26
#define BACKLIGHT 14 /* lowest level */
void spi_init(void)
{
nrf_gpio_pin_write(SPI_SCK, SPI_MODE >= 2);
nrf_gpio_cfg(SPI_SCK, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_CONNECT,
NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE);
nrf_gpio_pin_clear(SPI_MOSI);
nrf_gpio_cfg_output(SPI_MOSI);
nrf_spi_pins_set(SPIx, SPI_SCK, SPI_MOSI, NRF_SPI_PIN_NOT_CONNECTED);
nrf_spi_frequency_set(SPIx, NRF_SPI_FREQ_8M);
nrf_spi_configure(SPIx, SPI_MODE, NRF_SPI_BIT_ORDER_MSB_FIRST);
nrf_spi_enable(SPIx);
}
void spi_teardown(void)
{
/* no need to tear down SCK and MOSI - output pins can be left alone */
nrf_spi_event_clear(SPIx, NRF_SPI_EVENT_READY);
nrf_spi_disable(SPIx);
nrf_gpio_cfg_default(SPI_MOSI);
nrf_gpio_cfg_default(SPI_SCK);
}
void spi_write(const uint8_t *data, unsigned len)
{
const uint8_t *endp = data + len;
/* paranoid... but worthwhile due to the havoc this could cause */
nrf_spi_event_clear(SPIx, NRF_SPI_EVENT_READY);
/* send first character */
nrf_spi_txd_set(SPIx, *data++);
/* TXD is double buffers so we can xmit and then poll for the event */
while (data < endp) {
nrf_spi_txd_set(SPIx, *data++);
while (!nrf_spi_event_check(SPIx, NRF_SPI_EVENT_READY)) {}
nrf_spi_event_clear(SPIx, NRF_SPI_EVENT_READY);
(void) nrf_spi_rxd_get(SPIx);
}
/* wait for the final character */
while (!nrf_spi_event_check(SPIx, NRF_SPI_EVENT_READY)) {}
nrf_spi_event_clear(SPIx, NRF_SPI_EVENT_READY);
(void) nrf_spi_rxd_get(SPIx);
}
#define NOP 0x00
#define SWRESET 0x01
#define SLPOUT 0x11
#define NORON 0x13
#define INVOFF 0x20
#define INVON 0x21
#define DISPON 0x29
#define CASET 0x2a
#define RASET 0x2b
#define RAMWR 0x2c
#define COLMOD 0x3a
#define MADCTL 0x36
struct st7789_cmd {
uint8_t cmd;
const uint8_t *data;
uint8_t len;
};
const static struct st7789_cmd st7789_init_data[] = {
{ COLMOD, (uint8_t *) "\x05", 1 }, // MCU will send 16-bit RGB565
{ MADCTL, (uint8_t *) "\x00", 1 }, // Left to right, top to bottom
//{ INVOFF, NULL }, // Results in odd palette
{ INVON, NULL },
{ NORON, NULL },
{ NOP, NULL },
};
static void st7789_send(uint8_t cmd, const uint8_t *data, unsigned len)
{
nrf_gpio_pin_clear(DISP_SS);
// this means nop cannot be sent... but helps with big RAMWR dumps
if (cmd) {
nrf_gpio_pin_clear(DISP_DC);
spi_write(&cmd, 1);
}
if (data) {
nrf_gpio_pin_set(DISP_DC);
spi_write(data, len);
}
nrf_gpio_pin_set(DISP_SS);
}
void st7789_state(int state)
{
bool topclip = true;
bool bottomclip = true;
switch(state) {
case STATE_BOOTLOADER_STARTED:
break;
case STATE_BLE_DISCONNECTED:
topclip = false;
/* fallthrough */
case STATE_BLE_CONNECTED:
bottomclip = false;
break;
default:
return;
}
uint8_t linebuffer[2*240];
// 1-bit RLE, generated from res/pinedfu.png, 1085 bytes
const uint8_t rle[] = {
0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0,
0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0,
0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0x14, 0x8, 0x8, 0x3,
0x2b, 0x3, 0xac, 0xd, 0x6, 0x3, 0x2b, 0x3, 0xab, 0xe, 0x6, 0x3,
0x2b, 0x3, 0xab, 0x4, 0x8, 0x2, 0x6, 0x3, 0x2b, 0x3, 0xaa, 0x4,
0x11, 0x3, 0x2b, 0x3, 0xaa, 0x3, 0x10, 0xb, 0x6, 0x7, 0xb, 0x3,
0x3, 0x4, 0x1, 0xb, 0xa4, 0x3, 0x10, 0xb, 0x4, 0xb, 0x9, 0x3,
0x1, 0x6, 0x1, 0xb, 0xa4, 0x3, 0x10, 0xb, 0x4, 0xc, 0x8, 0xa,
0x1, 0xb, 0xa4, 0x4, 0x11, 0x3, 0xa,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
st7789_send(CASET, (uint8_t *) "\x00\x00\x00\xef", 4);
st7789_send(RASET, (uint8_t *) "\x00\x00\x00\xef", 4);
st7789_send(RAMWR, NULL, 0);
int y = 0;
uint16_t bp = 0;
uint16_t fg = 0xffff;
const uint16_t bg = 0;
uint16_t color = bg;
for (int i=0; i<sizeof(rle); i++) {
uint8_t rl = rle[i];
while (rl) {
linebuffer[bp] = color >> 8;
linebuffer[bp+1] = color & 0xff;
bp += 2;
rl -= 1;
if (bp >= sizeof(linebuffer)) {
/* currently we have a single image which we clip to show different modes */
if (topclip) {
if (y < 48)
memset(linebuffer, 0, sizeof(linebuffer));
else
memset(linebuffer, 0, 64*2);
}
if (bottomclip) {
memset(linebuffer+2*(240-64), 0, 64*2);
} else if (y == 179 && state == STATE_BLE_CONNECTED) {
fg = 0x18ff;
}
st7789_send(NOP, linebuffer, sizeof(linebuffer));
bp = 0;
y += 1;
}
}
if (color == bg)
color = fg;
else
color = bg;
}
}
void st7789_init(void)
{
nrf_gpio_pin_set(DISP_SS);
nrf_gpio_cfg_output(DISP_SS);
spi_init();
nrf_gpio_cfg_output(DISP_DC);
/* deliver a reset */
nrf_gpio_pin_clear(DISP_RESET);
nrf_gpio_cfg_output(DISP_RESET);
NRFX_DELAY_MS(10);
nrf_gpio_pin_set(DISP_RESET);
NRFX_DELAY_MS(125);
/* initialize the display */
st7789_send(SLPOUT, NULL, 0);
NRFX_DELAY_MS(10);
for (const struct st7789_cmd *i = st7789_init_data; i->cmd != NOP; i++)
st7789_send(i->cmd, i->data, i->len);
/* draw the initial screen content. this will take ~116ms so, with the
* delay after the SLPOUT we will have complete the SLPOUT/SLPIN lock
* out period during the update.
*/
st7789_state(STATE_BOOTLOADER_STARTED);
/* enable the display and light the backlight */
st7789_send(DISPON, NULL, 0);
nrf_gpio_pin_clear(BACKLIGHT);
nrf_gpio_cfg_output(BACKLIGHT);
}
void st7789_teardown(void)
{
nrf_gpio_cfg_default(DISP_DC);
nrf_gpio_cfg_default(DISP_RESET);
nrf_gpio_cfg_default(BACKLIGHT);
spi_teardown();
nrf_gpio_cfg_default(DISP_SS);
}
#endif
void wdt_init(void)
{
// 1 => keep running during a sleep, stop during SWD debug

View File

@ -48,6 +48,8 @@
#define RTS_PIN_NUMBER 0
#define HWFC false
#define ST7789_SPI_DISPLAY
// Used as model string in OTA mode
#define BLEDIS_MANUFACTURER "Pine64"
#define BLEDIS_MODEL "PineTime"