2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 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
29 * Creates two tasks that operate on an interrupt driven serial port. A loopback
\r
30 * connector should be used so that everything that is transmitted is also received.
\r
31 * The serial port does not use any flow control. On a standard 9way 'D' connector
\r
32 * pins two and three should be connected together.
\r
34 * The first task repeatedly sends a string to a queue, character at a time. The
\r
35 * serial port interrupt will empty the queue and transmit the characters. The
\r
36 * task blocks for a pseudo random period before resending the string.
\r
38 * The second task blocks on a queue waiting for a character to be received.
\r
39 * Characters received by the serial port interrupt routine are posted onto the
\r
40 * queue - unblocking the task making it ready to execute. If this is then the
\r
41 * highest priority task ready to run it will run immediately - with a context
\r
42 * switch occurring at the end of the interrupt service routine. The task
\r
43 * receiving characters is spawned with a higher priority than the task
\r
44 * transmitting the characters.
\r
46 * With the loop back connector in place, one task will transmit a string and the
\r
47 * other will immediately receive it. The receiving task knows the string it
\r
48 * expects to receive so can detect an error.
\r
50 * This also creates a third task. This is used to test semaphore usage from an
\r
51 * ISR and does nothing interesting.
\r
53 * \page ComTestC comtest.c
\r
54 * \ingroup DemoFiles
\r
61 + The priority of the Rx task has been lowered. Received characters are
\r
62 now processed (read from the queue) at the idle priority, allowing low
\r
63 priority tasks to run evenly at times of a high communications overhead.
\r
67 + The Tx task now waits a pseudo random time between transissions.
\r
68 Previously a fixed period was used but this was not such a good test as
\r
69 interrupts fired at regular intervals.
\r
71 Changes From V1.2.0:
\r
73 + Use vSerialPutString() instead of single character puts.
\r
74 + Only stop the check variable incrementing after two consecutive errors.
\r
78 + Made the Rx task 2 priorities higher than the Tx task. Previously it was
\r
79 only 1. This is done to tie in better with the other demo application
\r
84 + Delay periods are now specified using variables and constants of
\r
85 TickType_t rather than unsigned long.
\r
86 + Slight modification to task priorities.
\r
91 /* Scheduler include files. */
\r
94 #include "FreeRTOS.h"
\r
97 /* Demo program include files. */
\r
99 #include "comtest.h"
\r
102 /* The Tx task will transmit the sequence of characters at a pseudo random
\r
103 interval. This is the maximum and minimum block time between sends. */
\r
104 #define comTX_MAX_BLOCK_TIME ( ( TickType_t ) 0x15e )
\r
105 #define comTX_MIN_BLOCK_TIME ( ( TickType_t ) 0xc8 )
\r
107 #define comMAX_CONSECUTIVE_ERRORS ( 2 )
\r
109 #define comSTACK_SIZE ( ( unsigned short ) 256 )
\r
111 #define comRX_RELATIVE_PRIORITY ( 1 )
\r
113 /* Handle to the com port used by both tasks. */
\r
114 static xComPortHandle xPort;
\r
116 /* The transmit function as described at the top of the file. */
\r
117 static void vComTxTask( void *pvParameters );
\r
119 /* The receive function as described at the top of the file. */
\r
120 static void vComRxTask( void *pvParameters );
\r
122 /* The semaphore test function as described at the top of the file. */
\r
123 static void vSemTestTask( void * pvParameters );
\r
125 /* The string that is repeatedly transmitted. */
\r
126 const char * const pcMessageToExchange = "Send this message over and over again to check communications interrupts. "
\r
127 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";
\r
129 /* Variables that are incremented on each cycle of each task. These are used to
\r
130 check that both tasks are still executing. */
\r
131 volatile short sTxCount = 0, sRxCount = 0, sSemCount = 0;
\r
133 /* The handle to the semaphore test task. */
\r
134 static TaskHandle_t xSemTestTaskHandle = NULL;
\r
136 /*-----------------------------------------------------------*/
\r
138 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )
\r
140 const unsigned portBASE_TYPE uxBufferLength = 255;
\r
142 /* Initialise the com port then spawn both tasks. */
\r
143 xPort = xSerialPortInit( ePort, eBaudRate, serNO_PARITY, serBITS_8, serSTOP_1, uxBufferLength );
\r
144 xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority, NULL );
\r
145 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority + comRX_RELATIVE_PRIORITY, NULL );
\r
146 xTaskCreate( vSemTestTask, "ISRSem", comSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xSemTestTaskHandle );
\r
148 /*-----------------------------------------------------------*/
\r
150 static void vComTxTask( void *pvParameters )
\r
152 const char * const pcTaskStartMsg = "COM Tx task started.\r\n";
\r
153 TickType_t xTimeToWait;
\r
155 /* Stop warnings. */
\r
156 ( void ) pvParameters;
\r
158 /* Queue a message for printing to say the task has started. */
\r
159 vPrintDisplayMessage( &pcTaskStartMsg );
\r
163 /* Send the string to the serial port. */
\r
164 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );
\r
166 /* We have posted all the characters in the string - increment the variable
\r
167 used to check that this task is still running, then wait before re-sending
\r
171 xTimeToWait = xTaskGetTickCount();
\r
173 /* Make sure we don't wait too long... */
\r
174 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
176 /* ...but we do want to wait. */
\r
177 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
179 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
182 vTaskDelay( xTimeToWait );
\r
184 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
185 /*-----------------------------------------------------------*/
\r
187 static void vComRxTask( void *pvParameters )
\r
189 const char * const pcTaskStartMsg = "COM Rx task started.\r\n";
\r
190 const char * const pcTaskErrorMsg = "COM read error\r\n";
\r
191 const char * const pcTaskRestartMsg = "COM resynced\r\n";
\r
192 const char * const pcTaskTimeoutMsg = "COM Rx timed out\r\n";
\r
193 const TickType_t xBlockTime = ( TickType_t ) 0xffff / portTICK_PERIOD_MS;
\r
194 const char *pcExpectedChar;
\r
195 portBASE_TYPE xGotChar;
\r
197 short sResyncRequired, sConsecutiveErrors, sLatchedError;
\r
199 /* Stop warnings. */
\r
200 ( void ) pvParameters;
\r
202 /* Queue a message for printing to say the task has started. */
\r
203 vPrintDisplayMessage( &pcTaskStartMsg );
\r
205 /* The first expected character is the first character in the string. */
\r
206 pcExpectedChar = pcMessageToExchange;
\r
207 sResyncRequired = pdFALSE;
\r
208 sConsecutiveErrors = 0;
\r
209 sLatchedError = pdFALSE;
\r
213 /* Receive a message from the com port interrupt routine. If a message is
\r
214 not yet available the call will block the task. */
\r
215 xGotChar = xSerialGetChar( xPort, &cRxedChar, xBlockTime );
\r
216 if( xGotChar == pdTRUE )
\r
218 if( sResyncRequired == pdTRUE )
\r
220 /* We got out of sequence and are waiting for the start of the next
\r
221 transmission of the string. */
\r
222 if( cRxedChar == '\n' )
\r
224 /* This is the end of the message so we can start again - with
\r
225 the first character in the string being the next thing we expect
\r
227 pcExpectedChar = pcMessageToExchange;
\r
228 sResyncRequired = pdFALSE;
\r
230 /* Queue a message for printing to say that we are going to try
\r
232 vPrintDisplayMessage( &pcTaskRestartMsg );
\r
234 /* Stop incrementing the check variable, if consecutive errors occur. */
\r
235 sConsecutiveErrors++;
\r
236 if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )
\r
238 sLatchedError = pdTRUE;
\r
244 /* We have received a character, but is it the expected character? */
\r
245 if( cRxedChar != *pcExpectedChar )
\r
247 /* This was not the expected character so post a message for
\r
248 printing to say that an error has occurred. We will then wait
\r
249 to resynchronise. */
\r
250 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
251 sResyncRequired = pdTRUE;
\r
255 /* This was the expected character so next time we will expect
\r
256 the next character in the string. Wrap back to the beginning
\r
257 of the string when the null terminator has been reached. */
\r
259 if( *pcExpectedChar == '\0' )
\r
261 pcExpectedChar = pcMessageToExchange;
\r
263 /* We have got through the entire string without error. */
\r
264 sConsecutiveErrors = 0;
\r
269 /* Increment the count that is used to check that this task is still
\r
270 running. This is only done if an error has never occurred. */
\r
271 if( sLatchedError == pdFALSE )
\r
278 vPrintDisplayMessage( &pcTaskTimeoutMsg );
\r
281 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
282 /*-----------------------------------------------------------*/
\r
284 static void vSemTestTask( void * pvParameters )
\r
286 const char * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";
\r
287 portBASE_TYPE xError = pdFALSE;
\r
289 /* Stop warnings. */
\r
290 ( void ) pvParameters;
\r
292 /* Queue a message for printing to say the task has started. */
\r
293 vPrintDisplayMessage( &pcTaskStartMsg );
\r
297 if( xSerialWaitForSemaphore( xPort ) )
\r
299 if( xError == pdFALSE )
\r
309 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */
\r
310 /*-----------------------------------------------------------*/
\r
312 /* This is called to check that all the created tasks are still running. */
\r
313 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
315 static short sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;
\r
316 portBASE_TYPE xReturn;
\r
318 /* Not too worried about mutual exclusion on these variables as they are 16
\r
319 bits and we are only reading them. We also only care to see if they have
\r
322 if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )
\r
331 sLastTxCount = sTxCount;
\r
332 sLastRxCount = sRxCount;
\r
333 sLastSemCount = sSemCount;
\r
337 /*-----------------------------------------------------------*/
\r
339 void vComTestUnsuspendTask( void )
\r
341 /* The task that is suspended on the semaphore will be referenced from the
\r
342 Suspended list as it is blocking indefinitely. This call just checks that
\r
343 the kernel correctly detects this and does not attempt to unsuspend the
\r
345 xTaskResumeFromISR( xSemTestTaskHandle );
\r