| 1 |
606 |
jeremybenn |
/*
|
| 2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
| 3 |
|
|
|
| 4 |
|
|
***************************************************************************
|
| 5 |
|
|
* *
|
| 6 |
|
|
* If you are: *
|
| 7 |
|
|
* *
|
| 8 |
|
|
* + New to FreeRTOS, *
|
| 9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
| 10 |
|
|
* + Looking for basic training, *
|
| 11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
| 12 |
|
|
* *
|
| 13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
| 14 |
|
|
* *
|
| 15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
| 16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
| 17 |
|
|
* *
|
| 18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
| 19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
| 20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
| 21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
| 22 |
|
|
* *
|
| 23 |
|
|
***************************************************************************
|
| 24 |
|
|
|
| 25 |
|
|
This file is part of the FreeRTOS distribution.
|
| 26 |
|
|
|
| 27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
| 28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
| 29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
| 30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
| 31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
| 32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
| 33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
| 34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
| 35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
| 36 |
|
|
more details. You should have received a copy of the GNU General Public
|
| 37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
| 38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
| 39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
| 40 |
|
|
FreeRTOS WEB site.
|
| 41 |
|
|
|
| 42 |
|
|
1 tab == 4 spaces!
|
| 43 |
|
|
|
| 44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
| 45 |
|
|
contact details.
|
| 46 |
|
|
|
| 47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
| 48 |
|
|
critical systems.
|
| 49 |
|
|
|
| 50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
| 51 |
|
|
licensing and training services.
|
| 52 |
|
|
*/
|
| 53 |
|
|
|
| 54 |
|
|
|
| 55 |
|
|
/*
|
| 56 |
|
|
* This version of comtest. c is for use on systems that have limited stack
|
| 57 |
|
|
* space and no display facilities. The complete version can be found in
|
| 58 |
|
|
* the Demo/Common/Full directory.
|
| 59 |
|
|
*
|
| 60 |
|
|
* Creates two tasks that operate on an interrupt driven serial port. A
|
| 61 |
|
|
* loopback connector should be used so that everything that is transmitted is
|
| 62 |
|
|
* also received. The serial port does not use any flow control. On a
|
| 63 |
|
|
* standard 9way 'D' connector pins two and three should be connected together.
|
| 64 |
|
|
*
|
| 65 |
|
|
* The first task posts a sequence of characters to the Tx queue, toggling an
|
| 66 |
|
|
* LED on each successful post. At the end of the sequence it sleeps for a
|
| 67 |
|
|
* pseudo-random period before resending the same sequence.
|
| 68 |
|
|
*
|
| 69 |
|
|
* The UART Tx end interrupt is enabled whenever data is available in the Tx
|
| 70 |
|
|
* queue. The Tx end ISR removes a single character from the Tx queue and
|
| 71 |
|
|
* passes it to the UART for transmission.
|
| 72 |
|
|
*
|
| 73 |
|
|
* The second task blocks on the Rx queue waiting for a character to become
|
| 74 |
|
|
* available. When the UART Rx end interrupt receives a character it places
|
| 75 |
|
|
* it in the Rx queue, waking the second task. The second task checks that the
|
| 76 |
|
|
* characters removed from the Rx queue form the same sequence as those posted
|
| 77 |
|
|
* to the Tx queue, and toggles an LED for each correct character.
|
| 78 |
|
|
*
|
| 79 |
|
|
* The receiving task is spawned with a higher priority than the transmitting
|
| 80 |
|
|
* task. The receiver will therefore wake every time a character is
|
| 81 |
|
|
* transmitted so neither the Tx or Rx queue should ever hold more than a few
|
| 82 |
|
|
* characters.
|
| 83 |
|
|
*
|
| 84 |
|
|
*/
|
| 85 |
|
|
|
| 86 |
|
|
/* Scheduler include files. */
|
| 87 |
|
|
#include <stdlib.h>
|
| 88 |
|
|
#include "FreeRTOS.h"
|
| 89 |
|
|
#include "task.h"
|
| 90 |
|
|
|
| 91 |
|
|
/* Demo program include files. */
|
| 92 |
|
|
#include "serial.h"
|
| 93 |
|
|
#include "comtest.h"
|
| 94 |
|
|
#include "partest.h"
|
| 95 |
|
|
|
| 96 |
|
|
#define comSTACK_SIZE configMINIMAL_STACK_SIZE
|
| 97 |
|
|
#define comTX_LED_OFFSET ( 0 )
|
| 98 |
|
|
#define comRX_LED_OFFSET ( 1 )
|
| 99 |
|
|
#define comTOTAL_PERMISSIBLE_ERRORS ( 2 )
|
| 100 |
|
|
|
| 101 |
|
|
/* The Tx task will transmit the sequence of characters at a pseudo random
|
| 102 |
|
|
interval. This is the maximum and minimum block time between sends. */
|
| 103 |
|
|
#define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x96 )
|
| 104 |
|
|
#define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0x32 )
|
| 105 |
|
|
#define comOFFSET_TIME ( ( portTickType ) 3 )
|
| 106 |
|
|
|
| 107 |
|
|
/* We should find that each character can be queued for Tx immediately and we
|
| 108 |
|
|
don't have to block to send. */
|
| 109 |
|
|
#define comNO_BLOCK ( ( portTickType ) 0 )
|
| 110 |
|
|
|
| 111 |
|
|
/* The Rx task will block on the Rx queue for a long period. */
|
| 112 |
|
|
#define comRX_BLOCK_TIME ( ( portTickType ) 0xffff )
|
| 113 |
|
|
|
| 114 |
|
|
/* The sequence transmitted is from comFIRST_BYTE to and including comLAST_BYTE. */
|
| 115 |
|
|
#define comFIRST_BYTE ( 'A' )
|
| 116 |
|
|
#define comLAST_BYTE ( 'X' )
|
| 117 |
|
|
|
| 118 |
|
|
#define comBUFFER_LEN ( ( unsigned portBASE_TYPE ) ( comLAST_BYTE - comFIRST_BYTE ) + ( unsigned portBASE_TYPE ) 1 )
|
| 119 |
|
|
#define comINITIAL_RX_COUNT_VALUE ( 0 )
|
| 120 |
|
|
|
| 121 |
|
|
/* Handle to the com port used by both tasks. */
|
| 122 |
|
|
static xComPortHandle xPort = NULL;
|
| 123 |
|
|
|
| 124 |
|
|
/* The transmit task as described at the top of the file. */
|
| 125 |
|
|
static portTASK_FUNCTION_PROTO( vComTxTask, pvParameters );
|
| 126 |
|
|
|
| 127 |
|
|
/* The receive task as described at the top of the file. */
|
| 128 |
|
|
static portTASK_FUNCTION_PROTO( vComRxTask, pvParameters );
|
| 129 |
|
|
|
| 130 |
|
|
/* The LED that should be toggled by the Rx and Tx tasks. The Rx task will
|
| 131 |
|
|
toggle LED ( uxBaseLED + comRX_LED_OFFSET). The Tx task will toggle LED
|
| 132 |
|
|
( uxBaseLED + comTX_LED_OFFSET ). */
|
| 133 |
|
|
static unsigned portBASE_TYPE uxBaseLED = 0;
|
| 134 |
|
|
|
| 135 |
|
|
/* Check variable used to ensure no error have occurred. The Rx task will
|
| 136 |
|
|
increment this variable after every successfully received sequence. If at any
|
| 137 |
|
|
time the sequence is incorrect the the variable will stop being incremented. */
|
| 138 |
|
|
static volatile unsigned portBASE_TYPE uxRxLoops = comINITIAL_RX_COUNT_VALUE;
|
| 139 |
|
|
|
| 140 |
|
|
/*-----------------------------------------------------------*/
|
| 141 |
|
|
|
| 142 |
|
|
void vAltStartComTestTasks( unsigned portBASE_TYPE uxPriority, unsigned long ulBaudRate, unsigned portBASE_TYPE uxLED )
|
| 143 |
|
|
{
|
| 144 |
|
|
/* Initialise the com port then spawn the Rx and Tx tasks. */
|
| 145 |
|
|
uxBaseLED = uxLED;
|
| 146 |
|
|
xSerialPortInitMinimal( ulBaudRate, comBUFFER_LEN );
|
| 147 |
|
|
|
| 148 |
|
|
/* The Tx task is spawned with a lower priority than the Rx task. */
|
| 149 |
|
|
xTaskCreate( vComTxTask, ( signed char * ) "COMTx", comSTACK_SIZE, NULL, uxPriority - 1, ( xTaskHandle * ) NULL );
|
| 150 |
|
|
xTaskCreate( vComRxTask, ( signed char * ) "COMRx", comSTACK_SIZE, NULL, uxPriority, ( xTaskHandle * ) NULL );
|
| 151 |
|
|
}
|
| 152 |
|
|
/*-----------------------------------------------------------*/
|
| 153 |
|
|
|
| 154 |
|
|
static portTASK_FUNCTION( vComTxTask, pvParameters )
|
| 155 |
|
|
{
|
| 156 |
|
|
signed char cByteToSend;
|
| 157 |
|
|
portTickType xTimeToWait;
|
| 158 |
|
|
|
| 159 |
|
|
/* Just to stop compiler warnings. */
|
| 160 |
|
|
( void ) pvParameters;
|
| 161 |
|
|
|
| 162 |
|
|
for( ;; )
|
| 163 |
|
|
{
|
| 164 |
|
|
/* Simply transmit a sequence of characters from comFIRST_BYTE to
|
| 165 |
|
|
comLAST_BYTE. */
|
| 166 |
|
|
for( cByteToSend = comFIRST_BYTE; cByteToSend <= comLAST_BYTE; cByteToSend++ )
|
| 167 |
|
|
{
|
| 168 |
|
|
if( xSerialPutChar( xPort, cByteToSend, comNO_BLOCK ) == pdPASS )
|
| 169 |
|
|
{
|
| 170 |
|
|
vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );
|
| 171 |
|
|
}
|
| 172 |
|
|
}
|
| 173 |
|
|
|
| 174 |
|
|
/* Turn the LED off while we are not doing anything. */
|
| 175 |
|
|
vParTestSetLED( uxBaseLED + comTX_LED_OFFSET, pdFALSE );
|
| 176 |
|
|
|
| 177 |
|
|
/* We have posted all the characters in the string - wait before
|
| 178 |
|
|
re-sending. Wait a pseudo-random time as this will provide a better
|
| 179 |
|
|
test. */
|
| 180 |
|
|
xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;
|
| 181 |
|
|
|
| 182 |
|
|
/* Make sure we don't wait too long... */
|
| 183 |
|
|
xTimeToWait %= comTX_MAX_BLOCK_TIME;
|
| 184 |
|
|
|
| 185 |
|
|
/* ...but we do want to wait. */
|
| 186 |
|
|
if( xTimeToWait < comTX_MIN_BLOCK_TIME )
|
| 187 |
|
|
{
|
| 188 |
|
|
xTimeToWait = comTX_MIN_BLOCK_TIME;
|
| 189 |
|
|
}
|
| 190 |
|
|
|
| 191 |
|
|
vTaskDelay( xTimeToWait );
|
| 192 |
|
|
}
|
| 193 |
|
|
} /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
|
| 194 |
|
|
/*-----------------------------------------------------------*/
|
| 195 |
|
|
|
| 196 |
|
|
static portTASK_FUNCTION( vComRxTask, pvParameters )
|
| 197 |
|
|
{
|
| 198 |
|
|
signed char cExpectedByte, cByteRxed;
|
| 199 |
|
|
portBASE_TYPE xResyncRequired = pdFALSE, xErrorOccurred = pdFALSE;
|
| 200 |
|
|
|
| 201 |
|
|
/* Just to stop compiler warnings. */
|
| 202 |
|
|
( void ) pvParameters;
|
| 203 |
|
|
|
| 204 |
|
|
for( ;; )
|
| 205 |
|
|
{
|
| 206 |
|
|
/* We expect to receive the characters from comFIRST_BYTE to
|
| 207 |
|
|
comLAST_BYTE in an incrementing order. Loop to receive each byte. */
|
| 208 |
|
|
for( cExpectedByte = comFIRST_BYTE; cExpectedByte <= comLAST_BYTE; cExpectedByte++ )
|
| 209 |
|
|
{
|
| 210 |
|
|
/* Block on the queue that contains received bytes until a byte is
|
| 211 |
|
|
available. */
|
| 212 |
|
|
if( xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ) )
|
| 213 |
|
|
{
|
| 214 |
|
|
/* Was this the byte we were expecting? If so, toggle the LED,
|
| 215 |
|
|
otherwise we are out on sync and should break out of the loop
|
| 216 |
|
|
until the expected character sequence is about to restart. */
|
| 217 |
|
|
if( cByteRxed == cExpectedByte )
|
| 218 |
|
|
{
|
| 219 |
|
|
vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );
|
| 220 |
|
|
}
|
| 221 |
|
|
else
|
| 222 |
|
|
{
|
| 223 |
|
|
xResyncRequired = pdTRUE;
|
| 224 |
|
|
break; /*lint !e960 Non-switch break allowed. */
|
| 225 |
|
|
}
|
| 226 |
|
|
}
|
| 227 |
|
|
}
|
| 228 |
|
|
|
| 229 |
|
|
/* Turn the LED off while we are not doing anything. */
|
| 230 |
|
|
vParTestSetLED( uxBaseLED + comRX_LED_OFFSET, pdFALSE );
|
| 231 |
|
|
|
| 232 |
|
|
/* Did we break out of the loop because the characters were received in
|
| 233 |
|
|
an unexpected order? If so wait here until the character sequence is
|
| 234 |
|
|
about to restart. */
|
| 235 |
|
|
if( xResyncRequired == pdTRUE )
|
| 236 |
|
|
{
|
| 237 |
|
|
while( cByteRxed != comLAST_BYTE )
|
| 238 |
|
|
{
|
| 239 |
|
|
/* Block until the next char is available. */
|
| 240 |
|
|
xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME );
|
| 241 |
|
|
}
|
| 242 |
|
|
|
| 243 |
|
|
/* Note that an error occurred which caused us to have to resync.
|
| 244 |
|
|
We use this to stop incrementing the loop counter so
|
| 245 |
|
|
sAreComTestTasksStillRunning() will return false - indicating an
|
| 246 |
|
|
error. */
|
| 247 |
|
|
xErrorOccurred++;
|
| 248 |
|
|
|
| 249 |
|
|
/* We have now resynced with the Tx task and can continue. */
|
| 250 |
|
|
xResyncRequired = pdFALSE;
|
| 251 |
|
|
}
|
| 252 |
|
|
else
|
| 253 |
|
|
{
|
| 254 |
|
|
if( xErrorOccurred < comTOTAL_PERMISSIBLE_ERRORS )
|
| 255 |
|
|
{
|
| 256 |
|
|
/* Increment the count of successful loops. As error
|
| 257 |
|
|
occurring (i.e. an unexpected character being received) will
|
| 258 |
|
|
prevent this counter being incremented for the rest of the
|
| 259 |
|
|
execution. Don't worry about mutual exclusion on this
|
| 260 |
|
|
variable - it doesn't really matter as we just want it
|
| 261 |
|
|
to change. */
|
| 262 |
|
|
uxRxLoops++;
|
| 263 |
|
|
}
|
| 264 |
|
|
}
|
| 265 |
|
|
}
|
| 266 |
|
|
} /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
|
| 267 |
|
|
/*-----------------------------------------------------------*/
|
| 268 |
|
|
|
| 269 |
|
|
portBASE_TYPE xAreComTestTasksStillRunning( void )
|
| 270 |
|
|
{
|
| 271 |
|
|
portBASE_TYPE xReturn;
|
| 272 |
|
|
|
| 273 |
|
|
/* If the count of successful reception loops has not changed than at
|
| 274 |
|
|
some time an error occurred (i.e. a character was received out of sequence)
|
| 275 |
|
|
and we will return false. */
|
| 276 |
|
|
if( uxRxLoops == comINITIAL_RX_COUNT_VALUE )
|
| 277 |
|
|
{
|
| 278 |
|
|
xReturn = pdFALSE;
|
| 279 |
|
|
}
|
| 280 |
|
|
else
|
| 281 |
|
|
{
|
| 282 |
|
|
xReturn = pdTRUE;
|
| 283 |
|
|
}
|
| 284 |
|
|
|
| 285 |
|
|
/* Reset the count of successful Rx loops. When this function is called
|
| 286 |
|
|
again we expect this to have been incremented. */
|
| 287 |
|
|
uxRxLoops = comINITIAL_RX_COUNT_VALUE;
|
| 288 |
|
|
|
| 289 |
|
|
return xReturn;
|
| 290 |
|
|
}
|
| 291 |
|
|
|