EE 319K
Introduction to Embedded Systems
Lecture 2: I/O, Logic/Shift
Operations, Addressing modes,
Memory Operations, Subroutines,
Introduction to C
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-1
Agenda
Recap
Embedded systems
Product life cycle
ARM programming
Outline
Input/output
Logical/shift operations
Addressing modes, memory operations
Stack and subroutines
Introduction to C
o Structure of a C program
o Variables, expressions and assignments
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-2
Input/Output: TM4C123
C o rtex M 4
S ystick
S y stem B us Interface
N V IC
G P IO P o rt A
PA 7
PA 6
PA 5 /S S I0 T x
PA 4 /S S I0 R x
PA 3 /S S I0 F ss
PA 2 /S S I0 C lk
PA 1 /U 0 T x
PA 0 /U 0 R x
PC7
PC6
PC5
PC4
P C 3 /T D O /S W O
P C 2 /T D I
P C 1 /T M S /S W D IO
P C 0 /T C K /S W C L K
E igh t
U A RTs
Four
I2 C s
F ou r
S S Is
C A N 2.0
G P IO P o rt C
G P IO P o rt D
U S B 2.0
Tw elve
Tim ers
JTA G
S ix
64 -bit w id e
G P IO P o rt E
PE5
PE4
PE3
PE2
PE1
PE0
G P IO P o rt B
ADC
2 ch an nels
1 2 in pu ts
12 bits
A d van ced H igh P erfo rm ance B us
PB7
PB6
PB5
PB4
P B 3 /I2 C 0S D A
P B 2 /I2 C 0S C L
PB1
PB0
PD 7
PD 6
PD 5
PD 4
PD 3
PD 2
PD 1
PD 0
G P IO P o rt F
Tw o A nalog
C om parators
Tw o P W M
M od ules
PF4
PF3
PF2
PF1
PF0
6 General-Purpose
I/O (GPIO) ports:
• Four 8-bit ports
(A, B, C, D)
• One 6-bit port (E)
• One 5-bit port (F)
A d van ced P eripheral B us
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-3
I/O Ports and Control Registers
R ead from port address
n
n
P ro cesso r
n
GPIO_PORTF_DATA_R
n
D Q
W rite to port address
D irectio n b its
n
D Q
B us
Inp u t/O utp u t P o rt
GPIO_PORTF_DIR_R
1 m ean s ou tpu t
0 m ean s in put
W rite to port directio n register
 The input/output direction of a bidirectional
port is specified by its direction register.
GPIO_PORTF_DIR_R , specify if corresponding
pin is input or output:
 0 means input
 1 means output
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-4
I/O Ports and Control Registers
Address
7
6
5
4
3
2
1
0
Name
400F.E608
-
-
GPIOF
GPIOE
GPIOD
GPIOC
GPIOB
GPIOA
SYSCTL_RCGCGPIO_R
4002.53FC
-
-
-
DATA
DATA
DATA
DATA
DATA
GPIO_PORTF_DATA_R
4002.5400
-
-
-
DIR
DIR
DIR
DIR
DIR
GPIO_PORTF_DIR_R
4002.5420
-
-
-
SEL
SEL
SEL
SEL
SEL
GPIO_PORTF_AFSEL_R
4002.551C
-
-
-
DEN
DEN
DEN
DEN
DEN
GPIO_PORTF_DEN_R
• Initialization (executed once at beginning)
1. Turn on clock in SYSCTL_RCGCGPIO_R
2. Wait two bus cycles (two NOP instructions)
3. Set DIR to 1 for output or 0 for input
4. Clear AFSEL bits to 0 to select regular I/O
5. Set DEN bits to 1 to enable data pins
• Input/output from pin
6. Read/write GPIO_PORTF_DATA_R
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-5
Logic Operations
A
Rn
B
Operand2
A&B
AND
A|B
ORR
A^B
EOR
A&(~B)
BIC
A|(~B)
ORN
0
0
0
0
0
0
1
0
1
0
1
1
0
0
1
0
0
1
1
1
1
1
1
1
1
0
0
1
 Logic Instructions
AND{S}
ORR{S}
EOR{S}
BIC{S}
ORN{S}
{Rd,}
{Rd,}
{Rd,}
{Rd,}
{Rd,}
Rn,
Rn,
Rn,
Rn,
Rn,
<op2>
<op2>
<op2>
<op2>
<op2>
;
;
;
;
;
Rd=Rn&op2
Rd=Rn|op2
Rd=Rn^op2
Rd=Rn&(~op2)
Rd=Rn|(~op2)
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-6
To set
The or operation to set bits 1 and 0 of a register.
The other six bits remain constant.
Friendly software modifies just the bits that need to be.
GPIO_PORTD_DIR_R |= 0x03; // PD1,PD0 outputs
Assembly:
LDR R0,=GPIO_PORTD_DIR_R
LDR R1,[R0]
; read previous value
ORR R1,R1,#0x03 ; set bits 0 and 1
STR R1,[R0]
; update
c7 c6 c5 c4 c3 c2 c1 c0
0
0
0
0
0
0
1
1
c7 c6 c5 c4 c3 c2 1
1
value of R1
0x03 constant
result of the ORR
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-7
To toggle
The exclusive or operation can also be used to toggle
bits.
GPIO_PORTD_DATA_R ^= 0x80; /* toggle PD7 */
Assembly:
LDR R0,=GPIO_PORTD_DATA_R
LDR R1,[R0]
; read port D
EOR R1,R1,#0x80 ; toggle bit 7
STR R1,[R0]
; update
b7 b6 b5 b4 b3 b2 b1 b0
value of R1
1
0x80 constant
0
0
0
0
0
0
0
~b7 b6 b5 b4 b3 b2 b1 b0
result of the EOR
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-8
Switch Interfacing
N ot
pressed
+ 3.3V
+ 3.3V
L M 3S or
T M 4C
P ressed
10k
s
L M 3S or
T M 4C
t
Input port
Input port
10k
O pen
C losed
N egative logic
P ositive logic
The and operation to extract, or mask, individual bits:
Pressed = GPIO_PORTA_DATA_R & 0x10;
//true if the PA6 switch pressed
Assembly:
LDR R0,=GPIO_PORTA_DATA_R
LDR R1,[R0]
; read port A
AND R1,R1,#0x40 ; clear all bits except bit 6
LDR R0,=Pressed ; update variable
STR R1,[R0]
; true iff switch pressed
a7 a6 a5 a4 a3 a2 a1 a0
0
0
1 0
a6 0
0
0
0
0
0
0
0
0
0
0
value of R1
0x40 constant
result of the AND
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-9
Shift Operations
31
L o gical S hift R igh t
LSR
30
29
28
27
26
1
0
C
1 < n <32
0
A rithm etic S hift R ight
ASR
L o gical S hift L eft
LSL
1 < n <32
0
R o tate S hift R ig ht
ROR
R o tate R ig ht E xtended
RRX
0 < n <31
1 < n <32
n=1
Use the ASR instruction when manipulating signed numbers,
and use the LSR instruction when shifting unsigned numbers
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-10
Shift Example
High and Low are unsigned 4-bit components, which will be
combined into a single unsigned 8-bit Result.
Result = (High<<4)|Low;
Assembly:
LDR
LDR
LSL
LDR
LDR
ORR
LDR
STR
0
h3
0
h3
R0,=High
R1,[R0]
R1,R1,#4
R0,=Low
R2,[R0]
R1,R1,R2
R0,=Result
R1,[R0]
0 0
h2 h1
0 0
h2 h1
0
h0
0
h0
h3
0
l3
l3
h2
0
l2
l2
; read value of High
; shift into position
; read value of Low
; combine the two parts
; save the answer
h1
0
l1
l1
h0
0
l0
l0
value of High in R1
after last LSL
value of Low in R2
result of the ORR instruction
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-11
Design example
Market survey, profit estimate
Overall function, specifications
Data flow (test)
Flowchart (test)
Software (test)
Simulation, prototype (test)
Build (test)
Input
Output
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-12
Design
Function table (PD0 input, PD3 output)
How to test (switch input, LED output)
Draw a data flow graph
Draw a flow chart
Write pseudo code
Write assembly
Simulate
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-13
Process
 Solution in NOTGate-asm.zip
Start
Activate clock for port
Enable PD3, PD0 pins
Make PD0 (switch) pin an input
Make PD3 (LED) pin an output
Loop
Read input from Switch (0 or 1)
not function LED = 8*(not Switch)
Write output (8 or 0)
Editor
Source code
KeilTM uVision®
Start
; direction register
LDR R1,=GPIO_PORTD_DIR_R
LDR R0,[R1]
ORR R0,R0,#0x0F
; make PD3-0 output
STR R0, [R1]
Start
Debug
Session
Simulated
Microcontroller
Processor
Memory
I/O
Build Target (F7)
Object code
0x00000142
0x00000144
0x00000146
0x0000014A
4912
6808
F040000F
6008
Download
Address Data
Real
Microcontroller
Start
Debug
Session
Processor
Memory
I/O
branch Loop
 Build it, run it, test it with logic analyzer
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-14
ARM Assembly Language
Assembly format
Label Opcode
init MOV
BX
Operands
R0, #100
LR
Comment
; set table size
Comments
 Comments should explain why or how
 Comments should not explain the opcode and its operands
 Comments are a major component of self-documenting
code
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-15
Simple Addressing Modes
Second operand - <op2>
ADD
Rd, Rn, <op2>
Constant
o ADD Rd, Rn, #constant
; Rd = Rn+constant
Shift
o ADD R0, R1, LSL #4
o ADD R0, R1, R2, ASR #4
; R0 = R0+(R1*16)
; R0 = R1+(R2/16)
Memory accessed only with LDR STR
 Constant in ROM:
 Variable on the stack:
 Global variable in RAM:
 I/O port:
=Constant / [PC,#offs]
[SP,#offs]
[Rx]
[Rx]
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-16
Addressing Modes
 Immediate addressing
 Data is contained in the instruction
MOV
R0,#100
; R0=100, immediate addressing
100
R0
EEPRO M
PC
0x00000266
0x00000260
0x00000264
0x00000268
0x0000026C
F04F 0064
MOV R0,#100
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-17
Addressing Modes
Indexed Addressing
Address of the data in memory is in a register
LDR
R0,[R1]
; R0= value pointed to by R1
EEPRO M
PC
0x00000144
0x00000142
0x00000144
0x00000146
0x00000148
0x12345678
R0
R1
0x20000004
0x20000000
0x20000004
0x20000008
0x2000000C
6808 LDR R0,[R1]
RAM
12345678
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-18
Addressing Modes
PC Relative Addressing
Address of data in EEPROM is indexed based
upon the Program Counter
F irst,
LDR R1,=Count
0 x 20 00 .0 00 0
Count
R1
0 x2 00 0 .00 0 0
R O M spa ce
Second,
LDR R0,[R1]
R A M spa ce
R0
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-19
Memory Access Instructions
Loading a register with a constant,
address, or data
 LDR
 LDR
Rd, =number
Rd, =label
LDR and STR used to load/store RAM
using register-indexed addressing
Register [R0]
Base address plus offset [R0,#16]
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-20
Load/Store Instructions
 General load/store instruction format
LDR{type}
STR{type}
LDR{type}
STR{type}
LDR{type}
STR{type}
{type}
B
SB
H
SH
D
Rd,[Rn]
;load memory at [Rn] to Rd
Rt,[Rn]
;store Rt to memory at [Rn]
Rd,[Rn, #n] ;load memory at [Rn+n] to Rd
Rt,[Rn, #n] ;store Rt to memory [Rn+n]
Rd,[Rn,Rm,LSL #n] ;load [Rn+Rm<<n] to Rd
Rt,[Rn,Rm,LSL #n] ;store Rt to [Rn+Rm<<n]
Data type
32-bit word
Unsigned 8-bit byte
Signed 8-bit byte
Unsigned 16-bit halfword
Signed 16-bit halfword
64-bit data
Meaning
0 to 4,294,967,295 or -2,147,483,648 to +2,147,483,647
0 to 255,
Zero pad to 32 bits on load
-128 to +127,
Sign extend to 32 bits on load
0 to 65535,
Zero pad to 32 bits on load
-32768 to +32767,
Sign extend to 32 bits on load
Uses two registers
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-21
The Stack
Stack is last-in-first-out (LIFO) storage
32-bit data
Stack pointer, SP or R13, points to top
element of stack
Stack pointer decremented as data
placed on stack
PUSH and POP instructions used to load
and retrieve data
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-22
The Stack
 Stack is last-in-first-out (LIFO) storage
 32-bit data
 Stack pointer, SP or R13, points to top element
of stack
 Stack pointer decremented as data placed on
stack (incremented when data is removed)
 PUSH and POP instructions used to load and
retrieve data
PUSH {R0}
PUSH {R1}
PUSH {R2}
0x2000.0000
SP
SP
SP
SP
POP {R5}
1
POP {R4}
2
1
3
2
1
POP {R3}
0x2000.7F F C
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-23
Stack Usage
 Stack memory allocation
Stack starting at the first R A M location
N othing O verflow
0x2000.0000
Stack ending at the last R A M location
M ore
O verflow
RAM
0x2000.7000
A llocated
stack
area
SP
0x2000.0F F C
A llocated
stack
area
SP
0x2000.7F F C
M ore
RAM
U nderflow
N othing
U nderflow
 Rules for stack use
 Stack should always be balanced, i.e. functions should
have an equal number of pushes and pops
 Stack accesses (push or pop) should not be performed
outside the allocated area
 Stack reads and writes should not be performed
within the free area
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-24
Functions
m ain
C hange
N um = 0
N u m = N u m + 25
C han ge()
Change LDR
LDR
ADD
STR
BX
main
LDR
MOV
STR
loop
BL
B
R1,=Num
R0,[R1]
R0,R0,#25
R0,[R1]
LR
R1,=Num
R0,#0
R0,[R1]
Change
loop
;
;
;
;
;
;
;
;
;
;
5) R1 = &Num
6) R0 = Num
7) R0 = Num+25
8) Num = Num+25
9) return
1) R1 = &Num
2) R0 = 0
3) Num = 0
4) function call
10) repeat
R etu rn
unsigned long Num;
void Change(void){
Num = Num+25;
}
void main(void){
Num = 0;
while(1){
Change();
}
}
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-25
Subroutines
;------------Rand100-----------; Return R0=a random number between
; 1 and 100. Call Random and then divide
; the generated number by 100
; return the remainder+1
Rand100
PUSH {LR} ; SAVE Link
BL
Random
;R0 is a 32-bit random number
LDR R1,=100
BL
Divide
ADD R0,R3,#1
POP {LR} ;Restore Link back
BX
LR
;------------Divide-----------; find the unsigned quotient and remainder
; Inputs: dividend in R0
;
divisor in R1
; Outputs: quotient in R2
;
remainder in R3
;dividend = divisor*quotient + remainder
Divide
UDIV R2,R0,R1
;R2=R0/R1,R2 is quotient
MUL R3,R2,R1
;R3=(R0/R1)*R1
SUB R3,R0,R3
;R3=R0%R1,
;R3 is remainder of R0/R1
BX
LR
;return
ALIGN
END
POP {PC}
One function calls another, so LR must be saved
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-26
Reset, Subroutines and Stack
 A Reset occurs immediately after power is applied and
when the reset signal is asserted (Reset button pressed)
 The Stack Pointer, SP (R13) is initialized at Reset to the
32-bit value at location 0 (Default: 0x20000408)
 The Program Counter, PC (R15) is initialized at Reset to
the 32-bit value at location 4 (Reset Vector)
 The Link Register (R14) is initialized at Reset to
0xFFFFFFFF
 Thumb bit is set at Reset
 Processor automatically saves return address in LR when
a subroutine call is invoked.
 User can push and pull multiple registers on or from the
Stack at subroutine entry and before subroutine return.
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-27
Introduction to C
 C is a high-level language
 Abstracts hardware
 Expressive
 Readable
 Analyzable
 C is a procedural language
 The programmer explicitly specifies steps
 Program composed of procedures
o Functions/subroutines
 C is compiled (not interpreted)
 Code is analyzed as a whole (not line by line)
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-28
Why C?
C is popular
C influenced many languages
C is considered close-to-machine
Language of choice when careful
coordination and control is required
Straightforward behavior (typically)
Typically used to program low-level
software (with some assembly)
Drivers, runtime systems, operating
systems, schedulers, …
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-29
Introduction to C
Program structure
Subroutines and functions
Variables and types
Statements
Preprocessor
DEMO
S ysTick
IS R
m ain
S y sTick
in it
ADC
d riv er
S ysTick
hardw are
ADC
hardw are
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
LCD
driver
LCD
hardw are
2-30
C Program (demo)
Preprocessor directives
Variables
Functions
Se qu en c e
C o nditio nal
W h ile -lo op
Statements
B loc k 1
Expressions
B lock 1
B loc k 2
B lo c k
B
loc
k
2
Names
Operators
Comments
Syntax
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-31
Important Notes
 C comes with a lot of “built-in” functions
 printf() is one good example
 Definition included in header files
 #include<header_file.h>
 C has one special function called main()
 This is where execution starts (reset vector)
 C development process
 Compiler translates C code into assembly code
 Assembler (e.g. built into uVision4) translates
assembly code into object code
 Object code runs on machine
Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari
2-32
Descargar

Lecture 2: Execution and I/O