Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

Memory Mapping for

based Software Development

Madhusudhanan Ravi
www.embeddeduncharted.com

Madhusudhanan Ravi www.embeddeduncharted.com


Contents

▶ Introduction

▶ File Structure & Compiler Directives

▶ Section Types & Keywords

▶ Implementation in Files (Module & MemMap)

▶ HW Address Mapping

▶ Example

▶ References

2 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar MemMap
INTRODUCTION

3 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar Memory Mapping - Introduction
● The software source files in general contains various types of data and code. They shall be
classified based on different criteria
Size
(Alignement)

Accessibility
Class

Functions
Data

Performance
Init Policy

Variable / Usage
Constant

Usage

4 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar Memory Mapping - Introduction
● The Embedded software is usually implemented in such a way that the source file has mixed
alignment of data/code and is not structured based on the type of data.

● Mapping such misaligned data to hardware memory has some disadvantages.

● For many ECUs and microcontroller platforms it is also of utmost necessity to be able to map
code, variables and constants module wise to specific memory sections.

Misaligned Data
CODE / DATA HW MEMORY

5 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar Memory Mapping - Introduction
● Autosar Memory Mapping (Memmap) specifies mechanisms for the mapping of code and data to
specific hardware memory sections via memory mapping files.

● Autosar MemMap provides Data Alignment by assigning the source file data/code to various
memory sections and then mapping them to hardware memory.

● Memory mapping renders module implementation free from compiler and microcontroller
specific properties.

Aligned Data
MEMMAP
CODE / DATA HW MEMORY
SECTIONS

6 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar MemMap - Need for Memmap

Avoidance of waste of RAM

● If different variables (8, 16 and 32 bit) are used within different modules on a 32 bit platform, the linker
will leave gaps in RAM when allocating the variables in the RAM.

● This wastage of memory can be circumvented if the variables are mapped to specific memory sections
depending on their size. This minimizes unused space in RAM.

Four word-sized memory cells in a 32-bit MCU.

Memory with 32-bit variable declared

Memory with 8,16,32-bit variable declared

Memory with Padding - 1 Byte Wastage.

7 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar MemMap - Need for Memmap
Usage of specific ROM properties

In large ECUs with External Flash memory,

● Functions with higher performance requirements and mostly used shall be stored in internal memory.

● Functions with lower performance requirements and rarely used shall be stored in external memory.

8 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar MemMap - Need for Memmap

Support of Memory Protection

The usage of hardware memory protection requires a separation of the modules variables into different memory
areas.

● Internal variables are mapped into protected memory.

● Buffers for data exchange are mapped into unprotected memory.

9 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar MemMap - Need for Memmap

Bootloader & Application using Same Code

If a module shall be used both in bootloader and application, it is necessary to allow the mapping of code and
data to different memory sections.

10 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar MemMap
FILE STRUCTURE & COMPILER DIRECTIVE

11 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar Memmap - File Structure

● Autosar memory mapping is applicable for both the basic software and software components.

● The Memory Mapping shall provide one BSW memory mapping header file ‘MemMap.h’ and one
SWC memory mapping header file ‘<SWC>_MemMap.h’

● Memmap.h header file shall be included by the corresponding module / software components.

Memmap.h BSW module


<<includes>>

SWC_
SWC
Memmap.h <<includes>>

12 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar Memmap - Compiler Directive

● The following #pragma shall be used in the code to assign its following content to the mentioned
memory section.

#pragma section type "string"

● Ex: #pragma section ".text" ax - This directive assigns the code following it to the “.text” section.
Here the BSW_Init() function is assigned to the memory section .text

#pragma section ".text" ax


void BSW_Init (void)
{


}

Note: The #pragma command syntax specified here is for TASKING compiler. The syntax varies for compiler to
compiler. Refer AUTOSAR SWS for exact information.
13 Madhusudhanan Ravi www.embeddeduncharted.com
Autosar MemMap
SECTION TYPES & KEYWORDS

14 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar Memmap - Mapping of Variables & Code
● Each AUTOSAR basic software module and software component shall support the basic memory
sections specified by Autosar Memmap SWS.

● It is allowed to add module specific sections as they are mapped and thus are configurable
within the module’s configuration file.

● <ALIGNMENT> - Variable Alignment. To avoid possible memory gaps during allocation, variables
are allocated memory according to the variable size.

15 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar Memmap - Variable size based Section Type

ALIGNMENT Valid Variables

BOOLEAN Used for variables and constants of size 1 bit.

8 / 8BIT Used for variables and constants which have to be aligned to 8 bit. Arrays, structs and
unions containing elements of maximum 8 bits.

16 / 16BIT Used for variables and constants which have to be aligned to 16 bit. Arrays, structs and
unions containing elements of maximum 16 bits.

32 / 32BIT Used for variables and constants which have to be aligned to 32 bit. Arrays, structs and
unions containing elements of maximum 32 bits.

UNSPECIFIED used for variables, constants, structure, array and unions when SIZE does not fit the
criteria of 8,16 or 32 bit.
Used for variables and constants of Unknown size.

16 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar Memmap - INIT Policy
The following Initialization Policy post fixes of the variables are supported in the AUTOSAR.

Init Policy Description

NO_INIT Used for variables that are never cleared and never initialized.

CLEARED Used for variables that are cleared to zero after every reset.

POWER_ON_CLEARED Used for variables that are cleared to zero only after power on reset

INIT Used for variables that are initialized with values after every reset

POWER_ON_INIT Used for variables that are initialized with values only after power on reset.

17 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar Memmap - Keyword Templates
The following keywords are to be used before inclusion of the memory mapping header file in the source code
file.

<PREFIX>_START_SEC_<NAME>
#include “Memmap.h”

<CODE>

<PREFIX>_STOP_SEC_<NAME>
#include “Memmap.h”

18 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar Memmap - Keyword Templates
<PREFIX>_START_SEC_<NAME>
<PREFIX> for Basic Software Module

<PREFIX> is composed according to <snp>[_<vi>_<ai>]

where,
<snp> - Section Name Prefix. Module Abbreviation from the BSW Module list (e.g. ‘EEP’ or ‘CAN’) in upper case
letters of the BSW module.
<vi> is the vendorId of the BSW module.
<ai> is the vendorApiInfix of the BSW module.

Ex: ADC_START_SEC_<NAME>
CAN_17_MPCAN_START_SEC_<NAME>

<PREFIX> for Software Component

<PREFIX> is the shortName of the software component type for software components (case sensitive)

19 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar Memmap - Keyword Templates
<PREFIX>_START_SEC_<NAME>
<NAME>

<NAME> is the shortName of the Memory Section described in BSW Module Description or a SWC Description
(case sensitive) if the Memory Section has no symbol attribute defined.

<NAME> is the symbol of the Memory Section described in BSW Module Description or a SWC Description
(case sensitive) if the Memory Section has a symbol attribute.

Note : If the Memory Allocation Keywords shall appear in capital letters in the code the related Memory
Sections in the BSWMD or SWC Descriptions have to be named with capital letters.

20 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar Memmap - Data Memory Sections
Memory
Section/ Init Syntax Description
Policy

<PREFIX>_START_SEC_VAR_<INIT_POLICY>_<ALIGNMENT> To be used for all global or static variables.


<PREFIX>_STOP_SEC_VAR_<INIT_POLICY>_<ALIGNMENT>

<PREFIX>_START_SEC_VAR_FAST_<INIT_POLICY>_<ALIGNMENT> To be used for all global or static variables that have at


least one of the following properties:
<PREFIX>_STOP_SEC_VAR_FAST_<INIT_POLICY>_<ALIGNMENT> • accessed bitwise
• frequently used
• high number of accesses in source code.
VAR /
<INIT_ POLICY> <PREFIX>_START_SEC_VAR_SLOW_<INIT_POLICY>_<ALIGNMENT> To be used for all infrequently accessed global or static
<PREFIX>_STOP_SEC_VAR_SLOW_<INIT_POLICY>_<ALIGNMENT> variables.

<PREFIX>_START_SEC_INTERNAL_VAR_<INIT_POLICY>_<ALIGNMENT To be used for global or static variables those are


> accessible from a calibration tool.
<PREFIX>_STOP_SEC_INTERNAL_VAR_<INIT_POLICY>_<ALIGNMENT>

VAR / <PREFIX>_START_SEC_VAR_SAVED_ZONE<X>_<ALIGNMENT> To be used for RAM buffers of variables saved in non


NO-INIT <PREFIX>_STOP_SEC_VAR_SAVED_ZONE<X>_<ALIGNMENT> volatile memory.
Note : <INIT_POLICY> - Applicable for all INIT_POLICY Types
21 Madhusudhanan Ravi www.embeddeduncharted.com
Autosar Memmap - Data Memory Sections

Memory
Section/ Init Syntax Description
Policy

CONST / - - <PREFIX>_START_SEC_CONST_SAVED_RECOVERY_ZONE<X> To be used for ROM buffers of variables saved in non volatile
<PREFIX>_STOP_SEC_CONST_SAVED_RECOVERY_ZONE<X> memory.

<PREFIX>_START_SEC_CONST_<ALIGNMENT> To be used for global or static constants.


<PREFIX>_STOP_SEC_CONST_<ALIGNMENT>

<PREFIX>_START_SEC_CARTO_<ALIGNMENT> To be used for cartography constants.


<PREFIX>_STOP_SEC_CARTO_<ALIGNMENT>

CAL-PRM <PREFIX>_START_SEC_CALIB_<ALIGNMENT> To be used for calibration constants.


<PREFIX>_STOP_SEC_CALIB_<ALIGNMENT>

CONFIG-DATA <PREFIX>_START_SEC_CONFIG_DATA_<ALIGNMENT> Constants with attributes that show that they reside in one
/ <PREFIX>_STOP_SEC CONFIG_DATA_<ALIGNMENT> segment for module configuration.
--

Note : CONST & CONFIG DATA have no Init-Policy as the sections are never modified.

22 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar Memmap - Code Memory Sections

Memory
Section/ Init Syntax Description
Policy

<PREFIX>_START_SEC_CODE To be used for mapping code to application block, boot block, external flash
<PREFIX>_STOP_SEC_CODE etc.

<PREFIX>_START_SEC_CALLOUT_CODE To be used for mapping callouts of the BSW Modules


<PREFIX>_STOP_SEC_CALLOUT_CODE

CODE /- - <PREFIX>_START_SEC_CODE_FAST[_<NUM>] To be used for code that shall go into fast code memory segments.
<PREFIX>_STOP_SEC_CODE_FAST[_<NUM>] The optional suffix [_<NUM>] can qualify the expected access commonness,
e.g. typical period of code execution.

<PREFIX>_START_SEC_CODE_SLOW To be used for code that shall go into slow code memory segments.
<PREFIX>_STOP_SEC_CODE_SLOW

<PREFIX>_START_SEC_CODE_LIB To be used for code that shall go into library segments for BSW module or
<PREFIX>_STOP_SEC_CODE_LIB Software Component.

23 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar MemMap
IMPLEMENTATION IN
MODULE FILES & MEMMAP FILE

24 Madhusudhanan Ravi www.embeddeduncharted.com


AUTOSAR MEMMAP - WRAPPING OF CODE

Each AUTOSAR basic software module and software component shall wrap declaration start_sec
and definition of code, variables and constants using the following mechanism,

1. Definition of start symbol for module memory section. #include


2. Inclusion of the memory mapping header file.
3. Declaration/definition of code, variables or constants belonging to the specified
section. <CODE>
4. Definition of stop symbol for module memory section.
5. Inclusion of the memory mapping header file. stop_sec
Note: For code which is invariably implemented as inline function the wrapping with
Memory Allocation Keywords is not required. #include

25 Madhusudhanan Ravi www.embeddeduncharted.com


AUTOSAR MEMMAP - WRAPPING OF CODE
The code section / Data declaration section is wrapped with MemMap keyword and inclusion of header file.

#define PORT_START_SEC_CODE #define EEP_START_SEC_VAR_INIT_16


#include “MemMap.h” #include “MemMap.h”
Void Port_Init (void) static uint16 EepTimer = 100;
{ static uint16 EepRemainingBytes = 16;
… #define EEP_STOP_SEC_VAR_INIT_16
… #include “MemMap.h”
}
#define PORT_STOP_SEC_CODE For example (SWC):
#include “MemMap.h”
#define Abc_START_SEC_CODE
#include “Abc_MemMap.h”
/* --- Write a Code here */
#define Abc_STOP_SEC_CODE
#include “Abc_MemMap.h”
In <MSN>.c

26 Madhusudhanan Ravi www.embeddeduncharted.com


AUTOSAR MEMMAP - MEMMAP IMPLEMENTATION

● The Variables declared inside EEP VAR INIT 16


#ifdef EEP_START_SEC_VAR_INIT_16 section are first re-declared under General
DATA INIT 16.
#undef EEP_START_SEC_VAR_INIT_16
#define START_SECTION_DATA_INIT_16
● The variables declared under DATA INIT 16
#elif are then mapped to sect_data16.
/* additional mappings */
#endif

#ifdef START_SECTION_DATA_INIT_16
#pragma section data "sect_data16"
#undef START_SECTION_DATA_INIT_16 In MemMap.h
#undef MEMMAP_ERROR
#elif
/* additional statements */ EEP INIT VAR 16 --> DATA INIT 16 --> sect_data16
#endif
27 Madhusudhanan Ravi www.embeddeduncharted.com
AUTOSAR MEMMAP - MEMMAP IMPLEMENTATION
● As below, In MemMap.h similar sections from different modules are combined under a single Generic
MemMap section.
● The generic memmap sections are then allocated actual memory section.

Module Sections in
MSN Source File
EEP INIT VAR 16
Generic Section in Memory section
CAN INIT VAR 16 MemMap.h

FLS INIT VAR 16 DATA INIT 16 sect_data16

ADC INIT VAR 16

...

MCU INIT VAR 16 In MemMap.h

28 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar MemMap
HW ADDRESS MAPPING

29 Madhusudhanan Ravi www.embeddeduncharted.com


AUTOSAR MEMMAP - ADDRESS MAPPING
● Once the various code/data sections are combined to general sections in the memmap file, the general
sections shall be allocated to linker memory sections declared in the linker directives (.ld) file.

● The LD file also specifies the actual hardware addresses for these linker memory section.

● Usually for each hardware memory type/cluster, a linker memory section is dedicated. Ex RAM,
INT_ROM_1, INT_ROM_2, EXT_ROM, etc.

EEP INIT VAR 16 --> DATA INIT 16 --> sect_data16 --> LD MEM SECTION --> HW ADDRESS

30 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar MemMap
EXAMPLE

31 Madhusudhanan Ravi www.embeddeduncharted.com


AUTOSAR MEMMAP - EXAMPLE
An example memory mapping of the Autosar CAN module variable “Can_17_MCanP_DriverState” is
described in the following slides across,

● CAN Module file


● Memory map file
● Linker Description file
● Map file.

32 Madhusudhanan Ravi www.embeddeduncharted.com


AUTOSAR MEMMAP - EXAMPLE

In Can_17_MCanP.c file, the unintialized 8 bit variable Can_17_MCanP_DriverState is defined,

#define CAN_17_MCANP_START_SEC_VAR_NO_INIT_8BIT
#include "MemMap.h"

/* This flag stores the CAN driver state */


Can_17_MCanP_DriverStateType Can_17_MCanP_DriverState;

#define CAN_17_MCANP_STOP_SEC_VAR_NO_INIT_8BIT
#include "MemMap.h"

33 Madhusudhanan Ravi www.embeddeduncharted.com


AUTOSAR MEMMAP - EXAMPLE
In MemMap.h file, the consolidation of the variables from CAN specific 8-bit unitializied section into GENERIC 8 bit
section, which inturn is assigned to compiler identifiable ".uncleared“ section

#elif defined CAN_17_MCANP_START_SEC_VAR_NO_INIT_8BIT


#undef CAN_17_MCANP_START_SEC_VAR_NO_INIT_8BIT
#define START_SEC_VAR_NO_INIT_8BIT

#elif defined CAN_17_MCANP_STOP_SEC_VAR_NO_INIT_8BIT


#undef CAN_17_MCANP_STOP_SEC_VAR_NO_INIT_8BIT
#define STOP_SEC_VAR_NO_INIT_8BIT

/* !Comment: SEC_VAR_NO_INIT_8BIT Start Section treatment */


#elif defined START_SEC_VAR_NO_INIT_8BIT

#pragma section ".uncleared" aw

#undef MEMMAP_ERROR Error checking in Memmap


#define MEMMAP_START_DONE

34 Madhusudhanan Ravi www.embeddeduncharted.com


AUTOSAR MEMMAP - EXAMPLE
In Linker Directives file, a RAM address range is defined and allocated for the .uncleared memory section

MEMORY
{
#dsram section assigned for memory addresses - 0x70000400 to 0x700FFFFF
dsram0 (w!xp): org = 0x70000400, len = 71K
}

SECTIONS
{

CORE_SEC(.uncleared) : FLAGS(aw)
{
. = ALIGN(4);
KEEP(*(.uncleared));
KEEP(*(.uncleared.*));
} > dsram0 AT> pfls1
}

35 Madhusudhanan Ravi www.embeddeduncharted.com


AUTOSAR MEMMAP - EXAMPLE
In the build output .map file, it can be verified that the “Can_17_MCanP_DriverState” is placed at an address lying in
the range defined in the linker description file.

Can_17_MCanP_ConfigRoot 0x8011e4a4 0x8011e4db 56 pfls1


.const
Can_17_MCanP_ControllerMode 0x70000484 0x70000484 1 dsram0 .sbss
Can_17_MCanP_DisableControllerInterrupts 0x8010d4c2 0x8010d575 180 pfls1 .text
Can_17_MCanP_DriverState 0x700005f8 0x700005f8 1 dsram0 .sbss
Can_17_MCanP_EnableControllerInterrupts 0x8010d576 0x8010d625 176 pfls1 .text
Can_17_MCanP_Init 0x8010cbc8 0x8010d293 1740 pfls1 .text

36 Madhusudhanan Ravi www.embeddeduncharted.com


AUTOSAR MEMMAP - EXAMPLE

37 Madhusudhanan Ravi www.embeddeduncharted.com


Autosar MemMap
REFERENCES

38 Madhusudhanan Ravi www.embeddeduncharted.com


AUTOSAR MEMMAP - REFERENCES

 “Autosar Memory Mapping Specification” -


https://www.autosar.org/fileadmin/user_upload/standards/classic/4-
0/AUTOSAR_SWS_MemoryMapping.pdf

 “Linker Directives” - https://www.embeddeduncharted.com/post/understanding-linker-directives

 “Memory Alignment in C” - https://wr.informatik.uni-


hamburg.de/_media/teaching/wintersemester_2013_2014/epc-14-haase-svenhendrik-alignmentinc-
paper.pdf

39 Madhusudhanan Ravi www.embeddeduncharted.com

You might also like