SlideShare a Scribd company logo
1-1
EE 319K
Introduction to Microcontrollers
Lecture 1: Introduction,
Embedded Systems, ARM
Programming
1-2
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-3
EE306 Recap: Digital Logic
Positive logic: Negative logic :
True is higher voltage True is lower voltage
False is lower voltage False is higher voltage
0 1.3 2.0 5 V
"0" Illegal "1"
Digital
Analog
 AND, OR, NOT
 Flip flops
 Registers
1-4
EE302 Recap: Ohm’s Law
V = I * R Voltage = Current * Resistance
I = V / R Current = Voltage / Resistance
R = V / I Resistance = Voltage / Current
R = 1k
Battery
V=3.7V
Resistor
I = 3.7mA
R
I
V
•P = V * I Power = Voltage * Current
•P = V2
/ R Power = Voltage2
/ Resistance
•P = I2
* R Power = Current2
* Resistance
1-5
Embedded System
 Embedded Systems are
everywhere
 Ubiquitous, invisible
 Hidden (computer inside)
 Dedicated purpose
 MicroProcessor
 Intel: 4004, ..8080,.. x86
 Motorola: 6800, ..
6812,.. PowerPC
 ARM, DEC, SPARC, MIPS,
PowerPC, Natl. Semi.,…
 MicroController
 Processor+Memory+
I/O Ports (Interfaces)
communications
automotive
medical
appliances
consumer electronics
microcomputer
I/O Ports
Microcontroller Electrical,
mechanical,
chemical,
or
optical
devices
Embedded system
Bus ADC
Analog
signals
LM3S or LM4F
DAC
Processor
RAM
ROM
1-6
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-7
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-8
Texas Instruments TM4C123
ARM Cortex-M4
+ 256K EEPROM
+ 32K RAM
+ JTAG
+ Ports
+ SysTick
+ ADC
+ UART
GPIO Port D
GPIO Port A
ADC
2 channels
12 inputs
12 bits
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
PE5
PE4
PE3
PE2
PE1
PE0
GPIO Port C
GPIO Port E
JTAG
Four
SSIs
Eight
UARTs
PB7
PB6
PB5
PB4
PB3/I2C0SDA
PB2/I2C0SCL
PB1
PB0
PD7
PD6
PD5
PD4
PD3
PD2
PD1
PD0
PF4
PF3
PF2
PF1
PF0
GPIO Port B
Four
I2Cs
USB 2.0
Cortex M4 Systick
NVIC
Two Analog
Comparators
Advanced Peripheral Bus
Twelve
Timers
Six
64-bit wide
CAN 2.0
System Bus Interface
GPIO Port F
Advanced High Performance Bus
Two PWM
Modules
1-9
Structured Programming
Common Constructs (as Flowcharts)
Fork
Join
Trigger
interrupt
Return from
interrupt
main1
Init1
Body1
main2
Init2
Body2
main
Init
Body
Parallel Distributed Interrupt-driven concurrent
Block 1
Sequence Conditional While-loop
Block 2
Block 1 Block 2 Block
1-10
Flowchart
Toaster oven:
Coding in assembly and/or high-level language (C)
main
toast < desired
Output heat
is on Too cold
Input from
switch
Input toast
temperature
toast desired
Start
Not pressed
Pressed
Output heat
is off
Cook
return
Cook
1-11
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.
Clock
void SysTick_Handler(void){
PORTA = PORTA^0x01;
}
E
E
<
>
>
void main(void){
unsigned long n;
while(1){
n = PORTB;
n = (n/4)+12;
PORTD = n;
}
}
B
C
D
A
main
Input n from
PORTB
A
B
D
C
n = (n/4)+12
Output n to
PORTD
PORTA =
PORTA^1
1-12
ARM Cortex M4-based System
DCode bus
ARM® CortexTM
-M
processor
Data
RAM
Instructions
Flash ROM
Input
ports
Output
ports
Microcontroller
ICode bus
Internal
peripherals
PPB
System bus
Advanced
High-perf
Bus
 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-13
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-14
ARM ISA: Registers, Memory-map
R0
R1
R2
R3
R4
R5
R6
R7
R8
R9
R10
R11
R12
R13 (MSP)
R14 (LR)
R15 (PC)
Stack pointer
Link register
Program counter
General
purpose
registers
TI TM4C123
Microcontroller
256k Flash
ROM
32k RAM
I/O ports
Internal I/O
PPB
0x0000.0000
0x0003.FFFF
0x2000.0000
0x2000.7FFF
0x4000.0000
0x400F.FFFF
0xE000.0000
0xE004.1FFF
Condition Code Bits Indicates
N negative Result is negative
Z zero Result is zero
V overflow Signed overflow
C carry Unsigned overflow
1-15
Input/Output: TM4C123
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)
GPIO Port D
GPIO Port A
ADC
2 channels
12 inputs
12 bits
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
PE5
PE4
PE3
PE2
PE1
PE0
GPIO Port C
GPIO Port E
JTAG
Four
SSIs
Eight
UARTs
PB7
PB6
PB5
PB4
PB3/I2C0SDA
PB2/I2C0SCL
PB1
PB0
PD7
PD6
PD5
PD4
PD3
PD2
PD1
PD0
PF4
PF3
PF2
PF1
PF0
GPIO Port B
Four
I2Cs
USB 2.0
Cortex M4 Systick
NVIC
Two Analog
Comparators
Advanced Peripheral Bus
Twelve
Timers
Six
64-bit wide
CAN 2.0
System Bus Interface
GPIO Port F
Advanced High Performance Bus
Two PWM
Modules
1-16
I/O Ports and Control Registers
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
Input/Output Port
D Q
Write to port direction register
Direction bits
D Q
Write to port address
Processor
Read from port address
Bus
n
n
n
n
n 1 means output
0 means input
GPIO_PORTF_DATA_R
GPIO_PORTF_DIR_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_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-18
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-19
SW Development Environment
0x00000142 4912
0x00000144 6808
0x00000146 F040000F
0x0000014A 6008
Start
; direction register
LDR R1,=GPIO_PORTD_DIR_R
LDR R0,[R1]
ORR R0,R0,#0x0F
; make PD3-0 output
STR R0, [R1]
Source code
Build Target (F7)
Download
Object code
Processor
Memory
I/O
Simulated
Microcontroller
Address Data
Editor KeilTM uVision®
Processor
Memory
I/O
Real
Microcontroller
Start
Debug
Session
Start
Debug
Session
1-20
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-21
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-22
Introduction to C
Program structure
Subroutines and functions
Variables and types
Statements
Preprocessor
DEMO
main
Timer
hardware
Timer
driver
LCD
hardware
LCD
driver
Timer
ISR
ADC
hardware
ADC
driver
1-23
C Program (demo)
Preprocessor directives
Variables
Functions
Statements
Expressions
Names
Operators
Comments
Syntax
1-24
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-25
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-26
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-27
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-28
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-29
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-30
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-31
… 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
Visualization: How the
debugging information is
displayed

More Related Content

PPT
Microcontrollers (product life cycle, ARM programming)
PPT
introduction to microcontroller.........
PPT
introduction to microcontrollers presentation
PPT
Embedded systems and its ports
PPTX
2.instrumentation ii
PPT
Microcontroller 8051 By Mitesh kumar
PDF
Hardware interfacing basics using AVR
PPTX
MICROCONTROLLER.pptx
Microcontrollers (product life cycle, ARM programming)
introduction to microcontroller.........
introduction to microcontrollers presentation
Embedded systems and its ports
2.instrumentation ii
Microcontroller 8051 By Mitesh kumar
Hardware interfacing basics using AVR
MICROCONTROLLER.pptx

Similar to Introduction to Microprocessor & Microcontroller.ppt (20)

PPT
Lecture_8255_MODIfggggggggggggFIED_BY_ME_24-11-2020.ppt
PPTX
Presentation
PPTX
Mod-2 M&M.pptx
PDF
Lecture6
PDF
AVR arduino dasar
PPT
jbptunikompp-gdl-syahrul-23476-14-14-inter-y.ppt
PPTX
08 ATmega328P Arduino GPIO input outpt.pptx
PPTX
Microcontroller
PPT
Microcontroller 8051
PPTX
Basic Computer Organisation And Design
PPTX
8255_Ppi new
PPT
Microcontroller 8051
PDF
Unit 3-1 (1)
PDF
Analog to Digital Converter
PDF
P89v51rd2
PDF
Analog To Digital Conversion (ADC) Programming in LPC2148
PPT
8051 microcontroller training (2) (sahil gupta 9068557926)
DOCX
Keypad interfacing 8051 -NANOCDAC
PPTX
8255 ppi.pptx
Lecture_8255_MODIfggggggggggggFIED_BY_ME_24-11-2020.ppt
Presentation
Mod-2 M&M.pptx
Lecture6
AVR arduino dasar
jbptunikompp-gdl-syahrul-23476-14-14-inter-y.ppt
08 ATmega328P Arduino GPIO input outpt.pptx
Microcontroller
Microcontroller 8051
Basic Computer Organisation And Design
8255_Ppi new
Microcontroller 8051
Unit 3-1 (1)
Analog to Digital Converter
P89v51rd2
Analog To Digital Conversion (ADC) Programming in LPC2148
8051 microcontroller training (2) (sahil gupta 9068557926)
Keypad interfacing 8051 -NANOCDAC
8255 ppi.pptx
Ad

Recently uploaded (20)

PPTX
OOP with Java - Java Introduction (Basics)
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Well-logging-methods_new................
PPTX
CH1 Production IntroductoryConcepts.pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Construction Project Organization Group 2.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Welding lecture in detail for understanding
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
PPT on Performance Review to get promotions
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Lecture Notes Electrical Wiring System Components
OOP with Java - Java Introduction (Basics)
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Well-logging-methods_new................
CH1 Production IntroductoryConcepts.pptx
573137875-Attendance-Management-System-original
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Automation-in-Manufacturing-Chapter-Introduction.pdf
Construction Project Organization Group 2.pptx
R24 SURVEYING LAB MANUAL for civil enggi
Model Code of Practice - Construction Work - 21102022 .pdf
Welding lecture in detail for understanding
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
PPT on Performance Review to get promotions
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Lecture Notes Electrical Wiring System Components
Ad

Introduction to Microprocessor & Microcontroller.ppt

  • 1. 1-1 EE 319K Introduction to Microcontrollers Lecture 1: Introduction, Embedded Systems, ARM Programming
  • 2. 1-2 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
  • 3. 1-3 EE306 Recap: Digital Logic Positive logic: Negative logic : True is higher voltage True is lower voltage False is lower voltage False is higher voltage 0 1.3 2.0 5 V "0" Illegal "1" Digital Analog  AND, OR, NOT  Flip flops  Registers
  • 4. 1-4 EE302 Recap: Ohm’s Law V = I * R Voltage = Current * Resistance I = V / R Current = Voltage / Resistance R = V / I Resistance = Voltage / Current R = 1k Battery V=3.7V Resistor I = 3.7mA R I V •P = V * I Power = Voltage * Current •P = V2 / R Power = Voltage2 / Resistance •P = I2 * R Power = Current2 * Resistance
  • 5. 1-5 Embedded System  Embedded Systems are everywhere  Ubiquitous, invisible  Hidden (computer inside)  Dedicated purpose  MicroProcessor  Intel: 4004, ..8080,.. x86  Motorola: 6800, .. 6812,.. PowerPC  ARM, DEC, SPARC, MIPS, PowerPC, Natl. Semi.,…  MicroController  Processor+Memory+ I/O Ports (Interfaces) communications automotive medical appliances consumer electronics microcomputer I/O Ports Microcontroller Electrical, mechanical, chemical, or optical devices Embedded system Bus ADC Analog signals LM3S or LM4F DAC Processor RAM ROM
  • 6. 1-6 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
  • 7. 1-7 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
  • 8. 1-8 Texas Instruments TM4C123 ARM Cortex-M4 + 256K EEPROM + 32K RAM + JTAG + Ports + SysTick + ADC + UART GPIO Port D GPIO Port A ADC 2 channels 12 inputs 12 bits 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 PE5 PE4 PE3 PE2 PE1 PE0 GPIO Port C GPIO Port E JTAG Four SSIs Eight UARTs PB7 PB6 PB5 PB4 PB3/I2C0SDA PB2/I2C0SCL PB1 PB0 PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 PF4 PF3 PF2 PF1 PF0 GPIO Port B Four I2Cs USB 2.0 Cortex M4 Systick NVIC Two Analog Comparators Advanced Peripheral Bus Twelve Timers Six 64-bit wide CAN 2.0 System Bus Interface GPIO Port F Advanced High Performance Bus Two PWM Modules
  • 9. 1-9 Structured Programming Common Constructs (as Flowcharts) Fork Join Trigger interrupt Return from interrupt main1 Init1 Body1 main2 Init2 Body2 main Init Body Parallel Distributed Interrupt-driven concurrent Block 1 Sequence Conditional While-loop Block 2 Block 1 Block 2 Block
  • 10. 1-10 Flowchart Toaster oven: Coding in assembly and/or high-level language (C) main toast < desired Output heat is on Too cold Input from switch Input toast temperature toast desired Start Not pressed Pressed Output heat is off Cook return Cook
  • 11. 1-11 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. Clock void SysTick_Handler(void){ PORTA = PORTA^0x01; } E E < > > void main(void){ unsigned long n; while(1){ n = PORTB; n = (n/4)+12; PORTD = n; } } B C D A main Input n from PORTB A B D C n = (n/4)+12 Output n to PORTD PORTA = PORTA^1
  • 12. 1-12 ARM Cortex M4-based System DCode bus ARM® CortexTM -M processor Data RAM Instructions Flash ROM Input ports Output ports Microcontroller ICode bus Internal peripherals PPB System bus Advanced High-perf Bus  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
  • 13. 1-13 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
  • 14. 1-14 ARM ISA: Registers, Memory-map R0 R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12 R13 (MSP) R14 (LR) R15 (PC) Stack pointer Link register Program counter General purpose registers TI TM4C123 Microcontroller 256k Flash ROM 32k RAM I/O ports Internal I/O PPB 0x0000.0000 0x0003.FFFF 0x2000.0000 0x2000.7FFF 0x4000.0000 0x400F.FFFF 0xE000.0000 0xE004.1FFF Condition Code Bits Indicates N negative Result is negative Z zero Result is zero V overflow Signed overflow C carry Unsigned overflow
  • 15. 1-15 Input/Output: TM4C123 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) GPIO Port D GPIO Port A ADC 2 channels 12 inputs 12 bits 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 PE5 PE4 PE3 PE2 PE1 PE0 GPIO Port C GPIO Port E JTAG Four SSIs Eight UARTs PB7 PB6 PB5 PB4 PB3/I2C0SDA PB2/I2C0SCL PB1 PB0 PD7 PD6 PD5 PD4 PD3 PD2 PD1 PD0 PF4 PF3 PF2 PF1 PF0 GPIO Port B Four I2Cs USB 2.0 Cortex M4 Systick NVIC Two Analog Comparators Advanced Peripheral Bus Twelve Timers Six 64-bit wide CAN 2.0 System Bus Interface GPIO Port F Advanced High Performance Bus Two PWM Modules
  • 16. 1-16 I/O Ports and Control Registers 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 Input/Output Port D Q Write to port direction register Direction bits D Q Write to port address Processor Read from port address Bus n n n n n 1 means output 0 means input GPIO_PORTF_DATA_R GPIO_PORTF_DIR_R
  • 17. 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_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
  • 18. 1-18 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
  • 19. 1-19 SW Development Environment 0x00000142 4912 0x00000144 6808 0x00000146 F040000F 0x0000014A 6008 Start ; direction register LDR R1,=GPIO_PORTD_DIR_R LDR R0,[R1] ORR R0,R0,#0x0F ; make PD3-0 output STR R0, [R1] Source code Build Target (F7) Download Object code Processor Memory I/O Simulated Microcontroller Address Data Editor KeilTM uVision® Processor Memory I/O Real Microcontroller Start Debug Session Start Debug Session
  • 20. 1-20 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)
  • 21. 1-21 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, …
  • 22. 1-22 Introduction to C Program structure Subroutines and functions Variables and types Statements Preprocessor DEMO main Timer hardware Timer driver LCD hardware LCD driver Timer ISR ADC hardware ADC driver
  • 23. 1-23 C Program (demo) Preprocessor directives Variables Functions Statements Expressions Names Operators Comments Syntax
  • 24. 1-24 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
  • 25. 1-25 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
  • 26. 1-26 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
  • 27. 1-27 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: <<
  • 28. 1-28 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
  • 29. 1-29 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
  • 30. 1-30 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
  • 31. 1-31 … 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 Visualization: How the debugging information is displayed

Editor's Notes

  • #2: To do this week: get book, order a board, {read chapters 1 and 2 of the book or ebook 2 3 4}, do ws01, HW1, install Keil uVision 4.73 on laptop
  • #5: Consumer electronics: Washing machine, Exercise equipment, Remote controls, Clocks and watches, Games and toys, Audio/video electronics, Set-back thermostats, Camera, Camcoder, Television, VCR, cable box Communication systems: Answering machines, Telephones, Fax machines, Radios, Cellular phones, pagers Automotive systems: Automatic breaking, Noise cancellation, Locks, Electronic ignition, Power windows and seats, Cruise control, Collision avoidance, Climate control, Emission control, Instrumentation Military hardware: Smart weapons, Missile guidance systems, Global positioning systems, Surveillance systems Business applications: Cash registers, Vending machines, ATM machines, Traffic controllers, Industrial robots, Bar code readers and writers, Automatic sprinklers, Elevator controllers, RFID systems, Lighting and heating systems Medical devices: Monitors, Drug delivery systems, Cancer treatments, Pacemakers, Prosthetic devices, Dialysis machines
  • #7: Processor There are two classifications of computers: complex instruction set computer (CISC) and reduced instruction set computer (RISC). In reality, there is a spectrum of architectures that we can classify as CISC or RISC. We make these general observations when deciding whether to call a computer CISC or RISC: Complex instruction set computers (CISC) Early computers offered CPUs that were much faster than available memories. Fetching instructions limited performance A single complex instruction could perform many operations Example: Find the zeros of a polynomial Complex instructions require many processor clock cycles to complete and most instructions can access memory A program running on a CISC computer employed a relatively small number of complex instructions High code density, many instruction types w/ varying length, fewer and specialized registers, many addressing modes Complexity is embedded in the processor hardware (overhead) Examples: Intel (x86), Freescal 9S12 Reduced Instruction Set Computers (RISC) Memories match CPU speed No large penalty for instruction fetch Instructions simplified Example: dedicated load/store instructions, regular instructions can not access memory but only registers Single processor clock cycle per instruction (pipelined) A program running on a RISC computer employs a relatively larger number of simplified instructions Reduced code density, few instructions w/ fixed delay (pipeline!), many identical general-purpose registers, few addressing modes Complexity exists in the assembly code generated by the programmer or compiler, hardware is simple (low overhead/low power) Examples: LC3, MIPS, ARM, SPARC, PowerPC Which architecture is best is beyond the scope of this class, but it is important to recognize the terminology. It is very difficult to compare the execution speed of two computers, especially between a CISC and a RISC. One way to compare is to run a benchmark program on both, and measure the time it takes to execute. Time to execute benchmark = Instructions/program * Average cycles/instruction * Seconds/cycle For example, the 50 MHz ARM Cortex M has one bus cycle every 20ns. One average it may require 1.5 cycles per instruction. If the benchmark program executes 10,000,000 assembly instructions, then the time to execute the benchmark will be 0.3 seconds. Memory: EPROMs are Erasable Programmable ROMs. The mechanism used to erase and write is UV light EEPROMs are Electrically Erasable Flash memory is like EEPROM however writes are performed in large blocks as opposed to single bytes. Cheaper hence popular DRAMs require a periodic refresh SRAMs don’t. Both are volatile therefore are lost when powered down. Interfaces: Parallel - binary data is available simultaneously on groups of lines Serial - binary data is available one bit at a time on a single line Analog - data is encoded as a variable voltage Time - data is encoded as a period, frequency, pulse width or phase shift I/O Memory-mapped I/O I/O ports/registers appear as addresses on common bus with memory I/O ports/registers are accessed as though they are locations in memory Employed on the ARM, Freescale and TI processors I/O-mapped I/O I/O ports/registers have separate control signals from those used with memory Special instructions are used to access I/O ports/registers Employed on Intel x86 processors
  • #9: The Fork-Join construct is used in Parallel programming. This is different from multi-threading used in concurrent (Distributed) programming. In multi-threading there may be multiple threads that are active but at any given instant only one of them is being executed. In desktop systems that have multiple cores and parallel computers with several processors, multiple threads can be simultaneously executed.
  • #11: < : Hardware Interrupt causing the main program to be suspended and the corresponding ISR to execute > : Return from Interrupt causing the control to be returned to the point where the main program was suspended. The execution sequence of this simple system might be something like: ABCDB<E>CDBC<E>DBCD<E>BCD… We say this code is multi-threaded because we have two threads: The foreground thread computes the primes and the background thread issues a pulse. They are both active at the same time.
  • #12: The LC3 computer from EE 306 had an address space of 64Ki and an addressability of 16-bit for a total of 128KiB. The Memory Address Register (MAR) is 16 bits and the Memory Data Register (MDR) is also 16-bits. Vs. On the ARM, the MAR is still 32-bits. Total addressable memory is 4GiB. It has flexible addressability (bit,byte-8bits,halfword-16bits,word-32bits)
  • #14: The PC points to the address of the current instruction (Program Counter) The Link Register is akin to Register R7 in LC3 used to store the return address on subroutine calls. The stack is a temporary storage implemented in the RAM. We push and pop elements onto and off the stack as we desire. The stack pointer (SP) keeps track of the current location of the “top” of the stack. The CC (condition-code) bits contains codes that reflect the results of the most recent instruction. Many “branch” type instructions “test” these bits for their operation. The Program Status Register (PSR) has them.
  • #17: The lines marked in red are “leagacy code” the next slide shows what the new way is. If we are using portF pin 0 or portD pin 7, we have to perform an additional step to unlock these ports by writing the following: GPIO_PORTF_LOCK_R = 0x4C4F434B; // *) unlock GPIO Port F GPIO_PORTD_LOCK_R = 0x4C4F434B; // *) unlock GPIO Port D
  • #18: If we are using portF pin 0 or portD pin 7, we have to perform an additional step to unlock these ports by writing the following: GPIO_PORTF_LOCK_R = 0x4C4F434B; // *) unlock GPIO Port F GPIO_PORTD_LOCK_R = 0x4C4F434B; // *) unlock GPIO Port D
  • #28: Debugging principles Control and observability Can’t see everything; need to be smart when debugging
  • #29: Minimally intrusive Dumps execute faster (<1us) compared to print statements (10ms)
  • #30: Commands given: 1: WS 1, `VarName,0x10 Puts the variable called VarName in the first watch window 2: LA (PORTD & 0x02)>>1 Puts port D pin 2 in logic anlayzer
  • #31: Black box testing Control inputs, observe outputs White box testing 0) pick the most likely place of failure 1) desk check: conceive of what you expect will happen at each step 2) breakpoint at the start of the place 3) single step (or step over) 4) compare what actually happened to what you wanted to have happen