From b0d30a5da11f829d3d29d7d8e7734ae3ff6f8423 Mon Sep 17 00:00:00 2001 From: Marius Brehler Date: Mon, 6 Nov 2023 13:17:36 +0100 Subject: [PATCH 1/7] Define target `nrfx_nrf5340` --- build_tools/third_party/nrfx/CMakeLists.txt | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/build_tools/third_party/nrfx/CMakeLists.txt b/build_tools/third_party/nrfx/CMakeLists.txt index 4db7715..73b7dd0 100644 --- a/build_tools/third_party/nrfx/CMakeLists.txt +++ b/build_tools/third_party/nrfx/CMakeLists.txt @@ -35,6 +35,35 @@ target_compile_definitions(nrfx_nrf52840 # Allow ld to find generic linker script target_link_directories(nrfx_nrf52840 INTERFACE "${nrfx_custom_DIR}") +#------------------------------------------------------------------------------- +# nRF5340 +#------------------------------------------------------------------------------- + +add_library(nrfx_nrf5340 STATIC "") + +target_sources(nrfx_nrf5340 + PRIVATE + ${nrfx_DIR}/mdk/system_nrf5340_application.c + ${nrfx_custom_DIR}/gcc_startup_nrf5340_application.S +) + +target_include_directories(nrfx_nrf5340 + PUBLIC + ${CMSIS_5_SOURCE_DIR}/CMSIS/Core/Include/ + PRIVATE + ${nrfx_DIR}/mdk/ +) + +target_compile_definitions(nrfx_nrf5340 + PUBLIC + NRF5340_XXAA + NRF5340_XXAA_APPLICATION + NRF_APPLICATION +) + +# Allow ld to find generic linker script +target_link_directories(nrfx_nrf5340 INTERFACE "${nrfx_custom_DIR}") + #------------------------------------------------------------------------------- # nRF UART #------------------------------------------------------------------------------- From d82c60d3ceead6a1150ecf9ac61798e823a36fff Mon Sep 17 00:00:00 2001 From: Marius Brehler Date: Mon, 6 Nov 2023 13:17:58 +0100 Subject: [PATCH 2/7] Define target `nrfx_uarte` Co-authored-by: Lucas Camphausen --- build_tools/third_party/nrfx/CMakeLists.txt | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/build_tools/third_party/nrfx/CMakeLists.txt b/build_tools/third_party/nrfx/CMakeLists.txt index 73b7dd0..f623947 100644 --- a/build_tools/third_party/nrfx/CMakeLists.txt +++ b/build_tools/third_party/nrfx/CMakeLists.txt @@ -98,3 +98,38 @@ if(ARM_TARGET STREQUAL "NRF52840") nrfx_nrf52840 ) endif() + +#------------------------------------------------------------------------------- +# nRF UARTE +#------------------------------------------------------------------------------- + +add_library(nrfx_uarte STATIC "") + +target_sources(nrfx_uarte + PRIVATE + ${nrfx_DIR}/drivers/src/nrfx_uarte.c +) + +target_include_directories(nrfx_uarte + PUBLIC + ${nrfx_DIR} + ${nrfx_DIR}/drivers/include/ + ${nrfx_DIR}/hal/ + ${nrfx_DIR}/templates/ + ${nrfx_DIR}/mdk/ +) + +if(USE_UART0) + target_compile_definitions(nrfx_uarte + PUBLIC + NRFX_UARTE_ENABLED + NRFX_UARTE0_ENABLED + ) +endif() + +if(ARM_TARGET STREQUAL "NRF5340") + target_link_libraries(nrfx_uarte + PRIVATE + nrfx_nrf5340 +) +endif() From 18a099e30d458bd62deaacb6a035c65a2905ed45 Mon Sep 17 00:00:00 2001 From: Marius Brehler Date: Mon, 6 Nov 2023 14:53:42 +0100 Subject: [PATCH 3/7] Add initial utils for nRF5340 --- utils/CMakeLists.txt | 7 +++++++ utils/nrf5340_cmsis.c | 45 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 utils/nrf5340_cmsis.c diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index bc7588c..a4b50f5 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -8,6 +8,9 @@ if(BUILD_WITH_CMSIS) if(ARM_TARGET STREQUAL "NRF52840") set(UTILS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/nrf52840_cmsis.c) set(WRITE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/write.c) + elseif(ARM_TARGET STREQUAL "NRF5340") + set(UTILS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/nrf5340_cmsis.c) + set(WRITE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/write.c) elseif(ARM_TARGET MATCHES "^STM32F4") set(UTILS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/stm32f4_cmsis.c) elseif(ARM_TARGET MATCHES "^STM32F7") @@ -45,6 +48,10 @@ if(ARM_TARGET STREQUAL "NRF52840") target_link_libraries(utils PRIVATE nrfx_uart) endif() +if(ARM_TARGET STREQUAL "NRF5340") + target_link_libraries(utils PRIVATE nrfx_uarte) +endif() + if(USE_UART0) target_compile_definitions(utils PRIVATE USE_UART0) endif() diff --git a/utils/nrf5340_cmsis.c b/utils/nrf5340_cmsis.c new file mode 100644 index 0000000..ed0134c --- /dev/null +++ b/utils/nrf5340_cmsis.c @@ -0,0 +1,45 @@ +// Copyright 2023 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. +// +// Licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include +#include + +#include "nrf_gpio.h" +#include "nrfx_uarte.h" + +// UART instance +#ifdef USE_UART0 +nrfx_uarte_t uarte = NRFX_UARTE_INSTANCE(0); +#endif + +void clock_setup(void) {} + +// TODO: Fix me! +void usart_setup(void) { +#ifdef USE_UART0 + // Configuration for UARTE + const nrfx_uarte_config_t uarte_config = { + .pseltxd = NRF_GPIO_PIN_MAP(0, 6), + .pselrxd = NRF_GPIO_PIN_MAP(0, 8), + .pselcts = NRF_UARTE_PSEL_DISCONNECTED, + .pselrts = NRF_UARTE_PSEL_DISCONNECTED, + .baudrate = UARTE_BAUDRATE_BAUDRATE_Baud115200, + }; + + // Enable UART + nrfx_uarte_init(&uarte, &uarte_config, NULL); +#endif +} + +void gpio_setup(void) {} + +void send_blocking(uint16_t c) { +#ifdef USE_UART0 + nrfx_uarte_tx(&uarte, (uint8_t*)&c, 1); +#endif +} + +bool wait_until(uint64_t nanos) { return true; } From b2715fd681a5e2513f4738ae56efe9dab44adb8f Mon Sep 17 00:00:00 2001 From: Marius Brehler Date: Mon, 6 Nov 2023 13:24:45 +0100 Subject: [PATCH 4/7] Adapt configuration to build for nRF5340 --- CMakeLists.txt | 8 ++++---- samples/CMakeLists.txt | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8650545..a801c69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,7 +34,7 @@ endif() # Define ARM_TARGET property set(ARM_TARGET STM32F407 CACHE STRING "Target for which the samples are build") -set(ARM_TARGET_VALUES "NRF52840;STM32F407;STM32F411XE;STM32F446;STM32F4XX;STM32F746;STM32L4R5;STM32L476;CORSTONE-300" CACHE INTERNAL "List of possible targets") +set(ARM_TARGET_VALUES "NRF52840;NRF5340;STM32F407;STM32F411XE;STM32F446;STM32F4XX;STM32F746;STM32L4R5;STM32L476;CORSTONE-300" CACHE INTERNAL "List of possible targets") set_property(CACHE ARM_TARGET PROPERTY STRINGS ${ARM_TARGET_VALUES}) string(TOUPPER "${ARM_TARGET}" ARM_TARGET) @@ -61,9 +61,9 @@ endif() if(ARM_TARGET STREQUAL "STM32L4R5" AND USE_UART1) message(FATAL_ERROR "UART1 is not supported for STM32L4R5") endif() -# We don't support UART1 and UART2 on NRF52840 -if(ARM_TARGET STREQUAL "NRF52840" AND (USE_UART1 OR USE_UART2)) - message(FATAL_ERROR "UART1 and UART2 are not supported for NF528400") +# We don't support UART1 and UART2 on any NRF board +if(ARM_TARGET MATCHES "^NRF" AND (USE_UART1 OR USE_UART2)) + message(FATAL_ERROR "UART1 and UART2 are not supported for NRF boards") endif() # Define CLOCK_SOURCE property diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 8113674..58898a9 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -7,6 +7,8 @@ if(BUILD_WITH_CMSIS) if(ARM_TARGET STREQUAL "NRF52840") set(CONDITIONAL_DEP nrfx_nrf52840) + elseif(ARM_TARGET STREQUAL "NRF5340") + set(CONDITIONAL_DEP nrfx_nrf5340) elseif(ARM_TARGET STREQUAL "STM32F407" OR ARM_TARGET STREQUAL "STM32F4XX") set(CONDITIONAL_DEP cmsis_device_f407xx) elseif(ARM_TARGET STREQUAL "STM32F411XE") From 0636c4e03adb7e79ffd6985885e1c1fd0dd57730 Mon Sep 17 00:00:00 2001 From: Lucas Camphausen Date: Mon, 6 Nov 2023 15:57:10 +0100 Subject: [PATCH 5/7] Fix UARTE implementation for nRF5340 Co-authored-by: Marius Brehler --- build_tools/third_party/nrfx/CMakeLists.txt | 3 +++ third_party/nrfx-custom/nrfx.h | 5 ++++ third_party/nrfx-custom/nrfx_glue_nrf5340.h | 27 +++++++++++++++++++++ utils/nrf5340_cmsis.c | 11 ++++----- 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 third_party/nrfx-custom/nrfx_glue_nrf5340.h diff --git a/build_tools/third_party/nrfx/CMakeLists.txt b/build_tools/third_party/nrfx/CMakeLists.txt index f623947..d66881b 100644 --- a/build_tools/third_party/nrfx/CMakeLists.txt +++ b/build_tools/third_party/nrfx/CMakeLists.txt @@ -108,15 +108,18 @@ add_library(nrfx_uarte STATIC "") target_sources(nrfx_uarte PRIVATE ${nrfx_DIR}/drivers/src/nrfx_uarte.c + ${nrfx_DIR}/soc/nrfx_atomic.c ) target_include_directories(nrfx_uarte PUBLIC + ${nrfx_custom_DIR} ${nrfx_DIR} ${nrfx_DIR}/drivers/include/ ${nrfx_DIR}/hal/ ${nrfx_DIR}/templates/ ${nrfx_DIR}/mdk/ + ${nrfx_DIR}/soc/ ) if(USE_UART0) diff --git a/third_party/nrfx-custom/nrfx.h b/third_party/nrfx-custom/nrfx.h index 87ee696..ff4f3f9 100644 --- a/third_party/nrfx-custom/nrfx.h +++ b/third_party/nrfx-custom/nrfx.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2017 - 2023, Nordic Semiconductor ASA + * Copyright 2023 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. * All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause @@ -37,6 +38,10 @@ #include #include #include +#undef NRFX_ATOMIC_FETCH_OR +#undef NRFX_ATOMIC_FETCH_AND +#undef nrfx_atomic_t +#include #include #include #include diff --git a/third_party/nrfx-custom/nrfx_glue_nrf5340.h b/third_party/nrfx-custom/nrfx_glue_nrf5340.h new file mode 100644 index 0000000..670e93f --- /dev/null +++ b/third_party/nrfx-custom/nrfx_glue_nrf5340.h @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: 2023 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. +// SPDX-License-Identifier: BSD-3-Clause + +#ifndef NRFX_GLUE_NRFX5340_H__ +#define NRFX_GLUE_NRFX5340_H__ + +// File providing macros as requested in `nrfx_glue.h`. + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#define nrfx_atomic_t nrfx_atomic_u32_t + +#define NRFX_ATOMIC_FETCH_OR(p_data, value) \ + nrfx_atomic_u32_fetch_or(p_data, value) + +#define NRFX_ATOMIC_FETCH_AND(p_data, value) \ + nrfx_atomic_u32_fetch_and(p_data, value) + +#ifdef __cplusplus +} +#endif + +#endif // NRFX_GLUE_NRFX5340_H__ diff --git a/utils/nrf5340_cmsis.c b/utils/nrf5340_cmsis.c index ed0134c..0f31b40 100644 --- a/utils/nrf5340_cmsis.c +++ b/utils/nrf5340_cmsis.c @@ -17,15 +17,14 @@ nrfx_uarte_t uarte = NRFX_UARTE_INSTANCE(0); void clock_setup(void) {} -// TODO: Fix me! void usart_setup(void) { #ifdef USE_UART0 // Configuration for UARTE const nrfx_uarte_config_t uarte_config = { - .pseltxd = NRF_GPIO_PIN_MAP(0, 6), - .pselrxd = NRF_GPIO_PIN_MAP(0, 8), - .pselcts = NRF_UARTE_PSEL_DISCONNECTED, - .pselrts = NRF_UARTE_PSEL_DISCONNECTED, + .txd_pin = NRF_GPIO_PIN_MAP(1, 1), + .rxd_pin = NRF_GPIO_PIN_MAP(1, 0), + .rts_pin = NRF_UARTE_PSEL_DISCONNECTED, + .cts_pin = NRF_UARTE_PSEL_DISCONNECTED, .baudrate = UARTE_BAUDRATE_BAUDRATE_Baud115200, }; @@ -38,7 +37,7 @@ void gpio_setup(void) {} void send_blocking(uint16_t c) { #ifdef USE_UART0 - nrfx_uarte_tx(&uarte, (uint8_t*)&c, 1); + nrfx_uarte_tx(&uarte, (uint8_t*)&c, 1, NRFX_UARTE_TX_BLOCKING); #endif } From 471089478363015216efc65f5477f05d7e1e9e0b Mon Sep 17 00:00:00 2001 From: Marius Brehler Date: Mon, 6 Nov 2023 14:50:32 +0100 Subject: [PATCH 6/7] Adapt helper script for nRF5340 --- build_tools/configure_build.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/build_tools/configure_build.sh b/build_tools/configure_build.sh index 9ab5858..3b04035 100755 --- a/build_tools/configure_build.sh +++ b/build_tools/configure_build.sh @@ -59,6 +59,14 @@ case $2 in fi ;; + nrf5340) + echo "Building for NRF5340" + export ARM_CPU="cortex-m33" + if [ -z ${PATH_TO_LINKER_SCRIPT+x} ]; then + export PATH_TO_LINKER_SCRIPT="${PATH_TO_REPO}/third_party/nrfx-custom/nrf5340_xxaa_application.ld" + fi + ;; + stm32f407) echo "Building for STM32F407" export ARM_CPU="cortex-m4" @@ -128,6 +136,7 @@ case $2 in *) echo "Unknown device. Supported devices are" echo " 'nrf52840'" + echo " 'nrf5340'" echo " 'stm32f407'" echo " 'stm32f411xe'" echo " 'stm32f446'" @@ -146,6 +155,10 @@ case $2 in UART=0 ;; + nrf5340) + UART=0 + ;; + *) UART=2 ;; From 60864601d3532cf7adfe8aa64b7b9dfabff08137 Mon Sep 17 00:00:00 2001 From: Marius Brehler Date: Wed, 8 Nov 2023 09:34:04 +0100 Subject: [PATCH 7/7] Update README --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d9914b4..c5e699f 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,9 @@ This projects demonstrates how to build [IREE](https://github.com/openxla/iree) ## Target Support -The samples can be build for several [STM32 32-bit Arm Cortex MCUs](https://www.st.com/en/microcontrollers-microprocessors/stm32-32-bit-arm-cortex-mcus.html), for the [Nordic nRF52840 SoC](https://www.nordicsemi.com/products/nrf52840) +The samples can be build for several [STM32 32-bit Arm Cortex MCUs](https://www.st.com/en/microcontrollers-microprocessors/stm32-32-bit-arm-cortex-mcus.html), +for the [Nordic nRF52840 SoC](https://www.nordicsemi.com/products/nrf52840), +for the application core of the [Nordic nRF5340 SoC](https://www.nordicsemi.com/products/nrf5340) and for the Arm [Corstone-300](https://developer.arm.com/Processors/Corstone-300). ## Getting Started