Professional Documents
Culture Documents
Topic03D Using Assembly in The Freescale MC9S12X
Topic03D Using Assembly in The Freescale MC9S12X
3D
Monitor Equates ;
ut2hex:
o EQU
$FE16
I/O Equates ;
ORTA:
P EQU
$0000
DRA:
D EQU
$0002
Constant Equates ;
NE:
O EQU
1
Memory Map Equates ;
RG
P EQU
$0D00
RAM
S EQU
$0800
Program Location
The program location contains only one line with a single ORG pseudooperator, that denes the starting location of the program.
Program Initialisation
This section of the code contains instructions related to initialising system resources such as the stack pointer.
Program Header
Using an assembler directive to tell it were the start of the code is.
; export 'Entry' symbol ; for absolute assembly: mark this as application entry point
; include derivative specic macros INCLUDE 'mc9s12xdp512.inc' ROMStart EQU $4000 ; absolute address to place my code/constant data
Spin: BRA Spin ;************************************************************** ;* Interrupt Vectors ;************************************************************** ORG $FFFE DC.W Entry ; Reset Vector
Vector Table
With higher level languages such as C, the program ow is dened using various structures, such as:
Functions While / Repeat-until structures For looping structures If-Then-Else structures
A function in an assembler program is commonly referred to as a subroutine. A subroutine consists of a series of instructions, terminated by the RTS instruction. A subroutine is called using the JSR (jump-to-subroutine) instruction. A subroutine is always preceded by subroutine or function header containing the following information:
Title Description, including relevant constants used, etc. Inputs Outputs Author, Date, and Revision
BEGIN
InitSCI(IN: OUT:)
AMAP =1
InitSCI(IN: OUT:)
SCIACR1 =0 SCIACR2 =1
. . . This would be the place for the RTS command in the assembly code.
END
END
10
T
END
F
Do something
11
Example: Using a repeat-until structure repeat a section of code until the condition A>10 is no longer met.
; Repeat BEGIN LOOP:
CMPA
#10
BNE
LOOP ;
Until (i>10)
Repeat END ; END_WHILE
Do something
A>10
END
12
Example
; FOR (B=10;B>0;B--)
LDAB
#10 LOOP:
DBNE
B,LOOP
;END_FOR
DBNE Instruction
B=10
Do something
B=B - 1
B!=0
F
END
13
CHK:
CMPB #10 ; IF (B==10) BEQ THEN ; Else do this ELSE: BRA END ; Then do this THEN: ;End of IF END:
Then Path
B==10
Else Path
Do something
Do something else
14
CHK2
B<10
Do this
CHK3
B==10
Or do this
ELSE
B>20
Or do this
Otherwise do this
ENDIF
15
16
Strings: A string is made up of a series of characters, used mainly for display purposes.
For example: STR:
DC.B
The temperature is
17
String Example
MainLoop: JSR LDX Loop: LDAB CMPB BEQ JSR INX BRA BEGIN InitSCI #Str 0,X #0 Spin PutChar Loop B==0 END InitSCI(IN: OUT:) X=Str B=M[X]
Spin: BRA Spin ;Base Address for SCI1 Serial_Base EQU $00D0 ;Add serial routines INCLUDE 'Serial.inc' ; Constants go here. Str DC.B 'Hello World',$00
PutChar(IN: B OUT:)
X=X+1
18
Array Example
Array:
19
Lookup Tables
Preinitialised Arrays
20
21
Example
Our program grabs a character from the serial port and stores it in consecutive memory locations starting at $2000 until a CR character is detected.
MainLoop: JSR Loop: InitSCI LDX #$2000 JSR GetChar STAB 0,X INX CMPB #$OD BNE LOOP BRA Spin EQU $00D0 INCLUDE 'Serial.inc'
Spin: Serial_Base
The string that is being stored at $2000 can be of any length and the size changes each time the program is executed.
22
Debugging
Debugging software running on a microcontroller can be achieved using many different approaches including: In Circuit Emulator (ICE) ROM monitor systems Breakpoints Background Debugger JTAG Logic Analyser
23
In-Circuit Emulator
The in-circuit emulator (ICE) was one of the most popular methods used in embedded system debugging. An in-circuit emulator is a device consisting of two components, an emulator running on a host system and an adapter connected to the host at one end and sitting in place of the actual MCU at the other, providing all the functionality of the real MCU. The emulator attempts to completely replace the MCU, allowing the user the ability to stop the ICE at anytime and view the internal registers and memory of the emulated MCU at each individual clock cycle. The user has access to all the data provided on the inputs of the ICE and can trace the ow of data. The benets of an ICE include:
Able to insert the debugger into the actual circuit and debugged programs in actual application situations. Allow the programmer the ability to debug MCUs which do not contain a built-in debugger.
24
In-Circuit Emulator
The use of an ICE for debugging purposes is no longer worthwhile for the following reasons:
Modern day processors contained many pins at an extremely high pin density, making in-circuit emulators difcult, if not impossible to manufacture. The architecture of the modern processor is extremely difcult to emulate in real-time. Many modern processors contain instruction pipelines and multiple buses, that operate faster than any emulator ever could. Delays caused by the emulator create more problems in debugging the system. Connecting the emulator to the target via a cable consisting of many signal lines makes the debugging process difcult.
ICE debuggers are now a thing of the past since many prcoessors now contain on-chip debuggers.
25
26
Background Debugger
Background Debug Module (BDM)
The BDM module has replaced the need for an in-circuit emulator. Using a BDM pod is it possible to read and write to the memory of the target system, view and modify registers in the target system and also trace a single instruction at a time. The BDM setup consists of single wire connection between the BDM pod and the target system, the connection is made to the BGND pin on each of the MCUs.
27
JTAG
28
BSC
BSC
Every JTAG enabled device contains Four dedicated JTAG pins (TMS TCK, TDO, TDI) Every pin on the device is connected to a single bit buffer. All single bit buffers are daisy-chained together from TDI to TDO. The values in these buffers can be serially read and modied.
Figure from Texas Instruments JTAG Primer.
Input Pins
Core Logic
BSC
BSC
OE
BSC
JTAG
Boundary-Scan Register
Output Pins
BSC
BSC
BSC
User Data Register TDI Bypass Resister Instruction Register TMS TCK TAP
TDO
3-4
BSC
29
Logic Analyzers
As time has progressed, electronic systems have become larger and more complex. The oscilloscope is a priceless tool for analyzing electronic circuits. However, the oscilloscope has it limitations:
The wave form must be repetitive for it to be displayed properly. Oscilloscopes are limited to only a small number of inputs, with four being the largest number of inputs found on an oscilloscope.
Due to these limitations the oscilloscope is not the best tool for analyzing digital or P circuitry. To successfully analyze a digital circuit built around an 8 bit P, a device would be required that would be capable of monitoring 8 data lines, 16 address lines and several control lines, such a device is known as a logic analyzer.
30
Logic Analyzers
A basic logic analyzer system consists of many probes, which are connected to the electrical signals to be monitored on the P, a pod which interfaces the probes to the logic analyzer, and of course the logic analyzer itself. Each digital signal (0 or 1) is connected to a separate channel on the logic analyzer. The logic analyzer continually samples each channel and writes the data to its internal memory until a trigger event causes it to stop.
Monday, 31 August 2009 31
Logic Analyzers
The logic analyzer can monitor the signals on many lines simultaneously. The data can be viewed in one of two possible ways:
The data or addresses can be displayed as binary, octal, or hexadecimal numbers. Or the data can be displayed as a timing diagram, where each signal forms a line across the screen represented by a series of pulses.
32
Logic Analyzers
Both an oscilloscope and a logic analyzer rely on a trigger event, on an oscilloscope the signal can only be viewed after a trigger has occurred. The logic analyzer however allows the signal to be viewed before and after a trigger has occurred.
33