2 FreeRTOS V7.5.3 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\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
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
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\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
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
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
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\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
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
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
66 /* Scheduler includes. */
\r
67 #include "FreeRTOS.h"
\r
71 /* Demo includes. */
\r
75 * The LCD is written to by more than one task so is controlled by this
\r
76 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
77 * access the LCD directly. Other tasks wanting to display a message send
\r
78 * the message to the gatekeeper.
\r
80 static void vLCDTask( void *pvParameters );
\r
83 * Setup the peripherals required to communicate with the LCD.
\r
85 static void prvSetupLCD( void );
\r
88 * Move to the first (0) or second (1) row of the LCD.
\r
90 static void prvLCDGotoRow( unsigned portSHORT usRow );
\r
93 * Write a string of text to the LCD.
\r
95 static void prvLCDPutString( portCHAR *pcString );
\r
100 static void prvLCDClear( void );
\r
102 /*-----------------------------------------------------------*/
\r
104 /* Brief delay to permit the LCD to catch up with commands. */
\r
105 #define lcdVERY_SHORT_DELAY ( 1 )
\r
106 #define lcdSHORT_DELAY ( 4 / portTICK_RATE_MS )
\r
107 #define lcdLONG_DELAY ( 15 / portTICK_RATE_MS )
\r
109 /* LCD commands. */
\r
110 #define lcdCLEAR ( 0x01 )
\r
111 #define lcdHOME ( 0x02 )
\r
112 #define lcdLINE2 ( 0xc0 )
\r
114 /* SFR that seems to be missing from the standard header files. */
\r
115 #define PMAEN *( ( unsigned short * ) 0x60c )
\r
117 /* LCD R/W signal. */
\r
118 #define lcdRW LATDbits.LATD5
\r
120 /* LCD lcdRS signal. */
\r
121 #define lcdRS LATBbits.LATB15
\r
123 /* LCD lcdE signal . */
\r
124 #define lcdE LATDbits.LATD4
\r
126 /* Control signal pin direction. */
\r
127 #define RW_TRIS TRISDbits.TRISD5
\r
128 #define RS_TRIS TRISBbits.TRISB15
\r
129 #define E_TRIS TRISDbits.TRISD4
\r
131 /* Port for LCD data */
\r
132 #define lcdDATA LATE
\r
133 #define lcdDATAPORT PORTE
\r
135 /* I/O setup for data Port. */
\r
136 #define TRISDATA TRISE
\r
138 /* The length of the queue used to send messages to the LCD gatekeeper task. */
\r
139 #define lcdQUEUE_SIZE 3
\r
140 /*-----------------------------------------------------------*/
\r
142 /* The queue used to send messages to the LCD task. */
\r
143 xQueueHandle xLCDQueue;
\r
145 static void prvLCDCommand( portCHAR cCommand );
\r
146 static void prvLCDData( portCHAR cChar );
\r
148 /*-----------------------------------------------------------*/
\r
150 xQueueHandle xStartLCDTask( void )
\r
152 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
153 are received via this queue. */
\r
154 xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );
\r
156 /* Start the task that will write to the LCD. The LCD hardware is
\r
157 initialised from within the task itself so delays can be used. */
\r
158 xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
162 /*-----------------------------------------------------------*/
\r
164 static void prvLCDGotoRow( unsigned portSHORT usRow )
\r
168 prvLCDCommand( lcdHOME );
\r
172 prvLCDCommand( lcdLINE2 );
\r
175 /*-----------------------------------------------------------*/
\r
177 static void prvLCDCommand( portCHAR cCommand )
\r
179 /* Prepare RD0 - RD7. */
\r
180 lcdDATA &= 0xFF00;
\r
182 /* Command byte to lcd. */
\r
183 lcdDATA |= cCommand;
\r
185 /* Ensure lcdRW is 0. */
\r
189 /* Toggle lcdE line. */
\r
191 vTaskDelay( lcdVERY_SHORT_DELAY );
\r
194 vTaskDelay( lcdSHORT_DELAY );
\r
196 /*-----------------------------------------------------------*/
\r
198 static void prvLCDData( portCHAR cChar )
\r
200 /* ensure lcdRW is 0. */
\r
203 /* Assert register select to 1. */
\r
206 /* Prepare RD0 - RD7. */
\r
207 lcdDATA &= 0xFF00;
\r
209 /* Data byte to lcd. */
\r
216 /* Toggle lcdE signal. */
\r
219 /* Negate register select to 0. */
\r
222 vTaskDelay( lcdVERY_SHORT_DELAY );
\r
224 /*-----------------------------------------------------------*/
\r
226 static void prvLCDPutString( portCHAR *pcString )
\r
228 /* Write out each character with appropriate delay between each. */
\r
231 prvLCDData( *pcString );
\r
233 vTaskDelay( lcdSHORT_DELAY );
\r
236 /*-----------------------------------------------------------*/
\r
238 static void prvLCDClear( void )
\r
240 prvLCDCommand( lcdCLEAR );
\r
242 /*-----------------------------------------------------------*/
\r
244 static void prvSetupLCD( void )
\r
246 /* Wait for proper power up. */
\r
247 vTaskDelay( lcdLONG_DELAY );
\r
249 /* Set initial states for the data and control pins */
\r
252 /* R/W state set low. */
\r
255 /* lcdRS state set low. */
\r
258 /* lcdE state set low. */
\r
261 /* Set data and control pins to outputs */
\r
264 /* lcdRW pin set as output. */
\r
267 /* lcdRS pin set as output. */
\r
270 /* lcdE pin set as output. */
\r
273 /* 1st LCD initialization sequence */
\r
281 /* Toggle lcdE signal. */
\r
284 vTaskDelay( lcdSHORT_DELAY );
\r
285 vTaskDelay( lcdSHORT_DELAY );
\r
286 vTaskDelay( lcdSHORT_DELAY );
\r
288 /* 2nd LCD initialization sequence */
\r
296 /* Toggle lcdE signal. */
\r
299 vTaskDelay( lcdSHORT_DELAY );
\r
301 /* 3rd LCD initialization sequence */
\r
309 /* Toggle lcdE signal. */
\r
312 vTaskDelay( lcdSHORT_DELAY );
\r
315 /* Function set. */
\r
316 prvLCDCommand( 0x38 );
\r
318 /* Display on/off control, cursor blink off (0x0C). */
\r
319 prvLCDCommand( 0x0C );
\r
321 /* Entry mode set (0x06). */
\r
322 prvLCDCommand( 0x06 );
\r
324 prvLCDCommand( lcdCLEAR );
\r
326 /*-----------------------------------------------------------*/
\r
328 static void vLCDTask( void *pvParameters )
\r
330 xLCDMessage xMessage;
\r
331 unsigned portSHORT usRow = 0;
\r
333 /* Initialise the hardware. This uses delays so must not be called prior
\r
334 to the scheduler being started. */
\r
337 /* Welcome message. */
\r
338 prvLCDPutString( "www.FreeRTOS.org" );
\r
342 /* Wait for a message to arrive that requires displaying. */
\r
343 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
345 /* Clear the current display value. */
\r
348 /* Switch rows each time so we can see that the display is still being
\r
350 prvLCDGotoRow( usRow & 0x01 );
\r
352 prvLCDPutString( xMessage.pcMessage );
\r
354 /* Delay the requested amount of time to ensure the text just written
\r
355 to the LCD is not overwritten. */
\r
356 vTaskDelay( xMessage.xMinDisplayTime );
\r