Skip to content

Commit

Permalink
samples: code_relocation: An example for code relocation feature.
Browse files Browse the repository at this point in the history
This sample provides an example for using the code relocation
feature. This example will place text,data,bss from 3 files to
various parts in the SRAM. For this a custom linker file is used
which is derived from include/arch/arm/cortex_m/scripts/linker.ld.

Signed-off-by: Varun Sharma <[email protected]>
Signed-off-by: Adithya Baglody <[email protected]>
  • Loading branch information
AdithyaBaglody authored and nashif committed Dec 7, 2018
1 parent aa2890e commit 21fa433
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 0 deletions.
18 changes: 18 additions & 0 deletions samples/application_development/code_relocation/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.8.2)

include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(test_relocation)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

# Code relocation feature
zephyr_code_relocate(src/test_file1.c SRAM2)

zephyr_code_relocate(src/test_file2.c SRAM)

zephyr_code_relocate(src/test_file3.c SRAM2_TEXT)
zephyr_code_relocate(src/test_file3.c SRAM_DATA)
zephyr_code_relocate(src/test_file3.c SRAM2_BSS)

zephyr_code_relocate(../../../kernel/sem.c SRAM)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SECTION_DATA_PROLOGUE(_CUSTOM_SECTION_NAME2,,)
{
KEEP(*(".custom_section.*"));
} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2013-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @file
* @brief Linker command/script file
*
* Linker script for the Cortex-M platforms.
*/

#define _LINKER
#define _ASMLANGUAGE

#include <autoconf.h>
#include <linker/sections.h>
#include <generated_dts_board.h>

#include <linker/linker-defs.h>
#include <linker/linker-tool.h>

/** enable CONFIG_SRAM2 or any other partition in soc Kconfig,
* this is just an example to show relocation of code/data/bss script
*/
#if defined CONFIG_ARM
#define CONFIG_SRAM2 1
#define _SRAM2_DATA_SECTION_NAME .sram2_data
#define _SRAM2_BSS_SECTION_NAME .sram2_bss
#define _SRAM2_TEXT_SECTION_NAME .sram2_text
#endif

#define RAM_SIZE2 (CONFIG_SRAM_SIZE * 512)
MEMORY
{
#ifdef CONFIG_SRAM2
SRAM2 (wx) : ORIGIN = (CONFIG_SRAM_BASE_ADDRESS + RAM_SIZE2), LENGTH = RAM_SIZE2
#endif
}

#include <arch/arm/cortex_m/scripts/linker.ld>
4 changes: 4 additions & 0 deletions samples/application_development/code_relocation/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONFIG_CODE_DATA_RELOCATION=y
CONFIG_HAVE_CUSTOM_LINKER_SCRIPT=y
CONFIG_CUSTOM_LINKER_SCRIPT="linker_arm_sram2.ld"
CONFIG_CUSTOM_SECTIONS_LD=y
12 changes: 12 additions & 0 deletions samples/application_development/code_relocation/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sample:
description: Code data relocation Sample
name: code relocation
tests:
test:
harness: console
harness_config:
type: one_line
regex:
- "Hello World! (.*)"
platform_whitelist: qemu_cortex_m3 mps2_an385 sam_e70_xplained
tags: linker
50 changes: 50 additions & 0 deletions samples/application_development/code_relocation/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2018 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr.h>
#include <kernel.h>
#include <misc/printk.h>


/* This function will allow execute from sram region.
* This is needed only for this sample because by default all soc will
* disable the execute from SRAM.
* An application that requires that the code be executed from SRAM will
* have to configure the region appropriately in arm_mpu_regions.c.
*/

#ifdef CONFIG_ARM_MPU
#include <arch/arm/cortex_m/cmsis.h>
void disable_mpu_rasr_xn(void)
{
u32_t index;
/* Kept the max index as 8(irrespective of soc) because the sram
* would most likely be set at index 2.
*/
for (index = 0; index < 8; index++) {
MPU->RNR = index;
if (MPU->RASR & MPU_RASR_XN_Msk) {
MPU->RASR ^= MPU_RASR_XN_Msk;
}
}

}
#endif /* CONFIG_ARM_MPU */


extern void function_in_sram2(void);
extern void function_in_split_multiple(void);

void main(void)
{
#ifdef CONFIG_ARM_MPU
disable_mpu_rasr_xn();
#endif /* CONFIG_ARM_MPU */

printk("Address of main function %p\n\n", &main);
function_in_sram2();
function_in_split_multiple();
}
44 changes: 44 additions & 0 deletions samples/application_development/code_relocation/src/test_file1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr.h>
#include <misc/printk.h>

u32_t var_sram2_data = 10U;
u32_t var_sram2_bss;
K_SEM_DEFINE(test, 0, 1);
const u32_t var_sram2_rodata = 100U;

__in_section(custom_section, static, var) u32_t var_custom_data = 1U;

extern void function_in_sram(s32_t value);
void function_in_custom_section(void);
void function_in_sram2(void)
{
/* Print values from sram2 */
printk("Address of function_in_sram2 %p\n", &function_in_sram2);
printk("Address of var_sram2_data %p\n", &var_sram2_data);
printk("Address of k_sem_give %p\n", &k_sem_give);
printk("Address of var_sram2_rodata %p\n", &var_sram2_rodata);
printk("Address of var_sram2_bss %p\n\n", &var_sram2_bss);

/* Print values from sram */
function_in_sram(var_sram2_data);

/* Print values which were placed using attributes */
printk("Address of custom_section, func placed using attributes %p\n",
&function_in_custom_section);
printk("Address of custom_section data placed using attributes %p\n\n",
&var_custom_data);

k_sem_give(&test);
}

__in_section(custom_section, static, fun) void function_in_custom_section(void)
{
return;

}
14 changes: 14 additions & 0 deletions samples/application_development/code_relocation/src/test_file2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr.h>
#include <misc/printk.h>

void function_in_sram(s32_t value)
{
printk("Address of function_in_sram %p\n", &function_in_sram);
printk("Hello World! %s\n", CONFIG_BOARD);
}
19 changes: 19 additions & 0 deletions samples/application_development/code_relocation/src/test_file3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr.h>
#include <misc/printk.h>

u32_t var_file3_sram_data = 10U;
u32_t var_file3_sram2_bss;

void function_in_split_multiple(void)
{
printk("Address of function_in_split_multiple %p\n",
&function_in_split_multiple);
printk("Address of var_file3_sram_data %p\n", &var_file3_sram_data);
printk("Address of var_file3_sram2_bss %p\n\n", &var_file3_sram2_bss);
}

0 comments on commit 21fa433

Please sign in to comment.