eGUI alias D4D  Release 3.0
Reference Manual
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
d4dlcdhw_mqx_spi.c
Go to the documentation of this file.
1 /**************************************************************************
2 *
3 * Copyright 2014 by Petr Gargulak. eGUI Community.
4 * Copyright 2009-2013 by Petr Gargulak. Freescale Semiconductor, Inc.
5 *
6 ***************************************************************************
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License Version 3
9 * or later (the "LGPL").
10 *
11 * As a special exception, the copyright holders of the eGUI project give you
12 * permission to link the eGUI sources with independent modules to produce an
13 * executable, regardless of the license terms of these independent modules,
14 * and to copy and distribute the resulting executable under terms of your
15 * choice, provided that you also meet, for each linked independent module,
16 * the terms and conditions of the license of that module.
17 * An independent module is a module which is not derived from or based
18 * on this library.
19 * If you modify the eGUI sources, you may extend this exception
20 * to your version of the eGUI sources, but you are not obligated
21 * to do so. If you do not wish to do so, delete this
22 * exception statement from your version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 *
28 * You should have received a copy of the GNU General Public License
29 * and the GNU Lesser General Public License along with this program.
30 * If not, see <http://www.gnu.org/licenses/>.
31 *
32 ***************************************************************************/
46 #include "d4d.h" // include of all public items (types, function etc) of D4D driver
47 #include "common_files/d4d_lldapi.h" // include non public low level driver interface header file (types, function prototypes, enums etc. )
48 #include "common_files/d4d_private.h" // include the private header file that contains perprocessor macros as D4D_MK_STR
49 
50 
51 // identification string of driver - must be same as name D4DTCH_FUNCTIONS structure + "_ID"
52 // it is used for enable the code for compilation
53 #define d4dlcdhw_mqx_spi_ID 1
54 
55 
56 // copilation enable preprocessor condition
57 // the string d4dtch_Mqx_ID must be replaced by define created one line up
58 #if (D4D_MK_STR(D4D_LLD_LCD_HW) == d4dlcdhw_mqx_spi_ID)
59 
60 // include of low level driver heaser file
61 // it will be included into wole project only in case that this driver is selected in main D4D configuration file
62 #include <string.h>
63 #include <mqx.h>
64 #include <bsp.h>
65 #include <spi.h>
66 
67 #ifndef BSP_LCD_SPI_CHANNEL
68  #error Define BSP_LCD_SPI_CHANNEL in bsp.
69 #endif
70 
71 #ifndef BSP_LCD_DC_MUX_GPIO
72  #ifndef BSP_LCD_DC_FN
73  #define BSP_LCD_DC_MUX_GPIO 1
74  #warning Is not defined BSP_LCD_DC_MUX_GPIO, please check if it's OK default value 1!
75  #else
76  #define BSP_LCD_DC_MUX_GPIO BSP_LCD_DC_FN
77  #endif
78 #endif
79 
80 
81 #define INIT_OK 1
82 #define INIT_GPIO_FAILED 2
83 #define INIT_SPI_FAILED 3
84 #define INIT_SPI_SETTINGS_FAILED 4
85 /******************************************************************************
86 * Macros
87 ******************************************************************************/
88 #if D4D_COLOR_SYSTEM != D4D_COLOR_SYSTEM_RGB565
89  #error The eGUI low level driver "d4dlcdhw_mqx_spi" not supported selected type of D4D_COLOR_SYSTEM. To run this driver just select D4D_COLOR_SYSTEM_RGB565.
90 #endif
91 /******************************************************************************
92 * Internal function prototypes
93 ******************************************************************************/
94 
95 static unsigned char D4DLCDHW_Init_Mqx(void);
96 static unsigned char D4DLCDHW_DeInit_Mqx(void);
97 static void D4DLCDHW_SendDataWord_Mqx(unsigned short value);
98 static void D4DLCDHW_SendCmdWord_Mqx(unsigned short cmd);
99 static unsigned short D4DLCDHW_ReadDataWord_Mqx(void);
100 static unsigned short D4DLCDHW_ReadComWord_Mqx(void);
101 static unsigned char D4DLCDHW_PinCtl_Mqx(D4DLCDHW_PINS pinId, D4DHW_PIN_STATE setState);
102 static void D4DLCD_FlushBuffer_Mqx(D4DLCD_FLUSH_MODE mode);
103 
104 /**************************************************************/
109 static FILE_PTR spi_fd;
110 
111 static LWGPIO_STRUCT lcd_dc_pin;
112 
113 #if defined BSP_LCD_RESET
114  static LWGPIO_STRUCT lcd_reset_pin;
115 #endif
116 
117 #if defined BSP_LCD_BACKLIGHT
118  static LWGPIO_STRUCT lcd_backlight_pin;
119 #endif
120 
121 // the main structure that contains low level driver api functions
122 // the name fo this structure is used for recognizing of configured low level driver of whole D4D
123 // so this name has to be used in main configuration header file of D4D driver to enable this driver
124 const D4DLCDHW_FUNCTIONS d4dlcdhw_mqx_spi =
125 {
126  D4DLCDHW_Init_Mqx,
127  D4DLCDHW_SendDataWord_Mqx,
128  D4DLCDHW_SendCmdWord_Mqx,
129  D4DLCDHW_ReadDataWord_Mqx,
130  D4DLCDHW_ReadComWord_Mqx,
131  D4DLCDHW_PinCtl_Mqx,
132  D4DLCD_FlushBuffer_Mqx,
133  D4DLCDHW_DeInit_Mqx,
134 };
135 /**************************************************************/
141 /**************************************************************/
148 //-----------------------------------------------------------------------------
149 // FUNCTION: D4DLCDHW_Init_Mqx
150 // SCOPE: Low Level Driver API function
151 // DESCRIPTION: The function is used for initialization of this low level driver
152 //
153 // PARAMETERS: none
154 //
155 // RETURNS: none
156 //
157 //-----------------------------------------------------------------------------
158 static unsigned char D4DLCDHW_Init_Mqx(void)
159 {
160  uint_32 param;
161  unsigned char result = INIT_OK;
162 
163  /* initialize lwgpio handle (lcd_dc_pin) for BSP_LCD_DC pin (defined in mqx/source/bsp/<bsp_name>/<bsp_name>.h file) */
164  if (!lwgpio_init(&lcd_dc_pin, BSP_LCD_DC, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_LOW))
165  {
166  printf("Initializing LCD_DC GPIO as output failed.\n");
167  return D4D_FALSE;
168  }
169  /* swich pin functionality (MUX) to GPIO mode */
170  lwgpio_set_functionality(&lcd_dc_pin, BSP_LCD_DC_MUX_GPIO);
171 
172  #if defined BSP_LCD_RESET
173  /* initialize lwgpio handle (lcd_reset_pin) for BSP_LCD_RESET pin (defined in mqx/source/bsp/<bsp_name>/<bsp_name>.h file) */
174  if (!lwgpio_init(&lcd_reset_pin, BSP_LCD_RESET, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_HIGH))
175  {
176  printf("Initializing LCD_RESET GPIO as output failed.\n");
177  return D4D_FALSE;
178  }
179  /* swich pin functionality (MUX) to GPIO mode */
180  lwgpio_set_functionality(&lcd_reset_pin, BSP_LCD_RESET_MUX_GPIO);
181  #endif
182 
183  #if defined BSP_LCD_BACKLIGHT
184  /* initialize lwgpio handle (lcd_backlight_pin) for BSP_LCD_BACKLIGHT pin (defined in mqx/source/bsp/<bsp_name>/<bsp_name>.h file) */
185  if (!lwgpio_init(&lcd_backlight_pin, BSP_LCD_BACKLIGHT, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_LOW))
186  {
187  printf("Initializing LCD_BACKLIGHT GPIO as output failed.\n");
188  return D4D_FALSE;
189  }
190  /* swich pin functionality (MUX) to GPIO mode */
191  lwgpio_set_functionality(&lcd_backlight_pin, BSP_LCD_BACKLIGHT_MUX_GPIO);
192  #endif
193 
194  /* Open the SPI controller */
195  spi_fd = fopen (BSP_LCD_SPI_CHANNEL, NULL);
196  if (NULL == spi_fd)
197  {
198  result = INIT_SPI_FAILED;
199  }
200 
201  /* Set clock mode */
202  param = SPI_CLK_POL_PHA_MODE0;
203  if (SPI_OK != ioctl (spi_fd, IO_IOCTL_SPI_SET_MODE, &param))
204  {
205  result = INIT_SPI_SETTINGS_FAILED;
206  }
207 
208  /* Set big endian */
209  param = SPI_DEVICE_BIG_ENDIAN;
210  if (SPI_OK != ioctl (spi_fd, IO_IOCTL_SPI_SET_ENDIAN, &param))
211  {
212  result = INIT_SPI_SETTINGS_FAILED;
213  }
214 
215  /* Set transfer mode */
216  param = SPI_DEVICE_MASTER_MODE;
217  if (SPI_OK != ioctl (spi_fd, IO_IOCTL_SPI_SET_TRANSFER_MODE, &param))
218  {
219  result = INIT_SPI_SETTINGS_FAILED;
220  }
221 
222  /* Set framesize == 16 */
223  param = 16;
224 
225  if (SPI_OK != ioctl (spi_fd, IO_IOCTL_SPI_SET_FRAMESIZE, &param))
226  {
227  result = INIT_SPI_SETTINGS_FAILED;
228  }
229 
230 // /* Set baud rate */
231 // param = 25000000; /* 25 MHz - max baudrate for Kinetis */
232 // if (SPI_OK != ioctl (spi_fd, IO_IOCTL_SPI_SET_BAUD, &param))
233 // {
234 // result = INIT_SPI_SETTINGS_FAILED;
235 // }
236 
237  return result;
238 }
239 
240 //-----------------------------------------------------------------------------
241 // FUNCTION: D4DLCDHW_DeInit_Template
242 // SCOPE: Low Level Driver API function
243 // DESCRIPTION: The function is used for deinitialization of this low level driver
244 //
245 // PARAMETERS: none
246 //
247 // RETURNS: result: 1 - Success
248 // 0 - Failed
249 //-----------------------------------------------------------------------------
250 static unsigned char D4DLCDHW_DeInit_Mqx(void)
251 {
252  return 1;
253 }
254 
255 
256 //-----------------------------------------------------------------------------
257 // FUNCTION: D4DLCDHW_SendDataWord_Mqx
258 // SCOPE: Low Level Driver API function
259 // DESCRIPTION: The function send the one 16 bit variable into LCD
260 //
261 // PARAMETERS: unsigned short value variable to send
262 //
263 // RETURNS: none
264 //
265 //-----------------------------------------------------------------------------
266 static void D4DLCDHW_SendDataWord_Mqx(unsigned short value)
267 {
268  /* need to switch byte order */
269  unsigned short p_color = ((value & 0xFF00) >> 8) | (( value & 0xFF ) << 8) ;
270  (void)fwrite ( &p_color, 2, 1, spi_fd);
271 }
272 
273 //-----------------------------------------------------------------------------
274 // FUNCTION: D4DLCDHW_SendCmdWord_Mqx
275 // SCOPE: Low Level Driver API function
276 // DESCRIPTION: The function send the one 16 bit command into LCD
277 //
278 // PARAMETERS: unsigned short cmd command to send
279 //
280 // RETURNS: none
281 //
282 //-----------------------------------------------------------------------------
283 static void D4DLCDHW_SendCmdWord_Mqx(unsigned short cmd)
284 {
285  /* DC to 0 */
286  lwgpio_set_value(&lcd_dc_pin, LWGPIO_VALUE_LOW);
287 
288  D4DLCDHW_SendDataWord_Mqx( cmd );
289 
290  /* DC to 1 */
291  lwgpio_set_value(&lcd_dc_pin, LWGPIO_VALUE_HIGH);
292 }
293 
294 
295 //-----------------------------------------------------------------------------
296 // FUNCTION: D4DLCDHW_ReadDataWord_Template
297 // SCOPE: Low Level Driver API function
298 // DESCRIPTION: The function reads the one 16 bit variable from LCD (if this function is supported)
299 //
300 // PARAMETERS: none
301 //
302 // RETURNS: unsigned short - the readed value
303 //
304 //-----------------------------------------------------------------------------
305 static unsigned short D4DLCDHW_ReadDataWord_Mqx(void)
306 {
307  return 1;
308 }
309 
310 
311 //-----------------------------------------------------------------------------
312 // FUNCTION: D4DLCDHW_ReadCmdWord_Template
313 // SCOPE: Low Level Driver API function
314 // DESCRIPTION: The function reads the one 16 bit command from LCD (if this function is supported)
315 //
316 // PARAMETERS: none
317 //
318 // RETURNS: unsigned short - the readed value
319 //
320 //-----------------------------------------------------------------------------
321 static unsigned short D4DLCDHW_ReadComWord_Mqx(void)
322 {
323  return 1;
324 }
325 
326 //-----------------------------------------------------------------------------
327 // FUNCTION: D4DLCDHW_PinCtl_Mqx
328 // SCOPE: Low Level Driver API function
329 // DESCRIPTION: allows control GPIO pins for LCD conrol purposes
330 //
331 // PARAMETERS: D4DLCDHW_PINS pinId - Pin identification
332 // D4DHW_PIN_STATE setState - Pin action
333 // RETURNS: for Get action retuns the pin value
334 //-----------------------------------------------------------------------------
335 static unsigned char D4DLCDHW_PinCtl_Mqx(D4DLCDHW_PINS pinId, D4DHW_PIN_STATE setState)
336 {
337  switch( pinId )
338  {
339  case D4DLCD_RESET_PIN:
340  #if defined BSP_LCD_RESET
341  switch( setState )
342  {
343  case D4DHW_PIN_SET_1:
344  lwgpio_set_value(&lcd_reset_pin, LWGPIO_VALUE_HIGH);
345  break;
346 
347  case D4DHW_PIN_SET_0:
348  lwgpio_set_value(&lcd_reset_pin, LWGPIO_VALUE_LOW);
349  break;
350 
351  default:
352  break;
353  }
354  #endif
355  break;
356 
358  #if defined BSP_LCD_BACKLIGHT
359  switch( setState )
360  {
361  case D4DHW_PIN_SET_1:
362  lwgpio_set_value(&lcd_backlight_pin, LWGPIO_VALUE_HIGH);
363  break;
364 
365  case D4DHW_PIN_SET_0:
366  lwgpio_set_value(&lcd_backlight_pin, LWGPIO_VALUE_LOW);
367  break;
368 
369  default:
370  break;
371  }
372  #endif
373  break;
374 
375  default:
376  break;
377  }
378  return 1;
379 }
380 
381 //-----------------------------------------------------------------------------
382 // FUNCTION: D4DLCD_FlushBuffer_Mqx
383 // SCOPE: Low Level Driver API function
384 // DESCRIPTION: For buffered low level interfaces is used to inform
385 // driver the complete object is drawed and pending pixels should be flushed
386 //
387 // PARAMETERS: none
388 //
389 // RETURNS: none
390 //-----------------------------------------------------------------------------
391 static void D4DLCD_FlushBuffer_Mqx(D4DLCD_FLUSH_MODE mode)
392 {
393  D4D_UNUSED(mode);
394 }
395 
396 #endif //(D4D_MK_STR(D4D_LLD_LCD_HW) == d4dlcdhw_Mqx_ID)
D4D low level standard LCD Hardware interface API structure.
Definition: d4d_lldapi.h:163
#define D4D_FALSE
This is definition of boolean operation value in eGUI - FALSE.
Definition: d4d_types.h:104
D4D Driver main header file.
D4DHW_PIN_STATE
D4D low level MCU pin state enumeration type.
Definition: d4d_lldapi.h:73
D4DLCD_FLUSH_MODE
D4D low level eGUI flush screen types enumeration.
Definition: d4d_lldapi.h:136
D4D Driver private header file.
D4DLCDHW_PINS
D4D low level MCU types definition for general LCD screen control signals.
Definition: d4d_lldapi.h:96
#define NULL
Type definition of null pointer.
Definition: d4d_types.h:184
Set pin output register to logic 1.
Definition: d4d_lldapi.h:80
LCD device backlight enable signal.
Definition: d4d_lldapi.h:99
#define D4D_UNUSED(x)
Macro used just for notify compiler that the input parameter is not used.
Definition: d4d_base.h:504
Set pin output register to logic 0.
Definition: d4d_lldapi.h:79
D4D driver - resistive touch screen driver function header file.
LCD device reset signal.
Definition: d4d_lldapi.h:98