blob: 5f2e6cc16f803af8f5bf62f0385205b12f8adcb4 [file] [log] [blame]
Davide Grohmann144b2d22022-05-31 15:24:02 +02001/*
Jonny Svärd39ef89b2024-04-17 18:07:01 +02002 * SPDX-FileCopyrightText: Copyright 2022-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
Davide Grohmann144b2d22022-05-31 15:24:02 +02003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the License); you may
6 * not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#if defined(ETHOSU)
19
20#include "FreeRTOS.h"
21#include "semphr.h"
22
23#include <ethosu_driver.h>
24#include <stdio.h>
25
26extern "C" {
Davide Grohmann144b2d22022-05-31 15:24:02 +020027void *ethosu_mutex_create(void) {
28 return xSemaphoreCreateMutex();
29}
30
Jonny Svärd39ef89b2024-04-17 18:07:01 +020031void ethosu_mutex_destroy(void *mutex) {
32 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex);
33 vSemaphoreDelete(handle);
34}
35
Davide Grohmann144b2d22022-05-31 15:24:02 +020036int ethosu_mutex_lock(void *mutex) {
37 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex);
38 if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) {
39 printf("Error: Failed to lock mutex.\n");
40 return -1;
41 }
42 return 0;
43}
44
45int ethosu_mutex_unlock(void *mutex) {
46 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex);
47 if (xSemaphoreGive(handle) != pdTRUE) {
48 printf("Error: Failed to unlock mutex.\n");
49 return -1;
50 }
51 return 0;
52}
53
54void *ethosu_semaphore_create(void) {
Jonny Svärd87f6f7e2023-01-16 16:05:36 +010055 return xSemaphoreCreateCounting(255, 0);
Davide Grohmann144b2d22022-05-31 15:24:02 +020056}
57
Jonny Svärd39ef89b2024-04-17 18:07:01 +020058void ethosu_semaphore_destroy(void *sem) {
59 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem);
60 vSemaphoreDelete(handle);
61}
62
Jonny Svärd244a3102023-12-18 18:26:44 +010063int ethosu_semaphore_take(void *sem, uint64_t timeout) {
Davide Grohmann144b2d22022-05-31 15:24:02 +020064 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem);
Jonny Svärd244a3102023-12-18 18:26:44 +010065 if (xSemaphoreTake(handle, (TickType_t)timeout) != pdTRUE) {
66 // Semaphore take timed out
Davide Grohmann144b2d22022-05-31 15:24:02 +020067 return -1;
68 }
69 return 0;
70}
71
72int ethosu_semaphore_give(void *sem) {
73 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem);
74 if (xPortIsInsideInterrupt()) {
75 if (xSemaphoreGiveFromISR(handle, NULL) != pdTRUE) {
76 printf("Error: Failed to give semaphore from ISR.\n");
77 return -1;
78 }
79 } else {
Davide Grohmann144b2d22022-05-31 15:24:02 +020080 if (xSemaphoreGive(handle) != pdTRUE) {
Jonny Svärd87f6f7e2023-01-16 16:05:36 +010081 printf("Error: Failed to give semaphore.\n");
82 return -1;
Davide Grohmann144b2d22022-05-31 15:24:02 +020083 }
84 }
85 return 0;
86}
87}
88
89#endif