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