```EE 319K
Introduction to Microcontrollers
Lecture 1: Introduction,
Embedded Systems, ARM
Programming
1-1
Agenda
Course Description
Book, Labs, Equipment
Expectations/Responsibilities
Prerequisites
Embedded Systems
Microcontrollers
ARM Architecture
Instruction Set, Memory Layout
I/O ports and programming
Integrated Development Environment (IDE)
Intro to C
Debugging
1-2
EE306 Recap: Digital Logic
D ig ital
A n alog
"0"
0
Illeg al
1.3
2 .0
Positive logic:
True is higher voltage
False is lower voltage
"1"
5V
 AND, OR, NOT
 Flip flops
 Registers
Negative logic :
True is lower voltage
False is higher voltage
1-3
EE302 Recap: Ohm’s Law
V=I*R
I=V/R
R=V/I
Voltage = Current * Resistance
Current = Voltage / Resistance
Resistance = Voltage / Current
I = 3.7m A
I
V
R
•P = V * I
•P = V2 / R
•P = I2 * R
B attery
V = 3.7V
R = 1k
R esistor
Power = Voltage * Current
Power = Voltage2 / Resistance
Power = Current2 * Resistance
1-4
Embedded System
 Embedded Systems are
everywhere
autom otive
m edical
com m unications
 Ubiquitous, invisible
 Hidden (computer inside)
 Dedicated purpose
m ic ro com p ute r
appliances
consum er electronics
 MicroProcessor
E m b ed d ed system
M icro co n tro ller
L M 3S or L M 4F
P ro cesso r
I/O P o rts
RAM
ROM
E lectrica l,
m ech a n ica l,
ch em ica l,
or
o p tica l
d ev ices
DAC
B us
A n alo g
sig n als
 Intel: 4004, ..8080,..
x86
 Motorola: 6800, ..
6812,.. PowerPC
 ARM, DEC, SPARC, MIPS,
PowerPC, Natl. Semi.,…
 MicroController
 Processor+Memory+
I/O Ports (Interfaces)
1-5
Embedded Systems
A reactive system continuously
accepts inputs
performs calculations
generates outputs
A real time system
Specifies an upper bound on the time
required to perform the
input/calculation/output in reaction to
external events
1-6
Microcontroller
 Processor – Instruction Set
 CISC vs. RISC
 Memory
 Non-Volatile
o ROM
o EPROM, EEPROM, Flash
 Volatile
o RAM (DRAM, SRAM)
 Interfaces
 H/W: Ports
 S/W: Device Driver
 Parallel, Serial, Analog, Time
 I/O
 Memory-mapped vs. I/O mapped
1-7
Texas Instruments 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
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
ARM Cortex-M4
+ 256K EEPROM
+ 32K RAM
+ JTAG
+ Ports
+ SysTick
+ UART
PF4
PF3
PF2
PF1
PF0
A d van ced P eripheral B us
1-8
Structured Programming
Common Constructs (as Flowcharts)
Sequence
C onditional
W hile-loop
B lock 1
B lock 2
P arallel
F o rk
Jo in
B lock 1
B lock 2
D istribu ted
B lock
In te rru p t-d riv en co n cu rren t
m ain 1
m a in 2
m ain
Init1
In it2
In it
B ody1
B ody2
B ody
Trig g er
in terru p t
R etu rn fro m
in terru p t
1-9
Flowchart
Toaster oven:
C ook
m ain
O utput heat
is on
Input from
sw itch
S tart
N ot pressed
Input toast
tem perature
P ressed
C ook
Too cold
toast < desired
toast  desired
O utput heat
is off
return
Coding in assembly and/or high-level language (C)
1-10
Flowchart

Example 1.3. Design a flowchart for a system that performs two independent
tasks. The first task is to output a 20 kHz square wave on PORTA in real time
(period is 50 ms). The second task is to read a value from PORTB, divide the
value by 4, add 12, and output the result on PORTD. This second task is repeated
over and over.
m ain
In pu t n fro m
PORTB
A
B
C lo ck
<
PORTA =
P O R T A ^1
n = (n /4 )+ 1 2
O u tp u t n to
PORTD
E
C
>
D
void SysTick_Handler(void){
PORTA = PORTA^0x01;
E
}
>
void main(void){
unsigned long n;
while(1){
n = PORTB;
n = (n/4)+12;
PORTD = n;
}
}
A
B
C
D
1-11
ARM Cortex M4-based System
S ystem bus
M icrocontroller
A R M ® C ortex T M -M
processor
Input
ports
PPB
Internal
peripherals
A dvanced
H igh-perf
B us
Instructions
F lash R O M
IC ode bus
D C ode bus
O utput
ports
D ata
RAM
 ARM Cortex-M4 processor
 Harvard architecture
 Different busses for instructions and data
 RISC machine
 Pipelining effectively provides single cycle operation for many
instructions
 Thumb-2 configuration employs both 16 and 32 bit instructions
1-12
ARM ISA: Thumb2 Instruction Set
 Variable-length instructions
ARM instructions are a fixed
length of 32 bits
Thumb instructions are a fixed
length of 16 bits
Thumb-2 instructions can be
either 16-bit or 32-bit
 Thumb-2 gives approximately 26%
improvement in code density over
ARM
 Thumb-2 gives approximately 25%
improvement in performance over
Thumb
1-13
ARM ISA: Registers, Memory-map
General
purpose
registers
Stack pointer
Program counter
Condition Code Bits
N negative
Z zero
V overflow
C carry
R0
R1
R2
R3
R4
R5
R6
R7
R8
R9
R10
R11
R12
R13 (MSP)
R14 (LR)
R15 (PC)
Indicates
Result is negative
Result is zero
Signed overflow
Unsigned overflow
0x 00 00 .0 000
256k F lash
ROM
32k R A M
0x 00 03 .F F F F
0x 20 00 .0 000
0x 20 00 .7 F F F
I/O ports
0x 40 00 .0 000
0x 40 0F.F F F F
Internal I/O
PPB
0x E 00 0.00 00
0x E 00 4.1F F F
TI TM4C123
Microcontroller
1-14
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
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
1-15
I/O Ports and Control Registers
n
n
P ro cesso r
n
GPIO_PORTF_DATA_R
n
D Q
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
1-16
I/O Ports and Control Registers
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_RCGC2_R
2. Delay for clock to stabilize
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
1-17
I/O Ports and Control Registers
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 for clock to stabilize
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
1-18
SW Development Environment
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
Real
Microcontroller
Start
Debug
Session
Processor
Memory
I/O
1-19
Introduction to C
C is a high-level language
 Abstracts hardware
 Expressive
 Analyzable
C is a procedural language
 The programmer explicitly specifies steps
 Program composed of procedures
 Functions/subroutines
C is compiled (not interpreted)
 Code is analyzed as a whole (not line by line)
1-20
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, …
1-21
Introduction to C
Program structure
Subroutines and functions
Variables and types
Statements
Preprocessor
DEMO
Tim er
IS R
m ain
Tim e r
driver
drive r
Tim e r
ha rdw are
ha rdw are
LC D
drive r
LC D
ha rdw a re
1-22
C Program (demo)
Preprocessor directives
Variables
Se qu en c e
C o nditio nal
W h ile -lo op
Functions
B loc k 1
Statements
B lock 1
B loc k 2
B lo c k
B loc k 2
Expressions
Names
Operators
Syntax
1-23
Important Notes
C comes with a lot of “built-in” functions
 printf() is one good example
 Definition included in header files
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
1-24
C99 standard
C99 standard
Legacy
int8_t signed 8-bit
char
uint8_t unsigned 8-bit
unsigned char
int16_t signed 16-bit
short
uint16_t unsigned 16-bit
unsigned short
int32_t signed 32-bit
long
uint32_t unsigned 32-bit
unsigned long
char 8-bit ASCII characters
char
1-25
Logic Operations
A
B
A&B
A|B
A^B
A&(~B)
A|(~B)
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
1-26
Common Use
Friendly software modifies just the bits that need to be.
•The or operation to set bits 1 and 0 of a register, the
other
six
bits
remain
unchanged.
GPIO_PORTD_DIR_R |= 0x03; // PD1,PD0 outputs
•The exclusive or operation can also be used to toggle
bits.
GPIO_PORTD_DATA_R ^= 0x80; // toggle PD7
•The and operation to extract, or mask, individual bits:
Pressed = GPIO_PORTA_DATA_R & 0x10;
//true if the PA6 switch pressed
•Shift operations
•
Right shift: >>
•
Left Shift: <<
1-27
Debugging
Aka: Testing, Diagnostics,
Verification
Debugging Actions
 Functional debugging,
input/output values
 Performance debugging,
input/output values with
time
 Tracing, measure
sequence of operations
 Profiling,
 measure percentage for
 time relationship
 Performance
measurement, how
fast it executes
 Optimization, make
good
 improve speed,
 improve accuracy,
 reduce memory,
 reduce power,
 reduce size,
 reduce cost
1-28
Debugging Intrusiveness
Intrusive Debugging
 degree of perturbation
caused by the debugging
itself
 how much the debugging
slows down execution
Non-intrusive Debugging
 characteristic or quality
of a debugger
 allows system to operate
as if debugger did not
exist
 e.g., logic analyzer, ICE,
BDM
Minimally intrusive
 negligible effect on the
system being
debugged
 e.g.,
dumps(ScanPoint) and
monitors
Highly intrusive
 print statements,
breakpoints and
single-stepping
1-29
Debugging Aids in Keil
Interface
Breakpoints
Registers including xPSR
Memory and Watch Windows
Logic Analyzer, GPIO Panel
Single Step, StepOver, StepOut, Run, Run to
Cursor
Watching Variables in Assembly
EXPORT VarName[DATA,SIZE=4]
WS 1, `VarName,0x10
LA (PORTD & 0x02)>>1
1-30
… Debugging
Instrumentation: Code we
add to the system that aids
in debugging
 E.g., print statements
 Good practice: Define
instruments with specific
pattern in their names
 Use instruments that test
a run time global flag
 leaves a permanent
copy of the
debugging code
 causing it to suffer a
 simplifies “on-site”
customer support.
 Use conditional
compilation (or
conditional assembly)
 Keil supports
conditional assembly
 Easy to remove all
instruments
Visualization: How the
debugging information is
displayed
1-31
```