1 /* SPI Master example
2 
3    This example code is in the Public Domain (or CC0 licensed, at your option.)
4 
5    Unless required by applicable law or agreed to in writing, this
6    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7    CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "freertos/FreeRTOS.h"
13 #include "freertos/task.h"
14 #include "esp_system.h"
15 #include "driver/spi_master.h"
16 #include "driver/gpio.h"
17 
18 #include "pretty_effect.h"
19 
20 /*
21  This code displays some fancy graphics on the 320x240 LCD on an ESP-WROVER_KIT board.
22  This example demonstrates the use of both spi_device_transmit as well as
23  spi_device_queue_trans/spi_device_get_trans_result and pre-transmit callbacks.
24 
25  Some info about the ILI9341/ST7789V: It has an C/D line, which is connected to a GPIO here. It expects this
26  line to be low for a command and high for data. We use a pre-transmit callback here to control that
27  line: every transaction has as the user-definable argument the needed state of the D/C line and just
28  before the transaction is sent, the callback will set this line to the correct state.
29 */
30 
31 #ifdef CONFIG_IDF_TARGET_ESP32
32 #define LCD_HOST    HSPI_HOST
33 
34 #define PIN_NUM_MISO 25
35 #define PIN_NUM_MOSI 23
36 #define PIN_NUM_CLK  19
37 #define PIN_NUM_CS   22
38 
39 #define PIN_NUM_DC   21
40 #define PIN_NUM_RST  18
41 #define PIN_NUM_BCKL 5
42 #elif defined CONFIG_IDF_TARGET_ESP32S2
43 #define LCD_HOST    SPI2_HOST
44 
45 #define PIN_NUM_MISO 37
46 #define PIN_NUM_MOSI 35
47 #define PIN_NUM_CLK  36
48 #define PIN_NUM_CS   34
49 
50 #define PIN_NUM_DC   4
51 #define PIN_NUM_RST  5
52 #define PIN_NUM_BCKL 6
53 #elif defined CONFIG_IDF_TARGET_ESP32C3
54 #define LCD_HOST    SPI2_HOST
55 
56 #define PIN_NUM_MISO 2
57 #define PIN_NUM_MOSI 7
58 #define PIN_NUM_CLK  6
59 #define PIN_NUM_CS   10
60 
61 #define PIN_NUM_DC   9
62 #define PIN_NUM_RST  4
63 #define PIN_NUM_BCKL 5
64 #endif
65 
66 //To speed up transfers, every SPI transfer sends a bunch of lines. This define specifies how many. More means more memory use,
67 //but less overhead for setting up / finishing transfers. Make sure 240 is dividable by this.
68 #define PARALLEL_LINES 16
69 
70 /*
71  The LCD needs a bunch of command/argument values to be initialized. They are stored in this struct.
72 */
73 typedef struct {
74     uint8_t cmd;
75     uint8_t data[16];
76     uint8_t databytes; //No of data in data; bit 7 = delay after set; 0xFF = end of cmds.
77 } lcd_init_cmd_t;
78 
79 typedef enum {
80     LCD_TYPE_ILI = 1,
81     LCD_TYPE_ST,
82     LCD_TYPE_MAX,
83 } type_lcd_t;
84 
85 //Place data into DRAM. Constant data gets placed into DROM by default, which is not accessible by DMA.
86 DRAM_ATTR static const lcd_init_cmd_t st_init_cmds[]={
87     /* Memory Data Access Control, MX=MV=1, MY=ML=MH=0, RGB=0 */
88     {0x36, {(1<<5)|(1<<6)}, 1},
89     /* Interface Pixel Format, 16bits/pixel for RGB/MCU interface */
90     {0x3A, {0x55}, 1},
91     /* Porch Setting */
92     {0xB2, {0x0c, 0x0c, 0x00, 0x33, 0x33}, 5},
93     /* Gate Control, Vgh=13.65V, Vgl=-10.43V */
94     {0xB7, {0x45}, 1},
95     /* VCOM Setting, VCOM=1.175V */
96     {0xBB, {0x2B}, 1},
97     /* LCM Control, XOR: BGR, MX, MH */
98     {0xC0, {0x2C}, 1},
99     /* VDV and VRH Command Enable, enable=1 */
100     {0xC2, {0x01, 0xff}, 2},
101     /* VRH Set, Vap=4.4+... */
102     {0xC3, {0x11}, 1},
103     /* VDV Set, VDV=0 */
104     {0xC4, {0x20}, 1},
105     /* Frame Rate Control, 60Hz, inversion=0 */
106     {0xC6, {0x0f}, 1},
107     /* Power Control 1, AVDD=6.8V, AVCL=-4.8V, VDDS=2.3V */
108     {0xD0, {0xA4, 0xA1}, 1},
109     /* Positive Voltage Gamma Control */
110     {0xE0, {0xD0, 0x00, 0x05, 0x0E, 0x15, 0x0D, 0x37, 0x43, 0x47, 0x09, 0x15, 0x12, 0x16, 0x19}, 14},
111     /* Negative Voltage Gamma Control */
112     {0xE1, {0xD0, 0x00, 0x05, 0x0D, 0x0C, 0x06, 0x2D, 0x44, 0x40, 0x0E, 0x1C, 0x18, 0x16, 0x19}, 14},
113     /* Sleep Out */
114     {0x11, {0}, 0x80},
115     /* Display On */
116     {0x29, {0}, 0x80},
117     {0, {0}, 0xff}
118 };
119 
120 DRAM_ATTR static const lcd_init_cmd_t ili_init_cmds[]={
121     /* Power contorl B, power control = 0, DC_ENA = 1 */
122     {0xCF, {0x00, 0x83, 0X30}, 3},
123     /* Power on sequence control,
124      * cp1 keeps 1 frame, 1st frame enable
125      * vcl = 0, ddvdh=3, vgh=1, vgl=2
126      * DDVDH_ENH=1
127      */
128     {0xED, {0x64, 0x03, 0X12, 0X81}, 4},
129     /* Driver timing control A,
130      * non-overlap=default +1
131      * EQ=default - 1, CR=default
132      * pre-charge=default - 1
133      */
134     {0xE8, {0x85, 0x01, 0x79}, 3},
135     /* Power control A, Vcore=1.6V, DDVDH=5.6V */
136     {0xCB, {0x39, 0x2C, 0x00, 0x34, 0x02}, 5},
137     /* Pump ratio control, DDVDH=2xVCl */
138     {0xF7, {0x20}, 1},
139     /* Driver timing control, all=0 unit */
140     {0xEA, {0x00, 0x00}, 2},
141     /* Power control 1, GVDD=4.75V */
142     {0xC0, {0x26}, 1},
143     /* Power control 2, DDVDH=VCl*2, VGH=VCl*7, VGL=-VCl*3 */
144     {0xC1, {0x11}, 1},
145     /* VCOM control 1, VCOMH=4.025V, VCOML=-0.950V */
146     {0xC5, {0x35, 0x3E}, 2},
147     /* VCOM control 2, VCOMH=VMH-2, VCOML=VML-2 */
148     {0xC7, {0xBE}, 1},
149     /* Memory access contorl, MX=MY=0, MV=1, ML=0, BGR=1, MH=0 */
150     {0x36, {0x28}, 1},
151     /* Pixel format, 16bits/pixel for RGB/MCU interface */
152     {0x3A, {0x55}, 1},
153     /* Frame rate control, f=fosc, 70Hz fps */
154     {0xB1, {0x00, 0x1B}, 2},
155     /* Enable 3G, disabled */
156     {0xF2, {0x08}, 1},
157     /* Gamma set, curve 1 */
158     {0x26, {0x01}, 1},
159     /* Positive gamma correction */
160     {0xE0, {0x1F, 0x1A, 0x18, 0x0A, 0x0F, 0x06, 0x45, 0X87, 0x32, 0x0A, 0x07, 0x02, 0x07, 0x05, 0x00}, 15},
161     /* Negative gamma correction */
162     {0XE1, {0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3A, 0x78, 0x4D, 0x05, 0x18, 0x0D, 0x38, 0x3A, 0x1F}, 15},
163     /* Column address set, SC=0, EC=0xEF */
164     {0x2A, {0x00, 0x00, 0x00, 0xEF}, 4},
165     /* Page address set, SP=0, EP=0x013F */
166     {0x2B, {0x00, 0x00, 0x01, 0x3f}, 4},
167     /* Memory write */
168     {0x2C, {0}, 0},
169     /* Entry mode set, Low vol detect disabled, normal display */
170     {0xB7, {0x07}, 1},
171     /* Display function control */
172     {0xB6, {0x0A, 0x82, 0x27, 0x00}, 4},
173     /* Sleep out */
174     {0x11, {0}, 0x80},
175     /* Display on */
176     {0x29, {0}, 0x80},
177     {0, {0}, 0xff},
178 };
179 
180 /* Send a command to the LCD. Uses spi_device_polling_transmit, which waits
181  * until the transfer is complete.
182  *
183  * Since command transactions are usually small, they are handled in polling
184  * mode for higher speed. The overhead of interrupt transactions is more than
185  * just waiting for the transaction to complete.
186  */
lcd_cmd(spi_device_handle_t spi,const uint8_t cmd)187 void lcd_cmd(spi_device_handle_t spi, const uint8_t cmd)
188 {
189     esp_err_t ret;
190     spi_transaction_t t;
191     memset(&t, 0, sizeof(t));       //Zero out the transaction
192     t.length=8;                     //Command is 8 bits
193     t.tx_buffer=&cmd;               //The data is the cmd itself
194     t.user=(void*)0;                //D/C needs to be set to 0
195     ret=spi_device_polling_transmit(spi, &t);  //Transmit!
196     assert(ret==ESP_OK);            //Should have had no issues.
197 }
198 
199 /* Send data to the LCD. Uses spi_device_polling_transmit, which waits until the
200  * transfer is complete.
201  *
202  * Since data transactions are usually small, they are handled in polling
203  * mode for higher speed. The overhead of interrupt transactions is more than
204  * just waiting for the transaction to complete.
205  */
lcd_data(spi_device_handle_t spi,const uint8_t * data,int len)206 void lcd_data(spi_device_handle_t spi, const uint8_t *data, int len)
207 {
208     esp_err_t ret;
209     spi_transaction_t t;
210     if (len==0) return;             //no need to send anything
211     memset(&t, 0, sizeof(t));       //Zero out the transaction
212     t.length=len*8;                 //Len is in bytes, transaction length is in bits.
213     t.tx_buffer=data;               //Data
214     t.user=(void*)1;                //D/C needs to be set to 1
215     ret=spi_device_polling_transmit(spi, &t);  //Transmit!
216     assert(ret==ESP_OK);            //Should have had no issues.
217 }
218 
219 //This function is called (in irq context!) just before a transmission starts. It will
220 //set the D/C line to the value indicated in the user field.
lcd_spi_pre_transfer_callback(spi_transaction_t * t)221 void lcd_spi_pre_transfer_callback(spi_transaction_t *t)
222 {
223     int dc=(int)t->user;
224     gpio_set_level(PIN_NUM_DC, dc);
225 }
226 
lcd_get_id(spi_device_handle_t spi)227 uint32_t lcd_get_id(spi_device_handle_t spi)
228 {
229     //get_id cmd
230     lcd_cmd(spi, 0x04);
231 
232     spi_transaction_t t;
233     memset(&t, 0, sizeof(t));
234     t.length=8*3;
235     t.flags = SPI_TRANS_USE_RXDATA;
236     t.user = (void*)1;
237 
238     esp_err_t ret = spi_device_polling_transmit(spi, &t);
239     assert( ret == ESP_OK );
240 
241     return *(uint32_t*)t.rx_data;
242 }
243 
244 //Initialize the display
lcd_init(spi_device_handle_t spi)245 void lcd_init(spi_device_handle_t spi)
246 {
247     int cmd=0;
248     const lcd_init_cmd_t* lcd_init_cmds;
249 
250     //Initialize non-SPI GPIOs
251     gpio_set_direction(PIN_NUM_DC, GPIO_MODE_OUTPUT);
252     gpio_set_direction(PIN_NUM_RST, GPIO_MODE_OUTPUT);
253     gpio_set_direction(PIN_NUM_BCKL, GPIO_MODE_OUTPUT);
254 
255     //Reset the display
256     gpio_set_level(PIN_NUM_RST, 0);
257     vTaskDelay(100 / portTICK_RATE_MS);
258     gpio_set_level(PIN_NUM_RST, 1);
259     vTaskDelay(100 / portTICK_RATE_MS);
260 
261     //detect LCD type
262     uint32_t lcd_id = lcd_get_id(spi);
263     int lcd_detected_type = 0;
264     int lcd_type;
265 
266     printf("LCD ID: %08X\n", lcd_id);
267     if ( lcd_id == 0 ) {
268         //zero, ili
269         lcd_detected_type = LCD_TYPE_ILI;
270         printf("ILI9341 detected.\n");
271     } else {
272         // none-zero, ST
273         lcd_detected_type = LCD_TYPE_ST;
274         printf("ST7789V detected.\n");
275     }
276 
277 #ifdef CONFIG_LCD_TYPE_AUTO
278     lcd_type = lcd_detected_type;
279 #elif defined( CONFIG_LCD_TYPE_ST7789V )
280     printf("kconfig: force CONFIG_LCD_TYPE_ST7789V.\n");
281     lcd_type = LCD_TYPE_ST;
282 #elif defined( CONFIG_LCD_TYPE_ILI9341 )
283     printf("kconfig: force CONFIG_LCD_TYPE_ILI9341.\n");
284     lcd_type = LCD_TYPE_ILI;
285 #endif
286     if ( lcd_type == LCD_TYPE_ST ) {
287         printf("LCD ST7789V initialization.\n");
288         lcd_init_cmds = st_init_cmds;
289     } else {
290         printf("LCD ILI9341 initialization.\n");
291         lcd_init_cmds = ili_init_cmds;
292     }
293 
294     //Send all the commands
295     while (lcd_init_cmds[cmd].databytes!=0xff) {
296         lcd_cmd(spi, lcd_init_cmds[cmd].cmd);
297         lcd_data(spi, lcd_init_cmds[cmd].data, lcd_init_cmds[cmd].databytes&0x1F);
298         if (lcd_init_cmds[cmd].databytes&0x80) {
299             vTaskDelay(100 / portTICK_RATE_MS);
300         }
301         cmd++;
302     }
303 
304     ///Enable backlight
305     gpio_set_level(PIN_NUM_BCKL, 0);
306 }
307 
308 
309 /* To send a set of lines we have to send a command, 2 data bytes, another command, 2 more data bytes and another command
310  * before sending the line data itself; a total of 6 transactions. (We can't put all of this in just one transaction
311  * because the D/C line needs to be toggled in the middle.)
312  * This routine queues these commands up as interrupt transactions so they get
313  * sent faster (compared to calling spi_device_transmit several times), and at
314  * the mean while the lines for next transactions can get calculated.
315  */
send_lines(spi_device_handle_t spi,int ypos,uint16_t * linedata)316 static void send_lines(spi_device_handle_t spi, int ypos, uint16_t *linedata)
317 {
318     esp_err_t ret;
319     int x;
320     //Transaction descriptors. Declared static so they're not allocated on the stack; we need this memory even when this
321     //function is finished because the SPI driver needs access to it even while we're already calculating the next line.
322     static spi_transaction_t trans[6];
323 
324     //In theory, it's better to initialize trans and data only once and hang on to the initialized
325     //variables. We allocate them on the stack, so we need to re-init them each call.
326     for (x=0; x<6; x++) {
327         memset(&trans[x], 0, sizeof(spi_transaction_t));
328         if ((x&1)==0) {
329             //Even transfers are commands
330             trans[x].length=8;
331             trans[x].user=(void*)0;
332         } else {
333             //Odd transfers are data
334             trans[x].length=8*4;
335             trans[x].user=(void*)1;
336         }
337         trans[x].flags=SPI_TRANS_USE_TXDATA;
338     }
339     trans[0].tx_data[0]=0x2A;           //Column Address Set
340     trans[1].tx_data[0]=0;              //Start Col High
341     trans[1].tx_data[1]=0;              //Start Col Low
342     trans[1].tx_data[2]=(320)>>8;       //End Col High
343     trans[1].tx_data[3]=(320)&0xff;     //End Col Low
344     trans[2].tx_data[0]=0x2B;           //Page address set
345     trans[3].tx_data[0]=ypos>>8;        //Start page high
346     trans[3].tx_data[1]=ypos&0xff;      //start page low
347     trans[3].tx_data[2]=(ypos+PARALLEL_LINES)>>8;    //end page high
348     trans[3].tx_data[3]=(ypos+PARALLEL_LINES)&0xff;  //end page low
349     trans[4].tx_data[0]=0x2C;           //memory write
350     trans[5].tx_buffer=linedata;        //finally send the line data
351     trans[5].length=320*2*8*PARALLEL_LINES;          //Data length, in bits
352     trans[5].flags=0; //undo SPI_TRANS_USE_TXDATA flag
353 
354     //Queue all transactions.
355     for (x=0; x<6; x++) {
356         ret=spi_device_queue_trans(spi, &trans[x], portMAX_DELAY);
357         assert(ret==ESP_OK);
358     }
359 
360     //When we are here, the SPI driver is busy (in the background) getting the transactions sent. That happens
361     //mostly using DMA, so the CPU doesn't have much to do here. We're not going to wait for the transaction to
362     //finish because we may as well spend the time calculating the next line. When that is done, we can call
363     //send_line_finish, which will wait for the transfers to be done and check their status.
364 }
365 
366 
send_line_finish(spi_device_handle_t spi)367 static void send_line_finish(spi_device_handle_t spi)
368 {
369     spi_transaction_t *rtrans;
370     esp_err_t ret;
371     //Wait for all 6 transactions to be done and get back the results.
372     for (int x=0; x<6; x++) {
373         ret=spi_device_get_trans_result(spi, &rtrans, portMAX_DELAY);
374         assert(ret==ESP_OK);
375         //We could inspect rtrans now if we received any info back. The LCD is treated as write-only, though.
376     }
377 }
378 
379 
380 //Simple routine to generate some patterns and send them to the LCD. Don't expect anything too
381 //impressive. Because the SPI driver handles transactions in the background, we can calculate the next line
382 //while the previous one is being sent.
display_pretty_colors(spi_device_handle_t spi)383 static void display_pretty_colors(spi_device_handle_t spi)
384 {
385     uint16_t *lines[2];
386     //Allocate memory for the pixel buffers
387     for (int i=0; i<2; i++) {
388         lines[i]=heap_caps_malloc(320*PARALLEL_LINES*sizeof(uint16_t), MALLOC_CAP_DMA);
389         assert(lines[i]!=NULL);
390     }
391     int frame=0;
392     //Indexes of the line currently being sent to the LCD and the line we're calculating.
393     int sending_line=-1;
394     int calc_line=0;
395 
396     while(1) {
397         frame++;
398         for (int y=0; y<240; y+=PARALLEL_LINES) {
399             //Calculate a line.
400             pretty_effect_calc_lines(lines[calc_line], y, frame, PARALLEL_LINES);
401             //Finish up the sending process of the previous line, if any
402             if (sending_line!=-1) send_line_finish(spi);
403             //Swap sending_line and calc_line
404             sending_line=calc_line;
405             calc_line=(calc_line==1)?0:1;
406             //Send the line we currently calculated.
407             send_lines(spi, y, lines[sending_line]);
408             //The line set is queued up for sending now; the actual sending happens in the
409             //background. We can go on to calculate the next line set as long as we do not
410             //touch line[sending_line]; the SPI sending process is still reading from that.
411         }
412     }
413 }
414 
app_main(void)415 void app_main(void)
416 {
417     esp_err_t ret;
418     spi_device_handle_t spi;
419     spi_bus_config_t buscfg={
420         .miso_io_num=PIN_NUM_MISO,
421         .mosi_io_num=PIN_NUM_MOSI,
422         .sclk_io_num=PIN_NUM_CLK,
423         .quadwp_io_num=-1,
424         .quadhd_io_num=-1,
425         .max_transfer_sz=PARALLEL_LINES*320*2+8
426     };
427     spi_device_interface_config_t devcfg={
428 #ifdef CONFIG_LCD_OVERCLOCK
429         .clock_speed_hz=26*1000*1000,           //Clock out at 26 MHz
430 #else
431         .clock_speed_hz=10*1000*1000,           //Clock out at 10 MHz
432 #endif
433         .mode=0,                                //SPI mode 0
434         .spics_io_num=PIN_NUM_CS,               //CS pin
435         .queue_size=7,                          //We want to be able to queue 7 transactions at a time
436         .pre_cb=lcd_spi_pre_transfer_callback,  //Specify pre-transfer callback to handle D/C line
437     };
438     //Initialize the SPI bus
439     ret=spi_bus_initialize(LCD_HOST, &buscfg, SPI_DMA_CH_AUTO);
440     ESP_ERROR_CHECK(ret);
441     //Attach the LCD to the SPI bus
442     ret=spi_bus_add_device(LCD_HOST, &devcfg, &spi);
443     ESP_ERROR_CHECK(ret);
444     //Initialize the LCD
445     lcd_init(spi);
446     //Initialize the effect displayed
447     ret=pretty_effect_init();
448     ESP_ERROR_CHECK(ret);
449 
450     //Go do nice stuff.
451     display_pretty_colors(spi);
452 }
453