1
- /* Copyright (c) 2017 dXplore
1
+ /*
2
+ * Copyright (c) 2017 Jan Van Winkel <[email protected] >
2
3
*
3
4
* SPDX-License-Identifier: Apache-2.0
4
5
*/
5
6
6
7
#include "display_ili9340.h"
7
- #include <drivers/ display/ili9340 .h>
8
+ #include <display.h>
8
9
9
10
#define LOG_LEVEL CONFIG_LOG_ILI9340_LEVEL
10
11
#include <logging/log.h>
@@ -13,6 +14,7 @@ LOG_MODULE_REGISTER(display);
13
14
#include <gpio.h>
14
15
#include <misc/byteorder.h>
15
16
#include <spi.h>
17
+ #include <string.h>
16
18
17
19
struct ili9340_data {
18
20
struct device * reset_gpio ;
@@ -27,12 +29,13 @@ struct ili9340_data {
27
29
#define ILI9340_CMD_DATA_PIN_COMMAND 0
28
30
#define ILI9340_CMD_DATA_PIN_DATA 1
29
31
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
+ }
34
37
35
- int ili9340_init (struct device * dev )
38
+ static int ili9340_init (struct device * dev )
36
39
{
37
40
struct ili9340_data * data = (struct ili9340_data * )dev -> driver_data ;
38
41
@@ -59,7 +62,7 @@ int ili9340_init(struct device *dev)
59
62
#endif
60
63
61
64
data -> reset_gpio =
62
- device_get_binding (CONFIG_ILI9340_RESET_GPIO_PORT_NAME );
65
+ device_get_binding (CONFIG_ILI9340_RESET_GPIO_PORT_NAME );
63
66
if (data -> reset_gpio == NULL ) {
64
67
LOG_ERR ("Could not get GPIO port for ILI9340 reset" );
65
68
return - EPERM ;
@@ -69,7 +72,7 @@ int ili9340_init(struct device *dev)
69
72
GPIO_DIR_OUT );
70
73
71
74
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 );
73
76
if (data -> command_data_gpio == NULL ) {
74
77
LOG_ERR ("Could not get GPIO port for ILI9340 command/data" );
75
78
return - EPERM ;
@@ -92,57 +95,152 @@ int ili9340_init(struct device *dev)
92
95
LOG_DBG ("Exiting sleep mode" );
93
96
ili9340_exit_sleep (data );
94
97
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
-
101
98
return 0 ;
102
99
}
103
100
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 )
106
103
{
107
- u8_t rgb_data [] = { r , g , b } ;
104
+ u16_t spi_data [ 2 ] ;
108
105
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 );
111
113
}
112
114
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 )
116
119
{
117
120
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
+ }
118
143
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 ;
123
174
}
124
175
125
- void ili9340_display_on ( struct device * dev )
176
+ static int ili9340_display_blanking_off ( const struct device * dev )
126
177
{
127
178
struct ili9340_data * data = (struct ili9340_data * )dev -> driver_data ;
128
179
129
- LOG_DBG ("Turning display on " );
180
+ LOG_DBG ("Turning display blanking off " );
130
181
ili9340_transmit (data , ILI9340_CMD_DISPLAY_ON , NULL , 0 );
182
+ return 0 ;
131
183
}
132
184
133
- void ili9340_display_off ( struct device * dev )
185
+ static int ili9340_display_blanking_on ( const struct device * dev )
134
186
{
135
187
struct ili9340_data * data = (struct ili9340_data * )dev -> driver_data ;
136
188
137
- LOG_DBG ("Turning display off " );
189
+ LOG_DBG ("Turning display blanking on " );
138
190
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 ;
139
237
}
140
238
141
239
void ili9340_transmit (struct ili9340_data * data , u8_t cmd , void * tx_data ,
142
240
size_t tx_len )
143
241
{
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 };
146
244
147
245
gpio_pin_write (data -> command_data_gpio , CONFIG_ILI9340_CMD_DATA_PIN ,
148
246
ILI9340_CMD_DATA_PIN_COMMAND );
@@ -158,27 +256,21 @@ void ili9340_transmit(struct ili9340_data *data, u8_t cmd, void *tx_data,
158
256
}
159
257
}
160
258
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
+ };
180
271
181
272
static struct ili9340_data ili9340_data ;
182
273
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 );
0 commit comments