220 likes | 231 Views
NET+OS 6.1 Training. USB Device. USB Device topics. Introduction / Performance USB device driver APIs and internal USB Examples USB test setup info. USB device programming. 13 DMA channels A set of USB device IP control registers Number of BBUS DMA control registers
E N D
NET+OS 6.1 Training NetSilicon & Digi Confidential
USB Device NetSilicon & Digi Confidential
USB Device topics • Introduction / Performance • USB device driver APIs and internal • USB Examples • USB test setup info 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. 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. 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. NetSilicon & Digi Confidential
Device close() • Calls usb_dev_close() • Close a specific DMA channel from further access. • Example: Close(fd); 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. 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. NetSilicon & Digi Confidential
Internal Driver Structure NetSilicon & Digi Confidential
Interrupt State NetSilicon & Digi Confidential
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_FUNCTION • USB_SET_WRITE_BUFFER_CALLBACK_FUNCTION • USB_FLUSH_BUFFER • USB_SET_DMA_LOADING_IN_BATCH NetSilicon & Digi Confidential
Other APIs • UsbBuildInterfaceDesc(USB_INTERFACE_DESCRIPTOR * itd, MC_USB_INTERFACE_DESCRIPTOR *pUsbIT,MC_USB_ENDPOINT_DESCRIPTOR endpointArray[], size_t endpointNumber); • MCUsbBuildConfigurationDesc(USB_CONFIG_DESCRIPTOR* cfd, MC_USB_CONFIGURATION_DESCRIPTOR *pUsbCD, USB_INTERFACE_DESCRIPTOR usbInterfaceDesc[], size_t numInterface); 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); NetSilicon & Digi Confidential
Example • ret = ioctl (usb_dev[0], USB_SET_WRITE_BUFFER_CALLBACK_FUNCTION, (void *) writeCallBackFunc); • ret = ioctl (usb_dev[0], USB_SET_RECV_BUFFER_CALLBACK_FUNCTION, (void *) readCallBackFunc); • ret = ioctl (usb_dev[0], USB_SET_REQUEST_CALLBACK_FUNCTION, (void *) requestToSendCallBackFunc); 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}; 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; } 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; } } 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; 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 NetSilicon & Digi Confidential
Data.c • Has important Endpoint, Interface, Configuration data. • Mostly reusable in a typical application. NetSilicon & Digi Confidential
Reference • References: http://www.intel-u-press.com/usb_dbe/USBdocs/USBspec.pdf NetSilicon & Digi Confidential