USB Communication using PIC microcontrollers

From Mech
Revision as of 16:20, 21 June 2012 by DavidMeyer (talk | contribs)
Jump to navigationJump to search

***Under Construction***

USB Overview

Universal Serial Bus, or USB, is a very common method of communication between electronic devices. USB is universal, meaning that it's standard across all electronic devices, serial, meaning that data is transmitted on only one data line one bit at a time, and is a bus, meaning that all devices are connected through the same parallel electronic wires and therefore require addresses. A simple USB connection requires a host and a device. The host initiates all communication, assigns device addresses, and controls the flow of the data line in the connection. For our purposes, the USB host will be the PC and the USB device will be a PIC chip.

There are many different speeds associated with USB. USB 1.0 introduced data rates of 1.5 Mbits/s (Low-Speed) and 12 Mbits/s (Full-Speed). With USB 2.0 and 3.0, data rates of 480 Mbits/s (Hi-Speed) and 5 Gbits/s (Super-Speed), were added respectively. PIC chips have hardware capable of full speed USB.

Hardware

A USB cable has 4 wires: power (red), ground (black), D+ (green), and D- (white). You may notice that many USB plugs and receptacles have 5 pins. This fifth pin is called ID, and is often not connected, but has been added to the specifications for use with USB-OTG, or On-The-Go. OTG allows devices to also act as a host. Many PIC chips are compatible with USB-OTG communication, but we will not discuss this topic any further at this point.

The two data lines, D+ and D-, are used in a way to maximize the number of conditions. The electrical state of these lines gives signals about the conditions of the connection.

  • Detached State, or SE0 - device is unplugged, both data lines are low, pulled down by 15 kOhm resistors within the host hardware.
  • Attached State, Idle State, or J State - the device pulls up the D+ line (for a high-speed device) when it is attached.
  • K State - opposite polarity from J State. To communicate, the host or device alternates the device between the J and K states in specific patterns.
  • Illegal State, or SE1 - both data lines are high. If this happens, there is a hardware problem with the USB link.

All of these details are handled by the USB modules themselves, and the developer seldom needs to know these things. However, they are useful to those who are debugging hardware issues. More information about USB can be found at the USB Made Simple site.

Microchip USB Stack

Fortunately, Microchip has provided almost all of the code and examples to run USB code on PIC chips. This code is called the USB stack, and we will use this as the main library for communicating to PCs. Unfortunately, there are a few bugs for certain PIC chips and some quirks to getting the libraries setup in MPLAB. This section will document the steps necessary to install the USB stack and setup a proper programming environment in MPLAB.

Download

We'll begin with the assumption that the reader knows how to setup a project for a PIC chip in MPLAB and program their chip with some basic C code. The Microchip USB stack is a part of the Microchip Application Libraries, which are available at the Microchip website here You must download and install the entire Application Libraries package, which will place all the libraries in your C drive on a Windows machine and some other place (**FIND OUT WHERE**) on a Mac. The name of the root directory should be something like "Microchip Solutions v2012...", but from here on out, we'll refer to the root directory of that installation as <MAL>.

Include in Project

Once the installer has finished running, the actual USB stack is located at <MAL>/Microchip/USB. <MAL>/USB is not the stack, and only contains demos of the firmware for learning purposes. We will not be using any of those files for our purposes. After starting a project in MPLAB for your PIC chip, you must complete the following steps:

  • Include the USB libraries in your project
    • In MPLAB X, right-click on your project and select properties. In the left-sidebar, click on your compiler (pic32-gcc for PIC32 chips). In the main window, find the field called "Include Directories" and click the button with "..." Add the directories <MAL>/Microchip/USB, <MAL>/Microchip/Include, and importantly your current project directory. This last one is important because the stack references a Hardware Profile which we will include in our own project directory.
  • Add the HID device drivers to your project
    • In MPLAB X, right-click on your project source files and select "add existing..." Browse to <MAL>/Microchip/USB and add usb_device.c and ./HID Device Driver/usb_function_hid.c. These files do not need to be moved to your project directory, they just need to be added into your project via MPLAB.

USB Enumeration

HID Descriptors