blob: bc043fafee9ee41b730b0048961d5fe595796f00 [file] [log] [blame]
Davide Grohmann144b2d22022-05-31 15:24:02 +02001/*
2 * Copyright (c) 2022 Arm Limited.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#if defined(ETHOSU)
20
21#include "FreeRTOS.h"
22#include "semphr.h"
23
24#include <ethosu_driver.h>
25#include <stdio.h>
26
27extern "C" {
28
29void *ethosu_mutex_create(void) {
30 return xSemaphoreCreateMutex();
31}
32
33int ethosu_mutex_lock(void *mutex) {
34 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex);
35 if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) {
36 printf("Error: Failed to lock mutex.\n");
37 return -1;
38 }
39 return 0;
40}
41
42int ethosu_mutex_unlock(void *mutex) {
43 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex);
44 if (xSemaphoreGive(handle) != pdTRUE) {
45 printf("Error: Failed to unlock mutex.\n");
46 return -1;
47 }
48 return 0;
49}
50
51void *ethosu_semaphore_create(void) {
52 return xSemaphoreCreateBinary();
53}
54
55int ethosu_semaphore_take(void *sem) {
56 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem);
57 if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) {
58 printf("Error: Failed to take semaphore.\n");
59 return -1;
60 }
61 return 0;
62}
63
64int ethosu_semaphore_give(void *sem) {
65 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem);
66 if (xPortIsInsideInterrupt()) {
67 if (xSemaphoreGiveFromISR(handle, NULL) != pdTRUE) {
68 printf("Error: Failed to give semaphore from ISR.\n");
69 return -1;
70 }
71 } else {
72 /* A FreeRTOS binary semaphore is fundamentally a queue that can only hold one item. If the queue is full,
73 * xSemaphoreGive will return a pdFALSE value. Ignoring the return value in here, as a semaphore give failure
74 * does not affect the application correctness. */
75 if (xSemaphoreGive(handle) != pdTRUE) {
76 // do nothing
77 }
78 }
79 return 0;
80}
81}
82
83#endif