introduction to embedded systems and chapter 1...
TRANSCRIPT
Introduction to Embedded Systemsand
Chapter 1: Introduction to HCS12/MC9S12
EE383: Introduction to Embedded SystemsUniversity of Kentucky
Samir Rawashdeh
With slides based on material by H. Huang – Delmar Cengage Learning
1
Embedded Systems
2
3
Why learn Embedded Systems?
POV Propeller Clock
http://hackaday.com/2010/10/03/spinning-pov-clock-done-oh-so-right/
Homework: check out hackaday.com !
4
Time Fountain
http://cre.ations.net/creation/the-time-fountain
Why learn Embedded Systems?
5
At the Space Systems Lab
6
At the Space Systems Lab
Number System Issue
• Computer hardware uses binary numbers to perform all operations.• To display numbers produced by the computer, both the octal and hexadecimal
numbers have been used to shorten the representation.• Human beings are used to decimal number system. Conversion is often needed
to convert numbers between the internal (binary) and external (decimal) representations.
• It is common for mixed use of different number bases.• A prefix is used to indicate the base of a number.
Modulus Math• Computers perform modulus math because it uses a limited number of bits(e.g., 8, 16, or 32 bits) to represent numbers.
7
What is a computer?
Software
Hardware
Computer Hardware Organization
8
The Processor• A processor consists of arithmetic logic unit (ALU), control unit, and
registers.• The ALU can perform many different operations.• An ALU that can perform 4 operations is shown in Figure 1.1.
9
Registers• A register is a storage location inside the CPU.• A register is used to hold data or address during the execution of an instruction.• A register, very close to the ALU, provides fast access to operands for program execution.• Some processors provide a dedicated register (often called an accumulator) as one of the
operands of most of the instructions.• Freescale HCS12, Microchip PIC18, Intel 8051 microcontrollers use this approach.• Other processors provide many general-purpose registers in the CPU that can be used
as operands of instructions. • Atmel AVR and AVR32, Microchip PIC24 and PIC32, Freescale Coldfire use this approach.
Microprocessor• A processor implemented in a single integrated circuit (IC).• The first microprocessor, Intel 4004, was introduced in 1968.• The Intel 8008, the first 8-bit microprocessor from Intel, was introduced in 1972.• A microprocessor requires peripheral ICs to interface with I/O devices.• A microprocessor does not have peripheral functions such as timers, A/D converters,
D/A converters, parallel I/O ports, and memory.• The designer need to add peripheral ICs and memory devices to the microprocessor
in order to build a product.
10
Microcontroller• A microcontroller incorporates the processor and one or more of the following
peripheral functions and memory in one very large scale integrated circuit (VLSI):1. Memory2. Timer functions3. Serial communication interface such as UART, I2C, SPI, CAN, and Ethernet4. A/D converter5. D/A converter6. Direct memory access 7. Parallel I/O ports8. Memory component interface9. Software debug support
• Microcontrollers have been used in almost every product that requires a certain amount of intelligence.
• Microcontrollers have been used as controllers for displays, printers, keyboards,modems, charge card phones, palm-top computers, home appliances such as washing machines and microwave ovens, automobile control, and so on.
11
Embedded Systems• An embedded system is a computer designed to perform a dedicated function.• An embedded system performs one or a few predefined tasks, with very specific
requirements.• Embedded systems are often mass-produced and benefitting from economic scale.• Software written for embedded systems is often called firmware.
12
Memory Technologies
Magnetic Memory• Magnetic drums, magnetic tapes, and magnetic disks are the three major magnetic
memory devices that have been invented.• Only magnetic disks are still being widely used today• Hard disks are rarely used in an embedded system
Optical Memory• Compact disk (CD) and digital video disc (DVD) are two major optical memory devices.• CD was introduced in 1982.• CD has two versions: CD-R and CD-RW.• The capacity of a 12-inch CD is 700 MB.• A 12-inch DVD has the capacity of 4.7 GB• DVD has two versions: DVD-R and DVD-RW versions.
Semiconductor Memory• Semiconductor memory can be classified on the basis of volatility and read-writability.• On the basis of volatility, semiconductor memory is divided into volatile and
non-volatile memory.• On the basis of read-writability, semiconductor memory is divided into random-access
memory (RAM) and read-only memory (ROM).
13
Random-access memoryThere are four types of RAM:
Dynamic random-access memory (DRAM): • The one-bit cell of DRAM consists of one transistor and one capacitor.• The information is stored in the capacitor whereas the transistor controls the access of
the information stored in the capacitor.• Periodic refresh is required to maintain the contents of a DRAM chip
Static random-access memory (SRAM): • The one-bit of SRAM consists of 4 to 6 transistors.• No periodic refresh operation is required to maintain the information stored in SRAM.
14
Read-Only Memory• ROM is nonvolatile.• There are several types of ROM technologies.
Mask-Programmed ROM (MROM)• The contents of MROM is programmed when it is manufactured.• To be cost-effective, many thousands of copies of MROM chip must be manufactured.• MROM is one of the major ROM technologies to hold application programs
Programmable ROM (PROM)• The setting of each bit is locked by a fuse or antifuse.• PROM is programmed by blowing a fuse or an antifuse.• PROM can only be programmed once using a special programmer.
Erasable programmable ROM (EPROM) • An EPROM cell must be erased before it is programmed.• EPROM is electrically programmable many times.• EPROM is erased by ultraviolet light (through a window).• EPROM can only be erased in bulk (whole chip in one erasure operation).
15
Electrically erasable programmable ROM (EEPROM)• An EEPROM cell must be erased before it is programmed.• EEPROM is electrically erasable and programmable many times.• EEPROM can be erased one location, one row, or whole chip in one operation
Flash memory• Flash memory is electrically erasable and programmable many times.• Flash memory can only be erased in bulk or a sector at a time• Flash can achieve the density of DRAM and is the most widely used nonvolatile memory
technology.
16
• 16-bit CPU• 64 KB memory space (also supports expanded memory up to 1 MB through
a 16-KB window)• 0 KB to 4KB of EEPROM• 2KB to 14KB of on-chip SRAM• 32 KB to 512KB flash memory• Sophisticated timer functions that include: input capture, output compare,
pulse accumulators, real-time interrupt, and COP timer• Serial communication interfaces: SCI, SPI, CAN• Background debug mode (BDM)• 10-bit A/D converter• Instructions for supporting fuzzy logic function
The HCS12 Features
17
Computer software
• Computer programs are known as software• A program is a sequence of instructions
Machine instruction
• A sequence of binary digits which can be executed by the processor
0001 1000 0000 0110: A [A] + [B]0100 0011: A [A] + 11000 0110 0000 0110: A 6
• Hard to understand, enter, debug, and maintain for human being
18
Assembly language
• Defined by assembly instructions• An assembly instruction is a mnemonic representation of a machine instruction
ABA: A [A] + [B]DECA: A [A] – 1
• Assembly programs must be translated into machine instructions before it can be executed -- translated by an assembler
• Programmers need to work on the program logic at a very low level and cannot achieve high productivity.
19
High-level language
• Syntax of a high-level language is similar to English• A translator (called compiler) is required to translate the program written in a high-level
language
Source code
• A program written in assembly or high-level language
Object code
• The output of an assembler or compiler
20
line
1:
2:
3:
4:
5:
6:
addr.
2000
2003
2006
2009
machine code
= 00002000
B6 1000
BB 1001
BB 1002
7A 1100
source code
org $2000
ldaa $1000
adda $1001
adda $1002
staa $1100
end
21
The HCS12 CPU Registers
7 00 7A B
15 0D
8-bit accumulator A and Bor16-bit double accumulator D
15 0X
15 0Y
Index register X
Index register Y
15 0SP Stack pointer
15 0PC Program counter
Condition code register
CarryOverflowZeroNegativeI Interrupt maskHalf-Carry (from bit 3)X Interrupt MaskStop Disable
Figure 1.10 HCS12 CPU registers.
S X H I N Z V C
22
Control Unit
• The control unit uses a program counter to keep track of the address of the instruction to be executed next.
• The fetched instruction is placed in the instruction register when is decoded, and executed.
• The control unit decodes machine codes and performs the operations specified by the machine code.
• A machine instruction has several fields. A mandated field is the opcode field.• Instruction execution is timed by a clock signal. • The frequency of the clock signal used in an 8-bit or 16-bit microcontroller is from
a few mega hertz to several hundred mega Hertz•Program instructions are normally stored in sequential locations in memory.• The processor may not execute instructions in sequential due to the need to
execute instructions based on the condition or the need to repeat a certain group of instructions.
• The processor uses conditional and unconditional branch (or jump) instructions to change the program flow.
23
Memory System Operation• A block diagram of a memory system is shown in Figure 1.2.• Each memory location has two components: address and contents.• The organization of a memory chip is indicated by m x n, where m is
the number of locations in the chip and n indicates the number of bits in one location.• The notation [reg] and [addr] specifies the contents of a register and the memory location
at addr, respectively.
24
The HCS12 Addressing Modes• A HCS12 instruction consists of one or two bytes of opcode and zero to five bytes of
operand addressing information.
• Addressing modes specify the operand to be operated on.
• The addressing mode may specify a value, a register, or a memory location to be used as
an operand.
25
26
Inherent Mode
- Instructions that use this mode do not uses extra bytes to specify operands because
the instructions either do not need operands or all operands are CPU registers.
- Operands are implied by the opcode.
- Examples
NOP
INX
DECA
No operationIncrement XDecrement A
27
Immediate Mode
- Operands for instructions that use immediate mode are included in the instruction.
- CPU does not access memory for operands.
- For example,
LDAA #$55 ;A ← $55
LDX #$1000 ;X ← $1000
Direct Mode
- This mode can only specify memory locations in the range of 0 - 255.
- This mode uses only one byte to specify the operand address.
- Examples,
LDAA $20
LDAB $40
Loads A with the value $55
Loads X with the value $1000
Loads A with a copy of the content at $0020
Loads B with a copy of the content at $0040
28
Extended Mode
- This mode uses a 16-bit value to specify memory address is in the instruction.
LDAA $4000 ; A ← [$4000]
LDX $FE60 ; X ← [$FE60]:[$FE61]
Relative Mode
- Used only by branch instructions.
- Short and long conditional branch instructions use exclusively relative mode.
- A short branch instructions consists of an 8-bit opcode and a signed 8-bit offset. The
short relative mode can specify a range of -128 ~ +127.
- A long branch instruction consists of an 8-bit opcode and a signed 16-bit offset. The
range of the long relative mode is from -32768 ~ +32767.
- BRCLR (branch if clear) and BRSET (branch if set) instructions can also use relative mode to specify branch target.
- A programmer uses a symbol to specify the branch target and the assembler will figure out the actual branch offset (distance) from the instruction that follows branch instruction.
29
- For example,
minus …
…
bmi minus ;Branch if Minus
…
Indexed Mode
- This mode uses the sum of an index register (X, Y, PC, or SP) and an offset to specify the
address of an operand.
- The offset can be a 5-bit, 9-bit, and 16-bit signed value or the value in accumulator A,
B, or D.
- Automatic pre- or post-increment or pre- or post-decrement by -8 to +8 are options.
- PC can be used as the index register for all but auto-increment or auto-decrement mode.
- Indirect indexing with 16-bit offset or accumulator D as the offset is supported.
30
5-bit Constant Offset Indexed Addressing
- The base index register can be X, Y, SP, or PC.
- The range of the offset is from -16 to +15.
- Examples,
ldaa 0,X
ldab 1,X
9-bit Constant Offset Indexed Addressing
- The base index register can be X, Y, SP, or PC.
- The range of the offset is from -256 to +255.
- Examples,
ldaa $FF,X
ldab -20,Y
Loads A with the content at [X]+0
Loads B with the content at [X]+1
Loads A with the content at [X]+255
Loads B with the content at [Y]-20
31
16-bit Constant Offset Indexed Addressing
- The base index register can be X, Y, SP, or PC.
- This mode allows access any location in the 64-KB range.
- Examples,
ldaa 2000,X
staa 4000,Y
16-bit Constant Indirect Indexed Addressing
- A 16-bit offset is added to the base index register to form the address of a memory
location that contains a pointer to the memory location affected by the instruction.
- The square brackets distinguish this addressing mode from the 16-bit constant offset
indexing. For example,
ldaa [10,X]
staa [20,Y]
Loads A with the content at [X]+2000
Stores content of A at [Y]+4000
Loads A with the content pointed to by address at [X]+10
Stores content of A in memory at the location pointed to by [Y]+20
32
Auto Pre/Post Decrement/Increment Indexed Addressing
- The base index register can be X, Y, or SP.
- The index register can be incremented or decremented by an integer value either
before or after indexing taking place.
- The index register retains the changed value after indexing.
- The value to be incremented or decrement is in the ranges -8 thru -1 or 1 thru 8.
- The value need to be related to the size of the operand or the current instruction.
- Examples,
staa 1,-SP ; decrement SP by 1 and then store the contents of A in the location
; pointed to by SP and
staa 1,SP- ; store the contents of A in the location pointed by SP and then
; decrement SP by 1
ldx 2,+SP
ldx 2,SP+
33
Accumulator Offset Indexed Addressing
- The effective address of the operand is the sum of the accumulator and the base index
register.
- The base register can be X, Y, SP, or PC.
- The accumulator can be the 8-bit A or B or the 16-bit accumulator D.
- For example,
ldaa B,X
stab B,Y
ldy D,X
34
HCS12 Instruction Examples
The LOAD and STORE Instructions
- The LOAD instruction copies the contents of a memory location or places an immediate
value into an accumulator or a CPU register.
- STORE instructions save the contents of a CPU register into a memory location.
- N and Z flags of the CCR register are automatically updated and the V flag is cleared.
- All except for the relative mode can be used to select the memory location or value to
be loaded into an accumulator or CPU register.
- All except for the relative and immediate modes can be used to select memory location
to store contents of the CPU register. For example,
ldaa 0,X
staa $20
stx $8000
ldd #100
35
36
Transfer and Exchange Instructions
- Transfer instructions copy the contents of a CPU register or accumulator into another
CPU register or accumulator.
- TFR is the universal transfer instruction, but other mnemonics are accepted for
compatibility with the 68HC11.
- The TAB and TBA instructions affect the N, Z, and V condition code bits.
- The TFR instruction does not affect any condition code bits. For example,
TFR D,X ; [D] X
TFR A,B ; [A] B
TFR A,X ; sign-extended of[A] X ; A is signed extended to 16-bit and assigned to X
TFR X,A ; X[7:0] A ; lower 8 bits copied to A
37
- The EXG instruction exchanges the contents of a pair of registers or accumulators. For example,exg A, Bexg D,Xexg A,X ; A X[7:0], X $00:[A]exg X,B ; X $00:[B], B X[7:0]
- The SEX instruction sign-extend an 8-bit two’s complement number into a 16-bit number so that it can be used in 16-bit signed operations. For example, SEX A,X
38
Move Instructions
- These instructions move data bytes or words from a source to a destination in memory.
- Six combinations of immediate, extended, and index addressing modes are allowed to
specify the source and destination addresses:
IMM EXT, IMM IDX, EXT EXT,
EXT IDX, IDX EXT, IDX IDX
- For example,
movb $100,$800
movw 0,X, 0,Y
Move Byte
Move Word
39
Add and Subtract Instructions
- These instructions perform fundamental arithmetic operations.
- The destinations of these instructions are always a CPU register or accumulator.
- There are two-operand and three-operand versions of these instructions.
- Three-operand ADD or SUB instructions always include the C flag as one of the
operand.
- Three-operand ADD or SUB instructions are used to perform multi-precision addition
or subtraction.
- For example,
adda $1000 ; A [A] + [$1000]
adca $1000 ; A [A] + [$1000] + C
suba $1002 ; A [A] + [$1002]
sbca $1000 ; A [A] - [$1000] - C
40
41
Left for Dr Lumpp to discuss
42
Instruction Execution Cycle
- One or more read cycles to fetch instruction opcode bytes and addressing information.
- One or more read cycles to fetch the memory operand(s) (optional).
- Perform the operation specified by the opcode.
- One or more write cycles to write back the result to either a register or a memory
- location (optional).
Instruction Queue
- The HCS12 executes one instruction at a time and many instructions take several clock
cycles to complete.
- When the CPU is performing the operation, it does not need to access memory.
- The HCS12 pre-fetches instructions when the CPU is not accessing memory to speedup
the instruction execution process.
- There are two 16-bit queue stages and one 16-bit buffer. Unless buffering is required,
program information is first queued in stage 1, and then advanced to stage 2 for
execution.
43
Memory System Operation• A block diagram of a memory system is shown in Figure 1.2.• Each memory location has two components: address and contents.• The organization of a memory chip is indicated by m x n, where m is
the number of locations in the chip and n indicates the number of bits in one location.• The notation [reg] and [addr] specifies the contents of a register and the memory location
at addr, respectively.
44
Read Operation• The processor places the address of the memory location that it intends to read on
the address bus and asserts the RD signal.• The RD signal enables the memory chip to send out the contents of the selected location
on the data bus and the processor gets the data.
Write Operation• The processor places the address of the location that it intends to write on the address
bus.• The processor also places the data that it intends to write on the data bus and asserts
the WE signal.• In responds to the WE signal, the memory chip stores the data on the data bus in the
selected memory location.
45
Program ExecutionTo understand how processor executes the program, we need to answer the followingquestions:
Where does the processor start to execute program after power on?• From a fixed location such as address 0: PIC18, Atmel avr, and 8051• Fetch the starting address from a fixed location: HCS12
How does the processor update the program counter (PC)?• Increment the program counter by the length of the instruction just executed.• Increment or decrement by the amount specified in the current instruction—conditional
and unconditional branch instructions.• Set to the new address specified by the current instruction—jump (or goto) instruction or
subroutine call instruction.
Program Counter Circuit• A common building block of PC is the D flip-flop with set and reset capability (Figure 1.3).• The program counter circuit that can be forced to 0, incremented by 1, incremented by
a field in the instruction, and loaded with a jump target is shown in Figure 1.4.
46
47
Architecture of the Processor X
• Processor X has a separate data memory and program memory spaces.• Processor X is used to explain the instruction execution process.• The instruction set of processor X is given in Table 1.3. • Processor X has an 8-bit accumulator A and a 16-bit register ptr.• Processor X instructions can use an 8-bit value to specify a data memory location from
0 to 255 to be accessed.• For data memory locations with address higher than 255, the processor uses the 16-bit
register ptr to specify the address. • Processor X uses a MDR register to hold data to be written and read from data memory.• The instruction set implemented by processor X is given in Table 1.3.
48
Instruction sequence example
ld 0x20,#0 ; place 0 in data memory at 0x20ld 0x21,#20 ; place 20 in data memory at 0x21ld ptr,#0x2000 ; load 0x2000 into the ptr register
loop: ld A,@ptr ; load the memory contents pointed to by ptrand A,#0x03 ; and the value 0x03 with accumulator Abnz next ; branch if the result is not 0inc 0x20 ; increment the memory location at 0x20 by 1
next: dbnz 0x21,loop ; decrement memory location at 0x21 and branch; if the contents of 0x21 is not zero yet
49
Instruction Execution ProcessAssume that processor X executes the instruction sequence in the previous out of reset.
Instruction ld 0x20,#0 (machine code 75 20 00)Step 1Processor X places 0 (contents of PC) on the address bus to perform a read from programprogram memory.Step 2The 8-bit value at the location 0x0000 is the instruction opcode 0x75. At the end of this read cycle, the PC is incremented to 0x0001. The opcode byte 0x75 is fetched. Figure 1.5 shows the opcode read cycle.Step 3Recognizing that 0x75 is the opcode of a load instruction, processor performs two more read operations. The first read returns the address 0x20 whereas the second read returns the value 0.Step 4Processor places 0x20 on the data memory address bus and places 0 on the data bus andthen performs a write operation to write the value 0 to location 0x20.
50
51
Instruction ld 0x21,#20 (machine code 75 21 14)• The execution of this instruction is identical to that of the previous instruction.• PC is incremented to 0x0006 after the execution of this instruction.
Instruction ld ptr,#0x2000 (machine code 90 20 00)Step 1Processor X places the PC value (0x06) on the program memory address bus and makesrequest to read the program memory. Figure 1.7 shows the opcode read cycle.Step 2The opcode returned from location at 0x06 is 0x90 and PC is incremented to 0x07.Step 3The opcode 0x90 indicates that processor X should access program memory to fetch the16-bit value stored after the opcode byte. Processor performs two more read cycles fromprogram memory and places these two bytes in the ptr register. PC is incremented to 0x09.
52
Instruction ld A,@ptr (machine code E0)Step 1Processor X places the PC value (0x09) on the program memory address bus and requeststo read the program memory. At the end of this step, PC is incremented to 0x0A.
53
Step 2The opcode (0xE0) returned in Step 1 requires the processor to perform a read operationUsing the address stored in ptr register. Processor X places the contents of ptr registeron the data memory address bus and request a read operation.Step 3Data memory returns the byte in data memory pointed to by ptr register. This byte is placed in A.
54
Instruction and A,#0x03 (machine code 54 03)Step 1Processor X places the PC value on the program memory address bus to request a read.After this step is completed, PC is incremented to 0x0B.Step 2The opcode byte (0x54) returned in Step 1 requires the processor X to perform anotherread operation from program memory.Step 3Processor X places the PC value on program memory address bus and request to performa read operation. PC is incremented to 0x0C after this step.Step 4Program memory returns the value 0x03 to the processor.Step 5The processor X then performs an AND operation on the contents of A and the value 0x03and places the result in A
55
Instruction bnz next (machine code is 70 02)Step 1Processor X places the PC value (0x0C) on the program memory address bus and performsa read operation. After this step, PC is incremented to 0x0D.Step 2The opcode (0x70) returned in Step 1 requires processor X to performs another read tofetch the branch offset. Step 3Processor X places the value PC (0x0D) on the program memory address bus and performsa read. After this step the program memory returns the value 0x02 to the processor. PCis incremented to 0x0E.Step 4Processor checks whether the A value is zero and decides whether branch should betaken. If A contains a nonzero value, the next instruction will be skipped.
56
Instruction inc 0x20 (machine code 05 20)Step 1Processor X places the PC contents (0x0E) on the program memory address bus and Performs a read operation. At the end of the read operation, PC is incremented to 0x0F.Step 2The returned opcode (0x05) requires processor X to performs another read to fetch an8-bit address from program memory.Step 3Processor X places the contents of PC on the program memory address bus to requestanother read from program memory. PC is incremented to 0x10Step 4Processor X places the value 0x20 on the data memory address bus and requests a read.The returned value is placed in MDR register.Step 5Processor X adds one to the MDR register.Step 6Processor X places the contents of MDR on data memory data bus and 0x20 on the addressbus and requests a write operation. At the end the value in MDR register is written into data memory location at 0x20.
57
Instruction dbnz 0x21, loop (machine code is D5 21 0A)Step 1Processor X places the contents of PC on the program memory address bus and requestsa read operation. At the end of this step PC is incremented to 0x11.Step 2The opcode (0xD5) returned in step 1 request processor X to perform two more read operation to fetch a byte of data memory address and a byte of branch offset fromprogram memory. At the end of these two read operations, PC is incremented to 0x13.Step 3Processor places the value of 0x21 on the data memory address bus to perform a read.At the end of this read, the contents of data memory location at 0x21 is returned in MDR.Step 4Processor decrements the contents of MDR by 1 and then places this result on the data memory data bus. At the same time, it also places the value of 0x21 on the data memoryaddress bus and requests a write operation.Step 5If the value stored in MDR is not zero, processor X adds 0x0A to the PC and places the sumback in PC (this causes a branch). Otherwise, PC is not changed.
58