11 NuMicro USB

29
1 NuMicro USB Device Controller [email protected]

description

NuMicro USB

Transcript of 11 NuMicro USB

  • 1

    NuMicro USB Device Controller

    [email protected]

  • 2

    Agenda

    Features

    USB Block Diagram

    Function Description

    Buffer Control

    Register Map

    USB Driver Framework

    Control Flow of Run a USB Device

    HID Sample Code

  • 3

    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 )

  • 4

    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.

  • 5

    USB Block Diagram

    RXDP

    RXDM

    S0

    S1

    Transceiver

    USB_DP

    USB_DMSIE

    FLDET

    DEBOUNCE DPLL

    ENDPOINT

    CONTROL

    BUFFER

    CONTROL

    SRAM

    512

    BYTE

    APB WRAPPER

    APB Bus

    SFRINT

    Wakeup

    CLKGEN NVIC

    Floating detect

    DPLL use the 48MHz

  • 6

    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.

  • 7

    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

  • 8

    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)

  • 9

    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.

  • 10

    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.

  • 11

    Buffer Control

    USB SRAM Start Address USB_SRAM = USB_BASE + 0x0100h

    BUFSEG0 = 0x8

    BUFSEG1 = 0x48

    EP0 SRAM Buffer: 64 bytes

    EP1 SRAM Buffer: 64 Bytes

    EP2 SRAM Buffer

    BUFSEG2 = 0x88

    EP0 SA =USB_BASE + 0x108h

    MXPLD0 = 0x40

    EP1 SA =USB_BASE + 0x148h

    MXPLD1 = 0x40

    Setup Token Buffer: 8 bytes

    EP2 SA =USB_BASE + 0x188h

    EP3 SRAM Buffer

    BUFSEG3 = 0x100 EP3 SA =USB_BASE + 0x200h

    512

    Bytes

  • 12

    Register Map 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

    General Setting

    Endpoint Setting

  • 13

    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.

  • 14

    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.

  • 15

    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.

  • 16

    USB_BUFSEGx & USB_MXPLDx Register

    Token

    Idle

    Idle

    IN OUT

    DATA0/ DATA1

    Data DATA0/ DATA1

    Device Host

    1. Prepare related data into the specified endpoint buffer.

    2. The start address of endpoint buffer is defined in USB_BUFSEGx.

    3. Need to write the actual data length in

    USB_MXPLDx.

    USB host has requested data from device controller

    USB host wants to transmit data to device controller

    1. Hardware will buffer these data to specified endpoint buffer.

    2. The start address of endpoint buffer is defined in USB_BUFSEGx.

    3. Hardware will record the data length in USB_MXPLDx.

  • 17

    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.

  • 18

    USB Driver Framework (1/3)

    Endpoint

    Configuration

    USB

    Descriptors

    Class Function

    Handlers

    Control Pipe

    Handlers

    USB

    Initiator

    USB Event

    Handlers

    Register & Interrupt control

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

    (un)Plug

    Event

    Wakeup

    Event

    Bus

    Events

    USB

    Events

    Endpoint

    Events

    USER/USB

    Class

    USB

    Driver

    USB

    Controller

  • 19

    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.

  • 20

    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.

  • 21

    Control Flow of Run a USB Device

    Open USB

    Driver

    USB Plug-in

    Detect

    Dispatch USB

    Events

    Call USB

    Function

    Handlers

    Open USB

    Class Driver

  • 22

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

    /* LOCKREG */

    LOCKREG(); /* Update system core clock */ SystemCoreClockUpdate();

    /* Execute HID process */

    HID_MainProcess();

    Chip System Setting

    Execute HID Process

  • 23

    HID Sample Code (2/3)

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

    int32_t i32Ret = 0; 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. */

    _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();

    1. Open USB Driver

    2. Open USB Class Driver

  • 24

    HID Sample Code (3/3) /* Poll and handle USB events. */

    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

    3. USB Plug-in Detect

  • 25

    Run Smpl_HID Code

    Customer_CD

    NUC100SeriesBSP

    NuvotonPlatform_Keil

    USB

    Sample

    Smpl_HID.uvproj Smpl_HID

  • 26

    Run Smpl_HID Code

    Download load and free run, after to press INT1 Key, mouse cursor moving to right on the screen

  • 27

    Run Smpl_HID_IO Code & HID AP.exe

    Customer_CD

    NUC100SeriesBSP

    NuvotonPlatform_Keil

    NUC1xx-LB_002

    Sample

    Smpl_HID_IO.uvproj Smpl_HID_IO

    HID AP.exe Software GUI

  • 28

    Run HID AP.exe Result

  • 29

    Q&A

    Thank You