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