EE 371
Microprocessor Systems
Lecture 1: Introduction,
Embedded Systems, ARM
Programming
1-1
Agenda
Course Description
Book, Labs, Equipment
Grading Criteria
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
Recap: Digital Logic
Digital "0" Illegal "1" AND, OR, NOT
Flip flops
Analog 0 1.3 2.0 5V Registers
Positive logic: Negative logic :
True is higher voltage True is lower voltage
False is lower voltage False is higher voltage
1-3
Recap: Ohm’s Law
V=I*R Voltage = Current * Resistance
I=V/R Current = Voltage / Resistance
R=V/I Resistance = Voltage / Current
I = 3.7mA
I
Battery
V R V=3.7V R = 1k
Resistor
•P = V * I Power = Voltage * Current
• P = V2 / R Power = Voltage2 / Resistance
•P = I 2 * R Power = Current2 * Resistance
1-4
Embedded System
Embedded Systems are
automotive
everywhere
medical
communications
microcomp uter
Ubiquitous, invisible
Hidden (computer inside)
appliances
consumer electronics
Dedicated purpose
MicroProcessor
Intel: 4004, ..8080,.. x86
Motorola: 6800, ..
6812,.. PowerPC
Embedded system
ARM, DEC, SPARC, MIPS,
PowerPC, Natl. Semi.,…
LM3S or LM4F
Microcontroller Electrical,
mechanical, MicroController
Processor
I/O Ports
chemical,
or Processor+Memory+
RAM optical
devices
I/O Ports (Interfaces)
ROM DAC Analog
Bus ADC signals
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
Cortex M4 Systick
System Bus Interface NVIC
GPIO Port A GPIO Port B
PA7
PA6
Eight Four
PB7
PB6 ARM Cortex-M4
PA5/SSI0Tx I2Cs PB5
PA4/SSI0Rx
PA3/SSI0Fss
UARTs PB4
PB3/I2C0SDA
+ 256K EEPROM
+ 32K RAM
PA2/SSI0Clk Four PB2/I2C0SCL
PA1/U0Tx CAN 2.0 PB1
SSIs
PA0/U0Rx PB0
PC7 GPIO Port C GPIO Port D PD7 + JTAG
PC6 PD6
PC5
PC4
USB 2.0 Twelve
Timers
PD5
PD4 + Ports
PC3/TDO/SWO PD3
PC2/TDI
PC1/TMS/SWDIO
JTAG Six
64-bit wide
PD2
PD1
+ SysTick
PC0/TCK/SWCLK PD0
+ ADC
PE5
GPIO Port E GPIO Port F
+ UART
PE4 ADC Two Analog PF4
PE3 2 channels Comparators PF3
PE2 12 inputs PF2
PE1 12 bits Two PWM PF1
PE0 Modules PF0
Advanced High Performance Bus Advanced Peripheral Bus
1-8
Structured Programming
Common Constructs (as Flowcharts)
Sequence Conditional While-loop
Block 1
Block 1 Block 2 Block
Block 2
Parallel Distributed Interrupt-driven concurrent
Fork main Trigger
main1 main2 interrupt
Init1 Init2 Init
Join Body1 Body2 Body
Return from
interrupt
1-9
Flowchart
Toaster oven:
main Cook
Output heat
Input from is on
switch Too cold
Not pressed Input toast
Start temperature
Pressed
toast < desired
Cook toast desired
Output 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.
main A
void SysTick_Handler(void){
Input n from B Clock PORTA = PORTA^0x01; E
< } >
PORTB
PORTA = E void main(void){ A
PORTA^1 unsigned long n;
n = (n/4)+12 C while(1){
> n = PORTB; B
n = (n/4)+12; C
PORTD = n; D
Output n to }
PORTD D
}
1-11
ARM Cortex M4-based System
Microcontroller System bus
ARM® CortexTM-M
processor
Input
PPB ports
Internal
Advanced
peripherals High-perf Output
Bus ports
Instructions
Flash ROM Data
ICode bus DCode bus 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
R0 0x0000.0000
R1 256k Flash
R2 ROM [Link]
R3
R4 0x2000.0000
General R5 32k RAM
purpose R6 0x2000.7FFF
registers R7
R8
R9 0x4000.0000
R10
I/O ports
R11 [Link]
R12
Stack pointer R13 (MSP) 0xE000.0000
Link register R14 (LR) Internal I/O
Program counter R15 (PC) PPB 0xE004.1FFF
Condition Code Bits Indicates
N negative Result is negative TI TM4C123
Z zero Result is zero
V overflow Signed overflow Microcontroller
C carry Unsigned overflow
1-14
Input/Output: TM4C123
Cortex M4 Systick
System Bus Interface NVIC
GPIO Port A GPIO Port B
PA7 PB7
PA6 Four PB6
PA5/SSI0Tx Eight PB5
PA4/SSI0Rx UARTs I2Cs PB4
PA3/SSI0Fss PB3/I2C0SDA
PA2/SSI0Clk Four PB2/I2C0SCL
PA1/U0Tx CAN 2.0 PB1
SSIs
PA0/U0Rx PB0
PC7
PC6
GPIO Port C
USB 2.0
GPIO Port D
Twelve
PD7
PD6 6 General-Purpose
PC5 PD5
PC4
PC3/TDO/SWO
Timers PD4
PD3 I/O (GPIO) ports:
• Four 8-bit ports
PC2/TDI JTAG Six PD2
PC1/TMS/SWDIO 64-bit wide PD1
PC0/TCK/SWCLK PD0
GPIO Port E GPIO Port F
(A, B, C, D)
PE5
PE4 ADC Two Analog PF4
• One 6-bit port (E)
PE3 Comparators PF3
PE2
PE1
2 channels
12 inputs
Two PWM
PF2
PF1
• One 5-bit port (F)
12 bits
PE0 Modules PF0
Advanced High Performance Bus Advanced Peripheral Bus
1-15
I/O Ports and Control Registers
Read from port address
n n GPIO_PORTF_DATA_R
Processor
n n
DQ Input/Output Port
Write to port address
Direction bits GPIO_PORTF_DIR_R
n 1 means output
DQ 0 means input
Bus Write to port direction 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
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_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
6. Read/write GPIO_PORTF_DATA_R
1-17
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 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
6. Read/write GPIO_PORTF_DATA_R
1-18
SW Development Environment
Editor KeilTM uVision®
Simulated Processor
Source code Start Microcontroller
Start
; direction register Debug
LDR R1,=GPIO_PORTD_DIR_R Session Memory
LDR R0,[R1]
ORR R0,R0,#0x0F
; make PD3-0 output I/O
STR R0, [R1]
Build Target (F7)
Object code Real Processor
Microcontroller
0x00000142
0x00000144
4912
6808
Download
0x00000146 F040000F Memory
0x0000014A 6008 Start
Debug
Session I/O
Address Data
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
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
Timer
Statements main
ISR
Preprocessor
Timer ADC LCD
driver driver driver
DEMO Timer ADC LCD
hardware hardware hardware
1-22
C Program (demo)
Preprocessor directives
Variables
Functions
Statements
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 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, Performance
Verification measurement, how
Debugging Actions fast it executes
Functional debugging, Optimization, make
input/output values tradeoffs for overall
Performance debugging, good
input/output values with
time improve speed,
Tracing, measure improve accuracy,
sequence of operations reduce memory,
Profiling, reduce power,
measure percentage for
tasks,
reduce size,
time relationship reduce cost
between tasks
1-28
Debugging Intrusiveness
Intrusive Debugging Minimally intrusive
degree of perturbation
negligible effect on the
caused by the debugging
itself system being
debugged
how much the debugging
slows down execution e.g.,
Non-intrusive Debugging dumps(ScanPoint) and
monitors
characteristic or quality
of a debugger Highly intrusive
allows system to operate print statements,
as if debugger did not breakpoints and
exist
single-stepping
e.g., logic analyzer, ICE,
BDM
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]
Command Interface (Advanced but useful)
WS 1, `VarName,0x10
LA (PORTD & 0x02)>>1
1-30
… Debugging
Instrumentation: Code we Use conditional
add to the system that aids compilation (or
in debugging conditional assembly)
E.g., print statements Keil supports
conditional assembly
Good practice: Define
Easy to remove all
instruments with specific
pattern in their names instruments
Use instruments that test Visualization: How the
a run time global flag debugging information is
leaves a permanent displayed
copy of the debugging
code
causing it to suffer a
runtime overhead
simplifies “on-site”
customer support.
1-31