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