ebook img

USB Programming for PIC18 Devices PDF

190 Pages·5.358 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview USB Programming for PIC18 Devices

AN INTRODUCTION TO USB AND USB PROGRAMMING ON MICROCHIP DEVICES VOL. 1 PROGRAMMING MICROCHIP PIC18 USB DEVICES © Copyright Andrew W. Eliasz and The Internet Technical Bookshop ISBN 978-0-9935315-0-7 Published by: The Internet Technical Bookshop [email protected] www.itbs.co.uk No representation or warranties are made with respect to the contents or use of this material, and Although third-party applications and software packages may be referenced herein, this is done for demonstration or explanatory purposes only and shall not constitute an endorsement of any of these software applications. Microchip is a registered trademark of Microchip Technology Inc. Linux is a registered trademark of Linux Torvalds. If there are any questions regarding the proper and fair use of the material herein, please contact Andrew Eliasz at [email protected]. i INTRODUCTION The origins of this book were via a Kickstarter project, which, it turned out was much more ambitious than I realised at the time. The amount of material that needed to be included was much greater than could be accomplished in a single volume. covering the topic of . The PIC18 exemplar board used is the Microchip Picdem FUSB board. will cover on PIC18, PIC24, dsPIC and PIC32 devices. . Will cover on PIC24 and PIC32 devices. . Will cover . Purchasers of the First Edition of this volume will be entitled to an upgrade to the second edition at a 50% discount. ii Introduction ………………………………………………………………………………………………………………………………………………………..pii 1.0 First Steps ………………………………………………………………………………………………………………………………………….. p1 1.1 Getting to Know USB ………………………………………………………………………………………………………………………….. p3 1.2 USB as a Networking Technology ………………………………………………………………………………………………………….. p3 1.3 Connecting up The Parts ……………………………………………………………………………………………………………………… p3 1.4 USB Cables and Connectors ……………………………………………………………………………………………………………….. p3 1.5 USB Device Power Provision ………………………………………………………………………………………………………………… p4 1.6 Data Transmission ………………………………………………………………………………………………………………………………. p5 1.7 Conserving Power by Powering Down Unused Devices …………………………………………………………………………… p9 1.8 USB Packets and Data Transmission ………………………………………………………………………………………………….. p10 ………………………………………………………..p10 1.8.2 Transactions …………………………………………………………………………………………………………………………………….. p13 1.9 USB Data Flow Types ……………………………………………………………………………………………………………………….. p14 1.9.1 Bulk Transfers ………………………………………………………………………………………………………………………………….. p14 1.9.2 Interrupt Transfers …………………………………………………………………………………………………………………………….. p15 1.9.3 Isochronous Transfers ……………………………………………………………………………………………………………………….. p15 1.9.4 Control Transfers ………………………………………………………………………………………………………………………………. p16 1.10 The USB Protocol In Action ………………………………………………………………………………………………………………… p17 1.10.1 Standard Requests And Device Operation ……………………………………………………………………………………………. p19 1.10.2 Get Descriptor Requests ……………………………………………………………………………………………………………………. p21 …………………………………………………………………………………………………………….. p23 1.10.4 SET_CONFIGURATION Requests ……………………………………………………………………………………………………… p26 1.11 USB Device Classes …………………………………………………………………………………………………………………………. p27 2.1 Picdem FSUSB Board - Running the Microchip Example Applications ……………………………………………………… p32 2.1.1 Getting To Grips With The MPLABX IDE (Integrated Development Environment) and the XC8 C Compiler ….. p33 2.1.2 Compiling and Trying Out The composite_hid_msd Project ……………………………………………………………………. p37 2.2 An Overview Of The Microchip USB Framework Modules ………………………………………………………………………. p45 2.2.1 app_device_hid.c - Source Code Overview ………………………………………………………………………………………….. p47 2.3 The Generic Hid Simple Demo - Code Overview …………………………………………………………………………………… p50 2.4 Hid PnP Demo ………………………………………………………………………………………………………………………………….. p54 2.5 Command Line Applications ………………………………………………………………………………………………………………. p60 2.5.1 Microsoft Windows - Raw HID Input …………………………………………………………………………………………………….. p60 2.5.2 Building An Initial Application ………………………………………………………………………………………………………………. p69 2.5.3 Adapting the Rawhid Teensy AVR Example For the Picdem FSUSB ……………………………………………………….. p69 2.5.4 Implementing a PC Windows HID Application Communicating with the Picdem FSUSB ……………………………………………………………………………………………… p70 iii 3.0 Introduction ………………………………………………………………………………………………………………………………………. p71 3.1 Understanding the PIC18 USB Subsystem …………………………………………………………………………………………… p72 3.1.1 Resources and Documentation …………………………………………………………………………………………………………… p72 3.2 USB - Oscillator Related Issues ………………………………………………………………………………………………………….. p74 3.2.1 The PLL Frequency Multiplier …………………………………………………………………………………………………………….. p75 3.2.2 Oscillator Settings for USB …………………………………………………………………………………………………………………. p76 3.3 USB and PIC18 Data Memory Organisation …………………………………………………………………………………………. p77 3.3.1 USB RAM ………………………………………………………………………………………………………………………………………… p77 3.3.2 USB Interrupts and PIC18 Interrupt Handling ……………………………………………………………………………………….. p78 3.4 The USB Peripheral in Detail ……………………………………………………………………………………………………………… p79 …………………………………………………………………………………………. p80 3.4.2 The USB Control Register (UCON) ……………………………………………………………………………………………………… p80 …………………………………………………………………………………………….. p83 3.4.4 The Internal Transceiver …………………………………………………………………………………………………………………….. p83 3.4.5 The External Transceiver …………………………………………………………………………………………………………………… p83 3.4.6 Pull Up Resistors ………………………………………………………………………………………………………………………………. p85 3.4.7 Ping Pong Buffer Usage …………………………………………………………………………………………………………………….. p86 3.4.8 Eye Pattern Test Enable …………………………………………………………………………………………………………………….. p86 3.4.9 Internal Regulator ……………………………………………………………………………………………………………………………… p86 3.4.10 The USB Status Register …………………………………………………………………………………………………………………… p87 3.4.11 USB Endpoint Control Registers …………………………………………………………………………………………………………. p88 3.4.12 USB Address Register (UADDR) ………………………………………………………………………………………………………… p89 3.4.13 USB Frame Number Registers (UFRMH:UFRML) …………………………………………………………………………………. p90 3.4.14 Mapping Registers and Register Bits to C Data Structures and Variables …………………………………………………. p90 3.5 USB RAM in Depth ……………………………………………………………………………………………………………………………. p91 3.5.1 Buffer Descriptors and Buffer Descriptor Table Buffer Descriptors …………………………………………………………… p92 3.5.2 BDnSTAT Registers in SIE Mode ………………………………………………………………………………………………………… p96 3.5.3 BD Address Validation ……………………………………………………………………………………………………………………….. p97 3.6 Ping-Pong Buffering ………………………………………………………………………………………………………………………….. p97 3.7 USB Interrupts in Depth …………………………………………………………………………………………………………………… p100 3.7.1 USB Interrupt Status Register (UIR) …………………………………………………………………………………………………… p102 3.7.2 Detecting Bus Activity ………………………………………………………………………………………………………………………. p103 3.7.3 USB Interrupt Enable (UIE) Register ………………………………………………………………………………………………….. p103 3.7.4 USB Error Interrupt Status Register (UEIR) ………………………………………………………………………………………… p104 3.8 Exploring the Microcihip USB Stack - Lower Layers …………………………………………………………………………….. p106 3.8.1 usb_device.c File …………………………………………………………………………………………………………………………….. p106 3.8.1.2 Static (Private) Methods …………………………………………………………………………………………………………………… p107 3.8.1.3 Global Variables ……………………………………………………………………………………………………………………………… p108 3.8.1.4 IN_PIPE and OUT_PIPE ………………………………………………………………………………………………………………….. p108 3.8.1.5 USBDeviceInit() ………………………………………………………………………………………………………………………………. p110 3.8.1.6 USBDeviceTasks() …………………………………………………………………………………………………………………………… p111 iv 3.8.1.7 Enabling Endpoints …………………………………………………………………………………………………………………………. p117 3.8.1.8 Transferring a Single Packet of Data on the USB Bus ………………………………………………………………………….. p118 3.8.1.9 Host To Device Control Transfers ………………………………………………………………………………………………………. p120 3.8.2 USB Chapter 9 - USB Device Framework …………………………………………………………………………………………… p122 3.8.2.1 USB Descriptors ……………………………………………………………………………………………………………………………… p122 3.8.2.2 USB Setup Packet Structure …………………………………………………………………………………………………………….. p124 3.8.2.3 USB Events ……………………………………………………………………………………………………………………………………. p125 3.8.2.4 Hardware Abstraction Layer (HAL) …………………………………………………………………………………………………….. p127 3.9 PIC18 USB Applications …………………………………………………………………………………………………………………… p130 3.9.1 USB HID Device Layer …………………………………………………………………………………………………………………….. p130 3.9.2 Checking USB HID Requests ……………………………………………………………………………………………………………. p134 3.10 Exploration of the Micrcochip MLA HID Examples for the Picdem FSUSB Board …………………………………….. p135 3.10.1 Generic HID Example (generic_hid) - Overview ………………………………………………………………………………….. p136 3.10.2 HID Descriptors ………………………………………………………………………………………………………………………………. p137 3.10.3 Device Descriptors …………………………………………………………………………………………………………………………. p138 3.10.4 Report Descriptors ………………………………………………………………………………………………………………………….. p139 3.10.5 System Initialisation …………………………………………………………………………………………………………………………. p139 3.10.6 Application Main Loop ……………………………………………………………………………………………………………………… p140 3.10.7 Callback Event Handler ……………………………………………………………………………………………………………………. p141 3.11 HID USB Mouse Example ………………………………………………………………………………………………………………… p142 3.11.1 Mouse HID Descriptors ……………………………………………………………………………………………………………………. p143 3.11.2 Mouse Device IN Report ………………………………………………………………………………………………………………….. p144 3.11.3 Mouse Device Behaviour …………………………………………………………………………………………………………………. p145 3.11.4 Mouse Device State Machine ……………………………………………………………………………………………………………. p147 3.12 USB HID Keyboard Example ……………………………………………………………………………………………………………. p150 3.12.1 Keyboard Scan Codes …………………………………………………………………………………………………………………….. p150 3.12.2 Keyboard Key to HID Page and HID ID Mapping …………………………………………………………………………………. p151 …………………………………………………………………………………. p152 3.12.4 Example HID Keyboard Driver Stateful Behaviour ……………………………………………………………………………….. p155 3.12.4.1 Keyboard Initialisation ……………………………………………………………………………………………………………………… p155 3.12.4.2 Keyboard Stateful Task Behaviour …………………………………………………………………………………………………….. p156 3.12.4.3 Handling Keyboard OUTPUT Reports From the PC (Host) …………………………………………………………………… p158 3.13 CDC Devices ………………………………………………………………………………………………………………………………….. p159 3.13.1 CDC Devices and Microsoft Windows ………………………………………………………………………………………………… p160 3.13.2 Overview of the Microchip CDC API …………………………………………………………………………………………………… p160 3.13.2.1 Transmission and Reception of Data …………………………………………………………………………………………………. p163 3.13.2.2 CDC Class API Methods ………………………………………………………………………………………………………………….. p163 3.13.3 Exploring the Microchip MLA Basic CDC Example ………………………………………………………………………………. p166 3.13.3.1 CDC Device Descriptors ………………………………………………………………………………………………………………….. p166 3.13.3.2 CDC Device Implementation and Behaviour ……………………………………………………………………………………….. p168 3.13.4 CDC Interfaces on Windows : INF Files and Device Installation …………………………………………………………….. p170 3.13.5 Microchip CDC Driver ………………………………………………………………………………………………………………………. p173 v

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.