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