2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* peripheral library include */
\r
32 /* Scheduler includes. */
\r
33 #include "FreeRTOS.h"
\r
37 /* Demo includes. */
\r
41 * The LCD is written to by more than one task so is controlled by this
\r
42 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
43 * access the LCD directly. Other tasks wanting to display a message send
\r
44 * the message to the gatekeeper.
\r
46 static void vLCDTask( void *pvParameters );
\r
49 * Setup the peripherals required to communicate with the LCD.
\r
51 static void prvSetupLCD( void );
\r
54 * Move to the first (0) or second (1) row of the LCD.
\r
56 static void prvLCDGotoRow( unsigned short usRow );
\r
59 * Write a string of text to the LCD.
\r
61 static void prvLCDPutString( char *pcString );
\r
66 static void prvLCDClear( void );
\r
68 /*-----------------------------------------------------------*/
\r
70 /* Brief delay to permit the LCD to catch up with commands. */
\r
71 #define lcdVERY_SHORT_DELAY ( 1 )
\r
72 #define lcdSHORT_DELAY ( 8 / portTICK_PERIOD_MS )
\r
73 #define lcdLONG_DELAY ( 15 / portTICK_PERIOD_MS )
\r
75 /* LCD specific definitions. */
\r
76 #define LCD_CLEAR_DISPLAY_CMD 0x01
\r
77 #define LCD_CURSOR_HOME_CMD 0x02
\r
78 #define LCD_ENTRY_MODE_CMD 0x04
\r
79 #define LCD_ENTRY_MODE_INCREASE 0x02
\r
80 #define LCD_DISPLAY_CTRL_CMD 0x08
\r
81 #define LCD_DISPLAY_CTRL_DISPLAY_ON 0x04
\r
82 #define LCD_FUNCTION_SET_CMD 0x20
\r
83 #define LCD_FUNCTION_SET_8_BITS 0x10
\r
84 #define LCD_FUNCTION_SET_2_LINES 0x08
\r
85 #define LCD_FUNCTION_SET_LRG_FONT 0x04
\r
86 #define LCD_NEW_LINE 0xC0
\r
87 #define LCD_COMMAND_ADDRESS 0x00
\r
88 #define LCD_DATA_ADDRESS 0x01
\r
90 /* The length of the queue used to send messages to the LCD gatekeeper task. */
\r
91 #define lcdQUEUE_SIZE 3
\r
93 /*-----------------------------------------------------------*/
\r
95 /* The queue used to send messages to the LCD task. */
\r
96 QueueHandle_t xLCDQueue;
\r
98 /* LCD access functions. */
\r
99 static void prvLCDCommand( char cCommand );
\r
100 static void prvLCDData( char cChar );
\r
102 /*-----------------------------------------------------------*/
\r
104 QueueHandle_t xStartLCDTask( void )
\r
106 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
107 are received via this queue. */
\r
108 xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ));
\r
110 /* Start the task that will write to the LCD. The LCD hardware is
\r
111 initialised from within the task itself so delays can be used. */
\r
112 xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
116 /*-----------------------------------------------------------*/
\r
118 static void prvLCDGotoRow( unsigned short usRow )
\r
122 prvLCDCommand( LCD_CURSOR_HOME_CMD );
\r
126 prvLCDCommand( LCD_NEW_LINE );
\r
129 /*-----------------------------------------------------------*/
\r
131 static void prvLCDCommand( char cCommand )
\r
133 PMPSetAddress( LCD_COMMAND_ADDRESS );
\r
134 PMPMasterWrite( cCommand );
\r
135 vTaskDelay( lcdSHORT_DELAY );
\r
137 /*-----------------------------------------------------------*/
\r
139 static void prvLCDData( char cChar )
\r
141 PMPSetAddress( LCD_DATA_ADDRESS );
\r
142 PMPMasterWrite( cChar );
\r
143 vTaskDelay( lcdVERY_SHORT_DELAY );
\r
145 /*-----------------------------------------------------------*/
\r
147 static void prvLCDPutString( char *pcString )
\r
149 /* Write out each character with appropriate delay between each. */
\r
152 prvLCDData(*pcString);
\r
154 vTaskDelay(lcdSHORT_DELAY);
\r
157 /*-----------------------------------------------------------*/
\r
159 static void prvLCDClear(void)
\r
161 prvLCDCommand(LCD_CLEAR_DISPLAY_CMD);
\r
163 /*-----------------------------------------------------------*/
\r
165 static void prvSetupLCD(void)
\r
167 /* Wait for proper power up. */
\r
168 vTaskDelay( lcdLONG_DELAY );
\r
170 /* Open the PMP port */
\r
171 mPMPOpen((PMP_ON | PMP_READ_WRITE_EN | PMP_CS2_CS1_EN |
\r
172 PMP_LATCH_POL_HI | PMP_CS2_POL_HI | PMP_CS1_POL_HI |
\r
173 PMP_WRITE_POL_HI | PMP_READ_POL_HI),
\r
174 (PMP_MODE_MASTER1 | PMP_WAIT_BEG_4 | PMP_WAIT_MID_15 |
\r
178 /* Wait for the LCD to power up correctly. */
\r
179 vTaskDelay( lcdLONG_DELAY );
\r
180 vTaskDelay( lcdLONG_DELAY );
\r
181 vTaskDelay( lcdLONG_DELAY );
\r
183 /* Set up the LCD function. */
\r
184 prvLCDCommand( LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS | LCD_FUNCTION_SET_2_LINES | LCD_FUNCTION_SET_LRG_FONT );
\r
186 /* Turn the display on. */
\r
187 prvLCDCommand( LCD_DISPLAY_CTRL_CMD | LCD_DISPLAY_CTRL_DISPLAY_ON );
\r
189 /* Clear the display. */
\r
190 prvLCDCommand( LCD_CLEAR_DISPLAY_CMD );
\r
191 vTaskDelay( lcdLONG_DELAY );
\r
193 /* Increase the cursor. */
\r
194 prvLCDCommand( LCD_ENTRY_MODE_CMD | LCD_ENTRY_MODE_INCREASE );
\r
195 vTaskDelay( lcdLONG_DELAY );
\r
196 vTaskDelay( lcdLONG_DELAY );
\r
197 vTaskDelay( lcdLONG_DELAY );
\r
199 /*-----------------------------------------------------------*/
\r
201 static void vLCDTask(void *pvParameters)
\r
203 xLCDMessage xMessage;
\r
204 unsigned short usRow = 0;
\r
206 /* Initialise the hardware. This uses delays so must not be called prior
\r
207 to the scheduler being started. */
\r
210 /* Welcome message. */
\r
211 prvLCDPutString( "www.FreeRTOS.org" );
\r
215 /* Wait for a message to arrive that requires displaying. */
\r
216 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
218 /* Clear the current display value. */
\r
221 /* Switch rows each time so we can see that the display is still being
\r
223 prvLCDGotoRow( usRow & 0x01 );
\r
225 prvLCDPutString( xMessage.pcMessage );
\r
227 /* Delay the requested amount of time to ensure the text just written
\r
228 to the LCD is not overwritten. */
\r
229 vTaskDelay( xMessage.xMinDisplayTime );
\r