Slides created by: Professor Ian G. Harris Typical Embedded C Program #include main() { //...
-
Upload
laurence-smith -
Category
Documents
-
view
222 -
download
0
Transcript of Slides created by: Professor Ian G. Harris Typical Embedded C Program #include main() { //...
Slides created by: Professor Ian G. Harris
Typical Embedded C Program
#include <stdio.h>
main() { // initialization code
while (1) { // main code }}
#include is a compiler directive to include (concatenate) another filemain is the function where execution starts
Slides created by: Professor Ian G. Harris
Header Files
Files included at the top of a code fileTraditionally named with .h suffixInclude information to be shared between files
• Function prototypes• externs of global variables• Global #defines
Needed to refer to libraries
Slides created by: Professor Ian G. Harris
Function Calls
Functions enable simple code reuseControl moves to function, returns on completionFunctions return only 1 value
main() { int x; x = foo( 3, 4); printf(“%i\n”, x);}
int foo(int x, int y) { return (x+y*3); }
Slides created by: Professor Ian G. Harris
Function Call Overhead
main() { int x; x = foo(2); printf(“%i\n”, x);}int foo(int x) { int y=3; return (x+y*3); }
Program counter value needs to be restored after callLocal variables are stored on the stackFunction calls place arguments and return address on the stack
20:21:22:
30:31:
103: 3 local var
102: 2 argument
101: 21 return addr
100: 2 local var
Slides created by: Professor Ian G. Harris
Variables
Static allocation vs. Dynamic allocationStatic dedicates fixed space on the stackDynamic (malloc) allocates from the heap at runtimeType sizes depend on the architecture
•On x86, int is 32 bits•On ATmega2560, int is 16 bits•char is always 8 bits
Slides created by: Professor Ian G. Harris
Variable Base Representation
Base 10 is defaultBase can be specified with a prefix before the numberBinary is 0b, Hexadecimal is 0x
Ex. char x = 0b00110011;
char x = 0h33;Binary is useful to show each bit valueHex is compact and easy to convert to binary
1 hex digit = 4 binary digits
Slides created by: Professor Ian G. Harris
Volatile Variables
The value of a volatile variable may change at any time, not just at an explicit assignmentCompiler optimizations are not applied to volatile variables
When can variables change without an explicit assignment?
1. Memory-mapped peripheral registers
2. Global variables modified by an interrupt service routine
3. Global variables accessed by multiple tasks within a multi-threaded application
Slides created by: Professor Ian G. Harris
Volatile Example
.
.while (*periph != 1); // wait until data transfer. // is complete.
periph is the mapped address of the peripheral status info*periph is assigned by peripheral directly
Compiled code will move memory contents to a registerMemory will only be moved once because *periph does not change
Slides created by: Professor Ian G. Harris
Bitwise Operations
Treat the value as an array of bitsBitwise operations are performed on pairs of
corresponding bits
X = 0b0011, Y = 0b0110Z = X | Y = 0b0111Z = X & Y = 0b0001Z = X ^ Y = 0b0101Z = ~X = 0b1100Z = X << 1 = 0b0110Z = x >> 1 = 0b0001
Slides created by: Professor Ian G. Harris
Bit Masks
Need to access a subset of the bits in a variable• Write or read
Masks are bit sequences which identify the important
bits with a ‘1’ valueEx. Set bits 3 and 5 or X, don’t change other bits
X = 01010101, mask = 0010100
X = X | maskEx. Clear bits 2 and 4
mask = 11101011
X = X & mask
Slides created by: Professor Ian G. Harris
Bit Assignment Macros
1 << (n) and ~(1) << (n) create the mask• Single 1 (0) shifted n times
Macro doesn’t require memory access (on stack)
#define SET_BIT(p,n) ((p) |= (1 << (n)))
#define CLR_BIT(p,n) ((p) &= (~(1) << (n)))
Slides created by: Professor Ian G. Harris
Embedded Toolchain
A toolchain is the set of software tools which allow
a program to run on an embedded system Host machine is the machine running the toolchain Target machine is the embedded system where the
program will execute• Host has more computational power then target
We are using the GNU toolchain• Free, open source, many features
Slides created by: Professor Ian G. Harris
Cross-Compiler
A compiler which generates code for a platform
different from the one it executes on• Executes on host, generates code for target
Generates an object file (.o) Contains machine instructions References are virtual
• Absolute addresses are not yet available• Labels are used instead
Slides created by: Professor Ian G. Harris
Cross-Compiler Example
ABBOTT.o…MOVE R1, (idunno)CALL whosonfirst…
ABBOTT.cint idunno;…whosonfirst(idunno)…
Cross- compiler
COSTELLO.cint whosonfirst(int x){…}
Cross- compiler
COSTELLO.o……whosonfirst:…
Idunno, whosonfirst
Unknown addresses
Slides created by: Professor Ian G. Harris
Linker
Combines multiple object files References are relative to the start of the executable Executable is relocatable Typically need an operating system to handle
relocation
Slides created by: Professor Ian G. Harris
Linker Example
ABBOTT.o…MOVE R1, (idunno)CALL whosonfirst…
COSTELLO.o…whosonfirst:MOVE R5, R1…
HAHA.exe…MOVE R1, 2388CALL 1547…MOVE R5, R1…(value of idunno)
1547
2388
Linker
Functions are merged
Relative addresses used
Slides created by: Professor Ian G. Harris
Linker/Locator
Links executables and identifies absolute physical
addresses on the target Locating obviates the need for an operating system Needs memory map information
• Select type of memory to be used (Flash, SRAM, …)• Select location in memory to avoid important data (stack,
etc.)• Often provided manually
Slides created by: Professor Ian G. Harris
Segments
Data in an executable is typically divided into segments Type of memory is determined by the segment Instruction Segment - non-volatile storage Constant Strings – non-volatile storage Uninitialized Data – volatile storage Initialized Data – non-volatile and volatile
• Need to record initial values and allow for changes
Slides created by: Professor Ian G. Harris
AVR GNU Toolchain
Cross-Compiler: avr-gcc Linker/Locator: avr-ld Cross-Assembler: avr-as Programmer: avrdude
All can be invoked via AVR Studio 5
Slides created by: Professor Ian G. Harris
ATmega 2560 Pins
Fixed-Use pins• VCC, GND, RESET• XTAL1, XTAL2 - input/output for crystal oscillator• AVCC - power for ADC, connect to VCC• AREF - analog reference pin for ADC
General-Purpose ports• Ports A-E, G, H, J, L• Ports F and K are for analog inputs• All ports are 8-bits, except G (6 bits)
Slides created by: Professor Ian G. Harris
I/O Pins, Output Path
DDRx
PORTx
Slides created by: Professor Ian G. Harris
I/O Pins, Input Path
PINx
Slides created by: Professor Ian G. Harris
I/O Control Registers
DDRx – Controls the output tristate for port x• DDRx bit = 1 makes the port x an output pin• DDRx bit = 0 makes the port x an input pin• Ex. DDRA = 0b11001100, outputs are bits 7, 6, 3, and 2
PORTx – Control the value driven on port x• Only meaningful if port x is an output• Ex. PORTA = 0b00110011 assigns pin values as shown
PINx – Contains value on port x• Ex. Q = PINC;
Slides created by: Professor Ian G. Harris
Test and Debugging
Controllability and observability are required
Controllability• Ability to control sources of data used by the system• Input pins, input interfaces (serial, ethernet, etc.)• Registers and internal memory
Observability• Ability to observe intermediate and final results• Output pins, output interfaces• Registers and internal memory
Slides created by: Professor Ian G. Harris
I/O Access is Insufficient
Control and observation of I/O is not enough to debug
main(){ x = f1(RA0,RA1); foo (x);}
foo(x){ y = f2(x); bar (y);}
bar(y){ RA2 = f3(y);}
RA0
RA1RA2
If RA2 is incorrect, how do you locate the bug?Control/observe x and y at function calls?
Slides created by: Professor Ian G. Harris
Embedded Debugging
Properties of a debugging environment:
1. Run Control of the target- Start and stop the program execution
2. Ability to change code and data on target- Fix errors, test alternatives
3. Real-Time Monitoring of target execution- Non-intrusive in terms of performance
4. Timing and Functional Accuracy- Debugged system should act like the real system
Slides created by: Professor Ian G. Harris
Host-Based Debugging
Compile and debug your program on the host system, not target- Compile C to your laptop, not the microcontroller
Advantages:1.Can use a good debugging environment2.Easy to try it, not much setup (register names, etc)
Disadvantages:1.Timing is way off2.Peripherals will not work, need to simulate them3.Interrupts probably implemented differently4.Different data sizes and “endian”ness
Slides created by: Professor Ian G. Harris
Instruction Set Simulator
Instruction Set Simulator (ISS) runs on the host but simulates the targetEach machine instruction on the target is converted into a set of instructions on the host
Example:
Target Instruction - add x: Adds register x to the acc register, result in the acc register
Host equivalent: add acc, x, acc: Adds second reg to third, result in the first reg
Slides created by: Professor Ian G. Harris
ISS Tradeoffs
Advantages:1. Total run control2. Can change code and data easily
Disadvantages:1. Simulator assumptions can cause inaccuracies2. Timing is off, no real-time monitoring
- initial register values, timing assumptions3. “Hardware environment” of target cannot be easily modeled
Slides created by: Professor Ian G. Harris
QuickTime™ and aBMP decompressor
are needed to see this picture.
Hardware Environment
PIC communicates with the switch and the RAMCommunications must be modeled to test PIC codeSimulators allow generation of simple event sequencesResponsiveness is more difficult to model
Slides created by: Professor Ian G. Harris
Remote Debug/Debug Kernel
Remote debugger on the host interacts with a debug kernel on the targetCommunication through a spare channel (serial or ethernet)Debug kernel responds to commands from remote debuggerDebug kernel is an interrupt, so control is possible at any time
Host(PC)
Target (Atmega)
Serial or TCP/IP
Slides created by: Professor Ian G. Harris
Remote Debug Tradeoffs
Advantages:1.Good run control using interrupts to stop execution2.Debug kernel can alter memory and registers3.Perfect functional accuracy
Disadvantages:1.Debug interrupts alter timing so real-time monitoring is not possible2.Need a spare communication channel3.Need program in RAM (not flash) to add breakpoints
Slides created by: Professor Ian G. Harris
ROM Emulator
Common to read instructions from a separate ROM on the target ROM emulator substitutes the ROM for a RAM with a controller
Slides created by: Professor Ian G. Harris
ROM Emulator Features
Remote debugger where ROM is replaced by RAM- Debug kernel is in the RAM
Solves the “non-writable ROM” problem of remote debugging
ROM emulator completely controls the instructions- Full data access is possible
ROM emulator can contain a debug communication channelNo need for a spare channel
Slides created by: Professor Ian G. Harris
ROM Emulator Disadvantages
Instruction ROM must be separate from the microcontroller- No embedded ROM
There must be a way to write to the ROM- May be done with a complex sequence of reads
Alters timing, just as any debug kernel would
Slides created by: Professor Ian G. Harris
In-Circuit Emulation (ICE)
Replace the microcontroller with an new oneCan select instructions from external ROM (normal mode) or internal shadow RAM (test mode)
Slides created by: Professor Ian G. Harris
ICE Advantages
ICE can always maintain control of the program - Interrupt cannot be masked
Works even if system ROM is broken
Generally the best solution
Slides created by: Professor Ian G. Harris
Debouncing Buttons
Micro-controller
Vcc
Input
input
10ms
Mechanical bounce in switch causes signal to bounce
Noticable at MHz clock rates
Need to wait until signal settles before sampling it
Slides created by: Professor Ian G. Harris
Wait to Settle
settletime is the time a button signal must stay constant to be sure that it is settledAfter a signal change, wait settletime clksDebounce rising edge, reset counter every signal change to 0
i = 0;while (i < settletime) {
if (in == 0) i = 0; else i = i + 1;
}
Reset counterAdvance counter
Need to debounce falling edge as well as rising edge
Slides created by: Professor Ian G. Harris
Debouncing Code
while (1 == 1) { i = 0; while (i < settletime) {
if (in == 0) i = 0; else i = i + 1;
} i = 0; while (i < settletime) {
if (in == 1) i = 0; else i = i + 1;
} // perform operation}
Wait for rising edge to settle
Wait for falling edge to settle
Perform Operation