-
Notifications
You must be signed in to change notification settings - Fork 427
/
disp_spi.c
133 lines (107 loc) · 3.23 KB
/
disp_spi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* @file disp_spi.c
*
*/
/*********************
* INCLUDES
*********************/
#include "disp_spi.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include "freertos/task.h"
#include "lvgl/lvgl.h"
#include "ili9341.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void IRAM_ATTR spi_ready (spi_transaction_t *trans);
/**********************
* STATIC VARIABLES
**********************/
static spi_device_handle_t spi;
static volatile bool spi_trans_in_progress;
static volatile bool spi_color_sent;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
void disp_spi_init(void)
{
esp_err_t ret;
spi_bus_config_t buscfg={
.miso_io_num=-1,
.mosi_io_num=DISP_SPI_MOSI,
.sclk_io_num=DISP_SPI_CLK,
.quadwp_io_num=-1,
.quadhd_io_num=-1,
#if CONFIG_LVGL_TFT_DISPLAY_CONTROLLER == 0
.max_transfer_sz = DISP_BUF_SIZE * 2,
#elif CONFIG_LVGL_TFT_DISPLAY_CONTROLLER == 1
.max_transfer_sz = DISP_BUF_SIZE * 3,
#endif
};
spi_device_interface_config_t devcfg={
.clock_speed_hz=40*1000*1000, //Clock out at 40 MHz
.mode=0, //SPI mode 0
.spics_io_num=DISP_SPI_CS, //CS pin
.queue_size=1,
.pre_cb=NULL,
.post_cb=spi_ready,
.flags = SPI_DEVICE_HALFDUPLEX
};
//Initialize the SPI bus
ret=spi_bus_initialize(HSPI_HOST, &buscfg, 1);
assert(ret==ESP_OK);
//Attach the LCD to the SPI bus
ret=spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
assert(ret==ESP_OK);
}
void disp_spi_send_data(uint8_t * data, uint16_t length)
{
if (length == 0) return; //no need to send anything
while(spi_trans_in_progress);
spi_transaction_t t = {
.length = length * 8, // transaction length is in bits
.tx_buffer = data
};
spi_trans_in_progress = true;
spi_color_sent = false; //Mark the "lv_flush_ready" NOT needs to be called in "spi_ready"
spi_device_queue_trans(spi, &t, portMAX_DELAY);
}
void disp_spi_send_colors(uint8_t * data, uint16_t length)
{
if (length == 0) return; //no need to send anything
while(spi_trans_in_progress);
spi_transaction_t t = {
.length = length * 8, // transaction length is in bits
.tx_buffer = data
};
spi_trans_in_progress = true;
spi_color_sent = true; //Mark the "lv_flush_ready" needs to be called in "spi_ready"
spi_device_queue_trans(spi, &t, portMAX_DELAY);
}
bool disp_spi_is_busy(void)
{
return spi_trans_in_progress;
}
/**********************
* STATIC FUNCTIONS
**********************/
static void IRAM_ATTR spi_ready (spi_transaction_t *trans)
{
spi_trans_in_progress = false;
lv_disp_t * disp = lv_refr_get_disp_refreshing();
if(spi_color_sent) lv_disp_flush_ready(&disp->driver);
}