--- /dev/null
+/*\r
+ FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
+ \r
+\r
+ ***************************************************************************\r
+ * *\r
+ * FreeRTOS tutorial books are available in pdf and paperback. *\r
+ * Complete, revised, and edited pdf reference manuals are also *\r
+ * available. *\r
+ * *\r
+ * Purchasing FreeRTOS documentation will not only help you, by *\r
+ * ensuring you get running as quickly as possible and with an *\r
+ * in-depth knowledge of how to use FreeRTOS, it will also help *\r
+ * the FreeRTOS project to continue with its mission of providing *\r
+ * professional grade, cross platform, de facto standard solutions *\r
+ * for microcontrollers - completely free of charge! *\r
+ * *\r
+ * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *\r
+ * *\r
+ * Thank you for using FreeRTOS, and thank you for your support! *\r
+ * *\r
+ ***************************************************************************\r
+\r
+\r
+ This file is part of the FreeRTOS distribution.\r
+\r
+ FreeRTOS is free software; you can redistribute it and/or modify it under\r
+ the terms of the GNU General Public License (version 2) as published by the\r
+ Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
+ >>>NOTE<<< The modification to the GPL is included to allow you to\r
+ distribute a combined work that includes FreeRTOS without being obliged to\r
+ provide the source code for proprietary components outside of the FreeRTOS\r
+ kernel. FreeRTOS is distributed in the hope that it will be useful, but\r
+ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for\r
+ more details. You should have received a copy of the GNU General Public\r
+ License and the FreeRTOS license exception along with FreeRTOS; if not it\r
+ can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
+ by writing to Richard Barry, contact details for whom are available on the\r
+ FreeRTOS WEB site.\r
+\r
+ 1 tab == 4 spaces!\r
+\r
+ http://www.FreeRTOS.org - Documentation, latest information, license and\r
+ contact details.\r
+\r
+ http://www.SafeRTOS.com - A version that is certified for use in safety\r
+ critical systems.\r
+\r
+ http://www.OpenRTOS.com - Commercial support, development, porting,\r
+ licensing and training services.\r
+*/\r
+\r
+\r
+/*\r
+ * Creates a task and a timer that operate on an interrupt driven serial port.\r
+ * This demo assumes that the characters transmitted on a port will also be\r
+ * received on the same port. Therefore, the UART must either be connected to\r
+ * an echo server, or the uart connector must have a loopback connector fitted.\r
+ * See http://www.serialporttool.com/CommEcho.htm for a suitable echo server\r
+ * for Windows hosts.\r
+ *\r
+ * The timer sends a string to the UART, toggles an LED, then resets itself by \r
+ * changing its own period. The period is calculated as a pseudo random number \r
+ * between comTX_MAX_BLOCK_TIME and comTX_MIN_BLOCK_TIME.\r
+ *\r
+ * The task blocks on an Rx queue waiting for a character to become available. \r
+ * Received characters are checked to ensure they match those transmitted by the \r
+ * Tx timer. An error is latched if characters are missing, incorrect, or \r
+ * arrive too slowly.\r
+ *\r
+ * How characters are actually transmitted and received is port specific. Demos\r
+ * that include this test/demo file will provide example drivers. The Tx timer\r
+ * executes in the context of the timer service (daemon) task, and must \r
+ * therefore never attempt to block.\r
+ *\r
+ */\r
+\r
+/* Scheduler include files. */\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include "FreeRTOS.h"\r
+#include "task.h"\r
+#include "timers.h"\r
+\r
+#ifndef configUSE_TIMERS\r
+ #error This demo uses timers. configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.\r
+#endif\r
+\r
+#if configUSE_TIMERS != 1\r
+ #error This demo uses timers. configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.\r
+#endif\r
+\r
+\r
+/* Demo program include files. */\r
+#include "serial.h"\r
+#include "comtest_strings.h"\r
+#include "partest.h"\r
+\r
+/* The size of the stack given to the Rx task. */\r
+#define comSTACK_SIZE configMINIMAL_STACK_SIZE\r
+\r
+/* See the comment above the declaraction of the uxBaseLED variable. */\r
+#define comTX_LED_OFFSET ( 0 )\r
+#define comRX_LED_OFFSET ( 1 )\r
+\r
+/* The Tx timer transmits the sequence of characters at a pseudo random\r
+interval that is capped between comTX_MAX_BLOCK_TIME and\r
+comTX_MIN_BLOCK_TIME. */\r
+#define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x96 )\r
+#define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0x32 )\r
+#define comOFFSET_TIME ( ( portTickType ) 3 )\r
+\r
+/* States for the simple state machine implemented in the Rx task. */\r
+#define comtstWAITING_START_OF_STRING 0\r
+#define comtstWAITING_END_OF_STRING 1\r
+\r
+/* A short delay in ticks - this delay is used to allow the Rx queue to fill up\r
+a bit so more than one character can be processed at a time. This is relative\r
+to comTX_MIN_BLOCK_TIME to ensure it is never longer than the shortest gap\r
+between transmissions. It could be worked out more scientifically from the\r
+baud rate being used. */\r
+#define comSHORT_DELAY ( comTX_MIN_BLOCK_TIME >> ( portTickType ) 2 )\r
+\r
+/* The string that is transmitted and received. */\r
+#define comTRANSACTED_STRING "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"\r
+\r
+/* A block time of 0 simply means "don't block". */\r
+#define comtstDONT_BLOCK ( portTickType ) 0\r
+\r
+/* Handle to the com port used by both tasks. */\r
+static xComPortHandle xPort = NULL;\r
+\r
+/* The callback function allocated to the transmit timer, as described in the\r
+comments at the top of this file. */\r
+static void prvComTxTimerCallback( xTimerHandle xTimer );\r
+\r
+/* The receive task as described in the comments at the top of this file. */\r
+static void vComRxTask( void *pvParameters );\r
+\r
+/* The Rx task will toggle LED ( uxBaseLED + comRX_LED_OFFSET). The Tx task\r
+will toggle LED ( uxBaseLED + comTX_LED_OFFSET ). */\r
+static unsigned portBASE_TYPE uxBaseLED = 0;\r
+\r
+/* The Rx task toggles uxRxLoops on each successful iteration of its defined\r
+function - provided no errors have ever been latched. If this variable stops\r
+incrementing, then an error has occurred. */\r
+static volatile unsigned portBASE_TYPE uxRxLoops = 0UL;\r
+\r
+/* The timer used to periodically transmit the string. This is the timer that\r
+has prvComTxTimerCallback allocated to it as its callback function. */\r
+static xTimerHandle xTxTimer = NULL;\r
+\r
+/* The string length is held at file scope so the Tx timer does not need to\r
+calculate it each time it executes. */
+static size_t xStringLength = 0U;\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void vStartComTestStringsTasks( unsigned portBASE_TYPE uxPriority, unsigned long ulBaudRate, unsigned portBASE_TYPE uxLED )\r
+{\r
+ /* Store values that are used at run time. */\r
+ uxBaseLED = uxLED;\r
+\r
+ /* Calculate the string length here, rather than each time the Tx timer\r
+ executes. */\r
+ xStringLength = strlen( comTRANSACTED_STRING );\r
+\r
+ /* Include the null terminator in the string length as this is used to\r
+ detect the end of the string in the Rx task. */\r
+ xStringLength++;\r
+\r
+ /* Initialise the com port, then spawn the Rx task and create the Tx\r
+ timer. */\r
+ xSerialPortInitMinimal( ulBaudRate, ( xStringLength * 2U ) );\r
+\r
+ /* Create the Rx task and the Tx timer. The timer is started from the\r
+ Rx task. */
+ xTaskCreate( vComRxTask, ( signed char * ) "COMRx", comSTACK_SIZE, NULL, uxPriority, ( xTaskHandle * ) NULL );\r
+ xTxTimer = xTimerCreate( ( const signed char * ) "TxTimer", comTX_MIN_BLOCK_TIME, pdFALSE, NULL, prvComTxTimerCallback );\r
+ configASSERT( xTxTimer );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvComTxTimerCallback( xTimerHandle xTimer )\r
+{\r
+portTickType xTimeToWait;\r
+\r
+ /* The parameter is not used in this case. */\r
+ ( void ) xTimer;\r
+\r
+ /* Send the string. How this is actually performed depends on the\r
+ sample driver provided with this demo. However - as this is a timer,\r
+ it executes in the context of the timer task and therefore must not\r
+ block. */\r
+ vSerialPutString( xPort, ( const signed char * const ) comTRANSACTED_STRING, xStringLength );\r
+\r
+ /* Toggle an LED to give a visible indication that another transmission\r
+ has been performed. */
+ vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );\r
+\r
+ /* Wait a pseudo random time before sending the string again. */\r
+ xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;\r
+\r
+ /* Ensure the time to wait is not greater than comTX_MAX_BLOCK_TIME. */\r
+ xTimeToWait %= comTX_MAX_BLOCK_TIME;\r
+\r
+ /* Ensure the time to wait is not less than comTX_MIN_BLOCK_TIME. */\r
+ if( xTimeToWait < comTX_MIN_BLOCK_TIME )\r
+ {\r
+ xTimeToWait = comTX_MIN_BLOCK_TIME;\r
+ }\r
+\r
+ /* Reset the timer to run again xTimeToWait ticks from now. This function\r
+ is called from the context of the timer task, so the block time must not\r
+ be anything other than zero. */\r
+ xTimerChangePeriod( xTxTimer, xTimeToWait, comtstDONT_BLOCK );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void vComRxTask( void *pvParameters )\r
+{\r
+portBASE_TYPE xState = comtstWAITING_START_OF_STRING, xErrorOccurred = pdFALSE;\r
+signed char *pcExpectedByte, cRxedChar;\r
+const xComPortHandle xPort = NULL;\r
+\r
+ /* The parameter is not used in this example. */\r
+ ( void ) pvParameters;\r
+\r
+ /* Start the Tx timer. This only needs to be started once, as it will\r
+ reset itself thereafter. */\r
+ xTimerStart( xTxTimer, portMAX_DELAY );\r
+\r
+ /* The first expected Rx character is the first in the string that is\r
+ transmitted. */
+ pcExpectedByte = ( signed char * ) comTRANSACTED_STRING;\r
+\r
+ for( ;; )\r
+ {\r
+ /* Wait for the next character. */\r
+ if( xSerialGetChar( xPort, &cRxedChar, ( comTX_MAX_BLOCK_TIME * 2 ) ) == pdFALSE )\r
+ {\r
+ /* A character definitely should have been received by now. As a\r
+ character was not received an error must have occurred (which might\r
+ just be that the loopback connector is not fitted). */\r
+ xErrorOccurred = pdTRUE;
+ }\r
+\r
+ switch( xState )\r
+ {\r
+ case comtstWAITING_START_OF_STRING:\r
+ if( cRxedChar == *pcExpectedByte )\r
+ {\r
+ /* The received character was the first character of the\r
+ string. Move to the next state to check each character\r
+ as it comes in until the entire string has been received. */\r
+ xState = comtstWAITING_END_OF_STRING;\r
+ pcExpectedByte++;\r
+\r
+ /* Block for a short period. This just allows the Rx queue \r
+ to contain more than one character, and therefore prevent\r
+ thrashing reads to the queue, and repetitive context \r
+ switches as each character is received. */\r
+ vTaskDelay( comSHORT_DELAY );\r
+ }\r
+ break;\r
+\r
+ case comtstWAITING_END_OF_STRING:\r
+ if( cRxedChar == *pcExpectedByte )\r
+ {\r
+ /* The received character was the expected character. Was\r
+ it the last character in the string - i.e. the null\r
+ terminator? */\r
+ if( cRxedChar == 0x00 )\r
+ {\r
+ /* The entire string has been received. If no errors\r
+ have been latched, then increment the loop counter to\r
+ show this task is still healthy. */
+ if( xErrorOccurred == pdFALSE )\r
+ {\r
+ uxRxLoops++;\r
+\r
+ /* Toggle an LED to give a visible sign that a\r
+ complete string has been received. */
+ vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );\r
+ }\r
+\r
+ /* Go back to wait for the start of the next string. */\r
+ pcExpectedByte = ( signed char * ) comTRANSACTED_STRING;\r
+ xState = comtstWAITING_START_OF_STRING;\r
+ }\r
+ else\r
+ {\r
+ /* Wait for the next character in the string. */\r
+ pcExpectedByte++;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ /* The character received was not that expected. */\r
+ xErrorOccurred = pdTRUE;\r
+ }\r
+ break;\r
+\r
+ default:\r
+ /* Should not get here. Stop the Rx loop counter from\r
+ incrementing to latch the error. */\r
+ xErrorOccurred = pdTRUE;\r
+ break;\r
+ }\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+portBASE_TYPE xAreComTestTasksStillRunning( void )\r
+{\r
+portBASE_TYPE xReturn;\r
+\r
+ /* If the count of successful reception loops has not changed than at\r
+ some time an error occurred (i.e. a character was received out of sequence)\r
+ and false is returned. */\r
+ if( uxRxLoops == 0UL )\r
+ {\r
+ xReturn = pdFALSE;\r
+ }\r
+ else\r
+ {\r
+ xReturn = pdTRUE;\r
+ }\r
+\r
+ /* Reset the count of successful Rx loops. When this function is called\r
+ again it should have been incremented again. */\r
+ uxRxLoops = 0UL;\r
+\r
+ return xReturn;\r
+}\r
+\r
--- /dev/null
+/*\r
+ FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
+ \r
+\r
+ ***************************************************************************\r
+ * *\r
+ * FreeRTOS tutorial books are available in pdf and paperback. *\r
+ * Complete, revised, and edited pdf reference manuals are also *\r
+ * available. *\r
+ * *\r
+ * Purchasing FreeRTOS documentation will not only help you, by *\r
+ * ensuring you get running as quickly as possible and with an *\r
+ * in-depth knowledge of how to use FreeRTOS, it will also help *\r
+ * the FreeRTOS project to continue with its mission of providing *\r
+ * professional grade, cross platform, de facto standard solutions *\r
+ * for microcontrollers - completely free of charge! *\r
+ * *\r
+ * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *\r
+ * *\r
+ * Thank you for using FreeRTOS, and thank you for your support! *\r
+ * *\r
+ ***************************************************************************\r
+\r
+\r
+ This file is part of the FreeRTOS distribution.\r
+\r
+ FreeRTOS is free software; you can redistribute it and/or modify it under\r
+ the terms of the GNU General Public License (version 2) as published by the\r
+ Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
+ >>>NOTE<<< The modification to the GPL is included to allow you to\r
+ distribute a combined work that includes FreeRTOS without being obliged to\r
+ provide the source code for proprietary components outside of the FreeRTOS\r
+ kernel. FreeRTOS is distributed in the hope that it will be useful, but\r
+ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for\r
+ more details. You should have received a copy of the GNU General Public\r
+ License and the FreeRTOS license exception along with FreeRTOS; if not it\r
+ can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
+ by writing to Richard Barry, contact details for whom are available on the\r
+ FreeRTOS WEB site.\r
+\r
+ 1 tab == 4 spaces!\r
+\r
+ http://www.FreeRTOS.org - Documentation, latest information, license and\r
+ contact details.\r
+\r
+ http://www.SafeRTOS.com - A version that is certified for use in safety\r
+ critical systems.\r
+\r
+ http://www.OpenRTOS.com - Commercial support, development, porting,\r
+ licensing and training services.\r
+*/\r
+\r
+#ifndef COMTEST_STRINGS_H\r
+#define COMTEST_STRINGS_H\r
+\r
+void vStartComTestStringsTasks( unsigned portBASE_TYPE uxPriority, unsigned long ulBaudRate, unsigned portBASE_TYPE uxLED );\r
+portBASE_TYPE xAreComTestTasksStillRunning( void );\r
+\r
+#endif\r
+\r