IL2206 L04 IO Programming
IL2206 L04 IO Programming
Outline
Programming in C ! Programming with the HAL Library
!
Programming in C
C has been designed with respect to an efficient implementation, unnecessary features have been left out
!
! !
There is no Boolean variable, integers are used instead of True and False (0 is False, all other integers correspond to True) There is no garbage collection Programmer can access memory locations that are not declared ...
IL2206 Embedded Systems 5
Memory leaks Accessing wrong memory locations Assignment instead of comparison ...
IL2206 Embedded Systems 6
There are many books on C in the library! ! The course page gives recommendations
!
In order to design an efficient program it is often important to know how data structures are located in memory
!
Arryas in memory
An array is located as a sequence in the memory int x = 5; int y = 6; int a[5];
!
0x80 0x84 0x88 0x8C 0x90 0x94 0x98 5 6 x y a[0] a[1] a[2] a[3] a[4]
Pointers
!
5 6
x = 5; y = 6; a[5]; *intpointer;
intpointer
10
Pointers
/* Point to x */ intpointer = &x; /* Set x to 0 */ *intpointer = 0;
0x80 0x84 0x88 0x8C 0x90 0x94 0x98 0 6 x y a[0] a[1] a[2] a[3] a[4]
0x80
IL2206 Embedded Systems
intpointer
11
0 6
0x88
IL2206 Embedded Systems
intpointer
12
0 6
0x88
IL2206 Embedded Systems
intpointer
13
0x90
intpointer
14
Bitwise complement (~) Bitwise and (&) Bitwise exclusive or (^) Bitwise inclusive or (|) Left shift (<<)
!
0s are shifted in for unsigned data types 0s are shifted in for signed data types the sign bit is shifted in
IL2206 Embedded Systems 15
Refreshing C
Relational, Equality and Logical Operators
Relational and Equality
! ! !
Logical
! ! !
! !
Less than (<) Greater than (>) Less than or equal (<=) Greater than or equal (>=) Equal to (==) Not equal to (!=)
16
Storage Classes
!
type (int, char, float, double, !) storage class (auto, extern, register, static)
17
Default storage class for variables declared in a function An automatic variable is only visible in the block it is declared in
!
System allocates memory when entering the block System releases the memory when leaving the block
!
int x; /* storage class extern */ int f(int x) { /* both a and b are */ /* of storage class auto */ int a; auto int b; /* a, b are visible here */ /* x is visible here */ } /* a, b are not visible here */ /* x is visible here */
18
Default storage class for variables declared outside a function An extern variable is visible in the whole file after its declaration (global variable)
!
int x; /* storage class extern */ int f(int x) { /* both a and b are */ /* of storage class auto */ int a; auto int b; /* a, b are visible here */ /* x is visible here */ } /* a, b are not visible here */ /* x is visible here */
19
The keyword extern is used to tell the compiler to look for the variable or function elsewhere
! !
File1.c int a; File2.c int f(int x) { /* look for a outside */ /* this function */ extern int a; x = a; }
20
register storage class may only be declared inside functions Behaves in the same way as auto, but gives an additional recommendation to compiler that variable should be placed in register Compiler does not have to follow this advice
!
21
22
The type qualifier volatile indicates that a variable can be changed by other parts of the hardware in the system
!
Variable should not be put in register Variable can still be put in register or cache! Other directives (HAL functions) have to be used to avoid cache coherence problem
IL2206 Embedded Systems 23
24
A bit-field is a packed representation, where several small variables are integrated into one variable
struct myfield { unsigned var1 : 4; /* values from 0 to 15 */ int var2 : 3; /* values from -4 to 3 */ } x; x.var1 = 6;
A union defines a set of alternative values that can be stored in the same portion of memory Programmer has responsibility to keep track of the data type stored in a union The size of the union corresponds to the largest data type
27
29
30
If you do not feel very comfortable with C! There are many books on C in the library!
31
Busy Wait I/O is the most basic way to communicate with an I/O-device The processor wait until the I/ O-device has completed its current task Disadvantage: Processor cannot be used for other tasks during the waiting period! This method is also often called polling!
33
5 BF
5 BF
peek can be used to read a memory location (byte) char peek(char *location) {return *location;} ! poke can be used to write to a memory location (byte) void poke(char *location, char newval) {*location = newval;}
!
IL2206 Embedded Systems 36
peek can be used to read a memory location (byte) char peek(char *location) Altera provides the {return *location;} HAL library that abstracts from the underlying location (byte) ! poke can be used to write to a memoryhardware! void poke(char *location, char newval) {*location = newval;}
IL2206 Embedded Systems 37
Dont do this!
while (current_char != \0) { poke(Sender, *current_char++); while ((peek(Status) & 0x20) != 0) ; } /* Mask needed, since other bits */ /* in status register may not be zero */
7 0x1000 0x1001
IL2206 Embedded Systems
5 BF
Programmers may make mistakes that the compiler would not do (e.g. memory alignment) HAL (Hardware Abstraction Layer) offers optimized device drivers to access peripheral devices and memory
IL2206 Embedded Systems 39
CPU(s) peripherals
40
43
The system.h file describes each peripheral and provides the following details:
! ! ! !
The hardware configuration of the peripheral The base address The IRQ (interrupt request) priority A symbolic name for the peripheral
If the hardware changes, the source code is still valid, since it uses another correct system.h file
The Parallel I/O devices can be accessed by functions that are defined in altera_avalon_pio_regs. These functions use only symbolic addresses!
#include "system.h" #include "altera_avalon_pio_regs.h" int buttons_pressed(void) { return IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE); } void show_number_on_leds(int x) { IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, x); }
IL2206 Embedded Systems 49
7 0x1000 0x1001
5 BF
50
Programming Interrupt
Foreground Program
Do something! Interrupt Event
!
Interrupt Handler
! ! ! ! !
Save Registers Handle Interrupt Restore Registers Restore PC Clear interrupt disable flag
Interrupt Vector
!
The prototype has the following parameters: ! id is the hardware interrupt number for the device, as defined in system.h. Interrupt priority corresponds inversely to the IRQ number. Therefore, IRQ 0 represents the highest priority interrupt and IRQ 31 is the lowest. ! context is a pointer used to pass context-specific information to the ISR, and can point to any sort of ISR-specific information. The context value is opaque to the HAL; it is provided entirely for the benefit of the user-defined ISR ! isr is the function that is called in response to IRQ number id. The two input arguments provided to this function are the context pointer and id. Registering a null pointer for isr results in the interrupt being disabled ! If your ISR is successfully registered, the associated interrupt (as defined by id) is enabled on return from alt_irq_register()
IL2206 Embedded Systems 53
55
What happens?
!
1. 2.
3. 4.
How data is arranged in memory Behavior and structure of the stack Function calling conventions
57
58
To increase portability the following HAL standard data types are defined There is no defined size for C-data types in ANSI C
!
An int reflect usually the natural size of integers on the host machine (Kernighan and Ritchie, 1988)
59
an int requires 4 bytes, a char only 1 byte if only values between 0 and 10 are needed use a char (or even better the corresponding Altera type)
Think Hardware!
IL2206 Embedded Systems 60
Memory Alignment
!
! !
A function must be aligned to a minimum of 32-bit boundary. The minimum alignment of a data element is its natural size. A data element larger than 32-bits need only be aligned to a 32-bit boundary. Structures, unions, and strings must be aligned to a minimum of 32 bits. Bit-fields inside structures are always 32-bit aligned.
IL2206 Embedded Systems 61
Memory Alignment
0x00 0x04 0x08 0x0C 0x10 0x14
Byte Byte Short Short int int Byte Byte Short Short Byte
62
Register Usage
!
The ABI defines how the C compiler uses registers. If C and Assembler shall work together the ABI must be followed:
63
Register Usage
!
It is the responsibility of the calling function to save the following registers, the called function may freely use these registers without saving them on the stack:
64
Register Usage
!
If the called function wants to use the following registers, it has to save them on the stack first
65
Register Usage
!
There are many other important registers, like the stack pointer, and the return address
66
The stack grows downwards (i.e. to lower addresses) The stack pointer points to the last used slot The frame pointer points usually to the same location as the stack pointer
67
This applies for functions with not more than four 32-bit arguments
IL2206 Embedded Systems 68
Note: No need to save stack pointer or callee-saved registers (not used) or return address (leaf function, it does not call other functions)
70
Summary
Good understanding of underlying hardware is critical for design of efficient software ! HAL library provides abstraction from hardware and allows fast adaptation to changes in hardware ! Application Binary Interface defines a standard for communication between C- and Assembler parts
!
IL2206 Embedded Systems 71