USB Peripheral Module Introduction This module covers the USB peripheral, library and examples provided with the LM3S3748 evaluation kit. Learning Objectives • USB Basics • Stellaris Implementation • USB Library • USB Examples • Two labs Stellaris One-Day Workshop - USB Peripheral 14 - 1 Module Topics Module Topics USB Peripheral Module................................................................................................................................1 Module Topics.............................................................................................................................................2 USB Basics..................................................................................................................................................3 Stellaris USB ..........................................................................................................................................4 USB Stacks.............................................................................................................................................5 Stellaris MCU’s with USB .....................................................................................................................6 LM3S3748 USB Schematic....................................................................................................................7 USB Library ...........................................................................................................................................7 USB Code Examples ..............................................................................................................................8 Labs ........................................................................................................................................................9 USB Mass Storage Host Lab.....................................................................................................................11 Description: ..........................................................................................................................................11 Hardware list: .......................................................................................................................................11 Software list:.........................................................................................................................................11 Procedure..............................................................................................................................................12 USB HID Mouse Device Lab ....................................................................................................................16 Description: ..........................................................................................................................................16 Hardware list: .......................................................................................................................................16 Software list:.........................................................................................................................................16 Procedure..............................................................................................................................................17 14 - 2 Stellaris One-Day Workshop - USB Peripheral USB Basics USB Basics USB Basics Multiple connector sizes 4 thpins – power, ground and 2 data lines Configuration connects power 1st, then data Standards: (5 pin ID for USB 2.0 connectors) USB 1.1 • Defines Host (master) and Device (slave) • • USB 2.0 • Speeds to 480Mbits/sec • Speeds to 12Mbits/sec Devices can consume 500mA (100mA for startup) OTG addendum USB 3.0 • Speeds to 4.8Gbits/sec • • New connector(s) Separate transmit/receive data lines USB Tutorial: http://www.computer-solutions.co.uk/info/Embedded_tutorials/usb_tutorial.htm USB Basics USB Device … most USB products are slaves USB Host … usually a PC, but can be embedded USB OTG … On-The-Go Allows a single port to be either a Device (a printer connected to a PC) or a Host (a printer connected to a camera) Two connected OTG ports undergo host negotiation Host polls each Device at power up. Info from Device includes: Device Descriptor (Manufacturer & Product ID so Host can find driver) Configuration Descriptor (Power consumption and Interface descriptors) Endpoint Descriptors (Transfer type, speed, etc) Process is called Enumeration … allows Plug-and-Play Srellaris USB … Stellaris One-Day Workshop - USB Peripheral 14 - 3 USB Basics Stellaris USB Stellaris USB USB 2.0 Full Speed (12 Mbps) operation Transfer types: Control, Interrupt, Bulk and Isochronous Stellaris collaterals Luminary Micro is a member of the USB Implementers Forum. Stellaris is approved to use the USB logo Vendor/Product ID sharing FREE Vendor ID/ Product ID sharing program Block Diagram … USB Peripheral Block Diagram Integrated USB Controller and PHY Up to 32 Endpoints 1 dedicated control IN endpoint and 1 dedicated control OUT endpoint 14 - 4 Up to 15 configurable IN endpoints and 15 configurable OUT endpoints 4 KB Dedicated Endpoint Memory DMA capability (up to three IN Endpoints and three OUT Endpoints) 1 endpoint may be defined for double-buffered 1023-bytes isochronous packet size Stacks … Stellaris One-Day Workshop - USB Peripheral USB Basics USB Stacks USB Stacks USBX (H/D) uC/USB (D) smxUSB (O/H/D) USB (O/H/D) emUSB (H/D) CMX-USB (O/H/D) MCUs … Stellaris One-Day Workshop - USB Peripheral 14 - 5 USB Basics Stellaris MCU’s with USB USB Connected MCUs C or e Ser ia l I nt er f ac es Dig it al C AN M AC USB F u ll Spe ed UART I2 C SSI/SPI I 2S AD C Ch an ne ls AD C Spe ed (Ks ps) LD O Vo ltag e R eg ulat or Ana log C om pa ra to rs Dig ital Com pa ra to rs 64 ? 50 - 4 8 1 8 ? 8 4 1 - - - O/H /D 3 2 2 - 8 1 00 0 ? ? 3 - 61 ? 6 4- LQ F P 1 00 -L QF P L M 3S5 000 s 12 25 6 96 ? 80 ? 4 8 2 8 ? 8 4 2 - - 2 O/H /D 3 2 2 ? 16 1 00 0 ? ? 3 7 71 ? 6 4- LQ F P 1 00 -L QF P L M 3S9 000 s 6 25 6 96 ? 10 0 ? 4 8 2 8 ? 8 4 2 ? ? 2 O/H /D 3 2 2 ? 16 1 00 0 ? ? 3 7 65 ? 1 00 -L QF P Hib e rn a te IEEE 1 58 8 12 8 GPIO s ( 5- V) Q EI 4 ADC (1 0- bit) In ter na l T em p Sens or Fa ult Inp ut s 10 /10 0 Ethe rn et M AC+ PHY L M 3S3 000 s SRAM ( KB) R OM SW Lib ra ry Ma x Sp ee d ( MH z) I nte rn al Pre cisio n Osc illato r 3 2- bit T ime r RT C An al og O utp ut s C on t ro l PWM 16 -b it T im er Wat chd og T im er s CC P M o tio n T i m er M o du le s Fla sh (KB) G en e ra l Pu r p os e MCU s i n Ser ie s Sp ee d Pac ka ge O pt io n s M e m or y an d USB 2.0 full speed (12 Mbps) support (MAC+PHY) : Host/Device/OTG – with DMA Compact 64-pin LQFP or feature-rich 100-pin LQFP options – CSTN graphics display (128x128 resolution,16-bit color) Stellaris LM3S3748 Evaluation Kit – User LED, navigation switch and pushbutton – Magnetic speaker – LM3S3748 I/O available on labeled break-out pads $109 – Standard ARM® 20-pin J TAG debug connector with input and output modes – MicroSD card slot USB USB and JTAG cables, Jumper wires Flash Drive (128MB) Gen er al Pu r po se Tim e r M od u les Co re An alo g CCP Ou tput s F ault I npu ts QEI 10 /10 0 Ethe rne t M AC+PHY IEEE 15 88 CAN M AC USB F ull Spee d UART I2 C SSI/SPI 2 ADC Chan nels ADC Spe ed ( Ks ps) Inte rn al T emp Sen sor LDO Vo ltage Reg ulato r Ana lo g Co mp ara tor s 64 ? - 50 - 4 8 1 8 ? 8 4 1 - - - O/ H/D 3 2 2 - 8 100 0 ? ? 3 - 61 ? 64 -LQ FP 100 -L QFP LM 3S50 00s 12 256 96 ? ? 80 ? 4 8 2 8 ? 8 4 2 - - 2 O/ H/D 3 2 2 ? 16 100 0 ? ? 3 7 71 ? 64 -LQ FP 100 -L QFP LM 3S90 00s 6 256 96 ? ? 100 ? 4 8 2 8 ? 8 4 2 ? ? 2 O/ H/D 3 2 2 ? 16 100 0 ? ? 3 7 65 ? 100 -L QFP G PIO s (5 -V) Dig ital Com par ator s I S Oscillato r ADC ( 10 -bit) Hib er na te 16 -bit Tim er Watch dog T m i er s 128 RT C 32 -bit Tim er 4 I nter na l Pr ecisio n Ext . Perip her al Inte rfa ce LM 3S30 00s ROM SW Libr ary M ax Sp eed (M Hz) Dig it al SRAM ( KB) Ser ial Int er fac es F lash (KB) Mo tio n Co nt ro l PWM M CUs in Ser ies M em or y a nd Spe ed Pack ag e Op tio n s Ethernet+USB OTG Connected MCUs First MCUs featuring fully integrated 10/100 Ethernet MAC+PHY, USB OTG MAC+PHY, and up to 2 Bosch CAN 2.0 A/B MACs LM3S9B96 features SAFERTOS in ROM Evaluation board with LM3S9B90 (hibernate) or LM3S9B92 (max GPIOs) In-Circuit Debug Interface (BD-ICDI) board EK-LM3S9B90 $99 Connects to USB port on PC and to 10-pin ARM JTAG connector on the evaluation board 8-pin Power/UART connector provides power and virtual comm-port to the evaluation board Cables CD with evaluation software tools, documentation, source code, schematics EK-L M3S9B92 14 - 6 Schematic … Stellaris One-Day Workshop - USB Peripheral USB Basics LM3S3748 USB Schematic LM3S3748 Eval Board USB Schematic Fairchild USB Switch TI ESD protection diode array USBLib … USB Library Stellaris USB Library Supports Host, Device and On-The-Go ports Written entirely in C User Guide: SW-UG-4781.pdf Supports the following tool chains: Keil CodeSourcery IAR Embedded Workbench Code Red Technologies Code Composer Studio Code Examples … Stellaris One-Day Workshop - USB Peripheral 14 - 7 USB Basics USB Code Examples USB Code Examples USB Boot Loader Demo 1 USB Boot Loader Demo 2 USB Generic Bulk Device USB HID Keyboard Device USB HID Mouse Device USB MSC Device USB Serial Device USB HID Keyboard Host USB HID Mouse Host USB Mass Storage Class Host Location: C:\StellarisWare\boards\ek-lm3s3748 USB Code Examples Stellaris examples implement a custom USB “micro-stack” Stack footprint is about 10K in most configurations Application code sizes – usb_dev_mouse (HID) – 17kB – usb_dev_msc (MSC) – 16kB – usb_dev_serial (CDC) – 18kB – usb_host_mouse – 14kB – usb_host_msc – 21kB USB Bootloader (DFU) - <6k USB Flash stick updater – 10kB Lab … 14 - 8 Stellaris One-Day Workshop - USB Peripheral USB Basics Labs USB Mass Storage Class Host Lab LM3S3748 EVM USB Flash Drive USB cable LM3S3748 accesses Flash Drive through USB port as a Host Uses FatFs: An open-source file system for embedded designs Supports FAT12, FAT16 (with 8.3) and FAT32 Multiple volumes FAT32 & Long File Name (LFN) support requires license from Microsoft http://elm-chan.org/fsw/ff/00index_e.html Lab … USB HID Mouse Device Lab USB LM3S3748 EVM USB Flash Drive USB cable Code sends Descriptor structures and enumerates as a mouse Code sends Mouse movements and button press to the PC Host TI … Stellaris One-Day Workshop - USB Peripheral 14 - 9 USB Basics *** Left on its own, this page would have been blank *** 14 - 10 Stellaris One-Day Workshop - USB Peripheral USB Mass Storage Host Lab USB Mass Storage Host Lab Description: We’ll use the LM3S3748 MCU to access a Flash drive connected to the USB Host port of the evaluation board. Hardware list: 9 LM3S3748 Evaluation Board 9 USB Flash Drive with some files onboard (anything will do) 9 3 meter, 4 pin, USB Type A-M to mini-USB Type B-M Software list: 9 Code Composer Studio 9 LM3S3748 software examples Stellaris One-Day Workshop - USB Peripheral 14 - 11 USB Mass Storage Host Lab Procedure 1. LM3S3748 EVM Check the USB power mode switch on the board. We want to power the board from the DEBUG USB port. The switch should be in the “SELF” position as shown below. Don’t reposition the switch while power is applied as you may damage the switch contacts. 2. Connect the ICDI USB Cable Connect the small end of the USB cable to the connector on the LM3S3748 board marked “DEBUG USB”. You’ll find it on the corner of the board furthest away from the microprocessor. Make sure that you’re using the LM3S3748 board. Connect the other end of the USB cable to an open port on your PC. Since you already have the drivers loaded, your PC should recognize the board quickly. 3. Open the IDE Open Code Composer Studio and create a workspace_3748USB workspace (we’re using a new workspace for each lab to prevent issues and learn the steps by repetition). Close the Welcome screen if it appears. Click the C/C++ Perspective button to make sure that you’re in the editing perspective. Maximize Code Composer Studio. 4. Import driverlib Click on Project, and then select Import Existing CCS/CCE Eclipse Project. When the Import dialog appears, browse to the root directory of the driver library (C:\StellarisWare\driverlib) and click OK. Be sure that the checkbox next to driverlib in the Project pane is checked and that Copy projects into workspace is unchecked. Click Finish. 14 - 12 Stellaris One-Day Workshop - USB Peripheral USB Mass Storage Host Lab 5. Import grlib Click on Project, and then select Import Existing CCS/CCE Eclipse Project. When the Import dialog appears, browse to the root directory of the graphics library (C:\StellarisWare\grlib) and click OK. Be sure that the checkbox next to grlib in the Project pane is checked and that Copy projects into workspace is unchecked. Click Finish. 6. Import usblib Click on Project, and then select Import Existing CCS/CCE Eclipse Project. When the Import dialog appears, browse to the root directory of the USB library (C:\StellarisWare\usblib) and click OK. Be sure that the checkbox next to usblib in the Project pane is checked and that Copy projects into workspace is unchecked. Click Finish. 7. Import the usb_host_msc project Click on Project, and then select Import Existing CCS/CCE Eclipse Project. When the Import dialog appears, browse to the root directory of the project library (C:\StellarisWare\boards\ek-lm3s3748\usb_host_msc) and click OK. Be sure that the checkbox next to usb_host_msc in the Project pane is checked and that Copy projects into workspace is unchecked. Click Finish. 8. Project Contents Click on the “+” next to usb_host_msc and your Project Explorer should look like this: Stellaris One-Day Workshop - USB Peripheral 14 - 13 USB Mass Storage Host Lab The included files provide the following functionality: startup_ccs.c – Startup code to set Heap, Stack and basic interrupts Expand the usb_host_msc.c folder: usb_host_msc.c – Main example code Expand the drivers folder: buttons.c – Functions for handling the on-board push buttons formike128x128x16.c – LCD display driver code Expand the third_party, then fatfs , then the port and src folders: fat_usbmsc.c – Functions to access FAT file system via Luminary USB stack ff.c – Underlying FAT (File Allocation Table) file system code from FatFs 9. Open usb_host_msc.c for editing Double-click on usb_host.msc.c in the Project Explorer pane to open it for editing. Maximize the editor window if it hasn’t opened that way already. Click anywhere inside the code. On the menu bar, click Edit, Find/Replace and enter main( in the Find: box. Click Find, then Close. You should see where the main()_code begins. The next 150 lines or so performs the overall initialization. In that code, the initial state of g_eState ( a global variable for the enumeration state) is set to STATE_NO_DEVICE . Later in the initialization code, GrStringDraw() (a graphics library call) is used to write “No Device” to the LCD display. Also note that a GPIOPinWrite() call sets the USB switch shown earlier in the schematic to the Host port. Use the steps above to find while(1). A switch() statement switches between three states depending on g_eState (defined to have three states; STATE_NO_DEVICE, STATE_DEVICE_ENUM and STATE_DEVICE_READY). The earlier initialization code set g_eState = STATE_NO_DEVICE. The MSCCallback() code above main() (a callback from the lower level USB driver) sets the state of g_eState during runtime to either STATE_NO_DEVICE or STATE_DEVICE_ENUM. This callback function was registered with the driver in the line containing USBHMSCDriveOpen(). The first case in the while() loop, run during USB enumeration, detects whether enumeration has been successful and sets g_eState to STATE_DEVICE_READY (or it errors out). It also reads the directory (DirUpdate()) and writes the result to the LCD display (UpdateWindow()). The code in the STATE_DEVICE_READY case occurs after enumeration is complete and the USB device is ready. Note how the buttons move through the directory. 14 - 14 Stellaris One-Day Workshop - USB Peripheral USB Mass Storage Host Lab 10. Build and Load If the Console pane is not visible at the bottom of your display, click View Æ Console from the menu bar. Make sure that your USB Flash drive is NOT plugged into the EVM at this time. Click the Debug button on the menu bar to build/load the project to the LM3S3748 board. Monitor the progress of the build in the Console pane. The program will load to the 3748 Flash memory and the perspective will switch to Debug. (if it doesn’t, click the Debug perspective button in the upper right of your display) Once the load process completes, click the Run button to start program execution. 11. Test the Code The LCD display should have the top banner with the example code name on it, along with “No Device”. Plug in your USB Flash drive to the Host connector on the board and the directory should be displayed. (if you don’t see any files, maybe there are no files on the flash drive … copy some onto it from your laptop and try again). Due to licensing issues, FatFS only displays 8.3 filenames. Use the Navigation button below the LCD display to move up and down through the directory. If you have any sub-directories on the drive, you can highlight them and press the navigation button straight down to move into them. 12. Close the Project Stop debugging by pressing the Terminate All button on the menu bar. It it hasn’t already done so, switch the IDE back to the editing perspective by clicking the C/C++ on the far right of the menu bar. Collapse the view of the button usb_host_msc project in the Project Explorer , close any editor windows and disconnect your USB Flash drive from the evaluation board. Stellaris One-Day Workshop - USB Peripheral 14 - 15 USB HID Mouse Device Lab USB HID Mouse Device Lab Description: We’ll emulate a USB mouse device with the LM3S3748EVM through the boards USB Device port to our PC host. Hardware list: 9 LM3S3748 evaluation board 9 3 meter, 4 pin, USB Type A-M to mini-USB Type B-M Software list: 9 Code Composer Studio 9 LM3S3748 software examples 14 - 16 Stellaris One-Day Workshop - USB Peripheral USB HID Mouse Device Lab Procedure 1. Delete the usb_host_msc project Since we already have all the supporting files imported, we can just delete the previous code example and import the next, rather than reload everything from zero. Right-click on usb_host_msc in the Projects pane and select Delete. Make sure that Do not delete contents is checked (if not, the original example files would be deleted) and click Yes. 2. Import the usb_dev_mouse project Click on Project, and then select Import Existing CCS/CCE Eclipse Project. When the Import dialog appears, browse to the root directory of the project library (C:\StellarisWare\boards\ek-lm3s3748\usb_dev_mouse) and click OK. Be sure that the checkbox next to usb_dev_mouse in the Project pane is checked and that Copy projects into workspace is unchecked. Click Finish. 3. Open usb_mouse_structs.c for editing Expand the usb_dev_mouse project and the usb_mouse_structs.c folder. Remember those descriptors that a USB Device provides to the host during enumeration? Open the usb_mouse_structs.c file for editing and check out the manufacturer, product, serial number, interface and configuration structures. 4. Open usb_dev_mouse.c for editing Expand the usb_dev_mouse.c folder and open the usb_dev_mouse.c file for editing. Look around in the code at the initialization, graphics, etc, but most of the action occurs in the ButtonHandler() function about halfway through the code. This code is run from the main() loop each time the BUTTON_TICK_EVENT occurs. Look a little further down to see where the code formats the mouse movements and button presses for transfer to the host. 5. Build/Download the code Similar to the previous procedure, build and download the project to the evaluation board. Don’t run the code yet. 6. Connect the Board Disconnect the ICDI USB cable from the DEBUG port of the evaluation board. Check the USB power mode switch on the board (refer to the earlier illustration). Switch it to the “BUS” position so that power for the evaluation board will be provided through the USB Device port. Connect the USB cable to the USB DEVICE port on the EVM (just left of the LCD display). Stellaris One-Day Workshop - USB Peripheral 14 - 17 USB HID Mouse Device Lab 7. Test the Code The board should power up, and you should see “Waiting for Host …” on the LCD display. As soon as your PC recognizes the boards as a mouse, “Host connected …” should appear on the display. Use your PC’s mouse to position the cursor in the middle of your computer screen, and then use the Navigation button on the LM3S3748 board to move it around the screen. You can press the button down to select. Note that the LCD display shows the status of the Navigation button. 8. Turn it Off Disconnect the LM3S3748 evaluation board from your PC and switch the USB power mode switch back to “SELF”. Switch Code Composer Studio back to C/C++ perspective and exit the IDE. Remember that the QuickStart application has been replaced by this application. You can reprogram it at any time with either Code Composer Studio or the Flash Programmer. You’re done 14 - 18 Stellaris One-Day Workshop - USB Peripheral