aboutsummaryrefslogtreecommitdiff
path: root/applications/message_handler_openamp/main.cpp
blob: 9d3c074b655aafaec0ea34d5db8ca03e7c82c7ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use _this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*****************************************************************************
 * Includes
 *****************************************************************************/

#include <cinttypes>
#include <memory>
#include <stdio.h>

#include <ethosu_log.h>
#include <mailbox.hpp>

#if defined(MHU_V2)
#include <mhu_v2.hpp>
#elif defined(MHU_JUNO)
#include <mhu_juno.hpp>
#else
#include <mhu_dummy.hpp>
#endif

#include "inference_runner.hpp"
#include "message_handler.hpp"
#include "remoteproc.hpp"

/*****************************************************************************
 * TFLM arena
 *****************************************************************************/

// Number of parallell inference tasks. Typically one per NPU.
#if defined(ETHOSU) && defined(ETHOSU_NPU_COUNT) && ETHOSU_NPU_COUNT > 0
constexpr size_t NUM_PARALLEL_TASKS = ETHOSU_NPU_COUNT;
#else
constexpr size_t NUM_PARALLEL_TASKS = 1;
#endif

#ifndef TENSOR_ARENA_SIZE
#define TENSOR_ARENA_SIZE 2000000
#endif

// TensorArena static initialisation
constexpr size_t arenaSize = TENSOR_ARENA_SIZE;

/*****************************************************************************
 * Resource table
 *****************************************************************************/

extern "C" {
__attribute__((section(".resource_table"))) ResourceTable resourceTable(8, arenaSize *NUM_PARALLEL_TASKS);
}

/*****************************************************************************
 * Mailbox
 *****************************************************************************/

namespace {

#ifdef MHU_V2
Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase
#elif defined(MHU_JUNO)
Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS);
#else
Mailbox::MHUDummy mailbox;
#endif

#ifdef MHU_IRQ
void mailboxIrqHandler() {
    LOG_DEBUG("");
    mailbox.handleMessage();
}
#endif

} // namespace

/*****************************************************************************
 * main
 *****************************************************************************/

int main() {
    printf("Ethos-U Message Handler OpenAMP\n");

    auto mem            = std::make_shared<MetalIO>();
    auto rproc          = std::make_shared<RProc>(mailbox, resourceTable.table, sizeof(resourceTable), *mem);
    auto messageHandler = std::make_shared<MessageHandler>(*rproc, "ethos-u-0.0");

    printf("TFLM arena. pa=%" PRIx32 ", da=%" PRIx32 ", len=%" PRIx32 "\n",
           resourceTable.carveout.da,
           resourceTable.carveout.pa,
           resourceTable.carveout.len);

    std::array<std::shared_ptr<InferenceRunner>, NUM_PARALLEL_TASKS> inferenceRunner;

    for (size_t i = 0; i < NUM_PARALLEL_TASKS; i++) {
        auto tensorArena = static_cast<uint8_t *>(messageHandler->physicalToVirtual(resourceTable.carveout.pa));

        inferenceRunner[i] = std::make_shared<InferenceRunner>(&tensorArena[arenaSize * i],
                                                               arenaSize,
                                                               messageHandler->getInferenceQueue(),
                                                               messageHandler->getResponseQueue());
    }

#ifdef MHU_IRQ
    // Register mailbox interrupt handler
    NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler);
    NVIC_EnableIRQ((IRQn_Type)MHU_IRQ);
#endif

    // Start Scheduler
    vTaskStartScheduler();

    return 0;
}