2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\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
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\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
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\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
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\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
69 /* Scheduler includes. */
\r
70 #include "FreeRTOS.h"
\r
74 /* Demo includes. */
\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
83 static void vLCDTask( void *pvParameters );
\r
86 * Setup the peripherals required to communicate with the LCD.
\r
88 static void prvSetupLCD( void );
\r
91 * Move to the first (0) or second (1) row of the LCD.
\r
93 static void prvLCDGotoRow( unsigned portSHORT usRow );
\r
96 * Write a string of text to the LCD.
\r
98 static void prvLCDPutString( portCHAR *pcString );
\r
103 static void prvLCDClear( void );
\r
105 /*-----------------------------------------------------------*/
\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 ( 4 / portTICK_RATE_MS )
\r
110 #define lcdLONG_DELAY ( 15 / portTICK_RATE_MS )
\r
112 /* LCD commands. */
\r
113 #define lcdCLEAR ( 0x01 )
\r
114 #define lcdHOME ( 0x02 )
\r
115 #define lcdLINE2 ( 0xc0 )
\r
117 /* SFR that seems to be missing from the standard header files. */
\r
118 #define PMAEN *( ( unsigned short * ) 0x60c )
\r
120 /* LCD R/W signal. */
\r
121 #define lcdRW LATDbits.LATD5
\r
123 /* LCD lcdRS signal. */
\r
124 #define lcdRS LATBbits.LATB15
\r
126 /* LCD lcdE signal . */
\r
127 #define lcdE LATDbits.LATD4
\r
129 /* Control signal pin direction. */
\r
130 #define RW_TRIS TRISDbits.TRISD5
\r
131 #define RS_TRIS TRISBbits.TRISB15
\r
132 #define E_TRIS TRISDbits.TRISD4
\r
134 /* Port for LCD data */
\r
135 #define lcdDATA LATE
\r
136 #define lcdDATAPORT PORTE
\r
138 /* I/O setup for data Port. */
\r
139 #define TRISDATA TRISE
\r
141 /* The length of the queue used to send messages to the LCD gatekeeper task. */
\r
142 #define lcdQUEUE_SIZE 3
\r
143 /*-----------------------------------------------------------*/
\r
145 /* The queue used to send messages to the LCD task. */
\r
146 xQueueHandle xLCDQueue;
\r
148 static void prvLCDCommand( portCHAR cCommand );
\r
149 static void prvLCDData( portCHAR cChar );
\r
151 /*-----------------------------------------------------------*/
\r
153 xQueueHandle xStartLCDTask( void )
\r
155 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
156 are received via this queue. */
\r
157 xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );
\r
159 /* Start the task that will write to the LCD. The LCD hardware is
\r
160 initialised from within the task itself so delays can be used. */
\r
161 xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
165 /*-----------------------------------------------------------*/
\r
167 static void prvLCDGotoRow( unsigned portSHORT usRow )
\r
171 prvLCDCommand( lcdHOME );
\r
175 prvLCDCommand( lcdLINE2 );
\r
178 /*-----------------------------------------------------------*/
\r
180 static void prvLCDCommand( portCHAR cCommand )
\r
182 /* Prepare RD0 - RD7. */
\r
183 lcdDATA &= 0xFF00;
\r
185 /* Command byte to lcd. */
\r
186 lcdDATA |= cCommand;
\r
188 /* Ensure lcdRW is 0. */
\r
192 /* Toggle lcdE line. */
\r
194 vTaskDelay( lcdVERY_SHORT_DELAY );
\r
197 vTaskDelay( lcdSHORT_DELAY );
\r
199 /*-----------------------------------------------------------*/
\r
201 static void prvLCDData( portCHAR cChar )
\r
203 /* ensure lcdRW is 0. */
\r
206 /* Assert register select to 1. */
\r
209 /* Prepare RD0 - RD7. */
\r
210 lcdDATA &= 0xFF00;
\r
212 /* Data byte to lcd. */
\r
219 /* Toggle lcdE signal. */
\r
222 /* Negate register select to 0. */
\r
225 vTaskDelay( lcdVERY_SHORT_DELAY );
\r
227 /*-----------------------------------------------------------*/
\r
229 static void prvLCDPutString( portCHAR *pcString )
\r
231 /* Write out each character with appropriate delay between each. */
\r
234 prvLCDData( *pcString );
\r
236 vTaskDelay( lcdSHORT_DELAY );
\r
239 /*-----------------------------------------------------------*/
\r
241 static void prvLCDClear( void )
\r
243 prvLCDCommand( lcdCLEAR );
\r
245 /*-----------------------------------------------------------*/
\r
247 static void prvSetupLCD( void )
\r
249 /* Wait for proper power up. */
\r
250 vTaskDelay( lcdLONG_DELAY );
\r
252 /* Set initial states for the data and control pins */
\r
255 /* R/W state set low. */
\r
258 /* lcdRS state set low. */
\r
261 /* lcdE state set low. */
\r
264 /* Set data and control pins to outputs */
\r
267 /* lcdRW pin set as output. */
\r
270 /* lcdRS pin set as output. */
\r
273 /* lcdE pin set as output. */
\r
276 /* 1st LCD initialization sequence */
\r
284 /* Toggle lcdE signal. */
\r
287 vTaskDelay( lcdSHORT_DELAY );
\r
288 vTaskDelay( lcdSHORT_DELAY );
\r
289 vTaskDelay( lcdSHORT_DELAY );
\r
291 /* 2nd LCD initialization sequence */
\r
299 /* Toggle lcdE signal. */
\r
302 vTaskDelay( lcdSHORT_DELAY );
\r
304 /* 3rd LCD initialization sequence */
\r
312 /* Toggle lcdE signal. */
\r
315 vTaskDelay( lcdSHORT_DELAY );
\r
318 /* Function set. */
\r
319 prvLCDCommand( 0x38 );
\r
321 /* Display on/off control, cursor blink off (0x0C). */
\r
322 prvLCDCommand( 0x0C );
\r
324 /* Entry mode set (0x06). */
\r
325 prvLCDCommand( 0x06 );
\r
327 prvLCDCommand( lcdCLEAR );
\r
329 /*-----------------------------------------------------------*/
\r
331 static void vLCDTask( void *pvParameters )
\r
333 xLCDMessage xMessage;
\r
334 unsigned portSHORT usRow = 0;
\r
336 /* Initialise the hardware. This uses delays so must not be called prior
\r
337 to the scheduler being started. */
\r
340 /* Welcome message. */
\r
341 prvLCDPutString( "www.FreeRTOS.org" );
\r
345 /* Wait for a message to arrive that requires displaying. */
\r
346 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
348 /* Clear the current display value. */
\r
351 /* Switch rows each time so we can see that the display is still being
\r
353 prvLCDGotoRow( usRow & 0x01 );
\r
355 prvLCDPutString( xMessage.pcMessage );
\r
357 /* Delay the requested amount of time to ensure the text just written
\r
358 to the LCD is not overwritten. */
\r
359 vTaskDelay( xMessage.xMinDisplayTime );
\r