]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_SimpleLink_CC3220SF_CCS/ti/drivers/dpl/SemaphoreP_freertos.c
Add SimpleLink CC3220SF demo.
[freertos] / FreeRTOS / Demo / CORTEX_M4_SimpleLink_CC3220SF_CCS / ti / drivers / dpl / SemaphoreP_freertos.c
1 /*
2  * Copyright (c) 2015-2016, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
31  */
32
33 /*
34  *  ======== SemaphoreP_freertos.c ========
35  */
36
37 #include <ti/drivers/dpl/SemaphoreP.h>
38 #include <ti/drivers/dpl/HwiP.h>
39
40 #include <FreeRTOS.h>
41 #include <semphr.h>
42 #include <queue.h>
43
44 /*
45  *  ======== SemaphoreP_create ========
46  */
47 SemaphoreP_Handle SemaphoreP_create(unsigned int count,
48                                     SemaphoreP_Params *params)
49 {
50     SemaphoreHandle_t sem = NULL;
51     SemaphoreP_Params semParams;
52
53     if (params == NULL) {
54         params = &semParams;
55         SemaphoreP_Params_init(params);
56     }
57
58     if (params->mode == SemaphoreP_Mode_COUNTING) {
59 #if (configUSE_COUNTING_SEMAPHORES == 1)
60         sem = xSemaphoreCreateCounting((UBaseType_t)params->maxCount,
61                 (UBaseType_t)count);
62 #endif
63     }
64     else {
65         sem = xSemaphoreCreateBinary();
66         if (count != 0) {
67             xSemaphoreGive(sem);
68         }
69     }
70
71     return ((SemaphoreP_Handle)sem);
72 }
73
74 /*
75  *  ======== SemaphoreP_delete ========
76  */
77 SemaphoreP_Status SemaphoreP_delete(SemaphoreP_Handle handle)
78 {
79     vSemaphoreDelete((SemaphoreHandle_t)handle);
80     return (SemaphoreP_OK);
81 }
82
83 /*
84  *  ======== SemaphoreP_Params_init ========
85  */
86 void SemaphoreP_Params_init(SemaphoreP_Params *params)
87 {
88     params->mode = SemaphoreP_Mode_BINARY;
89     params->name = NULL;
90     params->maxCount = 1;
91     params->callback = NULL;
92 }
93
94 /*
95  *  ======== SemaphoreP_pend ========
96  */
97 SemaphoreP_Status SemaphoreP_pend(SemaphoreP_Handle handle, uint32_t timeout)
98 {
99     BaseType_t status;
100     uint32_t ticks;
101     uint32_t tickRateMS;
102
103     if (timeout == SemaphoreP_WAIT_FOREVER) {
104         ticks = portMAX_DELAY;
105     }
106     else {
107         tickRateMS = (configTICK_RATE_HZ / 1000);
108         /*
109          * Don't wait if tick rate resolution is greater than 1ms and
110          * prevent potential division by 0 when calculating the ticks to
111          * delay.
112          */
113         if (tickRateMS == 0) {
114             ticks = 0;
115         }
116         else {
117             ticks = (timeout / tickRateMS);
118         }
119     }
120
121     status = xSemaphoreTake((SemaphoreHandle_t)handle, ticks);
122
123     if (status == pdTRUE) {
124         return (SemaphoreP_OK);
125     }
126
127     return (SemaphoreP_TIMEOUT);
128 }
129
130 /*
131  *  ======== SemaphoreP_post ========
132  */
133 SemaphoreP_Status SemaphoreP_post(SemaphoreP_Handle handle)
134 {
135        BaseType_t xHigherPriorityTaskWoken;
136        BaseType_t result;
137        SemaphoreP_Status status;
138
139     if (!HwiP_inISR()) {
140         /* Not in ISR */
141         xSemaphoreGive((SemaphoreHandle_t)handle);
142         status = SemaphoreP_OK;
143     }
144     else {
145         result = xSemaphoreGiveFromISR((SemaphoreHandle_t)handle,
146             &xHigherPriorityTaskWoken);
147
148         if (result == pdTRUE) {
149             status = SemaphoreP_OK;
150         }
151         else {
152             /* The queue is full */
153             status = SemaphoreP_FAILURE;
154         }
155     }
156     return (status);
157 }
158
159 /*
160  *  ======== SemaphoreP_postFromClock ========
161  */
162 SemaphoreP_Status SemaphoreP_postFromClock(SemaphoreP_Handle handle)
163 {
164     return (SemaphoreP_post(handle));
165 }