Lec 1
Lec 1
Introduction to Microcontrollers
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
EE306 Recap: Digital Logic
1-3
EE302 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
1-4
Embedded System
automotive
Embedded Systems are
everywhere
medical
communications
microcomputer
Ubiquitous, invisible
Hidden (computer inside)
consumer electronics
appliances
Dedicated purpose
MicroProcessor
Intel: 4004, ..8080,.. x86
Motorola: 6800, .. 6812,..
PowerPC
ARM, DEC, SPARC, MIPS,
Embedded system
PowerPC, Natl. Semi.,…
Microcontroller LM3S or LM4F 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
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
1-8
Structured Programming
Common Constructs (as Flowcharts)
Sequence Conditional While-loop
Block 1
1-9
Flowchart
Toaster oven:
main Cook
Output heat
Input from is on
switch Too cold
return
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
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 0x0003.FFFF
R3
R4 0x2000.0000
General R5 32k RAM
purpose R6 0x2000.7FFF
registers R7
R8
R9 0x4000.0000
R10
I/O ports
R11 0x400F.FFFF
R12
Stack pointer R13 (MSP) 0xE000.0000
Link register R14 (LR) Internal I/O
Program counter R15 (PC) PPB 0xE004.1FFF
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
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
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
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
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]
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
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
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
reduce size,
tasks,
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
how much the debugging
debugged
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 compilation (or
aids 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