Vous êtes sur la page 1sur 29

NuMicro

USB Device Controller

NuMicro@nuvoton.com

Agenda
Features
USB Block Diagram
Function Description
Buffer Control
Register Map
USB Driver Framework
Control Flow of Run a USB Device
HID Sample Code

Features (1/2)
One set of USB 2.0 full-speed device controller and
transceiver.
Compliant with USB 2.0 Full-Speed device specification.
Provide 1 interrupt vector with 4 different interrupt
events.
WAKEUP ( wake up the system clock)
FLDET ( Floating detect interrupt )
USB ( USB events, like IN ACK, OUT ACK )
BUS ( Bus events, like reset, suspend, resume )

Features (2/2)
Support 4 transfer types
Control
Bulk
Interrupt
Isochronous

Provide 6 endpoints for configurable


Control/Bulk/Interrupt/Isochronous transfer types
Include 512 bytes internal SRAM as data buffer.

USB Block Diagram


DPLL use the 48MHz
CLKGEN

Floating detect

USB_DP

RXDP

USB_DM

RXDM
S0

FLDET
DEBOUNCE

SIE

DPLL

NVIC

Wakeup

INT
SFR

ENDPOINT
CONTROL
BUFFER
CONTROL

S1

SRAM
512
BYTE

Transceiver
APB WRAPPER

APB Bus

Function Description (1/5)


SIE (Serial Interface Engine)
The front-end of the device controller and handle most of the USB
packet protocol.
The function could include:
Packet recognition, transaction sequence
SOP, EOP, RESET, RESUME signal detection / generation.
Clock / Data separation
NRZI Data encoding / decoding and bit-stuffing
CRC & Packet ID generation / checking
Serial-Parallel / Parallel-Serial conversion.

Function Description (2/5)


Endpoint Control
There are 6 endpoint
All the operations including Control, Bulk, Interrupt and
Isochronous transfer are implemented in it.

DPLL (Digital Phase Lock Loop)


Use 48MHz to lock the input data from RXDP and RXDM.
The bit rate of USB is 12MHz. It is converted from DPLL

Function Description (3/5)


Floating De-bounce
A USB device may be plug-in or unplug from USB Host.
Provide hardware de-bounce for USB floating detect interrupt to
avoid bounce problems. (10ms)

Function Description (4/5)


Interrupt
Provide 1 interrupt vector with 4 interrupt events (WAKEUP,
FLDET, USB and BUS)
WAKEUP is only present after stop wakeup.
After the chip enters power down mode, any change on D+, D- and
floating detect pin can wake up this chip.

FLDET is for USB plug-in or unplug.


USB is to notify user to any USB event on the bus.
Acknowledge what kind of request is to which endpoint and take
necessary response.

BUS notifies users of some bus events, like USB reset, suspend,
time-out, and resume.

Function Description (5/5)


Power Saving
USB turns off PHY automatically to save power while this chip
enters power down mode.
Furthermore, user can write 0 to register ATTR[4] to turn off
PHY under special conditions like suspend to save power.

10

Buffer Control
USB SRAM Start Address USB_SRAM = USB_BASE + 0x0100h

Setup Token Buffer: 8 bytes


BUFSEG0 = 0x8
BUFSEG1 = 0x48

EP0 SRAM Buffer: 64 bytes

EP0 SA =USB_BASE + 0x108h


MXPLD0 = 0x40

EP1 SRAM Buffer: 64 Bytes

EP1 SA =USB_BASE + 0x148h


MXPLD1 = 0x40

BUFSEG2 = 0x88

EP2 SA =USB_BASE + 0x188h


EP2 SRAM Buffer

BUFSEG3 = 0x100

512
Bytes
EP3 SA =USB_BASE + 0x200h

EP3 SRAM Buffer

11

Register Map
Register

Offset

General Setting
R/W

Description

Reset Value

USB_BA = 0x4006_0000
USB_INTEN

USB_BA+0x000

R/W

USB Interrupt Enable Register

0x0000_0000

USB_INTSTS

USB_BA+0x004

R/W

USB Interrupt Event Status Register

0x0000_0000

USB_FADDR

USB_BA+0x008

R/W

USB Device Function Address Register

0x0000_0000

USB_EPSTS

USB_BA+0x00C

USB Endpoint Status Register

0x0000_00x0

USB_ATTR

USB_BA+0x010

R/W

USB Bus Status and Attribution Register

0x0000_0040

USB_FLDET

USB_BA+0x014

USB Floating Detected Register

0x0000_0000

USB_BUFSEG

USB_BA+0x018

R/W

Setup Token Buffer Segmentation Register

0x0000_0000

USB_BUFSEG0

USB_BA+0x020

R/W

Endpoint 0 Buffer Segmentation Register

0x0000_0000

USB_MXPLD0

USB_BA+0x024

R/W

Endpoint 0 Maximal Payload Register

0x0000_0000

USB_CFG0

USB_BA+0x028

R/W

Endpoint 0 Configuration Register

0x0000_0000

USB_CFGP0

USB_BA+0x02C

R/W

Endpoint 0 Set Stall and Clear In/out ready Control Register

0x0000_0000

Endpoint Setting

12

Register Function (1/4)


General Setting:
USB_INTEN
Enable / Disable USB related interrupt.

USB_INTSTS
Any USB event will cause an interrupt, and user just needs to
check the related event flags.

USB_FADDR
A 7-bit value uses as the address of a device on the bus.

USB_EPSTS
User can check related bits to know what kind of USB event was
occurred.

13

Register Function (2/4)


General Setting:
USB_ATTR (Bus Status & Attribution Register)
Acknowledge BUS events.
Pull-up register on USB_DP enable.
Enable and disable PHY transceiver.

USB_FLDET (Floating Detected Register)


Acknowledge USB plug-in / unplug by reading this register.

USB_BUFSEG (Setup Token Segmentation Register)


User needs to set the effective starting address of USB buffer for
setup token only.

14

Register Function (3/4)


Endpoint Setting:
USB_BUFSEGx, x=0~5 (Endpoint x Buffer Segmentation
Register)
User needs to set the effective starting address of USB buffer for
each endpoint (0~5) buffer.

MXPLDx, x=0~5 (Endpoint x Maximal Payload Register)


Once MXPLD is written, the data packets will be
transmitted/received immediately after IN/OUT token arrived.

15

USB_BUFSEGx & USB_MXPLDx Register


Idle

IN

OUT

Token

USB host has requested data from device


controller
DATA0/
DATA1

1.
2.
3.

Prepare related data into the specified


endpoint buffer.
The start address of endpoint buffer is
Host
defined in USB_BUFSEGx.
Need to write the actual data length in
USB_MXPLDx.

USB host wants to transmit data to device


controller

DATA0/
DATA1

Data

Hardware will buffer theseIdle


data to
specified endpoint buffer.
2. The start address of endpoint buffer is
Device
defined in USB_BUFSEGx.
3. Hardware will record the data length in
USB_MXPLDx.
1.

16

Register Function (4/4)


Endpoint Setting:
USB_CFGx, x=0~5 (Endpoint x s Configuration Register)
Each endpoint needs to be configured properly in advance for its
attribution (IN, OUT or ISO state) and endpoint number.

USB_CFGPx, x=0~5 (Endpoint x s Set Stall and Clear In/Out


Ready Control Register)
User can force device to respond STALL.
User can write 1 to clear the IN / OUT token had ready to
transmit / receive the data.

17

USB Driver Framework (1/3)


USB Class (HID / UAC / UDC ...)
USER/USB
Class

USB
Driver

USB
Descriptors

Endpoint
Configuration

Class Function
Handlers

USB
Initiator

Control Pipe
Handlers

USB Event
Handlers

Register & Interrupt control

USB
Controller

(un)Plug
Event

Wakeup
Event

Bus
Events

USB
Events

Endpoint
Events

18

USB Driver Framework (2/3)


USB Controller
The USB controller can issue SETUP or IN/OUT event when
getting relative tokens.

USB Driver
Initialize the USB control according to the USB endpoint
Process the standard requests of control pipe of USB
Call the relative USB event handlers.

19

USB Driver Framework (3/3)


USB Class
In this layer, user should define the all descriptors, such as
device descriptor, configuration descriptors, interface descriptors,
endpoint descriptors and class descriptors and etc.
An endpoint configuration to define the USB buffer usage of
each endpoint is also required.
The relative device/class function handler also needs to be
implemented here.

20

Control Flow of Run a USB Device


Open USB
Driver

Open USB
Class Driver

USB Plug-in
Detect

Dispatch USB
Events

Call USB
Function
Handlers

21

HID Sample Code (1/3)


/********************************** Main.c *****************************************/
/* UNLOCKREG */
UNLOCKREG();
/* Enable External 12MHz */
SYSCLK->PWRCON.XTL12M_EN = 1;
/* Enable PLL */
DrvSYS_SetPLLMode(0);
Delay(1000);
/* Switch to PLL clock */
DrvSYS_SetHCLKSource(2);
Delay(100);

Chip System Setting

/* LOCKREG */
LOCKREG();
/* Update system core clock */
SystemCoreClockUpdate();
/* Execute HID process */
HID_MainProcess();

Execute HID Process


22

HID Sample Code (2/3)


/******************************* HID_MainProcess *********************************/

int32_t i32Ret = 0;
1. Open USB Driver
E_DRVUSB_STATE eUsbState;
i32Ret = DrvUSB_Open((void *)DrvUSB_DispatchEvent);
if(i32Ret != 0)
return i32Ret;
while(1)
{
/* Disable USB-related interrupts. */

_DRVUSB_ENABLE_MISC_INT(0);
/* Enable float-detection interrupt. */

2. Open USB Class Driver

_DRVUSB_ENABLE_FLDET_INT();
/* Start HID and install the callback functions to handle in/out report */
HID_Init();
/* Enable USB-related interrupts. */
_DRVUSB_ENABLE_MISC_INT(INTEN_WAKEUP | INTEN_WAKEUPEN
| INTEN_FLDET | INTEN_USB | INTEN_BUS);
/* Enter power down to wait USB attached. */
PowerDown();
23

HID Sample Code (3/3)


/* Poll and handle USB events. */

3. USB Plug-in Detect

while(1)
{
eUsbState = DrvUSB_GetUsbState();
if (eUsbState == eDRVUSB_DETACHED)
{
DBG_PRINTF(USB Detached!\n);
break;
}
if (eUsbState == eDRVUSB_SUSPENDED)
{
/* Enter suspend here */
PowerDown();
}
else
{
/* Set HID IN report for interrupt IN transfer */
HID_SetInReport();
}
}
/* Disable USB-related interrupts. */

_DRVUSB_ENABLE_MISC_INT(0);

4. Dispatch USB events


5. Call USB Function Handler

}
24

Run Smpl_HID Code


Customer_CD
NUC100SeriesBSP

NuvotonPlatform_Keil

Sample

USB
Smpl_HID

Smpl_HID.uvproj

25

Run Smpl_HID Code

Download load and free run, after to press INT1 Key,


mouse cursor moving to right on the screen

26

Run Smpl_HID_IO Code & HID AP.exe


Customer_CD
NUC100SeriesBSP

NuvotonPlatform_Keil

Sample
NUC1xx-LB_002
Smpl_HID_IO

Smpl_HID_IO.uvproj

Software GUI

HID AP.exe

27

Run HID AP.exe Result

28

Q&A

Thank You

29

Vous aimerez peut-être aussi