Presentation is loading. Please wait.

Presentation is loading. Please wait.

Www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University Universal Serial Bus & SD card 5/30/2013 Richard Kuo Assistant.

Similar presentations


Presentation on theme: "Www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University Universal Serial Bus & SD card 5/30/2013 Richard Kuo Assistant."— Presentation transcript:

1 www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University Universal Serial Bus & SD card 5/30/2013 Richard Kuo Assistant Professor

2 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Outline ► USB Introduciton –13.NuMicro_USB_SDcard.ppt ► Exercise : USB Virtual Com. Port (Smpl_VCOM) –Using UART0 to USB ► Exercise : USB HID (Smpl_HID) –Emulating USB Human Interface Device (keyboad, mouse, digitizer) ► Exercise : Audio player with SDcard (Smpl_SDcard_ADPCM) –MCU decode.wav file read from SDcard ► Exercise : signal recording with SDcard (Smpl_SDcard_ADC7) –MCU record signal input of pin ADC7 and store 12-bit data into SDcard ► To design a SDcard driver (Driver_SDcard.c) using FAT File System

3 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw NuMicro USB Features ► Built-in USB2.0 full-speed device controller and transceiver ► Compliant to USB 2.0 Full-Speed specification ► Provide one interrupt vector supporting the following types –WAKEUP ( Stop mode wakeup ) –FLO ( Floating detect interrupt ) –USB ( USB events, like IN ACK, OUT ACK ) –BUS ( Bus events, like reset, suspend )

4 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw NuMicro USB Features ► Supporting 4 transmission modes : –Control –Bulk –Interrupt –Isochronous ► Support 6 endpoints , configurable transmission modes (Control/Bulk/Interrupt/Isochronous) ► Built-in USB buffer is 512 bytes SRAM

5 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw USB2.0 Controller Block Diagram

6 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw USB Controller Description ► UIE –The endpoints management –All the transfer operations 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 ► Floating De-bounce –A USB device may be plug-in or unplug from USB. –Provide hardware de-bounce for USB floating detect

7 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Function Description ► SIE –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 CRC & Packet ID generation / checking Serial-Parallel / Parallel-Serial conversion.

8 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Function Description ► Interrupt –Provide a interrupt signal with 4 interrupt events (WAKEUP, FLO, 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. –FLO 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 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Function Description ► 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 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Register Map 10 RegisterOffsetR/WDescriptionReset Value USB_BA = 0x4006_0000 IEF USB_BA+0x000 R/W Interrupt Enable Flag0x0000_0000 EVF USB_BA+0x004 R/W&C Interrupt Event Flag0x0000_0000 FADDR USB_BA+0x008 R/W Function Address0x0000_0000 STS USB_BA+0x00C R System state0x0000_00x0 ATTR USB_BA+0x010 R/W R Bus state & attribution0x0000_0040 FLODETB USB_BA+0x014 R Floating detect0x0000_0000 BUFSEG USB_BA+0x018 R/W Buffer Segmentation0x0000_0000 BUFSEG0 USB_BA+0x020 R/W Buffer Segmentation of endpoint 00x0000_0000 MXPLD0 USB_BA+0x024 R/W Maximal payload of endpoint 00x0000_0000 CFG0 USB_BA+0x028 R/W Configuration of endpoint 00x0000_0000 CFGP0 USB_BA+0x02C R/W W&C stall control register and In/out ready clear flag of endpoint 00x0000_0000 General Setting Endpoint Setting

11 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Register Function ► Normal Configuration: –IEF (Interrupt Enable Register) Enable / Disable USB related interrupt. –EVF (Interrupt Event Flag Register) Any USB event will cause an interrupt, and user just needs to check the related event flags. –FADDR (Function Address Register) A 7-bit value uses as the address of a device on the bus. –STS (System States Register) User can check related bits to know what kind of USB event was occurred.

12 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Register Function –ATTR (Bus States & Attribution Register) Acknowledge BUS events from ATTR. –FLODETB (Floating Detection Register) Acknowledge USB plug-in / unplug by reading this register. –BUFSEG (Buffer Segmentation Register) User needs to set the effective starting address of USB buffer for setup token only.

13 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Register Function ► Endpoint Setting: –BUFSEGx, x=0~5 (Buffer Segmentation Register) User needs to set the effective starting address of USB buffer for each endpoint (0~5) buffer. –MXPLDx, x=0~5 (Maximal Payload Register) Once MXPLD is written, the data packets will be transmitted/received immediately after IN/OUT token arrived.

14 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw BUFSEGx & MXPLDx Register Token Idle IN OUT DATA0/ DATA1 Data DATA0/ DATA1 DeviceHost 1.Prepare related data into the specified endpoint buffer. 2.The start address of endpoint buffer is defined in BUFSEGx. 3.Need to write the actual data length in 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 BUFSEGx. 3.Hardware will record the data length in MXPLDx.

15 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Register Function ► Endpoint Setting: –CFGx, x=0~5 (Configuration Register) Each endpoint needs to be configured properly in advance for its attribution (IN, OUT or ISO state) and endpoint number. –CFGPx, x=0~5 (Extra Configuration Register) User can force device to respond STALL.

16 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Software driver & Sample Code ► Website download “ NUC1XXBSP.zip ” ► http://www.nuvoton.com/hq/enu/ProductAndSales/ProductLines/Cons umerElectronicsIC/ARMMicrocontroller/ARMCortexTMM0/Softwar eLibrary.htm http://www.nuvoton.com/hq/enu/ProductAndSales/ProductLines/Cons umerElectronicsIC/ARMMicrocontroller/ARMCortexTMM0/Softwar eLibrary.htm ► Document: –“ NUC1XX Driver Reference Guide.pdf ” ► USB Driver files –DrvUSB.c –DrvUSB.h ► USB Sample Code –Smpl_HID (Human Interface Devices) –Smpl_UAC (USB Audio Class) –Smpl_UDC (USB Device Controller)

17 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw USB Driver Framework

18 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw USB Driver Framework ► 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 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw USB Driver Framework ► 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 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Control Flow of Run a USB Device

21 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_VCOM int32_t main (void) { STR_UART_T sParam; UNLOCKREG(); DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0); // set UART clock source to 12MHz DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1); // enable 12MHz crystal DrvSYS_SetPLLMode(0); // enable PLL DrvSYS_SelectHCLKSource(2); // switch to PLL clock Delay(100); SystemCoreClockUpdate(); DrvGPIO_InitFunction(E_FUNC_UART0); // set UART0 pins /* UART Setting */ sParam.u32BaudRate = 9600; sParam.u8cDataBits = DRVUART_DATABITS_8; sParam.u8cStopBits = DRVUART_STOPBITS_1; sParam.u8cParity = DRVUART_PARITY_NONE; sParam.u8cRxTriggerLevel= DRVUART_FIFO_1BYTES; DrvUART_Open(UART_PORT0, &sParam); // set UART0 configuration VCOM_MainProcess(); } USB Virtual Com port (USB-to-UART0)

22 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw VCOM_MainProcess() void VCOM_MainProcess(void) { E_DRVUSB_STATE eUsbState; int32_t i, i32Len /* Enable Interrupt and install the call back function */ DrvUART_EnableInt(UART_PORT0, (DRVUART_RLSINT | DRVUART_RDAINT | DRVUART_TOUTINT),UartIntHandler); DrvUSB_Open((void*)DrvUSB_DispatchEvent); VCOM_Open(); /* Enable Interrupt and install the call back function */ DrvUART_EnableInt(UART_PORT0, DRVUART_RDAINT, UartIntHandler); eUsbState = DrvUSB_GetUsbState(); if (eUsbState >= eDRVUSB_ATTACHED) { /* Force Bus Reset for 150 ms*/ _DRVUSB_ENABLE_SE0(); RoughDelay(150000); _DRVUSB_DISABLE_SE0(); }

23 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_VCOM _DRVUSB_ENABLE_MISC_INT(0); /* Disable USB-related interrupts. */ _DRVUSB_ENABLE_FLDET_INT(); /* Enable float-detection interrupt. */ _DRVUSB_ENABLE_MISC_INT(INTEN_WAKEUP | INTEN_WAKEUPEN | INTEN_FLDET | INTEN_USB | INTEN_BUS); while(1) { eUsbState = DrvUSB_GetUsbState(); if (eUsbState == eDRVUSB_DETACHED) { //printf("USB Detached.\n"); /* Just waiting for USB attach */ while(eUsbState == eDRVUSB_DETACHED) { eUsbState = DrvUSB_GetUsbState(); } //printf("USB Attached.\n"); }

24 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_VCOM /* Check if any data to send to USB & USB is ready to send them out */ if(comRbytes && (gu32TxSize == 0)) { i32Len = comRbytes; if(i32Len > BULK_EP_MXPLD) i32Len = BULK_EP_MXPLD; for(i=0;i<i32Len;i++) { gRxBuf[i] = comRbuf[comRhead++]; if(comRhead >= RXBUFSIZE) comRhead = 0; } NVIC_DisableIRQ(CAN1_IRQn); comRbytes -= i32Len; NVIC_EnableIRQ(CAN1_IRQn); gu32TxSize = i32Len; DrvUSB_DataIn(BULK_IN_EP_NUM, gRxBuf, i32Len); } From Com port to USB, comRbuf is Com Rx Buffer & its data are sent to USB comRbytes is byte count of Com Rx buffer

25 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_VCOM /* Process the Bulk out data */ if(gu32RxSize && (gu32RxSize <= TXBUFSIZE - comTbytes)) { for(i=0;i<gu32RxSize;i++) { comTbuf[comTtail++] = gpu8RxBuf[i]; if(comTtail >= TXBUFSIZE) comTtail = 0; } NVIC_DisableIRQ(CAN1_IRQn); comTbytes += gu32RxSize; NVIC_EnableIRQ(CAN1_IRQn); gu32RxSize = 0; /* Ready to get next BULK out */ DrvUSB_DataOutTrigger(BULK_OUT_EP_NUM, BULK_EP_MXPLD); }

26 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_VCOM /* Process the software Tx FIFO */ if(comTbytes) { /* Check if Tx is working */ if(UART0->IER.THRE_IEN == 0) { /* Send one bytes out */ UART0->DATA = comTbuf[comThead++]; if(comThead >= TXBUFSIZE) comThead = 0; NVIC_DisableIRQ(UART0_IRQn); comTbytes--; NVIC_EnableIRQ(UART0_IRQn); /* Enable Tx Empty Interrupt. (Trigger first one) */ UART0->IER.THRE_IEN = 1; } From USB to Com port, comTbuf is Com Tx Buffer & its data are from USB comTbytes is byte count of Com Tx buffer

27 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_HID (1/3) /********************************** Main.c *****************************************/ //UNLOCKREG DrvSYS_UnlockKeyAddr(); SYSCLK->PWRCON.XTL12M_EN = 1; /* Enable PLL */ DrvSYS_SetPLLPowerDown(0); Delay(1000); /* Switch to PLL clock */ DrvSYS_SetHCLKSource(2); Delay(100); /* The PLL must be 48MHz when using USB */ SystemFrequency = 48000000; //LOCKREG DrvSYS_LockKeyAddr(); /* Execute HID process */ HID_MainProcess(); Chip System Setting Execute HID Process

28 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_HID (2/3) /******************************* HID_MainProcess *********************************/ int32_t i32Ret = E_SUCCESS; E_DRVUSB_STATE eUsbState; i32Ret = DrvUSB_Open(0); if(i32Ret != E_SUCCESS) return i32Ret; while(1) { /* Disable USB-related interrupts. */ DRVUSB_ENABLE_MISC_INT(0); /* Enable float-detection interrupt. */ _DRVUSB_ENABLE_FLD_INT(); // Wait for USB connected. while (TRUE) { // Order here is significant. // Give a chance to handle remaining events before exiting this loop. eUsbState = DrvUSB_GetUsbState(); DrvUSB_DispatchEvent(); if (eUsbState >= eDRVUSB_ATTACHED &&eUsbState != eDRVUSB_SUSPENDED) break; } 1. Open USB Driver 2. USB Plug-in Detect

29 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_HID (3/3) /* Start HID and install the callback functions to handle in/out report */ HID_Init((void *)HID_GetInReport, (void *)HID_SetOutReport); // Enable USB-related interrupts. _DRVUSB_ENABLE_MISC_INT(IEF_WAKEUP | IEF_WAKEUPEN | IEF_FLD | IEF_USB | IEF_BUS); // Poll and handle USB events. while(1) { eUsbState = DrvUSB_GetUsbState(); DrvUSB_DispatchEvent(); HID_GetInReport(0); if (eUsbState == eDRVUSB_DETACHED) break; } // Disable USB-related interrupts. _DRVUSB_ENABLE_MISC_INT(0); } 3. Open USB Class Driver 4. Dispatch USB events 5. Call USB Function Handler

30 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_SDcard_ADPCM Audio playback of a.wav file on SD card

31 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_SDcard_ADPCM ► Store a.wav file into SD card –fix filename : adpcm.wav ► Use FAT FS to read SD card ► Use libImaAdpcm4bit.lib for audio playback ► Use onboard WAU8822 audio codec to drive a 8 ohm speaker (CON3)

32 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_SDcard_ADC7 ► Capture 10K samples from ADC7 pin ► Store 12-bit data (2 bytes) into SDcard using FAT FS (Linux)

33 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw General Disclaimer The Lecture is strictly used for educational purpose. MAKES NO GUARANTEE OF VALIDITY ► The lecture cannot guarantee the validity of the information found here. The lecture may recently have been changed, vandalized or altered by someone whose opinion does not correspond with the state of knowledge in the relevant fields. Note that most other encyclopedias and reference works also have similar disclaimers.similar disclaimers No formal peer review ► The lecture is not uniformly peer reviewed; while readers may correct errors or engage in casual peer review, they have no legal duty to do so and thus all information read here is without any implied warranty of fitness for any purpose or use whatsoever. Even articles that have been vetted by informal peer review or featured article processes may later have been edited inappropriately, just before you view them.peer reviewfeatured article No contract; limited license ► Please make sure that you understand that the information provided here is being provided freely, and that no kind of agreement or contract is created between you and the owners or users of this site, the owners of the servers upon which it is housed, the individual Wikipedia contributors, any project administrators, sysops or anyone else who is in any way connected with this project or sister projects subject to your claims against them directly. You are being granted a limited license to copy anything from this site; it does not create or imply any contractual or extracontractual liability on the part of Wikipedia or any of its agents, members, organizers or other users. ► There is no agreement or understanding between you and the content provider regarding your use or modification of this information beyond the Creative Commons Attribution-Sharealike 3.0 Unported License (CC-BY-SA) and the GNU Free Documentation License (GFDL);Creative Commons Attribution-Sharealike 3.0 Unported License GNU Free Documentation License

34 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw General Disclaimer Trademarks ► Any of the trademarks, service marks, collective marks, design rights or similar rights that are mentioned, used or cited in the lectures are the property of their respective owners. Their use here does not imply that you may use them for any purpose other than for the same or a similar informational use as contemplated by the original authors under the CC-BY- SA and GFDL licensing schemes. Unless otherwise stated, we are neither endorsed by nor affiliated with any of the holders of any such rights and as such we cannot grant any rights to use any otherwise protected materials. Your use of any such or similar incorporeal property is at your own risk. Personality rights ► The lecture may portray an identifiable person who is alive or deceased recently. The use of images of living or recently deceased individuals is, in some jurisdictions, restricted by laws pertaining to personality rights, independent from their copyright status. Before using these types of content, please ensure that you have the right to use it under the laws which apply in the circumstances of your intended use. You are solely responsible for ensuring that you do not infringe someone else's personality rights.personality rights


Download ppt "Www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University Universal Serial Bus & SD card 5/30/2013 Richard Kuo Assistant."

Similar presentations


Ads by Google