Transcript USB_device

NET+OS 6.1 Training
7/7/2015
NetSilicon & Digi Confidential
USB Device
7/7/2015
NetSilicon & Digi Confidential
USB Device topics
•
•
•
•
Introduction / Performance
USB device driver APIs and internal
USB Examples
USB test setup info
7/7/2015
NetSilicon & Digi Confidential
USB device programming
•
•
•
•
13 DMA channels
A set of USB device IP control registers
Number of BBUS DMA control registers
File I/O functions: open, close, read, write,
and ioctl.
• Dedicated I/O lines – No change of gpio.h
except for a GPIO line used for PNP.
7/7/2015
NetSilicon & Digi Confidential
Performance for Bulk-Out/In
•
•
•
•
•
750KBytes/second
Supporting of PNP for printing device.
Support USB 1.1/ USB2.0 Low & Full Speed
Support Control, Bulk, Interrupt transfers
Driver is used in: Hitachi SL5 printer.
7/7/2015
NetSilicon & Digi Confidential
Device Open()
• Calls usb_dev_open()
• The open function opens the specific DMA
channel for file I/O operation.
• fd=open(“/usb/0”,USB_CTRL);
• The USB device channels are: /usb/0, /usb/1,
… /usb/12.
• The USB mode are: O_WRONLY,
O_RDONLY and USB_CTRL.
7/7/2015
NetSilicon & Digi Confidential
Device close()
• Calls usb_dev_close()
• Close a specific DMA channel from further
access.
• Example: Close(fd);
7/7/2015
NetSilicon & Digi Confidential
Device read()
• Calls usb_dev_read.
• int usb_dev_read(int dmaIndex, void *
buffer, int length, int * notUsed)
• Example:
• Ret=Read(fd, (char*) buffer, bufferlength);
• Buffer is returned from a read callback
function in an ISR context. DON’T access it
before it is returned.
• Can not read DMA ch 0 and 1.
7/7/2015
NetSilicon & Digi Confidential
Device Write()
• Calls usb_dev_write().
• int usb_dev_read(int dmaIndex, void *
buffer, int length, int * notUsed);
• Example: ret=write(fd, (char *) buffer,
bufferLength);
• The buffer is returned from a write callback
function in an ISR context. Do not access it
before it is returned.
• Can not write to fds for DMA ch 0 and 1.
7/7/2015
NetSilicon & Digi Confidential
Internal Driver Structure
USB Device Read operation
User Application
Read Callback
USB device DMA
ISR NCIP
Read()
MCUsbActivateDmaChannel()
MCUsbProcessDMADesc()
Push buffer into Read Queue/
Func:MCUsbSetDataBuffer
DMA Buffer Descriptor Manager
Func: MCUsbFillDmaDesc()
Pop data from Read Buffer
Return Queue
Pop buffer from Read buffer
Queue, Push buffer into the
read buffer return Queue and
assign the buffer to DMA buffer
descriptors
Read Buffer
Queue
Read Buffer
Return Queue
USB Device Write operation
User Application
Write Callback
USB device DMA
ISR NCIP
Write()
MCUsbProcessDMADesc()
MCUsbActivateDmaChannel()
Push buffer into Write Queue/
Func:MCUsbSetDataBuffer
Pop data from Write Buffer
Return Queue
DMA Buffer Descriptor Manager
Func: MCUsbFillDmaDesc()
Write Buffer
Queue
7/7/2015
Pop buffer from Write buffer
Queue, Push buffer into the
Write buffer return Queue and
assign the buffer to DMA buffer
descriptors
NetSilicon & Digi Confidential
Write Buffer
Return Queue
Interrupt State
USB Device Interrupt
Is a USB
interrupt
Yes
No
Is SetCfg
or SetIntf
Yes
No
Set up the
appropriate
configuation
Is it a FIFO
status interrupt
Yes
Yes
End of Isr
No
Is it a FIFO
Error interrupt?
Is it a DMA
interrupt?
Yes
usbTakeDmaOut
OfReset()
Yes
Is it channel 0
Control-out
endpoint
No
Is it a USB-IN
request?
No
Yes
No
Is it channel 1
Control-IN
endpoint
No
MCUsbParseDataStruct()
Yes
Call
requestToSendC
allBackFunc()
No
Is it other Non
Control
Endpoints?
Clear Isr
Yes
Yes
Is it a FIFO
NACK Isr
MCUsbProcessD
maDesc()
Call
MCUsbResetFifo
Nack()
7/7/2015
MCUsbActivateD
maChannel()
NetSilicon & Digi Confidential
No
Device Ioctl
•
•
•
•
Calls usb_dev_ioctl()
Fd is limited to those of DMA ch 0 and 1.
Some important flags for the ioctl
USB_SET_REQUEST_CALLBACK_FUNCTION
USB_SET_RECV_BUFFER_CALLBACK_FUNC
TION
• USB_SET_WRITE_BUFFER_CALLBACK_FUN
CTION
• USB_FLUSH_BUFFER
• USB_SET_DMA_LOADING_IN_BATCH
7/7/2015
NetSilicon & Digi Confidential
Other APIs
• UsbBuildInterfaceDesc(USB_INTERFACE_DESC
RIPTOR * itd,
MC_USB_INTERFACE_DESCRIPTOR
*pUsbIT,MC_USB_ENDPOINT_DESCRIPTOR
endpointArray[], size_t endpointNumber);
• MCUsbBuildConfigurationDesc(USB_CONFIG_D
ESCRIPTOR* cfd,
MC_USB_CONFIGURATION_DESCRIPTOR
*pUsbCD, USB_INTERFACE_DESCRIPTOR
usbInterfaceDesc[], size_t numInterface);
7/7/2015
NetSilicon & Digi Confidential
Example
• Typical User application
• usb_dev[0] = open ("/usb/0", USB_CTRL);
• usb_dev[1] = open ("/usb/1", USB_CTRL);
• usb_dev[2] = open ("/usb/2", USB_BULK);
• usb_dev[3] = open ("/usb/3", USB_BULK);
7/7/2015
NetSilicon & Digi Confidential
Example
• ret = ioctl (usb_dev[0],
USB_SET_WRITE_BUFFER_CALLBACK_FUN
CTION, (void *) writeCallBackFunc);
• ret = ioctl (usb_dev[0],
USB_SET_RECV_BUFFER_CALLBACK_FUNC
TION, (void *) readCallBackFunc);
• ret = ioctl (usb_dev[0],
USB_SET_REQUEST_CALLBACK_FUNCTION
, (void *) requestToSendCallBackFunc);
7/7/2015
NetSilicon & Digi Confidential
Typical Callback Function
• void readCallBackFunc (unsigned int channel, char *ptrData, int
bufferLen, int dataRead)
• {…. int usbCallbackData[4]; int status;
usbCallbackData[0] = (unsigned long) ptrData;
usbCallbackData[1] = (unsigned long) bufferLen;
usbCallbackData[2] = (unsigned long) dataRead;
usbCallbackData[3] = channel; /* 1 = read */
status = tx_queue_send(&usbQueue, usbCallbackData,
TX_NO_WAIT);
tx_event_flags_set (&usbEvent, USB_READ_EVENT_FLAG,
TX_OR);
return};
7/7/2015
NetSilicon & Digi Confidential
Typical Write callback function
• void writeCallBackFunc (unsigned int channel, char
*ptrData, int bufferLen, int dataWritten)
{ int usbCallbackData[4]; int status;
usbCallbackData[0] = (unsigned long) ptrData;
usbCallbackData[1] = (unsigned long) bufferLen;
usbCallbackData[2] = (unsigned long) dataWritten;
usbCallbackData[3] = channel; /* 1 = read */
status = tx_queue_send(&usbQueue, usbCallbackData,
TX_NO_WAIT);
/* process the finished data buffer here */
tx_event_flags_set (&usbEvent,
USB_WRITE_EVENT_FLAG, TX_OR);
return; }
7/7/2015
NetSilicon & Digi Confidential
Typical Read() call
for (i = 0; i < 8; i++)
{
ret = read (usb_dev[2], (char *) dataBuffer[2][i],
4096);
if (ret != 0)
{
printf("Read failed.\n");
break;
}
}
7/7/2015
NetSilicon & Digi Confidential
PNP function control
• Using one of the GPIO line.
dmaChan = 2; /* two control endpoints */
ioctl(usb_dev[0], USB_SET_CONTROL_ENDPOINT, (void *) &dmaChan);
/* setting up control Desc */
MCInstallIsr (USB_INTERRUPT, (MC_ISR_HANDLER) usbDmaIsr, (void *)
2, 0);
/* Turn off the GPIO pin #17 to support PNP */
MCsetGPIOpin(17, 0);
/* set up the GPIO configuration */
*(unsigned long*) 0x90600018 |= 0x000000b0;
7/7/2015
NetSilicon & Digi Confidential
Important files
• Driver: Src/bsp/devices/ns9750/usb/*.*
• Example:
(1) src/examples/nausbdevapp/data.c
(2) src/examples/nausbdevapp/usb_test.c
7/7/2015
NetSilicon & Digi Confidential
Data.c
• Has important Endpoint, Interface,
Configuration data.
• Mostly reusable in a typical application.
7/7/2015
NetSilicon & Digi Confidential
Reference
• References:
http://www.intel-u-press.com/usb_dbe/USBdocs/USBspec.pdf
7/7/2015
NetSilicon & Digi Confidential