Davide Grohmann | ec72e9b | 2022-08-08 17:30:28 +0200 | [diff] [blame] | 1 | /* |
| 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 | #include "FreeRTOS.h" |
| 20 | #include "task.h" |
| 21 | |
| 22 | #include "ethosu_core_interface.h" |
| 23 | #include "message_client.hpp" |
| 24 | |
| 25 | using namespace EthosU; |
| 26 | |
| 27 | namespace MessageHandler { |
| 28 | |
| 29 | MessageClient::MessageClient(EthosU::ethosu_core_queue &_inputMessageQueue, |
| 30 | EthosU::ethosu_core_queue &_outputMessageQueue, |
| 31 | Mailbox::Mailbox &_mailbox) : |
| 32 | input(_inputMessageQueue), |
| 33 | output(_outputMessageQueue), mailbox(_mailbox) {} |
| 34 | |
| 35 | bool MessageClient::sendInputMessage(const uint32_t type, const void *src, uint32_t length) { |
| 36 | if (!input.write(type, src, length)) { |
Davide Grohmann | f4379e9 | 2022-06-15 11:20:41 +0200 | [diff] [blame^] | 37 | printf("ERROR: Msg: Failed to write message request. No mailbox message sent\n"); |
Davide Grohmann | ec72e9b | 2022-08-08 17:30:28 +0200 | [diff] [blame] | 38 | return false; |
| 39 | } |
| 40 | |
| 41 | mailbox.sendMessage(); |
| 42 | mailbox.handleMessage(); |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | bool MessageClient::waitAndReadOutputMessage(const uint32_t expected_type, uint8_t *dst, uint32_t length) { |
Davide Grohmann | f4379e9 | 2022-06-15 11:20:41 +0200 | [diff] [blame^] | 47 | constexpr TickType_t delay = pdMS_TO_TICKS(5); |
Davide Grohmann | ec72e9b | 2022-08-08 17:30:28 +0200 | [diff] [blame] | 48 | constexpr TickType_t deadline = pdMS_TO_TICKS(/* 1 minute */ 60 * 1000 * 1000); |
| 49 | struct ethosu_core_msg msg; |
| 50 | |
| 51 | TickType_t totalDelay = 0; |
| 52 | while (output.available() == 0) { |
| 53 | vTaskDelay(delay); |
| 54 | totalDelay += delay; |
| 55 | if (totalDelay >= deadline) { |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (!output.read(msg)) { |
| 61 | printf("ERROR: Failed to read msg header\n"); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | if (msg.magic != ETHOSU_CORE_MSG_MAGIC) { |
| 66 | printf("ERROR: Invalid Magic\n"); |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | if (msg.type != expected_type) { |
Davide Grohmann | f4379e9 | 2022-06-15 11:20:41 +0200 | [diff] [blame^] | 71 | printf("ERROR: Wrong message type. Got %" PRIu32 " expected %" PRIu32 "\n", msg.type, expected_type); |
Davide Grohmann | ec72e9b | 2022-08-08 17:30:28 +0200 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | |
| 75 | if (msg.length != length) { |
| 76 | printf("ERROR: Wrong message size\n"); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | if (length == 0) { |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | if (!output.read(dst, length)) { |
| 85 | printf("ERROR: Failed to read msg payload\n"); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | } // namespace MessageHandler |