--- /dev/null
+/*\r
+ FreeRTOS.org V4.7.0 - Copyright (C) 2003-2007 Richard Barry.\r
+\r
+ This file is part of the FreeRTOS.org distribution.\r
+\r
+ FreeRTOS.org is free software; you can redistribute it and/or modify\r
+ it under the terms of the GNU General Public License as published by\r
+ the Free Software Foundation; either version 2 of the License, or\r
+ (at your option) any later version.\r
+\r
+ FreeRTOS.org is distributed in the hope that it will be useful,\r
+ but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ GNU General Public License for more details.\r
+\r
+ You should have received a copy of the GNU General Public License\r
+ along with FreeRTOS.org; if not, write to the Free Software\r
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
+\r
+ A special exception to the GPL can be applied should you wish to distribute\r
+ a combined work that includes FreeRTOS.org, without being obliged to provide\r
+ the source code for any proprietary components. See the licensing section\r
+ of http://www.FreeRTOS.org for full details of how and when the exception\r
+ can be applied.\r
+\r
+ ***************************************************************************\r
+ See http://www.FreeRTOS.org for documentation, latest information, license\r
+ and contact details. Please ensure to read the configuration and relevant\r
+ port sections of the online documentation.\r
+\r
+ Also see http://www.SafeRTOS.com a version that has been certified for use\r
+ in safety critical systems, plus commercial licensing, development and\r
+ support options.\r
+ ***************************************************************************\r
+*/\r
+\r
+/*\r
+ * This demo application file demonstrates the use of queues to pass data\r
+ * between co-routines.\r
+ *\r
+ * N represents the number of 'fixed delay' co-routines that are created and\r
+ * is set during initialisation.\r
+ *\r
+ * N 'fixed delay' co-routines are created that just block for a fixed\r
+ * period then post the number of an LED onto a queue. Each such co-routine\r
+ * uses a different block period. A single 'flash' co-routine is also created\r
+ * that blocks on the same queue, waiting for the number of the next LED it\r
+ * should flash. Upon receiving a number it simply toggle the instructed LED\r
+ * then blocks on the queue once more. In this manner each LED from LED 0 to\r
+ * LED N-1 is caused to flash at a different rate.\r
+ *\r
+ * The 'fixed delay' co-routines are created with co-routine priority 0. The\r
+ * flash co-routine is created with co-routine priority 1. This means that\r
+ * the queue should never contain more than a single item. This is because\r
+ * posting to the queue will unblock the 'flash' co-routine, and as this has\r
+ * a priority greater than the tasks posting to the queue it is guaranteed to\r
+ * have emptied the queue and blocked once again before the queue can contain\r
+ * any more date. An error is indicated if an attempt to post data to the\r
+ * queue fails - indicating that the queue is already full.\r
+ *\r
+ */\r
+\r
+/* Scheduler includes. */\r
+#include "FreeRTOS.h"\r
+#include "croutine.h"\r
+#include "queue.h"\r
+\r
+/* Demo application includes. */\r
+#include "partest.h"\r
+#include "crflash.h"\r
+\r
+/* The queue should only need to be of length 1. See the description at the\r
+top of the file. */\r
+#define crfQUEUE_LENGTH 1\r
+\r
+#define crfFIXED_DELAY_PRIORITY 0\r
+#define crfFLASH_PRIORITY 1\r
+\r
+/* Only one flash co-routine is created so the index is not significant. */\r
+#define crfFLASH_INDEX 0\r
+\r
+/* Don't allow more than crfMAX_FLASH_TASKS 'fixed delay' co-routines to be\r
+created. */\r
+#define crfMAX_FLASH_TASKS 8\r
+\r
+/* We don't want to block when posting to the queue. */\r
+#define crfPOSTING_BLOCK_TIME 0\r
+\r
+/* Added by MPi, this define is added in order to make the vParTestToggleLED()\r
+work. This basically differentiates the PDR09 from PDR00. 7-seg display LEDs connected \r
+to PDR09 (SEG1) are used by the prvFlashCoRoutine() and PDR00 (SEG2) are used by tasks. */ \r
+#define PDR00_Offset 8\r
+\r
+/*\r
+ * The 'fixed delay' co-routine as described at the top of the file.\r
+ */\r
+static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
+\r
+/*\r
+ * The 'flash' co-routine as described at the top of the file.\r
+ */\r
+static void prvFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
+\r
+/* The queue used to pass data between the 'fixed delay' co-routines and the\r
+'flash' co-routine. */\r
+static xQueueHandle xFlashQueue;\r
+\r
+/* This will be set to pdFALSE if we detect an error. */\r
+static unsigned portBASE_TYPE uxCoRoutineFlashStatus = pdPASS;\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+/*\r
+ * See the header file for details.\r
+ */\r
+void vStartFlashCoRoutines( unsigned portBASE_TYPE uxNumberToCreate )\r
+{\r
+unsigned portBASE_TYPE uxIndex;\r
+\r
+ if( uxNumberToCreate > crfMAX_FLASH_TASKS )\r
+ {\r
+ uxNumberToCreate = crfMAX_FLASH_TASKS;\r
+ }\r
+\r
+ /* Create the queue used to pass data between the co-routines. */\r
+ xFlashQueue = xQueueCreate( crfQUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );\r
+\r
+ if( xFlashQueue )\r
+ {\r
+ /* Create uxNumberToCreate 'fixed delay' co-routines. */\r
+ for( uxIndex = 0; uxIndex < uxNumberToCreate; uxIndex++ )\r
+ {\r
+ xCoRoutineCreate( prvFixedDelayCoRoutine, crfFIXED_DELAY_PRIORITY, uxIndex );\r
+ }\r
+\r
+ /* Create the 'flash' co-routine. */\r
+ xCoRoutineCreate( prvFlashCoRoutine, crfFLASH_PRIORITY, crfFLASH_INDEX );\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
+{\r
+/* Even though this is a co-routine the xResult variable does not need to be\r
+static as we do not need it to maintain its state between blocks. */\r
+signed portBASE_TYPE xResult;\r
+/* The uxIndex parameter of the co-routine function is used as an index into\r
+the xFlashRates array to obtain the delay period to use. */\r
+static const portTickType xFlashRates[ crfMAX_FLASH_TASKS ] = { 150 / portTICK_RATE_MS,\r
+ 200 / portTICK_RATE_MS,\r
+ 250 / portTICK_RATE_MS,\r
+ 300 / portTICK_RATE_MS,\r
+ 350 / portTICK_RATE_MS,\r
+ 400 / portTICK_RATE_MS,\r
+ 450 / portTICK_RATE_MS,\r
+ 500 / portTICK_RATE_MS };\r
+\r
+ /* Co-routines MUST start with a call to crSTART. */\r
+ crSTART( xHandle );\r
+\r
+ for( ;; )\r
+ {\r
+ /* Post our uxIndex value onto the queue. This is used as the LED to\r
+ flash. */\r
+ crQUEUE_SEND( xHandle, xFlashQueue, ( void * ) &uxIndex, crfPOSTING_BLOCK_TIME, &xResult );\r
+\r
+ if( xResult != pdPASS )\r
+ {\r
+ /* For the reasons stated at the top of the file we should always\r
+ find that we can post to the queue. If we could not then an error\r
+ has occurred. */\r
+ uxCoRoutineFlashStatus = pdFAIL;\r
+ }\r
+\r
+ crDELAY( xHandle, xFlashRates[ uxIndex ] );\r
+ }\r
+\r
+ /* Co-routines MUST end with a call to crEND. */\r
+ crEND();\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
+{\r
+/* Even though this is a co-routine the variable do not need to be\r
+static as we do not need it to maintain their state between blocks. */\r
+signed portBASE_TYPE xResult;\r
+unsigned portBASE_TYPE uxLEDToFlash;\r
+\r
+ /* Co-routines MUST start with a call to crSTART. */\r
+ crSTART( xHandle );\r
+ ( void ) uxIndex;\r
+ \r
+ for( ;; )\r
+ {\r
+ /* Block to wait for the number of the LED to flash. */\r
+ crQUEUE_RECEIVE( xHandle, xFlashQueue, &uxLEDToFlash, portMAX_DELAY, &xResult ); \r
+\r
+ if( xResult != pdPASS )\r
+ {\r
+ /* We would not expect to wake unless we received something. */\r
+ uxCoRoutineFlashStatus = pdFAIL;\r
+ }\r
+ else\r
+ {\r
+ /* We received the number of an LED to flash - flash it! */\r
+ /* Added by MPi, PDR00_Offset is added in order to make the \r
+ vParTestToggleLED() work. */ \r
+ vParTestToggleLED( uxLEDToFlash + PDR00_Offset );\r
+ }\r
+ }\r
+\r
+ /* Co-routines MUST end with a call to crEND. */\r
+ crEND();\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+portBASE_TYPE xAreFlashCoRoutinesStillRunning( void )\r
+{\r
+ /* Return pdPASS or pdFAIL depending on whether an error has been detected\r
+ or not. */\r
+ return uxCoRoutineFlashStatus;\r
+}\r
+\r