2 * FreeRTOS Kernel V10.2.0
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* Scheduler includes. */
\r
29 #include "FreeRTOS.h"
\r
33 /* Demo includes. */
\r
37 * The LCD is written to by more than one task so is controlled by this
\r
38 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
39 * access the LCD directly. Other tasks wanting to display a message send
\r
40 * the message to the gatekeeper.
\r
42 static void vLCDTask( void *pvParameters );
\r
45 * Setup the peripherals required to communicate with the LCD.
\r
47 static void prvSetupLCD( void );
\r
50 * Move to the first (0) or second (1) row of the LCD.
\r
52 static void prvLCDGotoRow( unsigned short usRow );
\r
55 * Write a string of text to the LCD.
\r
57 static void prvLCDPutString( char *pcString );
\r
62 static void prvLCDClear( void );
\r
64 /*-----------------------------------------------------------*/
\r
66 /* Brief delay to permit the LCD to catch up with commands. */
\r
67 #define lcdVERY_SHORT_DELAY ( 1 )
\r
68 #define lcdSHORT_DELAY ( 4 / portTICK_PERIOD_MS )
\r
69 #define lcdLONG_DELAY ( 15 / portTICK_PERIOD_MS )
\r
72 #define lcdCLEAR ( 0x01 )
\r
73 #define lcdHOME ( 0x02 )
\r
74 #define lcdLINE2 ( 0xc0 )
\r
76 /* SFR that seems to be missing from the standard header files. */
\r
77 #define PMAEN *( ( unsigned short * ) 0x60c )
\r
79 /* LCD R/W signal. */
\r
80 #define lcdRW LATDbits.LATD5
\r
82 /* LCD lcdRS signal. */
\r
83 #define lcdRS LATBbits.LATB15
\r
85 /* LCD lcdE signal . */
\r
86 #define lcdE LATDbits.LATD4
\r
88 /* Control signal pin direction. */
\r
89 #define RW_TRIS TRISDbits.TRISD5
\r
90 #define RS_TRIS TRISBbits.TRISB15
\r
91 #define E_TRIS TRISDbits.TRISD4
\r
93 /* Port for LCD data */
\r
94 #define lcdDATA LATE
\r
95 #define lcdDATAPORT PORTE
\r
97 /* I/O setup for data Port. */
\r
98 #define TRISDATA TRISE
\r
100 /* The length of the queue used to send messages to the LCD gatekeeper task. */
\r
101 #define lcdQUEUE_SIZE 3
\r
102 /*-----------------------------------------------------------*/
\r
104 /* The queue used to send messages to the LCD task. */
\r
105 QueueHandle_t xLCDQueue;
\r
107 static void prvLCDCommand( char cCommand );
\r
108 static void prvLCDData( char cChar );
\r
110 /*-----------------------------------------------------------*/
\r
112 QueueHandle_t xStartLCDTask( void )
\r
114 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
115 are received via this queue. */
\r
116 xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ) );
\r
118 /* Start the task that will write to the LCD. The LCD hardware is
\r
119 initialised from within the task itself so delays can be used. */
\r
120 xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
124 /*-----------------------------------------------------------*/
\r
126 static void prvLCDGotoRow( unsigned short usRow )
\r
130 prvLCDCommand( lcdHOME );
\r
134 prvLCDCommand( lcdLINE2 );
\r
137 /*-----------------------------------------------------------*/
\r
139 static void prvLCDCommand( char cCommand )
\r
141 /* Prepare RD0 - RD7. */
\r
144 /* Command byte to lcd. */
\r
145 lcdDATA |= cCommand;
\r
147 /* Ensure lcdRW is 0. */
\r
151 /* Toggle lcdE line. */
\r
153 vTaskDelay( lcdVERY_SHORT_DELAY );
\r
156 vTaskDelay( lcdSHORT_DELAY );
\r
158 /*-----------------------------------------------------------*/
\r
160 static void prvLCDData( char cChar )
\r
162 /* ensure lcdRW is 0. */
\r
165 /* Assert register select to 1. */
\r
168 /* Prepare RD0 - RD7. */
\r
171 /* Data byte to lcd. */
\r
178 /* Toggle lcdE signal. */
\r
181 /* Negate register select to 0. */
\r
184 vTaskDelay( lcdVERY_SHORT_DELAY );
\r
186 /*-----------------------------------------------------------*/
\r
188 static void prvLCDPutString( char *pcString )
\r
190 /* Write out each character with appropriate delay between each. */
\r
193 prvLCDData( *pcString );
\r
195 vTaskDelay( lcdSHORT_DELAY );
\r
198 /*-----------------------------------------------------------*/
\r
200 static void prvLCDClear( void )
\r
202 prvLCDCommand( lcdCLEAR );
\r
204 /*-----------------------------------------------------------*/
\r
206 static void prvSetupLCD( void )
\r
208 /* Wait for proper power up. */
\r
209 vTaskDelay( lcdLONG_DELAY );
\r
211 /* Set initial states for the data and control pins */
\r
214 /* R/W state set low. */
\r
217 /* lcdRS state set low. */
\r
220 /* lcdE state set low. */
\r
223 /* Set data and control pins to outputs */
\r
226 /* lcdRW pin set as output. */
\r
229 /* lcdRS pin set as output. */
\r
232 /* lcdE pin set as output. */
\r
235 /* 1st LCD initialization sequence */
\r
243 /* Toggle lcdE signal. */
\r
246 vTaskDelay( lcdSHORT_DELAY );
\r
247 vTaskDelay( lcdSHORT_DELAY );
\r
248 vTaskDelay( lcdSHORT_DELAY );
\r
250 /* 2nd LCD initialization sequence */
\r
258 /* Toggle lcdE signal. */
\r
261 vTaskDelay( lcdSHORT_DELAY );
\r
263 /* 3rd LCD initialization sequence */
\r
271 /* Toggle lcdE signal. */
\r
274 vTaskDelay( lcdSHORT_DELAY );
\r
277 /* Function set. */
\r
278 prvLCDCommand( 0x38 );
\r
280 /* Display on/off control, cursor blink off (0x0C). */
\r
281 prvLCDCommand( 0x0C );
\r
283 /* Entry mode set (0x06). */
\r
284 prvLCDCommand( 0x06 );
\r
286 prvLCDCommand( lcdCLEAR );
\r
288 /*-----------------------------------------------------------*/
\r
290 static void vLCDTask( void *pvParameters )
\r
292 xLCDMessage xMessage;
\r
293 unsigned short usRow = 0;
\r
295 /* Initialise the hardware. This uses delays so must not be called prior
\r
296 to the scheduler being started. */
\r
299 /* Welcome message. */
\r
300 prvLCDPutString( "www.FreeRTOS.org" );
\r
304 /* Wait for a message to arrive that requires displaying. */
\r
305 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
307 /* Clear the current display value. */
\r
310 /* Switch rows each time so we can see that the display is still being
\r
312 prvLCDGotoRow( usRow & 0x01 );
\r
314 prvLCDPutString( xMessage.pcMessage );
\r
316 /* Delay the requested amount of time to ensure the text just written
\r
317 to the LCD is not overwritten. */
\r
318 vTaskDelay( xMessage.xMinDisplayTime );
\r