© 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 … }

Lecture Presentation 9.pdf fpga soc using c

  • 1.
    © 2017 ArmLimited Programming an SoC Using C Language
  • 2.
    © 2017 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 ArmLimited 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 … }