2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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 /* peripheral library include */
\r
31 /* Scheduler includes. */
\r
32 #include "FreeRTOS.h"
\r
36 /* Demo includes. */
\r
40 * The LCD is written to by more than one task so is controlled by this
\r
41 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
42 * access the LCD directly. Other tasks wanting to display a message send
\r
43 * the message to the gatekeeper.
\r
45 static void vLCDTask( void *pvParameters );
\r
48 * Setup the peripherals required to communicate with the LCD.
\r
50 static void prvSetupLCD( void );
\r
53 * Move to the first (0) or second (1) row of the LCD.
\r
55 static void prvLCDGotoRow( unsigned short usRow );
\r
58 * Write a string of text to the LCD.
\r
60 static void prvLCDPutString( char *pcString );
\r
65 static void prvLCDClear( void );
\r
67 /*-----------------------------------------------------------*/
\r
69 /* Brief delay to permit the LCD to catch up with commands. */
\r
70 #define lcdVERY_SHORT_DELAY ( 1 )
\r
71 #define lcdSHORT_DELAY ( 8 / portTICK_PERIOD_MS )
\r
72 #define lcdLONG_DELAY ( 15 / portTICK_PERIOD_MS )
\r
74 /* LCD specific definitions. */
\r
75 #define LCD_CLEAR_DISPLAY_CMD 0x01
\r
76 #define LCD_CURSOR_HOME_CMD 0x02
\r
77 #define LCD_ENTRY_MODE_CMD 0x04
\r
78 #define LCD_ENTRY_MODE_INCREASE 0x02
\r
79 #define LCD_DISPLAY_CTRL_CMD 0x08
\r
80 #define LCD_DISPLAY_CTRL_DISPLAY_ON 0x04
\r
81 #define LCD_FUNCTION_SET_CMD 0x20
\r
82 #define LCD_FUNCTION_SET_8_BITS 0x10
\r
83 #define LCD_FUNCTION_SET_2_LINES 0x08
\r
84 #define LCD_FUNCTION_SET_LRG_FONT 0x04
\r
85 #define LCD_NEW_LINE 0xC0
\r
86 #define LCD_COMMAND_ADDRESS 0x00
\r
87 #define LCD_DATA_ADDRESS 0x01
\r
89 /* The length of the queue used to send messages to the LCD gatekeeper task. */
\r
90 #define lcdQUEUE_SIZE 3
\r
92 /*-----------------------------------------------------------*/
\r
94 /* The queue used to send messages to the LCD task. */
\r
95 QueueHandle_t xLCDQueue;
\r
97 /* LCD access functions. */
\r
98 static void prvLCDCommand( char cCommand );
\r
99 static void prvLCDData( char cChar );
\r
101 /*-----------------------------------------------------------*/
\r
103 QueueHandle_t xStartLCDTask( void )
\r
105 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
106 are received via this queue. */
\r
107 xLCDQueue = xQueueCreate( lcdQUEUE_SIZE, sizeof( xLCDMessage ));
\r
109 /* Start the task that will write to the LCD. The LCD hardware is
\r
110 initialised from within the task itself so delays can be used. */
\r
111 xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );
\r
115 /*-----------------------------------------------------------*/
\r
117 static void prvLCDGotoRow( unsigned short usRow )
\r
121 prvLCDCommand( LCD_CURSOR_HOME_CMD );
\r
125 prvLCDCommand( LCD_NEW_LINE );
\r
128 /*-----------------------------------------------------------*/
\r
130 static void prvLCDCommand( char cCommand )
\r
132 PMPSetAddress( LCD_COMMAND_ADDRESS );
\r
133 PMPMasterWrite( cCommand );
\r
134 vTaskDelay( lcdSHORT_DELAY );
\r
136 /*-----------------------------------------------------------*/
\r
138 static void prvLCDData( char cChar )
\r
140 PMPSetAddress( LCD_DATA_ADDRESS );
\r
141 PMPMasterWrite( cChar );
\r
142 vTaskDelay( lcdVERY_SHORT_DELAY );
\r
144 /*-----------------------------------------------------------*/
\r
146 static void prvLCDPutString( char *pcString )
\r
148 /* Write out each character with appropriate delay between each. */
\r
151 prvLCDData(*pcString);
\r
153 vTaskDelay(lcdSHORT_DELAY);
\r
156 /*-----------------------------------------------------------*/
\r
158 static void prvLCDClear(void)
\r
160 prvLCDCommand(LCD_CLEAR_DISPLAY_CMD);
\r
162 /*-----------------------------------------------------------*/
\r
164 static void prvSetupLCD(void)
\r
166 /* Wait for proper power up. */
\r
167 vTaskDelay( lcdLONG_DELAY );
\r
169 /* Open the PMP port */
\r
170 mPMPOpen((PMP_ON | PMP_READ_WRITE_EN | PMP_CS2_CS1_EN |
\r
171 PMP_LATCH_POL_HI | PMP_CS2_POL_HI | PMP_CS1_POL_HI |
\r
172 PMP_WRITE_POL_HI | PMP_READ_POL_HI),
\r
173 (PMP_MODE_MASTER1 | PMP_WAIT_BEG_4 | PMP_WAIT_MID_15 |
\r
177 /* Wait for the LCD to power up correctly. */
\r
178 vTaskDelay( lcdLONG_DELAY );
\r
179 vTaskDelay( lcdLONG_DELAY );
\r
180 vTaskDelay( lcdLONG_DELAY );
\r
182 /* Set up the LCD function. */
\r
183 prvLCDCommand( LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS | LCD_FUNCTION_SET_2_LINES | LCD_FUNCTION_SET_LRG_FONT );
\r
185 /* Turn the display on. */
\r
186 prvLCDCommand( LCD_DISPLAY_CTRL_CMD | LCD_DISPLAY_CTRL_DISPLAY_ON );
\r
188 /* Clear the display. */
\r
189 prvLCDCommand( LCD_CLEAR_DISPLAY_CMD );
\r
190 vTaskDelay( lcdLONG_DELAY );
\r
192 /* Increase the cursor. */
\r
193 prvLCDCommand( LCD_ENTRY_MODE_CMD | LCD_ENTRY_MODE_INCREASE );
\r
194 vTaskDelay( lcdLONG_DELAY );
\r
195 vTaskDelay( lcdLONG_DELAY );
\r
196 vTaskDelay( lcdLONG_DELAY );
\r
198 /*-----------------------------------------------------------*/
\r
200 static void vLCDTask(void *pvParameters)
\r
202 xLCDMessage xMessage;
\r
203 unsigned short usRow = 0;
\r
205 /* Initialise the hardware. This uses delays so must not be called prior
\r
206 to the scheduler being started. */
\r
209 /* Welcome message. */
\r
210 prvLCDPutString( "www.FreeRTOS.org" );
\r
214 /* Wait for a message to arrive that requires displaying. */
\r
215 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
217 /* Clear the current display value. */
\r
220 /* Switch rows each time so we can see that the display is still being
\r
222 prvLCDGotoRow( usRow & 0x01 );
\r
224 prvLCDPutString( xMessage.pcMessage );
\r
226 /* Delay the requested amount of time to ensure the text just written
\r
227 to the LCD is not overwritten. */
\r
228 vTaskDelay( xMessage.xMinDisplayTime );
\r