1#
2# SPDX-FileCopyrightText: Copyright 2019-2021, 2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
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
19cmake_minimum_required(VERSION 3.15.6)
20
21project(ethosu_core_driver VERSION 0.0.1)
22
23#
24# Build options
25#
26
27option(DRIVER_PMU_AUTOINIT "Enable PMU boot auto-initialization" OFF)
28
29set(CMSIS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmsis" CACHE PATH "Path to CMSIS.")
30
31set(LOG_NAMES err warning info debug)
32set(ETHOSU_LOG_ENABLE ON CACHE BOOL "Toggle driver logs on/off (Defaults to ON)")
33set(ETHOSU_LOG_SEVERITY "warning" CACHE STRING "Driver log severity level ${LOG_NAMES} (Defaults to 'warning')")
34set(ETHOSU_TARGET_NPU_CONFIG "ethos-u55-128" CACHE STRING "Default NPU configuration")
35set(ETHOSU_INFERENCE_TIMEOUT "" CACHE STRING "Inference timeout (unit is implementation defined)")
36set_property(CACHE ETHOSU_LOG_SEVERITY PROPERTY STRINGS ${LOG_NAMES})
37
38#
39# Global settings
40#
41
42# Check that ETHOSU_LOG_SEVERITY has one of the supported levels
43list(FIND LOG_NAMES ${ETHOSU_LOG_SEVERITY} LOG_SEVERITY)
44if (${LOG_SEVERITY} EQUAL -1)
45    message(FATAL_ERROR "Unsupported log level ${ETHOSU_LOG_SEVERITY}")
46endif()
47
48# Make include directories available for current- and sub projects
49include_directories(include src)
50include_directories(${CMSIS_PATH}/CMSIS/Core/Include)
51
52#
53# Build libraries
54#
55
56# Build driver library
57add_library(ethosu_core_driver STATIC)
58target_include_directories(ethosu_core_driver PUBLIC include)
59target_sources(ethosu_core_driver PRIVATE src/ethosu_driver.c src/ethosu_pmu.c)
60
61string(TOLOWER ${ETHOSU_TARGET_NPU_CONFIG} ETHOSU_TARGET_NPU_CONFIG)
62if(ETHOSU_TARGET_NPU_CONFIG MATCHES "^ethos-(u[0-9]+|uz)-([0-9]+$)")
63    set(ETHOSU_ARCH ${CMAKE_MATCH_1})
64    set(ETHOSU_MACS ${CMAKE_MATCH_2})
65else()
66    message(FATAL_ERROR "Invalid Ethos-U target configuration '${ETHOSU_TARGET_NPU_CONFIG}")
67endif()
68
69target_compile_definitions(ethosu_core_driver PRIVATE
70    ETHOSU_ARCH=${ETHOSU_ARCH}
71    ETHOS$<UPPER_CASE:${ETHOSU_ARCH}>)
72
73if (ETHOSU_ARCH STREQUAL "u55" OR ETHOSU_ARCH STREQUAL "u65")
74    target_sources(ethosu_core_driver PRIVATE src/ethosu_device_u55_u65.c)
75else()
76    message(FATAL_ERROR "Invalid NPU configuration")
77endif()
78
79if(NOT "${ETHOSU_INFERENCE_TIMEOUT}" STREQUAL "")
80    target_compile_definitions(ethosu_core_driver PRIVATE
81        ETHOSU_SEMAPHORE_WAIT_INFERENCE=${ETHOSU_INFERENCE_TIMEOUT})
82    set(ETHOSU_INFERENCE_TIMEOUT_TEXT ${ETHOSU_INFERENCE_TIMEOUT})
83else()
84    set(ETHOSU_INFERENCE_TIMEOUT_TEXT "Default (no timeout)")
85endif()
86# Set the log level for the target
87target_compile_definitions(ethosu_core_driver PRIVATE
88    ETHOSU_LOG_SEVERITY=${LOG_SEVERITY}
89    ETHOSU_LOG_ENABLE=$<BOOL:${ETHOSU_LOG_ENABLE}>)
90
91# Install library and include files
92install(TARGETS ethosu_core_driver LIBRARY DESTINATION "lib")
93install(FILES include/ethosu_device.h include/ethosu_driver.h include/pmu_ethosu.h
94        DESTINATION "include")
95
96# Define ETHOSU macro
97target_compile_definitions(ethosu_core_driver PUBLIC ETHOSU)
98
99#
100# Print build status
101#
102
103message(STATUS "*******************************************************")
104message(STATUS "PROJECT_NAME                           : ${PROJECT_NAME}")
105message(STATUS "ETHOSU_TARGET_NPU_CONFIG               : ${ETHOSU_TARGET_NPU_CONFIG}")
106message(STATUS "CMAKE_SYSTEM_PROCESSOR                 : ${CMAKE_SYSTEM_PROCESSOR}")
107message(STATUS "CMSIS_PATH                             : ${CMSIS_PATH}")
108message(STATUS "ETHOSU_LOG_ENABLE                      : ${ETHOSU_LOG_ENABLE}")
109message(STATUS "ETHOSU_LOG_SEVERITY                    : ${ETHOSU_LOG_SEVERITY}")
110message(STATUS "ETHOSU_INFERENCE_TIMEOUT               : ${ETHOSU_INFERENCE_TIMEOUT_TEXT}")
111message(STATUS "*******************************************************")
112