2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
96 /* Scheduler includes. */
\r
97 #include "FreeRTOS.h"
\r
100 #include "semphr.h"
\r
102 /* Hardware specifics. */
\r
103 #include "iodefine.h"
\r
106 /* States used by the LCD tasks. */
\r
107 #define lcdRIGHT_TO_LEFT 0U
\r
108 #define lcdLEFT_TO_RIGHT 1U
\r
109 #define lcdRUNNING 0U
\r
111 /* Characters on each line. */
\r
112 #define lcdSTRING_LEN 8
\r
114 /* Commands sent from the IRQ to the task controlling the second line of the
\r
116 #define lcdSHIFT_BACK_COMMAND 0x01U
\r
117 #define lcdSTART_STOP_COMMAND 0x02U
\r
118 #define lcdSHIFT_FORWARD_COMMAND 0x03U
\r
120 /* The length of the queue used to send commands from the ISRs. */
\r
121 #define lcdCOMMAND_QUEUE_LENGTH 32U
\r
123 /* Defines the minimum time that must pass between consecutive button presses
\r
124 to accept a button press as a unique press rather than just a bounce. */
\r
125 #define lcdMIN_TIME_BETWEEN_INTERRUPTS_MS ( 125UL / portTICK_PERIOD_MS )
\r
127 /* Button interrupt handlers. */
\r
128 #pragma interrupt ( prvIRQ1_Handler( vect = 65, enable ) )
\r
129 static void prvIRQ1_Handler( void );
\r
131 #pragma interrupt ( prvIRQ3_Handler( vect = 67, enable ) )
\r
132 static void prvIRQ3_Handler( void );
\r
134 #pragma interrupt ( prvIRQ4_Handler(vect = 68, enable ) )
\r
135 static void prvIRQ4_Handler( void );
\r
138 * Setup the IO needed for the buttons to generate interrupts.
\r
140 static void prvSetupButtonIOAndInterrupts( void );
\r
143 * A task that simply scrolls a string from left to right, then back from the
\r
144 * right to the left. This is done on the first line of the display.
\r
146 static void prvLCDTaskLine1( void *pvParameters );
\r
149 * If no buttons are pushed, then this task acts as per prvLCDTaskLine1(), but
\r
150 * using the second line of the display.
\r
152 * Using the buttons, it is possible to start and stop the scrolling of the
\r
153 * text. Once the scrolling has been stopped, other buttons can be used to
\r
154 * manually scroll the text either left or right.
\r
156 static void prvLCDTaskLine2( void *pvParameters );
\r
159 * Looks at the direction the string is currently being scrolled in, and moves
\r
160 * the index into the portion of the string that can be seen on the display
\r
161 * either forward or backward as appropriate.
\r
163 static prvScrollString( unsigned char *pucDirection, unsigned short *pusPosition, size_t xStringLength );
\r
166 * Displays lcdSTRING_LEN characters starting from pcString on the line of the
\r
167 * display requested by ucLine.
\r
169 static void prvDisplayNextString( unsigned char ucLine, char *pcString );
\r
172 * Called from the IRQ interrupts, which are generated on button pushes. Send
\r
173 * ucCommand to the task on the button command queue if
\r
174 * lcdMIN_TIME_BETWEEN_INTERRUPTS_MS milliseconds have passed since the button
\r
175 * was last pushed (for debouncing).
\r
177 static portBASE_TYPE prvSendCommandOnDebouncedInput( TickType_t *pxTimeLastInterrupt, unsigned char ucCommand );
\r
179 /*-----------------------------------------------------------*/
\r
181 /* The queue used to pass commands from the button interrupt handlers to the
\r
182 prvLCDTaskLine2() task. */
\r
183 static QueueHandle_t xButtonCommandQueue = NULL;
\r
185 /* The mutex used to ensure only one task writes to the display at any one
\r
187 static SemaphoreHandle_t xLCDMutex = NULL;
\r
189 /* The string that is scrolled up and down the first line of the display. */
\r
190 static const char cDataString1[] = " http://www.FreeRTOS.org ";
\r
192 /* The string that is scrolled/nudged up and down the second line of the
\r
194 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
196 /* Structures passed into the two tasks to inform them which line to use on the
\r
197 display, how long to delay for, and which string to use. */
\r
198 struct _LCD_Params xLCDLine1 =
\r
200 LCD_LINE1, 215, ( char * ) cDataString1
\r
203 struct _LCD_Params xLCDLine2 =
\r
205 LCD_LINE2, 350, ( char * ) cDataString2
\r
209 /*-----------------------------------------------------------*/
\r
211 void vStartButtonAndLCDDemo( void )
\r
213 prvSetupButtonIOAndInterrupts();
\r
214 InitialiseDisplay();
\r
216 /* Create the mutex used to guard the LCD. */
\r
217 xLCDMutex = xSemaphoreCreateMutex();
\r
218 configASSERT( xLCDMutex );
\r
220 /* Create the queue used to pass commands from the IRQ interrupts to the
\r
221 prvLCDTaskLine2() task. */
\r
222 xButtonCommandQueue = xQueueCreate( lcdCOMMAND_QUEUE_LENGTH, sizeof( unsigned char ) );
\r
223 configASSERT( xButtonCommandQueue );
\r
225 /* Start the two tasks as described at the top of this file. */
\r
226 xTaskCreate( prvLCDTaskLine1, "LCD1", configMINIMAL_STACK_SIZE * 3, ( void * ) &xLCDLine1, tskIDLE_PRIORITY + 1, NULL );
\r
227 xTaskCreate( prvLCDTaskLine2, "LCD2", configMINIMAL_STACK_SIZE * 3, ( void * ) &xLCDLine2, tskIDLE_PRIORITY + 2, NULL );
\r
229 /*-----------------------------------------------------------*/
\r
231 static void prvLCDTaskLine1( void *pvParameters )
\r
233 struct _LCD_Params *pxLCDParamaters = ( struct _LCD_Params * ) pvParameters;
\r
234 unsigned short usPosition = 0U;
\r
235 unsigned char ucDirection = lcdRIGHT_TO_LEFT;
\r
239 vTaskDelay( pxLCDParamaters->Speed / portTICK_PERIOD_MS );
\r
241 /* Write the string. */
\r
242 prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );
\r
244 /* Move the string in whichever direction the scroll is currently going
\r
246 prvScrollString( &ucDirection, &usPosition, strlen( pxLCDParamaters->ptr_str ) );
\r
249 /*-----------------------------------------------------------*/
\r
251 static void prvLCDTaskLine2( void *pvParameters )
\r
253 struct _LCD_Params *pxLCDParamaters = ( struct _LCD_Params * ) pvParameters;
\r
254 unsigned short usPosition = 0U;
\r
255 unsigned char ucDirection = lcdRIGHT_TO_LEFT, ucStatus = lcdRUNNING, ucQueueData;
\r
256 TickType_t xDelayTicks = ( pxLCDParamaters->Speed / portTICK_PERIOD_MS );
\r
260 /* Wait for a message from an IRQ handler. */
\r
261 if( xQueueReceive( xButtonCommandQueue, &ucQueueData, xDelayTicks ) != pdPASS )
\r
263 /* A message was not received before xDelayTicks ticks passed, so
\r
264 generate the next string to display and display it. */
\r
265 prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );
\r
267 /* Move the string in whichever direction the scroll is currently
\r
269 prvScrollString( &ucDirection, &usPosition, strlen( pxLCDParamaters->ptr_str ) );
\r
273 /* A command was received. Process it. */
\r
274 switch( ucQueueData )
\r
276 case lcdSTART_STOP_COMMAND :
\r
278 /* If the LCD is running, top it. If the LCD is stopped, start
\r
280 ucStatus = !ucStatus;
\r
282 if( ucStatus == lcdRUNNING )
\r
284 xDelayTicks = ( pxLCDParamaters->Speed / portTICK_PERIOD_MS );
\r
288 xDelayTicks = portMAX_DELAY;
\r
293 case lcdSHIFT_BACK_COMMAND :
\r
295 if( ucStatus != lcdRUNNING )
\r
297 /* If not already at the start of the display.... */
\r
298 if( usPosition != 0U )
\r
300 /* ....move the display position back by one char. */
\r
302 prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );
\r
308 case lcdSHIFT_FORWARD_COMMAND :
\r
310 if( ucStatus != lcdRUNNING )
\r
312 /* If not already at the end of the display.... */
\r
313 if( usPosition != ( strlen( pxLCDParamaters->ptr_str ) - ( lcdSTRING_LEN - 1 ) ) )
\r
315 /* ....move the display position forward by one
\r
318 prvDisplayNextString( pxLCDParamaters->Line, &( pxLCDParamaters->ptr_str[ usPosition ] ) );
\r
326 /*-----------------------------------------------------------*/
\r
328 void prvSetupButtonIOAndInterrupts( void )
\r
330 /* Configure SW 1-3 pin settings */
\r
331 PORT3.PDR.BIT.B1 = 0; /* Switch 1 - Port 3.1 - IRQ1 */
\r
332 PORT3.PDR.BIT.B3 = 0; /* Switch 2 - Port 3.3 - IRQ3 */
\r
333 PORT3.PDR.BIT.B4 = 0; /* Switch 3 - Port 3.4 - IRQ4 */
\r
335 PORT3.PMR.BIT.B1 = 1;
\r
336 PORT3.PMR.BIT.B3 = 1;
\r
337 PORT3.PMR.BIT.B4 = 1;
\r
339 MPC.PWPR.BIT.B0WI = 0; /* Writing to the PFSWE bit is enabled */
\r
340 MPC.PWPR.BIT.PFSWE = 1; /* Writing to the PFS register is enabled */
\r
341 MPC.P31PFS.BIT.ISEL = 1;
\r
342 MPC.P33PFS.BIT.ISEL = 1;
\r
343 MPC.P34PFS.BIT.ISEL = 1;
\r
346 ICU.IER[ 0x08 ].BIT.IEN1 = 1;
\r
347 ICU.IPR[ 65 ].BIT.IPR = configMAX_SYSCALL_INTERRUPT_PRIORITY - 1;
\r
348 ICU.IR[ 65 ].BIT.IR = 0;
\r
349 ICU.IRQCR[ 1 ].BIT.IRQMD = 2; /* Rising edge */
\r
352 ICU.IER[ 0x08 ].BIT.IEN3 = 1;
\r
353 ICU.IPR[ 67 ].BIT.IPR = configMAX_SYSCALL_INTERRUPT_PRIORITY - 1;
\r
354 ICU.IR[ 67 ].BIT.IR = 0;
\r
355 ICU.IRQCR[ 3 ].BIT.IRQMD = 2; /* Rising edge */
\r
358 ICU.IER[ 0x08 ].BIT.IEN4 = 1;
\r
359 ICU.IPR[ 68 ].BIT.IPR = configMAX_SYSCALL_INTERRUPT_PRIORITY - 1;
\r
360 ICU.IR[ 68 ].BIT.IR = 0;
\r
361 ICU.IRQCR[ 4 ].BIT.IRQMD = 2; /* Rising edge */
\r
363 /*-----------------------------------------------------------*/
\r
365 static prvScrollString( unsigned char *pucDirection, unsigned short *pusPosition, size_t xStringLength )
\r
367 /* Check which way to scroll. */
\r
368 if( *pucDirection == lcdRIGHT_TO_LEFT )
\r
370 /* Move to the next character. */
\r
371 ( *pusPosition )++;
\r
373 /* Has the end of the string been reached? */
\r
374 if( ( *pusPosition ) == ( xStringLength - ( lcdSTRING_LEN - 1 ) ) )
\r
376 /* Switch direction. */
\r
377 *pucDirection = lcdLEFT_TO_RIGHT;
\r
378 ( *pusPosition )--;
\r
383 /* Move (backward) to the next character. */
\r
384 ( *pusPosition )--;
\r
385 if( *pusPosition == 0U )
\r
387 /* Switch Direction. */
\r
388 *pucDirection = lcdRIGHT_TO_LEFT;
\r
392 /*-----------------------------------------------------------*/
\r
394 static void prvDisplayNextString( unsigned char ucLine, char *pcString )
\r
396 static char cSingleLine[ lcdSTRING_LEN + 1 ];
\r
398 xSemaphoreTake( xLCDMutex, portMAX_DELAY );
\r
399 strncpy( cSingleLine, pcString, lcdSTRING_LEN );
\r
400 DisplayString( ucLine, cSingleLine );
\r
401 xSemaphoreGive( xLCDMutex );
\r
403 /*-----------------------------------------------------------*/
\r
405 static portBASE_TYPE prvSendCommandOnDebouncedInput( TickType_t *pxTimeLastInterrupt, unsigned char ucCommand )
\r
407 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
408 TickType_t xCurrentTickCount;
\r
410 /* Check the time now for debouncing purposes. */
\r
411 xCurrentTickCount = xTaskGetTickCountFromISR();
\r
413 /* Has enough time passed since the button was last push to accept it as a
\r
415 if( ( xCurrentTickCount - *pxTimeLastInterrupt ) > lcdMIN_TIME_BETWEEN_INTERRUPTS_MS )
\r
417 xQueueSendToBackFromISR( xButtonCommandQueue, &ucCommand, &xHigherPriorityTaskWoken );
\r
420 /* Remember the time now, so debounce can be performed again on the next
\r
422 *pxTimeLastInterrupt = xCurrentTickCount;
\r
424 return xHigherPriorityTaskWoken;
\r
426 /*-----------------------------------------------------------*/
\r
428 static void prvIRQ1_Handler( void )
\r
430 static TickType_t xTimeLastInterrupt = 0UL;
\r
431 static const unsigned char ucCommand = lcdSHIFT_BACK_COMMAND;
\r
432 portBASE_TYPE xHigherPriorityTaskWoken;
\r
434 xHigherPriorityTaskWoken = prvSendCommandOnDebouncedInput( &xTimeLastInterrupt, ucCommand );
\r
435 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
437 /*-----------------------------------------------------------*/
\r
439 static void prvIRQ3_Handler(void)
\r
441 static TickType_t xTimeLastInterrupt = 0UL;
\r
442 static const unsigned char ucCommand = lcdSTART_STOP_COMMAND;
\r
443 portBASE_TYPE xHigherPriorityTaskWoken;
\r
445 xHigherPriorityTaskWoken = prvSendCommandOnDebouncedInput( &xTimeLastInterrupt, ucCommand );
\r
446 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
448 /*-----------------------------------------------------------*/
\r
450 static void prvIRQ4_Handler(void)
\r
452 static TickType_t xTimeLastInterrupt = 0UL;
\r
453 static const unsigned char ucCommand = lcdSHIFT_FORWARD_COMMAND;
\r
454 portBASE_TYPE xHigherPriorityTaskWoken;
\r
456 xHigherPriorityTaskWoken = prvSendCommandOnDebouncedInput( &xTimeLastInterrupt, ucCommand );
\r
457 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r