|
| 1 | +# Copyright (c) The mldsa-native project authors |
| 2 | +# Copyright (c) The mlkem-native project authors |
| 3 | +# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 4 | + |
| 5 | +{ stdenvNoCC |
| 6 | +, fetchFromGitHub |
| 7 | +, writeText |
| 8 | +}: |
| 9 | +# Construct a minimal STM32CubeN6-based platform environment for building |
| 10 | +# nucleo-n657x0-q benchmarks. The package copies only the required Cube headers |
| 11 | +# and sources, then patches selected files where needed to get the intended |
| 12 | +# RAM-loaded benchmark behavior. |
| 13 | +stdenvNoCC.mkDerivation { |
| 14 | + pname = "mlkem-native-nucleo-n657x0-q"; |
| 15 | + version = "main"; |
| 16 | + |
| 17 | + src = fetchFromGitHub { |
| 18 | + owner = "STMicroelectronics"; |
| 19 | + repo = "STM32CubeN6"; |
| 20 | + rev = "1fc803683e03b0ce78e64523441d31acd0db2829"; |
| 21 | + hash = "sha256-7iD7R3A+0YAfMZqKndWBq+z5TUY37XDVxFy5HK8/5Aw="; |
| 22 | + fetchSubmodules = true; |
| 23 | + }; |
| 24 | + |
| 25 | + dontBuild = true; |
| 26 | + |
| 27 | + installPhase = '' |
| 28 | + set -eu |
| 29 | +
|
| 30 | + outp="$out/platform/nucleo-n657x0-q/src/platform" |
| 31 | + fsbl_tpl="Projects/NUCLEO-N657X0-Q/Templates/Template_FSBL_LRUN" |
| 32 | + hal="Drivers/STM32N6xx_HAL_Driver" |
| 33 | + cmsis_core="Drivers/CMSIS/Core/Include" |
| 34 | + cmsis_device="Drivers/CMSIS/Device/ST/STM32N6xx/Include" |
| 35 | +
|
| 36 | + install_file() { |
| 37 | + src_file="$1" |
| 38 | + dst_file="$outp/$2" |
| 39 | + mkdir -p "$(dirname "$dst_file")" |
| 40 | + cp "$src_file" "$dst_file" |
| 41 | + } |
| 42 | +
|
| 43 | + # CMSIS headers reached by stm32n657xx.h/core_cm55.h under the GCC build. |
| 44 | + for header in \ |
| 45 | + cmsis_compiler.h \ |
| 46 | + cmsis_gcc.h \ |
| 47 | + cmsis_version.h \ |
| 48 | + core_cm55.h \ |
| 49 | + m-profile/armv7m_cachel1.h \ |
| 50 | + m-profile/armv8m_mpu.h \ |
| 51 | + m-profile/armv8m_pmu.h \ |
| 52 | + m-profile/cmsis_gcc_m.h |
| 53 | + do |
| 54 | + install_file "$cmsis_core/$header" "Drivers/CMSIS/Core/Include/$header" |
| 55 | + done |
| 56 | +
|
| 57 | + for header in \ |
| 58 | + stm32n6xx.h \ |
| 59 | + stm32n657xx.h \ |
| 60 | + system_stm32n6xx.h |
| 61 | + do |
| 62 | + install_file "$cmsis_device/$header" "Drivers/CMSIS/Device/ST/STM32N6xx/Include/$header" |
| 63 | + done |
| 64 | +
|
| 65 | + # FSBL files used by platform.mk. |
| 66 | + install_file \ |
| 67 | + "$fsbl_tpl/STM32CubeIDE/Boot/Startup/startup_stm32n657xx_fsbl.s" \ |
| 68 | + "gcc/startup_stm32n657xx.S" |
| 69 | +
|
| 70 | + # Use the linker-provided stack top instead of Cube's fixed SRAM stack. |
| 71 | + sed -i.bak -E 's@[.]word[[:space:]]+0x34110000@.word _estack@' \ |
| 72 | + "$outp/gcc/startup_stm32n657xx.S" |
| 73 | +
|
| 74 | + # Mask interrupts as soon as Reset_Handler starts to avoid stale debug/board |
| 75 | + # state firing before this RAM-loaded test image initializes C runtime state. |
| 76 | + sed -i.bak -E '/^Reset_Handler:/a\ cpsid i' \ |
| 77 | + "$outp/gcc/startup_stm32n657xx.S" |
| 78 | + rm -f "$outp/gcc/startup_stm32n657xx.S.bak" |
| 79 | +
|
| 80 | + install_file "$fsbl_tpl/FSBL/Src/system_stm32n6xx_fsbl.c" "system_stm32n6xx.c" |
| 81 | + install_file "$fsbl_tpl/FSBL/Src/stm32n6xx_it.c" "stm32n6xx_it.c" |
| 82 | + install_file "$fsbl_tpl/FSBL/Inc/stm32n6xx_it.h" "Inc/stm32n6xx_it.h" |
| 83 | + install_file "$fsbl_tpl/FSBL/Inc/main.h" "Inc/main.h" |
| 84 | + install_file "$fsbl_tpl/FSBL/Inc/stm32n6xx_hal_conf.h" "Inc/stm32n6xx_hal_conf.h" |
| 85 | +
|
| 86 | + conf="$outp/Inc/stm32n6xx_hal_conf.h" |
| 87 | +
|
| 88 | + # Disable BSEC because the platform test build does not link the BSEC HAL. |
| 89 | + sed -i.bak -E 's@^[[:space:]]*#[[:space:]]*define[[:space:]]+HAL_BSEC_MODULE_ENABLED@/* #define HAL_BSEC_MODULE_ENABLED */@' "$conf" |
| 90 | +
|
| 91 | + # Disable XSPI because only the extracted clock setup is used, not Cube's |
| 92 | + # external memory setup or XSPI HAL driver. |
| 93 | + sed -i.bak -E 's@^[[:space:]]*#[[:space:]]*define[[:space:]]+HAL_XSPI_MODULE_ENABLED@/* #define HAL_XSPI_MODULE_ENABLED */@' "$conf" |
| 94 | + rm -f "$conf.bak" |
| 95 | +
|
| 96 | + # HAL files named directly by platform.mk, plus headers enabled by the |
| 97 | + # pruned FSBL HAL config and their direct dependencies. |
| 98 | + for header in \ |
| 99 | + Legacy/stm32_hal_legacy.h \ |
| 100 | + stm32n6xx_hal.h \ |
| 101 | + stm32n6xx_hal_cortex.h \ |
| 102 | + stm32n6xx_hal_def.h \ |
| 103 | + stm32n6xx_hal_dma.h \ |
| 104 | + stm32n6xx_hal_dma_ex.h \ |
| 105 | + stm32n6xx_hal_exti.h \ |
| 106 | + stm32n6xx_hal_gpio.h \ |
| 107 | + stm32n6xx_hal_gpio_ex.h \ |
| 108 | + stm32n6xx_hal_pwr.h \ |
| 109 | + stm32n6xx_hal_pwr_ex.h \ |
| 110 | + stm32n6xx_hal_rcc.h \ |
| 111 | + stm32n6xx_hal_rcc_ex.h \ |
| 112 | + stm32n6xx_ll_bus.h \ |
| 113 | + stm32n6xx_ll_rcc.h |
| 114 | + do |
| 115 | + install_file "$hal/Inc/$header" "Drivers/STM32N6xx_HAL_Driver/Inc/$header" |
| 116 | + done |
| 117 | +
|
| 118 | + for source in \ |
| 119 | + stm32n6xx_hal.c \ |
| 120 | + stm32n6xx_hal_cortex.c \ |
| 121 | + stm32n6xx_hal_pwr.c \ |
| 122 | + stm32n6xx_hal_pwr_ex.c \ |
| 123 | + stm32n6xx_hal_rcc.c \ |
| 124 | + stm32n6xx_hal_rcc_ex.c |
| 125 | + do |
| 126 | + install_file "$hal/Src/$source" "Drivers/STM32N6xx_HAL_Driver/Src/$source" |
| 127 | + done |
| 128 | +
|
| 129 | + fsbl_main_c="$fsbl_tpl/FSBL/Src/main.c" |
| 130 | + clock_out="$outp/clock_config.c" |
| 131 | + tmpclk="$TMPDIR/clock_config.$$.$RANDOM.c" |
| 132 | + : > "$tmpclk" |
| 133 | +
|
| 134 | + # Preserve the Cube file header in the generated standalone clock source. |
| 135 | + awk ' |
| 136 | + /\/\* USER CODE BEGIN Header \*\// { print; inhdr=1; next } |
| 137 | + inhdr { print } |
| 138 | + /\/\* USER CODE END Header \*\// { inhdr=0 } |
| 139 | + ' "$fsbl_main_c" >> "$tmpclk" |
| 140 | + printf "\n#include \"main.h\"\n\n" >> "$tmpclk" |
| 141 | +
|
| 142 | + # Keep Cube's clock-tree explanatory comment next to SystemClock_Config. |
| 143 | + awk ' |
| 144 | + /\/\* USER CODE BEGIN CLK 1 \*\// { print; inclk=1; next } |
| 145 | + inclk { print } |
| 146 | + /\/\* USER CODE END CLK 1 \*\// { inclk=0 } |
| 147 | + ' "$fsbl_main_c" >> "$tmpclk" |
| 148 | + printf "\n" >> "$tmpclk" |
| 149 | +
|
| 150 | + # Extract exactly the SystemClock_Config function body from the FSBL main.c |
| 151 | + # into the generated clock_config.c, tracking braces so that file does not |
| 152 | + # pull in unused FSBL code. |
| 153 | + awk ' |
| 154 | + BEGIN { copy=0; lvl=0; sig=0 } |
| 155 | + /^[ \t]*void[ \t]+SystemClock_Config[ \t]*\([ \t]*void[ \t]*\)[ \t]*\{/ { |
| 156 | + print; copy=1; lvl=1; next |
| 157 | + } |
| 158 | + /^[ \t]*void[ \t]+SystemClock_Config[ \t]*\([ \t]*void[ \t]*\)[ \t]*$/ { |
| 159 | + print; copy=1; sig=1; next |
| 160 | + } |
| 161 | + copy { |
| 162 | + if (sig && $0 !~ /\{/) { print; next } |
| 163 | + nopen=gsub(/{/,"{") |
| 164 | + nclose=gsub(/}/,"}") |
| 165 | + lvl += nopen - nclose |
| 166 | + print |
| 167 | + sig=0 |
| 168 | + if (lvl<=0) exit |
| 169 | + } |
| 170 | + ' "$fsbl_main_c" >> "$tmpclk" |
| 171 | + grep -q "SystemClock_Config" "$tmpclk" |
| 172 | + mv "$tmpclk" "$clock_out" |
| 173 | +
|
| 174 | + # The standalone clock_config.c calls SystemClock_Config, so make sure the |
| 175 | + # copied FSBL main.h exposes the declaration expected by local platform code. |
| 176 | + if ! grep -q "void[[:space:]]\+SystemClock_Config[[:space:]]*([[:space:]]*void[[:space:]]*)" "$outp/Inc/main.h"; then |
| 177 | + printf "\nvoid SystemClock_Config(void);\n" >> "$outp/Inc/main.h" |
| 178 | + fi |
| 179 | +
|
| 180 | + # SystemClock_Config calls Error_Handler on HAL failures; the implementation |
| 181 | + # is provided by the platform test sources, but main.h must declare it. |
| 182 | + if ! grep -q "void[[:space:]]\+Error_Handler[[:space:]]*([[:space:]]*void[[:space:]]*)" "$outp/Inc/main.h"; then |
| 183 | + printf "void Error_Handler(void);\n" >> "$outp/Inc/main.h" |
| 184 | + fi |
| 185 | + ''; |
| 186 | + |
| 187 | + setupHook = writeText "setup-hook.sh" '' |
| 188 | + export NUCLEO_N657X0_Q_PATH="$1/platform/nucleo-n657x0-q/src/platform/" |
| 189 | + ''; |
| 190 | + |
| 191 | + meta = { |
| 192 | + description = "Platform files for STM32 NUCLEO-N657X0-Q RAM-only OpenOCD tests"; |
| 193 | + homepage = "https://github.com/STMicroelectronics/STM32CubeN6"; |
| 194 | + }; |
| 195 | +} |
0 commit comments