C Programming in Embedded Systems Computer Science Engineering
C Programming in Embedded Systems Computer Science & Engineering Department Arizona State University Tempe, AZ 85287 Dr. Yann-Hang Lee yhlee@asu. edu (480) 727 -7507 7/23
C Programming in Embedded Systems q Assembly language v dependent of processor architecture v cache control, registers, program status, interrupt q High-level language v memory model v independent of processor architecture (partially true) q Advantages and disadvantages v performance v code size v software development and life cycle set 3 -- 1
Manage IO Operations Using C q Access memory-mapped IO – pointers q Example #define REG_READ (a, val) ((val) = *(volatile unsigned char *)(a)) #define REG_WRITE (a, val) (*(volatile unsigned char *)(a) = (val)) #define UART_USR 0 0 x 4000_0204 #define UART_CR 0 x 4000_0208 #define UART_RX_EN 1 #define UART_TX_EN (1<<2) char CR_word=0; CR_word |= UART_RX_EN | UART_TX_EN; REG_WRITE (UART_CR, CR_word); set 3 -- 2
Platform. h for MXL #ifndef __MCF 5213_UART_H__ #define __MCF 5213_UART_H__ /* Register read/write macros */ #define MCF_UART 0_UMR #define MCF_UART 0_USR #define MCF_UART 0_UCR (*(vuint 8 *)(&__IPSBAR[0 x 000200])) (*(vuint 8 *)(&__IPSBAR[0 x 000204])) (*(vuint 8 *)(&__IPSBAR[0 x 000208])) According to Linux C/C++ coding style: “_” Variable not intended to be exposed externally to the user. Internal workings only. Most often used by compiler and library authors. Sometimes used (not preferred) as a suffix to represent a class member variable. “__” Marks an use as extension to ANSI C++. Often not compiler independent. Usually reserved for use by compiler writers. set 3 -- 3
Bit Manipulation q Boolean operation v operate on 1 (true) and 0 (false) v (2 || !6 ) && 7 ? ? q Bitwise operation Operation Boolean op. Bitwise op. AND && & OR || | XOR unsupported ^ NOT ! ~ v operate on individual bit positions within the operands v (2 | ~6 ) & 7 = (0 x 0002 OR 0 x. FFF 1) AND 0 x 0007 if (bits & 0 x 0040) bits |= (1 <<7) if (bits & (1 <<6)) bits &= ~(1<<7) long integer: bits &= ~(1 L << 7) set 3 -- 4
Interface C and Assembly Language q Why combine C and assembly language v performance v C doesn’t handle most architecture features, such as registers, program status, etc. q Develop C and assembly programs and then link them together v at source level – in-line assembly code in C program v at object level – procedure call mwasmcf mwldmcf mwccmcf set 3 -- 5
Calling Convention q GCC calling convention stack pointer v arguments passed in registers and in stack dynamic area v registers saved by caller and callee (including frame pointer and returning PC) v frame pointer points just below the last argument passed on the stack (the bottom of the frame) v stack pointer points to the first word after the frame pointer local variables saved registers (by callee) argument x argument y saved registers (by caller) set 3 -- 6
Coldfire Calling Convention q Passes all parameters on the stack in reverse order. v Push the last argument first v Compact — Passes on even sized boundary for parameters smaller than int (2 for short and char). v Standard — Like compact, but always padded to 4 bytes. v Register — Passes in scratch registers D 0 — D 2 for integers, A 0— A 1 for pointers. q Returning v returns an integer value in register D 0. v returns a pointer value in register A 0. v If it returns a value of any other type, Ø the caller reserves temporary storage area for that type in the caller's stack and passes a pointer to that area as the last argument. Ø the called function returns its value in the temporary storage area. set 3 -- 7
Register Usage by Coldfire C Compiler q Save code pointer (the value of pc) allows the function corresponding to a stack backtrace structure to be located q Save frame and stack pointers to locate stack frame q Register usage v A 0 -A 1, D 0 -D 2 – scratch registers v A 2 through A 5 — for pointers v D 3 through D 7 — for integers and pointers. v A 6 – frame pointer v A 7 – stack pointer q Caller needs to save D 0 -D 2 and A 0 -A 2 before calling if the values must be preserved q Callee needs to save D 3 -D 7 and A 3 -A 5 if they are used in the procedure set 3 -- 8
Stack Usage by Coldfire C Compiler q Allocation of local variable in stack q Save frame and stack pointers to locate stack frame q Entry code link a 6, #-framesize ; save and set up fp and sp, movem. l …. . , -(sp) ; save registers move. l (. . , a 6), d 0 ; retrieve parameters ; link instruction: SP – 4 → SP; Ay → (SP); ; SP → Ay; SP + dn → SP q On function exit move. l …. , d 0 ; return value movem. l (sp)+, …. ; restore registers unlk a 6 ; restore sp and fp ; unlk instruction -- Ax → SP; (SP) → Ax; ; SP + 4 → SP rts set 3 -- 9
Calling Assembly Routine from C q In C program char *srcstr = "First string - source "; char *dststr = "Second string - destination "; strcopy(dststr, srcstr); q Assembly routine. global strcopy: move. l next: move. b bne. b rts. end (4, sp), a 0 (8, sp), a 1 (a 1)+, d 0, (a 0)+ next ; a 0 points to destination string. ; a 1 points to source string. ; Load byte and update address. ; Store byte and update address ; Check for zero terminator. ; Return. set 3 -- 10
Inline Assembly Code in C Program q A feature provided by C compiler v to specify that a block of code in your file as assembly language v use the “asm” keyword. v compiler will do the insertion and knows the variables and the registers q Example: function level long int b; struct mystruct { long int a; }; static asm long f(void) // Legal asm qualifier { move. l struct(mystruct. a)(A 0), D 0 // Accessing a struct. add. l b, D 0 // Using a global variable, put rts // return value in D 0. // Return from the function: // result = mystruct. a + b } set 3 -- 11
Inline Assembly and Access Control Registers q Statement level long square(short a) { long result=0; asm { move. w a, d 0 // fetch function argument ‘a’ mulu. w d 0, d 0 // multiply move. l d 0, result // store in local ‘result’ variable } return result; } q Access local and global variables and inline assembly directives q Write to the special purpose registers _mcf 5 xxx_wr_sr: _mcf 5 xxx_wr_vbr: move. l 4(SP), D 0. long 0 x 4 e 7 b 0801 /* movec d 0, VBR */ move. w D 0, SR nop rts set 3 -- 12
Example sumsq int *sum, array[5]; void sumsq (int *sum, int size, int array[]) { int total=0; int i; for ( i = size-1; i < 0; i--) total= total + array[i]^2; *sum=total; } int main() { sumsq(sum, 5, array); while(1); // Idle } set 3 -- 13
Example sumsq -- main int *sum, array[5]; ; 19: int main() ; 20: { ; 21: 0 x 0000 _main: ; main: 0 x 0000 0 x 4 E 560000 0 x 00000004 0 x 4 FEFFFF 4 ; ; 22: sumsq(sum, 5, array); ; 23: ; 0 x 00000008 0 x 41 F 90000 0 x 0000000 E 0 x 2 F 480008 0 x 00000012 0 x 7005 0 x 00000014 0 x 2 F 400004 0 x 00000018 0 x 41 F 90000 0 x 0000001 E 0 x 2010 0 x 00000020 0 x 2 E 80 0 x 00000022 0 x 4 EB 90000 link lea a 6, #0 -12(a 7), a 7 lea move. l moveq move. l lea move. l jsr _array, a 0, 8(a 7) #5, d 0, 4(a 7) _sum, a 0 (a 0), d 0, (a 7) _sumsq set 3 -- 14
Example sumsq -- main ; ; 24: while(1); // Idle ; 25: ; 0 x 00000028 0 x 60 FE bra. s *+0 0 x 0000002 A 0 x 4 E 71 nop ; 10: ; 11: void sumsq (int *sum, int size, int array[]) ; 12: { ; 0 x 0000 _sumsq: ; sumsq: 0 x 0000 0 x 4 E 560000 link a 6, #0 0 x 00000004 0 x 4 FEFFFF 4 lea -12(a 7), a 7 ; 13: int total=0; ; 14: int i; 0 x 00000008 0 x 7000 moveq #0, d 0 0 x 0000000 A 0 x 2 D 40 FFF 8 move. l d 0, -8(a 6) ; 0 x 00000028 set 3 -- 15
Example sumsq ; ; 15: for ( i = size-1; i < 0; i--) ; 0 x 0000000 E 0 x 202 E 000 C 0 x 00000012 0 x 5380 0 x 00000014 0 x 2 D 40 FFF 4 0 x 00000018 0 x 6030 0 x 0000004 a ; ; 16: total= total + array[i]^2; ; 0 x 0000001 A 0 x 202 EFFF 4 0 x 0000001 E 0 x 2 D 40 FFFC 0 x 00000022 0 x 202 EFFFC 0 x 00000026 0 x 206 E 0010 0 x 0000002 A 0 x 222 EFFFC 0 x 0000002 E 0 x 202 EFFF 8 0 x 00000032 0 x. D 0 B 01 C 00 0 x 00000036 0 x 0 A 800002 move. l subq. l move. l bra. s 12(a 6), d 0 #1, d 0, -12(a 6) *+50 move. l movea. l move. l add. l eori. l -12(a 6), d 0, -4(a 6), d 0 16(a 6), a 0 -4(a 6), d 1 -8(a 6), d 0 (a 0, d 1. l*4), d 0 #0 x 2, d 0 ; ; '. . ' set 3 -- 16
Example sumsq 0 x 0000003 C 0 x 00000040 0 x 00000044 0 x 00000046 0 x 0000004 A 0 x 0000004 E 0 x 00000050 ; ; 17: ; 0 x 00000052 0 x 00000056 0 x 0000005 A ; ; 18: } 0 x 0000005 C 0 x 0000005 E 0 x 2 D 40 FFF 8 0 x 202 EFFF 4 0 x 5380 0 x 2 D 40 FFF 4 0 x 202 EFFF 4 0 x 4 A 80 0 x 6 DC 8 move. l subq. l move. l tst. l blt. s d 0, -8(a 6) -12(a 6), d 0 #1, d 0, -12(a 6), d 0 *-54 ; 0 x 0000001 a *sum=total; 0 x 206 E 0008 0 x 202 EFFF 8 0 x 2080 movea. l 8(a 6), a 0 move. l -8(a 6), d 0 move. l d 0, (a 0) 0 x 4 E 5 E 0 x 4 E 75 unlk rts a 6 set 3 -- 17
- Slides: 18