]> begriffs open source - cmsis-freertos/blob - Demo/Common/Minimal/comtest_strings.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / Common / Minimal / comtest_strings.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70
71 /*
72  * Creates a task and a timer that operate on an interrupt driven serial port.
73  * This demo assumes that the characters transmitted on a port will also be
74  * received on the same port.  Therefore, the UART must either be connected to
75  * an echo server, or the uart connector must have a loopback connector fitted.
76  * See http://www.serialporttool.com/CommEcho.htm for a suitable echo server
77  * for Windows hosts.
78  *
79  * The timer sends a string to the UART, toggles an LED, then resets itself by
80  * changing its own period.  The period is calculated as a pseudo random number
81  * between comTX_MAX_BLOCK_TIME and comTX_MIN_BLOCK_TIME.
82  *
83  * The task blocks on an Rx queue waiting for a character to become available.
84  * Received characters are checked to ensure they match those transmitted by the
85  * Tx timer.  An error is latched if characters are missing, incorrect, or
86  * arrive too slowly.
87  *
88  * How characters are actually transmitted and received is port specific.  Demos
89  * that include this test/demo file will provide example drivers.  The Tx timer
90  * executes in the context of the timer service (daemon) task, and must
91  * therefore never attempt to block.
92  *
93  */
94
95 /* Scheduler include files. */
96 #include <stdlib.h>
97 #include <string.h>
98 #include "FreeRTOS.h"
99 #include "task.h"
100 #include "timers.h"
101
102 #ifndef configUSE_TIMERS
103         #error This demo uses timers.  configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.
104 #endif
105
106 #if configUSE_TIMERS != 1
107         #error This demo uses timers.  configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.
108 #endif
109
110
111 /* Demo program include files. */
112 #include "serial.h"
113 #include "comtest_strings.h"
114 #include "partest.h"
115
116 /* The size of the stack given to the Rx task. */
117 #define comSTACK_SIZE                           configMINIMAL_STACK_SIZE
118
119 /* See the comment above the declaraction of the uxBaseLED variable. */
120 #define comTX_LED_OFFSET                        ( 0 )
121 #define comRX_LED_OFFSET                        ( 1 )
122
123 /* The Tx timer transmits the sequence of characters at a pseudo random
124 interval that is capped between comTX_MAX_BLOCK_TIME and
125 comTX_MIN_BLOCK_TIME. */
126 #define comTX_MAX_BLOCK_TIME            ( ( TickType_t ) 0x96 )
127 #define comTX_MIN_BLOCK_TIME            ( ( TickType_t ) 0x32 )
128 #define comOFFSET_TIME                          ( ( TickType_t ) 3 )
129
130 /* States for the simple state machine implemented in the Rx task. */
131 #define comtstWAITING_START_OF_STRING   0
132 #define comtstWAITING_END_OF_STRING             1
133
134 /* A short delay in ticks - this delay is used to allow the Rx queue to fill up
135 a bit so more than one character can be processed at a time.  This is relative
136 to comTX_MIN_BLOCK_TIME to ensure it is never longer than the shortest gap
137 between transmissions.  It could be worked out more scientifically from the
138 baud rate being used. */
139 #define comSHORT_DELAY                          ( comTX_MIN_BLOCK_TIME >> ( TickType_t ) 2 )
140
141 /* The string that is transmitted and received. */
142 #define comTRANSACTED_STRING            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
143
144 /* A block time of 0 simply means "don't block". */
145 #define comtstDONT_BLOCK                        ( TickType_t ) 0
146
147 /* Handle to the com port used by both tasks. */
148 static xComPortHandle xPort = NULL;
149
150 /* The callback function allocated to the transmit timer, as described in the
151 comments at the top of this file. */
152 static void prvComTxTimerCallback( TimerHandle_t xTimer );
153
154 /* The receive task as described in the comments at the top of this file. */
155 static void vComRxTask( void *pvParameters );
156
157 /* The Rx task will toggle LED ( uxBaseLED + comRX_LED_OFFSET).  The Tx task
158 will toggle LED ( uxBaseLED + comTX_LED_OFFSET ). */
159 static UBaseType_t uxBaseLED = 0;
160
161 /* The Rx task toggles uxRxLoops on each successful iteration of its defined
162 function - provided no errors have ever been latched.  If this variable stops
163 incrementing, then an error has occurred. */
164 static volatile UBaseType_t uxRxLoops = 0UL;
165
166 /* The timer used to periodically transmit the string.  This is the timer that
167 has prvComTxTimerCallback allocated to it as its callback function. */
168 static TimerHandle_t xTxTimer = NULL;
169
170 /* The string length is held at file scope so the Tx timer does not need to
171 calculate it each time it executes. */
172 static size_t xStringLength = 0U;
173
174 /*-----------------------------------------------------------*/
175
176 void vStartComTestStringsTasks( UBaseType_t uxPriority, uint32_t ulBaudRate, UBaseType_t uxLED )
177 {
178         /* Store values that are used at run time. */
179         uxBaseLED = uxLED;
180
181         /* Calculate the string length here, rather than each time the Tx timer
182         executes. */
183         xStringLength = strlen( comTRANSACTED_STRING );
184
185         /* Include the null terminator in the string length as this is used to
186         detect the end of the string in the Rx task. */
187         xStringLength++;
188
189         /* Initialise the com port, then spawn the Rx task and create the Tx
190         timer. */
191         xSerialPortInitMinimal( ulBaudRate, ( xStringLength * 2U ) );
192
193         /* Create the Rx task and the Tx timer.  The timer is started from the
194         Rx task. */
195         xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority, ( TaskHandle_t * ) NULL );
196         xTxTimer = xTimerCreate( "TxTimer", comTX_MIN_BLOCK_TIME, pdFALSE, NULL, prvComTxTimerCallback );
197         configASSERT( xTxTimer );
198 }
199 /*-----------------------------------------------------------*/
200
201 static void prvComTxTimerCallback( TimerHandle_t xTimer )
202 {
203 TickType_t xTimeToWait;
204
205         /* The parameter is not used in this case. */
206         ( void ) xTimer;
207
208         /* Send the string.  How this is actually performed depends on the
209         sample driver provided with this demo.  However - as this is a timer,
210         it executes in the context of the timer task and therefore must not
211         block. */
212         vSerialPutString( xPort, comTRANSACTED_STRING, xStringLength );
213
214         /* Toggle an LED to give a visible indication that another transmission
215         has been performed. */
216         vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );
217
218         /* Wait a pseudo random time before sending the string again. */
219         xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;
220
221         /* Ensure the time to wait is not greater than comTX_MAX_BLOCK_TIME. */
222         xTimeToWait %= comTX_MAX_BLOCK_TIME;
223
224         /* Ensure the time to wait is not less than comTX_MIN_BLOCK_TIME. */
225         if( xTimeToWait < comTX_MIN_BLOCK_TIME )
226         {
227                 xTimeToWait = comTX_MIN_BLOCK_TIME;
228         }
229
230         /* Reset the timer to run again xTimeToWait ticks from now.  This function
231         is called from the context of the timer task, so the block time must not
232         be anything other than zero. */
233         xTimerChangePeriod( xTxTimer, xTimeToWait, comtstDONT_BLOCK );
234 }
235 /*-----------------------------------------------------------*/
236
237 static void vComRxTask( void *pvParameters )
238 {
239 BaseType_t xState = comtstWAITING_START_OF_STRING, xErrorOccurred = pdFALSE;
240 char *pcExpectedByte, cRxedChar;
241 const xComPortHandle xPort = NULL;
242
243         /* The parameter is not used in this example. */
244         ( void ) pvParameters;
245
246         /* Start the Tx timer.  This only needs to be started once, as it will
247         reset itself thereafter. */
248         xTimerStart( xTxTimer, portMAX_DELAY );
249
250         /* The first expected Rx character is the first in the string that is
251         transmitted. */
252         pcExpectedByte = comTRANSACTED_STRING;
253
254         for( ;; )
255         {
256                 /* Wait for the next character. */
257                 if( xSerialGetChar( xPort, &cRxedChar, ( comTX_MAX_BLOCK_TIME * 2 ) ) == pdFALSE )
258                 {
259                         /* A character definitely should have been received by now.  As a
260                         character was not received an error must have occurred (which might
261                         just be that the loopback connector is not fitted). */
262                         xErrorOccurred = pdTRUE;
263                 }
264
265                 switch( xState )
266                 {
267                         case comtstWAITING_START_OF_STRING:
268                                 if( cRxedChar == *pcExpectedByte )
269                                 {
270                                         /* The received character was the first character of the
271                                         string.  Move to the next state to check each character
272                                         as it comes in until the entire string has been received. */
273                                         xState = comtstWAITING_END_OF_STRING;
274                                         pcExpectedByte++;
275
276                                         /* Block for a short period.  This just allows the Rx queue
277                                         to contain more than one character, and therefore prevent
278                                         thrashing reads to the queue, and repetitive context
279                                         switches as     each character is received. */
280                                         vTaskDelay( comSHORT_DELAY );
281                                 }
282                                 break;
283
284                         case comtstWAITING_END_OF_STRING:
285                                 if( cRxedChar == *pcExpectedByte )
286                                 {
287                                         /* The received character was the expected character.  Was
288                                         it the last character in the string - i.e. the null
289                                         terminator? */
290                                         if( cRxedChar == 0x00 )
291                                         {
292                                                 /* The entire string has been received.  If no errors
293                                                 have been latched, then increment the loop counter to
294                                                 show this task is still healthy. */
295                                                 if( xErrorOccurred == pdFALSE )
296                                                 {
297                                                         uxRxLoops++;
298
299                                                         /* Toggle an LED to give a visible sign that a
300                                                         complete string has been received. */
301                                                         vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );
302                                                 }
303
304                                                 /* Go back to wait for the start of the next string. */
305                                                 pcExpectedByte = comTRANSACTED_STRING;
306                                                 xState = comtstWAITING_START_OF_STRING;
307                                         }
308                                         else
309                                         {
310                                                 /* Wait for the next character in the string. */
311                                                 pcExpectedByte++;
312                                         }
313                                 }
314                                 else
315                                 {
316                                         /* The character received was not that expected. */
317                                         xErrorOccurred = pdTRUE;
318                                 }
319                                 break;
320
321                         default:
322                                 /* Should not get here.  Stop the Rx loop counter from
323                                 incrementing to latch the error. */
324                                 xErrorOccurred = pdTRUE;
325                                 break;
326                 }
327         }
328 }
329 /*-----------------------------------------------------------*/
330
331 BaseType_t xAreComTestTasksStillRunning( void )
332 {
333 BaseType_t xReturn;
334
335         /* If the count of successful reception loops has not changed than at
336         some time an error occurred (i.e. a character was received out of sequence)
337         and false is returned. */
338         if( uxRxLoops == 0UL )
339         {
340                 xReturn = pdFALSE;
341         }
342         else
343         {
344                 xReturn = pdTRUE;
345         }
346
347         /* Reset the count of successful Rx loops.  When this function is called
348         again it should have been incremented again. */
349         uxRxLoops = 0UL;
350
351         return xReturn;
352 }
353