]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4S_Atmel_Studio/src/Common-Demo-Source/comtest.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4S_Atmel_Studio / src / Common-Demo-Source / comtest.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /*\r
72  * This version of comtest. c is for use on systems that have limited stack\r
73  * space and no display facilities.  The complete version can be found in\r
74  * the Demo/Common/Full directory.\r
75  *\r
76  * Creates two tasks that operate on an interrupt driven serial port.  A\r
77  * loopback connector should be used so that everything that is transmitted is\r
78  * also received.  The serial port does not use any flow control.  On a\r
79  * standard 9way 'D' connector pins two and three should be connected together.\r
80  *\r
81  * The first task posts a sequence of characters to the Tx queue, toggling an\r
82  * LED on each successful post.  At the end of the sequence it sleeps for a\r
83  * pseudo-random period before resending the same sequence.\r
84  *\r
85  * The UART Tx end interrupt is enabled whenever data is available in the Tx\r
86  * queue.  The Tx end ISR removes a single character from the Tx queue and\r
87  * passes it to the UART for transmission.\r
88  *\r
89  * The second task blocks on the Rx queue waiting for a character to become\r
90  * available.  When the UART Rx end interrupt receives a character it places\r
91  * it in the Rx queue, waking the second task.  The second task checks that the\r
92  * characters removed from the Rx queue form the same sequence as those posted\r
93  * to the Tx queue, and toggles an LED for each correct character.\r
94  *\r
95  * The receiving task is spawned with a higher priority than the transmitting\r
96  * task.  The receiver will therefore wake every time a character is\r
97  * transmitted so neither the Tx or Rx queue should ever hold more than a few\r
98  * characters.\r
99  *\r
100  */\r
101 \r
102 /* Scheduler include files. */\r
103 #include <stdlib.h>\r
104 #include "FreeRTOS.h"\r
105 #include "task.h"\r
106 \r
107 /* Demo program include files. */\r
108 #include "demo_serial.h"\r
109 #include "comtest2.h"\r
110 #include "partest.h"\r
111 \r
112 #define comSTACK_SIZE                           configMINIMAL_STACK_SIZE\r
113 #define comTX_LED_OFFSET                        ( 0 )\r
114 #define comRX_LED_OFFSET                        ( 1 )\r
115 #define comTOTAL_PERMISSIBLE_ERRORS ( 2 )\r
116 \r
117 /* The Tx task will transmit the sequence of characters at a pseudo random\r
118 interval.  This is the maximum and minimum block time between sends. */\r
119 #define comTX_MAX_BLOCK_TIME            ( ( TickType_t ) 0x96 )\r
120 #define comTX_MIN_BLOCK_TIME            ( ( TickType_t ) 0x32 )\r
121 #define comOFFSET_TIME                          ( ( TickType_t ) 3 )\r
122 \r
123 /* We should find that each character can be queued for Tx immediately and we\r
124 don't have to block to send. */\r
125 #define comNO_BLOCK                                     ( ( TickType_t ) 0 )\r
126 \r
127 /* The Rx task will block on the Rx queue for a long period. */\r
128 #define comRX_BLOCK_TIME                        ( ( TickType_t ) 0xffff )\r
129 \r
130 /* The sequence transmitted is from comFIRST_BYTE to and including comLAST_BYTE. */\r
131 #define comFIRST_BYTE                           ( 'A' )\r
132 #define comLAST_BYTE                            ( 'X' )\r
133 \r
134 #define comBUFFER_LEN                           ( ( unsigned portBASE_TYPE ) ( comLAST_BYTE - comFIRST_BYTE ) + ( unsigned portBASE_TYPE ) 1 )\r
135 #define comINITIAL_RX_COUNT_VALUE       ( 0 )\r
136 \r
137 /* Handle to the com port used by both tasks. */\r
138 static xComPortHandle xPort = NULL;\r
139 \r
140 /* The transmit task as described at the top of the file. */\r
141 static portTASK_FUNCTION_PROTO( vComTxTask, pvParameters );\r
142 \r
143 /* The receive task as described at the top of the file. */\r
144 static portTASK_FUNCTION_PROTO( vComRxTask, pvParameters );\r
145 \r
146 /* The LED that should be toggled by the Rx and Tx tasks.  The Rx task will\r
147 toggle LED ( uxBaseLED + comRX_LED_OFFSET).  The Tx task will toggle LED\r
148 ( uxBaseLED + comTX_LED_OFFSET ). */\r
149 static unsigned portBASE_TYPE uxBaseLED = 0;\r
150 \r
151 /* Check variable used to ensure no error have occurred.  The Rx task will\r
152 increment this variable after every successfully received sequence.  If at any\r
153 time the sequence is incorrect the the variable will stop being incremented. */\r
154 static volatile unsigned portBASE_TYPE uxRxLoops = comINITIAL_RX_COUNT_VALUE;\r
155 \r
156 /*-----------------------------------------------------------*/\r
157 \r
158 void vAltStartComTestTasks( unsigned portBASE_TYPE uxPriority, unsigned long ulBaudRate, unsigned portBASE_TYPE uxLED )\r
159 {\r
160         /* Initialise the com port then spawn the Rx and Tx tasks. */\r
161         uxBaseLED = uxLED;\r
162         xSerialPortInitMinimal( ulBaudRate, comBUFFER_LEN );\r
163 \r
164         /* The Tx task is spawned with a lower priority than the Rx task. */\r
165         xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority - 1, ( TaskHandle_t * ) NULL );\r
166         xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority, ( TaskHandle_t * ) NULL );\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 static portTASK_FUNCTION( vComTxTask, pvParameters )\r
171 {\r
172 signed char cByteToSend;\r
173 TickType_t xTimeToWait;\r
174 \r
175         /* Just to stop compiler warnings. */\r
176         ( void ) pvParameters;\r
177 \r
178         for( ;; )\r
179         {\r
180                 /* Simply transmit a sequence of characters from comFIRST_BYTE to\r
181                 comLAST_BYTE. */\r
182                 for( cByteToSend = comFIRST_BYTE; cByteToSend <= comLAST_BYTE; cByteToSend++ )\r
183                 {\r
184                         if( xSerialPutChar( xPort, cByteToSend, comNO_BLOCK ) == pdPASS )\r
185                         {\r
186                                 vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );\r
187                         }\r
188                 }\r
189 \r
190                 /* Turn the LED off while we are not doing anything. */\r
191                 vParTestSetLED( uxBaseLED + comTX_LED_OFFSET, pdFALSE );\r
192 \r
193                 /* We have posted all the characters in the string - wait before\r
194                 re-sending.  Wait a pseudo-random time as this will provide a better\r
195                 test. */\r
196                 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;\r
197 \r
198                 /* Make sure we don't wait too long... */\r
199                 xTimeToWait %= comTX_MAX_BLOCK_TIME;\r
200 \r
201                 /* ...but we do want to wait. */\r
202                 if( xTimeToWait < comTX_MIN_BLOCK_TIME )\r
203                 {\r
204                         xTimeToWait = comTX_MIN_BLOCK_TIME;\r
205                 }\r
206 \r
207                 vTaskDelay( xTimeToWait );\r
208         }\r
209 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 static portTASK_FUNCTION( vComRxTask, pvParameters )\r
213 {\r
214 signed char cExpectedByte, cByteRxed;\r
215 portBASE_TYPE xResyncRequired = pdFALSE, xErrorOccurred = pdFALSE;\r
216 \r
217         /* Just to stop compiler warnings. */\r
218         ( void ) pvParameters;\r
219 \r
220         for( ;; )\r
221         {\r
222                 /* We expect to receive the characters from comFIRST_BYTE to\r
223                 comLAST_BYTE in an incrementing order.  Loop to receive each byte. */\r
224                 for( cExpectedByte = comFIRST_BYTE; cExpectedByte <= comLAST_BYTE; cExpectedByte++ )\r
225                 {\r
226                         /* Block on the queue that contains received bytes until a byte is\r
227                         available. */\r
228                         if( xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ) )\r
229                         {\r
230                                 /* Was this the byte we were expecting?  If so, toggle the LED,\r
231                                 otherwise we are out on sync and should break out of the loop\r
232                                 until the expected character sequence is about to restart. */\r
233                                 if( cByteRxed == cExpectedByte )\r
234                                 {\r
235                                         vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );\r
236                                 }\r
237                                 else\r
238                                 {\r
239                                         xResyncRequired = pdTRUE;\r
240                                         break; /*lint !e960 Non-switch break allowed. */\r
241                                 }\r
242                         }\r
243                 }\r
244 \r
245                 /* Turn the LED off while we are not doing anything. */\r
246                 vParTestSetLED( uxBaseLED + comRX_LED_OFFSET, pdFALSE );\r
247 \r
248                 /* Did we break out of the loop because the characters were received in\r
249                 an unexpected order?  If so wait here until the character sequence is\r
250                 about to restart. */\r
251                 if( xResyncRequired == pdTRUE )\r
252                 {\r
253                         while( cByteRxed != comLAST_BYTE )\r
254                         {\r
255                                 /* Block until the next char is available. */\r
256                                 xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME );\r
257                         }\r
258 \r
259                         /* Note that an error occurred which caused us to have to resync.\r
260                         We use this to stop incrementing the loop counter so\r
261                         sAreComTestTasksStillRunning() will return false - indicating an\r
262                         error. */\r
263                         xErrorOccurred++;\r
264 \r
265                         /* We have now resynced with the Tx task and can continue. */\r
266                         xResyncRequired = pdFALSE;\r
267                 }\r
268                 else\r
269                 {\r
270                         if( xErrorOccurred < comTOTAL_PERMISSIBLE_ERRORS )\r
271                         {\r
272                                 /* Increment the count of successful loops.  As error\r
273                                 occurring (i.e. an unexpected character being received) will\r
274                                 prevent this counter being incremented for the rest of the\r
275                                 execution.   Don't worry about mutual exclusion on this\r
276                                 variable - it doesn't really matter as we just want it\r
277                                 to change. */\r
278                                 uxRxLoops++;\r
279                         }\r
280                 }\r
281         }\r
282 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 portBASE_TYPE xAreComTestTasksStillRunning( void )\r
286 {\r
287 portBASE_TYPE xReturn;\r
288 \r
289         /* If the count of successful reception loops has not changed than at\r
290         some time an error occurred (i.e. a character was received out of sequence)\r
291         and we will return false. */\r
292         if( uxRxLoops == comINITIAL_RX_COUNT_VALUE )\r
293         {\r
294                 xReturn = pdFALSE;\r
295         }\r
296         else\r
297         {\r
298                 xReturn = pdTRUE;\r
299         }\r
300 \r
301         /* Reset the count of successful Rx loops.  When this function is called\r
302         again we expect this to have been incremented. */\r
303         uxRxLoops = comINITIAL_RX_COUNT_VALUE;\r
304 \r
305         return xReturn;\r
306 }\r
307 \r