Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 1 | /* |
Jonny Svärd | 39ef89b | 2024-04-17 18:07:01 +0200 | [diff] [blame^] | 2 | * SPDX-FileCopyrightText: Copyright 2022-2024 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 3 | * 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 | |
| 26 | extern "C" { |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 27 | void *ethosu_mutex_create(void) { |
| 28 | return xSemaphoreCreateMutex(); |
| 29 | } |
| 30 | |
Jonny Svärd | 39ef89b | 2024-04-17 18:07:01 +0200 | [diff] [blame^] | 31 | void ethosu_mutex_destroy(void *mutex) { |
| 32 | SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex); |
| 33 | vSemaphoreDelete(handle); |
| 34 | } |
| 35 | |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 36 | int 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 | |
| 45 | int 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 | |
| 54 | void *ethosu_semaphore_create(void) { |
Jonny Svärd | 87f6f7e | 2023-01-16 16:05:36 +0100 | [diff] [blame] | 55 | return xSemaphoreCreateCounting(255, 0); |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Jonny Svärd | 39ef89b | 2024-04-17 18:07:01 +0200 | [diff] [blame^] | 58 | void ethosu_semaphore_destroy(void *sem) { |
| 59 | SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem); |
| 60 | vSemaphoreDelete(handle); |
| 61 | } |
| 62 | |
Jonny Svärd | 244a310 | 2023-12-18 18:26:44 +0100 | [diff] [blame] | 63 | int ethosu_semaphore_take(void *sem, uint64_t timeout) { |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 64 | SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem); |
Jonny Svärd | 244a310 | 2023-12-18 18:26:44 +0100 | [diff] [blame] | 65 | if (xSemaphoreTake(handle, (TickType_t)timeout) != pdTRUE) { |
| 66 | // Semaphore take timed out |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 67 | return -1; |
| 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | int 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 Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 80 | if (xSemaphoreGive(handle) != pdTRUE) { |
Jonny Svärd | 87f6f7e | 2023-01-16 16:05:36 +0100 | [diff] [blame] | 81 | printf("Error: Failed to give semaphore.\n"); |
| 82 | return -1; |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | #endif |