These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!

Committer:
frank26080115
Date:
Sun Mar 20 05:38:56 2011 +0000
Revision:
0:bf7b9fba3924

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frank26080115 0:bf7b9fba3924 1 /*----------------------------------------------------------------------------
frank26080115 0:bf7b9fba3924 2 * U S B - K e r n e l
frank26080115 0:bf7b9fba3924 3 *----------------------------------------------------------------------------
frank26080115 0:bf7b9fba3924 4 * Name: USBHW.C
frank26080115 0:bf7b9fba3924 5 * Purpose: USB Hardware Layer Module for NXP Semiconductor LPC family MCUs
frank26080115 0:bf7b9fba3924 6 * Version: V1.10
frank26080115 0:bf7b9fba3924 7 *----------------------------------------------------------------------------
frank26080115 0:bf7b9fba3924 8 * This software is supplied "AS IS" without any warranties, express,
frank26080115 0:bf7b9fba3924 9 * implied or statutory, including but not limited to the implied
frank26080115 0:bf7b9fba3924 10 * warranties of fitness for purpose, satisfactory quality and
frank26080115 0:bf7b9fba3924 11 * noninfringement. Keil extends you a royalty-free right to reproduce
frank26080115 0:bf7b9fba3924 12 * and distribute executable files created using this software for use
frank26080115 0:bf7b9fba3924 13 * on NXP Semiconductors LPC family microcontroller devices only. Nothing
frank26080115 0:bf7b9fba3924 14 * else gives you the right to use this software.
frank26080115 0:bf7b9fba3924 15 *
frank26080115 0:bf7b9fba3924 16 * Copyright (c) 2005-2009 Keil Software.
frank26080115 0:bf7b9fba3924 17 *---------------------------------------------------------------------------*/
frank26080115 0:bf7b9fba3924 18
frank26080115 0:bf7b9fba3924 19 #include "LPC17xx.h" /* LPC17xx definitions */
frank26080115 0:bf7b9fba3924 20
frank26080115 0:bf7b9fba3924 21 #include "lpc_types.h"
frank26080115 0:bf7b9fba3924 22
frank26080115 0:bf7b9fba3924 23 #include "usb.h"
frank26080115 0:bf7b9fba3924 24 #include "usbcfg.h"
frank26080115 0:bf7b9fba3924 25 #include "usbreg.h"
frank26080115 0:bf7b9fba3924 26 #include "usbhw.h"
frank26080115 0:bf7b9fba3924 27 #include "usbcore.h"
frank26080115 0:bf7b9fba3924 28 #include "usbuser.h"
frank26080115 0:bf7b9fba3924 29
frank26080115 0:bf7b9fba3924 30 #ifndef __IAR_SYSTEMS_ICC__
frank26080115 0:bf7b9fba3924 31 #pragma diag_suppress 1441
frank26080115 0:bf7b9fba3924 32 #endif
frank26080115 0:bf7b9fba3924 33
frank26080115 0:bf7b9fba3924 34 #define EP_MSK_CTRL 0x0001 /* Control Endpoint Logical Address Mask */
frank26080115 0:bf7b9fba3924 35 #define EP_MSK_BULK 0xC924 /* Bulk Endpoint Logical Address Mask */
frank26080115 0:bf7b9fba3924 36 #define EP_MSK_INT 0x4492 /* Interrupt Endpoint Logical Address Mask */
frank26080115 0:bf7b9fba3924 37 #define EP_MSK_ISO 0x1248 /* Isochronous Endpoint Logical Address Mask */
frank26080115 0:bf7b9fba3924 38
frank26080115 0:bf7b9fba3924 39
frank26080115 0:bf7b9fba3924 40 #if USB_DMA
frank26080115 0:bf7b9fba3924 41
frank26080115 0:bf7b9fba3924 42 #if defined ( __CC_ARM )
frank26080115 0:bf7b9fba3924 43 #pragma arm section zidata = "USB_RAM"
frank26080115 0:bf7b9fba3924 44 uint32_t UDCA[USB_EP_NUM]; /* UDCA in USB RAM */
frank26080115 0:bf7b9fba3924 45 uint32_t DD_NISO_Mem[4*DD_NISO_CNT]; /* Non-Iso DMA Descriptor Memory */
frank26080115 0:bf7b9fba3924 46 uint32_t DD_ISO_Mem [5*DD_ISO_CNT]; /* Iso DMA Descriptor Memory */
frank26080115 0:bf7b9fba3924 47 #pragma arm section zidata
frank26080115 0:bf7b9fba3924 48 uint32_t udca[USB_EP_NUM]; /* UDCA saved values */
frank26080115 0:bf7b9fba3924 49 uint32_t DDMemMap[2]; /* DMA Descriptor Memory Usage */
frank26080115 0:bf7b9fba3924 50 #endif
frank26080115 0:bf7b9fba3924 51
frank26080115 0:bf7b9fba3924 52 #if defined ( __IAR_SYSTEMS_ICC__ )
frank26080115 0:bf7b9fba3924 53 #pragma location = "USB_RAM"
frank26080115 0:bf7b9fba3924 54 uint32_t UDCA[USB_EP_NUM]; /* UDCA in USB RAM */
frank26080115 0:bf7b9fba3924 55 #pragma location = "USB_RAM"
frank26080115 0:bf7b9fba3924 56 uint32_t DD_NISO_Mem[4*DD_NISO_CNT]; /* Non-Iso DMA Descriptor Memory */
frank26080115 0:bf7b9fba3924 57 #pragma location = "USB_RAM"
frank26080115 0:bf7b9fba3924 58 uint32_t DD_ISO_Mem [5*DD_ISO_CNT]; /* Iso DMA Descriptor Memory */
frank26080115 0:bf7b9fba3924 59
frank26080115 0:bf7b9fba3924 60 uint32_t udca[USB_EP_NUM]; /* UDCA saved values */
frank26080115 0:bf7b9fba3924 61 uint32_t DDMemMap[2]; /* DMA Descriptor Memory Usage */
frank26080115 0:bf7b9fba3924 62 #endif
frank26080115 0:bf7b9fba3924 63
frank26080115 0:bf7b9fba3924 64 #if defined ( __GNUC__ )
frank26080115 0:bf7b9fba3924 65 uint32_t UDCA[USB_EP_NUM] __attribute__((section("USB_RAM"))); /* UDCA in USB RAM */
frank26080115 0:bf7b9fba3924 66 uint32_t DD_NISO_Mem[4*DD_NISO_CNT] __attribute__((section("USB_RAM"))); /* Non-Iso DMA Descriptor Memory */
frank26080115 0:bf7b9fba3924 67 uint32_t DD_ISO_Mem [5*DD_ISO_CNT] __attribute__((section("USB_RAM"))); /* Iso DMA Descriptor Memory */
frank26080115 0:bf7b9fba3924 68 uint32_t udca[USB_EP_NUM]; /* UDCA saved values */
frank26080115 0:bf7b9fba3924 69 uint32_t DDMemMap[2]; /* DMA Descriptor Memory Usage */
frank26080115 0:bf7b9fba3924 70 #endif
frank26080115 0:bf7b9fba3924 71
frank26080115 0:bf7b9fba3924 72 #endif
frank26080115 0:bf7b9fba3924 73
frank26080115 0:bf7b9fba3924 74
frank26080115 0:bf7b9fba3924 75 /*
frank26080115 0:bf7b9fba3924 76 * Get Endpoint Physical Address
frank26080115 0:bf7b9fba3924 77 * Parameters: EPNum: Endpoint Number
frank26080115 0:bf7b9fba3924 78 * EPNum.0..3: Address
frank26080115 0:bf7b9fba3924 79 * EPNum.7: Dir
frank26080115 0:bf7b9fba3924 80 * Return Value: Endpoint Physical Address
frank26080115 0:bf7b9fba3924 81 */
frank26080115 0:bf7b9fba3924 82
frank26080115 0:bf7b9fba3924 83 uint32_t EPAdr (uint32_t EPNum) {
frank26080115 0:bf7b9fba3924 84 uint32_t val;
frank26080115 0:bf7b9fba3924 85
frank26080115 0:bf7b9fba3924 86 val = (EPNum & 0x0F) << 1;
frank26080115 0:bf7b9fba3924 87 if (EPNum & 0x80) {
frank26080115 0:bf7b9fba3924 88 val += 1;
frank26080115 0:bf7b9fba3924 89 }
frank26080115 0:bf7b9fba3924 90 return (val);
frank26080115 0:bf7b9fba3924 91 }
frank26080115 0:bf7b9fba3924 92
frank26080115 0:bf7b9fba3924 93
frank26080115 0:bf7b9fba3924 94 /*
frank26080115 0:bf7b9fba3924 95 * Write Command
frank26080115 0:bf7b9fba3924 96 * Parameters: cmd: Command
frank26080115 0:bf7b9fba3924 97 * Return Value: None
frank26080115 0:bf7b9fba3924 98 */
frank26080115 0:bf7b9fba3924 99
frank26080115 0:bf7b9fba3924 100 void WrCmd (uint32_t cmd) {
frank26080115 0:bf7b9fba3924 101
frank26080115 0:bf7b9fba3924 102 LPC_USB->USBDevIntClr = CCEMTY_INT;
frank26080115 0:bf7b9fba3924 103 LPC_USB->USBCmdCode = cmd;
frank26080115 0:bf7b9fba3924 104 while ((LPC_USB->USBDevIntSt & CCEMTY_INT) == 0);
frank26080115 0:bf7b9fba3924 105 }
frank26080115 0:bf7b9fba3924 106
frank26080115 0:bf7b9fba3924 107
frank26080115 0:bf7b9fba3924 108 /*
frank26080115 0:bf7b9fba3924 109 * Write Command Data
frank26080115 0:bf7b9fba3924 110 * Parameters: cmd: Command
frank26080115 0:bf7b9fba3924 111 * val: Data
frank26080115 0:bf7b9fba3924 112 * Return Value: None
frank26080115 0:bf7b9fba3924 113 */
frank26080115 0:bf7b9fba3924 114
frank26080115 0:bf7b9fba3924 115 void WrCmdDat (uint32_t cmd, uint32_t val) {
frank26080115 0:bf7b9fba3924 116
frank26080115 0:bf7b9fba3924 117 LPC_USB->USBDevIntClr = CCEMTY_INT;
frank26080115 0:bf7b9fba3924 118 LPC_USB->USBCmdCode = cmd;
frank26080115 0:bf7b9fba3924 119 while ((LPC_USB->USBDevIntSt & CCEMTY_INT) == 0);
frank26080115 0:bf7b9fba3924 120 LPC_USB->USBDevIntClr = CCEMTY_INT;
frank26080115 0:bf7b9fba3924 121 LPC_USB->USBCmdCode = val;
frank26080115 0:bf7b9fba3924 122 while ((LPC_USB->USBDevIntSt & CCEMTY_INT) == 0);
frank26080115 0:bf7b9fba3924 123 }
frank26080115 0:bf7b9fba3924 124
frank26080115 0:bf7b9fba3924 125
frank26080115 0:bf7b9fba3924 126 /*
frank26080115 0:bf7b9fba3924 127 * Read Command Data
frank26080115 0:bf7b9fba3924 128 * Parameters: cmd: Command
frank26080115 0:bf7b9fba3924 129 * Return Value: Data Value
frank26080115 0:bf7b9fba3924 130 */
frank26080115 0:bf7b9fba3924 131
frank26080115 0:bf7b9fba3924 132 uint32_t RdCmdDat (uint32_t cmd) {
frank26080115 0:bf7b9fba3924 133
frank26080115 0:bf7b9fba3924 134 LPC_USB->USBDevIntClr = CCEMTY_INT | CDFULL_INT;
frank26080115 0:bf7b9fba3924 135 LPC_USB->USBCmdCode = cmd;
frank26080115 0:bf7b9fba3924 136 while ((LPC_USB->USBDevIntSt & CDFULL_INT) == 0);
frank26080115 0:bf7b9fba3924 137 return (LPC_USB->USBCmdData);
frank26080115 0:bf7b9fba3924 138 }
frank26080115 0:bf7b9fba3924 139
frank26080115 0:bf7b9fba3924 140
frank26080115 0:bf7b9fba3924 141 /*
frank26080115 0:bf7b9fba3924 142 * USB Initialize Function
frank26080115 0:bf7b9fba3924 143 * Called by the User to initialize USB
frank26080115 0:bf7b9fba3924 144 * Return Value: None
frank26080115 0:bf7b9fba3924 145 */
frank26080115 0:bf7b9fba3924 146
frank26080115 0:bf7b9fba3924 147 void USB_Init (void) {
frank26080115 0:bf7b9fba3924 148
frank26080115 0:bf7b9fba3924 149 LPC_PINCON->PINSEL1 &= ~((3<<26)|(3<<28)); /* P0.29 D+, P0.30 D- */
frank26080115 0:bf7b9fba3924 150 LPC_PINCON->PINSEL1 |= ((1<<26)|(1<<28)); /* PINSEL1 26.27, 28.29 = 01 */
frank26080115 0:bf7b9fba3924 151
frank26080115 0:bf7b9fba3924 152 LPC_PINCON->PINSEL3 &= ~((3<< 4)|(3<<28)); /* P1.18 GoodLink, P1.30 VBUS */
frank26080115 0:bf7b9fba3924 153 LPC_PINCON->PINSEL3 |= ((1<< 4)|(2<<28)); /* PINSEL3 4.5 = 01, 28.29 = 10 */
frank26080115 0:bf7b9fba3924 154
frank26080115 0:bf7b9fba3924 155 LPC_PINCON->PINSEL4 &= ~((3<<18) ); /* P2.9 SoftConnect */
frank26080115 0:bf7b9fba3924 156 LPC_PINCON->PINSEL4 |= ((1<<18) ); /* PINSEL4 18.19 = 01 */
frank26080115 0:bf7b9fba3924 157
frank26080115 0:bf7b9fba3924 158 LPC_SC->PCONP |= (1UL<<31); /* USB PCLK -> enable USB Per. */
frank26080115 0:bf7b9fba3924 159
frank26080115 0:bf7b9fba3924 160 LPC_USB->USBClkCtrl = 0x1A; /* Dev, PortSel, AHB clock enable */
frank26080115 0:bf7b9fba3924 161 while ((LPC_USB->USBClkSt & 0x1A) != 0x1A);
frank26080115 0:bf7b9fba3924 162
frank26080115 0:bf7b9fba3924 163 NVIC_EnableIRQ(USB_IRQn); /* enable USB interrupt */
frank26080115 0:bf7b9fba3924 164
frank26080115 0:bf7b9fba3924 165 USB_Reset();
frank26080115 0:bf7b9fba3924 166 USB_SetAddress(0);
frank26080115 0:bf7b9fba3924 167 }
frank26080115 0:bf7b9fba3924 168
frank26080115 0:bf7b9fba3924 169
frank26080115 0:bf7b9fba3924 170 /*
frank26080115 0:bf7b9fba3924 171 * USB Connect Function
frank26080115 0:bf7b9fba3924 172 * Called by the User to Connect/Disconnect USB
frank26080115 0:bf7b9fba3924 173 * Parameters: con: Connect/Disconnect
frank26080115 0:bf7b9fba3924 174 * Return Value: None
frank26080115 0:bf7b9fba3924 175 */
frank26080115 0:bf7b9fba3924 176
frank26080115 0:bf7b9fba3924 177 void USB_Connect (uint32_t con) {
frank26080115 0:bf7b9fba3924 178 WrCmdDat(CMD_SET_DEV_STAT, DAT_WR_BYTE(con ? DEV_CON : 0));
frank26080115 0:bf7b9fba3924 179 }
frank26080115 0:bf7b9fba3924 180
frank26080115 0:bf7b9fba3924 181
frank26080115 0:bf7b9fba3924 182 /*
frank26080115 0:bf7b9fba3924 183 * USB Reset Function
frank26080115 0:bf7b9fba3924 184 * Called automatically on USB Reset
frank26080115 0:bf7b9fba3924 185 * Return Value: None
frank26080115 0:bf7b9fba3924 186 */
frank26080115 0:bf7b9fba3924 187
frank26080115 0:bf7b9fba3924 188 void USB_Reset (void) {
frank26080115 0:bf7b9fba3924 189 #if USB_DMA
frank26080115 0:bf7b9fba3924 190 uint32_t n;
frank26080115 0:bf7b9fba3924 191 #endif
frank26080115 0:bf7b9fba3924 192
frank26080115 0:bf7b9fba3924 193 LPC_USB->USBEpInd = 0;
frank26080115 0:bf7b9fba3924 194 LPC_USB->USBMaxPSize = USB_MAX_PACKET0;
frank26080115 0:bf7b9fba3924 195 LPC_USB->USBEpInd = 1;
frank26080115 0:bf7b9fba3924 196 LPC_USB->USBMaxPSize = USB_MAX_PACKET0;
frank26080115 0:bf7b9fba3924 197 while ((LPC_USB->USBDevIntSt & EP_RLZED_INT) == 0);
frank26080115 0:bf7b9fba3924 198
frank26080115 0:bf7b9fba3924 199 LPC_USB->USBEpIntClr = 0xFFFFFFFF;
frank26080115 0:bf7b9fba3924 200 LPC_USB->USBEpIntEn = 0xFFFFFFFF ^ USB_DMA_EP;
frank26080115 0:bf7b9fba3924 201 LPC_USB->USBDevIntClr = 0xFFFFFFFF;
frank26080115 0:bf7b9fba3924 202 LPC_USB->USBDevIntEn = DEV_STAT_INT | EP_SLOW_INT |
frank26080115 0:bf7b9fba3924 203 (USB_SOF_EVENT ? FRAME_INT : 0) |
frank26080115 0:bf7b9fba3924 204 (USB_ERROR_EVENT ? ERR_INT : 0);
frank26080115 0:bf7b9fba3924 205
frank26080115 0:bf7b9fba3924 206 #if USB_DMA
frank26080115 0:bf7b9fba3924 207 LPC_USB->USBUDCAH = USB_RAM_ADR;
frank26080115 0:bf7b9fba3924 208 LPC_USB->USBDMARClr = 0xFFFFFFFF;
frank26080115 0:bf7b9fba3924 209 LPC_USB->USBEpDMADis = 0xFFFFFFFF;
frank26080115 0:bf7b9fba3924 210 LPC_USB->USBEpDMAEn = USB_DMA_EP;
frank26080115 0:bf7b9fba3924 211 LPC_USB->USBEoTIntClr = 0xFFFFFFFF;
frank26080115 0:bf7b9fba3924 212 LPC_USB->USBNDDRIntClr = 0xFFFFFFFF;
frank26080115 0:bf7b9fba3924 213 LPC_USB->USBSysErrIntClr = 0xFFFFFFFF;
frank26080115 0:bf7b9fba3924 214 LPC_USB->USBDMAIntEn = 0x00000007;
frank26080115 0:bf7b9fba3924 215 DDMemMap[0] = 0x00000000;
frank26080115 0:bf7b9fba3924 216 DDMemMap[1] = 0x00000000;
frank26080115 0:bf7b9fba3924 217 for (n = 0; n < USB_EP_NUM; n++) {
frank26080115 0:bf7b9fba3924 218 udca[n] = 0;
frank26080115 0:bf7b9fba3924 219 UDCA[n] = 0;
frank26080115 0:bf7b9fba3924 220 }
frank26080115 0:bf7b9fba3924 221 #endif
frank26080115 0:bf7b9fba3924 222 }
frank26080115 0:bf7b9fba3924 223
frank26080115 0:bf7b9fba3924 224
frank26080115 0:bf7b9fba3924 225 /*
frank26080115 0:bf7b9fba3924 226 * USB Suspend Function
frank26080115 0:bf7b9fba3924 227 * Called automatically on USB Suspend
frank26080115 0:bf7b9fba3924 228 * Return Value: None
frank26080115 0:bf7b9fba3924 229 */
frank26080115 0:bf7b9fba3924 230
frank26080115 0:bf7b9fba3924 231 void USB_Suspend (void) {
frank26080115 0:bf7b9fba3924 232 /* Performed by Hardware */
frank26080115 0:bf7b9fba3924 233 }
frank26080115 0:bf7b9fba3924 234
frank26080115 0:bf7b9fba3924 235
frank26080115 0:bf7b9fba3924 236 /*
frank26080115 0:bf7b9fba3924 237 * USB Resume Function
frank26080115 0:bf7b9fba3924 238 * Called automatically on USB Resume
frank26080115 0:bf7b9fba3924 239 * Return Value: None
frank26080115 0:bf7b9fba3924 240 */
frank26080115 0:bf7b9fba3924 241
frank26080115 0:bf7b9fba3924 242 void USB_Resume (void) {
frank26080115 0:bf7b9fba3924 243 /* Performed by Hardware */
frank26080115 0:bf7b9fba3924 244 }
frank26080115 0:bf7b9fba3924 245
frank26080115 0:bf7b9fba3924 246
frank26080115 0:bf7b9fba3924 247 /*
frank26080115 0:bf7b9fba3924 248 * USB Remote Wakeup Function
frank26080115 0:bf7b9fba3924 249 * Called automatically on USB Remote Wakeup
frank26080115 0:bf7b9fba3924 250 * Return Value: None
frank26080115 0:bf7b9fba3924 251 */
frank26080115 0:bf7b9fba3924 252
frank26080115 0:bf7b9fba3924 253 void USB_WakeUp (void) {
frank26080115 0:bf7b9fba3924 254
frank26080115 0:bf7b9fba3924 255 if (USB_DeviceStatus & USB_GETSTATUS_REMOTE_WAKEUP) {
frank26080115 0:bf7b9fba3924 256 WrCmdDat(CMD_SET_DEV_STAT, DAT_WR_BYTE(DEV_CON));
frank26080115 0:bf7b9fba3924 257 }
frank26080115 0:bf7b9fba3924 258 }
frank26080115 0:bf7b9fba3924 259
frank26080115 0:bf7b9fba3924 260
frank26080115 0:bf7b9fba3924 261 /*
frank26080115 0:bf7b9fba3924 262 * USB Remote Wakeup Configuration Function
frank26080115 0:bf7b9fba3924 263 * Parameters: cfg: Enable/Disable
frank26080115 0:bf7b9fba3924 264 * Return Value: None
frank26080115 0:bf7b9fba3924 265 */
frank26080115 0:bf7b9fba3924 266
frank26080115 0:bf7b9fba3924 267 void USB_WakeUpCfg (uint32_t cfg) {
frank26080115 0:bf7b9fba3924 268 /* Not needed */
frank26080115 0:bf7b9fba3924 269 }
frank26080115 0:bf7b9fba3924 270
frank26080115 0:bf7b9fba3924 271
frank26080115 0:bf7b9fba3924 272 /*
frank26080115 0:bf7b9fba3924 273 * USB Set Address Function
frank26080115 0:bf7b9fba3924 274 * Parameters: adr: USB Address
frank26080115 0:bf7b9fba3924 275 * Return Value: None
frank26080115 0:bf7b9fba3924 276 */
frank26080115 0:bf7b9fba3924 277
frank26080115 0:bf7b9fba3924 278 void USB_SetAddress (uint32_t adr) {
frank26080115 0:bf7b9fba3924 279 WrCmdDat(CMD_SET_ADDR, DAT_WR_BYTE(DEV_EN | adr)); /* Don't wait for next */
frank26080115 0:bf7b9fba3924 280 //WrCmdDat(CMD_SET_ADDR, DAT_WR_BYTE(DEV_EN | adr)); /* Setup Status Phase */
frank26080115 0:bf7b9fba3924 281 }
frank26080115 0:bf7b9fba3924 282
frank26080115 0:bf7b9fba3924 283
frank26080115 0:bf7b9fba3924 284 /*
frank26080115 0:bf7b9fba3924 285 * USB Configure Function
frank26080115 0:bf7b9fba3924 286 * Parameters: cfg: Configure/Deconfigure
frank26080115 0:bf7b9fba3924 287 * Return Value: None
frank26080115 0:bf7b9fba3924 288 */
frank26080115 0:bf7b9fba3924 289
frank26080115 0:bf7b9fba3924 290 void USB_Configure (uint32_t cfg) {
frank26080115 0:bf7b9fba3924 291
frank26080115 0:bf7b9fba3924 292 WrCmdDat(CMD_CFG_DEV, DAT_WR_BYTE(cfg ? CONF_DVICE : 0));
frank26080115 0:bf7b9fba3924 293
frank26080115 0:bf7b9fba3924 294 LPC_USB->USBReEp = 0x00000003;
frank26080115 0:bf7b9fba3924 295 while ((LPC_USB->USBDevIntSt & EP_RLZED_INT) == 0);
frank26080115 0:bf7b9fba3924 296 LPC_USB->USBDevIntClr = EP_RLZED_INT;
frank26080115 0:bf7b9fba3924 297 }
frank26080115 0:bf7b9fba3924 298
frank26080115 0:bf7b9fba3924 299
frank26080115 0:bf7b9fba3924 300 /*
frank26080115 0:bf7b9fba3924 301 * Configure USB Endpoint according to Descriptor
frank26080115 0:bf7b9fba3924 302 * Parameters: pEPD: Pointer to Endpoint Descriptor
frank26080115 0:bf7b9fba3924 303 * Return Value: None
frank26080115 0:bf7b9fba3924 304 */
frank26080115 0:bf7b9fba3924 305
frank26080115 0:bf7b9fba3924 306 void USB_ConfigEP (USB_ENDPOINT_DESCRIPTOR *pEPD) {
frank26080115 0:bf7b9fba3924 307 uint32_t num;
frank26080115 0:bf7b9fba3924 308
frank26080115 0:bf7b9fba3924 309 num = EPAdr(pEPD->bEndpointAddress);
frank26080115 0:bf7b9fba3924 310 LPC_USB->USBReEp |= (1 << num);
frank26080115 0:bf7b9fba3924 311 LPC_USB->USBEpInd = num;
frank26080115 0:bf7b9fba3924 312 LPC_USB->USBMaxPSize = pEPD->wMaxPacketSize;
frank26080115 0:bf7b9fba3924 313 while ((LPC_USB->USBDevIntSt & EP_RLZED_INT) == 0);
frank26080115 0:bf7b9fba3924 314 LPC_USB->USBDevIntClr = EP_RLZED_INT;
frank26080115 0:bf7b9fba3924 315 }
frank26080115 0:bf7b9fba3924 316
frank26080115 0:bf7b9fba3924 317
frank26080115 0:bf7b9fba3924 318 /*
frank26080115 0:bf7b9fba3924 319 * Set Direction for USB Control Endpoint
frank26080115 0:bf7b9fba3924 320 * Parameters: dir: Out (dir == 0), In (dir <> 0)
frank26080115 0:bf7b9fba3924 321 * Return Value: None
frank26080115 0:bf7b9fba3924 322 */
frank26080115 0:bf7b9fba3924 323
frank26080115 0:bf7b9fba3924 324 void USB_DirCtrlEP (uint32_t dir) {
frank26080115 0:bf7b9fba3924 325 /* Not needed */
frank26080115 0:bf7b9fba3924 326 }
frank26080115 0:bf7b9fba3924 327
frank26080115 0:bf7b9fba3924 328
frank26080115 0:bf7b9fba3924 329 /*
frank26080115 0:bf7b9fba3924 330 * Enable USB Endpoint
frank26080115 0:bf7b9fba3924 331 * Parameters: EPNum: Endpoint Number
frank26080115 0:bf7b9fba3924 332 * EPNum.0..3: Address
frank26080115 0:bf7b9fba3924 333 * EPNum.7: Dir
frank26080115 0:bf7b9fba3924 334 * Return Value: None
frank26080115 0:bf7b9fba3924 335 */
frank26080115 0:bf7b9fba3924 336
frank26080115 0:bf7b9fba3924 337 void USB_EnableEP (uint32_t EPNum) {
frank26080115 0:bf7b9fba3924 338 WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
frank26080115 0:bf7b9fba3924 339 }
frank26080115 0:bf7b9fba3924 340
frank26080115 0:bf7b9fba3924 341
frank26080115 0:bf7b9fba3924 342 /*
frank26080115 0:bf7b9fba3924 343 * Disable USB Endpoint
frank26080115 0:bf7b9fba3924 344 * Parameters: EPNum: Endpoint Number
frank26080115 0:bf7b9fba3924 345 * EPNum.0..3: Address
frank26080115 0:bf7b9fba3924 346 * EPNum.7: Dir
frank26080115 0:bf7b9fba3924 347 * Return Value: None
frank26080115 0:bf7b9fba3924 348 */
frank26080115 0:bf7b9fba3924 349
frank26080115 0:bf7b9fba3924 350 void USB_DisableEP (uint32_t EPNum) {
frank26080115 0:bf7b9fba3924 351 WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(EP_STAT_DA));
frank26080115 0:bf7b9fba3924 352 }
frank26080115 0:bf7b9fba3924 353
frank26080115 0:bf7b9fba3924 354
frank26080115 0:bf7b9fba3924 355 /*
frank26080115 0:bf7b9fba3924 356 * Reset USB Endpoint
frank26080115 0:bf7b9fba3924 357 * Parameters: EPNum: Endpoint Number
frank26080115 0:bf7b9fba3924 358 * EPNum.0..3: Address
frank26080115 0:bf7b9fba3924 359 * EPNum.7: Dir
frank26080115 0:bf7b9fba3924 360 * Return Value: None
frank26080115 0:bf7b9fba3924 361 */
frank26080115 0:bf7b9fba3924 362
frank26080115 0:bf7b9fba3924 363 void USB_ResetEP (uint32_t EPNum) {
frank26080115 0:bf7b9fba3924 364 WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
frank26080115 0:bf7b9fba3924 365 }
frank26080115 0:bf7b9fba3924 366
frank26080115 0:bf7b9fba3924 367
frank26080115 0:bf7b9fba3924 368 /*
frank26080115 0:bf7b9fba3924 369 * Set Stall for USB Endpoint
frank26080115 0:bf7b9fba3924 370 * Parameters: EPNum: Endpoint Number
frank26080115 0:bf7b9fba3924 371 * EPNum.0..3: Address
frank26080115 0:bf7b9fba3924 372 * EPNum.7: Dir
frank26080115 0:bf7b9fba3924 373 * Return Value: None
frank26080115 0:bf7b9fba3924 374 */
frank26080115 0:bf7b9fba3924 375
frank26080115 0:bf7b9fba3924 376 void USB_SetStallEP (uint32_t EPNum) {
frank26080115 0:bf7b9fba3924 377 WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(EP_STAT_ST));
frank26080115 0:bf7b9fba3924 378 }
frank26080115 0:bf7b9fba3924 379
frank26080115 0:bf7b9fba3924 380
frank26080115 0:bf7b9fba3924 381 /*
frank26080115 0:bf7b9fba3924 382 * Clear Stall for USB Endpoint
frank26080115 0:bf7b9fba3924 383 * Parameters: EPNum: Endpoint Number
frank26080115 0:bf7b9fba3924 384 * EPNum.0..3: Address
frank26080115 0:bf7b9fba3924 385 * EPNum.7: Dir
frank26080115 0:bf7b9fba3924 386 * Return Value: None
frank26080115 0:bf7b9fba3924 387 */
frank26080115 0:bf7b9fba3924 388
frank26080115 0:bf7b9fba3924 389 void USB_ClrStallEP (uint32_t EPNum) {
frank26080115 0:bf7b9fba3924 390 WrCmdDat(CMD_SET_EP_STAT(EPAdr(EPNum)), DAT_WR_BYTE(0));
frank26080115 0:bf7b9fba3924 391 }
frank26080115 0:bf7b9fba3924 392
frank26080115 0:bf7b9fba3924 393
frank26080115 0:bf7b9fba3924 394 /*
frank26080115 0:bf7b9fba3924 395 * Read USB Endpoint Data
frank26080115 0:bf7b9fba3924 396 * Parameters: EPNum: Endpoint Number
frank26080115 0:bf7b9fba3924 397 * EPNum.0..3: Address
frank26080115 0:bf7b9fba3924 398 * EPNum.7: Dir
frank26080115 0:bf7b9fba3924 399 * pData: Pointer to Data Buffer
frank26080115 0:bf7b9fba3924 400 * Return Value: Number of bytes read
frank26080115 0:bf7b9fba3924 401 */
frank26080115 0:bf7b9fba3924 402
frank26080115 0:bf7b9fba3924 403 uint32_t USB_ReadEP (uint32_t EPNum, uint8_t *pData) {
frank26080115 0:bf7b9fba3924 404 uint32_t cnt, n;
frank26080115 0:bf7b9fba3924 405
frank26080115 0:bf7b9fba3924 406 LPC_USB->USBCtrl = ((EPNum & 0x0F) << 2) | CTRL_RD_EN;
frank26080115 0:bf7b9fba3924 407
frank26080115 0:bf7b9fba3924 408 do {
frank26080115 0:bf7b9fba3924 409 cnt = LPC_USB->USBRxPLen;
frank26080115 0:bf7b9fba3924 410 } while ((cnt & PKT_RDY) == 0);
frank26080115 0:bf7b9fba3924 411 cnt &= PKT_LNGTH_MASK;
frank26080115 0:bf7b9fba3924 412
frank26080115 0:bf7b9fba3924 413 for (n = 0; n < (cnt + 3) / 4; n++) {
frank26080115 0:bf7b9fba3924 414 *((__packed uint32_t *)pData) = LPC_USB->USBRxData;
frank26080115 0:bf7b9fba3924 415 pData += 4;
frank26080115 0:bf7b9fba3924 416 }
frank26080115 0:bf7b9fba3924 417 LPC_USB->USBCtrl = 0;
frank26080115 0:bf7b9fba3924 418
frank26080115 0:bf7b9fba3924 419 if (((EP_MSK_ISO >> EPNum) & 1) == 0) { /* Non-Isochronous Endpoint */
frank26080115 0:bf7b9fba3924 420 WrCmd(CMD_SEL_EP(EPAdr(EPNum)));
frank26080115 0:bf7b9fba3924 421 WrCmd(CMD_CLR_BUF);
frank26080115 0:bf7b9fba3924 422 }
frank26080115 0:bf7b9fba3924 423 return (cnt);
frank26080115 0:bf7b9fba3924 424 }
frank26080115 0:bf7b9fba3924 425
frank26080115 0:bf7b9fba3924 426
frank26080115 0:bf7b9fba3924 427 /*
frank26080115 0:bf7b9fba3924 428 * Write USB Endpoint Data
frank26080115 0:bf7b9fba3924 429 * Parameters: EPNum: Endpoint Number
frank26080115 0:bf7b9fba3924 430 * EPNum.0..3: Address
frank26080115 0:bf7b9fba3924 431 * EPNum.7: Dir
frank26080115 0:bf7b9fba3924 432 * pData: Pointer to Data Buffer
frank26080115 0:bf7b9fba3924 433 * cnt: Number of bytes to write
frank26080115 0:bf7b9fba3924 434 * Return Value: Number of bytes written
frank26080115 0:bf7b9fba3924 435 */
frank26080115 0:bf7b9fba3924 436
frank26080115 0:bf7b9fba3924 437 uint32_t USB_WriteEP (uint32_t EPNum, uint8_t *pData, uint32_t cnt) {
frank26080115 0:bf7b9fba3924 438 uint32_t n;
frank26080115 0:bf7b9fba3924 439
frank26080115 0:bf7b9fba3924 440 LPC_USB->USBCtrl = ((EPNum & 0x0F) << 2) | CTRL_WR_EN;
frank26080115 0:bf7b9fba3924 441
frank26080115 0:bf7b9fba3924 442 LPC_USB->USBTxPLen = cnt;
frank26080115 0:bf7b9fba3924 443
frank26080115 0:bf7b9fba3924 444 for (n = 0; n < (cnt + 3) / 4; n++) {
frank26080115 0:bf7b9fba3924 445 LPC_USB->USBTxData = *((__packed uint32_t *)pData);
frank26080115 0:bf7b9fba3924 446 pData += 4;
frank26080115 0:bf7b9fba3924 447 }
frank26080115 0:bf7b9fba3924 448 LPC_USB->USBCtrl = 0;
frank26080115 0:bf7b9fba3924 449
frank26080115 0:bf7b9fba3924 450 WrCmd(CMD_SEL_EP(EPAdr(EPNum)));
frank26080115 0:bf7b9fba3924 451 WrCmd(CMD_VALID_BUF);
frank26080115 0:bf7b9fba3924 452 return (cnt);
frank26080115 0:bf7b9fba3924 453 }
frank26080115 0:bf7b9fba3924 454
frank26080115 0:bf7b9fba3924 455
frank26080115 0:bf7b9fba3924 456 #if USB_DMA
frank26080115 0:bf7b9fba3924 457
frank26080115 0:bf7b9fba3924 458
frank26080115 0:bf7b9fba3924 459 /* DMA Descriptor Memory Layout */
frank26080115 0:bf7b9fba3924 460 const uint32_t DDAdr[2] = { DD_NISO_ADR, DD_ISO_ADR };
frank26080115 0:bf7b9fba3924 461 const uint32_t DDSz [2] = { 16, 20 };
frank26080115 0:bf7b9fba3924 462
frank26080115 0:bf7b9fba3924 463
frank26080115 0:bf7b9fba3924 464 /*
frank26080115 0:bf7b9fba3924 465 * Setup USB DMA Transfer for selected Endpoint
frank26080115 0:bf7b9fba3924 466 * Parameters: EPNum: Endpoint Number
frank26080115 0:bf7b9fba3924 467 * pDD: Pointer to DMA Descriptor
frank26080115 0:bf7b9fba3924 468 * Return Value: TRUE - Success, FALSE - Error
frank26080115 0:bf7b9fba3924 469 */
frank26080115 0:bf7b9fba3924 470
frank26080115 0:bf7b9fba3924 471 uint32_t USB_DMA_Setup(uint32_t EPNum, USB_DMA_DESCRIPTOR *pDD) {
frank26080115 0:bf7b9fba3924 472 uint32_t num, ptr, nxt, iso, n;
frank26080115 0:bf7b9fba3924 473 uint32_t *tmp;
frank26080115 0:bf7b9fba3924 474
frank26080115 0:bf7b9fba3924 475 iso = pDD->Cfg.Type.IsoEP; /* Iso or Non-Iso Descriptor */
frank26080115 0:bf7b9fba3924 476 num = EPAdr(EPNum); /* Endpoint's Physical Address */
frank26080115 0:bf7b9fba3924 477
frank26080115 0:bf7b9fba3924 478 ptr = 0; /* Current Descriptor */
frank26080115 0:bf7b9fba3924 479 nxt = udca[num]; /* Initial Descriptor */
frank26080115 0:bf7b9fba3924 480 while (nxt) { /* Go through Descriptor List */
frank26080115 0:bf7b9fba3924 481 ptr = nxt; /* Current Descriptor */
frank26080115 0:bf7b9fba3924 482 if (!pDD->Cfg.Type.Link) { /* Check for Linked Descriptors */
frank26080115 0:bf7b9fba3924 483 n = (ptr - DDAdr[iso]) / DDSz[iso]; /* Descriptor Index */
frank26080115 0:bf7b9fba3924 484 DDMemMap[iso] &= ~(1 << n); /* Unmark Memory Usage */
frank26080115 0:bf7b9fba3924 485 }
frank26080115 0:bf7b9fba3924 486 nxt = *((uint32_t *)ptr); /* Next Descriptor */
frank26080115 0:bf7b9fba3924 487 }
frank26080115 0:bf7b9fba3924 488
frank26080115 0:bf7b9fba3924 489 for (n = 0; n < 32; n++) { /* Search for available Memory */
frank26080115 0:bf7b9fba3924 490 if ((DDMemMap[iso] & (1 << n)) == 0) {
frank26080115 0:bf7b9fba3924 491 break; /* Memory found */
frank26080115 0:bf7b9fba3924 492 }
frank26080115 0:bf7b9fba3924 493 }
frank26080115 0:bf7b9fba3924 494 if (n == 32) return (FALSE); /* Memory not available */
frank26080115 0:bf7b9fba3924 495
frank26080115 0:bf7b9fba3924 496 DDMemMap[iso] |= 1 << n; /* Mark Memory Usage */
frank26080115 0:bf7b9fba3924 497 nxt = DDAdr[iso] + n * DDSz[iso]; /* Next Descriptor */
frank26080115 0:bf7b9fba3924 498
frank26080115 0:bf7b9fba3924 499 if (ptr && pDD->Cfg.Type.Link) {
frank26080115 0:bf7b9fba3924 500 *((uint32_t *)(ptr + 0)) = nxt; /* Link in new Descriptor */
frank26080115 0:bf7b9fba3924 501 *((uint32_t *)(ptr + 4)) |= 0x00000004; /* Next DD is Valid */
frank26080115 0:bf7b9fba3924 502 } else {
frank26080115 0:bf7b9fba3924 503 udca[num] = nxt; /* Save new Descriptor */
frank26080115 0:bf7b9fba3924 504 UDCA[num] = nxt; /* Update UDCA in USB */
frank26080115 0:bf7b9fba3924 505 }
frank26080115 0:bf7b9fba3924 506
frank26080115 0:bf7b9fba3924 507 /* Fill in DMA Descriptor */
frank26080115 0:bf7b9fba3924 508 //*(((uint32_t *)nxt)++) = 0; /* Next DD Pointer */
frank26080115 0:bf7b9fba3924 509 tmp = (uint32_t *)nxt;
frank26080115 0:bf7b9fba3924 510 *tmp++ = 0;
frank26080115 0:bf7b9fba3924 511 // *(((uint32_t *)nxt)++) = pDD->Cfg.Type.ATLE |
frank26080115 0:bf7b9fba3924 512 // (pDD->Cfg.Type.IsoEP << 4) |
frank26080115 0:bf7b9fba3924 513 // (pDD->MaxSize << 5) |
frank26080115 0:bf7b9fba3924 514 // (pDD->BufLen << 16);
frank26080115 0:bf7b9fba3924 515 *tmp++ = pDD->Cfg.Type.ATLE |
frank26080115 0:bf7b9fba3924 516 (pDD->Cfg.Type.IsoEP << 4) |
frank26080115 0:bf7b9fba3924 517 (pDD->MaxSize << 5) |
frank26080115 0:bf7b9fba3924 518 (pDD->BufLen << 16);
frank26080115 0:bf7b9fba3924 519 // *(((uint32_t *)nxt)++) = pDD->BufAdr;
frank26080115 0:bf7b9fba3924 520 *tmp++ = pDD->BufAdr;
frank26080115 0:bf7b9fba3924 521 // *(((uint32_t *)nxt)++) = pDD->Cfg.Type.LenPos << 8;
frank26080115 0:bf7b9fba3924 522 *tmp++ = pDD->Cfg.Type.LenPos << 8;
frank26080115 0:bf7b9fba3924 523 if (iso) {
frank26080115 0:bf7b9fba3924 524 // *((uint32_t *)nxt) = pDD->InfoAdr;
frank26080115 0:bf7b9fba3924 525 *tmp = pDD->InfoAdr;
frank26080115 0:bf7b9fba3924 526 }
frank26080115 0:bf7b9fba3924 527
frank26080115 0:bf7b9fba3924 528 return (TRUE); /* Success */
frank26080115 0:bf7b9fba3924 529 }
frank26080115 0:bf7b9fba3924 530
frank26080115 0:bf7b9fba3924 531
frank26080115 0:bf7b9fba3924 532 /*
frank26080115 0:bf7b9fba3924 533 * Enable USB DMA Endpoint
frank26080115 0:bf7b9fba3924 534 * Parameters: EPNum: Endpoint Number
frank26080115 0:bf7b9fba3924 535 * EPNum.0..3: Address
frank26080115 0:bf7b9fba3924 536 * EPNum.7: Dir
frank26080115 0:bf7b9fba3924 537 * Return Value: None
frank26080115 0:bf7b9fba3924 538 */
frank26080115 0:bf7b9fba3924 539
frank26080115 0:bf7b9fba3924 540 void USB_DMA_Enable (uint32_t EPNum) {
frank26080115 0:bf7b9fba3924 541 LPC_USB->USBEpDMAEn = 1 << EPAdr(EPNum);
frank26080115 0:bf7b9fba3924 542 }
frank26080115 0:bf7b9fba3924 543
frank26080115 0:bf7b9fba3924 544
frank26080115 0:bf7b9fba3924 545 /*
frank26080115 0:bf7b9fba3924 546 * Disable USB DMA Endpoint
frank26080115 0:bf7b9fba3924 547 * Parameters: EPNum: Endpoint Number
frank26080115 0:bf7b9fba3924 548 * EPNum.0..3: Address
frank26080115 0:bf7b9fba3924 549 * EPNum.7: Dir
frank26080115 0:bf7b9fba3924 550 * Return Value: None
frank26080115 0:bf7b9fba3924 551 */
frank26080115 0:bf7b9fba3924 552
frank26080115 0:bf7b9fba3924 553 void USB_DMA_Disable (uint32_t EPNum) {
frank26080115 0:bf7b9fba3924 554 LPC_USB->USBEpDMADis = 1 << EPAdr(EPNum);
frank26080115 0:bf7b9fba3924 555 }
frank26080115 0:bf7b9fba3924 556
frank26080115 0:bf7b9fba3924 557
frank26080115 0:bf7b9fba3924 558 /*
frank26080115 0:bf7b9fba3924 559 * Get USB DMA Endpoint Status
frank26080115 0:bf7b9fba3924 560 * Parameters: EPNum: Endpoint Number
frank26080115 0:bf7b9fba3924 561 * EPNum.0..3: Address
frank26080115 0:bf7b9fba3924 562 * EPNum.7: Dir
frank26080115 0:bf7b9fba3924 563 * Return Value: DMA Status
frank26080115 0:bf7b9fba3924 564 */
frank26080115 0:bf7b9fba3924 565
frank26080115 0:bf7b9fba3924 566 uint32_t USB_DMA_Status (uint32_t EPNum) {
frank26080115 0:bf7b9fba3924 567 uint32_t ptr, val;
frank26080115 0:bf7b9fba3924 568
frank26080115 0:bf7b9fba3924 569 ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */
frank26080115 0:bf7b9fba3924 570 if (ptr == 0) return (USB_DMA_INVALID);
frank26080115 0:bf7b9fba3924 571
frank26080115 0:bf7b9fba3924 572 val = *((uint32_t *)(ptr + 3*4)); /* Status Information */
frank26080115 0:bf7b9fba3924 573 switch ((val >> 1) & 0x0F) {
frank26080115 0:bf7b9fba3924 574 case 0x00: /* Not serviced */
frank26080115 0:bf7b9fba3924 575 return (USB_DMA_IDLE);
frank26080115 0:bf7b9fba3924 576 case 0x01: /* Being serviced */
frank26080115 0:bf7b9fba3924 577 return (USB_DMA_BUSY);
frank26080115 0:bf7b9fba3924 578 case 0x02: /* Normal Completition */
frank26080115 0:bf7b9fba3924 579 return (USB_DMA_DONE);
frank26080115 0:bf7b9fba3924 580 case 0x03: /* Data Under Run */
frank26080115 0:bf7b9fba3924 581 return (USB_DMA_UNDER_RUN);
frank26080115 0:bf7b9fba3924 582 case 0x08: /* Data Over Run */
frank26080115 0:bf7b9fba3924 583 return (USB_DMA_OVER_RUN);
frank26080115 0:bf7b9fba3924 584 case 0x09: /* System Error */
frank26080115 0:bf7b9fba3924 585 return (USB_DMA_ERROR);
frank26080115 0:bf7b9fba3924 586 }
frank26080115 0:bf7b9fba3924 587 return (USB_DMA_UNKNOWN);
frank26080115 0:bf7b9fba3924 588 }
frank26080115 0:bf7b9fba3924 589
frank26080115 0:bf7b9fba3924 590
frank26080115 0:bf7b9fba3924 591 /*
frank26080115 0:bf7b9fba3924 592 * Get USB DMA Endpoint Current Buffer Address
frank26080115 0:bf7b9fba3924 593 * Parameters: EPNum: Endpoint Number
frank26080115 0:bf7b9fba3924 594 * EPNum.0..3: Address
frank26080115 0:bf7b9fba3924 595 * EPNum.7: Dir
frank26080115 0:bf7b9fba3924 596 * Return Value: DMA Address (or -1 when DMA is Invalid)
frank26080115 0:bf7b9fba3924 597 */
frank26080115 0:bf7b9fba3924 598
frank26080115 0:bf7b9fba3924 599 uint32_t USB_DMA_BufAdr (uint32_t EPNum) {
frank26080115 0:bf7b9fba3924 600 uint32_t ptr, val;
frank26080115 0:bf7b9fba3924 601
frank26080115 0:bf7b9fba3924 602 ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */
frank26080115 0:bf7b9fba3924 603 if (ptr == 0)
frank26080115 0:bf7b9fba3924 604 {
frank26080115 0:bf7b9fba3924 605 return ((uint32_t)(-1)); /* DMA Invalid */
frank26080115 0:bf7b9fba3924 606 }
frank26080115 0:bf7b9fba3924 607
frank26080115 0:bf7b9fba3924 608 val = *((uint32_t *)(ptr + 2*4)); /* Buffer Address */
frank26080115 0:bf7b9fba3924 609 return (val); /* Current Address */
frank26080115 0:bf7b9fba3924 610 }
frank26080115 0:bf7b9fba3924 611
frank26080115 0:bf7b9fba3924 612
frank26080115 0:bf7b9fba3924 613 /*
frank26080115 0:bf7b9fba3924 614 * Get USB DMA Endpoint Current Buffer Count
frank26080115 0:bf7b9fba3924 615 * Number of transfered Bytes or Iso Packets
frank26080115 0:bf7b9fba3924 616 * Parameters: EPNum: Endpoint Number
frank26080115 0:bf7b9fba3924 617 * EPNum.0..3: Address
frank26080115 0:bf7b9fba3924 618 * EPNum.7: Dir
frank26080115 0:bf7b9fba3924 619 * Return Value: DMA Count (or -1 when DMA is Invalid)
frank26080115 0:bf7b9fba3924 620 */
frank26080115 0:bf7b9fba3924 621
frank26080115 0:bf7b9fba3924 622 uint32_t USB_DMA_BufCnt (uint32_t EPNum) {
frank26080115 0:bf7b9fba3924 623 uint32_t ptr, val;
frank26080115 0:bf7b9fba3924 624
frank26080115 0:bf7b9fba3924 625 ptr = UDCA[EPAdr(EPNum)]; /* Current Descriptor */
frank26080115 0:bf7b9fba3924 626 if (ptr == 0)
frank26080115 0:bf7b9fba3924 627 {
frank26080115 0:bf7b9fba3924 628 return ((uint32_t)(-1)); /* DMA Invalid */
frank26080115 0:bf7b9fba3924 629 }
frank26080115 0:bf7b9fba3924 630 val = *((uint32_t *)(ptr + 3*4)); /* Status Information */
frank26080115 0:bf7b9fba3924 631 return (val >> 16); /* Current Count */
frank26080115 0:bf7b9fba3924 632 }
frank26080115 0:bf7b9fba3924 633
frank26080115 0:bf7b9fba3924 634
frank26080115 0:bf7b9fba3924 635 #endif /* USB_DMA */
frank26080115 0:bf7b9fba3924 636
frank26080115 0:bf7b9fba3924 637
frank26080115 0:bf7b9fba3924 638 /*
frank26080115 0:bf7b9fba3924 639 * Get USB Last Frame Number
frank26080115 0:bf7b9fba3924 640 * Parameters: None
frank26080115 0:bf7b9fba3924 641 * Return Value: Frame Number
frank26080115 0:bf7b9fba3924 642 */
frank26080115 0:bf7b9fba3924 643
frank26080115 0:bf7b9fba3924 644 uint32_t USB_GetFrame (void) {
frank26080115 0:bf7b9fba3924 645 uint32_t val;
frank26080115 0:bf7b9fba3924 646
frank26080115 0:bf7b9fba3924 647 WrCmd(CMD_RD_FRAME);
frank26080115 0:bf7b9fba3924 648 val = RdCmdDat(DAT_RD_FRAME);
frank26080115 0:bf7b9fba3924 649 val = val | (RdCmdDat(DAT_RD_FRAME) << 8);
frank26080115 0:bf7b9fba3924 650 return (val);
frank26080115 0:bf7b9fba3924 651 }
frank26080115 0:bf7b9fba3924 652
frank26080115 0:bf7b9fba3924 653
frank26080115 0:bf7b9fba3924 654 /*
frank26080115 0:bf7b9fba3924 655 * USB Interrupt Service Routine
frank26080115 0:bf7b9fba3924 656 */
frank26080115 0:bf7b9fba3924 657
frank26080115 0:bf7b9fba3924 658 void USB_IRQHandler (void) {
frank26080115 0:bf7b9fba3924 659 uint32_t disr, val, n, m;
frank26080115 0:bf7b9fba3924 660
frank26080115 0:bf7b9fba3924 661 disr = LPC_USB->USBDevIntSt; /* Device Interrupt Status */
frank26080115 0:bf7b9fba3924 662 LPC_USB->USBDevIntClr = disr;
frank26080115 0:bf7b9fba3924 663
frank26080115 0:bf7b9fba3924 664 /* Device Status Interrupt (Reset, Connect change, Suspend/Resume) */
frank26080115 0:bf7b9fba3924 665 if (disr & DEV_STAT_INT) {
frank26080115 0:bf7b9fba3924 666 WrCmd(CMD_GET_DEV_STAT);
frank26080115 0:bf7b9fba3924 667 val = RdCmdDat(DAT_GET_DEV_STAT); /* Device Status */
frank26080115 0:bf7b9fba3924 668 if (val & DEV_RST) { /* Reset */
frank26080115 0:bf7b9fba3924 669 USB_Reset();
frank26080115 0:bf7b9fba3924 670 #if USB_RESET_EVENT
frank26080115 0:bf7b9fba3924 671 USB_Reset_Event();
frank26080115 0:bf7b9fba3924 672 #endif
frank26080115 0:bf7b9fba3924 673 }
frank26080115 0:bf7b9fba3924 674 if (val & DEV_CON_CH) { /* Connect change */
frank26080115 0:bf7b9fba3924 675 #if USB_POWER_EVENT
frank26080115 0:bf7b9fba3924 676 USB_Power_Event(val & DEV_CON);
frank26080115 0:bf7b9fba3924 677 #endif
frank26080115 0:bf7b9fba3924 678 }
frank26080115 0:bf7b9fba3924 679 if (val & DEV_SUS_CH) { /* Suspend/Resume */
frank26080115 0:bf7b9fba3924 680 if (val & DEV_SUS) { /* Suspend */
frank26080115 0:bf7b9fba3924 681 USB_Suspend();
frank26080115 0:bf7b9fba3924 682 #if USB_SUSPEND_EVENT
frank26080115 0:bf7b9fba3924 683 USB_Suspend_Event();
frank26080115 0:bf7b9fba3924 684 #endif
frank26080115 0:bf7b9fba3924 685 } else { /* Resume */
frank26080115 0:bf7b9fba3924 686 USB_Resume();
frank26080115 0:bf7b9fba3924 687 #if USB_RESUME_EVENT
frank26080115 0:bf7b9fba3924 688 USB_Resume_Event();
frank26080115 0:bf7b9fba3924 689 #endif
frank26080115 0:bf7b9fba3924 690 }
frank26080115 0:bf7b9fba3924 691 }
frank26080115 0:bf7b9fba3924 692 goto isr_end;
frank26080115 0:bf7b9fba3924 693 }
frank26080115 0:bf7b9fba3924 694
frank26080115 0:bf7b9fba3924 695 #if USB_SOF_EVENT
frank26080115 0:bf7b9fba3924 696 /* Start of Frame Interrupt */
frank26080115 0:bf7b9fba3924 697 if (disr & FRAME_INT) {
frank26080115 0:bf7b9fba3924 698 USB_SOF_Event();
frank26080115 0:bf7b9fba3924 699 }
frank26080115 0:bf7b9fba3924 700 #endif
frank26080115 0:bf7b9fba3924 701
frank26080115 0:bf7b9fba3924 702 #if USB_ERROR_EVENT
frank26080115 0:bf7b9fba3924 703 /* Error Interrupt */
frank26080115 0:bf7b9fba3924 704 if (disr & ERR_INT) {
frank26080115 0:bf7b9fba3924 705 WrCmd(CMD_RD_ERR_STAT);
frank26080115 0:bf7b9fba3924 706 val = RdCmdDat(DAT_RD_ERR_STAT);
frank26080115 0:bf7b9fba3924 707 USB_Error_Event(val);
frank26080115 0:bf7b9fba3924 708 }
frank26080115 0:bf7b9fba3924 709 #endif
frank26080115 0:bf7b9fba3924 710
frank26080115 0:bf7b9fba3924 711 /* Endpoint's Slow Interrupt */
frank26080115 0:bf7b9fba3924 712 if (disr & EP_SLOW_INT) {
frank26080115 0:bf7b9fba3924 713 while (LPC_USB->USBEpIntSt) { /* Endpoint Interrupt Status */
frank26080115 0:bf7b9fba3924 714
frank26080115 0:bf7b9fba3924 715 for (n = 0; n < USB_EP_NUM; n++) { /* Check All Endpoints */
frank26080115 0:bf7b9fba3924 716 if (LPC_USB->USBEpIntSt & (1 << n)) {
frank26080115 0:bf7b9fba3924 717 m = n >> 1;
frank26080115 0:bf7b9fba3924 718 LPC_USB->USBEpIntClr = 1 << n;
frank26080115 0:bf7b9fba3924 719 while ((LPC_USB->USBDevIntSt & CDFULL_INT) == 0);
frank26080115 0:bf7b9fba3924 720 val = LPC_USB->USBCmdData;
frank26080115 0:bf7b9fba3924 721
frank26080115 0:bf7b9fba3924 722 if ((n & 1) == 0) { /* OUT Endpoint */
frank26080115 0:bf7b9fba3924 723 if (n == 0) { /* Control OUT Endpoint */
frank26080115 0:bf7b9fba3924 724 if (val & EP_SEL_STP) { /* Setup Packet */
frank26080115 0:bf7b9fba3924 725 if (USB_P_EP[0]) {
frank26080115 0:bf7b9fba3924 726 USB_P_EP[0](USB_EVT_SETUP);
frank26080115 0:bf7b9fba3924 727 continue;
frank26080115 0:bf7b9fba3924 728 }
frank26080115 0:bf7b9fba3924 729 }
frank26080115 0:bf7b9fba3924 730 }
frank26080115 0:bf7b9fba3924 731 if (USB_P_EP[m]) {
frank26080115 0:bf7b9fba3924 732 USB_P_EP[m](USB_EVT_OUT);
frank26080115 0:bf7b9fba3924 733 }
frank26080115 0:bf7b9fba3924 734 } else { /* IN Endpoint */
frank26080115 0:bf7b9fba3924 735 //if (USB_P_EP[m]) {
frank26080115 0:bf7b9fba3924 736 //USB_P_EP[m](USB_EVT_IN);
frank26080115 0:bf7b9fba3924 737 if(m==0)
frank26080115 0:bf7b9fba3924 738 USB_EndPoint0(USB_EVT_IN);
frank26080115 0:bf7b9fba3924 739 if(m==1)
frank26080115 0:bf7b9fba3924 740 USB_EndPoint1(USB_EVT_IN);
frank26080115 0:bf7b9fba3924 741 if(m==2)
frank26080115 0:bf7b9fba3924 742 USB_EndPoint2(USB_EVT_IN);
frank26080115 0:bf7b9fba3924 743 /* ............. */
frank26080115 0:bf7b9fba3924 744 //}
frank26080115 0:bf7b9fba3924 745 }
frank26080115 0:bf7b9fba3924 746 }
frank26080115 0:bf7b9fba3924 747 }
frank26080115 0:bf7b9fba3924 748 }
frank26080115 0:bf7b9fba3924 749 }
frank26080115 0:bf7b9fba3924 750
frank26080115 0:bf7b9fba3924 751 #if USB_DMA
frank26080115 0:bf7b9fba3924 752 if (LPC_USB->USBDMAIntSt & 0x00000001) { /* End of Transfer Interrupt */
frank26080115 0:bf7b9fba3924 753 val = LPC_USB->USBEoTIntSt;
frank26080115 0:bf7b9fba3924 754 for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
frank26080115 0:bf7b9fba3924 755 if (val & (1 << n)) {
frank26080115 0:bf7b9fba3924 756 m = n >> 1;
frank26080115 0:bf7b9fba3924 757 if ((n & 1) == 0) { /* OUT Endpoint */
frank26080115 0:bf7b9fba3924 758 if (USB_P_EP[m]) {
frank26080115 0:bf7b9fba3924 759 USB_P_EP[m](USB_EVT_OUT_DMA_EOT);
frank26080115 0:bf7b9fba3924 760 }
frank26080115 0:bf7b9fba3924 761 } else { /* IN Endpoint */
frank26080115 0:bf7b9fba3924 762 if (USB_P_EP[m]) {
frank26080115 0:bf7b9fba3924 763 USB_P_EP[m](USB_EVT_IN_DMA_EOT);
frank26080115 0:bf7b9fba3924 764 }
frank26080115 0:bf7b9fba3924 765 }
frank26080115 0:bf7b9fba3924 766 }
frank26080115 0:bf7b9fba3924 767 }
frank26080115 0:bf7b9fba3924 768 LPC_USB->USBEoTIntClr = val;
frank26080115 0:bf7b9fba3924 769 }
frank26080115 0:bf7b9fba3924 770
frank26080115 0:bf7b9fba3924 771 if (LPC_USB->USBDMAIntSt & 0x00000002) { /* New DD Request Interrupt */
frank26080115 0:bf7b9fba3924 772 val = LPC_USB->USBNDDRIntSt;
frank26080115 0:bf7b9fba3924 773 for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
frank26080115 0:bf7b9fba3924 774 if (val & (1 << n)) {
frank26080115 0:bf7b9fba3924 775 m = n >> 1;
frank26080115 0:bf7b9fba3924 776 if ((n & 1) == 0) { /* OUT Endpoint */
frank26080115 0:bf7b9fba3924 777 if (USB_P_EP[m]) {
frank26080115 0:bf7b9fba3924 778 USB_P_EP[m](USB_EVT_OUT_DMA_NDR);
frank26080115 0:bf7b9fba3924 779 }
frank26080115 0:bf7b9fba3924 780 } else { /* IN Endpoint */
frank26080115 0:bf7b9fba3924 781 if (USB_P_EP[m]) {
frank26080115 0:bf7b9fba3924 782 USB_P_EP[m](USB_EVT_IN_DMA_NDR);
frank26080115 0:bf7b9fba3924 783 }
frank26080115 0:bf7b9fba3924 784 }
frank26080115 0:bf7b9fba3924 785 }
frank26080115 0:bf7b9fba3924 786 }
frank26080115 0:bf7b9fba3924 787 LPC_USB->USBNDDRIntClr = val;
frank26080115 0:bf7b9fba3924 788 }
frank26080115 0:bf7b9fba3924 789
frank26080115 0:bf7b9fba3924 790 if (LPC_USB->USBDMAIntSt & 0x00000004) { /* System Error Interrupt */
frank26080115 0:bf7b9fba3924 791 val = LPC_USB->USBSysErrIntSt;
frank26080115 0:bf7b9fba3924 792 for (n = 2; n < USB_EP_NUM; n++) { /* Check All Endpoints */
frank26080115 0:bf7b9fba3924 793 if (val & (1 << n)) {
frank26080115 0:bf7b9fba3924 794 m = n >> 1;
frank26080115 0:bf7b9fba3924 795 if ((n & 1) == 0) { /* OUT Endpoint */
frank26080115 0:bf7b9fba3924 796 if (USB_P_EP[m]) {
frank26080115 0:bf7b9fba3924 797 USB_P_EP[m](USB_EVT_OUT_DMA_ERR);
frank26080115 0:bf7b9fba3924 798 }
frank26080115 0:bf7b9fba3924 799 } else { /* IN Endpoint */
frank26080115 0:bf7b9fba3924 800 if (USB_P_EP[m]) {
frank26080115 0:bf7b9fba3924 801 USB_P_EP[m](USB_EVT_IN_DMA_ERR);
frank26080115 0:bf7b9fba3924 802 }
frank26080115 0:bf7b9fba3924 803 }
frank26080115 0:bf7b9fba3924 804 }
frank26080115 0:bf7b9fba3924 805 }
frank26080115 0:bf7b9fba3924 806 LPC_USB->USBSysErrIntClr = val;
frank26080115 0:bf7b9fba3924 807 }
frank26080115 0:bf7b9fba3924 808 #endif /* USB_DMA */
frank26080115 0:bf7b9fba3924 809
frank26080115 0:bf7b9fba3924 810 isr_end:
frank26080115 0:bf7b9fba3924 811 return;
frank26080115 0:bf7b9fba3924 812 }