d tarek

download d tarek

of 36

Transcript of d tarek

  • 8/6/2019 d tarek

    1/36

    Pic USB interface with pcPic USB interface with pc

  • 8/6/2019 d tarek

    2/36

    Table of contentsTable of contents

    Why we choose this projectWhy we choose this project

    basic principles of the USB busbasic principles of the USB bus

    Microcontroller SoftwareMicrocontroller SoftwarePc host computer softwarePc host computer software

    Testing the projectTesting the project

  • 8/6/2019 d tarek

    3/36

    Why we choose this projectWhy we choose this project

    The first IBM pc was announced inThe first IBM pc was announced in 19811981 andandsince then wesince then weve all wanted to add hardve all wanted to add hardware to our PCs So that a lot of ports andware to our PCs So that a lot of ports andneeded protocols have been added to theneeded protocols have been added to thepc such as (serial,parallel,sp/pc such as (serial,parallel,sp/22, ), )--The power of the PC has grownThe power of the PC has grown--The number of tasks we want the PC to doThe number of tasks we want the PC to dohas grownhas grown

    --The number of devices we want to connectThe number of devices we want to connectto the PC has grownto the PC has grown--So USB is the best choice for our needsSo USB is the best choice for our needsbut why??but why??

  • 8/6/2019 d tarek

    4/36

    We can answer the previous questionWe can answer the previous questionby listing the features of the USBby listing the features of the USB

    USB has the following features: Easy to use, so theres no need to fiddle withconfiguration and setup details.

    Fast, so the interface doesnt become acommunications bottleneck.

    Reliable, so that errors are rare, with automaticretries when errors occur.

    Versatile, so many kinds of peripherals can usethe interface.

    -Hot-pluggable : I/O devices can be added whilethe pc is running

  • 8/6/2019 d tarek

    5/36

    Inexpensive, so manufacturers and users dont balk atthe price.

    Power-conserving, to save energy and extend batterylife in portable computers and devices.

    Supported by the Windows and other operatingsystems, so developers dont have to write low-level drivers

    to communicate with the peripherals.External to pc :there is no need to open the pc or designcards that must be installed in the pc

    Wireless Communications: USB originated as a

    wired interface, but options now exist for wireless devicesthat use USB to communicate with PCs.

  • 8/6/2019 d tarek

    6/36

  • 8/6/2019 d tarek

    7/36

    basic principles of the USB busbasic principles of the USB bus

    The Universal Serial Bus (USB) is a highThe Universal Serial Bus (USB) is a high--speedspeedserial interface that can also provide power toserial interface that can also provide power todevices connected to it.devices connected to it.

    Some hard ware specifications:-A USB bus supports up to 127 devices-

    -It connected through a four-wire serial cable ,of up to three or

    even five meters in length.

    -Many USB devices can be connected to the same bus with

    hubs, which can have 4, 8, or even 16 ports.-A device can be plugged into a hub which is plugged into

    another hub, and so on.

    -The maximum number of tiers permitted is six. According to the

    specification,

    -The maximum distance of a device from its host is about thirty

    meters, accomplished by using five hubs

  • 8/6/2019 d tarek

    8/36

    The USB bus specification comes in two versions:

    the earlier version, USB1.1, supports 11 Mbps,

    the new version, USB 2.0, supports up to 480 Mbps

    specification defines three data speeds:

    Low speed1.5Mb/sec

    Full speed12Mb/sec

    High speed480Mb/sec

    The maximum power available to an external device is limited toabout 100mA at 5.0V.

    USB is using a four-core shielded cable.

    Two types of connectors are specified and used:

    Type A and Type B.

  • 8/6/2019 d tarek

    9/36

    USB connector pin assignments

    Two of the pins Data (+) and Data( -) form

    a twisted pair and carry differential data

    signals

  • 8/6/2019 d tarek

    10/36

    USB Bus CommunicationUSB Bus Communication

    USB StatesUSB States:: Some of the USB bus states are:Some of the USB bus states are:IdleIdle: The bus is in idle state when the pulled: The bus is in idle state when the pulled--up line is high andup line is high andthe other line is low.the other line is low.

    This is the state of the lines before and after a packet transmission.This is the state of the lines before and after a packet transmission.

    J stateJ state: The same as idle state.: The same as idle state.Detached:Detached: When no device is connected to the busWhen no device is connected to the bus

    AttachedAttached: When a device is connected to the bus: When a device is connected to the bus

    SESE00:: The single ended zero state, where both lines on theThe single ended zero state, where both lines on thebus are pulled low.bus are pulled low.

    ResetReset: When the host wants to communicate with a device: When the host wants to communicate with a deviceon the bus, it first sends aon the bus, it first sends a resetreset condition by pulling lowcondition by pulling lowboth data lines (SEboth data lines (SE00 state) for at leaststate) for at least 1010msms

  • 8/6/2019 d tarek

    11/36

    EOP: The end of packet state, which is basically an SE0state for 2 bit times,

    followed by a J state for 1 bit time.

    Keep alive: The state achieved by EOP. Keep alive issent at least once every millisecond to keep the device

    from suspending.

    Suspend: Used to save power, suspend is implementedby not sending anything to a device for 3ms.

    A suspended device draws less than 0.5mA from the bus

    and must recognize reset and resume signals.

    Resume: A suspended device is woken up by reversingthe polarity of the signal on the data lines for at least 20ms,followed by a low-speed EOP signal.

  • 8/6/2019 d tarek

    12/36

    USB Bus CommunicationUSB Bus Communication

    USB is a hostUSB is a host--centric connectivity systemcentric connectivity systemwhere the host dictates the use of the USBwhere the host dictates the use of the USB

    ))moremorebus (bus (--Data is transmitted on a USB bus inData is transmitted on a USB bus in

    moremore..packetspackets--A packet starts with a sync pattern thenA packet starts with a sync pattern thenA packet identifier (PID) byte. ThenA packet identifier (PID) byte. Then

    The data bytes of the packet follow,The data bytes of the packet follow,ending with an end of packet signal.ending with an end of packet signal.--There are four packet formats, based onThere are four packet formats, based onwhich PID is at the start of the packet:which PID is at the start of the packet:

  • 8/6/2019 d tarek

    13/36

  • 8/6/2019 d tarek

    14/36

    Token packet

    Data packet

    Handshake packet

  • 8/6/2019 d tarek

    15/36

    Data Flow TypesData Flow Types

    Data can be transferred on a USB bus in fourData can be transferred on a USB bus in four))33:(:(waysways

  • 8/6/2019 d tarek

    16/36

    EnumerationEnumeration

    The steps of enumeration are:The steps of enumeration are:--When a device is plugged in, the host becomesWhen a device is plugged in, the host becomesaware of it because one of the data lines becomesaware of it because one of the data lines becomeslogic high.logic high.

    --The host sends a USB reset signal to the device toThe host sends a USB reset signal to the device toplace the device in a known state.place the device in a known state.

    --The host sends a request on addressThe host sends a request on address 00 to theto thedevice to find out its maximum packet size using adevice to find out its maximum packet size using a

    Get Descriptor command.Get Descriptor command.--The host sends a USB reset again.The host sends a USB reset again.

  • 8/6/2019 d tarek

    17/36

    internal Hard ware

  • 8/6/2019 d tarek

    18/36

    --The host assigns a unique address to the device and sendsThe host assigns a unique address to the device and sends

    a Set Address request to the device.a Set Address request to the device.

    --After the request is completed, the device assumes the newAfter the request is completed, the device assumes the newaddress.address.

    --At this point the host is free to reset any other newly pluggedAt this point the host is free to reset any other newly plugged--

    in devices on the bus.in devices on the bus.

    --The host sends a Get Device Descriptor request to retrieveThe host sends a Get Device Descriptor request to retrieve

    the complete device descriptor, gathering information such asthe complete device descriptor, gathering information such asmanufacturer, type of device, and maximum control packetmanufacturer, type of device, and maximum control packet

    sizesize

    --The host sends a Get Configuration Descriptors request toThe host sends a Get Configuration Descriptors request to

    receive the devices configuration data, such as powerreceive the devices configuration data, such as powerrequirements and the types and number of interfacesrequirements and the types and number of interfaces

    supported.supported.

    The host may request any additional descriptors from theThe host may request any additional descriptors from the

    device.device.

  • 8/6/2019 d tarek

    19/36

    The initial communication between the host andThe initial communication between the host andthe device is carried out usingthe device is carried out using

    the control transfer type of data flow.the control transfer type of data flow.

    Initially, the device is addressed, but it is in anInitially, the device is addressed, but it is in anunconfigured state.unconfigured state.

    After the host gathers enough information aboutAfter the host gathers enough information aboutthe device, it loads a suitable device driver whichthe device, it loads a suitable device driver whichconfiguresconfiguresthe device by sending it a Set Configurationthe device by sending it a Set Configurationrequest.request.

    At this point the device has been configured, andAt this point the device has been configured, andit is ready to respond to deviceit is ready to respond to device--specific requestsspecific requests(i.e., it can receive data from and send data to(i.e., it can receive data from and send data tothe host)the host)

  • 8/6/2019 d tarek

    20/36

    DescriptorsDescriptors

    All USB devices have a hierarchy of descriptors thatAll USB devices have a hierarchy of descriptors thatdescribe various features of the device:describe various features of the device:

    The most common USB descriptors are:The most common USB descriptors are:

    Device descriptors

    Configuration descriptors

    Interface descriptors

    HID descriptors

    Endpoint descriptors

  • 8/6/2019 d tarek

    21/36

    The Microcontroller SoftwareThe Microcontroller Software

  • 8/6/2019 d tarek

    22/36

    Generating the USB Descriptor FileGenerating the USB Descriptor File

    The USB descriptor is a file that mustThe USB descriptor is a file that mustbe included at the beginning of thebe included at the beginning of the

    microC program.microC program.

    This descriptor file is created using theThis descriptor file is created using the

    Tools menu option of the microCTools menu option of the microCcompiler.compiler.

  • 8/6/2019 d tarek

    23/36

  • 8/6/2019 d tarek

    24/36

    Microcontroller ClockMicrocontroller ClockThe USB module of the PICThe USB module of the PIC1818FF45504550microcontroller requires amicrocontroller requires a 4848MHz clock.MHz clock.

    AA 44MHz crystal is used to operate theMHz crystal is used to operate themicrocontroller. The actual CPU clock ismicrocontroller. The actual CPU clock israisedraised

    toto 4848MHz by setting configuration bits.MHz by setting configuration bits.

  • 8/6/2019 d tarek

    25/36

  • 8/6/2019 d tarek

    26/36

  • 8/6/2019 d tarek

    27/36

    microC library functionmicroC library functionHid_Enable:Hid_Enable: This function enables USB communicationThis function enables USB communicationand requires two arguments:and requires two arguments:the readthe read--buffer address and the writebuffer address and the write--buffer address. Itbuffer address. Itmust be called before any other functions of the USBmust be called before any other functions of the USBlibrary, and it returns no datalibrary, and it returns no dataHid_Read:Hid_Read: This function receives data from the USBThis function receives data from the USBbus and stores it in the receive buffer.bus and stores it in the receive buffer.

    It has no arguments but returns the number ofIt has no arguments but returns the number ofcharacters received.characters received.Hid_Write:Hid_Write: This function sends data from the writeThis function sends data from the write--buffer to the USB bus.buffer to the USB bus.The name of the buffer (the same buffer used in theThe name of the buffer (the same buffer used in the

    initialization) and the length of the data to be sent mustinitialization) and the length of the data to be sent mustbe specified as arguments to the function.be specified as arguments to the function.The function does not return any data.The function does not return any data.Hid_Disable:Hid_Disable: This function disables the USB dataThis function disables the USB datatransfer. It has no arguments and returns no data.transfer. It has no arguments and returns no data.

  • 8/6/2019 d tarek

    28/36

    PC softwarePC software ((EasyHID USB WizardEasyHID USB Wizard))

    EasyHID is designed to work with USBEasyHID is designed to work with USB

    22..00 it generates Visual Basic codes forit generates Visual Basic codes for

    the PC at the end of a USBthe PC at the end of a USBapplication using an HIDapplication using an HID--type devicetype deviceinterface.interface.

    In addition, the utility can generateIn addition, the utility can generateUSB template code for theUSB template code for thePICPIC1818FF45504550 and similarand similarmicrocontrollersmicrocontrollers

  • 8/6/2019 d tarek

    29/36

    EasyHID last formEasyHID last form

  • 8/6/2019 d tarek

    30/36

  • 8/6/2019 d tarek

    31/36

    Testing the ProjectTesting the Project

  • 8/6/2019 d tarek

    32/36

  • 8/6/2019 d tarek

    33/36

  • 8/6/2019 d tarek

    34/36

    Using the HID Terminal of microCUsing the HID Terminal of microC

    The microC IDE provides a USBThe microC IDE provides a USBterminal interface that can be usedterminal interface that can be usedfor sending andfor sending and

    receiving data over the USB bus.receiving data over the USB bus.

  • 8/6/2019 d tarek

    35/36

  • 8/6/2019 d tarek

    36/36

    Using the Visual Basic programUsing the Visual Basic program