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 if (xPortIsInsideInterrupt() == pdTRUE)
54 {
55 if (xSemaphoreGiveFromISR(sem, &taskToWake) == pdPASS)
56 {
57 portYIELD_FROM_ISR(taskToWake);
58 status = SRTM_Status_Success;
59 }
60 }
61 else
62 {
63 if (xSemaphoreGive(sem) == pdTRUE)
64 {
65 status = SRTM_Status_Success;
66 }
67 }
68
69 return status;
70 }
71
SRTM_Sem_Wait(srtm_sem_t sem,uint32_t timeout)72 srtm_status_t SRTM_Sem_Wait(srtm_sem_t sem, uint32_t timeout)
73 {
74 uint32_t ticks;
75 srtm_status_t status = SRTM_Status_Success;
76
77 if (timeout == SRTM_WAIT_FOR_EVER)
78 {
79 ticks = portMAX_DELAY;
80 }
81 else if (timeout == SRTM_NO_WAIT)
82 {
83 ticks = 0U;
84 }
85 else
86 {
87 ticks = ((uint32_t)configTICK_RATE_HZ * (uint32_t)(timeout - 1U)) / 1000U + 1U;
88 }
89
90 if (xSemaphoreTake(sem, ticks) == pdFALSE)
91 {
92 status = SRTM_Status_Timeout;
93 }
94
95 return status;
96 }
97