Upload
hakhue
View
227
Download
3
Embed Size (px)
Citation preview
Embedded Processor Basics (I)
James Barnes ([email protected])
Colorado State University
Department of Electrical and Computer Engineering
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 1
Outline
1) Overview of Two Processors (ATmel ATmega328P, AT91SAM)
2) Development Environments
– Exercise 1: Install Development Environment
3) Block Diagram, Instruction and Data Flow, Clocking and Reset
– Exercise 2: Compile and Upload Programs
4) Memory Organization
5) Number Representations
– Exercise 3: Develop code to detect integer overflow
6) Instruction Set and Program Flow
– Exercise 4: Trace program flow for simple function call
January 2013 Colorado State University, Department of
Electrical and Computer Engineering 2
References Used
• ATmega328P datasheet (AVR core)
– www.atmel.com/devices/atmega328p.aspx
• AT91SAM datasheet (ARM Cortex core)
– www.atmel.com/products/microcontrollers/arm/default.aspx
• Introduction to Embedded Systems Using ANSI C and the Arduino Development Environment, David J. Russell, ISBN 9781608454990 ebook
– one in a series of books, all downloadable from Morgan Library, listed in bibliography
• History of the AVR cpu core, Atmel involvement, links to Arduino
– en.wikipedia.org/wiki/Atmel_AVR
• Wikipedia article on AVR cpu core
– en.wikipedia.org/wiki/Atmel_AVR_instruction_set
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 3
Why These Processors?
• Both used in Arduino boards http://www.arduino.cc/
– “Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments “
• Another useful reference re Arduinohttp://en.wikipedia.org/wiki/Arduino
– Gives history of Arduino going back to Processing open project at MIT Media Lab and the Wiring Language and IDE which Arduino uses
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 4
Atmel ATmega328P Arduino Boards used in EECL
Seeeduino Stalker board
– Zigbee communication, battery-powered
– Used in building monitor project to track building occupancy
Arduino Ethernet board
– Ethernet communication, POE (Power Ever Ethernet)
– Used in Integrid resource monitoring
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 5
AT91SAM Arduino Board
• Arduine Due board
[Ref: http://arduino.cc/en/Main/ArduinoBoardDue]
• Has 32b ARM Cortex core (Atmega328P has 8b “AVR” core)
– Not suited for battery power
– Used in future projects which require more processing power
than the 328P can deliver
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 6
Atmel ATmega328P Datasheet
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 7
Ref: http://www.atmel.com/Images/doc8161.pdf
Atmel ATmega328P Datasheet (cont’d)
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 8
Ref: http://www.atmel.com/Images/doc8161.pdf
ATmel AT91SAM Datasheet
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 9
Ref: http://www.atmel.com/Images/doc11057.pdf
ATmel AT91SAM Datasheet (cont’d)
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 10
Ref: http://www.atmel.com/Images/doc11057.pdf
Outline
1) Overview of Two Processors (ATmel ATmega328P, AT91SAM)
2) Development Environments
– Exercise 1: Install Development Environment
3) Block Diagram, Instruction and Data Flow, Clocking and Reset
– Exercise 2: Compile and Upload Programs
4) Memory Organization
5) Number Representations
– Exercise 3: Develop code to detect integer overflow
6) Instruction Set and Program Flow
– Exercise 4: Trace program flow for simple function call
January 2013 Colorado State University, Department of
Electrical and Computer Engineering 11
Arduino Development Environments
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 12
Original Arduino Environment • Reads in *.pde file format
• Does not allow creation of custom
libraries
• Complete package, includes compile
tools
Eclipse IDE with Arduino plug-in • Reads in eclipse project setup file and standard C++
files
• Allows development of custom libraries
• Requires installation of AVR gcc compiler and
Arduino libraries.
• Requires a bit more work to write and compile
programs (well documented by Sam Kemp)
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 13
Simple Arduino Program (“Blink”) Using WIRING Library Ref: Russell, op. cit., pp 82-85
#include “Arduino.h” // This file only in Arduino-1.0, not -0022
#include <util/delay.h>
int ledPin = 13;
void setup () {
pinMode(ledPin , OUTPUT); // pinMode()is an Arduino library function
// OUTPUT defined in Arduino.h include
}
void loop () { // HIGH is defined in Arduino.h include
digitalWrite(ledPin , HIGH); // digitaWrite()is Arduino library function
delay(1000); // delay()is an Arduino library function
digitalWrite(ledPin , LOW);
delay(1000);
}
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 14
What’s happening behind the scene (both IDEs) Ref: Russell, op. cit., pp195-198
#include … // user includes, global variable declarations moved by IDE
#include “WProgram.h”
void setup(); // prototype declarations added by IDE
void loop();
int main(void) { // main() and execution loop added by Arduino library
init(); // Eveutually we will dispnese with all of this
setup(); // by creating our own libs
for (;;) {
loop();
}
return 0;
}
void setup(void) { // user-defined code
…
}
void loop(void) { // user-defined code
…
}
The Arduino IDE or Eclipse plug-in adds a wrapper to the user code
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 15
What’s happening behind the scene (both IDEs) Program Compilation and Upload
{PATH}/bin/avr-gcc -g -Wall -O2 -mmcu=atmega328p -c <file>.c
{PATH}/bin/avr-gcc -g -Wall -O2 -mmcu=atmega328p –o <file>.elf <file>.o
{PATH}/bin/avr-objcopy -j .text -j .data -O ihex <file>.elf <file>.hex
Commands run when you click “build”
Command run when you click “upload”
{PATH}/bin/avrdude -C [conf file] -p m328p -c stk500v1 -P [serialport] –b 57600 -D –U
flash:w:<file>.hex:i
Extra: command to disassemble (for looking at assembly code created)
{PATH}/bin/avr-objdump -h -S <file>.elf > <file>.lst
Debugging Using Serial Interface
• Standard debuggers such as gdb do not work with cross compilation
– Need a special hardware translator to give necessary status information to gdb
• Lacking that, debugging for embedded processors relies on “print” statements placed in core. The “printing” is done to the serial interface.
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 16
Exercise 1 – Setup Development Environment – Part 1
• Complete the first section, “Download and Installation” of the
document Eclipse_Install.pdf
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 17
Outline
1) Overview of Two Processors (ATmel ATmega328P, AT91SAM)
2) Development Environments
– Exercise 1: Install Development Environment
3) Block Diagram, Instruction and Data Flow, Clocking and Reset
– Exercise 2: Compile and Upload Programs
4) Memory Organization
5) Number Representations
– Exercise 3: Develop code to detect integer overflow
6) Instruction Set and Program Flow
– Exercise 4: Trace program flow for simple function call
January 2013 Colorado State University, Department of
Electrical and Computer Engineering 18
ATmega328P Top-level Block Diagram
• 328P is really an “SOC” (system on a chip)
– CPU PLUS clock generator, memory, ADC, timer/counters, EEPROM, serial ports.
• “Harvard” architecture (separate program and data memory).
• Peripheral blocks communicate with CPU via 8b DATABUS
• Configuration of peripherals through Control/Status Regs (CSRs).
• Memory-mapped addresses. Read/write to a memory location.
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 19
ATmega328P AVR Core Block Diagram
• ALU operates only on GP Reg values.
– Load/store instructions to get data between SRAM and GP regs.
• 32KB flash prog memory, but 2KB reserved for bootloader.
– Bootloader configures serial port for loading programs, also jumps to program start location after load, reset, or power cycle.
– If the bootloader memory section is corrupted, the board cannot be loaded. Need special hardware to restore functionality.
• Only 2KB to store variables (“data memory”). C long integer and C float are expensive memory-wise.
• Variable values which must persist after power off can be saved to the 1KB EEPROM.
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 20
Instruction Fetch/Decode/Execute Cycle
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 21
• Overlapping instr fetch and
execute of previous instruction
• Instruction addresses can come from
program counter or GP reg array
• Execute operation takes one clock
cycle
Data Memory R/W Access Cycle
• One clock cycle to decode
address and drive word
line.
• One clock cycle to transfer
SRAM <-> CP register file
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 22
System Clocking
• Five possible clock sources
– Some can be shut down for power savings
• Default clock source for CPU is calibrated RC oscillator but can be overridden by flash fuses (by board manufacturer)
– Stalker board uses 8MHz crystal oscillator or can use internal RC oscillator for power savings
– Ethernet board uses 16MHz crystal
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 23
System Reset
• Loaded program executes
after reset
• Reset occurs when
– Power applied
– Recovery from voltage
droop (“brownout”)
– Program upload
completes
– External reset button
pushed
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 24
Exercise 2 – Compile and Upload Programs
• Complete the final three sections of the document
Eclipse_Install.pdf
– “Building the Library” – Since we may be using different
Arduino boards, you may need help choosing which library to
build.
– “Generating An Executable” - Choose at least the “Hello World”
example, on the next slide, as it shows serial communication
between the board and your computer. If you have time, create a
new project and build the “Blink” example, also on the next slide
– “Upload the Project to Target Device”
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 25
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 26
Example Programs
#include <util/delay.h>
#include "Arduino.h"
#define LED 9 // Nbr will vary
void setup() {
// initialize the digital pin as output.
// Pin 9 has an LED on THIS Arduino board
pinMode(LED, OUTPUT);
}
void loop() {
// turn the LED on
digitalWrite(LED, HIGH);
// wait for a second
delay(1000);
// turn the LED off
digitalWrite(LED, LOW);
// wait for a second
delay(1000);
}
#include <util/delay.h> #include "Arduino.h” void setup() { Serial.begin(9600); } void loop() { String msg = "Hello World Number "; static uint8_t i = 0; char sint[5]; itoa(i,sint,10); Serial.println(msg+sint); i++; delay(1000); }
“Hello World” Main.cpp “Blink” Main.cpp
Outline
1) Overview of Two Processors (ATmel ATmega328P, AT91SAM)
2) Development Environments
– Exercise 1: Install Development Environment
3) Block Diagram, Instruction and Data Flow, Clocking and Reset
– Exercise 2: Compile and Upload Programs
4) Memory Organization
5) Number Representations
– Exercise 3: Develop code to detect integer overflow
6) Instruction Set and Program Flow
– Exercise 4: Trace program flow for simple function call
January 2013 Colorado State University, Department of
Electrical and Computer Engineering 27
Program Memory Organization
AVR instructions are all 16b or 32b wide
Program memory organized as 16K x 16b words (upper 1K words for bootloader)
14b Instruction Address Register (addresses entire flash, including bootloader, but bootloader section is “protected”).
Prog memory R/W access is via 64 word Pages. Temporary buffer used to build pages before writing. A page is erased before writing prog memory from the temp buffer.
Bootloader will load IAR with Addr 0 after program load, PON, or reset.
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 28
Data Memory Organization
• GP Regs and control regs (CSRs) can be read/written using short (16b) pointers to the address (examples later).
• Gray region is accessible via “extended addressing”. Instruction address field is only capable of directly addressing white region.
• Generally the AVR-GCC compiler can handle GP Reg access.
• Setup of MOST common peripheral functions can be done through WIRED library routines which handle the low-level CSR access, but occasionally it is necessary to use pointer access.
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 29
• Holds ALL program variables
• SRAM memory, byte-addressable
• The 32 8b CPU general purpose
registers and 224 8b control
registers are memory-mapped
starting at address 0.
GP Register File
• The X, Y, and Z registers
are used for indirect
addressing of the data
memory space.
• X,Y,Z regs appear in some
of the machine instructions
and must be used for
extended addressing.
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 30
Control/Status Register Example:
Status Register (SREG)
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 31
• Bit 7 – I: Global Interrupt Enable
• Bit 6 – T: Bit Copy Storage
• Bit 5 – H: Half Carry Flag
• Bit 4 – S: Sign Bit, S = N ⊕ V
• Bit 3 – V: Two’s Complement Overflow
Flag
• Bit 2 – N: Negative Flag
• Bit 1 – Z: Zero Flag
• Bit 0 – C: Carry Flag
Stack and Stack Pointer Register
Stack is used to store temporary data, local variables, context (for interrupt calls), and return addresses for function and interrupt calls
Stack starts at highest address of
Data Memory and grows downward Stack pointer (0x5E,0x5D) is 16b
wide (14b are used) and points to top of stack (lowest occupied address minus 1).
Every time a byte is pushed on
stack, the SP is decremented.
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 32
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 33
Stack Pointer Experiment
#include "Arduino.h” #define HEX 16 #define SPH_ADDR 0x5E #define SPL_ADDR 0x5D signed int my_add(int, int); signed int my_add3(int, int, int); void print_sp(); void print_spl(); void setup() { Serial.begin(9600); Serial.print("Starting "); print_sp(); signed int a; signed int b; signed int c; signed int d; // not good style a=2; b=3; c=4; // not good style Serial.print("after c=4; "); print_sp(); d=my_add(a,b); Serial.print("Return from d=my_add(a,b) "); print_sp(); d = my_add3(a,b,c); Serial.print("Return from d=my_add3(a,b,c) "); print_sp(); Serial.println(); } void loop() { } signed int my_add(int x, int y) { Serial.print("In my_add "); print_sp(); return x+y; } signed int my_add3(int x, int y, int z) { Serial.print("In my_add3 "); print_sp(); return x+y+z; }
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 34
Function print_sp() and Output of Program
void print_sp() {
unsigned char * sph = (unsigned char *) SPH_ADDR ;
unsigned char * spl = (unsigned char *) SPL_ADDR ;
Serial.print(*sph,HEX);
Serial.print("\t");
Serial.print(*spl,HEX);
Serial.println();
}
Starting 8 F3
after c=4; 8 F3
In my_add 8 ED
Return from d=my_add(a,b) 8 F3
In my_add3 8 EB
Return from d=my_add3(a,b,c) 8 F3
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 36
Direct Access of Control Registers:
ANSI C Version of “Blink” Ref: Russell, op. cit., pp 82-85
#include “Arduino.h”
#include <util/delay.h>
void setup () {
unsigned char * portDDRB ;
portDDRB = (unsigned char *) 0x24;
*portDDRB |= 0x20; // Equiv to *portDDRB = *portDDRB | 0x20
}
void loop () {
unsigned char * portB ;
portB = (unsigned char *) 0x25;
*portB |= 0x20; // 0x20 = 0010 0000
MyDelay (1000);
*portB &= 0xDF;
MyDelay (1000);
}
void MyDelay (unsigned long mSecondsApx ) {
volatile unsigned long i;
unsigned long endTime = 1000 * mSecondsApx ;
for (i = 0; i < endTime ; i++);
}
PORTB/DDRB Registers Ref: Russell, op. cit., p 108
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 37
Port B Data Pin Connections to Board Connector (Seeeduino Stalker V2.0)
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 38
Stalker Board Schematic
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 39
Seeeduino Stalker V2.0Schematic
(Zoomed View of uProc and Connector Pins)
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 40
Outline
1) Overview of Two Processors (ATmel ATmega328P, AT91SAM)
2) Development Environments
– Exercise 1: Install Development Environment
3) Block Diagram, Instruction and Data Flow, Clocking and Reset
– Exercise 2: Compile and Upload Programs
4) Memory Organization
5) Number Representations
– Exercise 3: Develop code to detect integer overflow
6) Instruction Set and Program Flow
– Exercise 4: Trace program flow for simple function call
January 2013 Colorado State University, Department of
Electrical and Computer Engineering 41
Arduino Data Types
Type Width
(bytes)
Range
bool 1 0 to 1
byte 1 0 to 255
char, signed char, int8_t, signed int8_t 1 -128 to 127
unsigned char, uint8_t, unsigned int8_t 1 0 to 255
short, signed short 2 -32,768 to 32,767
unsigned short 2 0 to 65,535
int, signed int, int16_t, signed int16_t 2 -32,768 to 32,767
unsigned int, uint8_t, unsigned int8_t 2 0 to 65,535
long, signed long, int32_t, signed int32_t 4 -2,147,483,648 to 2,147,483,647
unsigned long, uint32_t, unsigned int32_t 4 0 to 4,294,967,295
float 4 -3.4028235E+38 to 3.4028235E+38
double 4 -3.4028235E+38 to 3.4028235E+38
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 42
Conserving Data Memory
• long, float, and double are
expensive in mem usage (and
float/double arithmetic is slow)
• bool type does not save memory
over byte or char because mem is
byte-addressable
• Arrays of bool do not pack; each
bit requires one byte of mem
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 43
Overflow vs Carryout for Integers
January 2013 Colorado State University, Department of Electrical and
Computer Engineering 44
For simplicity, assume 4 bit integers (not a defined C variable type)
0
2
1
4 3
F
5 6
7
8
9
A
C B E
D
unsigned
0
2
1
4 3
-1
5 6
7
-8
-7
-6
-4 -5 -2
-3
signed 2’s
complement
• Can only get overflow
when both ops have
same sign.
• Overflow occurs when
result has different sign
from ops.
• Carryout does not
matter
• Can’t do subtraction with
both operands unsigned.
• For addition, overflow
occurs when carryout
occurs.
Ref: Russell
p 13
• Test for overflow:
((a+b) < a) or ((a+b) < b)
Works for both signed and unsigned integers
Casting (Type Conversion)
• Casting is conversion from one data type to another. Conversion to a wider type is
done according to padding rules and is relatively safe (if you understand the padding
rules)
– unsigned to unsigned
– signed to signed
• Casting to a narrower type involves truncation of upper bits and can result in error if
the upper bits were not created by padding or sign extension
• Casting from signed to unsigned or vice versa can lead to unexpected results
– Ex: signed negative to unsigned results in a large positive number
– Ex: unsigned positive large number to signed results in small negative number.
January 2013
Colorado State University, Department of Electrical and Computer
Engineering 45
unsigned int8_t a = 0xFF;
unsigned int16_t b;
b = (unsigned int16_t) a; // b = 0x00FF, i.e. zero padding of upper bits
signed int8_t a = 0xFF;
signed int16_t b;
b = (signed int16_t) a; // b = 0xFFFF, i.e. sign extension of upper bits
IEEE Single Precision Floating Point Format
• Single precision has 6-8 decimal places of precision
• The actual exponent used in computation is 2e-127 so the real range of
the exponent is [2-127, 2128]
• The mantissa has an implied 1, i.e. all 0’s in the mantissa field
implies 1.00000000…
• The floating point hardware does the adjustments on the exponent
and mantissa before doing an floating point operation. The
adjustments are undone before writing a result back to memory
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 46
Categories of IEEE Floating Point Numbers
• Only Normal and Zero are considered valid floating point
numbers. A processor used in general purpose computing will
throw a floating point exception for the other types.
• The AVR processor apparently does support the IEEE standard,
although it may not enforce Normal
– The maximum number is 3.4028235E+38 , which equals (2 – 2-23)*2127
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 47
Exercise 3
• Write a simple function my_add() to test for signed and
unsigned integer addition overflow. Return both the overflow
test result (True/False) and the sum. Hint: you will need to use
a pointer. Verify proper operation, including, for the signed
case, both positive and negative operands.
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 48
Outline
1) Overview of Two Processors (ATmel ATmega328P, AT91SAM)
2) Development Environments
– Exercise 1: Install Development Environment
3) Block Diagram, Instruction and Data Flow, Clocking and Reset
– Exercise 2: Compile and Upload Programs
4) Memory Organization
5) Number Representations
– Exercise 3: Develop code to detect integer overflow
6) Instruction Set and Program Flow
– Exercise 4: Trace program flow for simple function call
January 2013 Colorado State University, Department of
Electrical and Computer Engineering 49
Instruction Set and Assembly Language
• Why care about this?
– Rarely, need to code in assembly because the compiler code is
not “good” enough (small enough, fast enough, or both).
• Inline assembly (short sections embedded in mostly C/C++) allows
selective use of assembly in a mostly C/C++ environment
– Probably more useful in debugging, since we don’t have
software debugger. Can see exactly what the chip is doing.
• References
– Atmel ATmega328p Datasheet http://www.atmel.com/Images/doc8161.pdf
– AVR Instruction Set Manual http://www.atmel.com/images/doc0856.pdf
– Russell, Chapter 11.
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 50
Compilation Process
• The compiler output file.s is human-readable “assembly language”.
• Programming in assembly gives ultimate control over the hardware.
• Assembly language may have some concessions for the human programmer, but there is a 1 to 1 relationship between assembly lines and machine operations
• Machine instruction set assembly language set
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 51
Sample of Binary Instruction Format
• These 16b words are divided into instruction and operand fields. They are decoded by logic to access registers, control ALU operations, etc.
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 52
Assembly Language Summary in AVR Document
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 53
AVR Assembly Language Reference
• Good reference: http://www.atmel.com/images/doc0856.pdf
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 54
January 2013 Colorado State University, Department of Electrical and Computer
Engineering 55
Exercise 4: Trace Assembly Language Data Flow for
Function Call my_add() (Handout)
#include "Arduino.h" #define HEX 16 #define SPH_ADDR 0x5E #define SPL_ADDR 0x5D signed int my_add(int, int); void print_sp(); void setup() { Serial.begin(9600); Serial.print("Starting "); print_sp(); signed int a; signed int b; signed int c; signed int d; // not good style a=0x1234; b=0x2345; c=0x3456; d=0x4567; // not good style Serial.print("after d=.. "); print_sp(); d=my_add(a,b); Serial.print("Return from d=my_add(a,b) "); print_sp(); Serial.print(c,HEX); Serial.println(); } void loop() { } signed int my_add(int x, int y) { Serial.print("In my_add "); print_sp(); return x+y; } void print_sp() { unsigned char * sph = (unsigned char *) SPH_ADDR ; unsigned char * spl = (unsigned char *) SPL_ADDR ; Serial.print(*sph,HEX); Serial.print("\t"); Serial.print(*spl,HEX); Serial.println(); }