Francisco Paez / freertos-cm3

Dependents:   mbed_lpc1768_freertos_lib

Committer:
fep
Date:
Wed May 31 02:36:43 2017 +0000
Revision:
0:5ff20db10a96
FreeRTOS v9.0.0 for ARM Cortex-M3 based boards.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fep 0:5ff20db10a96 1 /*
fep 0:5ff20db10a96 2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
fep 0:5ff20db10a96 3 All rights reserved
fep 0:5ff20db10a96 4
fep 0:5ff20db10a96 5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
fep 0:5ff20db10a96 6
fep 0:5ff20db10a96 7 This file is part of the FreeRTOS distribution.
fep 0:5ff20db10a96 8
fep 0:5ff20db10a96 9 FreeRTOS is free software; you can redistribute it and/or modify it under
fep 0:5ff20db10a96 10 the terms of the GNU General Public License (version 2) as published by the
fep 0:5ff20db10a96 11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
fep 0:5ff20db10a96 12
fep 0:5ff20db10a96 13 ***************************************************************************
fep 0:5ff20db10a96 14 >>! NOTE: The modification to the GPL is included to allow you to !<<
fep 0:5ff20db10a96 15 >>! distribute a combined work that includes FreeRTOS without being !<<
fep 0:5ff20db10a96 16 >>! obliged to provide the source code for proprietary components !<<
fep 0:5ff20db10a96 17 >>! outside of the FreeRTOS kernel. !<<
fep 0:5ff20db10a96 18 ***************************************************************************
fep 0:5ff20db10a96 19
fep 0:5ff20db10a96 20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
fep 0:5ff20db10a96 21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
fep 0:5ff20db10a96 22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
fep 0:5ff20db10a96 23 link: http://www.freertos.org/a00114.html
fep 0:5ff20db10a96 24
fep 0:5ff20db10a96 25 ***************************************************************************
fep 0:5ff20db10a96 26 * *
fep 0:5ff20db10a96 27 * FreeRTOS provides completely free yet professionally developed, *
fep 0:5ff20db10a96 28 * robust, strictly quality controlled, supported, and cross *
fep 0:5ff20db10a96 29 * platform software that is more than just the market leader, it *
fep 0:5ff20db10a96 30 * is the industry's de facto standard. *
fep 0:5ff20db10a96 31 * *
fep 0:5ff20db10a96 32 * Help yourself get started quickly while simultaneously helping *
fep 0:5ff20db10a96 33 * to support the FreeRTOS project by purchasing a FreeRTOS *
fep 0:5ff20db10a96 34 * tutorial book, reference manual, or both: *
fep 0:5ff20db10a96 35 * http://www.FreeRTOS.org/Documentation *
fep 0:5ff20db10a96 36 * *
fep 0:5ff20db10a96 37 ***************************************************************************
fep 0:5ff20db10a96 38
fep 0:5ff20db10a96 39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
fep 0:5ff20db10a96 40 the FAQ page "My application does not run, what could be wrong?". Have you
fep 0:5ff20db10a96 41 defined configASSERT()?
fep 0:5ff20db10a96 42
fep 0:5ff20db10a96 43 http://www.FreeRTOS.org/support - In return for receiving this top quality
fep 0:5ff20db10a96 44 embedded software for free we request you assist our global community by
fep 0:5ff20db10a96 45 participating in the support forum.
fep 0:5ff20db10a96 46
fep 0:5ff20db10a96 47 http://www.FreeRTOS.org/training - Investing in training allows your team to
fep 0:5ff20db10a96 48 be as productive as possible as early as possible. Now you can receive
fep 0:5ff20db10a96 49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
fep 0:5ff20db10a96 50 Ltd, and the world's leading authority on the world's leading RTOS.
fep 0:5ff20db10a96 51
fep 0:5ff20db10a96 52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
fep 0:5ff20db10a96 53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
fep 0:5ff20db10a96 54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
fep 0:5ff20db10a96 55
fep 0:5ff20db10a96 56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
fep 0:5ff20db10a96 57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
fep 0:5ff20db10a96 58
fep 0:5ff20db10a96 59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
fep 0:5ff20db10a96 60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
fep 0:5ff20db10a96 61 licenses offer ticketed support, indemnification and commercial middleware.
fep 0:5ff20db10a96 62
fep 0:5ff20db10a96 63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
fep 0:5ff20db10a96 64 engineered and independently SIL3 certified version for use in safety and
fep 0:5ff20db10a96 65 mission critical applications that require provable dependability.
fep 0:5ff20db10a96 66
fep 0:5ff20db10a96 67 1 tab == 4 spaces!
fep 0:5ff20db10a96 68 */
fep 0:5ff20db10a96 69
fep 0:5ff20db10a96 70 #ifndef SEMAPHORE_H
fep 0:5ff20db10a96 71 #define SEMAPHORE_H
fep 0:5ff20db10a96 72
fep 0:5ff20db10a96 73 #ifndef INC_FREERTOS_H
fep 0:5ff20db10a96 74 #error "include FreeRTOS.h" must appear in source files before "include semphr.h"
fep 0:5ff20db10a96 75 #endif
fep 0:5ff20db10a96 76
fep 0:5ff20db10a96 77 #include "queue.h"
fep 0:5ff20db10a96 78
fep 0:5ff20db10a96 79 typedef QueueHandle_t SemaphoreHandle_t;
fep 0:5ff20db10a96 80
fep 0:5ff20db10a96 81 #define semBINARY_SEMAPHORE_QUEUE_LENGTH ( ( uint8_t ) 1U )
fep 0:5ff20db10a96 82 #define semSEMAPHORE_QUEUE_ITEM_LENGTH ( ( uint8_t ) 0U )
fep 0:5ff20db10a96 83 #define semGIVE_BLOCK_TIME ( ( TickType_t ) 0U )
fep 0:5ff20db10a96 84
fep 0:5ff20db10a96 85
fep 0:5ff20db10a96 86 /**
fep 0:5ff20db10a96 87 * semphr. h
fep 0:5ff20db10a96 88 * <pre>vSemaphoreCreateBinary( SemaphoreHandle_t xSemaphore )</pre>
fep 0:5ff20db10a96 89 *
fep 0:5ff20db10a96 90 * In many usage scenarios it is faster and more memory efficient to use a
fep 0:5ff20db10a96 91 * direct to task notification in place of a binary semaphore!
fep 0:5ff20db10a96 92 * http://www.freertos.org/RTOS-task-notifications.html
fep 0:5ff20db10a96 93 *
fep 0:5ff20db10a96 94 * This old vSemaphoreCreateBinary() macro is now deprecated in favour of the
fep 0:5ff20db10a96 95 * xSemaphoreCreateBinary() function. Note that binary semaphores created using
fep 0:5ff20db10a96 96 * the vSemaphoreCreateBinary() macro are created in a state such that the
fep 0:5ff20db10a96 97 * first call to 'take' the semaphore would pass, whereas binary semaphores
fep 0:5ff20db10a96 98 * created using xSemaphoreCreateBinary() are created in a state such that the
fep 0:5ff20db10a96 99 * the semaphore must first be 'given' before it can be 'taken'.
fep 0:5ff20db10a96 100 *
fep 0:5ff20db10a96 101 * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.
fep 0:5ff20db10a96 102 * The queue length is 1 as this is a binary semaphore. The data size is 0
fep 0:5ff20db10a96 103 * as we don't want to actually store any data - we just want to know if the
fep 0:5ff20db10a96 104 * queue is empty or full.
fep 0:5ff20db10a96 105 *
fep 0:5ff20db10a96 106 * This type of semaphore can be used for pure synchronisation between tasks or
fep 0:5ff20db10a96 107 * between an interrupt and a task. The semaphore need not be given back once
fep 0:5ff20db10a96 108 * obtained, so one task/interrupt can continuously 'give' the semaphore while
fep 0:5ff20db10a96 109 * another continuously 'takes' the semaphore. For this reason this type of
fep 0:5ff20db10a96 110 * semaphore does not use a priority inheritance mechanism. For an alternative
fep 0:5ff20db10a96 111 * that does use priority inheritance see xSemaphoreCreateMutex().
fep 0:5ff20db10a96 112 *
fep 0:5ff20db10a96 113 * @param xSemaphore Handle to the created semaphore. Should be of type SemaphoreHandle_t.
fep 0:5ff20db10a96 114 *
fep 0:5ff20db10a96 115 * Example usage:
fep 0:5ff20db10a96 116 <pre>
fep 0:5ff20db10a96 117 SemaphoreHandle_t xSemaphore = NULL;
fep 0:5ff20db10a96 118
fep 0:5ff20db10a96 119 void vATask( void * pvParameters )
fep 0:5ff20db10a96 120 {
fep 0:5ff20db10a96 121 // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().
fep 0:5ff20db10a96 122 // This is a macro so pass the variable in directly.
fep 0:5ff20db10a96 123 vSemaphoreCreateBinary( xSemaphore );
fep 0:5ff20db10a96 124
fep 0:5ff20db10a96 125 if( xSemaphore != NULL )
fep 0:5ff20db10a96 126 {
fep 0:5ff20db10a96 127 // The semaphore was created successfully.
fep 0:5ff20db10a96 128 // The semaphore can now be used.
fep 0:5ff20db10a96 129 }
fep 0:5ff20db10a96 130 }
fep 0:5ff20db10a96 131 </pre>
fep 0:5ff20db10a96 132 * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary
fep 0:5ff20db10a96 133 * \ingroup Semaphores
fep 0:5ff20db10a96 134 */
fep 0:5ff20db10a96 135 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
fep 0:5ff20db10a96 136 #define vSemaphoreCreateBinary( xSemaphore ) \
fep 0:5ff20db10a96 137 { \
fep 0:5ff20db10a96 138 ( xSemaphore ) = xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE ); \
fep 0:5ff20db10a96 139 if( ( xSemaphore ) != NULL ) \
fep 0:5ff20db10a96 140 { \
fep 0:5ff20db10a96 141 ( void ) xSemaphoreGive( ( xSemaphore ) ); \
fep 0:5ff20db10a96 142 } \
fep 0:5ff20db10a96 143 }
fep 0:5ff20db10a96 144 #endif
fep 0:5ff20db10a96 145
fep 0:5ff20db10a96 146 /**
fep 0:5ff20db10a96 147 * semphr. h
fep 0:5ff20db10a96 148 * <pre>SemaphoreHandle_t xSemaphoreCreateBinary( void )</pre>
fep 0:5ff20db10a96 149 *
fep 0:5ff20db10a96 150 * Creates a new binary semaphore instance, and returns a handle by which the
fep 0:5ff20db10a96 151 * new semaphore can be referenced.
fep 0:5ff20db10a96 152 *
fep 0:5ff20db10a96 153 * In many usage scenarios it is faster and more memory efficient to use a
fep 0:5ff20db10a96 154 * direct to task notification in place of a binary semaphore!
fep 0:5ff20db10a96 155 * http://www.freertos.org/RTOS-task-notifications.html
fep 0:5ff20db10a96 156 *
fep 0:5ff20db10a96 157 * Internally, within the FreeRTOS implementation, binary semaphores use a block
fep 0:5ff20db10a96 158 * of memory, in which the semaphore structure is stored. If a binary semaphore
fep 0:5ff20db10a96 159 * is created using xSemaphoreCreateBinary() then the required memory is
fep 0:5ff20db10a96 160 * automatically dynamically allocated inside the xSemaphoreCreateBinary()
fep 0:5ff20db10a96 161 * function. (see http://www.freertos.org/a00111.html). If a binary semaphore
fep 0:5ff20db10a96 162 * is created using xSemaphoreCreateBinaryStatic() then the application writer
fep 0:5ff20db10a96 163 * must provide the memory. xSemaphoreCreateBinaryStatic() therefore allows a
fep 0:5ff20db10a96 164 * binary semaphore to be created without using any dynamic memory allocation.
fep 0:5ff20db10a96 165 *
fep 0:5ff20db10a96 166 * The old vSemaphoreCreateBinary() macro is now deprecated in favour of this
fep 0:5ff20db10a96 167 * xSemaphoreCreateBinary() function. Note that binary semaphores created using
fep 0:5ff20db10a96 168 * the vSemaphoreCreateBinary() macro are created in a state such that the
fep 0:5ff20db10a96 169 * first call to 'take' the semaphore would pass, whereas binary semaphores
fep 0:5ff20db10a96 170 * created using xSemaphoreCreateBinary() are created in a state such that the
fep 0:5ff20db10a96 171 * the semaphore must first be 'given' before it can be 'taken'.
fep 0:5ff20db10a96 172 *
fep 0:5ff20db10a96 173 * This type of semaphore can be used for pure synchronisation between tasks or
fep 0:5ff20db10a96 174 * between an interrupt and a task. The semaphore need not be given back once
fep 0:5ff20db10a96 175 * obtained, so one task/interrupt can continuously 'give' the semaphore while
fep 0:5ff20db10a96 176 * another continuously 'takes' the semaphore. For this reason this type of
fep 0:5ff20db10a96 177 * semaphore does not use a priority inheritance mechanism. For an alternative
fep 0:5ff20db10a96 178 * that does use priority inheritance see xSemaphoreCreateMutex().
fep 0:5ff20db10a96 179 *
fep 0:5ff20db10a96 180 * @return Handle to the created semaphore, or NULL if the memory required to
fep 0:5ff20db10a96 181 * hold the semaphore's data structures could not be allocated.
fep 0:5ff20db10a96 182 *
fep 0:5ff20db10a96 183 * Example usage:
fep 0:5ff20db10a96 184 <pre>
fep 0:5ff20db10a96 185 SemaphoreHandle_t xSemaphore = NULL;
fep 0:5ff20db10a96 186
fep 0:5ff20db10a96 187 void vATask( void * pvParameters )
fep 0:5ff20db10a96 188 {
fep 0:5ff20db10a96 189 // Semaphore cannot be used before a call to xSemaphoreCreateBinary().
fep 0:5ff20db10a96 190 // This is a macro so pass the variable in directly.
fep 0:5ff20db10a96 191 xSemaphore = xSemaphoreCreateBinary();
fep 0:5ff20db10a96 192
fep 0:5ff20db10a96 193 if( xSemaphore != NULL )
fep 0:5ff20db10a96 194 {
fep 0:5ff20db10a96 195 // The semaphore was created successfully.
fep 0:5ff20db10a96 196 // The semaphore can now be used.
fep 0:5ff20db10a96 197 }
fep 0:5ff20db10a96 198 }
fep 0:5ff20db10a96 199 </pre>
fep 0:5ff20db10a96 200 * \defgroup xSemaphoreCreateBinary xSemaphoreCreateBinary
fep 0:5ff20db10a96 201 * \ingroup Semaphores
fep 0:5ff20db10a96 202 */
fep 0:5ff20db10a96 203 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
fep 0:5ff20db10a96 204 #define xSemaphoreCreateBinary() xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE )
fep 0:5ff20db10a96 205 #endif
fep 0:5ff20db10a96 206
fep 0:5ff20db10a96 207 /**
fep 0:5ff20db10a96 208 * semphr. h
fep 0:5ff20db10a96 209 * <pre>SemaphoreHandle_t xSemaphoreCreateBinaryStatic( StaticSemaphore_t *pxSemaphoreBuffer )</pre>
fep 0:5ff20db10a96 210 *
fep 0:5ff20db10a96 211 * Creates a new binary semaphore instance, and returns a handle by which the
fep 0:5ff20db10a96 212 * new semaphore can be referenced.
fep 0:5ff20db10a96 213 *
fep 0:5ff20db10a96 214 * NOTE: In many usage scenarios it is faster and more memory efficient to use a
fep 0:5ff20db10a96 215 * direct to task notification in place of a binary semaphore!
fep 0:5ff20db10a96 216 * http://www.freertos.org/RTOS-task-notifications.html
fep 0:5ff20db10a96 217 *
fep 0:5ff20db10a96 218 * Internally, within the FreeRTOS implementation, binary semaphores use a block
fep 0:5ff20db10a96 219 * of memory, in which the semaphore structure is stored. If a binary semaphore
fep 0:5ff20db10a96 220 * is created using xSemaphoreCreateBinary() then the required memory is
fep 0:5ff20db10a96 221 * automatically dynamically allocated inside the xSemaphoreCreateBinary()
fep 0:5ff20db10a96 222 * function. (see http://www.freertos.org/a00111.html). If a binary semaphore
fep 0:5ff20db10a96 223 * is created using xSemaphoreCreateBinaryStatic() then the application writer
fep 0:5ff20db10a96 224 * must provide the memory. xSemaphoreCreateBinaryStatic() therefore allows a
fep 0:5ff20db10a96 225 * binary semaphore to be created without using any dynamic memory allocation.
fep 0:5ff20db10a96 226 *
fep 0:5ff20db10a96 227 * This type of semaphore can be used for pure synchronisation between tasks or
fep 0:5ff20db10a96 228 * between an interrupt and a task. The semaphore need not be given back once
fep 0:5ff20db10a96 229 * obtained, so one task/interrupt can continuously 'give' the semaphore while
fep 0:5ff20db10a96 230 * another continuously 'takes' the semaphore. For this reason this type of
fep 0:5ff20db10a96 231 * semaphore does not use a priority inheritance mechanism. For an alternative
fep 0:5ff20db10a96 232 * that does use priority inheritance see xSemaphoreCreateMutex().
fep 0:5ff20db10a96 233 *
fep 0:5ff20db10a96 234 * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,
fep 0:5ff20db10a96 235 * which will then be used to hold the semaphore's data structure, removing the
fep 0:5ff20db10a96 236 * need for the memory to be allocated dynamically.
fep 0:5ff20db10a96 237 *
fep 0:5ff20db10a96 238 * @return If the semaphore is created then a handle to the created semaphore is
fep 0:5ff20db10a96 239 * returned. If pxSemaphoreBuffer is NULL then NULL is returned.
fep 0:5ff20db10a96 240 *
fep 0:5ff20db10a96 241 * Example usage:
fep 0:5ff20db10a96 242 <pre>
fep 0:5ff20db10a96 243 SemaphoreHandle_t xSemaphore = NULL;
fep 0:5ff20db10a96 244 StaticSemaphore_t xSemaphoreBuffer;
fep 0:5ff20db10a96 245
fep 0:5ff20db10a96 246 void vATask( void * pvParameters )
fep 0:5ff20db10a96 247 {
fep 0:5ff20db10a96 248 // Semaphore cannot be used before a call to xSemaphoreCreateBinary().
fep 0:5ff20db10a96 249 // The semaphore's data structures will be placed in the xSemaphoreBuffer
fep 0:5ff20db10a96 250 // variable, the address of which is passed into the function. The
fep 0:5ff20db10a96 251 // function's parameter is not NULL, so the function will not attempt any
fep 0:5ff20db10a96 252 // dynamic memory allocation, and therefore the function will not return
fep 0:5ff20db10a96 253 // return NULL.
fep 0:5ff20db10a96 254 xSemaphore = xSemaphoreCreateBinary( &xSemaphoreBuffer );
fep 0:5ff20db10a96 255
fep 0:5ff20db10a96 256 // Rest of task code goes here.
fep 0:5ff20db10a96 257 }
fep 0:5ff20db10a96 258 </pre>
fep 0:5ff20db10a96 259 * \defgroup xSemaphoreCreateBinaryStatic xSemaphoreCreateBinaryStatic
fep 0:5ff20db10a96 260 * \ingroup Semaphores
fep 0:5ff20db10a96 261 */
fep 0:5ff20db10a96 262 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
fep 0:5ff20db10a96 263 #define xSemaphoreCreateBinaryStatic( pxStaticSemaphore ) xQueueGenericCreateStatic( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticSemaphore, queueQUEUE_TYPE_BINARY_SEMAPHORE )
fep 0:5ff20db10a96 264 #endif /* configSUPPORT_STATIC_ALLOCATION */
fep 0:5ff20db10a96 265
fep 0:5ff20db10a96 266 /**
fep 0:5ff20db10a96 267 * semphr. h
fep 0:5ff20db10a96 268 * <pre>xSemaphoreTake(
fep 0:5ff20db10a96 269 * SemaphoreHandle_t xSemaphore,
fep 0:5ff20db10a96 270 * TickType_t xBlockTime
fep 0:5ff20db10a96 271 * )</pre>
fep 0:5ff20db10a96 272 *
fep 0:5ff20db10a96 273 * <i>Macro</i> to obtain a semaphore. The semaphore must have previously been
fep 0:5ff20db10a96 274 * created with a call to xSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
fep 0:5ff20db10a96 275 * xSemaphoreCreateCounting().
fep 0:5ff20db10a96 276 *
fep 0:5ff20db10a96 277 * @param xSemaphore A handle to the semaphore being taken - obtained when
fep 0:5ff20db10a96 278 * the semaphore was created.
fep 0:5ff20db10a96 279 *
fep 0:5ff20db10a96 280 * @param xBlockTime The time in ticks to wait for the semaphore to become
fep 0:5ff20db10a96 281 * available. The macro portTICK_PERIOD_MS can be used to convert this to a
fep 0:5ff20db10a96 282 * real time. A block time of zero can be used to poll the semaphore. A block
fep 0:5ff20db10a96 283 * time of portMAX_DELAY can be used to block indefinitely (provided
fep 0:5ff20db10a96 284 * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h).
fep 0:5ff20db10a96 285 *
fep 0:5ff20db10a96 286 * @return pdTRUE if the semaphore was obtained. pdFALSE
fep 0:5ff20db10a96 287 * if xBlockTime expired without the semaphore becoming available.
fep 0:5ff20db10a96 288 *
fep 0:5ff20db10a96 289 * Example usage:
fep 0:5ff20db10a96 290 <pre>
fep 0:5ff20db10a96 291 SemaphoreHandle_t xSemaphore = NULL;
fep 0:5ff20db10a96 292
fep 0:5ff20db10a96 293 // A task that creates a semaphore.
fep 0:5ff20db10a96 294 void vATask( void * pvParameters )
fep 0:5ff20db10a96 295 {
fep 0:5ff20db10a96 296 // Create the semaphore to guard a shared resource.
fep 0:5ff20db10a96 297 xSemaphore = xSemaphoreCreateBinary();
fep 0:5ff20db10a96 298 }
fep 0:5ff20db10a96 299
fep 0:5ff20db10a96 300 // A task that uses the semaphore.
fep 0:5ff20db10a96 301 void vAnotherTask( void * pvParameters )
fep 0:5ff20db10a96 302 {
fep 0:5ff20db10a96 303 // ... Do other things.
fep 0:5ff20db10a96 304
fep 0:5ff20db10a96 305 if( xSemaphore != NULL )
fep 0:5ff20db10a96 306 {
fep 0:5ff20db10a96 307 // See if we can obtain the semaphore. If the semaphore is not available
fep 0:5ff20db10a96 308 // wait 10 ticks to see if it becomes free.
fep 0:5ff20db10a96 309 if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
fep 0:5ff20db10a96 310 {
fep 0:5ff20db10a96 311 // We were able to obtain the semaphore and can now access the
fep 0:5ff20db10a96 312 // shared resource.
fep 0:5ff20db10a96 313
fep 0:5ff20db10a96 314 // ...
fep 0:5ff20db10a96 315
fep 0:5ff20db10a96 316 // We have finished accessing the shared resource. Release the
fep 0:5ff20db10a96 317 // semaphore.
fep 0:5ff20db10a96 318 xSemaphoreGive( xSemaphore );
fep 0:5ff20db10a96 319 }
fep 0:5ff20db10a96 320 else
fep 0:5ff20db10a96 321 {
fep 0:5ff20db10a96 322 // We could not obtain the semaphore and can therefore not access
fep 0:5ff20db10a96 323 // the shared resource safely.
fep 0:5ff20db10a96 324 }
fep 0:5ff20db10a96 325 }
fep 0:5ff20db10a96 326 }
fep 0:5ff20db10a96 327 </pre>
fep 0:5ff20db10a96 328 * \defgroup xSemaphoreTake xSemaphoreTake
fep 0:5ff20db10a96 329 * \ingroup Semaphores
fep 0:5ff20db10a96 330 */
fep 0:5ff20db10a96 331 #define xSemaphoreTake( xSemaphore, xBlockTime ) xQueueGenericReceive( ( QueueHandle_t ) ( xSemaphore ), NULL, ( xBlockTime ), pdFALSE )
fep 0:5ff20db10a96 332
fep 0:5ff20db10a96 333 /**
fep 0:5ff20db10a96 334 * semphr. h
fep 0:5ff20db10a96 335 * xSemaphoreTakeRecursive(
fep 0:5ff20db10a96 336 * SemaphoreHandle_t xMutex,
fep 0:5ff20db10a96 337 * TickType_t xBlockTime
fep 0:5ff20db10a96 338 * )
fep 0:5ff20db10a96 339 *
fep 0:5ff20db10a96 340 * <i>Macro</i> to recursively obtain, or 'take', a mutex type semaphore.
fep 0:5ff20db10a96 341 * The mutex must have previously been created using a call to
fep 0:5ff20db10a96 342 * xSemaphoreCreateRecursiveMutex();
fep 0:5ff20db10a96 343 *
fep 0:5ff20db10a96 344 * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
fep 0:5ff20db10a96 345 * macro to be available.
fep 0:5ff20db10a96 346 *
fep 0:5ff20db10a96 347 * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
fep 0:5ff20db10a96 348 *
fep 0:5ff20db10a96 349 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
fep 0:5ff20db10a96 350 * doesn't become available again until the owner has called
fep 0:5ff20db10a96 351 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
fep 0:5ff20db10a96 352 * if a task successfully 'takes' the same mutex 5 times then the mutex will
fep 0:5ff20db10a96 353 * not be available to any other task until it has also 'given' the mutex back
fep 0:5ff20db10a96 354 * exactly five times.
fep 0:5ff20db10a96 355 *
fep 0:5ff20db10a96 356 * @param xMutex A handle to the mutex being obtained. This is the
fep 0:5ff20db10a96 357 * handle returned by xSemaphoreCreateRecursiveMutex();
fep 0:5ff20db10a96 358 *
fep 0:5ff20db10a96 359 * @param xBlockTime The time in ticks to wait for the semaphore to become
fep 0:5ff20db10a96 360 * available. The macro portTICK_PERIOD_MS can be used to convert this to a
fep 0:5ff20db10a96 361 * real time. A block time of zero can be used to poll the semaphore. If
fep 0:5ff20db10a96 362 * the task already owns the semaphore then xSemaphoreTakeRecursive() will
fep 0:5ff20db10a96 363 * return immediately no matter what the value of xBlockTime.
fep 0:5ff20db10a96 364 *
fep 0:5ff20db10a96 365 * @return pdTRUE if the semaphore was obtained. pdFALSE if xBlockTime
fep 0:5ff20db10a96 366 * expired without the semaphore becoming available.
fep 0:5ff20db10a96 367 *
fep 0:5ff20db10a96 368 * Example usage:
fep 0:5ff20db10a96 369 <pre>
fep 0:5ff20db10a96 370 SemaphoreHandle_t xMutex = NULL;
fep 0:5ff20db10a96 371
fep 0:5ff20db10a96 372 // A task that creates a mutex.
fep 0:5ff20db10a96 373 void vATask( void * pvParameters )
fep 0:5ff20db10a96 374 {
fep 0:5ff20db10a96 375 // Create the mutex to guard a shared resource.
fep 0:5ff20db10a96 376 xMutex = xSemaphoreCreateRecursiveMutex();
fep 0:5ff20db10a96 377 }
fep 0:5ff20db10a96 378
fep 0:5ff20db10a96 379 // A task that uses the mutex.
fep 0:5ff20db10a96 380 void vAnotherTask( void * pvParameters )
fep 0:5ff20db10a96 381 {
fep 0:5ff20db10a96 382 // ... Do other things.
fep 0:5ff20db10a96 383
fep 0:5ff20db10a96 384 if( xMutex != NULL )
fep 0:5ff20db10a96 385 {
fep 0:5ff20db10a96 386 // See if we can obtain the mutex. If the mutex is not available
fep 0:5ff20db10a96 387 // wait 10 ticks to see if it becomes free.
fep 0:5ff20db10a96 388 if( xSemaphoreTakeRecursive( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
fep 0:5ff20db10a96 389 {
fep 0:5ff20db10a96 390 // We were able to obtain the mutex and can now access the
fep 0:5ff20db10a96 391 // shared resource.
fep 0:5ff20db10a96 392
fep 0:5ff20db10a96 393 // ...
fep 0:5ff20db10a96 394 // For some reason due to the nature of the code further calls to
fep 0:5ff20db10a96 395 // xSemaphoreTakeRecursive() are made on the same mutex. In real
fep 0:5ff20db10a96 396 // code these would not be just sequential calls as this would make
fep 0:5ff20db10a96 397 // no sense. Instead the calls are likely to be buried inside
fep 0:5ff20db10a96 398 // a more complex call structure.
fep 0:5ff20db10a96 399 xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
fep 0:5ff20db10a96 400 xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
fep 0:5ff20db10a96 401
fep 0:5ff20db10a96 402 // The mutex has now been 'taken' three times, so will not be
fep 0:5ff20db10a96 403 // available to another task until it has also been given back
fep 0:5ff20db10a96 404 // three times. Again it is unlikely that real code would have
fep 0:5ff20db10a96 405 // these calls sequentially, but instead buried in a more complex
fep 0:5ff20db10a96 406 // call structure. This is just for illustrative purposes.
fep 0:5ff20db10a96 407 xSemaphoreGiveRecursive( xMutex );
fep 0:5ff20db10a96 408 xSemaphoreGiveRecursive( xMutex );
fep 0:5ff20db10a96 409 xSemaphoreGiveRecursive( xMutex );
fep 0:5ff20db10a96 410
fep 0:5ff20db10a96 411 // Now the mutex can be taken by other tasks.
fep 0:5ff20db10a96 412 }
fep 0:5ff20db10a96 413 else
fep 0:5ff20db10a96 414 {
fep 0:5ff20db10a96 415 // We could not obtain the mutex and can therefore not access
fep 0:5ff20db10a96 416 // the shared resource safely.
fep 0:5ff20db10a96 417 }
fep 0:5ff20db10a96 418 }
fep 0:5ff20db10a96 419 }
fep 0:5ff20db10a96 420 </pre>
fep 0:5ff20db10a96 421 * \defgroup xSemaphoreTakeRecursive xSemaphoreTakeRecursive
fep 0:5ff20db10a96 422 * \ingroup Semaphores
fep 0:5ff20db10a96 423 */
fep 0:5ff20db10a96 424 #if( configUSE_RECURSIVE_MUTEXES == 1 )
fep 0:5ff20db10a96 425 #define xSemaphoreTakeRecursive( xMutex, xBlockTime ) xQueueTakeMutexRecursive( ( xMutex ), ( xBlockTime ) )
fep 0:5ff20db10a96 426 #endif
fep 0:5ff20db10a96 427
fep 0:5ff20db10a96 428 /**
fep 0:5ff20db10a96 429 * semphr. h
fep 0:5ff20db10a96 430 * <pre>xSemaphoreGive( SemaphoreHandle_t xSemaphore )</pre>
fep 0:5ff20db10a96 431 *
fep 0:5ff20db10a96 432 * <i>Macro</i> to release a semaphore. The semaphore must have previously been
fep 0:5ff20db10a96 433 * created with a call to xSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
fep 0:5ff20db10a96 434 * xSemaphoreCreateCounting(). and obtained using sSemaphoreTake().
fep 0:5ff20db10a96 435 *
fep 0:5ff20db10a96 436 * This macro must not be used from an ISR. See xSemaphoreGiveFromISR () for
fep 0:5ff20db10a96 437 * an alternative which can be used from an ISR.
fep 0:5ff20db10a96 438 *
fep 0:5ff20db10a96 439 * This macro must also not be used on semaphores created using
fep 0:5ff20db10a96 440 * xSemaphoreCreateRecursiveMutex().
fep 0:5ff20db10a96 441 *
fep 0:5ff20db10a96 442 * @param xSemaphore A handle to the semaphore being released. This is the
fep 0:5ff20db10a96 443 * handle returned when the semaphore was created.
fep 0:5ff20db10a96 444 *
fep 0:5ff20db10a96 445 * @return pdTRUE if the semaphore was released. pdFALSE if an error occurred.
fep 0:5ff20db10a96 446 * Semaphores are implemented using queues. An error can occur if there is
fep 0:5ff20db10a96 447 * no space on the queue to post a message - indicating that the
fep 0:5ff20db10a96 448 * semaphore was not first obtained correctly.
fep 0:5ff20db10a96 449 *
fep 0:5ff20db10a96 450 * Example usage:
fep 0:5ff20db10a96 451 <pre>
fep 0:5ff20db10a96 452 SemaphoreHandle_t xSemaphore = NULL;
fep 0:5ff20db10a96 453
fep 0:5ff20db10a96 454 void vATask( void * pvParameters )
fep 0:5ff20db10a96 455 {
fep 0:5ff20db10a96 456 // Create the semaphore to guard a shared resource.
fep 0:5ff20db10a96 457 xSemaphore = vSemaphoreCreateBinary();
fep 0:5ff20db10a96 458
fep 0:5ff20db10a96 459 if( xSemaphore != NULL )
fep 0:5ff20db10a96 460 {
fep 0:5ff20db10a96 461 if( xSemaphoreGive( xSemaphore ) != pdTRUE )
fep 0:5ff20db10a96 462 {
fep 0:5ff20db10a96 463 // We would expect this call to fail because we cannot give
fep 0:5ff20db10a96 464 // a semaphore without first "taking" it!
fep 0:5ff20db10a96 465 }
fep 0:5ff20db10a96 466
fep 0:5ff20db10a96 467 // Obtain the semaphore - don't block if the semaphore is not
fep 0:5ff20db10a96 468 // immediately available.
fep 0:5ff20db10a96 469 if( xSemaphoreTake( xSemaphore, ( TickType_t ) 0 ) )
fep 0:5ff20db10a96 470 {
fep 0:5ff20db10a96 471 // We now have the semaphore and can access the shared resource.
fep 0:5ff20db10a96 472
fep 0:5ff20db10a96 473 // ...
fep 0:5ff20db10a96 474
fep 0:5ff20db10a96 475 // We have finished accessing the shared resource so can free the
fep 0:5ff20db10a96 476 // semaphore.
fep 0:5ff20db10a96 477 if( xSemaphoreGive( xSemaphore ) != pdTRUE )
fep 0:5ff20db10a96 478 {
fep 0:5ff20db10a96 479 // We would not expect this call to fail because we must have
fep 0:5ff20db10a96 480 // obtained the semaphore to get here.
fep 0:5ff20db10a96 481 }
fep 0:5ff20db10a96 482 }
fep 0:5ff20db10a96 483 }
fep 0:5ff20db10a96 484 }
fep 0:5ff20db10a96 485 </pre>
fep 0:5ff20db10a96 486 * \defgroup xSemaphoreGive xSemaphoreGive
fep 0:5ff20db10a96 487 * \ingroup Semaphores
fep 0:5ff20db10a96 488 */
fep 0:5ff20db10a96 489 #define xSemaphoreGive( xSemaphore ) xQueueGenericSend( ( QueueHandle_t ) ( xSemaphore ), NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
fep 0:5ff20db10a96 490
fep 0:5ff20db10a96 491 /**
fep 0:5ff20db10a96 492 * semphr. h
fep 0:5ff20db10a96 493 * <pre>xSemaphoreGiveRecursive( SemaphoreHandle_t xMutex )</pre>
fep 0:5ff20db10a96 494 *
fep 0:5ff20db10a96 495 * <i>Macro</i> to recursively release, or 'give', a mutex type semaphore.
fep 0:5ff20db10a96 496 * The mutex must have previously been created using a call to
fep 0:5ff20db10a96 497 * xSemaphoreCreateRecursiveMutex();
fep 0:5ff20db10a96 498 *
fep 0:5ff20db10a96 499 * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
fep 0:5ff20db10a96 500 * macro to be available.
fep 0:5ff20db10a96 501 *
fep 0:5ff20db10a96 502 * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
fep 0:5ff20db10a96 503 *
fep 0:5ff20db10a96 504 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
fep 0:5ff20db10a96 505 * doesn't become available again until the owner has called
fep 0:5ff20db10a96 506 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
fep 0:5ff20db10a96 507 * if a task successfully 'takes' the same mutex 5 times then the mutex will
fep 0:5ff20db10a96 508 * not be available to any other task until it has also 'given' the mutex back
fep 0:5ff20db10a96 509 * exactly five times.
fep 0:5ff20db10a96 510 *
fep 0:5ff20db10a96 511 * @param xMutex A handle to the mutex being released, or 'given'. This is the
fep 0:5ff20db10a96 512 * handle returned by xSemaphoreCreateMutex();
fep 0:5ff20db10a96 513 *
fep 0:5ff20db10a96 514 * @return pdTRUE if the semaphore was given.
fep 0:5ff20db10a96 515 *
fep 0:5ff20db10a96 516 * Example usage:
fep 0:5ff20db10a96 517 <pre>
fep 0:5ff20db10a96 518 SemaphoreHandle_t xMutex = NULL;
fep 0:5ff20db10a96 519
fep 0:5ff20db10a96 520 // A task that creates a mutex.
fep 0:5ff20db10a96 521 void vATask( void * pvParameters )
fep 0:5ff20db10a96 522 {
fep 0:5ff20db10a96 523 // Create the mutex to guard a shared resource.
fep 0:5ff20db10a96 524 xMutex = xSemaphoreCreateRecursiveMutex();
fep 0:5ff20db10a96 525 }
fep 0:5ff20db10a96 526
fep 0:5ff20db10a96 527 // A task that uses the mutex.
fep 0:5ff20db10a96 528 void vAnotherTask( void * pvParameters )
fep 0:5ff20db10a96 529 {
fep 0:5ff20db10a96 530 // ... Do other things.
fep 0:5ff20db10a96 531
fep 0:5ff20db10a96 532 if( xMutex != NULL )
fep 0:5ff20db10a96 533 {
fep 0:5ff20db10a96 534 // See if we can obtain the mutex. If the mutex is not available
fep 0:5ff20db10a96 535 // wait 10 ticks to see if it becomes free.
fep 0:5ff20db10a96 536 if( xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 ) == pdTRUE )
fep 0:5ff20db10a96 537 {
fep 0:5ff20db10a96 538 // We were able to obtain the mutex and can now access the
fep 0:5ff20db10a96 539 // shared resource.
fep 0:5ff20db10a96 540
fep 0:5ff20db10a96 541 // ...
fep 0:5ff20db10a96 542 // For some reason due to the nature of the code further calls to
fep 0:5ff20db10a96 543 // xSemaphoreTakeRecursive() are made on the same mutex. In real
fep 0:5ff20db10a96 544 // code these would not be just sequential calls as this would make
fep 0:5ff20db10a96 545 // no sense. Instead the calls are likely to be buried inside
fep 0:5ff20db10a96 546 // a more complex call structure.
fep 0:5ff20db10a96 547 xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
fep 0:5ff20db10a96 548 xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
fep 0:5ff20db10a96 549
fep 0:5ff20db10a96 550 // The mutex has now been 'taken' three times, so will not be
fep 0:5ff20db10a96 551 // available to another task until it has also been given back
fep 0:5ff20db10a96 552 // three times. Again it is unlikely that real code would have
fep 0:5ff20db10a96 553 // these calls sequentially, it would be more likely that the calls
fep 0:5ff20db10a96 554 // to xSemaphoreGiveRecursive() would be called as a call stack
fep 0:5ff20db10a96 555 // unwound. This is just for demonstrative purposes.
fep 0:5ff20db10a96 556 xSemaphoreGiveRecursive( xMutex );
fep 0:5ff20db10a96 557 xSemaphoreGiveRecursive( xMutex );
fep 0:5ff20db10a96 558 xSemaphoreGiveRecursive( xMutex );
fep 0:5ff20db10a96 559
fep 0:5ff20db10a96 560 // Now the mutex can be taken by other tasks.
fep 0:5ff20db10a96 561 }
fep 0:5ff20db10a96 562 else
fep 0:5ff20db10a96 563 {
fep 0:5ff20db10a96 564 // We could not obtain the mutex and can therefore not access
fep 0:5ff20db10a96 565 // the shared resource safely.
fep 0:5ff20db10a96 566 }
fep 0:5ff20db10a96 567 }
fep 0:5ff20db10a96 568 }
fep 0:5ff20db10a96 569 </pre>
fep 0:5ff20db10a96 570 * \defgroup xSemaphoreGiveRecursive xSemaphoreGiveRecursive
fep 0:5ff20db10a96 571 * \ingroup Semaphores
fep 0:5ff20db10a96 572 */
fep 0:5ff20db10a96 573 #if( configUSE_RECURSIVE_MUTEXES == 1 )
fep 0:5ff20db10a96 574 #define xSemaphoreGiveRecursive( xMutex ) xQueueGiveMutexRecursive( ( xMutex ) )
fep 0:5ff20db10a96 575 #endif
fep 0:5ff20db10a96 576
fep 0:5ff20db10a96 577 /**
fep 0:5ff20db10a96 578 * semphr. h
fep 0:5ff20db10a96 579 * <pre>
fep 0:5ff20db10a96 580 xSemaphoreGiveFromISR(
fep 0:5ff20db10a96 581 SemaphoreHandle_t xSemaphore,
fep 0:5ff20db10a96 582 BaseType_t *pxHigherPriorityTaskWoken
fep 0:5ff20db10a96 583 )</pre>
fep 0:5ff20db10a96 584 *
fep 0:5ff20db10a96 585 * <i>Macro</i> to release a semaphore. The semaphore must have previously been
fep 0:5ff20db10a96 586 * created with a call to xSemaphoreCreateBinary() or xSemaphoreCreateCounting().
fep 0:5ff20db10a96 587 *
fep 0:5ff20db10a96 588 * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
fep 0:5ff20db10a96 589 * must not be used with this macro.
fep 0:5ff20db10a96 590 *
fep 0:5ff20db10a96 591 * This macro can be used from an ISR.
fep 0:5ff20db10a96 592 *
fep 0:5ff20db10a96 593 * @param xSemaphore A handle to the semaphore being released. This is the
fep 0:5ff20db10a96 594 * handle returned when the semaphore was created.
fep 0:5ff20db10a96 595 *
fep 0:5ff20db10a96 596 * @param pxHigherPriorityTaskWoken xSemaphoreGiveFromISR() will set
fep 0:5ff20db10a96 597 * *pxHigherPriorityTaskWoken to pdTRUE if giving the semaphore caused a task
fep 0:5ff20db10a96 598 * to unblock, and the unblocked task has a priority higher than the currently
fep 0:5ff20db10a96 599 * running task. If xSemaphoreGiveFromISR() sets this value to pdTRUE then
fep 0:5ff20db10a96 600 * a context switch should be requested before the interrupt is exited.
fep 0:5ff20db10a96 601 *
fep 0:5ff20db10a96 602 * @return pdTRUE if the semaphore was successfully given, otherwise errQUEUE_FULL.
fep 0:5ff20db10a96 603 *
fep 0:5ff20db10a96 604 * Example usage:
fep 0:5ff20db10a96 605 <pre>
fep 0:5ff20db10a96 606 \#define LONG_TIME 0xffff
fep 0:5ff20db10a96 607 \#define TICKS_TO_WAIT 10
fep 0:5ff20db10a96 608 SemaphoreHandle_t xSemaphore = NULL;
fep 0:5ff20db10a96 609
fep 0:5ff20db10a96 610 // Repetitive task.
fep 0:5ff20db10a96 611 void vATask( void * pvParameters )
fep 0:5ff20db10a96 612 {
fep 0:5ff20db10a96 613 for( ;; )
fep 0:5ff20db10a96 614 {
fep 0:5ff20db10a96 615 // We want this task to run every 10 ticks of a timer. The semaphore
fep 0:5ff20db10a96 616 // was created before this task was started.
fep 0:5ff20db10a96 617
fep 0:5ff20db10a96 618 // Block waiting for the semaphore to become available.
fep 0:5ff20db10a96 619 if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )
fep 0:5ff20db10a96 620 {
fep 0:5ff20db10a96 621 // It is time to execute.
fep 0:5ff20db10a96 622
fep 0:5ff20db10a96 623 // ...
fep 0:5ff20db10a96 624
fep 0:5ff20db10a96 625 // We have finished our task. Return to the top of the loop where
fep 0:5ff20db10a96 626 // we will block on the semaphore until it is time to execute
fep 0:5ff20db10a96 627 // again. Note when using the semaphore for synchronisation with an
fep 0:5ff20db10a96 628 // ISR in this manner there is no need to 'give' the semaphore back.
fep 0:5ff20db10a96 629 }
fep 0:5ff20db10a96 630 }
fep 0:5ff20db10a96 631 }
fep 0:5ff20db10a96 632
fep 0:5ff20db10a96 633 // Timer ISR
fep 0:5ff20db10a96 634 void vTimerISR( void * pvParameters )
fep 0:5ff20db10a96 635 {
fep 0:5ff20db10a96 636 static uint8_t ucLocalTickCount = 0;
fep 0:5ff20db10a96 637 static BaseType_t xHigherPriorityTaskWoken;
fep 0:5ff20db10a96 638
fep 0:5ff20db10a96 639 // A timer tick has occurred.
fep 0:5ff20db10a96 640
fep 0:5ff20db10a96 641 // ... Do other time functions.
fep 0:5ff20db10a96 642
fep 0:5ff20db10a96 643 // Is it time for vATask () to run?
fep 0:5ff20db10a96 644 xHigherPriorityTaskWoken = pdFALSE;
fep 0:5ff20db10a96 645 ucLocalTickCount++;
fep 0:5ff20db10a96 646 if( ucLocalTickCount >= TICKS_TO_WAIT )
fep 0:5ff20db10a96 647 {
fep 0:5ff20db10a96 648 // Unblock the task by releasing the semaphore.
fep 0:5ff20db10a96 649 xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
fep 0:5ff20db10a96 650
fep 0:5ff20db10a96 651 // Reset the count so we release the semaphore again in 10 ticks time.
fep 0:5ff20db10a96 652 ucLocalTickCount = 0;
fep 0:5ff20db10a96 653 }
fep 0:5ff20db10a96 654
fep 0:5ff20db10a96 655 if( xHigherPriorityTaskWoken != pdFALSE )
fep 0:5ff20db10a96 656 {
fep 0:5ff20db10a96 657 // We can force a context switch here. Context switching from an
fep 0:5ff20db10a96 658 // ISR uses port specific syntax. Check the demo task for your port
fep 0:5ff20db10a96 659 // to find the syntax required.
fep 0:5ff20db10a96 660 }
fep 0:5ff20db10a96 661 }
fep 0:5ff20db10a96 662 </pre>
fep 0:5ff20db10a96 663 * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR
fep 0:5ff20db10a96 664 * \ingroup Semaphores
fep 0:5ff20db10a96 665 */
fep 0:5ff20db10a96 666 #define xSemaphoreGiveFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueGiveFromISR( ( QueueHandle_t ) ( xSemaphore ), ( pxHigherPriorityTaskWoken ) )
fep 0:5ff20db10a96 667
fep 0:5ff20db10a96 668 /**
fep 0:5ff20db10a96 669 * semphr. h
fep 0:5ff20db10a96 670 * <pre>
fep 0:5ff20db10a96 671 xSemaphoreTakeFromISR(
fep 0:5ff20db10a96 672 SemaphoreHandle_t xSemaphore,
fep 0:5ff20db10a96 673 BaseType_t *pxHigherPriorityTaskWoken
fep 0:5ff20db10a96 674 )</pre>
fep 0:5ff20db10a96 675 *
fep 0:5ff20db10a96 676 * <i>Macro</i> to take a semaphore from an ISR. The semaphore must have
fep 0:5ff20db10a96 677 * previously been created with a call to xSemaphoreCreateBinary() or
fep 0:5ff20db10a96 678 * xSemaphoreCreateCounting().
fep 0:5ff20db10a96 679 *
fep 0:5ff20db10a96 680 * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
fep 0:5ff20db10a96 681 * must not be used with this macro.
fep 0:5ff20db10a96 682 *
fep 0:5ff20db10a96 683 * This macro can be used from an ISR, however taking a semaphore from an ISR
fep 0:5ff20db10a96 684 * is not a common operation. It is likely to only be useful when taking a
fep 0:5ff20db10a96 685 * counting semaphore when an interrupt is obtaining an object from a resource
fep 0:5ff20db10a96 686 * pool (when the semaphore count indicates the number of resources available).
fep 0:5ff20db10a96 687 *
fep 0:5ff20db10a96 688 * @param xSemaphore A handle to the semaphore being taken. This is the
fep 0:5ff20db10a96 689 * handle returned when the semaphore was created.
fep 0:5ff20db10a96 690 *
fep 0:5ff20db10a96 691 * @param pxHigherPriorityTaskWoken xSemaphoreTakeFromISR() will set
fep 0:5ff20db10a96 692 * *pxHigherPriorityTaskWoken to pdTRUE if taking the semaphore caused a task
fep 0:5ff20db10a96 693 * to unblock, and the unblocked task has a priority higher than the currently
fep 0:5ff20db10a96 694 * running task. If xSemaphoreTakeFromISR() sets this value to pdTRUE then
fep 0:5ff20db10a96 695 * a context switch should be requested before the interrupt is exited.
fep 0:5ff20db10a96 696 *
fep 0:5ff20db10a96 697 * @return pdTRUE if the semaphore was successfully taken, otherwise
fep 0:5ff20db10a96 698 * pdFALSE
fep 0:5ff20db10a96 699 */
fep 0:5ff20db10a96 700 #define xSemaphoreTakeFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueReceiveFromISR( ( QueueHandle_t ) ( xSemaphore ), NULL, ( pxHigherPriorityTaskWoken ) )
fep 0:5ff20db10a96 701
fep 0:5ff20db10a96 702 /**
fep 0:5ff20db10a96 703 * semphr. h
fep 0:5ff20db10a96 704 * <pre>SemaphoreHandle_t xSemaphoreCreateMutex( void )</pre>
fep 0:5ff20db10a96 705 *
fep 0:5ff20db10a96 706 * Creates a new mutex type semaphore instance, and returns a handle by which
fep 0:5ff20db10a96 707 * the new mutex can be referenced.
fep 0:5ff20db10a96 708 *
fep 0:5ff20db10a96 709 * Internally, within the FreeRTOS implementation, mutex semaphores use a block
fep 0:5ff20db10a96 710 * of memory, in which the mutex structure is stored. If a mutex is created
fep 0:5ff20db10a96 711 * using xSemaphoreCreateMutex() then the required memory is automatically
fep 0:5ff20db10a96 712 * dynamically allocated inside the xSemaphoreCreateMutex() function. (see
fep 0:5ff20db10a96 713 * http://www.freertos.org/a00111.html). If a mutex is created using
fep 0:5ff20db10a96 714 * xSemaphoreCreateMutexStatic() then the application writer must provided the
fep 0:5ff20db10a96 715 * memory. xSemaphoreCreateMutexStatic() therefore allows a mutex to be created
fep 0:5ff20db10a96 716 * without using any dynamic memory allocation.
fep 0:5ff20db10a96 717 *
fep 0:5ff20db10a96 718 * Mutexes created using this function can be accessed using the xSemaphoreTake()
fep 0:5ff20db10a96 719 * and xSemaphoreGive() macros. The xSemaphoreTakeRecursive() and
fep 0:5ff20db10a96 720 * xSemaphoreGiveRecursive() macros must not be used.
fep 0:5ff20db10a96 721 *
fep 0:5ff20db10a96 722 * This type of semaphore uses a priority inheritance mechanism so a task
fep 0:5ff20db10a96 723 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
fep 0:5ff20db10a96 724 * semaphore it is no longer required.
fep 0:5ff20db10a96 725 *
fep 0:5ff20db10a96 726 * Mutex type semaphores cannot be used from within interrupt service routines.
fep 0:5ff20db10a96 727 *
fep 0:5ff20db10a96 728 * See xSemaphoreCreateBinary() for an alternative implementation that can be
fep 0:5ff20db10a96 729 * used for pure synchronisation (where one task or interrupt always 'gives' the
fep 0:5ff20db10a96 730 * semaphore and another always 'takes' the semaphore) and from within interrupt
fep 0:5ff20db10a96 731 * service routines.
fep 0:5ff20db10a96 732 *
fep 0:5ff20db10a96 733 * @return If the mutex was successfully created then a handle to the created
fep 0:5ff20db10a96 734 * semaphore is returned. If there was not enough heap to allocate the mutex
fep 0:5ff20db10a96 735 * data structures then NULL is returned.
fep 0:5ff20db10a96 736 *
fep 0:5ff20db10a96 737 * Example usage:
fep 0:5ff20db10a96 738 <pre>
fep 0:5ff20db10a96 739 SemaphoreHandle_t xSemaphore;
fep 0:5ff20db10a96 740
fep 0:5ff20db10a96 741 void vATask( void * pvParameters )
fep 0:5ff20db10a96 742 {
fep 0:5ff20db10a96 743 // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
fep 0:5ff20db10a96 744 // This is a macro so pass the variable in directly.
fep 0:5ff20db10a96 745 xSemaphore = xSemaphoreCreateMutex();
fep 0:5ff20db10a96 746
fep 0:5ff20db10a96 747 if( xSemaphore != NULL )
fep 0:5ff20db10a96 748 {
fep 0:5ff20db10a96 749 // The semaphore was created successfully.
fep 0:5ff20db10a96 750 // The semaphore can now be used.
fep 0:5ff20db10a96 751 }
fep 0:5ff20db10a96 752 }
fep 0:5ff20db10a96 753 </pre>
fep 0:5ff20db10a96 754 * \defgroup xSemaphoreCreateMutex xSemaphoreCreateMutex
fep 0:5ff20db10a96 755 * \ingroup Semaphores
fep 0:5ff20db10a96 756 */
fep 0:5ff20db10a96 757 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
fep 0:5ff20db10a96 758 #define xSemaphoreCreateMutex() xQueueCreateMutex( queueQUEUE_TYPE_MUTEX )
fep 0:5ff20db10a96 759 #endif
fep 0:5ff20db10a96 760
fep 0:5ff20db10a96 761 /**
fep 0:5ff20db10a96 762 * semphr. h
fep 0:5ff20db10a96 763 * <pre>SemaphoreHandle_t xSemaphoreCreateMutexStatic( StaticSemaphore_t *pxMutexBuffer )</pre>
fep 0:5ff20db10a96 764 *
fep 0:5ff20db10a96 765 * Creates a new mutex type semaphore instance, and returns a handle by which
fep 0:5ff20db10a96 766 * the new mutex can be referenced.
fep 0:5ff20db10a96 767 *
fep 0:5ff20db10a96 768 * Internally, within the FreeRTOS implementation, mutex semaphores use a block
fep 0:5ff20db10a96 769 * of memory, in which the mutex structure is stored. If a mutex is created
fep 0:5ff20db10a96 770 * using xSemaphoreCreateMutex() then the required memory is automatically
fep 0:5ff20db10a96 771 * dynamically allocated inside the xSemaphoreCreateMutex() function. (see
fep 0:5ff20db10a96 772 * http://www.freertos.org/a00111.html). If a mutex is created using
fep 0:5ff20db10a96 773 * xSemaphoreCreateMutexStatic() then the application writer must provided the
fep 0:5ff20db10a96 774 * memory. xSemaphoreCreateMutexStatic() therefore allows a mutex to be created
fep 0:5ff20db10a96 775 * without using any dynamic memory allocation.
fep 0:5ff20db10a96 776 *
fep 0:5ff20db10a96 777 * Mutexes created using this function can be accessed using the xSemaphoreTake()
fep 0:5ff20db10a96 778 * and xSemaphoreGive() macros. The xSemaphoreTakeRecursive() and
fep 0:5ff20db10a96 779 * xSemaphoreGiveRecursive() macros must not be used.
fep 0:5ff20db10a96 780 *
fep 0:5ff20db10a96 781 * This type of semaphore uses a priority inheritance mechanism so a task
fep 0:5ff20db10a96 782 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
fep 0:5ff20db10a96 783 * semaphore it is no longer required.
fep 0:5ff20db10a96 784 *
fep 0:5ff20db10a96 785 * Mutex type semaphores cannot be used from within interrupt service routines.
fep 0:5ff20db10a96 786 *
fep 0:5ff20db10a96 787 * See xSemaphoreCreateBinary() for an alternative implementation that can be
fep 0:5ff20db10a96 788 * used for pure synchronisation (where one task or interrupt always 'gives' the
fep 0:5ff20db10a96 789 * semaphore and another always 'takes' the semaphore) and from within interrupt
fep 0:5ff20db10a96 790 * service routines.
fep 0:5ff20db10a96 791 *
fep 0:5ff20db10a96 792 * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,
fep 0:5ff20db10a96 793 * which will be used to hold the mutex's data structure, removing the need for
fep 0:5ff20db10a96 794 * the memory to be allocated dynamically.
fep 0:5ff20db10a96 795 *
fep 0:5ff20db10a96 796 * @return If the mutex was successfully created then a handle to the created
fep 0:5ff20db10a96 797 * mutex is returned. If pxMutexBuffer was NULL then NULL is returned.
fep 0:5ff20db10a96 798 *
fep 0:5ff20db10a96 799 * Example usage:
fep 0:5ff20db10a96 800 <pre>
fep 0:5ff20db10a96 801 SemaphoreHandle_t xSemaphore;
fep 0:5ff20db10a96 802 StaticSemaphore_t xMutexBuffer;
fep 0:5ff20db10a96 803
fep 0:5ff20db10a96 804 void vATask( void * pvParameters )
fep 0:5ff20db10a96 805 {
fep 0:5ff20db10a96 806 // A mutex cannot be used before it has been created. xMutexBuffer is
fep 0:5ff20db10a96 807 // into xSemaphoreCreateMutexStatic() so no dynamic memory allocation is
fep 0:5ff20db10a96 808 // attempted.
fep 0:5ff20db10a96 809 xSemaphore = xSemaphoreCreateMutexStatic( &xMutexBuffer );
fep 0:5ff20db10a96 810
fep 0:5ff20db10a96 811 // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,
fep 0:5ff20db10a96 812 // so there is no need to check it.
fep 0:5ff20db10a96 813 }
fep 0:5ff20db10a96 814 </pre>
fep 0:5ff20db10a96 815 * \defgroup xSemaphoreCreateMutexStatic xSemaphoreCreateMutexStatic
fep 0:5ff20db10a96 816 * \ingroup Semaphores
fep 0:5ff20db10a96 817 */
fep 0:5ff20db10a96 818 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
fep 0:5ff20db10a96 819 #define xSemaphoreCreateMutexStatic( pxMutexBuffer ) xQueueCreateMutexStatic( queueQUEUE_TYPE_MUTEX, ( pxMutexBuffer ) )
fep 0:5ff20db10a96 820 #endif /* configSUPPORT_STATIC_ALLOCATION */
fep 0:5ff20db10a96 821
fep 0:5ff20db10a96 822
fep 0:5ff20db10a96 823 /**
fep 0:5ff20db10a96 824 * semphr. h
fep 0:5ff20db10a96 825 * <pre>SemaphoreHandle_t xSemaphoreCreateRecursiveMutex( void )</pre>
fep 0:5ff20db10a96 826 *
fep 0:5ff20db10a96 827 * Creates a new recursive mutex type semaphore instance, and returns a handle
fep 0:5ff20db10a96 828 * by which the new recursive mutex can be referenced.
fep 0:5ff20db10a96 829 *
fep 0:5ff20db10a96 830 * Internally, within the FreeRTOS implementation, recursive mutexs use a block
fep 0:5ff20db10a96 831 * of memory, in which the mutex structure is stored. If a recursive mutex is
fep 0:5ff20db10a96 832 * created using xSemaphoreCreateRecursiveMutex() then the required memory is
fep 0:5ff20db10a96 833 * automatically dynamically allocated inside the
fep 0:5ff20db10a96 834 * xSemaphoreCreateRecursiveMutex() function. (see
fep 0:5ff20db10a96 835 * http://www.freertos.org/a00111.html). If a recursive mutex is created using
fep 0:5ff20db10a96 836 * xSemaphoreCreateRecursiveMutexStatic() then the application writer must
fep 0:5ff20db10a96 837 * provide the memory that will get used by the mutex.
fep 0:5ff20db10a96 838 * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to
fep 0:5ff20db10a96 839 * be created without using any dynamic memory allocation.
fep 0:5ff20db10a96 840 *
fep 0:5ff20db10a96 841 * Mutexes created using this macro can be accessed using the
fep 0:5ff20db10a96 842 * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros. The
fep 0:5ff20db10a96 843 * xSemaphoreTake() and xSemaphoreGive() macros must not be used.
fep 0:5ff20db10a96 844 *
fep 0:5ff20db10a96 845 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
fep 0:5ff20db10a96 846 * doesn't become available again until the owner has called
fep 0:5ff20db10a96 847 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
fep 0:5ff20db10a96 848 * if a task successfully 'takes' the same mutex 5 times then the mutex will
fep 0:5ff20db10a96 849 * not be available to any other task until it has also 'given' the mutex back
fep 0:5ff20db10a96 850 * exactly five times.
fep 0:5ff20db10a96 851 *
fep 0:5ff20db10a96 852 * This type of semaphore uses a priority inheritance mechanism so a task
fep 0:5ff20db10a96 853 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
fep 0:5ff20db10a96 854 * semaphore it is no longer required.
fep 0:5ff20db10a96 855 *
fep 0:5ff20db10a96 856 * Mutex type semaphores cannot be used from within interrupt service routines.
fep 0:5ff20db10a96 857 *
fep 0:5ff20db10a96 858 * See xSemaphoreCreateBinary() for an alternative implementation that can be
fep 0:5ff20db10a96 859 * used for pure synchronisation (where one task or interrupt always 'gives' the
fep 0:5ff20db10a96 860 * semaphore and another always 'takes' the semaphore) and from within interrupt
fep 0:5ff20db10a96 861 * service routines.
fep 0:5ff20db10a96 862 *
fep 0:5ff20db10a96 863 * @return xSemaphore Handle to the created mutex semaphore. Should be of type
fep 0:5ff20db10a96 864 * SemaphoreHandle_t.
fep 0:5ff20db10a96 865 *
fep 0:5ff20db10a96 866 * Example usage:
fep 0:5ff20db10a96 867 <pre>
fep 0:5ff20db10a96 868 SemaphoreHandle_t xSemaphore;
fep 0:5ff20db10a96 869
fep 0:5ff20db10a96 870 void vATask( void * pvParameters )
fep 0:5ff20db10a96 871 {
fep 0:5ff20db10a96 872 // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
fep 0:5ff20db10a96 873 // This is a macro so pass the variable in directly.
fep 0:5ff20db10a96 874 xSemaphore = xSemaphoreCreateRecursiveMutex();
fep 0:5ff20db10a96 875
fep 0:5ff20db10a96 876 if( xSemaphore != NULL )
fep 0:5ff20db10a96 877 {
fep 0:5ff20db10a96 878 // The semaphore was created successfully.
fep 0:5ff20db10a96 879 // The semaphore can now be used.
fep 0:5ff20db10a96 880 }
fep 0:5ff20db10a96 881 }
fep 0:5ff20db10a96 882 </pre>
fep 0:5ff20db10a96 883 * \defgroup xSemaphoreCreateRecursiveMutex xSemaphoreCreateRecursiveMutex
fep 0:5ff20db10a96 884 * \ingroup Semaphores
fep 0:5ff20db10a96 885 */
fep 0:5ff20db10a96 886 #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configUSE_RECURSIVE_MUTEXES == 1 ) )
fep 0:5ff20db10a96 887 #define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex( queueQUEUE_TYPE_RECURSIVE_MUTEX )
fep 0:5ff20db10a96 888 #endif
fep 0:5ff20db10a96 889
fep 0:5ff20db10a96 890 /**
fep 0:5ff20db10a96 891 * semphr. h
fep 0:5ff20db10a96 892 * <pre>SemaphoreHandle_t xSemaphoreCreateRecursiveMutexStatic( StaticSemaphore_t *pxMutexBuffer )</pre>
fep 0:5ff20db10a96 893 *
fep 0:5ff20db10a96 894 * Creates a new recursive mutex type semaphore instance, and returns a handle
fep 0:5ff20db10a96 895 * by which the new recursive mutex can be referenced.
fep 0:5ff20db10a96 896 *
fep 0:5ff20db10a96 897 * Internally, within the FreeRTOS implementation, recursive mutexs use a block
fep 0:5ff20db10a96 898 * of memory, in which the mutex structure is stored. If a recursive mutex is
fep 0:5ff20db10a96 899 * created using xSemaphoreCreateRecursiveMutex() then the required memory is
fep 0:5ff20db10a96 900 * automatically dynamically allocated inside the
fep 0:5ff20db10a96 901 * xSemaphoreCreateRecursiveMutex() function. (see
fep 0:5ff20db10a96 902 * http://www.freertos.org/a00111.html). If a recursive mutex is created using
fep 0:5ff20db10a96 903 * xSemaphoreCreateRecursiveMutexStatic() then the application writer must
fep 0:5ff20db10a96 904 * provide the memory that will get used by the mutex.
fep 0:5ff20db10a96 905 * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to
fep 0:5ff20db10a96 906 * be created without using any dynamic memory allocation.
fep 0:5ff20db10a96 907 *
fep 0:5ff20db10a96 908 * Mutexes created using this macro can be accessed using the
fep 0:5ff20db10a96 909 * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros. The
fep 0:5ff20db10a96 910 * xSemaphoreTake() and xSemaphoreGive() macros must not be used.
fep 0:5ff20db10a96 911 *
fep 0:5ff20db10a96 912 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
fep 0:5ff20db10a96 913 * doesn't become available again until the owner has called
fep 0:5ff20db10a96 914 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
fep 0:5ff20db10a96 915 * if a task successfully 'takes' the same mutex 5 times then the mutex will
fep 0:5ff20db10a96 916 * not be available to any other task until it has also 'given' the mutex back
fep 0:5ff20db10a96 917 * exactly five times.
fep 0:5ff20db10a96 918 *
fep 0:5ff20db10a96 919 * This type of semaphore uses a priority inheritance mechanism so a task
fep 0:5ff20db10a96 920 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
fep 0:5ff20db10a96 921 * semaphore it is no longer required.
fep 0:5ff20db10a96 922 *
fep 0:5ff20db10a96 923 * Mutex type semaphores cannot be used from within interrupt service routines.
fep 0:5ff20db10a96 924 *
fep 0:5ff20db10a96 925 * See xSemaphoreCreateBinary() for an alternative implementation that can be
fep 0:5ff20db10a96 926 * used for pure synchronisation (where one task or interrupt always 'gives' the
fep 0:5ff20db10a96 927 * semaphore and another always 'takes' the semaphore) and from within interrupt
fep 0:5ff20db10a96 928 * service routines.
fep 0:5ff20db10a96 929 *
fep 0:5ff20db10a96 930 * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,
fep 0:5ff20db10a96 931 * which will then be used to hold the recursive mutex's data structure,
fep 0:5ff20db10a96 932 * removing the need for the memory to be allocated dynamically.
fep 0:5ff20db10a96 933 *
fep 0:5ff20db10a96 934 * @return If the recursive mutex was successfully created then a handle to the
fep 0:5ff20db10a96 935 * created recursive mutex is returned. If pxMutexBuffer was NULL then NULL is
fep 0:5ff20db10a96 936 * returned.
fep 0:5ff20db10a96 937 *
fep 0:5ff20db10a96 938 * Example usage:
fep 0:5ff20db10a96 939 <pre>
fep 0:5ff20db10a96 940 SemaphoreHandle_t xSemaphore;
fep 0:5ff20db10a96 941 StaticSemaphore_t xMutexBuffer;
fep 0:5ff20db10a96 942
fep 0:5ff20db10a96 943 void vATask( void * pvParameters )
fep 0:5ff20db10a96 944 {
fep 0:5ff20db10a96 945 // A recursive semaphore cannot be used before it is created. Here a
fep 0:5ff20db10a96 946 // recursive mutex is created using xSemaphoreCreateRecursiveMutexStatic().
fep 0:5ff20db10a96 947 // The address of xMutexBuffer is passed into the function, and will hold
fep 0:5ff20db10a96 948 // the mutexes data structures - so no dynamic memory allocation will be
fep 0:5ff20db10a96 949 // attempted.
fep 0:5ff20db10a96 950 xSemaphore = xSemaphoreCreateRecursiveMutexStatic( &xMutexBuffer );
fep 0:5ff20db10a96 951
fep 0:5ff20db10a96 952 // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,
fep 0:5ff20db10a96 953 // so there is no need to check it.
fep 0:5ff20db10a96 954 }
fep 0:5ff20db10a96 955 </pre>
fep 0:5ff20db10a96 956 * \defgroup xSemaphoreCreateRecursiveMutexStatic xSemaphoreCreateRecursiveMutexStatic
fep 0:5ff20db10a96 957 * \ingroup Semaphores
fep 0:5ff20db10a96 958 */
fep 0:5ff20db10a96 959 #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configUSE_RECURSIVE_MUTEXES == 1 ) )
fep 0:5ff20db10a96 960 #define xSemaphoreCreateRecursiveMutexStatic( pxStaticSemaphore ) xQueueCreateMutexStatic( queueQUEUE_TYPE_RECURSIVE_MUTEX, pxStaticSemaphore )
fep 0:5ff20db10a96 961 #endif /* configSUPPORT_STATIC_ALLOCATION */
fep 0:5ff20db10a96 962
fep 0:5ff20db10a96 963 /**
fep 0:5ff20db10a96 964 * semphr. h
fep 0:5ff20db10a96 965 * <pre>SemaphoreHandle_t xSemaphoreCreateCounting( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount )</pre>
fep 0:5ff20db10a96 966 *
fep 0:5ff20db10a96 967 * Creates a new counting semaphore instance, and returns a handle by which the
fep 0:5ff20db10a96 968 * new counting semaphore can be referenced.
fep 0:5ff20db10a96 969 *
fep 0:5ff20db10a96 970 * In many usage scenarios it is faster and more memory efficient to use a
fep 0:5ff20db10a96 971 * direct to task notification in place of a counting semaphore!
fep 0:5ff20db10a96 972 * http://www.freertos.org/RTOS-task-notifications.html
fep 0:5ff20db10a96 973 *
fep 0:5ff20db10a96 974 * Internally, within the FreeRTOS implementation, counting semaphores use a
fep 0:5ff20db10a96 975 * block of memory, in which the counting semaphore structure is stored. If a
fep 0:5ff20db10a96 976 * counting semaphore is created using xSemaphoreCreateCounting() then the
fep 0:5ff20db10a96 977 * required memory is automatically dynamically allocated inside the
fep 0:5ff20db10a96 978 * xSemaphoreCreateCounting() function. (see
fep 0:5ff20db10a96 979 * http://www.freertos.org/a00111.html). If a counting semaphore is created
fep 0:5ff20db10a96 980 * using xSemaphoreCreateCountingStatic() then the application writer can
fep 0:5ff20db10a96 981 * instead optionally provide the memory that will get used by the counting
fep 0:5ff20db10a96 982 * semaphore. xSemaphoreCreateCountingStatic() therefore allows a counting
fep 0:5ff20db10a96 983 * semaphore to be created without using any dynamic memory allocation.
fep 0:5ff20db10a96 984 *
fep 0:5ff20db10a96 985 * Counting semaphores are typically used for two things:
fep 0:5ff20db10a96 986 *
fep 0:5ff20db10a96 987 * 1) Counting events.
fep 0:5ff20db10a96 988 *
fep 0:5ff20db10a96 989 * In this usage scenario an event handler will 'give' a semaphore each time
fep 0:5ff20db10a96 990 * an event occurs (incrementing the semaphore count value), and a handler
fep 0:5ff20db10a96 991 * task will 'take' a semaphore each time it processes an event
fep 0:5ff20db10a96 992 * (decrementing the semaphore count value). The count value is therefore
fep 0:5ff20db10a96 993 * the difference between the number of events that have occurred and the
fep 0:5ff20db10a96 994 * number that have been processed. In this case it is desirable for the
fep 0:5ff20db10a96 995 * initial count value to be zero.
fep 0:5ff20db10a96 996 *
fep 0:5ff20db10a96 997 * 2) Resource management.
fep 0:5ff20db10a96 998 *
fep 0:5ff20db10a96 999 * In this usage scenario the count value indicates the number of resources
fep 0:5ff20db10a96 1000 * available. To obtain control of a resource a task must first obtain a
fep 0:5ff20db10a96 1001 * semaphore - decrementing the semaphore count value. When the count value
fep 0:5ff20db10a96 1002 * reaches zero there are no free resources. When a task finishes with the
fep 0:5ff20db10a96 1003 * resource it 'gives' the semaphore back - incrementing the semaphore count
fep 0:5ff20db10a96 1004 * value. In this case it is desirable for the initial count value to be
fep 0:5ff20db10a96 1005 * equal to the maximum count value, indicating that all resources are free.
fep 0:5ff20db10a96 1006 *
fep 0:5ff20db10a96 1007 * @param uxMaxCount The maximum count value that can be reached. When the
fep 0:5ff20db10a96 1008 * semaphore reaches this value it can no longer be 'given'.
fep 0:5ff20db10a96 1009 *
fep 0:5ff20db10a96 1010 * @param uxInitialCount The count value assigned to the semaphore when it is
fep 0:5ff20db10a96 1011 * created.
fep 0:5ff20db10a96 1012 *
fep 0:5ff20db10a96 1013 * @return Handle to the created semaphore. Null if the semaphore could not be
fep 0:5ff20db10a96 1014 * created.
fep 0:5ff20db10a96 1015 *
fep 0:5ff20db10a96 1016 * Example usage:
fep 0:5ff20db10a96 1017 <pre>
fep 0:5ff20db10a96 1018 SemaphoreHandle_t xSemaphore;
fep 0:5ff20db10a96 1019
fep 0:5ff20db10a96 1020 void vATask( void * pvParameters )
fep 0:5ff20db10a96 1021 {
fep 0:5ff20db10a96 1022 SemaphoreHandle_t xSemaphore = NULL;
fep 0:5ff20db10a96 1023
fep 0:5ff20db10a96 1024 // Semaphore cannot be used before a call to xSemaphoreCreateCounting().
fep 0:5ff20db10a96 1025 // The max value to which the semaphore can count should be 10, and the
fep 0:5ff20db10a96 1026 // initial value assigned to the count should be 0.
fep 0:5ff20db10a96 1027 xSemaphore = xSemaphoreCreateCounting( 10, 0 );
fep 0:5ff20db10a96 1028
fep 0:5ff20db10a96 1029 if( xSemaphore != NULL )
fep 0:5ff20db10a96 1030 {
fep 0:5ff20db10a96 1031 // The semaphore was created successfully.
fep 0:5ff20db10a96 1032 // The semaphore can now be used.
fep 0:5ff20db10a96 1033 }
fep 0:5ff20db10a96 1034 }
fep 0:5ff20db10a96 1035 </pre>
fep 0:5ff20db10a96 1036 * \defgroup xSemaphoreCreateCounting xSemaphoreCreateCounting
fep 0:5ff20db10a96 1037 * \ingroup Semaphores
fep 0:5ff20db10a96 1038 */
fep 0:5ff20db10a96 1039 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
fep 0:5ff20db10a96 1040 #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( ( uxMaxCount ), ( uxInitialCount ) )
fep 0:5ff20db10a96 1041 #endif
fep 0:5ff20db10a96 1042
fep 0:5ff20db10a96 1043 /**
fep 0:5ff20db10a96 1044 * semphr. h
fep 0:5ff20db10a96 1045 * <pre>SemaphoreHandle_t xSemaphoreCreateCountingStatic( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount, StaticSemaphore_t *pxSemaphoreBuffer )</pre>
fep 0:5ff20db10a96 1046 *
fep 0:5ff20db10a96 1047 * Creates a new counting semaphore instance, and returns a handle by which the
fep 0:5ff20db10a96 1048 * new counting semaphore can be referenced.
fep 0:5ff20db10a96 1049 *
fep 0:5ff20db10a96 1050 * In many usage scenarios it is faster and more memory efficient to use a
fep 0:5ff20db10a96 1051 * direct to task notification in place of a counting semaphore!
fep 0:5ff20db10a96 1052 * http://www.freertos.org/RTOS-task-notifications.html
fep 0:5ff20db10a96 1053 *
fep 0:5ff20db10a96 1054 * Internally, within the FreeRTOS implementation, counting semaphores use a
fep 0:5ff20db10a96 1055 * block of memory, in which the counting semaphore structure is stored. If a
fep 0:5ff20db10a96 1056 * counting semaphore is created using xSemaphoreCreateCounting() then the
fep 0:5ff20db10a96 1057 * required memory is automatically dynamically allocated inside the
fep 0:5ff20db10a96 1058 * xSemaphoreCreateCounting() function. (see
fep 0:5ff20db10a96 1059 * http://www.freertos.org/a00111.html). If a counting semaphore is created
fep 0:5ff20db10a96 1060 * using xSemaphoreCreateCountingStatic() then the application writer must
fep 0:5ff20db10a96 1061 * provide the memory. xSemaphoreCreateCountingStatic() therefore allows a
fep 0:5ff20db10a96 1062 * counting semaphore to be created without using any dynamic memory allocation.
fep 0:5ff20db10a96 1063 *
fep 0:5ff20db10a96 1064 * Counting semaphores are typically used for two things:
fep 0:5ff20db10a96 1065 *
fep 0:5ff20db10a96 1066 * 1) Counting events.
fep 0:5ff20db10a96 1067 *
fep 0:5ff20db10a96 1068 * In this usage scenario an event handler will 'give' a semaphore each time
fep 0:5ff20db10a96 1069 * an event occurs (incrementing the semaphore count value), and a handler
fep 0:5ff20db10a96 1070 * task will 'take' a semaphore each time it processes an event
fep 0:5ff20db10a96 1071 * (decrementing the semaphore count value). The count value is therefore
fep 0:5ff20db10a96 1072 * the difference between the number of events that have occurred and the
fep 0:5ff20db10a96 1073 * number that have been processed. In this case it is desirable for the
fep 0:5ff20db10a96 1074 * initial count value to be zero.
fep 0:5ff20db10a96 1075 *
fep 0:5ff20db10a96 1076 * 2) Resource management.
fep 0:5ff20db10a96 1077 *
fep 0:5ff20db10a96 1078 * In this usage scenario the count value indicates the number of resources
fep 0:5ff20db10a96 1079 * available. To obtain control of a resource a task must first obtain a
fep 0:5ff20db10a96 1080 * semaphore - decrementing the semaphore count value. When the count value
fep 0:5ff20db10a96 1081 * reaches zero there are no free resources. When a task finishes with the
fep 0:5ff20db10a96 1082 * resource it 'gives' the semaphore back - incrementing the semaphore count
fep 0:5ff20db10a96 1083 * value. In this case it is desirable for the initial count value to be
fep 0:5ff20db10a96 1084 * equal to the maximum count value, indicating that all resources are free.
fep 0:5ff20db10a96 1085 *
fep 0:5ff20db10a96 1086 * @param uxMaxCount The maximum count value that can be reached. When the
fep 0:5ff20db10a96 1087 * semaphore reaches this value it can no longer be 'given'.
fep 0:5ff20db10a96 1088 *
fep 0:5ff20db10a96 1089 * @param uxInitialCount The count value assigned to the semaphore when it is
fep 0:5ff20db10a96 1090 * created.
fep 0:5ff20db10a96 1091 *
fep 0:5ff20db10a96 1092 * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,
fep 0:5ff20db10a96 1093 * which will then be used to hold the semaphore's data structure, removing the
fep 0:5ff20db10a96 1094 * need for the memory to be allocated dynamically.
fep 0:5ff20db10a96 1095 *
fep 0:5ff20db10a96 1096 * @return If the counting semaphore was successfully created then a handle to
fep 0:5ff20db10a96 1097 * the created counting semaphore is returned. If pxSemaphoreBuffer was NULL
fep 0:5ff20db10a96 1098 * then NULL is returned.
fep 0:5ff20db10a96 1099 *
fep 0:5ff20db10a96 1100 * Example usage:
fep 0:5ff20db10a96 1101 <pre>
fep 0:5ff20db10a96 1102 SemaphoreHandle_t xSemaphore;
fep 0:5ff20db10a96 1103 StaticSemaphore_t xSemaphoreBuffer;
fep 0:5ff20db10a96 1104
fep 0:5ff20db10a96 1105 void vATask( void * pvParameters )
fep 0:5ff20db10a96 1106 {
fep 0:5ff20db10a96 1107 SemaphoreHandle_t xSemaphore = NULL;
fep 0:5ff20db10a96 1108
fep 0:5ff20db10a96 1109 // Counting semaphore cannot be used before they have been created. Create
fep 0:5ff20db10a96 1110 // a counting semaphore using xSemaphoreCreateCountingStatic(). The max
fep 0:5ff20db10a96 1111 // value to which the semaphore can count is 10, and the initial value
fep 0:5ff20db10a96 1112 // assigned to the count will be 0. The address of xSemaphoreBuffer is
fep 0:5ff20db10a96 1113 // passed in and will be used to hold the semaphore structure, so no dynamic
fep 0:5ff20db10a96 1114 // memory allocation will be used.
fep 0:5ff20db10a96 1115 xSemaphore = xSemaphoreCreateCounting( 10, 0, &xSemaphoreBuffer );
fep 0:5ff20db10a96 1116
fep 0:5ff20db10a96 1117 // No memory allocation was attempted so xSemaphore cannot be NULL, so there
fep 0:5ff20db10a96 1118 // is no need to check its value.
fep 0:5ff20db10a96 1119 }
fep 0:5ff20db10a96 1120 </pre>
fep 0:5ff20db10a96 1121 * \defgroup xSemaphoreCreateCountingStatic xSemaphoreCreateCountingStatic
fep 0:5ff20db10a96 1122 * \ingroup Semaphores
fep 0:5ff20db10a96 1123 */
fep 0:5ff20db10a96 1124 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
fep 0:5ff20db10a96 1125 #define xSemaphoreCreateCountingStatic( uxMaxCount, uxInitialCount, pxSemaphoreBuffer ) xQueueCreateCountingSemaphoreStatic( ( uxMaxCount ), ( uxInitialCount ), ( pxSemaphoreBuffer ) )
fep 0:5ff20db10a96 1126 #endif /* configSUPPORT_STATIC_ALLOCATION */
fep 0:5ff20db10a96 1127
fep 0:5ff20db10a96 1128 /**
fep 0:5ff20db10a96 1129 * semphr. h
fep 0:5ff20db10a96 1130 * <pre>void vSemaphoreDelete( SemaphoreHandle_t xSemaphore );</pre>
fep 0:5ff20db10a96 1131 *
fep 0:5ff20db10a96 1132 * Delete a semaphore. This function must be used with care. For example,
fep 0:5ff20db10a96 1133 * do not delete a mutex type semaphore if the mutex is held by a task.
fep 0:5ff20db10a96 1134 *
fep 0:5ff20db10a96 1135 * @param xSemaphore A handle to the semaphore to be deleted.
fep 0:5ff20db10a96 1136 *
fep 0:5ff20db10a96 1137 * \defgroup vSemaphoreDelete vSemaphoreDelete
fep 0:5ff20db10a96 1138 * \ingroup Semaphores
fep 0:5ff20db10a96 1139 */
fep 0:5ff20db10a96 1140 #define vSemaphoreDelete( xSemaphore ) vQueueDelete( ( QueueHandle_t ) ( xSemaphore ) )
fep 0:5ff20db10a96 1141
fep 0:5ff20db10a96 1142 /**
fep 0:5ff20db10a96 1143 * semphr.h
fep 0:5ff20db10a96 1144 * <pre>TaskHandle_t xSemaphoreGetMutexHolder( SemaphoreHandle_t xMutex );</pre>
fep 0:5ff20db10a96 1145 *
fep 0:5ff20db10a96 1146 * If xMutex is indeed a mutex type semaphore, return the current mutex holder.
fep 0:5ff20db10a96 1147 * If xMutex is not a mutex type semaphore, or the mutex is available (not held
fep 0:5ff20db10a96 1148 * by a task), return NULL.
fep 0:5ff20db10a96 1149 *
fep 0:5ff20db10a96 1150 * Note: This is a good way of determining if the calling task is the mutex
fep 0:5ff20db10a96 1151 * holder, but not a good way of determining the identity of the mutex holder as
fep 0:5ff20db10a96 1152 * the holder may change between the function exiting and the returned value
fep 0:5ff20db10a96 1153 * being tested.
fep 0:5ff20db10a96 1154 */
fep 0:5ff20db10a96 1155 #define xSemaphoreGetMutexHolder( xSemaphore ) xQueueGetMutexHolder( ( xSemaphore ) )
fep 0:5ff20db10a96 1156
fep 0:5ff20db10a96 1157 /**
fep 0:5ff20db10a96 1158 * semphr.h
fep 0:5ff20db10a96 1159 * <pre>UBaseType_t uxSemaphoreGetCount( SemaphoreHandle_t xSemaphore );</pre>
fep 0:5ff20db10a96 1160 *
fep 0:5ff20db10a96 1161 * If the semaphore is a counting semaphore then uxSemaphoreGetCount() returns
fep 0:5ff20db10a96 1162 * its current count value. If the semaphore is a binary semaphore then
fep 0:5ff20db10a96 1163 * uxSemaphoreGetCount() returns 1 if the semaphore is available, and 0 if the
fep 0:5ff20db10a96 1164 * semaphore is not available.
fep 0:5ff20db10a96 1165 *
fep 0:5ff20db10a96 1166 */
fep 0:5ff20db10a96 1167 #define uxSemaphoreGetCount( xSemaphore ) uxQueueMessagesWaiting( ( QueueHandle_t ) ( xSemaphore ) )
fep 0:5ff20db10a96 1168
fep 0:5ff20db10a96 1169 #endif /* SEMAPHORE_H */
fep 0:5ff20db10a96 1170
fep 0:5ff20db10a96 1171
fep 0:5ff20db10a96 1172