-
Notifications
You must be signed in to change notification settings - Fork 16
/
mipi_display.c
323 lines (257 loc) · 10.1 KB
/
mipi_display.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
MIT License
Copyright (c) 2017-2018 Espressif Systems (Shanghai) PTE LTD
Copyright (c) 2019-2021 Mika Tuupola
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-cut-
This code is based on Espressif provided SPI Master example which was
released to Public Domain: https://goo.gl/ksC2Ln
This file is part of the MIPI DCS Display Driver:
https://github.com/tuupola/esp_mipi
SPDX-License-Identifier: MIT
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <esp_system.h>
#include <driver/spi_master.h>
#include <driver/ledc.h>
#include <soc/gpio_struct.h>
#include <driver/gpio.h>
#include <esp_log.h>
#include <esp_rom_gpio.h>
#include "sdkconfig.h"
#include "mipi_dcs.h"
#include "mipi_display.h"
static const char *TAG = "mipi_display";
static SemaphoreHandle_t mutex;
static void mipi_display_write_command(spi_device_handle_t spi, const uint8_t command)
{
spi_transaction_t transaction;
memset(&transaction, 0, sizeof(transaction));
/* Length in bits. */
transaction.length = 1 * 8;
transaction.tx_buffer = &command;
ESP_LOGD(TAG, "Sending command 0x%02x", (uint8_t)command);
/* Set DC low to denote a command. */
gpio_set_level(CONFIG_MIPI_DISPLAY_PIN_DC, 0);
ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &transaction));
}
static void mipi_display_write_data(spi_device_handle_t spi, const uint8_t *data, size_t length)
{
if (0 == length) {
return;
};
spi_transaction_t transaction;
memset(&transaction, 0, sizeof(transaction));
/* Length in bits. */
transaction.length = length * 8;
transaction.tx_buffer = data;
/* Set DC high to denote data. */
gpio_set_level(CONFIG_MIPI_DISPLAY_PIN_DC, 1);
ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &transaction));
ESP_LOG_BUFFER_HEX_LEVEL(TAG, data, length, ESP_LOG_DEBUG);
}
static void mipi_display_read_data(spi_device_handle_t spi, uint8_t *data, size_t length)
{
if (0 == length) {
return;
};
spi_transaction_t transaction;
memset(&transaction, 0, sizeof(transaction));
/* Length in bits */
transaction.length = length * 8;
transaction.rxlength = length * 8;
transaction.rx_buffer = data;
//transaction.flags = SPI_TRANS_USE_RXDATA;
/* Set DC high to denote data. */
gpio_set_level(CONFIG_MIPI_DISPLAY_PIN_DC, 1);
ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &transaction));
}
static void mipi_display_set_address(spi_device_handle_t spi, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
uint8_t data[4];
static uint16_t prev_x1, prev_x2, prev_y1, prev_y2;
x1 = x1 + CONFIG_MIPI_DISPLAY_OFFSET_X;
y1 = y1 + CONFIG_MIPI_DISPLAY_OFFSET_Y;
x2 = x2 + CONFIG_MIPI_DISPLAY_OFFSET_X;
y2 = y2 + CONFIG_MIPI_DISPLAY_OFFSET_Y;
/* Change column address only if it has changed. */
if ((prev_x1 != x1 || prev_x2 != x2)) {
mipi_display_write_command(spi, MIPI_DCS_SET_COLUMN_ADDRESS);
data[0] = x1 >> 8;
data[1] = x1 & 0xff;
data[2] = x2 >> 8;
data[3] = x2 & 0xff;
mipi_display_write_data(spi, data, 4);
prev_x1 = x1;
prev_x2 = x2;
}
/* Change page address only if it has changed. */
if ((prev_y1 != y1 || prev_y2 != y2)) {
mipi_display_write_command(spi, MIPI_DCS_SET_PAGE_ADDRESS);
data[0] = y1 >> 8;
data[1] = y1 & 0xff;
data[2] = y2 >> 8;
data[3] = y2 & 0xff;
mipi_display_write_data(spi, data, 4);
prev_y1 = y1;
prev_y2 = y2;
}
mipi_display_write_command(spi, MIPI_DCS_WRITE_MEMORY_START);
}
size_t mipi_display_write(spi_device_handle_t spi, uint16_t x1, uint16_t y1, uint16_t w, uint16_t h, uint8_t *buffer)
{
if (0 == w || 0 == h) {
return 0;
}
const int32_t x2 = x1 + w - 1;
const int32_t y2 = y1 + h - 1;
const size_t size = w * h * DISPLAY_DEPTH / 8;
xSemaphoreTake(mutex, portMAX_DELAY);
mipi_display_set_address(spi, x1, y1, x2, y2);
mipi_display_write_data(spi, buffer, size);
xSemaphoreGive(mutex);
return size;
}
static void mipi_display_spi_master_init(spi_device_handle_t *spi)
{
spi_bus_config_t buscfg = {
.miso_io_num = CONFIG_MIPI_DISPLAY_PIN_MISO,
.mosi_io_num = CONFIG_MIPI_DISPLAY_PIN_MOSI,
.sclk_io_num = CONFIG_MIPI_DISPLAY_PIN_CLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
/* Max transfer size in bytes. */
.max_transfer_sz = SPI_MAX_TRANSFER_SIZE
};
spi_device_interface_config_t devcfg = {
.clock_speed_hz = CONFIG_MIPI_DISPLAY_SPI_CLOCK_SPEED_HZ,
.mode = CONFIG_MIPI_DISPLAY_SPI_MODE,
.spics_io_num = CONFIG_MIPI_DISPLAY_PIN_CS,
.queue_size = 64,
.flags = SPI_DEVICE_NO_DUMMY
};
/* ESP32S2 requires DMA channel to match the SPI host. */
ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_MIPI_DISPLAY_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_MIPI_DISPLAY_SPI_HOST, &devcfg, spi));
}
void mipi_display_init(spi_device_handle_t *spi)
{
mutex = xSemaphoreCreateMutex();
if (CONFIG_MIPI_DISPLAY_PIN_CS > 0) {
esp_rom_gpio_pad_select_gpio(CONFIG_MIPI_DISPLAY_PIN_CS);
gpio_set_direction(CONFIG_MIPI_DISPLAY_PIN_CS, GPIO_MODE_OUTPUT);
gpio_set_level(CONFIG_MIPI_DISPLAY_PIN_CS, 0);
};
esp_rom_gpio_pad_select_gpio(CONFIG_MIPI_DISPLAY_PIN_DC);
gpio_set_direction(CONFIG_MIPI_DISPLAY_PIN_DC, GPIO_MODE_OUTPUT);
mipi_display_spi_master_init(spi);
vTaskDelay(100 / portTICK_PERIOD_MS);
/* Reset the display. */
if (CONFIG_MIPI_DISPLAY_PIN_RST > 0) {
esp_rom_gpio_pad_select_gpio(CONFIG_MIPI_DISPLAY_PIN_RST);
gpio_set_direction(CONFIG_MIPI_DISPLAY_PIN_RST, GPIO_MODE_OUTPUT);
gpio_set_level(CONFIG_MIPI_DISPLAY_PIN_RST, 0);
vTaskDelay(100 / portTICK_PERIOD_MS);
gpio_set_level(CONFIG_MIPI_DISPLAY_PIN_RST, 1);
vTaskDelay(100 / portTICK_PERIOD_MS);
}
/* Send minimal init commands. */
mipi_display_write_command(*spi, MIPI_DCS_SOFT_RESET);
vTaskDelay(200 / portTICK_PERIOD_MS);
mipi_display_write_command(*spi, MIPI_DCS_SET_ADDRESS_MODE);
mipi_display_write_data(*spi, &(uint8_t){MIPI_DISPLAY_ADDRESS_MODE}, 1);
mipi_display_write_command(*spi, MIPI_DCS_SET_PIXEL_FORMAT);
mipi_display_write_data(*spi, &(uint8_t){CONFIG_MIPI_DISPLAY_PIXEL_FORMAT}, 1);
#ifdef CONFIG_MIPI_DISPLAY_INVERT
mipi_display_write_command(*spi, MIPI_DCS_ENTER_INVERT_MODE);
#else
mipi_display_write_command(*spi, MIPI_DCS_EXIT_INVERT_MODE);
#endif
mipi_display_write_command(*spi, MIPI_DCS_EXIT_SLEEP_MODE);
vTaskDelay(200 / portTICK_PERIOD_MS);
mipi_display_write_command(*spi, MIPI_DCS_SET_DISPLAY_ON);
vTaskDelay(200 / portTICK_PERIOD_MS);
#if CONFIG_MIPI_DISPLAY_PIN_BL > 0
/* Enable backlight. */
ESP_LOGI(TAG, "Enabling backlight pin %d", CONFIG_MIPI_DISPLAY_PIN_BL);
esp_rom_gpio_pad_select_gpio(CONFIG_MIPI_DISPLAY_PIN_BL);
gpio_set_direction(CONFIG_MIPI_DISPLAY_PIN_BL, GPIO_MODE_OUTPUT);
gpio_set_level(CONFIG_MIPI_DISPLAY_PIN_BL, CONFIG_MIPI_DISPLAY_PIN_BL_ACTIVE);
#endif
#if CONFIG_MIPI_DISPLAY_PWM_BL > 0
/* Enable backlight PWM. */
ESP_LOGI(TAG, "Setting backlight PWM to %d", CONFIG_MIPI_DISPLAY_PWM_BL);
ledc_timer_config_t timercfg = {
.duty_resolution = LEDC_TIMER_13_BIT,
.freq_hz = 9765,
.speed_mode = LEDC_LOW_SPEED_MODE,
.timer_num = LEDC_TIMER_0,
.clk_cfg = LEDC_AUTO_CLK,
};
ledc_timer_config(&timercfg);
ledc_channel_config_t channelcfg = {
.channel = LEDC_CHANNEL_0,
.duty = CONFIG_MIPI_DISPLAY_PWM_BL,
.gpio_num = CONFIG_MIPI_DISPLAY_PIN_BL,
.speed_mode = LEDC_LOW_SPEED_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_0,
};
ledc_channel_config(&channelcfg);
#endif
ESP_LOGI(TAG, "Display initialized.");
spi_device_acquire_bus(*spi, portMAX_DELAY);
}
void mipi_display_ioctl(spi_device_handle_t spi, const uint8_t command, uint8_t *data, size_t size)
{
xSemaphoreTake(mutex, portMAX_DELAY);
switch (command) {
case MIPI_DCS_GET_COMPRESSION_MODE:
case MIPI_DCS_GET_DISPLAY_ID:
case MIPI_DCS_GET_RED_CHANNEL:
case MIPI_DCS_GET_GREEN_CHANNEL:
case MIPI_DCS_GET_BLUE_CHANNEL:
case MIPI_DCS_GET_DISPLAY_STATUS:
case MIPI_DCS_GET_POWER_MODE:
case MIPI_DCS_GET_ADDRESS_MODE:
case MIPI_DCS_GET_PIXEL_FORMAT:
case MIPI_DCS_GET_DISPLAY_MODE:
case MIPI_DCS_GET_SIGNAL_MODE:
case MIPI_DCS_GET_DIAGNOSTIC_RESULT:
case MIPI_DCS_GET_SCANLINE:
case MIPI_DCS_GET_DISPLAY_BRIGHTNESS:
case MIPI_DCS_GET_CONTROL_DISPLAY:
case MIPI_DCS_GET_POWER_SAVE:
case MIPI_DCS_READ_DDB_START:
case MIPI_DCS_READ_DDB_CONTINUE:
mipi_display_write_command(spi, command);
mipi_display_read_data(spi, data, size);
break;
default:
mipi_display_write_command(spi, command);
mipi_display_write_data(spi, data, size);
}
xSemaphoreGive(mutex);
}
void mipi_display_close(spi_device_handle_t spi)
{
spi_device_release_bus(spi);
}