]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MX_MPLAB/lcd.c
Update version number.
[freertos] / FreeRTOS / Demo / PIC32MX_MPLAB / lcd.c
1 /*\r
2     FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /* peripheral library include */\r
66 #include <plib.h>\r
67 \r
68 /* Scheduler includes. */\r
69 #include "FreeRTOS.h"\r
70 #include "task.h"\r
71 #include "queue.h"\r
72 \r
73 /* Demo includes. */\r
74 #include "lcd.h"\r
75 \r
76 /*\r
77  * The LCD is written to by more than one task so is controlled by this\r
78  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
79  * access the LCD directly.  Other tasks wanting to display a message send\r
80  * the message to the gatekeeper.\r
81  */\r
82 static void vLCDTask( void *pvParameters );\r
83 \r
84 /*\r
85  * Setup the peripherals required to communicate with the LCD.\r
86  */\r
87 static void prvSetupLCD( void );\r
88 \r
89 /* \r
90  * Move to the first (0) or second (1) row of the LCD. \r
91  */\r
92 static void prvLCDGotoRow( unsigned short usRow );\r
93 \r
94 /* \r
95  * Write a string of text to the LCD. \r
96  */\r
97 static void prvLCDPutString( char *pcString );\r
98 \r
99 /* \r
100  * Clear the LCD. \r
101  */\r
102 static void prvLCDClear( void );\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 /* Brief delay to permit the LCD to catch up with commands. */\r
107 #define lcdVERY_SHORT_DELAY     ( 1 )\r
108 #define lcdSHORT_DELAY          ( 4 / portTICK_RATE_MS )\r
109 #define lcdLONG_DELAY           ( 15 / portTICK_RATE_MS )\r
110 \r
111 /* LCD specific definitions. */\r
112 #define LCD_CLEAR_DISPLAY_CMD                   0x01\r
113 #define LCD_CURSOR_HOME_CMD                             0x02\r
114 #define LCD_ENTRY_MODE_CMD                              0x04\r
115 #define LCD_ENTRY_MODE_INCREASE                 0x02\r
116 #define LCD_DISPLAY_CTRL_CMD                    0x08\r
117 #define LCD_DISPLAY_CTRL_DISPLAY_ON             0x04\r
118 #define LCD_FUNCTION_SET_CMD                    0x20\r
119 #define LCD_FUNCTION_SET_8_BITS                 0x10\r
120 #define LCD_FUNCTION_SET_2_LINES                0x08\r
121 #define LCD_FUNCTION_SET_LRG_FONT               0x04\r
122 #define LCD_NEW_LINE                                    0xC0\r
123 #define LCD_COMMAND_ADDRESS                             0x00\r
124 #define LCD_DATA_ADDRESS                                0x01\r
125 \r
126 /* The length of the queue used to send messages to the LCD gatekeeper task. */\r
127 #define lcdQUEUE_SIZE           3\r
128 \r
129 /*-----------------------------------------------------------*/\r
130 \r
131 /* The queue used to send messages to the LCD task. */\r
132 xQueueHandle xLCDQueue;\r
133 \r
134 /* LCD access functions. */\r
135 static void prvLCDCommand( char cCommand );\r
136 static void prvLCDData( char cChar );\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 \r
140 xQueueHandle xStartLCDTask( void )\r
141 {\r
142         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
143         are received via this queue. */\r
144         xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ));\r
145 \r
146         /* Start the task that will write to the LCD.  The LCD hardware is\r
147         initialised from within the task itself so delays can be used. */\r
148         xTaskCreate( vLCDTask, ( signed char * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );\r
149 \r
150         return xLCDQueue;\r
151 }\r
152 /*-----------------------------------------------------------*/\r
153 \r
154 static void prvLCDGotoRow( unsigned short usRow )\r
155 {\r
156         if(usRow == 0) \r
157         {\r
158                 prvLCDCommand( LCD_CURSOR_HOME_CMD );\r
159         } \r
160         else \r
161         {\r
162                 prvLCDCommand( LCD_NEW_LINE );\r
163         }\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 static void prvLCDCommand( char cCommand ) \r
168 {\r
169         PMPSetAddress( LCD_COMMAND_ADDRESS );\r
170         PMPMasterWrite( cCommand );\r
171         vTaskDelay( lcdSHORT_DELAY );\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 static void prvLCDData( char cChar )\r
176 {\r
177         PMPSetAddress( LCD_DATA_ADDRESS );\r
178         PMPMasterWrite( cChar );\r
179         vTaskDelay( lcdVERY_SHORT_DELAY );\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 static void prvLCDPutString( char *pcString )\r
184 {\r
185         /* Write out each character with appropriate delay between each. */\r
186         while(*pcString)\r
187         {\r
188                 prvLCDData(*pcString);\r
189                 pcString++;\r
190                 vTaskDelay(lcdSHORT_DELAY);\r
191         }\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 static void prvLCDClear(void)\r
196 {\r
197         prvLCDCommand(LCD_CLEAR_DISPLAY_CMD);\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 static void prvSetupLCD(void)\r
202 {\r
203         /* Wait for proper power up. */\r
204         vTaskDelay( lcdLONG_DELAY );\r
205         \r
206         /* Open the PMP port */\r
207         mPMPOpen((PMP_ON | PMP_READ_WRITE_EN | PMP_CS2_CS1_EN |\r
208                           PMP_LATCH_POL_HI | PMP_CS2_POL_HI | PMP_CS1_POL_HI |\r
209                           PMP_WRITE_POL_HI | PMP_READ_POL_HI),\r
210                          (PMP_MODE_MASTER1 | PMP_WAIT_BEG_4 | PMP_WAIT_MID_15 |\r
211                           PMP_WAIT_END_4),\r
212                           PMP_PEN_0, 0);\r
213                          \r
214         /* Wait for the LCD to power up correctly. */\r
215         vTaskDelay( lcdLONG_DELAY );\r
216         vTaskDelay( lcdLONG_DELAY );\r
217         vTaskDelay( lcdLONG_DELAY );\r
218 \r
219         /* Set up the LCD function. */\r
220         prvLCDCommand( LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS | LCD_FUNCTION_SET_2_LINES | LCD_FUNCTION_SET_LRG_FONT );\r
221         \r
222         /* Turn the display on. */\r
223         prvLCDCommand( LCD_DISPLAY_CTRL_CMD | LCD_DISPLAY_CTRL_DISPLAY_ON );\r
224         \r
225         /* Clear the display. */\r
226         prvLCDCommand( LCD_CLEAR_DISPLAY_CMD );\r
227         vTaskDelay( lcdLONG_DELAY );    \r
228         \r
229         /* Increase the cursor. */\r
230         prvLCDCommand( LCD_ENTRY_MODE_CMD | LCD_ENTRY_MODE_INCREASE );\r
231         vTaskDelay( lcdLONG_DELAY );                    \r
232         vTaskDelay( lcdLONG_DELAY );                    \r
233         vTaskDelay( lcdLONG_DELAY );\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 static void vLCDTask(void *pvParameters)\r
238 {\r
239 xLCDMessage xMessage;\r
240 unsigned short usRow = 0;\r
241 \r
242         /* Initialise the hardware.  This uses delays so must not be called prior\r
243         to the scheduler being started. */\r
244         prvSetupLCD();\r
245 \r
246         /* Welcome message. */\r
247         prvLCDPutString( "www.FreeRTOS.org" );\r
248 \r
249         for(;;)\r
250         {\r
251                 /* Wait for a message to arrive that requires displaying. */\r
252                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
253 \r
254                 /* Clear the current display value. */\r
255                 prvLCDClear();\r
256 \r
257                 /* Switch rows each time so we can see that the display is still being\r
258                 updated. */\r
259                 prvLCDGotoRow( usRow & 0x01 );\r
260                 usRow++;\r
261                 prvLCDPutString( xMessage.pcMessage );\r
262 \r
263                 /* Delay the requested amount of time to ensure the text just written \r
264                 to the LCD is not overwritten. */\r
265                 vTaskDelay( xMessage.xMinDisplayTime );         \r
266         }\r
267 }\r
268 \r
269 \r
270 \r
271 \r