Thursday, September 22, 2016

Controlled variable memory assignment in a EMBEDDED system code: Example

Following is an example of writing embedded code, where you control the space allocation for the variable using different coding technique.

Code is executed on Keil uVision IDE: IDE-Version: µVision V5.20.0.39
Using simulator

C Compiler:      Armcc.exe V5.06 update 1 (build 61)
Assembler:       Armasm.exe V5.06 update 1 (build 61)
Linker/Locator:  ArmLink.exe V5.06 update 1 (build 61)

startup file used: startup_ARMCM4.s
CMSIS device system source file: system_ARMCM4.c
(Startup code are auto generated by Keil, during the project creation process)

-->user_main.c



int __main(void)
{


/******** Accessing IRAM memory address using pointers *******/

unsigned int *ptr_a = (unsigned int *)0x20000000;
unsigned int *ptr_b = (unsigned int *)0x20000010;
unsigned int *ptr_c = (unsigned int *)0x20000020;
unsigned int *ptr_d = (unisgned int *)0x20000030;
*ptr_a = 9;
*ptr_b = 3;
while (1)
{
*ptr_c = (*ptr_a) * (*ptr_b);
*ptr_d = *ptr_c - *ptr_a;
*ptr_c = *ptr_d - *ptr_a;
(*ptr_a)++;
(*ptr_b)++;
}



/******** Auto addressing assignment for variables in IRAM *******/
/*
static unsigned int a = 0x10;
static unsigned int b = 0x11;
static unsigned int c = 0x12;
static unsigned int d = 0x13;
a = 9;
b = 3;
while (1)
{
c = a * b;
d = c - a;
c = d - a;
a++;
b++;
}
*/


/******** Absolute addressing assignment for variables in IRAM *******/
/*
static unsigned int a __attribute__((at(0x20000000))) = 0x10;
static unsigned int b __attribute__((at(0x20000010))) = 0x11;
static unsigned int c __attribute__((at(0x20000020))) = 0x12;
static unsigned int d __attribute__((at(0x20000020))) = 0x13;
a = 9;
b = 3;
while (1)
{
c = a * b;
d = c - a;
c = d - a;
a++;
b++;
}
*/


/******** Inlining Embedded ARM cortexM4-F assembly code into C code *******/
/*
__asm
(
"MOVS r0, #0x10 \n"
"MOVS r1, #0x11 \n"
"MOVS r2, #0x12 \n"
"MOVS r3, #0x13 \n"
);
while(1)
{
__asm
(
"MUL r2, r0, r1 \n"
"SUBS r3, r2, r0 \n"
"SUBS r2, r3, r1 \n"
"ADDS r0, r0, #0x01 \n"
"ADDS r1, r1, #0x01 \n"
);
}
*/


}


Execute various segments of code one at a time to see the effect.
Run the code in debug mode.

No comments:

Post a Comment

PROFILE

My photo
India
Design Engineer ( IFM Engineering Private Limited )

Followers