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