1 /*
2 * Copyright 2017, NXP
3 * All rights reserved.
4 *
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include <FreeRTOS.h>
10 #include <semphr.h>
11
12 #include "srtm_sem.h"
13 #include "fsl_common.h"
14
15 /*******************************************************************************
16 * Definitions
17 ******************************************************************************/
18
19 /*******************************************************************************
20 * Prototypes
21 ******************************************************************************/
22
23 /*******************************************************************************
24 * Variables
25 ******************************************************************************/
26
27 /*******************************************************************************
28 * Code
29 ******************************************************************************/
30 #if defined(SRTM_STATIC_API) && SRTM_STATIC_API
SRTM_Sem_Create(uint32_t maxCount,uint32_t initCount,srtm_sem_buf_t * stack)31 srtm_sem_t SRTM_Sem_Create(uint32_t maxCount, uint32_t initCount, srtm_sem_buf_t *stack)
32 {
33 return xSemaphoreCreateCountingStatic(maxCount, initCount, stack);
34 }
35 #else
SRTM_Sem_Create(uint32_t maxCount,uint32_t initCount)36 srtm_sem_t SRTM_Sem_Create(uint32_t maxCount, uint32_t initCount)
37 {
38 return xSemaphoreCreateCounting(maxCount, initCount);
39 }
40 #endif
41
SRTM_Sem_Destroy(srtm_sem_t sem)42 void SRTM_Sem_Destroy(srtm_sem_t sem)
43 {
44 assert(sem);
45
46 vSemaphoreDelete(sem);
47 }
48
SRTM_Sem_Post(srtm_sem_t sem)49 srtm_status_t SRTM_Sem_Post(srtm_sem_t sem)
50 {
51 srtm_status_t status = SRTM_Status_Error;
52 portBASE_TYPE taskToWake = pdFALSE;
53
54 if (__get_IPSR() != 0U)
55 {
56 if (xSemaphoreGiveFromISR(sem, &taskToWake) == pdPASS)
57 {
58 portYIELD_FROM_ISR(taskToWake);
59 status = SRTM_Status_Success;
60 }
61 }
62 else
63 {
64 if (xSemaphoreGive(sem) == pdTRUE)
65 {
66 status = SRTM_Status_Success;
67 }
68 }
69
70 return status;
71 }
72
SRTM_Sem_Wait(srtm_sem_t sem,uint32_t timeout)73 srtm_status_t SRTM_Sem_Wait(srtm_sem_t sem, uint32_t timeout)
74 {
75 uint32_t ticks;
76 srtm_status_t status = SRTM_Status_Success;
77
78 if (timeout == SRTM_WAIT_FOR_EVER)
79 {
80 ticks = portMAX_DELAY;
81 }
82 else if (timeout == SRTM_NO_WAIT)
83 {
84 ticks = 0U;
85 }
86 else
87 {
88 ticks = ((uint32_t)configTICK_RATE_HZ * (uint32_t)(timeout - 1U)) / 1000U + 1U;
89 }
90
91 if (xSemaphoreTake(sem, ticks) == pdFALSE)
92 {
93 status = SRTM_Status_Timeout;
94 }
95
96 return status;
97 }
98