Please see my note book http://mbed.org/users/kenjiArai/notebook/freertos-on-mbed-board-with-mbed-cloud-ide--never-/

This is too old.
Below is another FreeRTOS on mbed.
http://developer.mbed.org/users/rgrover1/code/FreeRTOS/
I don't know it works well or not.
I have not evaluated it.

Committer:
kenjiArai
Date:
Sat Jan 01 11:17:45 2011 +0000
Revision:
0:d4960fcea8ff

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:d4960fcea8ff 1 /*
kenjiArai 0:d4960fcea8ff 2 FreeRTOS V6.0.3 - Copyright (C) 2010 Real Time Engineers Ltd.
kenjiArai 0:d4960fcea8ff 3
kenjiArai 0:d4960fcea8ff 4 ***************************************************************************
kenjiArai 0:d4960fcea8ff 5 * *
kenjiArai 0:d4960fcea8ff 6 * If you are: *
kenjiArai 0:d4960fcea8ff 7 * *
kenjiArai 0:d4960fcea8ff 8 * + New to FreeRTOS, *
kenjiArai 0:d4960fcea8ff 9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
kenjiArai 0:d4960fcea8ff 10 * + Looking for basic training, *
kenjiArai 0:d4960fcea8ff 11 * + Wanting to improve your FreeRTOS skills and productivity *
kenjiArai 0:d4960fcea8ff 12 * *
kenjiArai 0:d4960fcea8ff 13 * then take a look at the FreeRTOS eBook *
kenjiArai 0:d4960fcea8ff 14 * *
kenjiArai 0:d4960fcea8ff 15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
kenjiArai 0:d4960fcea8ff 16 * http://www.FreeRTOS.org/Documentation *
kenjiArai 0:d4960fcea8ff 17 * *
kenjiArai 0:d4960fcea8ff 18 * A pdf reference manual is also available. Both are usually delivered *
kenjiArai 0:d4960fcea8ff 19 * to your inbox within 20 minutes to two hours when purchased between 8am *
kenjiArai 0:d4960fcea8ff 20 * and 8pm GMT (although please allow up to 24 hours in case of *
kenjiArai 0:d4960fcea8ff 21 * exceptional circumstances). Thank you for your support! *
kenjiArai 0:d4960fcea8ff 22 * *
kenjiArai 0:d4960fcea8ff 23 ***************************************************************************
kenjiArai 0:d4960fcea8ff 24
kenjiArai 0:d4960fcea8ff 25 This file is part of the FreeRTOS distribution.
kenjiArai 0:d4960fcea8ff 26
kenjiArai 0:d4960fcea8ff 27 FreeRTOS is free software; you can redistribute it and/or modify it under
kenjiArai 0:d4960fcea8ff 28 the terms of the GNU General Public License (version 2) as published by the
kenjiArai 0:d4960fcea8ff 29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
kenjiArai 0:d4960fcea8ff 30 ***NOTE*** The exception to the GPL is included to allow you to distribute
kenjiArai 0:d4960fcea8ff 31 a combined work that includes FreeRTOS without being obliged to provide the
kenjiArai 0:d4960fcea8ff 32 source code for proprietary components outside of the FreeRTOS kernel.
kenjiArai 0:d4960fcea8ff 33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
kenjiArai 0:d4960fcea8ff 34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
kenjiArai 0:d4960fcea8ff 35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
kenjiArai 0:d4960fcea8ff 36 more details. You should have received a copy of the GNU General Public
kenjiArai 0:d4960fcea8ff 37 License and the FreeRTOS license exception along with FreeRTOS; if not it
kenjiArai 0:d4960fcea8ff 38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
kenjiArai 0:d4960fcea8ff 39 by writing to Richard Barry, contact details for whom are available on the
kenjiArai 0:d4960fcea8ff 40 FreeRTOS WEB site.
kenjiArai 0:d4960fcea8ff 41
kenjiArai 0:d4960fcea8ff 42 1 tab == 4 spaces!
kenjiArai 0:d4960fcea8ff 43
kenjiArai 0:d4960fcea8ff 44 http://www.FreeRTOS.org - Documentation, latest information, license and
kenjiArai 0:d4960fcea8ff 45 contact details.
kenjiArai 0:d4960fcea8ff 46
kenjiArai 0:d4960fcea8ff 47 http://www.SafeRTOS.com - A version that is certified for use in safety
kenjiArai 0:d4960fcea8ff 48 critical systems.
kenjiArai 0:d4960fcea8ff 49
kenjiArai 0:d4960fcea8ff 50 http://www.OpenRTOS.com - Commercial support, development, porting,
kenjiArai 0:d4960fcea8ff 51 licensing and training services.
kenjiArai 0:d4960fcea8ff 52 */
kenjiArai 0:d4960fcea8ff 53
kenjiArai 0:d4960fcea8ff 54 /*
kenjiArai 0:d4960fcea8ff 55 * This version of PollQ. c is for use on systems that have limited stack
kenjiArai 0:d4960fcea8ff 56 * space and no display facilities. The complete version can be found in
kenjiArai 0:d4960fcea8ff 57 * the Demo/Common/Full directory.
kenjiArai 0:d4960fcea8ff 58 *
kenjiArai 0:d4960fcea8ff 59 * Creates two tasks that communicate over a single queue. One task acts as a
kenjiArai 0:d4960fcea8ff 60 * producer, the other a consumer.
kenjiArai 0:d4960fcea8ff 61 *
kenjiArai 0:d4960fcea8ff 62 * The producer loops for three iteration, posting an incrementing number onto the
kenjiArai 0:d4960fcea8ff 63 * queue each cycle. It then delays for a fixed period before doing exactly the
kenjiArai 0:d4960fcea8ff 64 * same again.
kenjiArai 0:d4960fcea8ff 65 *
kenjiArai 0:d4960fcea8ff 66 * The consumer loops emptying the queue. Each item removed from the queue is
kenjiArai 0:d4960fcea8ff 67 * checked to ensure it contains the expected value. When the queue is empty it
kenjiArai 0:d4960fcea8ff 68 * blocks for a fixed period, then does the same again.
kenjiArai 0:d4960fcea8ff 69 *
kenjiArai 0:d4960fcea8ff 70 * All queue access is performed without blocking. The consumer completely empties
kenjiArai 0:d4960fcea8ff 71 * the queue each time it runs so the producer should never find the queue full.
kenjiArai 0:d4960fcea8ff 72 *
kenjiArai 0:d4960fcea8ff 73 * An error is flagged if the consumer obtains an unexpected value or the producer
kenjiArai 0:d4960fcea8ff 74 * find the queue is full.
kenjiArai 0:d4960fcea8ff 75 */
kenjiArai 0:d4960fcea8ff 76
kenjiArai 0:d4960fcea8ff 77 /*
kenjiArai 0:d4960fcea8ff 78 Changes from V2.0.0
kenjiArai 0:d4960fcea8ff 79
kenjiArai 0:d4960fcea8ff 80 + Delay periods are now specified using variables and constants of
kenjiArai 0:d4960fcea8ff 81 portTickType rather than unsigned long.
kenjiArai 0:d4960fcea8ff 82 */
kenjiArai 0:d4960fcea8ff 83
kenjiArai 0:d4960fcea8ff 84 #include <stdlib.h>
kenjiArai 0:d4960fcea8ff 85
kenjiArai 0:d4960fcea8ff 86 /* Scheduler include files. */
kenjiArai 0:d4960fcea8ff 87 #include "FreeRTOS.h"
kenjiArai 0:d4960fcea8ff 88 #include "task.h"
kenjiArai 0:d4960fcea8ff 89 #include "queue.h"
kenjiArai 0:d4960fcea8ff 90
kenjiArai 0:d4960fcea8ff 91 /* Demo program include files. */
kenjiArai 0:d4960fcea8ff 92 #include "PollQ.h"
kenjiArai 0:d4960fcea8ff 93
kenjiArai 0:d4960fcea8ff 94 #define pollqSTACK_SIZE configMINIMAL_STACK_SIZE
kenjiArai 0:d4960fcea8ff 95 #define pollqQUEUE_SIZE ( 10 )
kenjiArai 0:d4960fcea8ff 96 #define pollqPRODUCER_DELAY ( ( portTickType ) 200 / portTICK_RATE_MS )
kenjiArai 0:d4960fcea8ff 97 #define pollqCONSUMER_DELAY ( pollqPRODUCER_DELAY - ( portTickType ) ( 20 / portTICK_RATE_MS ) )
kenjiArai 0:d4960fcea8ff 98 #define pollqNO_DELAY ( ( portTickType ) 0 )
kenjiArai 0:d4960fcea8ff 99 #define pollqVALUES_TO_PRODUCE ( ( signed portBASE_TYPE ) 3 )
kenjiArai 0:d4960fcea8ff 100 #define pollqINITIAL_VALUE ( ( signed portBASE_TYPE ) 0 )
kenjiArai 0:d4960fcea8ff 101
kenjiArai 0:d4960fcea8ff 102 /* The task that posts the incrementing number onto the queue. */
kenjiArai 0:d4960fcea8ff 103 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );
kenjiArai 0:d4960fcea8ff 104
kenjiArai 0:d4960fcea8ff 105 /* The task that empties the queue. */
kenjiArai 0:d4960fcea8ff 106 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );
kenjiArai 0:d4960fcea8ff 107
kenjiArai 0:d4960fcea8ff 108 /* Variables that are used to check that the tasks are still running with no
kenjiArai 0:d4960fcea8ff 109 errors. */
kenjiArai 0:d4960fcea8ff 110 static volatile signed portBASE_TYPE xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;
kenjiArai 0:d4960fcea8ff 111
kenjiArai 0:d4960fcea8ff 112 /*-----------------------------------------------------------*/
kenjiArai 0:d4960fcea8ff 113
kenjiArai 0:d4960fcea8ff 114 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
kenjiArai 0:d4960fcea8ff 115 {
kenjiArai 0:d4960fcea8ff 116 static xQueueHandle xPolledQueue;
kenjiArai 0:d4960fcea8ff 117
kenjiArai 0:d4960fcea8ff 118 /* Create the queue used by the producer and consumer. */
kenjiArai 0:d4960fcea8ff 119 xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
kenjiArai 0:d4960fcea8ff 120
kenjiArai 0:d4960fcea8ff 121 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
kenjiArai 0:d4960fcea8ff 122 in use. The queue registry is provided as a means for kernel aware
kenjiArai 0:d4960fcea8ff 123 debuggers to locate queues and has no purpose if a kernel aware debugger
kenjiArai 0:d4960fcea8ff 124 is not being used. The call to vQueueAddToRegistry() will be removed
kenjiArai 0:d4960fcea8ff 125 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
kenjiArai 0:d4960fcea8ff 126 defined to be less than 1. */
kenjiArai 0:d4960fcea8ff 127 vQueueAddToRegistry( xPolledQueue, ( signed char * ) "Poll_Test_Queue" );
kenjiArai 0:d4960fcea8ff 128
kenjiArai 0:d4960fcea8ff 129 /* Spawn the producer and consumer. */
kenjiArai 0:d4960fcea8ff 130 xTaskCreate( vPolledQueueConsumer, ( signed char * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
kenjiArai 0:d4960fcea8ff 131 xTaskCreate( vPolledQueueProducer, ( signed char * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
kenjiArai 0:d4960fcea8ff 132 }
kenjiArai 0:d4960fcea8ff 133 /*-----------------------------------------------------------*/
kenjiArai 0:d4960fcea8ff 134
kenjiArai 0:d4960fcea8ff 135 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )
kenjiArai 0:d4960fcea8ff 136 {
kenjiArai 0:d4960fcea8ff 137 unsigned short usValue = ( unsigned short ) 0;
kenjiArai 0:d4960fcea8ff 138 signed portBASE_TYPE xError = pdFALSE, xLoop;
kenjiArai 0:d4960fcea8ff 139
kenjiArai 0:d4960fcea8ff 140 for( ;; )
kenjiArai 0:d4960fcea8ff 141 {
kenjiArai 0:d4960fcea8ff 142 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )
kenjiArai 0:d4960fcea8ff 143 {
kenjiArai 0:d4960fcea8ff 144 /* Send an incrementing number on the queue without blocking. */
kenjiArai 0:d4960fcea8ff 145 if( xQueueSend( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )
kenjiArai 0:d4960fcea8ff 146 {
kenjiArai 0:d4960fcea8ff 147 /* We should never find the queue full so if we get here there
kenjiArai 0:d4960fcea8ff 148 has been an error. */
kenjiArai 0:d4960fcea8ff 149 xError = pdTRUE;
kenjiArai 0:d4960fcea8ff 150 }
kenjiArai 0:d4960fcea8ff 151 else
kenjiArai 0:d4960fcea8ff 152 {
kenjiArai 0:d4960fcea8ff 153 if( xError == pdFALSE )
kenjiArai 0:d4960fcea8ff 154 {
kenjiArai 0:d4960fcea8ff 155 /* If an error has ever been recorded we stop incrementing the
kenjiArai 0:d4960fcea8ff 156 check variable. */
kenjiArai 0:d4960fcea8ff 157 portENTER_CRITICAL();
kenjiArai 0:d4960fcea8ff 158 xPollingProducerCount++;
kenjiArai 0:d4960fcea8ff 159 portEXIT_CRITICAL();
kenjiArai 0:d4960fcea8ff 160 }
kenjiArai 0:d4960fcea8ff 161
kenjiArai 0:d4960fcea8ff 162 /* Update the value we are going to post next time around. */
kenjiArai 0:d4960fcea8ff 163 usValue++;
kenjiArai 0:d4960fcea8ff 164 }
kenjiArai 0:d4960fcea8ff 165 }
kenjiArai 0:d4960fcea8ff 166
kenjiArai 0:d4960fcea8ff 167 /* Wait before we start posting again to ensure the consumer runs and
kenjiArai 0:d4960fcea8ff 168 empties the queue. */
kenjiArai 0:d4960fcea8ff 169 vTaskDelay( pollqPRODUCER_DELAY );
kenjiArai 0:d4960fcea8ff 170 }
kenjiArai 0:d4960fcea8ff 171 } /*lint !e818 Function prototype must conform to API. */
kenjiArai 0:d4960fcea8ff 172 /*-----------------------------------------------------------*/
kenjiArai 0:d4960fcea8ff 173
kenjiArai 0:d4960fcea8ff 174 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )
kenjiArai 0:d4960fcea8ff 175 {
kenjiArai 0:d4960fcea8ff 176 unsigned short usData, usExpectedValue = ( unsigned short ) 0;
kenjiArai 0:d4960fcea8ff 177 signed portBASE_TYPE xError = pdFALSE;
kenjiArai 0:d4960fcea8ff 178
kenjiArai 0:d4960fcea8ff 179 for( ;; )
kenjiArai 0:d4960fcea8ff 180 {
kenjiArai 0:d4960fcea8ff 181 /* Loop until the queue is empty. */
kenjiArai 0:d4960fcea8ff 182 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )
kenjiArai 0:d4960fcea8ff 183 {
kenjiArai 0:d4960fcea8ff 184 if( xQueueReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )
kenjiArai 0:d4960fcea8ff 185 {
kenjiArai 0:d4960fcea8ff 186 if( usData != usExpectedValue )
kenjiArai 0:d4960fcea8ff 187 {
kenjiArai 0:d4960fcea8ff 188 /* This is not what we expected to receive so an error has
kenjiArai 0:d4960fcea8ff 189 occurred. */
kenjiArai 0:d4960fcea8ff 190 xError = pdTRUE;
kenjiArai 0:d4960fcea8ff 191
kenjiArai 0:d4960fcea8ff 192 /* Catch-up to the value we received so our next expected
kenjiArai 0:d4960fcea8ff 193 value should again be correct. */
kenjiArai 0:d4960fcea8ff 194 usExpectedValue = usData;
kenjiArai 0:d4960fcea8ff 195 }
kenjiArai 0:d4960fcea8ff 196 else
kenjiArai 0:d4960fcea8ff 197 {
kenjiArai 0:d4960fcea8ff 198 if( xError == pdFALSE )
kenjiArai 0:d4960fcea8ff 199 {
kenjiArai 0:d4960fcea8ff 200 /* Only increment the check variable if no errors have
kenjiArai 0:d4960fcea8ff 201 occurred. */
kenjiArai 0:d4960fcea8ff 202 portENTER_CRITICAL();
kenjiArai 0:d4960fcea8ff 203 xPollingConsumerCount++;
kenjiArai 0:d4960fcea8ff 204 portEXIT_CRITICAL();
kenjiArai 0:d4960fcea8ff 205 }
kenjiArai 0:d4960fcea8ff 206 }
kenjiArai 0:d4960fcea8ff 207
kenjiArai 0:d4960fcea8ff 208 /* Next time round we would expect the number to be one higher. */
kenjiArai 0:d4960fcea8ff 209 usExpectedValue++;
kenjiArai 0:d4960fcea8ff 210 }
kenjiArai 0:d4960fcea8ff 211 }
kenjiArai 0:d4960fcea8ff 212
kenjiArai 0:d4960fcea8ff 213 /* Now the queue is empty we block, allowing the producer to place more
kenjiArai 0:d4960fcea8ff 214 items in the queue. */
kenjiArai 0:d4960fcea8ff 215 vTaskDelay( pollqCONSUMER_DELAY );
kenjiArai 0:d4960fcea8ff 216 }
kenjiArai 0:d4960fcea8ff 217 } /*lint !e818 Function prototype must conform to API. */
kenjiArai 0:d4960fcea8ff 218 /*-----------------------------------------------------------*/
kenjiArai 0:d4960fcea8ff 219
kenjiArai 0:d4960fcea8ff 220 /* This is called to check that all the created tasks are still running with no errors. */
kenjiArai 0:d4960fcea8ff 221 portBASE_TYPE xArePollingQueuesStillRunning( void )
kenjiArai 0:d4960fcea8ff 222 {
kenjiArai 0:d4960fcea8ff 223 portBASE_TYPE xReturn;
kenjiArai 0:d4960fcea8ff 224
kenjiArai 0:d4960fcea8ff 225 /* Check both the consumer and producer poll count to check they have both
kenjiArai 0:d4960fcea8ff 226 been changed since out last trip round. We do not need a critical section
kenjiArai 0:d4960fcea8ff 227 around the check variables as this is called from a higher priority than
kenjiArai 0:d4960fcea8ff 228 the other tasks that access the same variables. */
kenjiArai 0:d4960fcea8ff 229 if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||
kenjiArai 0:d4960fcea8ff 230 ( xPollingProducerCount == pollqINITIAL_VALUE )
kenjiArai 0:d4960fcea8ff 231 )
kenjiArai 0:d4960fcea8ff 232 {
kenjiArai 0:d4960fcea8ff 233 xReturn = pdFALSE;
kenjiArai 0:d4960fcea8ff 234 }
kenjiArai 0:d4960fcea8ff 235 else
kenjiArai 0:d4960fcea8ff 236 {
kenjiArai 0:d4960fcea8ff 237 xReturn = pdTRUE;
kenjiArai 0:d4960fcea8ff 238 }
kenjiArai 0:d4960fcea8ff 239
kenjiArai 0:d4960fcea8ff 240 /* Set the check variables back down so we know if they have been
kenjiArai 0:d4960fcea8ff 241 incremented the next time around. */
kenjiArai 0:d4960fcea8ff 242 xPollingConsumerCount = pollqINITIAL_VALUE;
kenjiArai 0:d4960fcea8ff 243 xPollingProducerCount = pollqINITIAL_VALUE;
kenjiArai 0:d4960fcea8ff 244
kenjiArai 0:d4960fcea8ff 245 return xReturn;
kenjiArai 0:d4960fcea8ff 246 }