You are on page 1of 29

NuMicro

USB Device Controller

NuMicro@nuvoton.com

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

2
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 )

3
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.

4
USB Block Diagram
DPLL use the 48MHz

CLKGEN NVIC

Floating detect
FLDET Wakeup
DEBOUNCE DPLL INT
SFR

USB_DP RXDP
ENDPOINT
USB_DM RXDM CONTROL SRAM
SIE 512
S0 BUFFER
BYTE
CONTROL
S1

Transceiver
APB WRAPPER

APB Bus

5
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.

6
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

7
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)

8
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.

9
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


EP0 SA =USB_BASE + 0x108h
BUFSEG0 = 0x8
EP0 SRAM Buffer: 64 bytes MXPLD0 = 0x40

BUFSEG1 = 0x48 EP1 SA =USB_BASE + 0x148h


EP1 SRAM Buffer: 64 Bytes MXPLD1 = 0x40

BUFSEG2 = 0x88 EP2 SA =USB_BASE + 0x188h

EP2 SRAM Buffer 512


Bytes

BUFSEG3 = 0x100 EP3 SA =USB_BASE + 0x200h

EP3 SRAM Buffer

11
Register Map General Setting
Register Offset 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 R 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 R 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

12
Endpoint Setting
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 USB host wants to transmit data to device
controller controller

DATA0/ DATA0/
Data
DATA1 DATA1

1. Prepare related data into the specified Hardware will buffer theseIdle
1. data to
endpoint buffer. specified endpoint buffer.
2. The start address of endpoint buffer is 2. The start address of endpoint buffer is
Host Device
defined in USB_BUFSEGx. defined in USB_BUFSEGx.
3. Need to write the actual data length in 3. Hardware will record the data length in
USB_MXPLDx. USB_MXPLDx.

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 Endpoint Class Function
Descriptors Configuration Handlers

USB Control Pipe USB Event


USB Initiator Handlers Handlers
Driver

Register & Interrupt control

USB (un)Plug Wakeup Bus USB Endpoint


Controller Event Event Events Events 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

Dispatch USB
Events
Open USB
Class Driver

Call USB
Function
USB Plug-in
Handlers
Detect

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. */
4. Dispatch USB events
_DRVUSB_ENABLE_MISC_INT(0); 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, 26


mouse cursor moving to right on the screen
Run “Smpl_HID_IO” Code & “HID AP.exe”

Customer_CD

NUC100SeriesBSP

NuvotonPlatform_Keil

Sample

NUC1xx-LB_002

Smpl_HID_IO.uvproj
Smpl_HID_IO

Software GUI HID AP.exe

27
Run “HID AP.exe” Result

28
Q&A

Thank You

29

You might also like