brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame^] | 1 | /* |
| 2 | LPCUSB, an USB device driver for LPC microcontrollers |
| 3 | Copyright (C) 2006 Bertrik Sikken (bertrik@sikken.nl) |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | |
| 8 | 1. Redistributions of source code must retain the above copyright |
| 9 | notice, this list of conditions and the following disclaimer. |
| 10 | 2. Redistributions in binary form must reproduce the above copyright |
| 11 | notice, this list of conditions and the following disclaimer in the |
| 12 | documentation and/or other materials provided with the distribution. |
| 13 | 3. The name of the author may not be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | @file |
| 30 | */ |
| 31 | |
| 32 | #include "usbstruct.h" // for TSetupPacket |
| 33 | |
| 34 | /************************************************************************* |
| 35 | USB configuration |
| 36 | **************************************************************************/ |
| 37 | |
| 38 | #define MAX_PACKET_SIZE0 64 /**< maximum packet size for EP 0 */ |
| 39 | |
| 40 | /************************************************************************* |
| 41 | USB hardware interface |
| 42 | **************************************************************************/ |
| 43 | |
| 44 | // endpoint status sent through callback |
| 45 | #define EP_STATUS_DATA (1<<0) /**< EP has data */ |
| 46 | #define EP_STATUS_STALLED (1<<1) /**< EP is stalled */ |
| 47 | #define EP_STATUS_SETUP (1<<2) /**< EP received setup packet */ |
| 48 | #define EP_STATUS_ERROR (1<<3) /**< EP data was overwritten by setup packet */ |
| 49 | #define EP_STATUS_NACKED (1<<4) /**< EP sent NAK */ |
| 50 | |
| 51 | // device status sent through callback |
| 52 | #define DEV_STATUS_CONNECT (1<<0) /**< device just got connected */ |
| 53 | #define DEV_STATUS_SUSPEND (1<<2) /**< device entered suspend state */ |
| 54 | #define DEV_STATUS_RESET (1<<4) /**< device just got reset */ |
| 55 | |
| 56 | // interrupt bits for NACK events in USBHwNakIntEnable |
| 57 | // (these bits conveniently coincide with the LPC176x USB controller bit) |
| 58 | #define INACK_CI (1<<1) /**< interrupt on NACK for control in */ |
| 59 | #define INACK_CO (1<<2) /**< interrupt on NACK for control out */ |
| 60 | #define INACK_II (1<<3) /**< interrupt on NACK for interrupt in */ |
| 61 | #define INACK_IO (1<<4) /**< interrupt on NACK for interrupt out */ |
| 62 | #define INACK_BI (1<<5) /**< interrupt on NACK for bulk in */ |
| 63 | #define INACK_BO (1<<6) /**< interrupt on NACK for bulk out */ |
| 64 | |
| 65 | BOOL USBHwInit(void); |
| 66 | void USBHwISR(void); |
| 67 | |
| 68 | void USBHwNakIntEnable(unsigned char bIntBits); |
| 69 | |
| 70 | void USBHwConnect(BOOL fConnect); |
| 71 | int USBIsConnected(void); |
| 72 | |
| 73 | void USBHwSetAddress(unsigned char bAddr); |
| 74 | void USBHwConfigDevice(BOOL fConfigured); |
| 75 | |
| 76 | // endpoint operations |
| 77 | void USBHwEPConfig(unsigned char bEP, unsigned short wMaxPacketSize); |
| 78 | int USBHwEPRead(unsigned char bEP, unsigned char *pbBuf, int iMaxLen); |
| 79 | int USBHwEPWrite(unsigned char bEP, unsigned char *pbBuf, int iLen); |
| 80 | void USBHwEPStall(unsigned char bEP, BOOL fStall); |
| 81 | unsigned char USBHwEPGetStatus(unsigned char bEP); |
| 82 | |
| 83 | /** Endpoint interrupt handler callback */ |
| 84 | typedef void (TFnEPIntHandler)(unsigned char bEP, unsigned char bEPStatus); |
| 85 | void USBHwRegisterEPIntHandler(unsigned char bEP, TFnEPIntHandler *pfnHandler); |
| 86 | |
| 87 | /** Device status handler callback */ |
| 88 | typedef void (TFnDevIntHandler)(unsigned char bDevStatus); |
| 89 | void USBHwRegisterDevIntHandler(TFnDevIntHandler *pfnHandler); |
| 90 | |
| 91 | /** Frame event handler callback */ |
| 92 | typedef void (TFnFrameHandler)(unsigned short wFrame); |
| 93 | void USBHwRegisterFrameHandler(TFnFrameHandler *pfnHandler); |
| 94 | |
| 95 | /** Configuration event handler callback */ |
| 96 | typedef void (SetConfigHandler)(void); |
| 97 | void USBHwRegisterSetConfigHandler(SetConfigHandler *handler); |
| 98 | |
| 99 | |
| 100 | /************************************************************************* |
| 101 | USB application interface |
| 102 | **************************************************************************/ |
| 103 | |
| 104 | // initialise the complete stack, including HW |
| 105 | BOOL USBInit(void); |
| 106 | |
| 107 | /** Request handler callback (standard, vendor, class) */ |
| 108 | typedef BOOL (TFnHandleRequest)(TSetupPacket *pSetup, int *piLen, unsigned char **ppbData); |
| 109 | void USBRegisterRequestHandler(int iType, TFnHandleRequest *pfnHandler, unsigned char *pbDataStore); |
| 110 | void USBRegisterCustomReqHandler(TFnHandleRequest *pfnHandler); |
| 111 | |
| 112 | /** Descriptor handler callback */ |
| 113 | typedef BOOL (TFnGetDescriptor)(unsigned short wTypeIndex, unsigned short wLangID, int *piLen, unsigned char **ppbData); |
| 114 | |
| 115 | /** Default standard request handler */ |
| 116 | BOOL USBHandleStandardRequest(TSetupPacket *pSetup, int *piLen, unsigned char **ppbData); |
| 117 | |
| 118 | /** Default EP0 handler */ |
| 119 | void USBHandleControlTransfer(unsigned char bEP, unsigned char bEPStat); |
| 120 | |
| 121 | /** Descriptor handling */ |
| 122 | void USBRegisterDescriptors(const unsigned char *pabDescriptors); |
| 123 | BOOL USBGetDescriptor(unsigned short wTypeIndex, unsigned short wLangID, int *piLen, unsigned char **ppbData); |
| 124 | |
| 125 | void USBHwAllowConnect(void); |