Professional Documents
Culture Documents
Introduction to Microcontrollers
Lecture 1: Introduction,
Embedded Systems, ARM
Programming
1-1
Agenda
Course Description
Embedded Systems
Microcontrollers
ARM Architecture
Instruction Set, Memory Layout
I/O ports and programming
Integrated Development Environment (IDE)
Intro to C
Debugging
1-2
Digital
Analog 0
"0"
Illegal
1.3
2.0
Positive logic:
True is higher voltage
False is lower voltage
"1"
5V
Negative logic :
True is lower voltage
False is higher voltage
1-3
I
V
P = V * I
P = V2 / R
P = I2 * R
Battery
V=3.7V
R = 1k
Resistor
1-4
Embedded System
automotive
medical
communications
micro computer
appliances
consumer electronics
LM3S or LM4F
Processor
I/O Ports
RAM
ROM
Bus
ADC
Ubiquitous, invisible
Hidden (computer inside)
Dedicated purpose
MicroProcessor
Embedded system
Microcontroller
Electrical,
mechanical,
chemical,
or
optical
devices
DAC
Analog
signals
MicroController
Processor+Memory+
I/O Ports (Interfaces)
1-5
Embedded Systems
A reactive system continuously
accepts inputs
performs calculations
generates outputs
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
Systick
NVIC
GPIO Port A
PA7
PA6
PA5/SSI0Tx
PA4/SSI0Rx
PA3/SSI0Fss
PA2/SSI0Clk
PA1/U0Tx
PA0/U0Rx
PC7
PC6
PC5
PC4
PC3/TDO/SWO
PC2/TDI
PC1/TMS/SWDIO
PC0/TCK/SWCLK
Eight
UARTs
Four
I2Cs
Four
SSIs
CAN 2.0
GPIO Port C
GPIO Port D
USB 2.0
Twelve
Timers
JTAG
Six
64-bit wide
GPIO Port E
PE5
PE4
PE3
PE2
PE1
PE0
GPIO Port B
ADC
2 channels
12 inputs
12 bits
PB7
PB6
PB5
PB4
PB3/I2C0SDA
PB2/I2C0SCL
PB1
PB0
PD7
PD6
PD5
PD4
PD3
PD2
PD1
PD0
GPIO Port F
Two Analog
Comparators
Two PWM
Modules
ARM Cortex-M4
+ 256K EEPROM
+ 32K RAM
+ JTAG
+ Ports
+ SysTick
+ ADC
+ UART
PF4
PF3
PF2
PF1
PF0
1-8
Structured Programming
Common Constructs (as Flowcharts)
Sequence
Conditional
While-loop
Block 1
Block 2
Parallel
Fork
Join
Block 1
Block 2
Distributed
Block
Interrupt-driven concurrent
Trigger
main
interrupt
main1
main2
Init1
Init2
Init
Body1
Body2
Body
Return from
interrupt
1-9
Flowchart
Toaster oven:
Cook
main
Output heat
is on
Input from
switch
Start
Not pressed
Input toast
temperature
Pressed
Cook
Too cold
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.
main
Input n from B
PORTB
n = (n/4)+12
Output n to
PORTD
Clock
PORTA =
PORTA^1
<
E
>
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
Microcontroller
ARM CortexTM-M
processor
Input
ports
PPB
Internal
peripherals
Advanced
High-perf
Bus
Instructions
Flash ROM
ICode bus
DCode bus
Output
ports
Data
RAM
RISC machine
Pipelining effectively provides single cycle operation for many
instructions
Thumb-2 configuration employs both 16 and 32 bit instructions
1-12
1-13
General
purpose
registers
Stack pointer
Link register
Program counter
Condition Code Bit s
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
256k Flash
ROM
32k RAM
0x0000.0000
0x0003.FFFF
0x2000.0000
0x2000.7FFF
I/O ports
0x4000.0000
0x400F.FFFF
Internal I/O
PPB
0xE000.0000
0xE004.1FFF
TI TM4C123
Microcontroller
1-14
Input/Output: TM4C123
Cortex M4
Systick
NVIC
GPIO Port A
PA7
PA6
PA5/SSI0Tx
PA4/SSI0Rx
PA3/SSI0Fss
PA2/SSI0Clk
PA1/U0Tx
PA0/U0Rx
PC7
PC6
PC5
PC4
PC3/TDO/SWO
PC2/TDI
PC1/TMS/SWDIO
PC0/TCK/SWCLK
Eight
UARTs
Four
I2Cs
Four
SSIs
CAN 2.0
GPIO Port C
GPIO Port D
USB 2.0
Twelve
Timers
JTAG
Six
64-bit wide
GPIO Port E
PE5
PE4
PE3
PE2
PE1
PE0
GPIO Port B
ADC
2 channels
12 inputs
12 bits
PB7
PB6
PB5
PB4
PB3/I2C0SDA
PB2/I2C0SCL
PB1
PB0
PD7
PD6
PD5
PD4
PD3
PD2
PD1
PD0
GPIO Port F
Two Analog
Comparators
Two PWM
Modules
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)
1-15
GPIO_PORTF_DATA_R
n
Input/Output Port
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
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
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
4912
6808
F040000F
6008
Address Data
Download
Real
Microcontroller
Start
Debug
Session
Processor
Memory
I/O
1-19
Introduction to C
C is a high-level language
Abstracts hardware
Expressive
Readable
Analyzable
C is a procedural language
The programmer explicitly specifies steps
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)
Introduction to C
Program structure
Subroutines and functions
Timer
ISR
main
Timer
driver
ADC
driver
Timer
hardware
ADC
hardware
LCD
driver
LCD
hardware
1-22
C Program (demo)
Preprocessor directives
Variables
Sequence
Conditional
While-loop
Functions
Block 1
Statements
Block 1
Block 2
Block
Block 2
Expressions
Names
Operators
Comments
Syntax
1-23
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 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
char
unsigned char
short
unsigned short
long
unsigned long
char
1-25
Logic Operations
A
A&B
A|B
A^B
A&(~B)
A|(~B)
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
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
tasks,
time relationship
between tasks
Performance
measurement, how
fast it executes
Optimization, make
tradeoffs for overall
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
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
runtime overhead
simplifies on-site
customer support.
Use conditional
compilation (or
conditional assembly)
Keil supports
conditional assembly
Easy to remove all
instruments
1-31