1/19
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Steps for building a system on chip
hardware design
software and low-level drivers + libraries programming
Software high-level application development
C program generation flow
Compile → assemble → link → download
Steps:
C code
Compile into assembly code
Assemble into machine code
Link libraries into program image
Download into program memory

Arm Program generation flow
C source code and assembly source code
Compile C source code using Armcc. Assemble assembly source coude with Armasm. Turn into object files
Link using Armlink with libraries. Created into executable program mage(.axf file or .lib file)
Download into program memory as bin file or hex file.
Cortex M program memory structure
start up routine (initialize uninititalized data memory variables. labelled _main by Arm compiler)
program code (initial variable values and constants)
c library code (linker may add additional library code to program image)
vector table at 0×00000000
interrupt vecotrs
systick vector
pendsv vector
reserved
hard fauolt vecotr
NMI cector
rset vecortr
inital msp value at 0×0000000

Program image in global memory
reserved
private peripheral bus
external device
external ram
eripherals
sram (used for data memory)
code (program image ie on-chip flash) at 0×00000000

Program data types
To use the data types in the image, you must include stdint.h.
Provides int, uint: 8_t (char), 16_t (short), 32_t (int), 64_t (long) as well as others

Xilinx data types
In xil_types.h there are optimized type definitions:
s32 (int32_t)
unint32_t = u32
int16_t = s16
u16
s8
u8
const usage
Used when the value is:
never written by the prgoram
can be put into ROM to save RAM
can be scoped within functions or files (better encapsulation compared to #define)
vol
volatile usage
Use when value:
can be changed outside of normal program flow such as by ISR or hardware register, memory mapped IO, shared memory, polling loops and device status checking
may get optimized out by the compiler or youd ont’ want optimzation
#define usage
Preprocessor directive that replaces all ocurrences of a name with the given text
extern usage
Declare a global variable or function in another file iwhtout defining it again. Tells teh compiler that the variable or function existsts somewhere esle.
Data storage regions
Static data: contains global variables and static variables
Stack: contains temporary data for local variables, parameter passing in funciotn calls, registers saving during exceptions, etc
Heap: contains pieces of memory spaces that are dynmaically reserved by function calls such as alloc() or malloc()
Data storage regions example
int b is stored in zero-initialized static data (volatile meomry)
const char c = 123 is sotred in constant data (non-volatile memory)
int d = 31 is stored in initizlied static data (volatile memory)
int i within main is stored in stack data (volatile memory)
malloc(28) results in storage in heap data (volatile memory)
The addition operation in e = d + 7 results in a call to the program code in non-volatile memory
The call to printf results in a call to non-voltatile memory called runtime library code

How to write to a peripheral register
(unsigned int*) AHB_TIMER_BASE = 0×3FFFF; // store value to a peripheral
Read a value from a peripheral
i = *(unsigned int*) AHB_GPIO_BASE;
Precautions for Calling a C function from assembly
R0, R1, R2, R3, R12 and LR may be changed so save them to the stack
SP needs to be aligned to a double-word address boundary
Input parameters need to be sotred to the correct registers (R0 to R3)
Output is usually in R0
Calling C function from assembly Xilinx Example
More in depth example in image
.global start
.extern print_hello
.section .text
.align 4
_start:
bl print_hello
b . // infinite loop to prevent exit
Guidelines for creating an assembly function called from C
If registers R4 to R11 need to be changed, then need to be stack and restored in the the assembly function
If another function is called inside the assembly function, LR needs to be saved on the stack and used for return
Return value placed in R0
Assembly function called in C example
.global factorial_asm
.section .text
.align 4
factorial_asm:
push (r4, lr)
cmp r0, #1
ble base_case
mov r4, r0
sub r0, r0, #1
bl factorial_num
mul r0, r4, r0
pop (r4, lr)
bx lr;
base_case:
mov r0, #1
pop (r4, lr)
bx lr#include <stdio.h>
extern int factorial_num(int a); // a stored in R0
int main() {
int num = 5;
int result = factorial_sum(num);
return 0;
}First 4 are passed to R0 to R3. The rest are on the stack.