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
30 * Creates a task and a timer that operate on an interrupt driven serial port.
\r
31 * This demo assumes that the characters transmitted on a port will also be
\r
32 * received on the same port. Therefore, the UART must either be connected to
\r
33 * an echo server, or the uart connector must have a loopback connector fitted.
\r
34 * See http://www.serialporttool.com/CommEcho.htm for a suitable echo server
\r
35 * for Windows hosts.
\r
37 * The timer sends a string to the UART, toggles an LED, then resets itself by
\r
38 * changing its own period. The period is calculated as a pseudo random number
\r
39 * between comTX_MAX_BLOCK_TIME and comTX_MIN_BLOCK_TIME.
\r
41 * The task blocks on an Rx queue waiting for a character to become available.
\r
42 * Received characters are checked to ensure they match those transmitted by the
\r
43 * Tx timer. An error is latched if characters are missing, incorrect, or
\r
44 * arrive too slowly.
\r
46 * How characters are actually transmitted and received is port specific. Demos
\r
47 * that include this test/demo file will provide example drivers. The Tx timer
\r
48 * executes in the context of the timer service (daemon) task, and must
\r
49 * therefore never attempt to block.
\r
53 /* Scheduler include files. */
\r
56 #include "FreeRTOS.h"
\r
60 #ifndef configUSE_TIMERS
\r
61 #error This demo uses timers. configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.
\r
64 #if configUSE_TIMERS != 1
\r
65 #error This demo uses timers. configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.
\r
69 /* Demo program include files. */
\r
71 #include "comtest_strings.h"
\r
72 #include "partest.h"
\r
74 /* The size of the stack given to the Rx task. */
\r
75 #define comSTACK_SIZE configMINIMAL_STACK_SIZE
\r
77 /* See the comment above the declaraction of the uxBaseLED variable. */
\r
78 #define comTX_LED_OFFSET ( 0 )
\r
79 #define comRX_LED_OFFSET ( 1 )
\r
81 /* The Tx timer transmits the sequence of characters at a pseudo random
\r
82 interval that is capped between comTX_MAX_BLOCK_TIME and
\r
83 comTX_MIN_BLOCK_TIME. */
\r
84 #define comTX_MAX_BLOCK_TIME ( ( TickType_t ) 0x96 )
\r
85 #define comTX_MIN_BLOCK_TIME ( ( TickType_t ) 0x32 )
\r
86 #define comOFFSET_TIME ( ( TickType_t ) 3 )
\r
88 /* States for the simple state machine implemented in the Rx task. */
\r
89 #define comtstWAITING_START_OF_STRING 0
\r
90 #define comtstWAITING_END_OF_STRING 1
\r
92 /* A short delay in ticks - this delay is used to allow the Rx queue to fill up
\r
93 a bit so more than one character can be processed at a time. This is relative
\r
94 to comTX_MIN_BLOCK_TIME to ensure it is never longer than the shortest gap
\r
95 between transmissions. It could be worked out more scientifically from the
\r
96 baud rate being used. */
\r
97 #define comSHORT_DELAY ( comTX_MIN_BLOCK_TIME >> ( TickType_t ) 2 )
\r
99 /* The string that is transmitted and received. */
\r
100 #define comTRANSACTED_STRING "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
\r
102 /* A block time of 0 simply means "don't block". */
\r
103 #define comtstDONT_BLOCK ( TickType_t ) 0
\r
105 /* Handle to the com port used by both tasks. */
\r
106 static xComPortHandle xPort = NULL;
\r
108 /* The callback function allocated to the transmit timer, as described in the
\r
109 comments at the top of this file. */
\r
110 static void prvComTxTimerCallback( TimerHandle_t xTimer );
\r
112 /* The receive task as described in the comments at the top of this file. */
\r
113 static void vComRxTask( void *pvParameters );
\r
115 /* The Rx task will toggle LED ( uxBaseLED + comRX_LED_OFFSET). The Tx task
\r
116 will toggle LED ( uxBaseLED + comTX_LED_OFFSET ). */
\r
117 static UBaseType_t uxBaseLED = 0;
\r
119 /* The Rx task toggles uxRxLoops on each successful iteration of its defined
\r
120 function - provided no errors have ever been latched. If this variable stops
\r
121 incrementing, then an error has occurred. */
\r
122 static volatile UBaseType_t uxRxLoops = 0UL;
\r
124 /* The timer used to periodically transmit the string. This is the timer that
\r
125 has prvComTxTimerCallback allocated to it as its callback function. */
\r
126 static TimerHandle_t xTxTimer = NULL;
\r
128 /* The string length is held at file scope so the Tx timer does not need to
\r
129 calculate it each time it executes. */
\r
130 static size_t xStringLength = 0U;
\r
132 /*-----------------------------------------------------------*/
\r
134 void vStartComTestStringsTasks( UBaseType_t uxPriority, uint32_t ulBaudRate, UBaseType_t uxLED )
\r
136 /* Store values that are used at run time. */
\r
139 /* Calculate the string length here, rather than each time the Tx timer
\r
141 xStringLength = strlen( comTRANSACTED_STRING );
\r
143 /* Include the null terminator in the string length as this is used to
\r
144 detect the end of the string in the Rx task. */
\r
147 /* Initialise the com port, then spawn the Rx task and create the Tx
\r
149 xSerialPortInitMinimal( ulBaudRate, ( xStringLength * 2U ) );
\r
151 /* Create the Rx task and the Tx timer. The timer is started from the
\r
153 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority, ( TaskHandle_t * ) NULL );
\r
154 xTxTimer = xTimerCreate( "TxTimer", comTX_MIN_BLOCK_TIME, pdFALSE, NULL, prvComTxTimerCallback );
\r
155 configASSERT( xTxTimer );
\r
157 /*-----------------------------------------------------------*/
\r
159 static void prvComTxTimerCallback( TimerHandle_t xTimer )
\r
161 TickType_t xTimeToWait;
\r
163 /* The parameter is not used in this case. */
\r
166 /* Send the string. How this is actually performed depends on the
\r
167 sample driver provided with this demo. However - as this is a timer,
\r
168 it executes in the context of the timer task and therefore must not
\r
170 vSerialPutString( xPort, comTRANSACTED_STRING, xStringLength );
\r
172 /* Toggle an LED to give a visible indication that another transmission
\r
173 has been performed. */
\r
174 vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );
\r
176 /* Wait a pseudo random time before sending the string again. */
\r
177 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;
\r
179 /* Ensure the time to wait is not greater than comTX_MAX_BLOCK_TIME. */
\r
180 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
182 /* Ensure the time to wait is not less than comTX_MIN_BLOCK_TIME. */
\r
183 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
185 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
188 /* Reset the timer to run again xTimeToWait ticks from now. This function
\r
189 is called from the context of the timer task, so the block time must not
\r
190 be anything other than zero. */
\r
191 xTimerChangePeriod( xTxTimer, xTimeToWait, comtstDONT_BLOCK );
\r
193 /*-----------------------------------------------------------*/
\r
195 static void vComRxTask( void *pvParameters )
\r
197 BaseType_t xState = comtstWAITING_START_OF_STRING, xErrorOccurred = pdFALSE;
\r
198 char *pcExpectedByte, cRxedChar;
\r
199 const xComPortHandle xPort = NULL;
\r
201 /* The parameter is not used in this example. */
\r
202 ( void ) pvParameters;
\r
204 /* Start the Tx timer. This only needs to be started once, as it will
\r
205 reset itself thereafter. */
\r
206 xTimerStart( xTxTimer, portMAX_DELAY );
\r
208 /* The first expected Rx character is the first in the string that is
\r
210 pcExpectedByte = comTRANSACTED_STRING;
\r
214 /* Wait for the next character. */
\r
215 if( xSerialGetChar( xPort, &cRxedChar, ( comTX_MAX_BLOCK_TIME * 2 ) ) == pdFALSE )
\r
217 /* A character definitely should have been received by now. As a
\r
218 character was not received an error must have occurred (which might
\r
219 just be that the loopback connector is not fitted). */
\r
220 xErrorOccurred = pdTRUE;
\r
225 case comtstWAITING_START_OF_STRING:
\r
226 if( cRxedChar == *pcExpectedByte )
\r
228 /* The received character was the first character of the
\r
229 string. Move to the next state to check each character
\r
230 as it comes in until the entire string has been received. */
\r
231 xState = comtstWAITING_END_OF_STRING;
\r
234 /* Block for a short period. This just allows the Rx queue
\r
235 to contain more than one character, and therefore prevent
\r
236 thrashing reads to the queue, and repetitive context
\r
237 switches as each character is received. */
\r
238 vTaskDelay( comSHORT_DELAY );
\r
242 case comtstWAITING_END_OF_STRING:
\r
243 if( cRxedChar == *pcExpectedByte )
\r
245 /* The received character was the expected character. Was
\r
246 it the last character in the string - i.e. the null
\r
248 if( cRxedChar == 0x00 )
\r
250 /* The entire string has been received. If no errors
\r
251 have been latched, then increment the loop counter to
\r
252 show this task is still healthy. */
\r
253 if( xErrorOccurred == pdFALSE )
\r
257 /* Toggle an LED to give a visible sign that a
\r
258 complete string has been received. */
\r
259 vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );
\r
262 /* Go back to wait for the start of the next string. */
\r
263 pcExpectedByte = comTRANSACTED_STRING;
\r
264 xState = comtstWAITING_START_OF_STRING;
\r
268 /* Wait for the next character in the string. */
\r
274 /* The character received was not that expected. */
\r
275 xErrorOccurred = pdTRUE;
\r
280 /* Should not get here. Stop the Rx loop counter from
\r
281 incrementing to latch the error. */
\r
282 xErrorOccurred = pdTRUE;
\r
287 /*-----------------------------------------------------------*/
\r
289 BaseType_t xAreComTestTasksStillRunning( void )
\r
291 BaseType_t xReturn;
\r
293 /* If the count of successful reception loops has not changed than at
\r
294 some time an error occurred (i.e. a character was received out of sequence)
\r
295 and false is returned. */
\r
296 if( uxRxLoops == 0UL )
\r
305 /* Reset the count of successful Rx loops. When this function is called
\r
306 again it should have been incremented again. */
\r