C Programming for Embedded Systems Computer Layers Lowlevel
C Programming for Embedded Systems
Computer Layers Low-level hardware to high-level software (4 GL: “domain-specific”, report-driven, e. g. ) fig_06_00
From source code to executable program: Compilation: Preprocessing step: fig_06_01
Compiling or assembling: fig_06_03
The entire process: fig_06_04
Example files in the process: fig_06_05
Important features of embedded code: Performance Robustness (response to failures) Ease of change Style—simple, understandable example: flag = x != 0 && ! y/x < 0 how is this evaluated? in embedded systems: use parentheses!!!!!!! fig_06_06
C integral types: char short int long (signed or unsigned)
Unsigned integer—range and format fig_06_08
Signed integer (1’s complement)—range & form fig_06_10
Numbers in memory—sign in most sig “nibble” fig_06_12
Character data fig_06_14
Floating point data fig_06_15
Exponent: “excess notation” fig_06_16
Example program: fig_06_17
Variable designations and visibility: scope fig_06_18
Storage classes fig_06_20
Example: fig_06_21
Separate compilations: fig_06_22
Makefiles: example fig_06_23
Bitwise Operators Pointers Functions Structs Interrupts (in C)
Bitwise operators: useful for dealing with signals of different widths; NOTE: these are LOGICAL operators and variables are declared UNSIGNED— why? (example: homework 1, problem 1) Additional useful reference: http: //www. cs. cf. ac. uk/Dave/C/node 13. html table_07_00
Examples of c operations at the byte level fig_07_00
Example of c program—”port. Shadow” mirrors what is on port; Note use of parentheses to guarantee order of bitlevel operations (even if we are using default order) fig_07_01
Using shift operators (remember these are logical): fig_07_02
Redoing the previous problem with shifts: fig_07_03
Getting data from the port: fig_07_04
Using bitlevel operations for arithmetic; Are there any problems with this approach? C: on signed data, left shift undefined if overflow occurs, right shift is implementation-dependent; Java: all integers are signed fig_07_05
Can use shifts for slightly more complex multiplies and divides; More complex operations (e. g. , with 3 1’s in multiplier or divider) are probably not efficient fig_07_06
Pointers: example data fig_07_07
Example instruction: fig_07_09
Example 2 of Instruction execution fig_07_10
Example: what is output? fig_07_11
Pointer arithmetic—not for beginners! fig_07_12 a
Pointer arithmetic: additional examples fig_07_12 b
Constant pointers: fig_07_13
Using constants and constant pointers: fig_07_14
Note: pointers can be “generic” or NULL: Generic: Type void: pointer can point to a variable of any type example: 7. 3, p. 265 NULL: Pointer needs to be assigned a valid address before dereferencing, otherwise hard-to-find bugs can occur Address 0 is not acceptable Solution: use null pointer address, (void*) 0 fig_07_14
C functions: fig_07_15
Example: fig_07_16
Function call: note order of parameters on stack fig_07_17
Using stack to return result: fig_07_18 Function body in memory:
Pass by value (default in c): fig_07_20
Pass by reference: fig_07_21
Information hiding: static qualifier prevents visibility outside this file (even to linker): fig_07_22
Function documentation: template for header fig_07_23
We can also define pointers to functions: Dereferencing methods: fig_07_24
Example: fig_07_26
Example 2: fig_07_27
Pointing to add: fig_07_28
Pointing to subtract: pointer does not know functionality, only address fig_07_29
User-defined data structure: struct fig_07_31
User-defined data structure: struct fig_07_30 Example:
Can also define a type and use it repeatedly: fig_07_34
Syntax for using struct: fig_07_35
Example: define a rectangle fig_07_36
One struct using another: fig_07_37
C code for this example: fig_07_38
Defining the rectangle type: fig_07_39
Using point and rectangle definitions: fig_07_40
Rectangle functions: fig_07_41
Example program: fig_07_42
Example: passing a struct to a function: fig_07_43
Interrupt service routines: ISR—needs to be SHORT and SIMPLE fig_07_44
How an interrupt occurs: Interrupt may be disabled under certain conditions fig_07_45
Enable / disable control mechanisms: Global Masking method 1: use priorities method 2: use mask register (bitwise masks) Note: interrupts are transient, if we choose to ignore one we may not be able to service it later fig_07_46
- Slides: 66