]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_ATSAM3S-EK2_Atmel_Studio/src/Common-Demo-Source/comtest.c
91875e568088f213329fbc6d2c44d447e472e406
[freertos] / FreeRTOS / Demo / CORTEX_ATSAM3S-EK2_Atmel_Studio / src / Common-Demo-Source / comtest.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 \r
29 /*\r
30  * This version of comtest. c is for use on systems that have limited stack\r
31  * space and no display facilities.  The complete version can be found in\r
32  * the Demo/Common/Full directory.\r
33  *\r
34  * Creates two tasks that operate on an interrupt driven serial port.  A\r
35  * loopback connector should be used so that everything that is transmitted is\r
36  * also received.  The serial port does not use any flow control.  On a\r
37  * standard 9way 'D' connector pins two and three should be connected together.\r
38  *\r
39  * The first task posts a sequence of characters to the Tx queue, toggling an\r
40  * LED on each successful post.  At the end of the sequence it sleeps for a\r
41  * pseudo-random period before resending the same sequence.\r
42  *\r
43  * The UART Tx end interrupt is enabled whenever data is available in the Tx\r
44  * queue.  The Tx end ISR removes a single character from the Tx queue and\r
45  * passes it to the UART for transmission.\r
46  *\r
47  * The second task blocks on the Rx queue waiting for a character to become\r
48  * available.  When the UART Rx end interrupt receives a character it places\r
49  * it in the Rx queue, waking the second task.  The second task checks that the\r
50  * characters removed from the Rx queue form the same sequence as those posted\r
51  * to the Tx queue, and toggles an LED for each correct character.\r
52  *\r
53  * The receiving task is spawned with a higher priority than the transmitting\r
54  * task.  The receiver will therefore wake every time a character is\r
55  * transmitted so neither the Tx or Rx queue should ever hold more than a few\r
56  * characters.\r
57  *\r
58  */\r
59 \r
60 /* Scheduler include files. */\r
61 #include <stdlib.h>\r
62 #include "FreeRTOS.h"\r
63 #include "task.h"\r
64 \r
65 /* Demo program include files. */\r
66 #include "demo_serial.h"\r
67 #include "comtest2.h"\r
68 #include "partest.h"\r
69 \r
70 #define comSTACK_SIZE                           configMINIMAL_STACK_SIZE\r
71 #define comTX_LED_OFFSET                        ( 0 )\r
72 #define comRX_LED_OFFSET                        ( 1 )\r
73 #define comTOTAL_PERMISSIBLE_ERRORS ( 2 )\r
74 \r
75 /* The Tx task will transmit the sequence of characters at a pseudo random\r
76 interval.  This is the maximum and minimum block time between sends. */\r
77 #define comTX_MAX_BLOCK_TIME            ( ( TickType_t ) 0x96 )\r
78 #define comTX_MIN_BLOCK_TIME            ( ( TickType_t ) 0x32 )\r
79 #define comOFFSET_TIME                          ( ( TickType_t ) 3 )\r
80 \r
81 /* We should find that each character can be queued for Tx immediately and we\r
82 don't have to block to send. */\r
83 #define comNO_BLOCK                                     ( ( TickType_t ) 0 )\r
84 \r
85 /* The Rx task will block on the Rx queue for a long period. */\r
86 #define comRX_BLOCK_TIME                        ( ( TickType_t ) 0xffff )\r
87 \r
88 /* The sequence transmitted is from comFIRST_BYTE to and including comLAST_BYTE. */\r
89 #define comFIRST_BYTE                           ( 'A' )\r
90 #define comLAST_BYTE                            ( 'X' )\r
91 \r
92 #define comBUFFER_LEN                           ( ( unsigned portBASE_TYPE ) ( comLAST_BYTE - comFIRST_BYTE ) + ( unsigned portBASE_TYPE ) 1 )\r
93 #define comINITIAL_RX_COUNT_VALUE       ( 0 )\r
94 \r
95 /* Handle to the com port used by both tasks. */\r
96 static xComPortHandle xPort = NULL;\r
97 \r
98 /* The transmit task as described at the top of the file. */\r
99 static portTASK_FUNCTION_PROTO( vComTxTask, pvParameters );\r
100 \r
101 /* The receive task as described at the top of the file. */\r
102 static portTASK_FUNCTION_PROTO( vComRxTask, pvParameters );\r
103 \r
104 /* The LED that should be toggled by the Rx and Tx tasks.  The Rx task will\r
105 toggle LED ( uxBaseLED + comRX_LED_OFFSET).  The Tx task will toggle LED\r
106 ( uxBaseLED + comTX_LED_OFFSET ). */\r
107 static unsigned portBASE_TYPE uxBaseLED = 0;\r
108 \r
109 /* Check variable used to ensure no error have occurred.  The Rx task will\r
110 increment this variable after every successfully received sequence.  If at any\r
111 time the sequence is incorrect the the variable will stop being incremented. */\r
112 static volatile unsigned portBASE_TYPE uxRxLoops = comINITIAL_RX_COUNT_VALUE;\r
113 \r
114 /*-----------------------------------------------------------*/\r
115 \r
116 void vAltStartComTestTasks( unsigned portBASE_TYPE uxPriority, unsigned long ulBaudRate, unsigned portBASE_TYPE uxLED )\r
117 {\r
118         /* Initialise the com port then spawn the Rx and Tx tasks. */\r
119         uxBaseLED = uxLED;\r
120         xSerialPortInitMinimal( ulBaudRate, comBUFFER_LEN );\r
121 \r
122         /* The Tx task is spawned with a lower priority than the Rx task. */\r
123         xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority - 1, ( TaskHandle_t * ) NULL );\r
124         xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority, ( TaskHandle_t * ) NULL );\r
125 }\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 static portTASK_FUNCTION( vComTxTask, pvParameters )\r
129 {\r
130 signed char cByteToSend;\r
131 TickType_t xTimeToWait;\r
132 \r
133         /* Just to stop compiler warnings. */\r
134         ( void ) pvParameters;\r
135 \r
136         for( ;; )\r
137         {\r
138                 /* Simply transmit a sequence of characters from comFIRST_BYTE to\r
139                 comLAST_BYTE. */\r
140                 for( cByteToSend = comFIRST_BYTE; cByteToSend <= comLAST_BYTE; cByteToSend++ )\r
141                 {\r
142                         if( xSerialPutChar( xPort, cByteToSend, comNO_BLOCK ) == pdPASS )\r
143                         {\r
144                                 vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );\r
145                         }\r
146                 }\r
147 \r
148                 /* Turn the LED off while we are not doing anything. */\r
149                 vParTestSetLED( uxBaseLED + comTX_LED_OFFSET, pdFALSE );\r
150 \r
151                 /* We have posted all the characters in the string - wait before\r
152                 re-sending.  Wait a pseudo-random time as this will provide a better\r
153                 test. */\r
154                 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;\r
155 \r
156                 /* Make sure we don't wait too long... */\r
157                 xTimeToWait %= comTX_MAX_BLOCK_TIME;\r
158 \r
159                 /* ...but we do want to wait. */\r
160                 if( xTimeToWait < comTX_MIN_BLOCK_TIME )\r
161                 {\r
162                         xTimeToWait = comTX_MIN_BLOCK_TIME;\r
163                 }\r
164 \r
165                 vTaskDelay( xTimeToWait );\r
166         }\r
167 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 static portTASK_FUNCTION( vComRxTask, pvParameters )\r
171 {\r
172 signed char cExpectedByte, cByteRxed;\r
173 portBASE_TYPE xResyncRequired = pdFALSE, xErrorOccurred = pdFALSE;\r
174 \r
175         /* Just to stop compiler warnings. */\r
176         ( void ) pvParameters;\r
177 \r
178         for( ;; )\r
179         {\r
180                 /* We expect to receive the characters from comFIRST_BYTE to\r
181                 comLAST_BYTE in an incrementing order.  Loop to receive each byte. */\r
182                 for( cExpectedByte = comFIRST_BYTE; cExpectedByte <= comLAST_BYTE; cExpectedByte++ )\r
183                 {\r
184                         /* Block on the queue that contains received bytes until a byte is\r
185                         available. */\r
186                         if( xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ) )\r
187                         {\r
188                                 /* Was this the byte we were expecting?  If so, toggle the LED,\r
189                                 otherwise we are out on sync and should break out of the loop\r
190                                 until the expected character sequence is about to restart. */\r
191                                 if( cByteRxed == cExpectedByte )\r
192                                 {\r
193                                         vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );\r
194                                 }\r
195                                 else\r
196                                 {\r
197                                         xResyncRequired = pdTRUE;\r
198                                         break; /*lint !e960 Non-switch break allowed. */\r
199                                 }\r
200                         }\r
201                 }\r
202 \r
203                 /* Turn the LED off while we are not doing anything. */\r
204                 vParTestSetLED( uxBaseLED + comRX_LED_OFFSET, pdFALSE );\r
205 \r
206                 /* Did we break out of the loop because the characters were received in\r
207                 an unexpected order?  If so wait here until the character sequence is\r
208                 about to restart. */\r
209                 if( xResyncRequired == pdTRUE )\r
210                 {\r
211                         while( cByteRxed != comLAST_BYTE )\r
212                         {\r
213                                 /* Block until the next char is available. */\r
214                                 xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME );\r
215                         }\r
216 \r
217                         /* Note that an error occurred which caused us to have to resync.\r
218                         We use this to stop incrementing the loop counter so\r
219                         sAreComTestTasksStillRunning() will return false - indicating an\r
220                         error. */\r
221                         xErrorOccurred++;\r
222 \r
223                         /* We have now resynced with the Tx task and can continue. */\r
224                         xResyncRequired = pdFALSE;\r
225                 }\r
226                 else\r
227                 {\r
228                         if( xErrorOccurred < comTOTAL_PERMISSIBLE_ERRORS )\r
229                         {\r
230                                 /* Increment the count of successful loops.  As error\r
231                                 occurring (i.e. an unexpected character being received) will\r
232                                 prevent this counter being incremented for the rest of the\r
233                                 execution.   Don't worry about mutual exclusion on this\r
234                                 variable - it doesn't really matter as we just want it\r
235                                 to change. */\r
236                                 uxRxLoops++;\r
237                         }\r
238                 }\r
239         }\r
240 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 portBASE_TYPE xAreComTestTasksStillRunning( void )\r
244 {\r
245 portBASE_TYPE xReturn;\r
246 \r
247         /* If the count of successful reception loops has not changed than at\r
248         some time an error occurred (i.e. a character was received out of sequence)\r
249         and we will return false. */\r
250         if( uxRxLoops == comINITIAL_RX_COUNT_VALUE )\r
251         {\r
252                 xReturn = pdFALSE;\r
253         }\r
254         else\r
255         {\r
256                 xReturn = pdTRUE;\r
257         }\r
258 \r
259         /* Reset the count of successful Rx loops.  When this function is called\r
260         again we expect this to have been incremented. */\r
261         uxRxLoops = comINITIAL_RX_COUNT_VALUE;\r
262 \r
263         return xReturn;\r
264 }\r
265 \r