|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Silicon Laboratories Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#if !defined(CONFIG_CPU_CORTEX_M) |
| 8 | +#error project can only run on Cortex-M |
| 9 | +#endif |
| 10 | + |
| 11 | +#include <zephyr/kernel.h> |
| 12 | +#include <zephyr/sys/printk.h> |
| 13 | +#include <zephyr/ztest.h> |
| 14 | + |
| 15 | +#ifdef SCB_VTOR_TBLBASE_Msk |
| 16 | +#define VTOR_MASK (SCB_VTOR_TBLBASE_Msk | SCB_VTOR_TBLOFF_Msk) |
| 17 | +#else |
| 18 | +#define VTOR_MASK SCB_VTOR_TBLOFF_Msk |
| 19 | +#endif |
| 20 | + |
| 21 | +/* This function will allow execute from sram region. |
| 22 | + * This is needed only for this sample because by default all soc will |
| 23 | + * disable the execute from SRAM. |
| 24 | + * An application that requires that the code be executed from SRAM will |
| 25 | + * have to configure the region appropriately in arm_mpu_regions.c. |
| 26 | + */ |
| 27 | + |
| 28 | +#if (defined(CONFIG_ARM_MPU) && !defined(CONFIG_CPU_HAS_NXP_SYSMPU)) |
| 29 | +#include <cmsis_core.h> |
| 30 | +void disable_mpu_rasr_xn(void) |
| 31 | +{ |
| 32 | + uint32_t index; |
| 33 | + /* Kept the max index as 8(irrespective of soc) because the sram |
| 34 | + * would most likely be set at index 2. |
| 35 | + */ |
| 36 | + for (index = 0U; index < 8; index++) { |
| 37 | + MPU->RNR = index; |
| 38 | +#if defined(CONFIG_ARMV8_M_BASELINE) || defined(CONFIG_ARMV8_M_MAINLINE) |
| 39 | + if (MPU->RBAR & MPU_RBAR_XN_Msk) { |
| 40 | + MPU->RBAR ^= MPU_RBAR_XN_Msk; |
| 41 | + } |
| 42 | +#else |
| 43 | + if (MPU->RASR & MPU_RASR_XN_Msk) { |
| 44 | + MPU->RASR ^= MPU_RASR_XN_Msk; |
| 45 | + } |
| 46 | +#endif /* CONFIG_ARMV8_M_BASELINE || CONFIG_ARMV8_M_MAINLINE */ |
| 47 | + } |
| 48 | +} |
| 49 | +#endif /* CONFIG_ARM_MPU */ |
| 50 | + |
| 51 | +ZTEST(vector_table_relocation, test_vector_table_in_ram) |
| 52 | +{ |
| 53 | + /* Check that VTOR register effectively point to a RAM based location */ |
| 54 | + volatile uint32_t vtor_address = SCB->VTOR & VTOR_MASK; |
| 55 | + |
| 56 | + printf("VTOR address: 0x%x\n", vtor_address); |
| 57 | + zassert_true(vtor_address >= CONFIG_SRAM_BASE_ADDRESS && |
| 58 | + vtor_address <= CONFIG_SRAM_BASE_ADDRESS + CONFIG_SRAM_SIZE, |
| 59 | + "Vector table is not in RAM! Address: 0x%x", vtor_address); |
| 60 | +} |
| 61 | + |
| 62 | +void *relocate_code_setup(void) |
| 63 | +{ |
| 64 | +#if (defined(CONFIG_ARM_MPU) && !defined(CONFIG_CPU_HAS_NXP_SYSMPU)) |
| 65 | + disable_mpu_rasr_xn(); |
| 66 | +#endif /* CONFIG_ARM_MPU */ |
| 67 | + return NULL; |
| 68 | +} |
| 69 | + |
| 70 | +ZTEST_SUITE(vector_table_relocation, NULL, relocate_code_setup, NULL, NULL, NULL); |
0 commit comments