2 FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /* Scheduler includes. */
\r
71 #include "FreeRTOS.h"
\r
76 /* Hardware specifics. */
\r
77 #include "iodefine.h"
\r
80 /* States used by the LCD tasks. */
\r
81 #define lcdRIGHT_TO_LEFT 0U
\r
82 #define lcdLEFT_TO_RIGHT 1U
\r
83 #define lcdRUNNING 0U
\r
85 /* Characters on each line. */
\r
86 #define lcdSTRING_LEN 8
\r
88 /* Commands sent from the IRQ to the task controlling the second line of the
\r
90 #define lcdSHIFT_BACK_COMMAND 0x01U
\r
91 #define lcdSTART_STOP_COMMAND 0x02U
\r
92 #define lcdSHIFT_FORWARD_COMMAND 0x03U
\r
94 /* The length of the queue used to send commands from the ISRs. */
\r
95 #define lcdCOMMAND_QUEUE_LENGTH 32U
\r
97 /* Defines the minimum time that must pass between consecutive button presses
\r
98 to accept a button press as a unique press rather than just a bounce. */
\r
99 #define lcdMIN_TIME_BETWEEN_INTERRUPTS_MS ( 125UL / portTICK_PERIOD_MS )
\r
101 /* Button interrupt handlers. */
\r
102 #pragma interrupt ( prvIRQ1_Handler( vect = 65, enable ) )
\r
103 static void prvIRQ1_Handler( void );
\r
105 #pragma interrupt ( prvIRQ3_Handler( vect = 67, enable ) )
\r
106 static void prvIRQ3_Handler( void );
\r
108 #pragma interrupt ( prvIRQ4_Handler(vect = 68, enable ) )
\r
109 static void prvIRQ4_Handler( void );
\r
112 * Setup the IO needed for the buttons to generate interrupts.
\r
114 static void prvSetupButtonIOAndInterrupts( void );
\r
117 * A task that simply scrolls a string from left to right, then back from the
\r
118 * right to the left. This is done on the first line of the display.
\r
120 static void prvLCDTaskLine1( void *pvParameters );
\r
123 * If no buttons are pushed, then this task acts as per prvLCDTaskLine1(), but
\r
124 * using the second line of the display.
\r
126 * Using the buttons, it is possible to start and stop the scrolling of the
\r
127 * text. Once the scrolling has been stopped, other buttons can be used to
\r
128 * manually scroll the text either left or right.
\r
130 static void prvLCDTaskLine2( void *pvParameters );
\r
133 * Looks at the direction the string is currently being scrolled in, and moves
\r
134 * the index into the portion of the string that can be seen on the display
\r
135 * either forward or backward as appropriate.
\r
137 static prvScrollString( unsigned char *pucDirection, unsigned short *pusPosition, size_t xStringLength );
\r
140 * Displays lcdSTRING_LEN characters starting from pcString on the line of the
\r
141 * display requested by ucLine.
\r
143 static void prvDisplayNextString( unsigned char ucLine, char *pcString );
\r
146 * Called from the IRQ interrupts, which are generated on button pushes. Send
\r
147 * ucCommand to the task on the button command queue if
\r
148 * lcdMIN_TIME_BETWEEN_INTERRUPTS_MS milliseconds have passed since the button
\r
149 * was last pushed (for debouncing).
\r
151 static portBASE_TYPE prvSendCommandOnDebouncedInput( TickType_t *pxTimeLastInterrupt, unsigned char ucCommand );
\r
153 /*-----------------------------------------------------------*/
\r
155 /* The queue used to pass commands from the button interrupt handlers to the
\r
156 prvLCDTaskLine2() task. */
\r
157 static QueueHandle_t xButtonCommandQueue = NULL;
\r
159 /* The mutex used to ensure only one task writes to the display at any one
\r
161 static SemaphoreHandle_t xLCDMutex = NULL;
\r
163 /* The string that is scrolled up and down the first line of the display. */
\r
164 static const char cDataString1[] = " http://www.FreeRTOS.org ";
\r
166 /* The string that is scrolled/nudged up and down the second line of the
\r
168 static const char cDataString2[] = "........Rx210 Highlights....1.56 DMips/MHz....DSP functions....1.62V-5.5V operation....200 uA/MHz....Up to 512 kBytes Flash....up to 64 kbytes SRAM....EE Dataflash with 100k w/e....1.3 uA in Real Time Clock Mode....Powerful Motor control timer....4 x 16-bit timers....4 x 8-bit timers....Full Real Time Clock calendar with calibration and alarm functions....Up to 16 channels 1 uS 12-bit ADC, with Dual group programmable SCAN, 3 sample and holds, sample accumulate function....DMA controller....Data Transfer Controller....Up to 9 serial Channels....Up to 6 USARTs ( with Simple I2C / SPI )....USART ( with unique Frame based protocol support )....Multimaster IIC....RSPI....Temperature Sensor....Event Link Controller....Comparators....Safety features include CRC....March X....Dual watchdog Timers with window and independent oscillator....ADC self test....I/O Pin Test....Supported with E1 on chip debugger and RSK210 evaluation system....Rx210 Highlights........";
\r
170 /* Structures passed into the two tasks to inform them which line to use on the
\r
171 display, how long to delay for, and which string to use. */
\r
172 struct _LCD_Params xLCDLine1 =
\r
174 LCD_LINE1, 215, ( char * ) cDataString1
\r
177 struct _LCD_Params xLCDLine2 =
\r
179 LCD_LINE2, 350, ( char * ) cDataString2
\r
183 /*-----------------------------------------------------------*/
\r
185 void vStartButtonAndLCDDemo( void )
\r
187 prvSetupButtonIOAndInterrupts();
\r
188 InitialiseDisplay();
\r
190 /* Create the mutex used to guard the LCD. */
\r
191 xLCDMutex = xSemaphoreCreateMutex();
\r
192 configASSERT( xLCDMutex );
\r
194 /* Create the queue used to pass commands from the IRQ interrupts to the
\r
195 prvLCDTaskLine2() task. */
\r
196 xButtonCommandQueue = xQueueCreate( lcdCOMMAND_QUEUE_LENGTH, sizeof( unsigned char ) );
\r
197 configASSERT( xButtonCommandQueue );
\r
199 /* Start the two tasks as described at the top of this file. */
\r
200 xTaskCreate( prvLCDTaskLine1, "LCD1", configMINIMAL_STACK_SIZE * 3, ( void * ) &xLCDLine1, tskIDLE_PRIORITY + 1, NULL );
\r
201 xTaskCreate( prvLCDTaskLine2, "LCD2", configMINIMAL_STACK_SIZE * 3, ( void * ) &xLCDLine2, tskIDLE_PRIORITY + 2, NULL );
\r
203 /*-----------------------------------------------------------*/
\r
205 static void prvLCDTaskLine1( void *pvParameters )
\r
207 struct _LCD_Params *pxLCDParamaters = ( struct _LCD_Params * ) pvParameters;
\r
208 unsigned short usPosition = 0U;
\r
209 unsigned char ucDirection = lcdRIGHT_TO_LEFT;
\r
213 vTaskDelay( pxLCDParamaters->Speed / portTICK_PERIOD_MS );
\r
215 /* Write the string. */
\r
216 prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );
\r
218 /* Move the string in whichever direction the scroll is currently going
\r
220 prvScrollString( &ucDirection, &usPosition, strlen( pxLCDParamaters->ptr_str ) );
\r
223 /*-----------------------------------------------------------*/
\r
225 static void prvLCDTaskLine2( void *pvParameters )
\r
227 struct _LCD_Params *pxLCDParamaters = ( struct _LCD_Params * ) pvParameters;
\r
228 unsigned short usPosition = 0U;
\r
229 unsigned char ucDirection = lcdRIGHT_TO_LEFT, ucStatus = lcdRUNNING, ucQueueData;
\r
230 TickType_t xDelayTicks = ( pxLCDParamaters->Speed / portTICK_PERIOD_MS );
\r
234 /* Wait for a message from an IRQ handler. */
\r
235 if( xQueueReceive( xButtonCommandQueue, &ucQueueData, xDelayTicks ) != pdPASS )
\r
237 /* A message was not received before xDelayTicks ticks passed, so
\r
238 generate the next string to display and display it. */
\r
239 prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );
\r
241 /* Move the string in whichever direction the scroll is currently
\r
243 prvScrollString( &ucDirection, &usPosition, strlen( pxLCDParamaters->ptr_str ) );
\r
247 /* A command was received. Process it. */
\r
248 switch( ucQueueData )
\r
250 case lcdSTART_STOP_COMMAND :
\r
252 /* If the LCD is running, top it. If the LCD is stopped, start
\r
254 ucStatus = !ucStatus;
\r
256 if( ucStatus == lcdRUNNING )
\r
258 xDelayTicks = ( pxLCDParamaters->Speed / portTICK_PERIOD_MS );
\r
262 xDelayTicks = portMAX_DELAY;
\r
267 case lcdSHIFT_BACK_COMMAND :
\r
269 if( ucStatus != lcdRUNNING )
\r
271 /* If not already at the start of the display.... */
\r
272 if( usPosition != 0U )
\r
274 /* ....move the display position back by one char. */
\r
276 prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );
\r
282 case lcdSHIFT_FORWARD_COMMAND :
\r
284 if( ucStatus != lcdRUNNING )
\r
286 /* If not already at the end of the display.... */
\r
287 if( usPosition != ( strlen( pxLCDParamaters->ptr_str ) - ( lcdSTRING_LEN - 1 ) ) )
\r
289 /* ....move the display position forward by one
\r
292 prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );
\r
300 /*-----------------------------------------------------------*/
\r
302 void prvSetupButtonIOAndInterrupts( void )
\r
304 /* Configure SW 1-3 pin settings */
\r
305 PORT3.PDR.BIT.B1 = 0; /* Switch 1 - Port 3.1 - IRQ1 */
\r
306 PORT3.PDR.BIT.B3 = 0; /* Switch 2 - Port 3.3 - IRQ3 */
\r
307 PORT3.PDR.BIT.B4 = 0; /* Switch 3 - Port 3.4 - IRQ4 */
\r
309 PORT3.PMR.BIT.B1 = 1;
\r
310 PORT3.PMR.BIT.B3 = 1;
\r
311 PORT3.PMR.BIT.B4 = 1;
\r
313 MPC.PWPR.BIT.B0WI = 0; /* Writing to the PFSWE bit is enabled */
\r
314 MPC.PWPR.BIT.PFSWE = 1; /* Writing to the PFS register is enabled */
\r
315 MPC.P31PFS.BIT.ISEL = 1;
\r
316 MPC.P33PFS.BIT.ISEL = 1;
\r
317 MPC.P34PFS.BIT.ISEL = 1;
\r
320 ICU.IER[ 0x08 ].BIT.IEN1 = 1;
\r
321 ICU.IPR[ 65 ].BIT.IPR = configMAX_SYSCALL_INTERRUPT_PRIORITY - 1;
\r
322 ICU.IR[ 65 ].BIT.IR = 0;
\r
323 ICU.IRQCR[ 1 ].BIT.IRQMD = 2; /* Rising edge */
\r
326 ICU.IER[ 0x08 ].BIT.IEN3 = 1;
\r
327 ICU.IPR[ 67 ].BIT.IPR = configMAX_SYSCALL_INTERRUPT_PRIORITY - 1;
\r
328 ICU.IR[ 67 ].BIT.IR = 0;
\r
329 ICU.IRQCR[ 3 ].BIT.IRQMD = 2; /* Rising edge */
\r
332 ICU.IER[ 0x08 ].BIT.IEN4 = 1;
\r
333 ICU.IPR[ 68 ].BIT.IPR = configMAX_SYSCALL_INTERRUPT_PRIORITY - 1;
\r
334 ICU.IR[ 68 ].BIT.IR = 0;
\r
335 ICU.IRQCR[ 4 ].BIT.IRQMD = 2; /* Rising edge */
\r
337 /*-----------------------------------------------------------*/
\r
339 static prvScrollString( unsigned char *pucDirection, unsigned short *pusPosition, size_t xStringLength )
\r
341 /* Check which way to scroll. */
\r
342 if( *pucDirection == lcdRIGHT_TO_LEFT )
\r
344 /* Move to the next character. */
\r
345 ( *pusPosition )++;
\r
347 /* Has the end of the string been reached? */
\r
348 if( ( *pusPosition ) == ( xStringLength - ( lcdSTRING_LEN - 1 ) ) )
\r
350 /* Switch direction. */
\r
351 *pucDirection = lcdLEFT_TO_RIGHT;
\r
352 ( *pusPosition )--;
\r
357 /* Move (backward) to the next character. */
\r
358 ( *pusPosition )--;
\r
359 if( *pusPosition == 0U )
\r
361 /* Switch Direction. */
\r
362 *pucDirection = lcdRIGHT_TO_LEFT;
\r
366 /*-----------------------------------------------------------*/
\r
368 static void prvDisplayNextString( unsigned char ucLine, char *pcString )
\r
370 static char cSingleLine[ lcdSTRING_LEN + 1 ];
\r
372 xSemaphoreTake( xLCDMutex, portMAX_DELAY );
\r
373 strncpy( cSingleLine, pcString, lcdSTRING_LEN );
\r
374 DisplayString( ucLine, cSingleLine );
\r
375 xSemaphoreGive( xLCDMutex );
\r
377 /*-----------------------------------------------------------*/
\r
379 static portBASE_TYPE prvSendCommandOnDebouncedInput( TickType_t *pxTimeLastInterrupt, unsigned char ucCommand )
\r
381 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
382 TickType_t xCurrentTickCount;
\r
384 /* Check the time now for debouncing purposes. */
\r
385 xCurrentTickCount = xTaskGetTickCountFromISR();
\r
387 /* Has enough time passed since the button was last push to accept it as a
\r
389 if( ( xCurrentTickCount - *pxTimeLastInterrupt ) > lcdMIN_TIME_BETWEEN_INTERRUPTS_MS )
\r
391 xQueueSendToBackFromISR( xButtonCommandQueue, &ucCommand, &xHigherPriorityTaskWoken );
\r
394 /* Remember the time now, so debounce can be performed again on the next
\r
396 *pxTimeLastInterrupt = xCurrentTickCount;
\r
398 return xHigherPriorityTaskWoken;
\r
400 /*-----------------------------------------------------------*/
\r
402 static void prvIRQ1_Handler( void )
\r
404 static TickType_t xTimeLastInterrupt = 0UL;
\r
405 static const unsigned char ucCommand = lcdSHIFT_BACK_COMMAND;
\r
406 portBASE_TYPE xHigherPriorityTaskWoken;
\r
408 xHigherPriorityTaskWoken = prvSendCommandOnDebouncedInput( &xTimeLastInterrupt, ucCommand );
\r
409 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
411 /*-----------------------------------------------------------*/
\r
413 static void prvIRQ3_Handler(void)
\r
415 static TickType_t xTimeLastInterrupt = 0UL;
\r
416 static const unsigned char ucCommand = lcdSTART_STOP_COMMAND;
\r
417 portBASE_TYPE xHigherPriorityTaskWoken;
\r
419 xHigherPriorityTaskWoken = prvSendCommandOnDebouncedInput( &xTimeLastInterrupt, ucCommand );
\r
420 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
422 /*-----------------------------------------------------------*/
\r
424 static void prvIRQ4_Handler(void)
\r
426 static TickType_t xTimeLastInterrupt = 0UL;
\r
427 static const unsigned char ucCommand = lcdSHIFT_FORWARD_COMMAND;
\r
428 portBASE_TYPE xHigherPriorityTaskWoken;
\r
430 xHigherPriorityTaskWoken = prvSendCommandOnDebouncedInput( &xTimeLastInterrupt, ucCommand );
\r
431 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r