SYSC3320 - Note 12 Programming and SoC using C and Mixed C and Assembly

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/19

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 2:54 AM on 4/13/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

20 Terms

1
New cards

Steps for building a system on chip

  1. hardware design

  2. software and low-level drivers + libraries programming

  3. Software high-level application development

2
New cards

C program generation flow

Compile → assemble → link → download

Steps:

  1. C code

  2. Compile into assembly code

  3. Assemble into machine code

  4. Link libraries into program image

  5. Download into program memory

<p>Compile → assemble → link → download</p><p>Steps:</p><ol><li><p>C code</p></li><li><p>Compile into assembly code</p></li><li><p>Assemble into machine code</p></li><li><p>Link libraries into program image</p></li><li><p>Download into program memory</p></li></ol><p></p><p></p>
3
New cards

Arm Program generation flow

  1. C source code and assembly source code

  2. Compile C source code using Armcc. Assemble assembly source coude with Armasm. Turn into object files

  3. Link using Armlink with libraries. Created into executable program mage(.axf file or .lib file)

  4. Download into program memory as bin file or hex file.

4
New cards

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

<ul><li><p>start up routine (initialize uninititalized data memory variables. labelled _main by Arm compiler)</p></li><li><p>program code (initial variable values and constants)</p></li><li><p>c library code (linker may add additional library code to program image)</p></li><li><p>vector table at 0×00000000</p><ul><li><p>interrupt vecotrs</p></li><li><p>systick vector </p></li><li><p>pendsv vector</p></li><li><p>reserved</p></li><li><p>hard fauolt vecotr</p></li><li><p>NMI cector</p></li><li><p>rset vecortr</p></li><li><p>inital msp value at 0×0000000</p></li></ul></li></ul><p></p>
5
New cards

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

<ul><li><p>reserved</p></li><li><p>private peripheral bus</p></li><li><p>external device</p></li><li><p>external ram</p></li><li><p>eripherals</p></li><li><p>sram (used for data memory)</p></li><li><p>code (program image ie on-chip flash) at 0×00000000</p></li></ul><p></p><p></p>
6
New cards

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

<p>To use the data types in the image, you must include stdint.h.</p><p>Provides int, uint: 8_t (char), 16_t (short), 32_t (int), 64_t (long) as well as others</p>
7
New cards

Xilinx data types

In xil_types.h there are optimized type definitions:

  • s32 (int32_t)

  • unint32_t = u32

  • int16_t = s16

  • u16

  • s8

  • u8

8
New cards

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

9
New cards

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

10
New cards

#define usage

Preprocessor directive that replaces all ocurrences of a name with the given text

11
New cards

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.

12
New cards

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()

13
New cards

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

<ul><li><p>int b is stored in zero-initialized static data (volatile meomry)</p></li><li><p>const char c = 123 is sotred in constant data (non-volatile memory)</p></li><li><p>int d = 31 is stored in initizlied static data (volatile memory)</p></li><li><p>int i within main is stored in stack data (volatile memory)</p></li><li><p>malloc(28) results in storage in heap data (volatile memory)</p></li><li><p>The addition operation in e = d + 7 results in a call to the program code in non-volatile memory</p></li><li><p>The call to printf results in a call to non-voltatile memory called runtime library code</p></li></ul><p></p><p></p>
14
New cards

How to write to a peripheral register

(unsigned int*) AHB_TIMER_BASE = 0×3FFFF; // store value to a peripheral

15
New cards

Read a value from a peripheral

i = *(unsigned int*) AHB_GPIO_BASE;

16
New cards

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

17
New cards

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

<p>More in depth example in image</p><pre><code>.global start
.extern print_hello

.section .text
.align 4

_start:
	bl print_hello
	b . // infinite loop to prevent exit</code></pre><p></p>
18
New cards

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

19
New cards

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;
}

20
New cards

Consider the following function:
void foo(int a, int b, int c, int d, int e, int f) {
int x = a + b + c + d + e + f;
} how are the parameters passed to the function?

First 4 are passed to R0 to R3. The rest are on the stack.