2017-01-06 01:20:32 -05:00
|
|
|
/* SPI Master example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "driver/spi_master.h"
|
|
|
|
#include "soc/gpio_struct.h"
|
|
|
|
#include "driver/gpio.h"
|
|
|
|
|
2018-03-08 23:40:12 -05:00
|
|
|
#include "pretty_effect.h"
|
2017-01-06 01:20:32 -05:00
|
|
|
|
|
|
|
/*
|
2017-07-18 05:02:07 -04:00
|
|
|
This code displays some fancy graphics on the 320x240 LCD on an ESP-WROVER_KIT board.
|
2018-04-18 02:36:26 -04:00
|
|
|
This example demonstrates the use of both spi_device_transmit as well as
|
2018-03-08 23:40:12 -05:00
|
|
|
spi_device_queue_trans/spi_device_get_trans_result and pre-transmit callbacks.
|
2017-01-06 01:20:32 -05:00
|
|
|
|
2017-07-18 05:02:07 -04:00
|
|
|
Some info about the ILI9341/ST7789V: It has an C/D line, which is connected to a GPIO here. It expects this
|
2017-01-06 01:20:32 -05:00
|
|
|
line to be low for a command and high for data. We use a pre-transmit callback here to control that
|
|
|
|
line: every transaction has as the user-definable argument the needed state of the D/C line and just
|
|
|
|
before the transaction is sent, the callback will set this line to the correct state.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define PIN_NUM_MISO 25
|
|
|
|
#define PIN_NUM_MOSI 23
|
|
|
|
#define PIN_NUM_CLK 19
|
|
|
|
#define PIN_NUM_CS 22
|
|
|
|
|
|
|
|
#define PIN_NUM_DC 21
|
|
|
|
#define PIN_NUM_RST 18
|
|
|
|
#define PIN_NUM_BCKL 5
|
|
|
|
|
2018-04-18 02:36:26 -04:00
|
|
|
//To speed up transfers, every SPI transfer sends a bunch of lines. This define specifies how many. More means more memory use,
|
2018-03-08 23:40:12 -05:00
|
|
|
//but less overhead for setting up / finishing transfers. Make sure 240 is dividable by this.
|
|
|
|
#define PARALLEL_LINES 16
|
2017-01-06 01:20:32 -05:00
|
|
|
|
|
|
|
/*
|
2017-07-18 05:02:07 -04:00
|
|
|
The LCD needs a bunch of command/argument values to be initialized. They are stored in this struct.
|
2017-01-06 01:20:32 -05:00
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
uint8_t cmd;
|
|
|
|
uint8_t data[16];
|
|
|
|
uint8_t databytes; //No of data in data; bit 7 = delay after set; 0xFF = end of cmds.
|
2017-07-18 05:02:07 -04:00
|
|
|
} lcd_init_cmd_t;
|
2017-01-06 01:20:32 -05:00
|
|
|
|
2017-08-31 03:03:19 -04:00
|
|
|
typedef enum {
|
|
|
|
LCD_TYPE_ILI = 1,
|
|
|
|
LCD_TYPE_ST,
|
|
|
|
LCD_TYPE_MAX,
|
|
|
|
} type_lcd_t;
|
2017-07-18 05:02:07 -04:00
|
|
|
|
2017-08-31 03:03:19 -04:00
|
|
|
//Place data into DRAM. Constant data gets placed into DROM by default, which is not accessible by DMA.
|
|
|
|
DRAM_ATTR static const lcd_init_cmd_t st_init_cmds[]={
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Memory Data Access Control, MX=MV=1, MY=ML=MH=0, RGB=0 */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0x36, {(1<<5)|(1<<6)}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Interface Pixel Format, 16bits/pixel for RGB/MCU interface */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0x3A, {0x55}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Porch Setting */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0xB2, {0x0c, 0x0c, 0x00, 0x33, 0x33}, 5},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Gate Control, Vgh=13.65V, Vgl=-10.43V */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0xB7, {0x45}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* VCOM Setting, VCOM=1.175V */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0xBB, {0x2B}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* LCM Control, XOR: BGR, MX, MH */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0xC0, {0x2C}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* VDV and VRH Command Enable, enable=1 */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0xC2, {0x01, 0xff}, 2},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* VRH Set, Vap=4.4+... */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0xC3, {0x11}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* VDV Set, VDV=0 */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0xC4, {0x20}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Frame Rate Control, 60Hz, inversion=0 */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0xC6, {0x0f}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Power Control 1, AVDD=6.8V, AVCL=-4.8V, VDDS=2.3V */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0xD0, {0xA4, 0xA1}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Positive Voltage Gamma Control */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0xE0, {0xD0, 0x00, 0x05, 0x0E, 0x15, 0x0D, 0x37, 0x43, 0x47, 0x09, 0x15, 0x12, 0x16, 0x19}, 14},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Negative Voltage Gamma Control */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0xE1, {0xD0, 0x00, 0x05, 0x0D, 0x0C, 0x06, 0x2D, 0x44, 0x40, 0x0E, 0x1C, 0x18, 0x16, 0x19}, 14},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Sleep Out */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0x11, {0}, 0x80},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Display On */
|
2017-07-18 05:02:07 -04:00
|
|
|
{0x29, {0}, 0x80},
|
|
|
|
{0, {0}, 0xff}
|
|
|
|
};
|
|
|
|
|
2017-08-31 03:03:19 -04:00
|
|
|
DRAM_ATTR static const lcd_init_cmd_t ili_init_cmds[]={
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Power contorl B, power control = 0, DC_ENA = 1 */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xCF, {0x00, 0x83, 0X30}, 3},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Power on sequence control,
|
|
|
|
* cp1 keeps 1 frame, 1st frame enable
|
|
|
|
* vcl = 0, ddvdh=3, vgh=1, vgl=2
|
|
|
|
* DDVDH_ENH=1
|
|
|
|
*/
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xED, {0x64, 0x03, 0X12, 0X81}, 4},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Driver timing control A,
|
|
|
|
* non-overlap=default +1
|
|
|
|
* EQ=default - 1, CR=default
|
|
|
|
* pre-charge=default - 1
|
|
|
|
*/
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xE8, {0x85, 0x01, 0x79}, 3},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Power control A, Vcore=1.6V, DDVDH=5.6V */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xCB, {0x39, 0x2C, 0x00, 0x34, 0x02}, 5},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Pump ratio control, DDVDH=2xVCl */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xF7, {0x20}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Driver timing control, all=0 unit */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xEA, {0x00, 0x00}, 2},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Power control 1, GVDD=4.75V */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xC0, {0x26}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Power control 2, DDVDH=VCl*2, VGH=VCl*7, VGL=-VCl*3 */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xC1, {0x11}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* VCOM control 1, VCOMH=4.025V, VCOML=-0.950V */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xC5, {0x35, 0x3E}, 2},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* VCOM control 2, VCOMH=VMH-2, VCOML=VML-2 */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xC7, {0xBE}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Memory access contorl, MX=MY=0, MV=1, ML=0, BGR=1, MH=0 */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0x36, {0x28}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Pixel format, 16bits/pixel for RGB/MCU interface */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0x3A, {0x55}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Frame rate control, f=fosc, 70Hz fps */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xB1, {0x00, 0x1B}, 2},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Enable 3G, disabled */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xF2, {0x08}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Gamma set, curve 1 */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0x26, {0x01}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Positive gamma correction */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xE0, {0x1F, 0x1A, 0x18, 0x0A, 0x0F, 0x06, 0x45, 0X87, 0x32, 0x0A, 0x07, 0x02, 0x07, 0x05, 0x00}, 15},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Negative gamma correction */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0XE1, {0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3A, 0x78, 0x4D, 0x05, 0x18, 0x0D, 0x38, 0x3A, 0x1F}, 15},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Column address set, SC=0, EC=0xEF */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0x2A, {0x00, 0x00, 0x00, 0xEF}, 4},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Page address set, SP=0, EP=0x013F */
|
|
|
|
{0x2B, {0x00, 0x00, 0x01, 0x3f}, 4},
|
|
|
|
/* Memory write */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0x2C, {0}, 0},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Entry mode set, Low vol detect disabled, normal display */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xB7, {0x07}, 1},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Display function control */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0xB6, {0x0A, 0x82, 0x27, 0x00}, 4},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Sleep out */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0x11, {0}, 0x80},
|
2018-04-18 02:36:26 -04:00
|
|
|
/* Display on */
|
2017-01-06 01:20:32 -05:00
|
|
|
{0x29, {0}, 0x80},
|
|
|
|
{0, {0}, 0xff},
|
|
|
|
};
|
|
|
|
|
2017-07-18 05:02:07 -04:00
|
|
|
//Send a command to the LCD. Uses spi_device_transmit, which waits until the transfer is complete.
|
2018-04-18 02:36:26 -04:00
|
|
|
void lcd_cmd(spi_device_handle_t spi, const uint8_t cmd)
|
2017-01-06 01:20:32 -05:00
|
|
|
{
|
|
|
|
esp_err_t ret;
|
|
|
|
spi_transaction_t t;
|
|
|
|
memset(&t, 0, sizeof(t)); //Zero out the transaction
|
|
|
|
t.length=8; //Command is 8 bits
|
|
|
|
t.tx_buffer=&cmd; //The data is the cmd itself
|
|
|
|
t.user=(void*)0; //D/C needs to be set to 0
|
|
|
|
ret=spi_device_transmit(spi, &t); //Transmit!
|
|
|
|
assert(ret==ESP_OK); //Should have had no issues.
|
|
|
|
}
|
|
|
|
|
2017-07-18 05:02:07 -04:00
|
|
|
//Send data to the LCD. Uses spi_device_transmit, which waits until the transfer is complete.
|
2018-04-18 02:36:26 -04:00
|
|
|
void lcd_data(spi_device_handle_t spi, const uint8_t *data, int len)
|
2017-01-06 01:20:32 -05:00
|
|
|
{
|
|
|
|
esp_err_t ret;
|
|
|
|
spi_transaction_t t;
|
|
|
|
if (len==0) return; //no need to send anything
|
|
|
|
memset(&t, 0, sizeof(t)); //Zero out the transaction
|
|
|
|
t.length=len*8; //Len is in bytes, transaction length is in bits.
|
|
|
|
t.tx_buffer=data; //Data
|
|
|
|
t.user=(void*)1; //D/C needs to be set to 1
|
|
|
|
ret=spi_device_transmit(spi, &t); //Transmit!
|
|
|
|
assert(ret==ESP_OK); //Should have had no issues.
|
|
|
|
}
|
|
|
|
|
|
|
|
//This function is called (in irq context!) just before a transmission starts. It will
|
|
|
|
//set the D/C line to the value indicated in the user field.
|
2018-04-18 02:36:26 -04:00
|
|
|
void lcd_spi_pre_transfer_callback(spi_transaction_t *t)
|
2017-01-06 01:20:32 -05:00
|
|
|
{
|
|
|
|
int dc=(int)t->user;
|
|
|
|
gpio_set_level(PIN_NUM_DC, dc);
|
|
|
|
}
|
|
|
|
|
2018-04-18 02:36:26 -04:00
|
|
|
uint32_t lcd_get_id(spi_device_handle_t spi)
|
2017-08-31 03:03:19 -04:00
|
|
|
{
|
|
|
|
//get_id cmd
|
|
|
|
lcd_cmd( spi, 0x04);
|
|
|
|
|
|
|
|
spi_transaction_t t;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
|
|
t.length=8*3;
|
|
|
|
t.flags = SPI_TRANS_USE_RXDATA;
|
|
|
|
t.user = (void*)1;
|
|
|
|
|
|
|
|
esp_err_t ret = spi_device_transmit(spi, &t);
|
|
|
|
assert( ret == ESP_OK );
|
|
|
|
|
|
|
|
return *(uint32_t*)t.rx_data;
|
|
|
|
}
|
|
|
|
|
2017-01-06 01:20:32 -05:00
|
|
|
//Initialize the display
|
2018-04-18 02:36:26 -04:00
|
|
|
void lcd_init(spi_device_handle_t spi)
|
2017-01-06 01:20:32 -05:00
|
|
|
{
|
|
|
|
int cmd=0;
|
2017-08-31 03:03:19 -04:00
|
|
|
const lcd_init_cmd_t* lcd_init_cmds;
|
|
|
|
|
2017-01-06 01:20:32 -05:00
|
|
|
//Initialize non-SPI GPIOs
|
|
|
|
gpio_set_direction(PIN_NUM_DC, GPIO_MODE_OUTPUT);
|
|
|
|
gpio_set_direction(PIN_NUM_RST, GPIO_MODE_OUTPUT);
|
|
|
|
gpio_set_direction(PIN_NUM_BCKL, GPIO_MODE_OUTPUT);
|
|
|
|
|
|
|
|
//Reset the display
|
|
|
|
gpio_set_level(PIN_NUM_RST, 0);
|
|
|
|
vTaskDelay(100 / portTICK_RATE_MS);
|
|
|
|
gpio_set_level(PIN_NUM_RST, 1);
|
|
|
|
vTaskDelay(100 / portTICK_RATE_MS);
|
|
|
|
|
2017-08-31 03:03:19 -04:00
|
|
|
//detect LCD type
|
|
|
|
uint32_t lcd_id = lcd_get_id(spi);
|
|
|
|
int lcd_detected_type = 0;
|
|
|
|
int lcd_type;
|
|
|
|
|
|
|
|
printf("LCD ID: %08X\n", lcd_id);
|
|
|
|
if ( lcd_id == 0 ) {
|
|
|
|
//zero, ili
|
|
|
|
lcd_detected_type = LCD_TYPE_ILI;
|
2018-03-08 23:40:12 -05:00
|
|
|
printf("ILI9341 detected.\n");
|
2017-08-31 03:03:19 -04:00
|
|
|
} else {
|
|
|
|
// none-zero, ST
|
|
|
|
lcd_detected_type = LCD_TYPE_ST;
|
2018-03-08 23:40:12 -05:00
|
|
|
printf("ST7789V detected.\n");
|
2017-08-31 03:03:19 -04:00
|
|
|
}
|
2017-07-18 05:02:07 -04:00
|
|
|
|
2017-08-31 03:03:19 -04:00
|
|
|
#ifdef CONFIG_LCD_TYPE_AUTO
|
|
|
|
lcd_type = lcd_detected_type;
|
|
|
|
#elif defined( CONFIG_LCD_TYPE_ST7789V )
|
|
|
|
printf("kconfig: force CONFIG_LCD_TYPE_ST7789V.\n");
|
|
|
|
lcd_type = LCD_TYPE_ST;
|
|
|
|
#elif defined( CONFIG_LCD_TYPE_ILI9341 )
|
|
|
|
printf("kconfig: force CONFIG_LCD_TYPE_ILI9341.\n");
|
|
|
|
lcd_type = LCD_TYPE_ILI;
|
2018-03-08 23:40:12 -05:00
|
|
|
#endif
|
2017-08-31 03:03:19 -04:00
|
|
|
if ( lcd_type == LCD_TYPE_ST ) {
|
2017-07-18 05:02:07 -04:00
|
|
|
printf("LCD ST7789V initialization.\n");
|
2017-08-31 03:03:19 -04:00
|
|
|
lcd_init_cmds = st_init_cmds;
|
|
|
|
} else {
|
2018-03-08 23:40:12 -05:00
|
|
|
printf("LCD ILI9341 initialization.\n");
|
2017-08-31 03:03:19 -04:00
|
|
|
lcd_init_cmds = ili_init_cmds;
|
|
|
|
}
|
2017-07-18 05:02:07 -04:00
|
|
|
|
2017-08-31 03:03:19 -04:00
|
|
|
//Send all the commands
|
2017-07-18 05:02:07 -04:00
|
|
|
while (lcd_init_cmds[cmd].databytes!=0xff) {
|
|
|
|
lcd_cmd(spi, lcd_init_cmds[cmd].cmd);
|
|
|
|
lcd_data(spi, lcd_init_cmds[cmd].data, lcd_init_cmds[cmd].databytes&0x1F);
|
|
|
|
if (lcd_init_cmds[cmd].databytes&0x80) {
|
2017-01-06 01:20:32 -05:00
|
|
|
vTaskDelay(100 / portTICK_RATE_MS);
|
|
|
|
}
|
|
|
|
cmd++;
|
|
|
|
}
|
|
|
|
|
|
|
|
///Enable backlight
|
|
|
|
gpio_set_level(PIN_NUM_BCKL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-08 23:40:12 -05:00
|
|
|
//To send a set of lines we have to send a command, 2 data bytes, another command, 2 more data bytes and another command
|
2017-01-06 01:20:32 -05:00
|
|
|
//before sending the line data itself; a total of 6 transactions. (We can't put all of this in just one transaction
|
|
|
|
//because the D/C line needs to be toggled in the middle.)
|
|
|
|
//This routine queues these commands up so they get sent as quickly as possible.
|
2018-04-18 02:36:26 -04:00
|
|
|
static void send_lines(spi_device_handle_t spi, int ypos, uint16_t *linedata)
|
2017-01-06 01:20:32 -05:00
|
|
|
{
|
|
|
|
esp_err_t ret;
|
|
|
|
int x;
|
|
|
|
//Transaction descriptors. Declared static so they're not allocated on the stack; we need this memory even when this
|
|
|
|
//function is finished because the SPI driver needs access to it even while we're already calculating the next line.
|
|
|
|
static spi_transaction_t trans[6];
|
|
|
|
|
|
|
|
//In theory, it's better to initialize trans and data only once and hang on to the initialized
|
|
|
|
//variables. We allocate them on the stack, so we need to re-init them each call.
|
|
|
|
for (x=0; x<6; x++) {
|
|
|
|
memset(&trans[x], 0, sizeof(spi_transaction_t));
|
|
|
|
if ((x&1)==0) {
|
|
|
|
//Even transfers are commands
|
|
|
|
trans[x].length=8;
|
|
|
|
trans[x].user=(void*)0;
|
|
|
|
} else {
|
|
|
|
//Odd transfers are data
|
|
|
|
trans[x].length=8*4;
|
|
|
|
trans[x].user=(void*)1;
|
|
|
|
}
|
2017-01-10 01:41:12 -05:00
|
|
|
trans[x].flags=SPI_TRANS_USE_TXDATA;
|
2017-01-06 01:20:32 -05:00
|
|
|
}
|
|
|
|
trans[0].tx_data[0]=0x2A; //Column Address Set
|
|
|
|
trans[1].tx_data[0]=0; //Start Col High
|
|
|
|
trans[1].tx_data[1]=0; //Start Col Low
|
|
|
|
trans[1].tx_data[2]=(320)>>8; //End Col High
|
|
|
|
trans[1].tx_data[3]=(320)&0xff; //End Col Low
|
|
|
|
trans[2].tx_data[0]=0x2B; //Page address set
|
|
|
|
trans[3].tx_data[0]=ypos>>8; //Start page high
|
|
|
|
trans[3].tx_data[1]=ypos&0xff; //start page low
|
2018-03-08 23:40:12 -05:00
|
|
|
trans[3].tx_data[2]=(ypos+PARALLEL_LINES)>>8; //end page high
|
|
|
|
trans[3].tx_data[3]=(ypos+PARALLEL_LINES)&0xff; //end page low
|
2017-01-06 01:20:32 -05:00
|
|
|
trans[4].tx_data[0]=0x2C; //memory write
|
2018-03-08 23:40:12 -05:00
|
|
|
trans[5].tx_buffer=linedata; //finally send the line data
|
|
|
|
trans[5].length=320*2*8*PARALLEL_LINES; //Data length, in bits
|
2017-01-10 01:41:12 -05:00
|
|
|
trans[5].flags=0; //undo SPI_TRANS_USE_TXDATA flag
|
2017-01-06 01:20:32 -05:00
|
|
|
|
|
|
|
//Queue all transactions.
|
|
|
|
for (x=0; x<6; x++) {
|
|
|
|
ret=spi_device_queue_trans(spi, &trans[x], portMAX_DELAY);
|
|
|
|
assert(ret==ESP_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
//When we are here, the SPI driver is busy (in the background) getting the transactions sent. That happens
|
|
|
|
//mostly using DMA, so the CPU doesn't have much to do here. We're not going to wait for the transaction to
|
|
|
|
//finish because we may as well spend the time calculating the next line. When that is done, we can call
|
|
|
|
//send_line_finish, which will wait for the transfers to be done and check their status.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-18 02:36:26 -04:00
|
|
|
static void send_line_finish(spi_device_handle_t spi)
|
2017-01-06 01:20:32 -05:00
|
|
|
{
|
|
|
|
spi_transaction_t *rtrans;
|
|
|
|
esp_err_t ret;
|
|
|
|
//Wait for all 6 transactions to be done and get back the results.
|
|
|
|
for (int x=0; x<6; x++) {
|
|
|
|
ret=spi_device_get_trans_result(spi, &rtrans, portMAX_DELAY);
|
|
|
|
assert(ret==ESP_OK);
|
|
|
|
//We could inspect rtrans now if we received any info back. The LCD is treated as write-only, though.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Simple routine to generate some patterns and send them to the LCD. Don't expect anything too
|
2018-04-18 02:36:26 -04:00
|
|
|
//impressive. Because the SPI driver handles transactions in the background, we can calculate the next line
|
2017-01-06 01:20:32 -05:00
|
|
|
//while the previous one is being sent.
|
2018-04-18 02:36:26 -04:00
|
|
|
static void display_pretty_colors(spi_device_handle_t spi)
|
2017-01-06 01:20:32 -05:00
|
|
|
{
|
2018-03-08 23:40:12 -05:00
|
|
|
uint16_t *lines[2];
|
|
|
|
//Allocate memory for the pixel buffers
|
|
|
|
for (int i=0; i<2; i++) {
|
|
|
|
lines[i]=heap_caps_malloc(320*PARALLEL_LINES*sizeof(uint16_t), MALLOC_CAP_DMA);
|
|
|
|
assert(lines[i]!=NULL);
|
|
|
|
}
|
|
|
|
int frame=0;
|
2017-01-06 01:20:32 -05:00
|
|
|
//Indexes of the line currently being sent to the LCD and the line we're calculating.
|
|
|
|
int sending_line=-1;
|
|
|
|
int calc_line=0;
|
2018-04-18 02:36:26 -04:00
|
|
|
|
2017-01-06 01:20:32 -05:00
|
|
|
while(1) {
|
|
|
|
frame++;
|
2018-03-08 23:40:12 -05:00
|
|
|
for (int y=0; y<240; y+=PARALLEL_LINES) {
|
2017-01-06 01:20:32 -05:00
|
|
|
//Calculate a line.
|
2018-03-08 23:40:12 -05:00
|
|
|
pretty_effect_calc_lines(lines[calc_line], y, frame, PARALLEL_LINES);
|
2017-01-06 01:20:32 -05:00
|
|
|
//Finish up the sending process of the previous line, if any
|
|
|
|
if (sending_line!=-1) send_line_finish(spi);
|
|
|
|
//Swap sending_line and calc_line
|
|
|
|
sending_line=calc_line;
|
|
|
|
calc_line=(calc_line==1)?0:1;
|
|
|
|
//Send the line we currently calculated.
|
2018-03-08 23:40:12 -05:00
|
|
|
send_lines(spi, y, lines[sending_line]);
|
|
|
|
//The line set is queued up for sending now; the actual sending happens in the
|
|
|
|
//background. We can go on to calculate the next line set as long as we do not
|
2017-01-06 01:20:32 -05:00
|
|
|
//touch line[sending_line]; the SPI sending process is still reading from that.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_main()
|
|
|
|
{
|
|
|
|
esp_err_t ret;
|
|
|
|
spi_device_handle_t spi;
|
|
|
|
spi_bus_config_t buscfg={
|
2017-01-10 22:25:56 -05:00
|
|
|
.miso_io_num=PIN_NUM_MISO,
|
|
|
|
.mosi_io_num=PIN_NUM_MOSI,
|
|
|
|
.sclk_io_num=PIN_NUM_CLK,
|
|
|
|
.quadwp_io_num=-1,
|
2018-03-08 23:40:12 -05:00
|
|
|
.quadhd_io_num=-1,
|
|
|
|
.max_transfer_sz=PARALLEL_LINES*320*2+8
|
2017-01-06 01:20:32 -05:00
|
|
|
};
|
|
|
|
spi_device_interface_config_t devcfg={
|
2018-03-08 23:40:12 -05:00
|
|
|
#ifdef CONFIG_LCD_OVERCLOCK
|
|
|
|
.clock_speed_hz=26*1000*1000, //Clock out at 26 MHz
|
|
|
|
#else
|
|
|
|
.clock_speed_hz=10*1000*1000, //Clock out at 10 MHz
|
|
|
|
#endif
|
2017-01-06 01:20:32 -05:00
|
|
|
.mode=0, //SPI mode 0
|
|
|
|
.spics_io_num=PIN_NUM_CS, //CS pin
|
|
|
|
.queue_size=7, //We want to be able to queue 7 transactions at a time
|
2017-07-18 05:02:07 -04:00
|
|
|
.pre_cb=lcd_spi_pre_transfer_callback, //Specify pre-transfer callback to handle D/C line
|
2017-01-06 01:20:32 -05:00
|
|
|
};
|
|
|
|
//Initialize the SPI bus
|
|
|
|
ret=spi_bus_initialize(HSPI_HOST, &buscfg, 1);
|
2018-03-08 23:40:12 -05:00
|
|
|
ESP_ERROR_CHECK(ret);
|
2017-01-06 01:20:32 -05:00
|
|
|
//Attach the LCD to the SPI bus
|
|
|
|
ret=spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
|
2018-03-08 23:40:12 -05:00
|
|
|
ESP_ERROR_CHECK(ret);
|
2017-01-06 01:20:32 -05:00
|
|
|
//Initialize the LCD
|
2017-07-18 05:02:07 -04:00
|
|
|
lcd_init(spi);
|
2018-03-08 23:40:12 -05:00
|
|
|
//Initialize the effect displayed
|
|
|
|
ret=pretty_effect_init();
|
|
|
|
ESP_ERROR_CHECK(ret);
|
|
|
|
|
2017-01-06 01:20:32 -05:00
|
|
|
//Go do nice stuff.
|
|
|
|
display_pretty_colors(spi);
|
|
|
|
}
|