2 * Copyright (c) 2015-2016, Texas Instruments Incorporated
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * ======== SemaphoreP_freertos.c ========
37 #include <ti/drivers/dpl/SemaphoreP.h>
38 #include <ti/drivers/dpl/HwiP.h>
45 * ======== SemaphoreP_create ========
47 SemaphoreP_Handle SemaphoreP_create(unsigned int count,
48 SemaphoreP_Params *params)
50 SemaphoreHandle_t sem = NULL;
51 SemaphoreP_Params semParams;
55 SemaphoreP_Params_init(params);
58 if (params->mode == SemaphoreP_Mode_COUNTING) {
59 #if (configUSE_COUNTING_SEMAPHORES == 1)
60 sem = xSemaphoreCreateCounting((UBaseType_t)params->maxCount,
65 sem = xSemaphoreCreateBinary();
71 return ((SemaphoreP_Handle)sem);
75 * ======== SemaphoreP_delete ========
77 SemaphoreP_Status SemaphoreP_delete(SemaphoreP_Handle handle)
79 vSemaphoreDelete((SemaphoreHandle_t)handle);
80 return (SemaphoreP_OK);
84 * ======== SemaphoreP_Params_init ========
86 void SemaphoreP_Params_init(SemaphoreP_Params *params)
88 params->mode = SemaphoreP_Mode_BINARY;
91 params->callback = NULL;
95 * ======== SemaphoreP_pend ========
97 SemaphoreP_Status SemaphoreP_pend(SemaphoreP_Handle handle, uint32_t timeout)
103 if (timeout == SemaphoreP_WAIT_FOREVER) {
104 ticks = portMAX_DELAY;
107 tickRateMS = (configTICK_RATE_HZ / 1000);
109 * Don't wait if tick rate resolution is greater than 1ms and
110 * prevent potential division by 0 when calculating the ticks to
113 if (tickRateMS == 0) {
117 ticks = (timeout / tickRateMS);
121 status = xSemaphoreTake((SemaphoreHandle_t)handle, ticks);
123 if (status == pdTRUE) {
124 return (SemaphoreP_OK);
127 return (SemaphoreP_TIMEOUT);
131 * ======== SemaphoreP_post ========
133 SemaphoreP_Status SemaphoreP_post(SemaphoreP_Handle handle)
135 BaseType_t xHigherPriorityTaskWoken;
137 SemaphoreP_Status status;
141 xSemaphoreGive((SemaphoreHandle_t)handle);
142 status = SemaphoreP_OK;
145 result = xSemaphoreGiveFromISR((SemaphoreHandle_t)handle,
146 &xHigherPriorityTaskWoken);
148 if (result == pdTRUE) {
149 status = SemaphoreP_OK;
152 /* The queue is full */
153 status = SemaphoreP_FAILURE;
160 * ======== SemaphoreP_postFromClock ========
162 SemaphoreP_Status SemaphoreP_postFromClock(SemaphoreP_Handle handle)
164 return (SemaphoreP_post(handle));