1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 
8 /************* Include Files ****************/
9 #include "cc_pal_types.h"
10 #include "cc_pal_mutex.h"
11 #include "FreeRTOS.h"
12 #include "semphr.h"
13 #include <stdio.h>
14 
15 /************************ Defines ******************************/
16 
17 /************************ Enums ******************************/
18 
19 /************************ Typedefs ******************************/
20 
21 /************************ Global Data ******************************/
22 
23 /************************ Private Functions ******************************/
24 
25 /************************ Public Functions ******************************/
26 
27 
28 /**
29  * @brief This function purpose is to create a mutex.
30  *
31  *
32  * @param[out] pMutexId - Pointer to created mutex handle
33  *
34  * @return returns 0 on success, otherwise indicates failure
35  */
CC_PalMutexCreate(CC_PalMutex * pMutexId)36 CCError_t CC_PalMutexCreate(CC_PalMutex *pMutexId)
37 {
38     *pMutexId = xSemaphoreCreateMutex(); // function returns SemaphoreHandle_t
39     if (*pMutexId != NULL)
40         return CC_SUCCESS;
41     return CC_FAIL;
42 }
43 
44 
45 /**
46  * @brief This function purpose is to destroy a mutex
47  *
48  *
49  * @param[in] pMutexId - pointer to Mutex handle
50  *
51  * @return returns 0 on success, otherwise indicates failure
52  */
CC_PalMutexDestroy(CC_PalMutex * pMutexId)53 CCError_t CC_PalMutexDestroy(CC_PalMutex *pMutexId)
54 {
55     vSemaphoreDelete(*pMutexId);
56     return CC_SUCCESS;
57 }
58 
59 /**
60  * @brief This function purpose is to Wait for Mutex with aTimeOut. aTimeOut is
61  *        specified in milliseconds. (CC_INFINITE is blocking)
62  *
63  *
64  * @param[in] pMutexId - pointer to Mutex handle
65  * @param[in] timeOut - timeout in mSec, or CC_INFINITE
66  *
67  * @return returns 0 on success, otherwise indicates failure
68  */
CC_PalMutexLock(CC_PalMutex * pMutexId,uint32_t timeOut)69 CCError_t CC_PalMutexLock(CC_PalMutex *pMutexId, uint32_t timeOut)
70 {
71     if(!CC_INFINITE)
72         timeOut = timeOut / portTICK_PERIOD_MS;
73     else
74         timeOut = portMAX_DELAY;
75 
76     return (xSemaphoreTake(*pMutexId, timeOut) ? CC_SUCCESS : CC_FAIL);
77 }
78 
79 /**
80  * @brief This function purpose is to release the mutex.
81  *
82  *
83  * @param[in] pMutexId - pointer to Mutex handle
84  *
85  * @return returns 0 on success, otherwise indicates failure
86  */
CC_PalMutexUnlock(CC_PalMutex * pMutexId)87 CCError_t CC_PalMutexUnlock(CC_PalMutex *pMutexId)
88 {
89     return (xSemaphoreGive(*pMutexId) ? CC_SUCCESS : CC_FAIL);
90 }
91