AN3560
AN3560
AN3560
Application Note
Document Number: AN3560
Rev. 1, 07/2008
Contents
Freescale Semiconductor, Inc., 2008. All rights reserved.
1 Introduction
MC9S08J M60 devices form part of the Freescale Flexis
series of microcontrollers. The Flexis
TM
series of
microcontrollers is the connection point of the Freescale
Controller Continuum where 8-bit and 32-bit
compatibility becomes reality.
The 8-bit MC9S08J M60 MCUs are devices with a
full-speed USB module providing best-in-class USB
module performance, system integration, and software
support. The USB module of the J M series MCU has
seven endpoints and 256 bytes RAM for high efficiency
data transfer.
MC9S08J M60 MCUs provide many peripherals, such as
USB, SPI, IIC, SCI, ADC, TPM, and RTC. These MCUs
are flexible and can easily be integrated into different
applications such as game pads, security control panels,
printers, and PC peripherals.
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 MC9S08JM60 USB Module Introduction. . . . . . . . . . . . . 2
2.1 USB Endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 VREG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 USB Transceiver . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.4 USB SIE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.5 USB RAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.5.1 Buffer Descriptor Table (BDT) . . . . . . . . . . . 6
2.5.2 Double Buffer (Ping-Pong Buffer) . . . . . . . . 11
3 USB Device Development . . . . . . . . . . . . . . . . . . . . . . . 13
3.1 Hardware Design. . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1.1 Clocking Generation . . . . . . . . . . . . . . . . . . 13
3.1.2 USB Device Power . . . . . . . . . . . . . . . . . . 14
3.1.3 Pullup Resistor . . . . . . . . . . . . . . . . . . . . . . 15
3.2 USB Firmware Design. . . . . . . . . . . . . . . . . . . . . . 16
3.2.1 USB Communication Model . . . . . . . . . . . . 16
3.2.2 Main Firmware Structure . . . . . . . . . . . . . . 16
3.2.3 USB State Machine . . . . . . . . . . . . . . . . . . 19
3.2.4 USB Device Enumeration Process. . . . . . . 20
3.2.5 USB Transactions on EP1-6 . . . . . . . . . . . 27
3.2.6 Suspend and Resume . . . . . . . . . . . . . . . . 28
4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Appendix AFirmware for HID Mouse . . . . . . . . . . . . . . . . . . . 32
Appendix BEnumeration Process of an HID Mouse . . . . . . . 33
USB Device Development with
the MC9S08JM60
In-depth Understanding of the MC9S08JM60 USB Module
by: Derek Liu
Asia & Pacific Operation Microcontroller Division
USB Device Development with the MC9S08JM60, Rev. 1
MC9S08JM60 USB Module Introduction
Freescale Semiconductor 2
More detailed information on the USB module of MC9S08J M60 devices and how to use it in applications
are discussed in this document. In addition, some skills and issues that must be noticed in design have also
been discussed.
2 MC9S08JM60 USB Module Introduction
Figure1 shows the block diagram of the USB module for the MC9S08J M60 MCU. It includes:
On-chip 3.3 V regulator (VREG)
USB transceiver (XCVR)
Serial interface engine (SIE)
USB RAM
Figure 1. USB Module Block Diagram
S
k
y
B
l
u
e
G
a
s
k
e
t
R
A
M
A
r
b
i
t
r
a
t
i
o
n
USB RAM
256 bytes
IRQ
L
o
c
a
l
B
u
s
USBDP
USBDN
Serial Interface Engine
T
o
I
n
t
e
r
r
u
p
t
C
o
n
t
r
o
l
l
e
r
P
e
r
i
p
h
e
r
a
l
B
u
s
USB CONTROLLER
SkyBlue
XCVR
VREG
Protocol and Rate
Match
V
USB33
BVCI
Target
TX
Logic
BVCI
Initiator
RX
Logic
48 MHz Reference Clock
24 MHz Clock (bus clk)
Enable
(SIE)
USBDP pullup
BVCI: Basic Virtual Component Interface
XCVR: Transceiver
B
u
f
f
e
r
M
a
n
a
g
e
m
e
n
t
MC9S08JM60 USB Module Introduction
USB Device Development with the MC9S08JM60, Rev. 1
Freescale Semiconductor 3
Figure2 shows the layers of a typical USB device.
Figure 2. USB Protocol Layer
The physical layer involves data signaling, such as J and K signals, start of packet (SOP), and resume
signals. The MC9S08J M60 USB PHY corresponds to the physical layer.
The protocol layer lies above the physical layer. Transactions of a different transfer type are managed by
this layer. The packet in the USB low-level protocol are processed in this layer. In the MC9S08J M60 USB
module they are finished by a SIE.
The command processor and data report handler layer lies between the protocol and application layers.
This layer corresponds to the USB stack firmware described in this document. The USB enumeration and
configuration, the standard class request, or the customized protocol are all processed by this layer.
The top layer is the application layer. It it is based on the USB communication provided in the lower layers.
The program in this layer focuses on the customized application.
2.1 USB Endpoint
One important concept needs to be discussed before explaining the USB module the endpoint. Each
USB logical device consists of a collection of independent endpoints. An endpoint is a uniquely
identifiable portion of a USB device that is the terminus of a communication flow between the host and
device. A USB pipe is an association between an endpoint on a device and software on a host.
The MC9S08J M60 device has seven endpoints that can be used to build seven communication pipes
between the USB host and device.
Application layer
Command processor
&
Data reports handler
Protocol layer
Physical layer
USB Device Development with the MC9S08JM60, Rev. 1
MC9S08JM60 USB Module Introduction
Freescale Semiconductor 4
Endpoint 0 is bidirectional (in and out), mainly used for control transfer. Endpoint 0 is required for all USB
devices.
Endpoint 16 are directional, they can be configured to do only in or out direction communication at a
time.
Endpoints 5 and 6 are double buffering (also called ping-pong buffering). Each of these has two buffers.
One buffer can be operated by the CPU, when the other communicates with the host (controlled by SIE).
These two buffers exchange their roles after the current transaction is over. The CPU has the control of one
of the double buffers in turn. With this feature, the communication efficiency improves because the CPU
waiting time is shortened.
Each endpoint can be enabled or disabled by the EPCTLn register. The direction and handshake for all
endpoints are also controlled by the EPCTLn register.
The control transfer, bulk transfer, isochronous transfer, and interrupt transfer are all supported by each
endpoint of MC9S08J M60 series MCU. That matches all kinds of USB data transfer requirements.
2.2 VREG
On-chip regulator (VREG) provides a stable power source to the USB internal transceiver and internal (or
external) pullup resistor. It requires a voltage supply input in the range of 3.9 V to 5.5 V, and its output
voltage range is from 3.0 V to 3.6 V. The VREG shares the same power supply with the MCU, but the
MCU can work with the power voltage from 2.7 V to 5.5 V.
The corresponding pin to regulator output voltage is V
USB3.3
, that
can be used to supply the power for the
external pullup resistor.
The on-chip regulator can be enabled or disabled by USBVREN bit of the USBCTL0 register. If it is
disabled, an external 3.3 V voltage must be provided to USB module via V
USB3.3
pin.
NOTE
Damage to the part may occur if the external 3.3 V power supply is provided while
the internal regulator is enabled.
2.3 USB Transceiver
USB transceiver belongs to the physical layer of the USB standard protocol. It provides a differential
signal for USB communication. The USBDP and USBDN pins are analog input/output lines for full-speed
data communication.
2.4 USB SIE
The USB serial interface engine (SIE) is mainly used to implement transferring and receiving of logic.
For the SIE transferring logic, the firmware immediately needs to configure three BD registers and fills
the data into endpoint buffer; then hands over the control to SIE. The SIE will send the data to the host
automatically after the host issues an IN operation. All work required in the USB specification, such as
MC9S08JM60 USB Module Introduction
USB Device Development with the MC9S08JM60, Rev. 1
Freescale Semiconductor 5
coding (NRZI), bit stuffing, CRC, SYNC, etc., are implemented by SIE without firmwares intervention.
The SIE hands over the control to the CPU after the transfer is finished.
As for the receiving logic, the SIE can receive data packets automatically. The receiving logic process
includes decoding, synchronizing, detection, bit stuff removal, EOP detection, CRC validation, PID check,
etc.
The data packet is filled into the endpoint buffer, and the BD registers are updated. Then the SIE gives the
control to the CPU for reading data out. Some status and flag bits in the USB registers (INSTAT, STAT,
ERRSTAT, and EPCTLn) for this transaction are refreshed. The SIE owns the control of the BD and
endpoint buffer before the data ends to receive.
The SIE sends the acknowledge (ACK) or non-acknowledge (NAK) handshake to the host. It also stalls
the current transfer (STALL). The handshake is enabled or disabled by the EPHSHK bit in the EPCTLn
register. If it is enabled, ACK is transferred to the host when the transaction is correctly received. The NAK
is sent out by SIE if the OWN bit in BD status and control register is 0. The STALL is issued by setting
the EPSTALL bit in the EPCTL register or the BDTSTALL bit in the BD status and control register.
With the SIE transferring and receiving logic, the work of the firmware is greatly reduced resulting in more
CPU time being saved.
2.5 USB RAM
MC9S08J M60 MCU provides 256 bytes RAM space for the USB module, in which the USB BD registers
and endpoint buffer are located. The USB RAM can also be allocated by the system as general purpose
RAM when the USB module is disabled or it still has free space.
The USB RAM runs at 48 MHz, which is twice the speed of the bus clock. It can be accessed by the CPU
system and SIE. This is an area to exchange data and information between the CPU and SIE.
All the USB control and the status registers (BD registers) and USB data buffer must be allocated in the
USB RAM. It is illegal to allocate them in other space.
The USB RAM occupies a separate space instead of a part of the MCU RAM. From the memory map of
MC9S08J M60 in Figure3, the USB RAM address range is from 0x1860 to 0x195F, but the 4K bytes of
MCU RAM starts from 0x00B0, ends at 0x10AF.
USB Device Development with the MC9S08JM60, Rev. 1
MC9S08JM60 USB Module Introduction
Freescale Semiconductor 6
Figure 3. USB RAM
Figure3 shows the USB RAM location of MC9S08J M60 in the memory map. The organization of USB
RAM is also illustrated.
2.5.1 Buffer Descriptor Table (BDT)
Buffer descriptor table (BDT) is used to manage USB endpoint communication efficiently. It provides the
status and control for each endpoint.
Figure 4. Buffer Descriptor
FLASH
59,088 Bytes
DIRECT PAGE
RAM
FLASH
HIGH PAGE REGISTERS
96 BYTES
USB RAM
256 BYTES
4096 BYTES
1872 Bytes
REGISTERS
0x0000
0x00AF
0x00B0
0x10AF
0x10B0
0x17FF
0x1800
0x185F
0x1860
0x1960
0xFFFF
0x195F
Endpoint 0 IN, BD
Endpoint 0 OUT, BD
Endpoint 1, BD
Endpoint 2, BD
Endpoint 3, BD
Endpoint 4, BD
Endpoint 5 EVEN BD
Endpoint 5 ODD, BD
Endpoint 6 EVEN BD
Reserved
Endpoint 6 ODD, BD
USB RAM
0x00
Offset
0x03
0x06
0x09
0x0C
0x0F
0x12
0x15
0x18
0xIB
0x1D
0x00
0x20
0x1860
0x195F
Endpoint Buffer
JM60 Memory Map
Endpoint 0 BD, IN
Endpoint 0 BD, OUT
Endpoint 1, BD
Endpoint 2, BD
Endpoint 3, BD
Endpoint 4, BD
Endpoint 5 BD, EVEN
Endpoint 5 BD, ODD
Endpoint 6 BD, EVEN
Endpoint 6 BD, ODD
Buffer
Descriptor
Table
(BDT)
0x00
offset
0x1D
Buffer
Double
Status & Control Register
BC[7:0]
EPADR[9..2]
0
1
2
offset
Buffer Descriptor (BD)
Registers
MC9S08JM60 USB Module Introduction
USB Device Development with the MC9S08JM60, Rev. 1
Freescale Semiconductor 7
These registers for each endpoint are called buffer descriptor (BD). Each BD comprises three bytes, in
which the endpoint data buffers location, length, control logic, and status are kept.
The BDT locates at the beginning of USB RAM. The BDs for seven endpoints are organized and placed
one by one from EP0 in to EP6 out. There are 10 BDs in total and 30 bytes are allocated from 0x1860 to
0x187D (offset: 0x000x1D in USB RAM space).
Figure4 shows the BDs for seven endpoints and three BD registers for endpoint 0 (in direction). For
endpoint 0 (bidirectional), 5 and 6 (double buffer), each has two BDs. Endpoint of 14 owns only one BD.
The first byte of the BD is the status and control register (see Figure5). It includes four bits that the CPU
reads or writes for controlling the USB communication. They are OWN (bit 7), DATA0/1(bit 6), DTS (bit
3), and BDTSTALL (bit 2).
BDTSTALL can be set by the CPU to stall the next transaction. A STALL handshake is issued on
its associated endpoint if the BDTSTALL bit is set.
The DTS bit is used to enable or disable the data toggle synchronization.
DATA0/1 bit defines a DATA0 or DATA1 packet to be transmitted or received.
OWN bit is implemented as the semaphore mechanism. Its value determines whether the BD and
endpoint buffer is controlled by the CPU or SIE. When it is set to logic 0, the CPU has the control
of the BD and endpoint buffer, the CPU can modify the contents of the BD registers and the data
in the endpoint buffer. If it is set to logic 1, the SIE has the control of BD and endpoint buffer.
The bit[5:2] are updated by SIE after a new token packet is received. The token packet PID is kept
in these four bits.
The firmware designer must rewrite all bits in the status and control the register in terms of the
configuration for the next transaction. Otherwise all the data packets may not be received or transferred.
The wrong setting for DATA0/1 results in the data being discarded by the SIE or the host. The DTS and
BDTSTALL bits can also result in the data packet not being received or being stalled. The values for these
two bits are replaced by bit 0 and bit 1 of the new packets PID. They must be updated before the next
packet is allocated.
The second byte (BC[7:0]) is the length of the data packet to be transferred or the packet that has been
received. It can be set to zero for some packets (e.g, the transaction in the status stage of control transfer).
The maximum length is 64 for the MC9S08J M60 MCU. The BC register must be set to the actual packet
size to be transferred for the in direction endpoint, but it must be initialized to be greater than or equal to
the packet size that is received for out direction endpoint.
The third byte (EPADD[9:2]) keeps the start address of the endpoint buffer. It must be filled with the bit
92 of the relative address in USB RAM. It can be calculated by shifting two bits of the relative address
to right so the endpoint buffer address is four bytes alignment.
Bit 7 6 5 4 3 2 1 0
OWN DATA0/1
0
BDTKPID[3]
0
BDTKPID[3]
DTS
BDTKPID[1]
BDTSTALL
BDTKPID[0]
0 0
Figure 5. Status and Control Register of BD
USB Device Development with the MC9S08JM60, Rev. 1
MC9S08JM60 USB Module Introduction
Freescale Semiconductor 8
Figure 6 shows an example for calculating the value of the EPADD register. Provided that the endpoint
buffer locates 0x1880 (absolute address), the address relative to the start address of USB RAM is 0x20.
Thus the value of EPADD for the endpoint can be calculated by shifting the value 0x20 two bits to right
and the result is 0x08.
Figure 6. Example for Calculating the Value of EPADD Register
Setting these registers correctly is the basic requirement for successful endpoint communication. Figure7
is an example of BDT configuration. It shows the content in the BD registers of the endpoints 0 and 1.
In this example, the buffer length is eight bytes for endpoint 0 (in and out) and four bytes for endpoint 1.
The endpoint buffers are allocated continually from 0x1880 (relative address 0x20).
0
0 x 1860
0 x 1880
= 0x20
0x20
0x08
1
Shift two bits to right
Two low bits are always zero
0 0 0 0 0 0
0 0 0 0 1 0 0 0
MC9S08JM60 USB Module Introduction
USB Device Development with the MC9S08JM60, Rev. 1
Freescale Semiconductor 9
Figure 7. BDT Configuration Example
Endpoint 0 in direction:
If the content of the status and control register is 0x00, the CPU still has the control of BD.
If the BC[7:0] register is filled with 0x08, it means that the packet size transmitted by EP0 in is
eight bytes. This value takes effect when the SIE controls this endpoint.
The maximum packet size for the endpoint is transferred to the host in the USB device enumeration
process. The value for the BC registers can be less than the maximum value the data that exceeds
the maximum length must be divided into several packets. The actual number to be transferred to
the host must be set to the BC register before the OWN bit of the status and control register is set
(hand over the control of BD to SIE).
The value of EPADD[9:2] is 0x08. It can be calculated by the start address allocated for endpoint
0 in direction. Its absolute address is 0x1880. The calculation method is illustrated in Figure6.
Status & Control Bit
BC[7:0]
EPADR[9:2]
Status & Control Bit
.........
Reserved
EP0 OUT Data Buffer
Status & Control Bit
BC[7:0]
EPADR[9:2]
BC[7:0]
EPADR[9:2]
EP1 Data Buffer
Other Ep Buffer
General purpose RAM
EP0 IN Data Buffer
Address
Registers Contents
0x1860 (0x00)
0x1861 (0x01)
0x1862 (0x02)
EP0IN
0x1863 (0x03)
0x1864 (0x04)
0x1865 (0x05)
EP0OUT
0x1866 (0x00)
0x1867 (0x01)
0x1868 (0x02)
EP1IN
Other End Points
0x1866~0x187D
(0x06~0x1D)
0x187E, 0x187F (0x1E,0x1F)
0x1880 (0x20)
0x18A0 (0x40)
0x1890 (0x30)
0x1895 (0x35)
0x195F(0xFF)
Endpoint
Data
Buffer
0x00
0x08
0x08
0x88
0x08
0x0C
0x00
0x04
0x10
Start Address
Buffer
Descriptor
Table
USB Device Development with the MC9S08JM60, Rev. 1
MC9S08JM60 USB Module Introduction
Freescale Semiconductor 10
Endpoint 0 out direction:
The BD for endpoint 0 out direction is set to receive a DATA0 packet. The content of the status and
control register is 0x88. This means the control for BD and data buffer is handed over to SIE
(OWN =1). Then endpoint 0 receives DATA0 (DATA0/1 =0) packet. When the data toggle has
been enabled (DTS =1), the correct data packet is received (STALL =0).
The SIE changes the OWN bit to 0 for handing over the control to the CPU. When one packet is
finished receiving, the exact data length is updated and written to BC register.
Endpoint 1 in direction:
The endpoint 1 is used for the in direction (transmitting data to the host). It is configured to transfer
only four bytes.
NOTE
When developing firmware, the programmer must avoid the address overlap
for different endpoint buffers. In addition, the maximum size of the endpoint
buffer must be the same as the length in the USB configuration descriptor.
The start address of the endpoint buffer must be sixteen bytes aligned in
USB RAM.
As for the space in USB RAM that is not assigned, the firmware can use
them for another purpose.
The flowchart in Figure8 shows the operation of BD registers used for in and out direction. In out
direction, after the data packet is received from the host, the TOKDNEF bit is set, and the CPU becomes
the control of the BD and endpoint buffer (OWN =0). Firmware can deal with this in an interrupt service
routine or in a polling routine. The program reads the length of the received data from the BC register and
then reads out the data from the endpoint buffer and processes it if necessary. After that, firmware updates
the BD register and hands over the control to SIE for next transaction.
MC9S08JM60 USB Module Introduction
USB Device Development with the MC9S08JM60, Rev. 1
Freescale Semiconductor 11
Figure 8. BDT Operation Example
Before the first packet is received, the firmware initializes the associated BD registers and hands over the
control to SIE. Then the USB module is ready to receive data from the host.
In in direction of control transfer, the USB device receives one data packet that includes one request or
command, then the device begins to prepare the data to be delivered to the host according to some
protocols. The firmware writes the data length to the BC register, writes the data to the endpoint data
buffer, then updates the status and control the register (DATA0/1, OWN, etc.). After that, the USB module
begins to wait for the host to read data (in operation).
The TOKDNEF bit is set after the host finishes reading out the data in the endpoint (the transaction has
finished). The USB interrupt is triggered if it is enabled. The new data for the next transaction can be
prepared in an interrupt service routine or in a polling routine.
2.5.2 Double Buffer (Ping-Pong Buffer)
Endpoints 5 and 6 of the MC9S08J M60 MCU USB module are double-buffering endpoints (seeTable1 on
page 15). Either of them has two sets of BD (even and odd) registers. When one BD and its associated
buffer are being operated by SIE at the same time; the CPU can access or control the other BD and attached
data buffer.
The CPU and SIE exchange the control of the BD registers and endpoint buffer after they finish their work.
The CPU gives the control to SIE as processing takes place for the data operation and the configuration of
the BD registers. The SIE returns the control to the CPU and tries to control the other buffer when it
finishes the current transaction. With the communications going on, the CPU and SIE have the control of
two BDs and their attached data buffers in turn, so the double buffer is also called the ping-pong buffer.
TOKDNEF
(OWN =0)
Update the control and
status register
( Toggle the DATA0/1,
OWN=1,DTS =0,
BDTSTALL=0)
Read the length of
the received data
Read the data from
Endpoint buffer
and process data
Update the control and
status register
( Toggle the DATA0/1,
OWN=1,DTS =0,
BDTSTALL=0)
Return
Wait the USB interrupt
TOKDNEF
(OWN =0)
Host send data
(OUT operation)
Host receive data
(IN operation)
Return
Wait the USB interrupt
Endpoint IN operation Endpoint OUT operation
Write the data length
to BC register
Write the data to
endpoint buffer
Clear interrupt Flag
(TOKDNEF =1)
Clear interrupt Flag
(TOKDNEF =1)
USB Device Development with the MC9S08JM60, Rev. 1
MC9S08JM60 USB Module Introduction
Freescale Semiconductor 12
The mechanism of the double buffer makes the CPU and SIE cooperate well without waiting for each other
for a long time hence the efficiency of communication is improved.
The flowchart in Figure9 shows how to use the ping-pong buffer. Both BDs and their attached buffers are
initialized at first. The double buffer has two sets of BD and data buffers that occupy different space in
USB RAM. They both belong to one endpoint and have same direction (in or out) and share one EPCTLx(5
or 6) register.
The ODD bit in the STAT register reflects the operation completed by SIE. The users program
understands that BD and attached buffer is dealt with in terms of the value expressed in this bit. The ODD
bit can be reset to point to even buffer by setting the OODRST bit in the CTL register.
Figure 9. Operation for Double Buffer (Ping-Pong Buffer)
The operation of a ping-pong buffer for in direction is illustrated in Figure9. After the transaction with in
operation ends, the TOKDNEF bit is set and the MCU becomes the controller of BD. Then the firmware
reads the ODD bit to determine which BD and data buffer must be dealt. After that, the data buffer, the BC
register, and the control register are written or updated. The MCU hands over the control to the SIE at last.
For ping-pong buffer operation, the firmware can set one BD to send or receive the DATA0 packet, and
the other for DATA1 packet without toggle (DTS =0). That is, the even buffer is for DATA0 and the odd
buffer is for DATA1.
Configure the BD registers
(EPADDR, BC, status and
control register)
Set the EPCTLn register
(0x04)
Write the data to even
buffer (DATA0)
Set the EP5 even BD
registers (BC, status and
control registers)
ODD
TOKDNEF=1
(EP5 )
Write the data to odd
buffer (DATA1)
Set the EP5 odd BD
registers (BC, status and
control registers)
Hand over the control to SIE
In USB module
initialization routine
Return
IN operation
TOKDNEF =1
Y N
USB Device Development
USB Device Development with the MC9S08JM60, Rev. 1
Freescale Semiconductor 13
3 USB Device Development
3.1 Hardware Design
Hardware design for USB device with MC9S08J M60 is simple and flexible. The designer can choose to
use the on-chip regulator or external power supply and to use the internal pullup or external pullup resistor.
You can use the internal pullup or external pullup resistor. The MCU provides many interfaces such as SPI,
SCI, IIC, ADC, KBI, etc. The designers thus have more selections in design. The hardware design of the
USB interface is discussed in the following sections.
3.1.1 Clocking Generation
USB module requires two clock sources. They are the 24 MHz bus clock and 48 MHz reference clock.
According to the MCG features, the MCG must work in PEE mode (PLL engaged external) and an external
clock source is essential for matching this requirement. An oscillator or crystal is used as the external clock
source.
Example 1 shows the MCG initialization routine by using an external 12 MHz crystal. The system clock
is switched from FEI mode to FBE mode and then to PBE mode and finally to PEE mode. The MCG status
register (MCGSC) is checked in each step to ensure that the clock is stable, locked by PLL (or FLL), and
switched successfully.
Example 1. MCG Initialization Routine Using External 12 MHz Crystal
voi d MCG_I ni t ( )
{
/ * t he MCG i s set t o FEI mode by def aul t , i t shoul d be change t o FBE mode at f i r st */
MCGC2 = 0x36; / *Sel ect hi gh f r equency, hi gh gai n, Osci l l at or r equest ed*/
whi l e( ! ( MCGSC & 0x02) ) ; / *Wai t f or t he st abl e of Osci l l at or */
MCGC1 = 0x9B; / *Ext er nal cl ock i s sel ect ed, RDI V = 0b011 ( t o get 1. 5MHz) */
whi l e( ( MCGSC & 0x1C) ! = 0x08) ;
/ *Check whet her t he ext er nal r ef er ence cl ock i s sel ect ed */
/ * Swi t ch t o PBE mode f r omFBE*/
MCGC3 = 0x48; / *Enabl e PLL, VDI V = 0b1000, mul t i pl y by 32*/
whi l e ( ( MCGSC & 0x48) ! = 0x48) ; / *Wai t f or t he PLL t o be l ocked */
/ *Swi t ch t o PEE mode f r omPBE mode*/
MCGC1 &= 0x3F; / *PLL out put i s sel ect ed*/
whi l e( ( MCGSC & 0x6C) ! = 0x6C) ;
/ *Wai t f or t he PLL out put becomi ng t he syst emcl ock sour ce */
r et ur n;
}
USB Device Development with the MC9S08JM60, Rev. 1
USB Device Development
Freescale Semiconductor 14
3.1.2 USB Device Power
Figure 10. USB Connection Example
The dedicated on-chip regulator powers the USB transceiver. It shares the same power supply with MCU.
The on-chip regulator is enabled or disabled by the USBVREN bit in the USBCTL0 register. When the
on-chip regulator is disabled (USBVREN =0), one external 3.3 V power is connected to V
USB3.3
pin to
provide the power for USB transceiver and pullup resistor.
The nominal input range of this regulator is from 3.9 V to 5.5 V, but the MCU works at lower voltage (2.7
V 5.5 V). When the power supply for MCU is below 3.9 V, an external power supply must be provided
for the USB module and the internal regulator must be disabled.
In addition, it is highly recommended that two capacitors are connected to the V
USB
3.3
pin to decrease the
ripple on the output of the regulator. Their recommended values are 0.47 F, and 4.7 F. (refer to
Figure10), and they must be placed as near as possible to the V
USB3.3
pin.
USB devices have two power modes self-powered and the bus-powered. Refer to the USB specification
for detailed information.
Bus-Powered
Bus-powered USB devices are powered from the USB bus. The maximum current drawn from the
host is 500 mA. The USB device is powered when it is attached to USB bus and the connection is
valid, then the host will find it attached.
External power
(regulator)
XCVR
USB RAM
other control logic
Regulator
USB module
USBVREN
USBPU
Internal
pull-up
C1
4.7 uF
C2
0.47 uF
USBDP
USBDN
MC9S08J M60
MCU
VDD (2.7 V 5.5 V)
(3.9 V 5.5 V)
Vss
Rs
Rf
X1
C4
C3 EXTAL
XTAL (3 V 3.6 V)
1) USB power : VBus
(bus powered)
2) External power
(self-powered)
Note:
1) C1 and C2 are highly recommended when using an internal regulator.
2) Place R1, R2, C1 and C2 as close as possible to the MCU.
VUSB33
External
pull-up
GND
D-
D+
Optional
Ferrite bead
Optional
Ferrite bead
VBUS
C5 C6
VDD or
external
regulator
R1 (33 )
R2 (33 )