blob: d023ad2a8f3525b9ac03d9d480088bbbeaa1e74f [file] [log] [blame]
Davide Grohmann144b2d22022-05-31 15:24:02 +02001/*
Jonny Svärd87f6f7e2023-01-16 16:05:36 +01002 * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
Davide Grohmann144b2d22022-05-31 15:24:02 +02003 *
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) {
Jonny Svärd87f6f7e2023-01-16 16:05:36 +010052 return xSemaphoreCreateCounting(255, 0);
Davide Grohmann144b2d22022-05-31 15:24:02 +020053}
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 {
Davide Grohmann144b2d22022-05-31 15:24:02 +020072 if (xSemaphoreGive(handle) != pdTRUE) {
Jonny Svärd87f6f7e2023-01-16 16:05:36 +010073 printf("Error: Failed to give semaphore.\n");
74 return -1;
Davide Grohmann144b2d22022-05-31 15:24:02 +020075 }
76 }
77 return 0;
78}
79}
80
81#endif