firmware/usbdrv.h
author root@rika
Thu, 23 Apr 2009 20:55:41 +0200
changeset 33 7a0c4b0354ba
parent 2 2f55e5dd591d
permissions -rw-r--r--
updated documentation
     1 /*********************************************************************
     2  *
     3  *                Microchip USB C18 Firmware Version 1.0
     4  *
     5  *********************************************************************
     6  * FileName:        usbdrv.h
     7  * Dependencies:    See INCLUDES section below
     8  * Processor:       PIC18
     9  * Compiler:        C18 2.30.01+
    10  * Company:         Microchip Technology, Inc.
    11  *
    12  * Software License Agreement
    13  *
    14  * The software supplied herewith by Microchip Technology Incorporated
    15  * (the “Company”) for its PICmicro® Microcontroller is intended and
    16  * supplied to you, the Company’s customer, for use solely and
    17  * exclusively on Microchip PICmicro Microcontroller products. The
    18  * software is owned by the Company and/or its supplier, and is
    19  * protected under applicable copyright laws. All rights are reserved.
    20  * Any use in violation of the foregoing restrictions may subject the
    21  * user to criminal sanctions under applicable laws, as well as to
    22  * civil liability for the breach of the terms and conditions of this
    23  * license.
    24  *
    25  * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
    26  * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
    27  * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    28  * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
    29  * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
    30  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
    31  *
    32  * Author               Date        Comment
    33  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    34  * Rawin Rojvanit       11/19/04    Original.
    35  ********************************************************************/
    36 
    37 #ifndef USBDRV_H
    38 #define USBDRV_H
    39 
    40 /** I N C L U D E S **********************************************************/
    41 #include "typedefs.h"
    42 #include "usb.h"
    43 
    44 /** D E F I N I T I O N S ****************************************************/
    45 
    46 /* UCFG Initialization Parameters */
    47 #define _PPBM0      0x00            // Pingpong Buffer Mode 0
    48 #define _PPBM1      0x01            // Pingpong Buffer Mode 1
    49 #define _PPBM2      0x02            // Pingpong Buffer Mode 2
    50 #define _LS         0x00            // Use Low-Speed USB Mode
    51 #define _FS         0x04            // Use Full-Speed USB Mode
    52 #define _TRINT      0x00            // Use internal transceiver
    53 #define _TREXT      0x08            // Use external transceiver
    54 #define _PUEN       0x10            // Use internal pull-up resistor
    55 #define _OEMON      0x40            // Use SIE output indicator
    56 #define _UTEYE      0x80            // Use Eye-Pattern test
    57 
    58 /* UEPn Initialization Parameters */
    59 #define EP_CTRL     0x06            // Cfg Control pipe for this ep
    60 #define EP_OUT      0x0C            // Cfg OUT only pipe for this ep
    61 #define EP_IN       0x0A            // Cfg IN only pipe for this ep
    62 #define EP_OUT_IN   0x0E            // Cfg both OUT & IN pipes for this ep
    63 #define HSHK_EN     0x10            // Enable handshake packet
    64                                     // Handshake should be disable for isoch
    65 
    66 /******************************************************************************
    67  * USB - PICmicro Endpoint Definitions
    68  * PICmicro EP Address Format: X:EP3:EP2:EP1:EP0:DIR:PPBI:X
    69  * This is used when checking the value read from USTAT
    70  *
    71  * NOTE: These definitions are not used in the descriptors.
    72  * EP addresses used in the descriptors have different format and
    73  * are defined in: "system\usb\usbdefs\usbdefs_std_dsc.h"
    74  *****************************************************************************/
    75 #define OUT         0
    76 #define IN          1
    77 
    78 #define PIC_EP_NUM_MASK 0b01111000
    79 #define PIC_EP_DIR_MASK 0b00000100
    80 
    81 #define EP00_OUT    (0x00<<3)|(OUT<<2)
    82 #define EP00_IN     (0x00<<3)|(IN<<2)
    83 #define EP01_OUT    (0x01<<3)|(OUT<<2)
    84 #define EP01_IN     (0x01<<3)|(IN<<2)
    85 #define EP02_OUT    (0x02<<3)|(OUT<<2)
    86 #define EP02_IN     (0x02<<3)|(IN<<2)
    87 #define EP03_OUT    (0x03<<3)|(OUT<<2)
    88 #define EP03_IN     (0x03<<3)|(IN<<2)
    89 #define EP04_OUT    (0x04<<3)|(OUT<<2)
    90 #define EP04_IN     (0x04<<3)|(IN<<2)
    91 #define EP05_OUT    (0x05<<3)|(OUT<<2)
    92 #define EP05_IN     (0x05<<3)|(IN<<2)
    93 #define EP06_OUT    (0x06<<3)|(OUT<<2)
    94 #define EP06_IN     (0x06<<3)|(IN<<2)
    95 #define EP07_OUT    (0x07<<3)|(OUT<<2)
    96 #define EP07_IN     (0x07<<3)|(IN<<2)
    97 #define EP08_OUT    (0x08<<3)|(OUT<<2)
    98 #define EP08_IN     (0x08<<3)|(IN<<2)
    99 #define EP09_OUT    (0x09<<3)|(OUT<<2)
   100 #define EP09_IN     (0x09<<3)|(IN<<2)
   101 #define EP10_OUT    (0x0A<<3)|(OUT<<2)
   102 #define EP10_IN     (0x0A<<3)|(IN<<2)
   103 #define EP11_OUT    (0x0B<<3)|(OUT<<2)
   104 #define EP11_IN     (0x0B<<3)|(IN<<2)
   105 #define EP12_OUT    (0x0C<<3)|(OUT<<2)
   106 #define EP12_IN     (0x0C<<3)|(IN<<2)
   107 #define EP13_OUT    (0x0D<<3)|(OUT<<2)
   108 #define EP13_IN     (0x0D<<3)|(IN<<2)
   109 #define EP14_OUT    (0x0E<<3)|(OUT<<2)
   110 #define EP14_IN     (0x0E<<3)|(IN<<2)
   111 #define EP15_OUT    (0x0F<<3)|(OUT<<2)
   112 #define EP15_IN     (0x0F<<3)|(IN<<2)
   113 
   114 /******************************************************************************
   115  * Macro:           void mInitializeUSBDriver(void)
   116  *
   117  * PreCondition:    None
   118  *
   119  * Input:           None
   120  *
   121  * Output:          None
   122  *
   123  * Side Effects:    None
   124  *
   125  * Overview:        Configures the USB module, definition of UCFG_VAL can be
   126  *                  found in autofiles\usbcfg.h
   127  *
   128  *                  This register determines: USB Speed, On-chip pull-up
   129  *                  resistor selection, On-chip tranceiver selection, bus
   130  *                  eye pattern generation mode, Ping-pong buffering mode
   131  *                  selection.
   132  *
   133  * Note:            None
   134  *****************************************************************************/
   135 #define mInitializeUSBDriver()      {UCFG = UCFG_VAL;                       \
   136                                      usb_device_state = DETACHED_STATE;     \
   137                                      usb_stat._byte = 0x00;                 \
   138                                      usb_active_cfg = 0x00;}
   139 
   140 /******************************************************************************
   141  * Macro:           void mDisableEP1to15(void)
   142  *
   143  * PreCondition:    None
   144  *
   145  * Input:           None
   146  *
   147  * Output:          None
   148  *
   149  * Side Effects:    None
   150  *
   151  * Overview:        This macro disables all endpoints except EP0.
   152  *                  This macro should be called when the host sends a RESET
   153  *                  signal or a SET_CONFIGURATION request.
   154  *
   155  * Note:            None
   156  *****************************************************************************/
   157 #define mDisableEP1to15()       ClearArray((byte*)&UEP1,15);
   158 /*
   159 #define mDisableEP1to15()       UEP1=0x00;UEP2=0x00;UEP3=0x00;\
   160                                 UEP4=0x00;UEP5=0x00;UEP6=0x00;UEP7=0x00;\
   161                                 UEP8=0x00;UEP9=0x00;UEP10=0x00;UEP11=0x00;\
   162                                 UEP12=0x00;UEP13=0x00;UEP14=0x00;UEP15=0x00;
   163 */
   164 
   165 /******************************************************************************
   166  * Macro:           void mUSBBufferReady(buffer_dsc)
   167  *
   168  * PreCondition:    IN Endpoint: Buffer is loaded and ready to be sent.
   169  *                  OUT Endpoint: Buffer is free to be written to by SIE.
   170  *
   171  * Input:           byte buffer_dsc: Root name of the buffer descriptor group.
   172  *                  i.e. ep0Bo, ep1Bi, ... Declared in usbmmap.c
   173  *                  Names can be remapped for readability, see examples in
   174  *                  usbcfg.h (#define HID_BD_OUT      ep1Bo)
   175  *
   176  * Output:          None
   177  *
   178  * Side Effects:    None
   179  *
   180  * Overview:        This macro should be called each time after:
   181  *                  1. A non-EP0 IN endpoint buffer is populated with data.
   182  *                  2. A non-EP0 OUT endpoint buffer is read.
   183  *                  This macro turns the buffer ownership to SIE for servicing.
   184  *                  It also toggles the DTS bit for synchronization.
   185  *
   186  * Note:            None
   187  *****************************************************************************/
   188 #define mUSBBufferReady(buffer_dsc)                                         \
   189 {                                                                           \
   190     buffer_dsc.Stat._byte &= _DTSMASK;          /* Save only DTS bit */     \
   191     buffer_dsc.Stat.DTS = !buffer_dsc.Stat.DTS; /* Toggle DTS bit    */     \
   192     buffer_dsc.Stat._byte |= _USIE|_DTSEN;      /* Turn ownership to SIE */ \
   193 }
   194 
   195 /** T Y P E S ****************************************************************/
   196 
   197 /** E X T E R N S ************************************************************/
   198 
   199 /** P U B L I C  P R O T O T Y P E S *****************************************/
   200 void USBCheckBusStatus(void);
   201 void USBDriverService(void);
   202 void USBRemoteWakeup(void);
   203 void USBSoftDetach(void); 
   204 
   205 void ClearArray(byte* startAdr,byte count);
   206 #endif //USBDRV_H