2 FreeRTOS.org V4.4.0 - Copyright (C) 2003-2007 Richard Barry.
4 This file is part of the FreeRTOS.org distribution.
6 FreeRTOS.org is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 FreeRTOS.org is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with FreeRTOS.org; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 A special exception to the GPL can be applied should you wish to distribute
21 a combined work that includes FreeRTOS.org, without being obliged to provide
22 the source code for any proprietary components. See the licensing section
23 of http://www.FreeRTOS.org for full details of how and when the exception
26 ***************************************************************************
27 See http://www.FreeRTOS.org for documentation, latest information, license
28 and contact details. Please ensure to read the configuration and relevant
29 port sections of the online documentation.
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
32 with commercial development and support options.
33 ***************************************************************************
41 typedef xQueueHandle xSemaphoreHandle;
43 #define semBINARY_SEMAPHORE_QUEUE_LENGTH ( ( unsigned portCHAR ) 1 )
44 #define semSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned portCHAR ) 0 )
45 #define semGIVE_BLOCK_TIME ( ( portTickType ) 0 )
50 * <pre>vSemaphoreCreateBinary( xSemaphoreHandle xSemaphore )</pre>
52 * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.
53 * The queue length is 1 as this is a binary semaphore. The data size is 0
54 * as we don't want to actually store any data - we just want to know if the
55 * queue is empty or full.
57 * @param xSemaphore Handle to the created semaphore. Should be of type xSemaphoreHandle.
61 xSemaphoreHandle xSemaphore;
63 void vATask( void * pvParameters )
65 // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().
66 // This is a macro so pass the variable in directly.
67 vSemaphoreCreateBinary( xSemaphore );
69 if( xSemaphore != NULL )
71 // The semaphore was created successfully.
72 // The semaphore can now be used.
76 * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary
79 #define vSemaphoreCreateBinary( xSemaphore ) { \
80 xSemaphore = xQueueCreate( ( unsigned portCHAR ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH ); \
81 if( xSemaphore != NULL ) \
83 xSemaphoreGive( xSemaphore ); \
90 * xSemaphoreHandle xSemaphore,
91 * portTickType xBlockTime
94 * <i>Macro</i> to obtain a semaphore. The semaphore must of been created using
95 * vSemaphoreCreateBinary ().
97 * @param xSemaphore A handle to the semaphore being obtained. This is the
98 * handle returned by vSemaphoreCreateBinary ();
100 * @param xBlockTime The time in ticks to wait for the semaphore to become
101 * available. The macro portTICK_RATE_MS can be used to convert this to a
102 * real time. A block time of zero can be used to poll the semaphore.
104 * @return pdTRUE if the semaphore was obtained. pdFALSE if xBlockTime
105 * expired without the semaphore becoming available.
109 xSemaphoreHandle xSemaphore = NULL;
111 // A task that creates a semaphore.
112 void vATask( void * pvParameters )
114 // Create the semaphore to guard a shared resource.
115 vSemaphoreCreateBinary( xSemaphore );
118 // A task that uses the semaphore.
119 void vAnotherTask( void * pvParameters )
121 // ... Do other things.
123 if( xSemaphore != NULL )
125 // See if we can obtain the semaphore. If the semaphore is not available
126 // wait 10 ticks to see if it becomes free.
127 if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE )
129 // We were able to obtain the semaphore and can now access the
134 // We have finished accessing the shared resource. Release the
136 xSemaphoreGive( xSemaphore );
140 // We could not obtain the semaphore and can therefore not access
141 // the shared resource safely.
146 * \defgroup xSemaphoreTake xSemaphoreTake
147 * \ingroup Semaphores
149 #define xSemaphoreTake( xSemaphore, xBlockTime ) xQueueReceive( ( xQueueHandle ) xSemaphore, NULL, xBlockTime )
153 * <pre>xSemaphoreGive( xSemaphoreHandle xSemaphore )</pre>
155 * <i>Macro</i> to release a semaphore. The semaphore must of been created using
156 * vSemaphoreCreateBinary (), and obtained using sSemaphoreTake ().
158 * This must not be used from an ISR. See xSemaphoreGiveFromISR () for
159 * an alternative which can be used from an ISR.
161 * @param xSemaphore A handle to the semaphore being released. This is the
162 * handle returned by vSemaphoreCreateBinary ();
164 * @return pdTRUE if the semaphore was released. pdFALSE if an error occurred.
165 * Semaphores are implemented using queues. An error can occur if there is
166 * no space on the queue to post a message - indicating that the
167 * semaphore was not first obtained correctly.
171 xSemaphoreHandle xSemaphore = NULL;
173 void vATask( void * pvParameters )
175 // Create the semaphore to guard a shared resource.
176 vSemaphoreCreateBinary( xSemaphore );
178 if( xSemaphore != NULL )
180 if( xSemaphoreGive( xSemaphore ) != pdTRUE )
182 // We would expect this call to fail because we cannot give
183 // a semaphore without first "taking" it!
186 // Obtain the semaphore - don't block if the semaphore is not
187 // immediately available.
188 if( xSemaphoreTake( xSemaphore, ( portTickType ) 0 ) )
190 // We now have the semaphore and can access the shared resource.
194 // We have finished accessing the shared resource so can free the
196 if( xSemaphoreGive( xSemaphore ) != pdTRUE )
198 // We would not expect this call to fail because we must have
199 // obtained the semaphore to get here.
205 * \defgroup xSemaphoreGive xSemaphoreGive
206 * \ingroup Semaphores
208 #define xSemaphoreGive( xSemaphore ) xQueueSend( ( xQueueHandle ) xSemaphore, NULL, semGIVE_BLOCK_TIME )
213 xSemaphoreGiveFromISR(
214 xSemaphoreHandle xSemaphore,
215 portSHORT sTaskPreviouslyWoken
218 * <i>Macro</i> to release a semaphore. The semaphore must of been created using
219 * vSemaphoreCreateBinary (), and obtained using xSemaphoreTake ().
221 * This macro can be used from an ISR.
223 * @param xSemaphore A handle to the semaphore being released. This is the
224 * handle returned by vSemaphoreCreateBinary ();
226 * @param sTaskPreviouslyWoken This is included so an ISR can make multiple calls
227 * to xSemaphoreGiveFromISR () from a single interrupt. The first call
228 * should always pass in pdFALSE. Subsequent calls should pass in
229 * the value returned from the previous call. See the file serial .c in the
230 * PC port for a good example of using xSemaphoreGiveFromISR ().
232 * @return pdTRUE if a task was woken by releasing the semaphore. This is
233 * used by the ISR to determine if a context switch may be required following
238 #define LONG_TIME 0xffff
239 #define TICKS_TO_WAIT 10
240 xSemaphoreHandle xSemaphore = NULL;
243 void vATask( void * pvParameters )
247 // We want this task to run every 10 ticks or a timer. The semaphore
248 // was created before this task was started
250 // Block waiting for the semaphore to become available.
251 if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )
253 // It is time to execute.
257 // We have finished our task. Return to the top of the loop where
258 // we will block on the semaphore until it is time to execute
265 void vTimerISR( void * pvParameters )
267 static unsigned portCHAR ucLocalTickCount = 0;
269 // A timer tick has occurred.
271 // ... Do other time functions.
273 // Is it time for vATask () to run?
275 if( ucLocalTickCount >= TICKS_TO_WAIT )
277 // Unblock the task by releasing the semaphore.
278 xSemaphoreGive( xSemaphore );
280 // Reset the count so we release the semaphore again in 10 ticks time.
281 ucLocalTickCount = 0;
285 * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR
286 * \ingroup Semaphores
288 #define xSemaphoreGiveFromISR( xSemaphore, xTaskPreviouslyWoken ) xQueueSendFromISR( ( xQueueHandle ) xSemaphore, NULL, xTaskPreviouslyWoken )