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