SlideShare a Scribd company logo
© 2017 Arm Limited
Programming an SoC
Using C Language
© 2017 Arm Limited
2
Module Syllabus
Principles of C Programming and Assembly Programming
Programming Cortex-M0 Processors using C Language and Assembly Language
Writing Assembly Functions Inside C Files
© 2017 Arm Limited
3
Building a System on a Chip (SoC)
Memory
VGA
Peripheral
UART
Peripheral
Timer
Peripheral
GPIO
Peripheral
7-Segment
Peripheral
Arm CMSIS-Core
Application Programming Interface (API)
Application Design (e.g., Game)
Arm Cortex-M0
Processor
Hardware design
Software low-level drivers
& libraries programming
Software high-level
application development
Peripheral Drivers
AHB
Interrupt
© 2017 Arm Limited
4
C and Assembly Language Review
Language Advantages Disadvantages
C
Easy to learn Limited or no direct access to core registers and
stack
Portable No direct control over instruction sequence
generation
Easy handling of complex data structures No direct control over stack usage
Assembly
Allow direct control to each instruction step and all
memory
Take longer time to learn
Allows direct access to instructions that cannot be
generated with C
Difficult to manage data structure
Less portable
© 2017 Arm Limited
5
Typical Program-Generation Flow
The generation of a program follows a typical development flow.
• Compile > Assemble > Link > Download
• The generated executable file (or program image) is stored in the program memory (normally an on-chip flash
memory), to be fetched by the processor.
Data Output
C Code
Assembly Code
Machine Code Libraries
Program Image
Compile
Assemble
Link
Download
Program Memory
Fetch
Decode
Execute
Processor
Processing
Data Input
Instruction
Fetch
Off-line Compilation
Typical program-generation flow
© 2017 Arm Limited
6
Program-Generation Flow with Arm Tools
Compile Using Armcc Assemble Using Armasm
C Source Code Assembly Source Code
Object Files (.o) C/ C++
C/ C++
Libraries
Link Using Armlink
Executable Program Mage
Binary
.O Files
.S Files
.AXF File
.LIB file
.BIN File
.HEX File
Disassembly File
C, C++
ASM files
Download to Program Memory
© 2017 Arm Limited
7
Program Image
0x00000000
Initial MSP value
Code region
Start-up routine
Vector table
Program
Image
Reset vector
NMI vector
Hard fault vector
Reserved
SVC vector
Reserved
PendSV vector
SysTick vector
Interrupt vectors
0x00000000
0x00000004
0x00000008
0x0000000C
0x0000002C
0x00000038
0x00000040
0x0000003C
Program code
C library code
© 2017 Arm Limited
8
Program Image
0x00000000
Initial MSP value
Code region
Start-up routine
Vector table
Program
Image
Reset vector
NMI vector
Hard fault vector
Reserved
SVC vector
Reserved
PendSV vector
SysTick vector
Interrupt vectors
0x00000000
0x00000004
0x00000008
0x0000000C
0x0000002C
0x00000038
0x00000040
0x0000003C
Program code
C library code
© 2017 Arm Limited
9
Program Image
0x00000000
Initial MSP value
Code region
Start-up routine
Vector table
Program
Image
Reset vector
NMI vector
Hard fault vector
Reserved
SVC vector
Reserved
PendSV vector
SysTick vector
Interrupt vectors
0x00000000
0x00000004
0x00000008
0x0000000C
0x0000002C
0x00000038
0x00000040
0x0000003C
Program code
C library code
© 2017 Arm Limited
10
Program Image
0x00000000
Initial MSP value
Code region
Start-up routine
Vector table
Program
Image
Reset vector
NMI vector
Hard fault vector
Reserved
SVC vector
Reserved
PendSV vector
SysTick vector
Interrupt vectors
0x00000000
0x00000004
0x00000008
0x0000000C
0x0000002C
0x00000038
0x00000040
0x0000003C
Program code
C library code
© 2017 Arm Limited
11
Program Image
0x00000000
Initial MSP value
Code region
Start-up routine
Vector table
Program
Image
Reset vector
NMI vector
Hard fault vector
Reserved
SVC vector
Reserved
PendSV vector
SysTick vector
Interrupt vectors
0x00000000
0x00000004
0x00000008
0x0000000C
0x0000002C
0x00000038
0x00000040
0x0000003C
Program code
C library code
© 2017 Arm Limited
12
Program Image in Global Memory
Reserved
External Device
External RAM
Peripherals
SRAM
Code
0xFFFFFFFF
0xE0000000
Private Peripheral Bus
0xDFFFFFFF
0xA0000000
0x9FFFFFFF
0x60000000
0x5FFFFFFF
0x40000000
0x3FFFFFFF
0x1FFFFFFF
0x20000000
0x00000000
512MB
512MB
512MB
1GB
1GB
512MB
0xE00FFFFF
0xE0100000
Mainly used for data memory
e.g., on-chip SRAM, SDRAM
Mainly used for program image
e.g., on-chip FLASH
© 2017 Arm Limited
13
Program Data Types
Data type Size Signed range Unsigned range
char, int8_t, uint8_t Byte -128 to 127 0 to 255
short, int16_t, uint16_t Half word -32768 to 32767 0 to 65535
int, int32_t, uint32_t, long Word -2147483648 to 2147483647 0 to 4294967295
long , int64_t, uint64_t Double word -263 to 263-1 0 to 264-1
float Word -3.4028234 × 1038 to 3.4028234 × 1038
double, long double Double word -1.7976931348623157 ×10308 to 1.7976931348623157 ×10308
pointers Word 0x00 to 0xFFFFFFFF
enum Byte/half word/word Smallest possible data type
bool (C++), _bool(C) Byte True or false
wchar_t Half word 0 to 65535
© 2017 Arm Limited
14
Data Qualifiers in C Language
Const
• Never written by program; can be put in ROM to save RAM
Volatile
• Can be changed outside of normal program flow: ISR, hardware register
• Compiler must be careful with optimizations
© 2017 Arm Limited
15
How Is Data Stored in RAM
Typically, data can be stored in three regions:
static data, stack, and heap
• Static data: contains global variables and static variables
• Stack: contains temporary data for local variables,
parameter passing in function calls, registers saving
during exceptions, etc.
• Heap: contains the pieces of memory spaces that are
dynamically reserved by function calls, such as “alloc(),”
“malloc()”
Memory
Address
Grow
Downwards
High
Stack
Static
Data
Heap
Grow
Upwards
Low
© 2017 Arm Limited
16
i n t a , b ;
c o n s t c h a r c = 1 2 3 ;
i n t d = 3 1 ;
vo i d m a i n ( vo i d ) {
i n t i ;
c h a r f [ 3 2 ] ;
i n t * a r ray ;
a r ray = ( i n t * ) m a l l o c ( 1 2 8 ) ;
e = d + 7 ;
p r i n t f ( “ H e l l o ! ” ) ;
}
Example of Data Storage
Heap Data
Initialized Static Data
Stack Data
Zero-initialized Static
Data
Usually stored in volatile
memories, e.g., SRAM
Usually stored in non-volatile memories,
e.g., FLASH
Runtime Library Code
Initialization Data
Constant Data
Program Code
.text
Startup Code
© 2017 Arm Limited
17
Define Interrupt Vector in C
The interrupt vector can be defined in either C language or assembly language. For
example, in C:
typedef void(* const ExecFuncPtr)(void) __irq;
#pragma arm section rodata="exceptions_area”
ExecFuncPtr exception_table[] = {
(ExecFuncPtr)&Image$$ARM_LIB_STACK$$ZI$$Limit, /* Initial SP */
(ExecFuncPtr)__main, /* Initial PC */
NMIException,
HardFaultException,
MemManageException,
BusFaultException,
UsageFaultException,
0, 0, 0, 0, /* Reserved */
SVCHandler,
DebugMonitor,
0, /* Reserved */
PendSVC,
SysTickHandler
/* Configurable interrupts start here...*/
};
#pragma arm section
© 2017 Arm Limited
18
Define Stack and Heap
The stack and heap can be defined in either C language (with a linker file) or assembly
language. For example, in C:
/* Set stack and heap parameters */
#define STACK_BASE 0x10020000 //stack start address
#define STACK_SIZE 0x5000 //length of the stack
#define HEAP_BASE 0x10001000 //heap starts address
#define HEAP_SIZE 0x10000 – 0x6000 //heap length
/* inker generated stack base addresses */
extern unsigned int Image$$ARM_LIB_STACK$$ZI$$Limit
extern unsigned int Image$$ARM_LIB_STACKHEAP$$ZI$$Limit
…
© 2017 Arm Limited
19
Define Stack and Heap
Define stack and heap in assembly language:
Stack_Size EQU 0x00000400 ; 256KB of STACK
AREA STACK, NOINIT, READWRITE, ALIGN=4
Stack_Mem SPACE Stack_Size
__initial_sp
Heap_Size EQU 0x00000400 ; 1MB of HEAP
AREA HEAP, NOINIT, READWRITE, ALIGN=4
__heap_base
Heap_Mem SPACE Heap_Size
__heap_limit
© 2017 Arm Limited
20
Accessing Peripherals in C
Define base addresses for peripherals.
Write a value to a peripheral register.
Read a value from a peripheral register.
#define AHB_VGA_BASE 0x50000000
#define AHB_UART_BASE 0x51000000
#define AHB_TIMER_BASE 0x52000000
#define AHB_GPIO_BASE 0x53000000
#define AHB_7SEG_BASE 0x54000000
#define NVIC_INT_ENABLE 0xE000E100
*(unsigned int*) AHB_TIMER_BASE = 0x3FFFF; //store a value to the peripheral
i=*(unsigned int*) AHB_GPIO_BASE; //read a value from the peripheral
© 2017 Arm Limited
21
Calling a C Function from Assembly
When a C function is called from an assembly file, the following areas should be checked:
• Register R0, R1, R2, R3, R12, and LR could be changed; hence, it is better to save them to the stack.
• The value of SP should be aligned to a double-word address boundary.
• Input parameters have to be stored in the correct registers; for example, registers R0 to R3 can be used for passing
four parameters.
• The return value is usually stored in R0.
© 2017 Arm Limited
22
Calling a C Function from Assembly
ISR can be written in either assembly or C language; for example, in C:
Call a C function from the assembly code; for example:
void UART_ISR() {
char c;
c=*(char*) AHB_UART_BASE; //read a character from UART
…
}
UART_Handler PROC
EXPORT UART_Handler // label name in assembly
IMPORT UART_ISR // function name in C
PUSH {R0,R1,R2,LR} // context saving
BL UART_ISR // branch to ISR written in C
POP {R0,R1,R2,PC} // context restoring
ENDP
© 2017 Arm Limited
23
Calling an Assembly Function from C
When calling an assembly function from C code, the following areas should be checked:
• If registers R4 to R11 need to be changed, they have to be stacked and restored in the assembly function.
• If another function is called inside the assembly function, the LR register needs to be saved on the stack and used
for return.
• The function return value is normally stored in R0.
© 2017 Arm Limited
24
Calling an Assembly Function from C
Write a function in assembly:
Calling an assembly function in C:
EXPORT add_asm
add_asm FUNCTION
ADDS R0, R0, R1
ADDS R0, R0, R2
ADDS R0, R0, R3
BX LR ; result is returned in R0
ENDFUNC
external int add_asm( int k1, int k2, int k3, int k4);
void main {
int x;
x = add_asm (11,22,33,44); // call assembly function
…
}
© 2017 Arm Limited
25
Embedded Assembly
The embedded assembler allows a developer to write assembly functions inside C files:
_asm int add_asm( int k1, int k2, int k3, int k4) {
ADDS R0, R0, R1
ADDS R0, R0, R2
ADDS R0, R0, R3
BX LR
}
void main {
int x;
x = add_asm (11,22,33,44); // call assembly function
…
}

More Related Content

Similar to Lecture Presentation 9.pdf fpga soc using c (20)

PPTX
Introduction to ARM Systems-11-17-2012.pptx
mithunkarthikb24
 
PPTX
EC8791 ARM Processor and Peripherals.pptx
deviifet2015
 
PDF
ARM AAE - Developing Code for ARM
Anh Dung NGUYEN
 
PDF
Lecture Presentation 11.pdfLecture Presentation 9.pdf fpga soc
minamelad457
 
PPTX
arm.pptx
Siddharth2001213
 
PPTX
18CS44-MES-Module-2(Chapter 6).pptx
rakshitha481121
 
PPTX
Introduction to Processor Design and ARM Processor
Darling Jemima
 
PDF
AAME ARM Techcon2013 003v02 Software Development
Anh Dung NGUYEN
 
PPTX
Introduction to computer architecture .pptx
Fatma Sayed Ibrahim
 
PDF
Assembly programming
Omar Sanchez
 
PPTX
Mces MOD 1.pptx
RadhaC10
 
PPTX
Mod 3.pptx
lekha349785
 
PDF
C programming session9 -
Keroles karam khalil
 
PDF
The walking 0xDEAD
Carlos Garcia Prado
 
PPT
CO_Chapter2.ppt
Pranav726214
 
PPT
LPC 2148 Instructions Set.ppt
ProfBadariNathK
 
PPT
Embedded Systems ARM Computer Architecture
ssuserb53446
 
PDF
Module4.pdf ,...................................
chetanreddy2212
 
PPTX
Chapter_04_ARM_Assembly.pptx ARM ASSEMBLY CODE
NagarathnaRajur2
 
PPTX
Software for embedded systems complete
Anand Kumar
 
Introduction to ARM Systems-11-17-2012.pptx
mithunkarthikb24
 
EC8791 ARM Processor and Peripherals.pptx
deviifet2015
 
ARM AAE - Developing Code for ARM
Anh Dung NGUYEN
 
Lecture Presentation 11.pdfLecture Presentation 9.pdf fpga soc
minamelad457
 
18CS44-MES-Module-2(Chapter 6).pptx
rakshitha481121
 
Introduction to Processor Design and ARM Processor
Darling Jemima
 
AAME ARM Techcon2013 003v02 Software Development
Anh Dung NGUYEN
 
Introduction to computer architecture .pptx
Fatma Sayed Ibrahim
 
Assembly programming
Omar Sanchez
 
Mces MOD 1.pptx
RadhaC10
 
Mod 3.pptx
lekha349785
 
C programming session9 -
Keroles karam khalil
 
The walking 0xDEAD
Carlos Garcia Prado
 
CO_Chapter2.ppt
Pranav726214
 
LPC 2148 Instructions Set.ppt
ProfBadariNathK
 
Embedded Systems ARM Computer Architecture
ssuserb53446
 
Module4.pdf ,...................................
chetanreddy2212
 
Chapter_04_ARM_Assembly.pptx ARM ASSEMBLY CODE
NagarathnaRajur2
 
Software for embedded systems complete
Anand Kumar
 

Recently uploaded (20)

PPTX
Introduction to Python Programming Language
merlinjohnsy
 
PDF
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
PPTX
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
PPTX
Precooling and Refrigerated storage.pptx
ThongamSunita
 
PPT
دراسة حاله لقرية تقع في جنوب غرب السودان
محمد قصص فتوتة
 
PPTX
CST413 KTU S7 CSE Machine Learning Neural Networks and Support Vector Machine...
resming1
 
PPTX
How to Un-Obsolete Your Legacy Keypad Design
Epec Engineered Technologies
 
PPTX
Computer network Computer network Computer network Computer network
Shrikant317689
 
PPT
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
PDF
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
PPTX
Work at Height training for workers .pptx
cecos12
 
PPTX
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
 
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
 
PPTX
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
PDF
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
PDF
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PPTX
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
PPSX
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
PDF
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
Introduction to Python Programming Language
merlinjohnsy
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
Precooling and Refrigerated storage.pptx
ThongamSunita
 
دراسة حاله لقرية تقع في جنوب غرب السودان
محمد قصص فتوتة
 
CST413 KTU S7 CSE Machine Learning Neural Networks and Support Vector Machine...
resming1
 
How to Un-Obsolete Your Legacy Keypad Design
Epec Engineered Technologies
 
Computer network Computer network Computer network Computer network
Shrikant317689
 
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
Work at Height training for workers .pptx
cecos12
 
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
 
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
Ad

Lecture Presentation 9.pdf fpga soc using c

  • 1. © 2017 Arm Limited Programming an SoC Using C Language
  • 2. © 2017 Arm Limited 2 Module Syllabus Principles of C Programming and Assembly Programming Programming Cortex-M0 Processors using C Language and Assembly Language Writing Assembly Functions Inside C Files
  • 3. © 2017 Arm Limited 3 Building a System on a Chip (SoC) Memory VGA Peripheral UART Peripheral Timer Peripheral GPIO Peripheral 7-Segment Peripheral Arm CMSIS-Core Application Programming Interface (API) Application Design (e.g., Game) Arm Cortex-M0 Processor Hardware design Software low-level drivers & libraries programming Software high-level application development Peripheral Drivers AHB Interrupt
  • 4. © 2017 Arm Limited 4 C and Assembly Language Review Language Advantages Disadvantages C Easy to learn Limited or no direct access to core registers and stack Portable No direct control over instruction sequence generation Easy handling of complex data structures No direct control over stack usage Assembly Allow direct control to each instruction step and all memory Take longer time to learn Allows direct access to instructions that cannot be generated with C Difficult to manage data structure Less portable
  • 5. © 2017 Arm Limited 5 Typical Program-Generation Flow The generation of a program follows a typical development flow. • Compile > Assemble > Link > Download • The generated executable file (or program image) is stored in the program memory (normally an on-chip flash memory), to be fetched by the processor. Data Output C Code Assembly Code Machine Code Libraries Program Image Compile Assemble Link Download Program Memory Fetch Decode Execute Processor Processing Data Input Instruction Fetch Off-line Compilation Typical program-generation flow
  • 6. © 2017 Arm Limited 6 Program-Generation Flow with Arm Tools Compile Using Armcc Assemble Using Armasm C Source Code Assembly Source Code Object Files (.o) C/ C++ C/ C++ Libraries Link Using Armlink Executable Program Mage Binary .O Files .S Files .AXF File .LIB file .BIN File .HEX File Disassembly File C, C++ ASM files Download to Program Memory
  • 7. © 2017 Arm Limited 7 Program Image 0x00000000 Initial MSP value Code region Start-up routine Vector table Program Image Reset vector NMI vector Hard fault vector Reserved SVC vector Reserved PendSV vector SysTick vector Interrupt vectors 0x00000000 0x00000004 0x00000008 0x0000000C 0x0000002C 0x00000038 0x00000040 0x0000003C Program code C library code
  • 8. © 2017 Arm Limited 8 Program Image 0x00000000 Initial MSP value Code region Start-up routine Vector table Program Image Reset vector NMI vector Hard fault vector Reserved SVC vector Reserved PendSV vector SysTick vector Interrupt vectors 0x00000000 0x00000004 0x00000008 0x0000000C 0x0000002C 0x00000038 0x00000040 0x0000003C Program code C library code
  • 9. © 2017 Arm Limited 9 Program Image 0x00000000 Initial MSP value Code region Start-up routine Vector table Program Image Reset vector NMI vector Hard fault vector Reserved SVC vector Reserved PendSV vector SysTick vector Interrupt vectors 0x00000000 0x00000004 0x00000008 0x0000000C 0x0000002C 0x00000038 0x00000040 0x0000003C Program code C library code
  • 10. © 2017 Arm Limited 10 Program Image 0x00000000 Initial MSP value Code region Start-up routine Vector table Program Image Reset vector NMI vector Hard fault vector Reserved SVC vector Reserved PendSV vector SysTick vector Interrupt vectors 0x00000000 0x00000004 0x00000008 0x0000000C 0x0000002C 0x00000038 0x00000040 0x0000003C Program code C library code
  • 11. © 2017 Arm Limited 11 Program Image 0x00000000 Initial MSP value Code region Start-up routine Vector table Program Image Reset vector NMI vector Hard fault vector Reserved SVC vector Reserved PendSV vector SysTick vector Interrupt vectors 0x00000000 0x00000004 0x00000008 0x0000000C 0x0000002C 0x00000038 0x00000040 0x0000003C Program code C library code
  • 12. © 2017 Arm Limited 12 Program Image in Global Memory Reserved External Device External RAM Peripherals SRAM Code 0xFFFFFFFF 0xE0000000 Private Peripheral Bus 0xDFFFFFFF 0xA0000000 0x9FFFFFFF 0x60000000 0x5FFFFFFF 0x40000000 0x3FFFFFFF 0x1FFFFFFF 0x20000000 0x00000000 512MB 512MB 512MB 1GB 1GB 512MB 0xE00FFFFF 0xE0100000 Mainly used for data memory e.g., on-chip SRAM, SDRAM Mainly used for program image e.g., on-chip FLASH
  • 13. © 2017 Arm Limited 13 Program Data Types Data type Size Signed range Unsigned range char, int8_t, uint8_t Byte -128 to 127 0 to 255 short, int16_t, uint16_t Half word -32768 to 32767 0 to 65535 int, int32_t, uint32_t, long Word -2147483648 to 2147483647 0 to 4294967295 long , int64_t, uint64_t Double word -263 to 263-1 0 to 264-1 float Word -3.4028234 × 1038 to 3.4028234 × 1038 double, long double Double word -1.7976931348623157 ×10308 to 1.7976931348623157 ×10308 pointers Word 0x00 to 0xFFFFFFFF enum Byte/half word/word Smallest possible data type bool (C++), _bool(C) Byte True or false wchar_t Half word 0 to 65535
  • 14. © 2017 Arm Limited 14 Data Qualifiers in C Language Const • Never written by program; can be put in ROM to save RAM Volatile • Can be changed outside of normal program flow: ISR, hardware register • Compiler must be careful with optimizations
  • 15. © 2017 Arm Limited 15 How Is Data Stored in RAM Typically, data can be stored in three regions: static data, stack, and heap • Static data: contains global variables and static variables • Stack: contains temporary data for local variables, parameter passing in function calls, registers saving during exceptions, etc. • Heap: contains the pieces of memory spaces that are dynamically reserved by function calls, such as “alloc(),” “malloc()” Memory Address Grow Downwards High Stack Static Data Heap Grow Upwards Low
  • 16. © 2017 Arm Limited 16 i n t a , b ; c o n s t c h a r c = 1 2 3 ; i n t d = 3 1 ; vo i d m a i n ( vo i d ) { i n t i ; c h a r f [ 3 2 ] ; i n t * a r ray ; a r ray = ( i n t * ) m a l l o c ( 1 2 8 ) ; e = d + 7 ; p r i n t f ( “ H e l l o ! ” ) ; } Example of Data Storage Heap Data Initialized Static Data Stack Data Zero-initialized Static Data Usually stored in volatile memories, e.g., SRAM Usually stored in non-volatile memories, e.g., FLASH Runtime Library Code Initialization Data Constant Data Program Code .text Startup Code
  • 17. © 2017 Arm Limited 17 Define Interrupt Vector in C The interrupt vector can be defined in either C language or assembly language. For example, in C: typedef void(* const ExecFuncPtr)(void) __irq; #pragma arm section rodata="exceptions_area” ExecFuncPtr exception_table[] = { (ExecFuncPtr)&Image$$ARM_LIB_STACK$$ZI$$Limit, /* Initial SP */ (ExecFuncPtr)__main, /* Initial PC */ NMIException, HardFaultException, MemManageException, BusFaultException, UsageFaultException, 0, 0, 0, 0, /* Reserved */ SVCHandler, DebugMonitor, 0, /* Reserved */ PendSVC, SysTickHandler /* Configurable interrupts start here...*/ }; #pragma arm section
  • 18. © 2017 Arm Limited 18 Define Stack and Heap The stack and heap can be defined in either C language (with a linker file) or assembly language. For example, in C: /* Set stack and heap parameters */ #define STACK_BASE 0x10020000 //stack start address #define STACK_SIZE 0x5000 //length of the stack #define HEAP_BASE 0x10001000 //heap starts address #define HEAP_SIZE 0x10000 – 0x6000 //heap length /* inker generated stack base addresses */ extern unsigned int Image$$ARM_LIB_STACK$$ZI$$Limit extern unsigned int Image$$ARM_LIB_STACKHEAP$$ZI$$Limit …
  • 19. © 2017 Arm Limited 19 Define Stack and Heap Define stack and heap in assembly language: Stack_Size EQU 0x00000400 ; 256KB of STACK AREA STACK, NOINIT, READWRITE, ALIGN=4 Stack_Mem SPACE Stack_Size __initial_sp Heap_Size EQU 0x00000400 ; 1MB of HEAP AREA HEAP, NOINIT, READWRITE, ALIGN=4 __heap_base Heap_Mem SPACE Heap_Size __heap_limit
  • 20. © 2017 Arm Limited 20 Accessing Peripherals in C Define base addresses for peripherals. Write a value to a peripheral register. Read a value from a peripheral register. #define AHB_VGA_BASE 0x50000000 #define AHB_UART_BASE 0x51000000 #define AHB_TIMER_BASE 0x52000000 #define AHB_GPIO_BASE 0x53000000 #define AHB_7SEG_BASE 0x54000000 #define NVIC_INT_ENABLE 0xE000E100 *(unsigned int*) AHB_TIMER_BASE = 0x3FFFF; //store a value to the peripheral i=*(unsigned int*) AHB_GPIO_BASE; //read a value from the peripheral
  • 21. © 2017 Arm Limited 21 Calling a C Function from Assembly When a C function is called from an assembly file, the following areas should be checked: • Register R0, R1, R2, R3, R12, and LR could be changed; hence, it is better to save them to the stack. • The value of SP should be aligned to a double-word address boundary. • Input parameters have to be stored in the correct registers; for example, registers R0 to R3 can be used for passing four parameters. • The return value is usually stored in R0.
  • 22. © 2017 Arm Limited 22 Calling a C Function from Assembly ISR can be written in either assembly or C language; for example, in C: Call a C function from the assembly code; for example: void UART_ISR() { char c; c=*(char*) AHB_UART_BASE; //read a character from UART … } UART_Handler PROC EXPORT UART_Handler // label name in assembly IMPORT UART_ISR // function name in C PUSH {R0,R1,R2,LR} // context saving BL UART_ISR // branch to ISR written in C POP {R0,R1,R2,PC} // context restoring ENDP
  • 23. © 2017 Arm Limited 23 Calling an Assembly Function from C When calling an assembly function from C code, the following areas should be checked: • If registers R4 to R11 need to be changed, they have to be stacked and restored in the assembly function. • If another function is called inside the assembly function, the LR register needs to be saved on the stack and used for return. • The function return value is normally stored in R0.
  • 24. © 2017 Arm Limited 24 Calling an Assembly Function from C Write a function in assembly: Calling an assembly function in C: EXPORT add_asm add_asm FUNCTION ADDS R0, R0, R1 ADDS R0, R0, R2 ADDS R0, R0, R3 BX LR ; result is returned in R0 ENDFUNC external int add_asm( int k1, int k2, int k3, int k4); void main { int x; x = add_asm (11,22,33,44); // call assembly function … }
  • 25. © 2017 Arm Limited 25 Embedded Assembly The embedded assembler allows a developer to write assembly functions inside C files: _asm int add_asm( int k1, int k2, int k3, int k4) { ADDS R0, R0, R1 ADDS R0, R0, R2 ADDS R0, R0, R3 BX LR } void main { int x; x = add_asm (11,22,33,44); // call assembly function … }