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