Skip to content

Commit d332ae5

Browse files
vanwinkeljannashif
authored andcommitted
driver: Updated ILI9340 driver to use display API
Updated ILI9340 display driver and sample application to make use off the display API Signed-off-by: Jan Van Winkel <[email protected]>
1 parent 7926a34 commit d332ae5

File tree

7 files changed

+189
-85
lines changed

7 files changed

+189
-85
lines changed

drivers/display/Kconfig.ili9340

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Kconfig - ILI9340 display driver configuration options
22

33
#
4-
# Copyright (c) 2017 dXplore
4+
# Copyright (c) 2017 Jan Van Winkel <[email protected]>
55
#
66
# SPDX-License-Identifier: Apache-2.0
77
#

drivers/display/Kconfig.microbit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Kconfig - Microbit display driver configuration options
22

33
#
4-
# Copyright (c) 2017 dXplore
4+
# Copyright (c) 2017 Jan Van Winkel <[email protected]>
55
#
66
# SPDX-License-Identifier: Apache-2.0
77
#

drivers/display/display_ili9340.c

Lines changed: 146 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
/* Copyright (c) 2017 dXplore
1+
/*
2+
* Copyright (c) 2017 Jan Van Winkel <[email protected]>
23
*
34
* SPDX-License-Identifier: Apache-2.0
45
*/
56

67
#include "display_ili9340.h"
7-
#include <drivers/display/ili9340.h>
8+
#include <display.h>
89

910
#define LOG_LEVEL CONFIG_LOG_ILI9340_LEVEL
1011
#include <logging/log.h>
@@ -13,6 +14,7 @@ LOG_MODULE_REGISTER(display);
1314
#include <gpio.h>
1415
#include <misc/byteorder.h>
1516
#include <spi.h>
17+
#include <string.h>
1618

1719
struct ili9340_data {
1820
struct device *reset_gpio;
@@ -27,12 +29,13 @@ struct ili9340_data {
2729
#define ILI9340_CMD_DATA_PIN_COMMAND 0
2830
#define ILI9340_CMD_DATA_PIN_DATA 1
2931

30-
static void ili9340_exit_sleep(struct ili9340_data *data);
31-
static void ili9340_set_mem_area(struct ili9340_data *data, const u16_t x,
32-
const u16_t y, const u16_t w, const u16_t h);
33-
static int ili9340_init(struct device *dev);
32+
static void ili9340_exit_sleep(struct ili9340_data *data)
33+
{
34+
ili9340_transmit(data, ILI9340_CMD_EXIT_SLEEP, NULL, 0);
35+
k_sleep(120);
36+
}
3437

35-
int ili9340_init(struct device *dev)
38+
static int ili9340_init(struct device *dev)
3639
{
3740
struct ili9340_data *data = (struct ili9340_data *)dev->driver_data;
3841

@@ -59,7 +62,7 @@ int ili9340_init(struct device *dev)
5962
#endif
6063

6164
data->reset_gpio =
62-
device_get_binding(CONFIG_ILI9340_RESET_GPIO_PORT_NAME);
65+
device_get_binding(CONFIG_ILI9340_RESET_GPIO_PORT_NAME);
6366
if (data->reset_gpio == NULL) {
6467
LOG_ERR("Could not get GPIO port for ILI9340 reset");
6568
return -EPERM;
@@ -69,7 +72,7 @@ int ili9340_init(struct device *dev)
6972
GPIO_DIR_OUT);
7073

7174
data->command_data_gpio =
72-
device_get_binding(CONFIG_ILI9340_CMD_DATA_GPIO_PORT_NAME);
75+
device_get_binding(CONFIG_ILI9340_CMD_DATA_GPIO_PORT_NAME);
7376
if (data->command_data_gpio == NULL) {
7477
LOG_ERR("Could not get GPIO port for ILI9340 command/data");
7578
return -EPERM;
@@ -92,57 +95,152 @@ int ili9340_init(struct device *dev)
9295
LOG_DBG("Exiting sleep mode");
9396
ili9340_exit_sleep(data);
9497

95-
/* device_get_binding checks if driver_api is not zero before checking
96-
* device name.
97-
* So just set driver_api to 1 else the function call will fail
98-
*/
99-
dev->driver_api = (void *)1;
100-
10198
return 0;
10299
}
103100

104-
void ili9340_write_pixel(const struct device *dev, const u16_t x, const u16_t y,
105-
const u8_t r, const u8_t g, const u8_t b)
101+
static void ili9340_set_mem_area(struct ili9340_data *data, const u16_t x,
102+
const u16_t y, const u16_t w, const u16_t h)
106103
{
107-
u8_t rgb_data[] = {r, g, b};
104+
u16_t spi_data[2];
108105

109-
LOG_DBG("Writing pixel @ %dx%d (x,y)", x, y);
110-
ili9340_write_bitmap(dev, x, y, 1, 1, &rgb_data[0]);
106+
spi_data[0] = sys_cpu_to_be16(x);
107+
spi_data[1] = sys_cpu_to_be16(x + w - 1);
108+
ili9340_transmit(data, ILI9340_CMD_COLUMN_ADDR, &spi_data[0], 4);
109+
110+
spi_data[0] = sys_cpu_to_be16(y);
111+
spi_data[1] = sys_cpu_to_be16(y + h - 1);
112+
ili9340_transmit(data, ILI9340_CMD_PAGE_ADDR, &spi_data[0], 4);
111113
}
112114

113-
void ili9340_write_bitmap(const struct device *dev, const u16_t x,
114-
const u16_t y, const u16_t w, const u16_t h,
115-
const u8_t *rgb_data)
115+
static int ili9340_write(const struct device *dev, const u16_t x,
116+
const u16_t y,
117+
const struct display_buffer_descriptor *desc,
118+
const void *buf)
116119
{
117120
struct ili9340_data *data = (struct ili9340_data *)dev->driver_data;
121+
const u8_t *write_data_start = (u8_t *) buf;
122+
struct spi_buf tx_buf;
123+
struct spi_buf_set tx_bufs;
124+
u16_t write_cnt;
125+
u16_t nbr_of_writes;
126+
u16_t write_h;
127+
128+
__ASSERT(desc->width <= desc->pitch, "Pitch is smaller then width");
129+
__ASSERT((3 * desc->pitch * desc->height) <= desc->bu_size,
130+
"Input buffer to small");
131+
132+
LOG_DBG("Writing %dx%d (w,h) @ %dx%d (x,y)", desc->width, desc->height,
133+
x, y);
134+
ili9340_set_mem_area(data, x, y, desc->width, desc->height);
135+
136+
if (desc->pitch > desc->width) {
137+
write_h = 1;
138+
nbr_of_writes = desc->height;
139+
} else {
140+
write_h = desc->height;
141+
nbr_of_writes = 1;
142+
}
118143

119-
LOG_DBG("Writing %dx%d (w,h) bitmap @ %dx%d (x,y)", w, h, x, y);
120-
ili9340_set_mem_area(data, x, y, w, h);
121-
ili9340_transmit(data, ILI9340_CMD_MEM_WRITE, (void *)rgb_data,
122-
3 * w * h);
144+
ili9340_transmit(data, ILI9340_CMD_MEM_WRITE,
145+
(void *) write_data_start, 3 * desc->width * write_h);
146+
147+
tx_bufs.buffers = &tx_buf;
148+
tx_bufs.count = 1;
149+
150+
write_data_start += (3 * desc->pitch);
151+
for (write_cnt = 1; write_cnt < nbr_of_writes; ++write_cnt) {
152+
tx_buf.buf = (void *)write_data_start;
153+
tx_buf.len = 3 * desc->width * write_h;
154+
spi_write(data->spi_dev, &data->spi_config, &tx_bufs);
155+
write_data_start += (3 * desc->pitch);
156+
}
157+
158+
return 0;
159+
}
160+
161+
static int ili9340_read(const struct device *dev, const u16_t x,
162+
const u16_t y,
163+
const struct display_buffer_descriptor *desc,
164+
void *buf)
165+
{
166+
LOG_ERR("Reading not supported");
167+
return -ENOTSUP;
168+
}
169+
170+
static void *ili9340_get_framebuffer(const struct device *dev)
171+
{
172+
LOG_ERR("Direct framebuffer access not supported");
173+
return NULL;
123174
}
124175

125-
void ili9340_display_on(struct device *dev)
176+
static int ili9340_display_blanking_off(const struct device *dev)
126177
{
127178
struct ili9340_data *data = (struct ili9340_data *)dev->driver_data;
128179

129-
LOG_DBG("Turning display on");
180+
LOG_DBG("Turning display blanking off");
130181
ili9340_transmit(data, ILI9340_CMD_DISPLAY_ON, NULL, 0);
182+
return 0;
131183
}
132184

133-
void ili9340_display_off(struct device *dev)
185+
static int ili9340_display_blanking_on(const struct device *dev)
134186
{
135187
struct ili9340_data *data = (struct ili9340_data *)dev->driver_data;
136188

137-
LOG_DBG("Turning display off");
189+
LOG_DBG("Turning display blanking on");
138190
ili9340_transmit(data, ILI9340_CMD_DISPLAY_OFF, NULL, 0);
191+
return 0;
192+
}
193+
194+
static int ili9340_set_brightness(const struct device *dev,
195+
const u8_t brightness)
196+
{
197+
LOG_WRN("Set brightness not implemented");
198+
return -ENOTSUP;
199+
}
200+
201+
static int ili9340_set_contrast(const struct device *dev, const u8_t contrast)
202+
{
203+
LOG_ERR("Set contrast not supported");
204+
return -ENOTSUP;
205+
}
206+
207+
static int ili9340_set_pixel_format(const struct device *dev,
208+
const enum display_pixel_format
209+
pixel_format)
210+
{
211+
if (pixel_format == PIXEL_FORMAT_RGB_888) {
212+
return 0;
213+
}
214+
LOG_ERR("Pixel format change not implemented");
215+
return -ENOTSUP;
216+
}
217+
218+
static int ili9340_set_orientation(const struct device *dev,
219+
const enum display_orientation orientation)
220+
{
221+
if (orientation == DISPLAY_ORIENTATION_NORMAL) {
222+
return 0;
223+
}
224+
LOG_ERR("Changing display orientation not implemented");
225+
return -ENOTSUP;
226+
}
227+
228+
static void ili9340_get_capabilities(const struct device *dev,
229+
struct display_capabilities *capabilities)
230+
{
231+
memset(capabilities, 0, sizeof(struct display_capabilities));
232+
capabilities->x_resolution = 320;
233+
capabilities->y_resolution = 240;
234+
capabilities->supported_pixel_formats = PIXEL_FORMAT_RGB_888;
235+
capabilities->current_pixel_format = PIXEL_FORMAT_RGB_888;
236+
capabilities->current_orientation = DISPLAY_ORIENTATION_NORMAL;
139237
}
140238

141239
void ili9340_transmit(struct ili9340_data *data, u8_t cmd, void *tx_data,
142240
size_t tx_len)
143241
{
144-
struct spi_buf tx_buf = {.buf = &cmd, .len = 1};
145-
struct spi_buf_set tx_bufs = {.buffers = &tx_buf, .count = 1};
242+
struct spi_buf tx_buf = { .buf = &cmd, .len = 1 };
243+
struct spi_buf_set tx_bufs = { .buffers = &tx_buf, .count = 1 };
146244

147245
gpio_pin_write(data->command_data_gpio, CONFIG_ILI9340_CMD_DATA_PIN,
148246
ILI9340_CMD_DATA_PIN_COMMAND);
@@ -158,27 +256,21 @@ void ili9340_transmit(struct ili9340_data *data, u8_t cmd, void *tx_data,
158256
}
159257
}
160258

161-
void ili9340_exit_sleep(struct ili9340_data *data)
162-
{
163-
ili9340_transmit(data, ILI9340_CMD_EXIT_SLEEP, NULL, 0);
164-
k_sleep(120);
165-
}
166-
167-
void ili9340_set_mem_area(struct ili9340_data *data, const u16_t x,
168-
const u16_t y, const u16_t w, const u16_t h)
169-
{
170-
u16_t spi_data[2];
171-
172-
spi_data[0] = sys_cpu_to_be16(x);
173-
spi_data[1] = sys_cpu_to_be16(x + w - 1);
174-
ili9340_transmit(data, ILI9340_CMD_COLUMN_ADDR, &spi_data[0], 4);
175-
176-
spi_data[0] = sys_cpu_to_be16(y);
177-
spi_data[1] = sys_cpu_to_be16(y + h - 1);
178-
ili9340_transmit(data, ILI9340_CMD_PAGE_ADDR, &spi_data[0], 4);
179-
}
259+
static const struct display_driver_api ili9340_api = {
260+
.blanking_on = ili9340_display_blanking_on,
261+
.blanking_off = ili9340_display_blanking_off,
262+
.write = ili9340_write,
263+
.read = ili9340_read,
264+
.get_framebuffer = ili9340_get_framebuffer,
265+
.set_brightness = ili9340_set_brightness,
266+
.set_contrast = ili9340_set_contrast,
267+
.get_capabilities = ili9340_get_capabilities,
268+
.set_pixel_format = ili9340_set_pixel_format,
269+
.set_orientation = ili9340_set_orientation,
270+
};
180271

181272
static struct ili9340_data ili9340_data;
182273

183-
DEVICE_INIT(ili9340, CONFIG_ILI9340_DEV_NAME, &ili9340_init, &ili9340_data,
184-
NULL, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
274+
DEVICE_AND_API_INIT(ili9340, CONFIG_ILI9340_DEV_NAME, &ili9340_init,
275+
&ili9340_data, NULL, APPLICATION,
276+
CONFIG_APPLICATION_INIT_PRIORITY, &ili9340_api);

drivers/display/display_ili9340.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright (c) 2017 dXplore
1+
/*
2+
* Copyright (c) 2017 Jan Van Winkel <[email protected]>
23
*
34
* SPDX-License-Identifier: Apache-2.0
45
*/

drivers/display/display_ili9340_adafruit_1480.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright (c) 2017 dXplore
1+
/*
2+
* Copyright (c) 2017 Jan Van Winkel <[email protected]>
23
*
34
* SPDX-License-Identifier: Apache-2.0
45
*/

include/drivers/display/ili9340.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright (c) 2017 dXplore
1+
/*
2+
* Copyright (c) 2017 Jan Van Winkel <[email protected]>
23
*
34
* SPDX-License-Identifier: Apache-2.0
45
*/
@@ -35,23 +36,18 @@ void ili9340_write_pixel(const struct device *dev, const u16_t x, const u16_t y,
3536
* @param rgb_data pointer to the RGB data array, the data array should be at
3637
* minimum w * h * 3 bytes
3738
*/
38-
void ili9340_write_bitmap(const struct device *dev, const u16_t x,
39-
const u16_t y, const u16_t w, const u16_t h,
40-
const u8_t *rgb_data);
4139

4240
/**
4341
* @brief Turn display on
4442
*
4543
* @param dev Pointer to device structure
4644
*/
47-
void ili9340_display_on(struct device *dev);
4845

4946
/**
5047
* @brief Turn display off
5148
*
5249
* @param dev Pointer to device structure
5350
*/
54-
void ili9340_display_off(struct device *dev);
5551

5652
#ifdef __cplusplus
5753
}

0 commit comments

Comments
 (0)