as
Dependencies: C12832 CMPS03 FatFileSystemCpp mbed
USBHostLite/usbhost_lpc17xx.cpp@0:6cf24371ad4f, 2014-05-07 (annotated)
- Committer:
- JWitherstone
- Date:
- Wed May 07 10:15:01 2014 +0000
- Revision:
- 0:6cf24371ad4f
this is compass;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JWitherstone | 0:6cf24371ad4f | 1 | /* |
JWitherstone | 0:6cf24371ad4f | 2 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 3 | * NXP USB Host Stack |
JWitherstone | 0:6cf24371ad4f | 4 | * |
JWitherstone | 0:6cf24371ad4f | 5 | * (c) Copyright 2008, NXP SemiConductors |
JWitherstone | 0:6cf24371ad4f | 6 | * (c) Copyright 2008, OnChip Technologies LLC |
JWitherstone | 0:6cf24371ad4f | 7 | * All Rights Reserved |
JWitherstone | 0:6cf24371ad4f | 8 | * |
JWitherstone | 0:6cf24371ad4f | 9 | * www.nxp.com |
JWitherstone | 0:6cf24371ad4f | 10 | * www.onchiptech.com |
JWitherstone | 0:6cf24371ad4f | 11 | * |
JWitherstone | 0:6cf24371ad4f | 12 | * File : usbhost_lpc17xx.c |
JWitherstone | 0:6cf24371ad4f | 13 | * Programmer(s) : Ravikanth.P |
JWitherstone | 0:6cf24371ad4f | 14 | * Version : |
JWitherstone | 0:6cf24371ad4f | 15 | * |
JWitherstone | 0:6cf24371ad4f | 16 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 17 | */ |
JWitherstone | 0:6cf24371ad4f | 18 | |
JWitherstone | 0:6cf24371ad4f | 19 | /* |
JWitherstone | 0:6cf24371ad4f | 20 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 21 | * INCLUDE HEADER FILES |
JWitherstone | 0:6cf24371ad4f | 22 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 23 | */ |
JWitherstone | 0:6cf24371ad4f | 24 | |
JWitherstone | 0:6cf24371ad4f | 25 | #include "usbhost_lpc17xx.h" |
JWitherstone | 0:6cf24371ad4f | 26 | |
JWitherstone | 0:6cf24371ad4f | 27 | /* |
JWitherstone | 0:6cf24371ad4f | 28 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 29 | * GLOBAL VARIABLES |
JWitherstone | 0:6cf24371ad4f | 30 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 31 | */ |
JWitherstone | 0:6cf24371ad4f | 32 | int gUSBConnected; |
JWitherstone | 0:6cf24371ad4f | 33 | |
JWitherstone | 0:6cf24371ad4f | 34 | volatile USB_INT32U HOST_RhscIntr = 0; /* Root Hub Status Change interrupt */ |
JWitherstone | 0:6cf24371ad4f | 35 | volatile USB_INT32U HOST_WdhIntr = 0; /* Semaphore to wait until the TD is submitted */ |
JWitherstone | 0:6cf24371ad4f | 36 | volatile USB_INT08U HOST_TDControlStatus = 0; |
JWitherstone | 0:6cf24371ad4f | 37 | volatile HCED *EDCtrl; /* Control endpoint descriptor structure */ |
JWitherstone | 0:6cf24371ad4f | 38 | volatile HCED *EDBulkIn; /* BulkIn endpoint descriptor structure */ |
JWitherstone | 0:6cf24371ad4f | 39 | volatile HCED *EDBulkOut; /* BulkOut endpoint descriptor structure */ |
JWitherstone | 0:6cf24371ad4f | 40 | volatile HCTD *TDHead; /* Head transfer descriptor structure */ |
JWitherstone | 0:6cf24371ad4f | 41 | volatile HCTD *TDTail; /* Tail transfer descriptor structure */ |
JWitherstone | 0:6cf24371ad4f | 42 | volatile HCCA *Hcca; /* Host Controller Communications Area structure */ |
JWitherstone | 0:6cf24371ad4f | 43 | USB_INT16U *TDBufNonVol; /* Identical to TDBuffer just to reduce compiler warnings */ |
JWitherstone | 0:6cf24371ad4f | 44 | volatile USB_INT08U *TDBuffer; /* Current Buffer Pointer of transfer descriptor */ |
JWitherstone | 0:6cf24371ad4f | 45 | |
JWitherstone | 0:6cf24371ad4f | 46 | // USB host structures |
JWitherstone | 0:6cf24371ad4f | 47 | // AHB SRAM block 1 |
JWitherstone | 0:6cf24371ad4f | 48 | #define HOSTBASEADDR 0x2007C000 |
JWitherstone | 0:6cf24371ad4f | 49 | // reserve memory for the linker |
JWitherstone | 0:6cf24371ad4f | 50 | static USB_INT08U HostBuf[0x200] __attribute__((at(HOSTBASEADDR))); |
JWitherstone | 0:6cf24371ad4f | 51 | /* |
JWitherstone | 0:6cf24371ad4f | 52 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 53 | * DELAY IN MILLI SECONDS |
JWitherstone | 0:6cf24371ad4f | 54 | * |
JWitherstone | 0:6cf24371ad4f | 55 | * Description: This function provides a delay in milli seconds |
JWitherstone | 0:6cf24371ad4f | 56 | * |
JWitherstone | 0:6cf24371ad4f | 57 | * Arguments : delay The delay required |
JWitherstone | 0:6cf24371ad4f | 58 | * |
JWitherstone | 0:6cf24371ad4f | 59 | * Returns : None |
JWitherstone | 0:6cf24371ad4f | 60 | * |
JWitherstone | 0:6cf24371ad4f | 61 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 62 | */ |
JWitherstone | 0:6cf24371ad4f | 63 | |
JWitherstone | 0:6cf24371ad4f | 64 | void Host_DelayMS (USB_INT32U delay) |
JWitherstone | 0:6cf24371ad4f | 65 | { |
JWitherstone | 0:6cf24371ad4f | 66 | volatile USB_INT32U i; |
JWitherstone | 0:6cf24371ad4f | 67 | |
JWitherstone | 0:6cf24371ad4f | 68 | |
JWitherstone | 0:6cf24371ad4f | 69 | for (i = 0; i < delay; i++) { |
JWitherstone | 0:6cf24371ad4f | 70 | Host_DelayUS(1000); |
JWitherstone | 0:6cf24371ad4f | 71 | } |
JWitherstone | 0:6cf24371ad4f | 72 | } |
JWitherstone | 0:6cf24371ad4f | 73 | |
JWitherstone | 0:6cf24371ad4f | 74 | /* |
JWitherstone | 0:6cf24371ad4f | 75 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 76 | * DELAY IN MICRO SECONDS |
JWitherstone | 0:6cf24371ad4f | 77 | * |
JWitherstone | 0:6cf24371ad4f | 78 | * Description: This function provides a delay in micro seconds |
JWitherstone | 0:6cf24371ad4f | 79 | * |
JWitherstone | 0:6cf24371ad4f | 80 | * Arguments : delay The delay required |
JWitherstone | 0:6cf24371ad4f | 81 | * |
JWitherstone | 0:6cf24371ad4f | 82 | * Returns : None |
JWitherstone | 0:6cf24371ad4f | 83 | * |
JWitherstone | 0:6cf24371ad4f | 84 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 85 | */ |
JWitherstone | 0:6cf24371ad4f | 86 | |
JWitherstone | 0:6cf24371ad4f | 87 | void Host_DelayUS (USB_INT32U delay) |
JWitherstone | 0:6cf24371ad4f | 88 | { |
JWitherstone | 0:6cf24371ad4f | 89 | volatile USB_INT32U i; |
JWitherstone | 0:6cf24371ad4f | 90 | |
JWitherstone | 0:6cf24371ad4f | 91 | |
JWitherstone | 0:6cf24371ad4f | 92 | for (i = 0; i < (4 * delay); i++) { /* This logic was tested. It gives app. 1 micro sec delay */ |
JWitherstone | 0:6cf24371ad4f | 93 | ; |
JWitherstone | 0:6cf24371ad4f | 94 | } |
JWitherstone | 0:6cf24371ad4f | 95 | } |
JWitherstone | 0:6cf24371ad4f | 96 | |
JWitherstone | 0:6cf24371ad4f | 97 | // bits of the USB/OTG clock control register |
JWitherstone | 0:6cf24371ad4f | 98 | #define HOST_CLK_EN (1<<0) |
JWitherstone | 0:6cf24371ad4f | 99 | #define DEV_CLK_EN (1<<1) |
JWitherstone | 0:6cf24371ad4f | 100 | #define PORTSEL_CLK_EN (1<<3) |
JWitherstone | 0:6cf24371ad4f | 101 | #define AHB_CLK_EN (1<<4) |
JWitherstone | 0:6cf24371ad4f | 102 | |
JWitherstone | 0:6cf24371ad4f | 103 | // bits of the USB/OTG clock status register |
JWitherstone | 0:6cf24371ad4f | 104 | #define HOST_CLK_ON (1<<0) |
JWitherstone | 0:6cf24371ad4f | 105 | #define DEV_CLK_ON (1<<1) |
JWitherstone | 0:6cf24371ad4f | 106 | #define PORTSEL_CLK_ON (1<<3) |
JWitherstone | 0:6cf24371ad4f | 107 | #define AHB_CLK_ON (1<<4) |
JWitherstone | 0:6cf24371ad4f | 108 | |
JWitherstone | 0:6cf24371ad4f | 109 | // we need host clock, OTG/portsel clock and AHB clock |
JWitherstone | 0:6cf24371ad4f | 110 | #define CLOCK_MASK (HOST_CLK_EN | PORTSEL_CLK_EN | AHB_CLK_EN) |
JWitherstone | 0:6cf24371ad4f | 111 | |
JWitherstone | 0:6cf24371ad4f | 112 | /* |
JWitherstone | 0:6cf24371ad4f | 113 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 114 | * INITIALIZE THE HOST CONTROLLER |
JWitherstone | 0:6cf24371ad4f | 115 | * |
JWitherstone | 0:6cf24371ad4f | 116 | * Description: This function initializes lpc17xx host controller |
JWitherstone | 0:6cf24371ad4f | 117 | * |
JWitherstone | 0:6cf24371ad4f | 118 | * Arguments : None |
JWitherstone | 0:6cf24371ad4f | 119 | * |
JWitherstone | 0:6cf24371ad4f | 120 | * Returns : |
JWitherstone | 0:6cf24371ad4f | 121 | * |
JWitherstone | 0:6cf24371ad4f | 122 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 123 | */ |
JWitherstone | 0:6cf24371ad4f | 124 | void Host_Init (void) |
JWitherstone | 0:6cf24371ad4f | 125 | { |
JWitherstone | 0:6cf24371ad4f | 126 | PRINT_Log("In Host_Init\n"); |
JWitherstone | 0:6cf24371ad4f | 127 | NVIC_DisableIRQ(USB_IRQn); /* Disable the USB interrupt source */ |
JWitherstone | 0:6cf24371ad4f | 128 | |
JWitherstone | 0:6cf24371ad4f | 129 | // turn on power for USB |
JWitherstone | 0:6cf24371ad4f | 130 | LPC_SC->PCONP |= (1UL<<31); |
JWitherstone | 0:6cf24371ad4f | 131 | // Enable USB host clock, port selection and AHB clock |
JWitherstone | 0:6cf24371ad4f | 132 | LPC_USB->USBClkCtrl |= CLOCK_MASK; |
JWitherstone | 0:6cf24371ad4f | 133 | // Wait for clocks to become available |
JWitherstone | 0:6cf24371ad4f | 134 | while ((LPC_USB->USBClkSt & CLOCK_MASK) != CLOCK_MASK) |
JWitherstone | 0:6cf24371ad4f | 135 | ; |
JWitherstone | 0:6cf24371ad4f | 136 | |
JWitherstone | 0:6cf24371ad4f | 137 | // it seems the bits[0:1] mean the following |
JWitherstone | 0:6cf24371ad4f | 138 | // 0: U1=device, U2=host |
JWitherstone | 0:6cf24371ad4f | 139 | // 1: U1=host, U2=host |
JWitherstone | 0:6cf24371ad4f | 140 | // 2: reserved |
JWitherstone | 0:6cf24371ad4f | 141 | // 3: U1=host, U2=device |
JWitherstone | 0:6cf24371ad4f | 142 | // NB: this register is only available if OTG clock (aka "port select") is enabled!! |
JWitherstone | 0:6cf24371ad4f | 143 | // since we don't care about port 2, set just bit 0 to 1 (U1=host) |
JWitherstone | 0:6cf24371ad4f | 144 | LPC_USB->OTGStCtrl |= 1; |
JWitherstone | 0:6cf24371ad4f | 145 | |
JWitherstone | 0:6cf24371ad4f | 146 | // now that we've configured the ports, we can turn off the portsel clock |
JWitherstone | 0:6cf24371ad4f | 147 | LPC_USB->USBClkCtrl &= ~PORTSEL_CLK_EN; |
JWitherstone | 0:6cf24371ad4f | 148 | |
JWitherstone | 0:6cf24371ad4f | 149 | // power pins are not connected on mbed, so we can skip them |
JWitherstone | 0:6cf24371ad4f | 150 | /* P1[18] = USB_UP_LED, 01 */ |
JWitherstone | 0:6cf24371ad4f | 151 | /* P1[19] = /USB_PPWR, 10 */ |
JWitherstone | 0:6cf24371ad4f | 152 | /* P1[22] = USB_PWRD, 10 */ |
JWitherstone | 0:6cf24371ad4f | 153 | /* P1[27] = /USB_OVRCR, 10 */ |
JWitherstone | 0:6cf24371ad4f | 154 | /*LPC_PINCON->PINSEL3 &= ~((3<<4) | (3<<6) | (3<<12) | (3<<22)); |
JWitherstone | 0:6cf24371ad4f | 155 | LPC_PINCON->PINSEL3 |= ((1<<4)|(2<<6) | (2<<12) | (2<<22)); // 0x00802080 |
JWitherstone | 0:6cf24371ad4f | 156 | */ |
JWitherstone | 0:6cf24371ad4f | 157 | |
JWitherstone | 0:6cf24371ad4f | 158 | // configure USB D+/D- pins |
JWitherstone | 0:6cf24371ad4f | 159 | /* P0[29] = USB_D+, 01 */ |
JWitherstone | 0:6cf24371ad4f | 160 | /* P0[30] = USB_D-, 01 */ |
JWitherstone | 0:6cf24371ad4f | 161 | LPC_PINCON->PINSEL1 &= ~((3<<26) | (3<<28)); |
JWitherstone | 0:6cf24371ad4f | 162 | LPC_PINCON->PINSEL1 |= ((1<<26)|(1<<28)); // 0x14000000 |
JWitherstone | 0:6cf24371ad4f | 163 | |
JWitherstone | 0:6cf24371ad4f | 164 | PRINT_Log("Initializing Host Stack\n"); |
JWitherstone | 0:6cf24371ad4f | 165 | |
JWitherstone | 0:6cf24371ad4f | 166 | Hcca = (volatile HCCA *)(HostBuf+0x000); |
JWitherstone | 0:6cf24371ad4f | 167 | TDHead = (volatile HCTD *)(HostBuf+0x100); |
JWitherstone | 0:6cf24371ad4f | 168 | TDTail = (volatile HCTD *)(HostBuf+0x110); |
JWitherstone | 0:6cf24371ad4f | 169 | EDCtrl = (volatile HCED *)(HostBuf+0x120); |
JWitherstone | 0:6cf24371ad4f | 170 | EDBulkIn = (volatile HCED *)(HostBuf+0x130); |
JWitherstone | 0:6cf24371ad4f | 171 | EDBulkOut = (volatile HCED *)(HostBuf+0x140); |
JWitherstone | 0:6cf24371ad4f | 172 | TDBuffer = (volatile USB_INT08U *)(HostBuf+0x150); |
JWitherstone | 0:6cf24371ad4f | 173 | |
JWitherstone | 0:6cf24371ad4f | 174 | /* Initialize all the TDs, EDs and HCCA to 0 */ |
JWitherstone | 0:6cf24371ad4f | 175 | Host_EDInit(EDCtrl); |
JWitherstone | 0:6cf24371ad4f | 176 | Host_EDInit(EDBulkIn); |
JWitherstone | 0:6cf24371ad4f | 177 | Host_EDInit(EDBulkOut); |
JWitherstone | 0:6cf24371ad4f | 178 | Host_TDInit(TDHead); |
JWitherstone | 0:6cf24371ad4f | 179 | Host_TDInit(TDTail); |
JWitherstone | 0:6cf24371ad4f | 180 | Host_HCCAInit(Hcca); |
JWitherstone | 0:6cf24371ad4f | 181 | |
JWitherstone | 0:6cf24371ad4f | 182 | Host_DelayMS(50); /* Wait 50 ms before apply reset */ |
JWitherstone | 0:6cf24371ad4f | 183 | LPC_USB->HcControl = 0; /* HARDWARE RESET */ |
JWitherstone | 0:6cf24371ad4f | 184 | LPC_USB->HcControlHeadED = 0; /* Initialize Control list head to Zero */ |
JWitherstone | 0:6cf24371ad4f | 185 | LPC_USB->HcBulkHeadED = 0; /* Initialize Bulk list head to Zero */ |
JWitherstone | 0:6cf24371ad4f | 186 | |
JWitherstone | 0:6cf24371ad4f | 187 | /* SOFTWARE RESET */ |
JWitherstone | 0:6cf24371ad4f | 188 | LPC_USB->HcCommandStatus = OR_CMD_STATUS_HCR; |
JWitherstone | 0:6cf24371ad4f | 189 | LPC_USB->HcFmInterval = DEFAULT_FMINTERVAL; /* Write Fm Interval and Largest Data Packet Counter */ |
JWitherstone | 0:6cf24371ad4f | 190 | |
JWitherstone | 0:6cf24371ad4f | 191 | /* Put HC in operational state */ |
JWitherstone | 0:6cf24371ad4f | 192 | LPC_USB->HcControl = (LPC_USB->HcControl & (~OR_CONTROL_HCFS)) | OR_CONTROL_HC_OPER; |
JWitherstone | 0:6cf24371ad4f | 193 | LPC_USB->HcRhStatus = OR_RH_STATUS_LPSC; /* Set Global Power */ |
JWitherstone | 0:6cf24371ad4f | 194 | |
JWitherstone | 0:6cf24371ad4f | 195 | LPC_USB->HcHCCA = (USB_INT32U)Hcca; |
JWitherstone | 0:6cf24371ad4f | 196 | LPC_USB->HcInterruptStatus |= LPC_USB->HcInterruptStatus; /* Clear Interrrupt Status */ |
JWitherstone | 0:6cf24371ad4f | 197 | |
JWitherstone | 0:6cf24371ad4f | 198 | |
JWitherstone | 0:6cf24371ad4f | 199 | LPC_USB->HcInterruptEnable = OR_INTR_ENABLE_MIE | |
JWitherstone | 0:6cf24371ad4f | 200 | OR_INTR_ENABLE_WDH | |
JWitherstone | 0:6cf24371ad4f | 201 | OR_INTR_ENABLE_RHSC; |
JWitherstone | 0:6cf24371ad4f | 202 | |
JWitherstone | 0:6cf24371ad4f | 203 | NVIC_SetPriority(USB_IRQn, 0); /* highest priority */ |
JWitherstone | 0:6cf24371ad4f | 204 | /* Enable the USB Interrupt */ |
JWitherstone | 0:6cf24371ad4f | 205 | NVIC_EnableIRQ(USB_IRQn); |
JWitherstone | 0:6cf24371ad4f | 206 | PRINT_Log("Host Initialized\n"); |
JWitherstone | 0:6cf24371ad4f | 207 | } |
JWitherstone | 0:6cf24371ad4f | 208 | |
JWitherstone | 0:6cf24371ad4f | 209 | /* |
JWitherstone | 0:6cf24371ad4f | 210 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 211 | * INTERRUPT SERVICE ROUTINE |
JWitherstone | 0:6cf24371ad4f | 212 | * |
JWitherstone | 0:6cf24371ad4f | 213 | * Description: This function services the interrupt caused by host controller |
JWitherstone | 0:6cf24371ad4f | 214 | * |
JWitherstone | 0:6cf24371ad4f | 215 | * Arguments : None |
JWitherstone | 0:6cf24371ad4f | 216 | * |
JWitherstone | 0:6cf24371ad4f | 217 | * Returns : None |
JWitherstone | 0:6cf24371ad4f | 218 | * |
JWitherstone | 0:6cf24371ad4f | 219 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 220 | */ |
JWitherstone | 0:6cf24371ad4f | 221 | |
JWitherstone | 0:6cf24371ad4f | 222 | void USB_IRQHandler (void) __irq |
JWitherstone | 0:6cf24371ad4f | 223 | { |
JWitherstone | 0:6cf24371ad4f | 224 | USB_INT32U int_status; |
JWitherstone | 0:6cf24371ad4f | 225 | USB_INT32U ie_status; |
JWitherstone | 0:6cf24371ad4f | 226 | |
JWitherstone | 0:6cf24371ad4f | 227 | int_status = LPC_USB->HcInterruptStatus; /* Read Interrupt Status */ |
JWitherstone | 0:6cf24371ad4f | 228 | ie_status = LPC_USB->HcInterruptEnable; /* Read Interrupt enable status */ |
JWitherstone | 0:6cf24371ad4f | 229 | |
JWitherstone | 0:6cf24371ad4f | 230 | if (!(int_status & ie_status)) { |
JWitherstone | 0:6cf24371ad4f | 231 | return; |
JWitherstone | 0:6cf24371ad4f | 232 | } else { |
JWitherstone | 0:6cf24371ad4f | 233 | |
JWitherstone | 0:6cf24371ad4f | 234 | int_status = int_status & ie_status; |
JWitherstone | 0:6cf24371ad4f | 235 | if (int_status & OR_INTR_STATUS_RHSC) { /* Root hub status change interrupt */ |
JWitherstone | 0:6cf24371ad4f | 236 | if (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_CSC) { |
JWitherstone | 0:6cf24371ad4f | 237 | if (LPC_USB->HcRhStatus & OR_RH_STATUS_DRWE) { |
JWitherstone | 0:6cf24371ad4f | 238 | /* |
JWitherstone | 0:6cf24371ad4f | 239 | * When DRWE is on, Connect Status Change |
JWitherstone | 0:6cf24371ad4f | 240 | * means a remote wakeup event. |
JWitherstone | 0:6cf24371ad4f | 241 | */ |
JWitherstone | 0:6cf24371ad4f | 242 | HOST_RhscIntr = 1;// JUST SOMETHING FOR A BREAKPOINT |
JWitherstone | 0:6cf24371ad4f | 243 | } |
JWitherstone | 0:6cf24371ad4f | 244 | else { |
JWitherstone | 0:6cf24371ad4f | 245 | /* |
JWitherstone | 0:6cf24371ad4f | 246 | * When DRWE is off, Connect Status Change |
JWitherstone | 0:6cf24371ad4f | 247 | * is NOT a remote wakeup event |
JWitherstone | 0:6cf24371ad4f | 248 | */ |
JWitherstone | 0:6cf24371ad4f | 249 | if (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_CCS) { |
JWitherstone | 0:6cf24371ad4f | 250 | if (!gUSBConnected) { |
JWitherstone | 0:6cf24371ad4f | 251 | HOST_TDControlStatus = 0; |
JWitherstone | 0:6cf24371ad4f | 252 | HOST_WdhIntr = 0; |
JWitherstone | 0:6cf24371ad4f | 253 | HOST_RhscIntr = 1; |
JWitherstone | 0:6cf24371ad4f | 254 | gUSBConnected = 1; |
JWitherstone | 0:6cf24371ad4f | 255 | } |
JWitherstone | 0:6cf24371ad4f | 256 | else |
JWitherstone | 0:6cf24371ad4f | 257 | PRINT_Log("Spurious status change (connected)?\n"); |
JWitherstone | 0:6cf24371ad4f | 258 | } else { |
JWitherstone | 0:6cf24371ad4f | 259 | if (gUSBConnected) { |
JWitherstone | 0:6cf24371ad4f | 260 | LPC_USB->HcInterruptEnable = 0; // why do we get multiple disc. rupts??? |
JWitherstone | 0:6cf24371ad4f | 261 | HOST_RhscIntr = 0; |
JWitherstone | 0:6cf24371ad4f | 262 | gUSBConnected = 0; |
JWitherstone | 0:6cf24371ad4f | 263 | } |
JWitherstone | 0:6cf24371ad4f | 264 | else |
JWitherstone | 0:6cf24371ad4f | 265 | PRINT_Log("Spurious status change (disconnected)?\n"); |
JWitherstone | 0:6cf24371ad4f | 266 | } |
JWitherstone | 0:6cf24371ad4f | 267 | } |
JWitherstone | 0:6cf24371ad4f | 268 | LPC_USB->HcRhPortStatus1 = OR_RH_PORT_CSC; |
JWitherstone | 0:6cf24371ad4f | 269 | } |
JWitherstone | 0:6cf24371ad4f | 270 | if (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_PRSC) { |
JWitherstone | 0:6cf24371ad4f | 271 | LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRSC; |
JWitherstone | 0:6cf24371ad4f | 272 | } |
JWitherstone | 0:6cf24371ad4f | 273 | } |
JWitherstone | 0:6cf24371ad4f | 274 | if (int_status & OR_INTR_STATUS_WDH) { /* Writeback Done Head interrupt */ |
JWitherstone | 0:6cf24371ad4f | 275 | HOST_WdhIntr = 1; |
JWitherstone | 0:6cf24371ad4f | 276 | HOST_TDControlStatus = (TDHead->Control >> 28) & 0xf; |
JWitherstone | 0:6cf24371ad4f | 277 | } |
JWitherstone | 0:6cf24371ad4f | 278 | LPC_USB->HcInterruptStatus = int_status; /* Clear interrupt status register */ |
JWitherstone | 0:6cf24371ad4f | 279 | } |
JWitherstone | 0:6cf24371ad4f | 280 | return; |
JWitherstone | 0:6cf24371ad4f | 281 | } |
JWitherstone | 0:6cf24371ad4f | 282 | |
JWitherstone | 0:6cf24371ad4f | 283 | /* |
JWitherstone | 0:6cf24371ad4f | 284 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 285 | * PROCESS TRANSFER DESCRIPTOR |
JWitherstone | 0:6cf24371ad4f | 286 | * |
JWitherstone | 0:6cf24371ad4f | 287 | * Description: This function processes the transfer descriptor |
JWitherstone | 0:6cf24371ad4f | 288 | * |
JWitherstone | 0:6cf24371ad4f | 289 | * Arguments : ed Endpoint descriptor that contains this transfer descriptor |
JWitherstone | 0:6cf24371ad4f | 290 | * token SETUP, IN, OUT |
JWitherstone | 0:6cf24371ad4f | 291 | * buffer Current Buffer Pointer of the transfer descriptor |
JWitherstone | 0:6cf24371ad4f | 292 | * buffer_len Length of the buffer |
JWitherstone | 0:6cf24371ad4f | 293 | * |
JWitherstone | 0:6cf24371ad4f | 294 | * Returns : OK if TD submission is successful |
JWitherstone | 0:6cf24371ad4f | 295 | * ERROR if TD submission fails |
JWitherstone | 0:6cf24371ad4f | 296 | * |
JWitherstone | 0:6cf24371ad4f | 297 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 298 | */ |
JWitherstone | 0:6cf24371ad4f | 299 | |
JWitherstone | 0:6cf24371ad4f | 300 | USB_INT32S Host_ProcessTD (volatile HCED *ed, |
JWitherstone | 0:6cf24371ad4f | 301 | volatile USB_INT32U token, |
JWitherstone | 0:6cf24371ad4f | 302 | volatile USB_INT08U *buffer, |
JWitherstone | 0:6cf24371ad4f | 303 | USB_INT32U buffer_len) |
JWitherstone | 0:6cf24371ad4f | 304 | { |
JWitherstone | 0:6cf24371ad4f | 305 | volatile USB_INT32U td_toggle; |
JWitherstone | 0:6cf24371ad4f | 306 | |
JWitherstone | 0:6cf24371ad4f | 307 | |
JWitherstone | 0:6cf24371ad4f | 308 | if (ed == EDCtrl) { |
JWitherstone | 0:6cf24371ad4f | 309 | if (token == TD_SETUP) { |
JWitherstone | 0:6cf24371ad4f | 310 | td_toggle = TD_TOGGLE_0; |
JWitherstone | 0:6cf24371ad4f | 311 | } else { |
JWitherstone | 0:6cf24371ad4f | 312 | td_toggle = TD_TOGGLE_1; |
JWitherstone | 0:6cf24371ad4f | 313 | } |
JWitherstone | 0:6cf24371ad4f | 314 | } else { |
JWitherstone | 0:6cf24371ad4f | 315 | td_toggle = 0; |
JWitherstone | 0:6cf24371ad4f | 316 | } |
JWitherstone | 0:6cf24371ad4f | 317 | TDHead->Control = (TD_ROUNDING | |
JWitherstone | 0:6cf24371ad4f | 318 | token | |
JWitherstone | 0:6cf24371ad4f | 319 | TD_DELAY_INT(0) | |
JWitherstone | 0:6cf24371ad4f | 320 | td_toggle | |
JWitherstone | 0:6cf24371ad4f | 321 | TD_CC); |
JWitherstone | 0:6cf24371ad4f | 322 | TDTail->Control = 0; |
JWitherstone | 0:6cf24371ad4f | 323 | TDHead->CurrBufPtr = (USB_INT32U) buffer; |
JWitherstone | 0:6cf24371ad4f | 324 | TDTail->CurrBufPtr = 0; |
JWitherstone | 0:6cf24371ad4f | 325 | TDHead->Next = (USB_INT32U) TDTail; |
JWitherstone | 0:6cf24371ad4f | 326 | TDTail->Next = 0; |
JWitherstone | 0:6cf24371ad4f | 327 | TDHead->BufEnd = (USB_INT32U)(buffer + (buffer_len - 1)); |
JWitherstone | 0:6cf24371ad4f | 328 | TDTail->BufEnd = 0; |
JWitherstone | 0:6cf24371ad4f | 329 | |
JWitherstone | 0:6cf24371ad4f | 330 | ed->HeadTd = (USB_INT32U)TDHead | ((ed->HeadTd) & 0x00000002); |
JWitherstone | 0:6cf24371ad4f | 331 | ed->TailTd = (USB_INT32U)TDTail; |
JWitherstone | 0:6cf24371ad4f | 332 | ed->Next = 0; |
JWitherstone | 0:6cf24371ad4f | 333 | |
JWitherstone | 0:6cf24371ad4f | 334 | if (ed == EDCtrl) { |
JWitherstone | 0:6cf24371ad4f | 335 | LPC_USB->HcControlHeadED = (USB_INT32U)ed; |
JWitherstone | 0:6cf24371ad4f | 336 | LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | OR_CMD_STATUS_CLF; |
JWitherstone | 0:6cf24371ad4f | 337 | LPC_USB->HcControl = LPC_USB->HcControl | OR_CONTROL_CLE; |
JWitherstone | 0:6cf24371ad4f | 338 | } else { |
JWitherstone | 0:6cf24371ad4f | 339 | LPC_USB->HcBulkHeadED = (USB_INT32U)ed; |
JWitherstone | 0:6cf24371ad4f | 340 | LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | OR_CMD_STATUS_BLF; |
JWitherstone | 0:6cf24371ad4f | 341 | LPC_USB->HcControl = LPC_USB->HcControl | OR_CONTROL_BLE; |
JWitherstone | 0:6cf24371ad4f | 342 | } |
JWitherstone | 0:6cf24371ad4f | 343 | |
JWitherstone | 0:6cf24371ad4f | 344 | Host_WDHWait(); |
JWitherstone | 0:6cf24371ad4f | 345 | |
JWitherstone | 0:6cf24371ad4f | 346 | // if (!(TDHead->Control & 0xF0000000)) { |
JWitherstone | 0:6cf24371ad4f | 347 | if (!HOST_TDControlStatus) { |
JWitherstone | 0:6cf24371ad4f | 348 | return (OK); |
JWitherstone | 0:6cf24371ad4f | 349 | } else { |
JWitherstone | 0:6cf24371ad4f | 350 | return (ERR_TD_FAIL); |
JWitherstone | 0:6cf24371ad4f | 351 | } |
JWitherstone | 0:6cf24371ad4f | 352 | } |
JWitherstone | 0:6cf24371ad4f | 353 | |
JWitherstone | 0:6cf24371ad4f | 354 | /* |
JWitherstone | 0:6cf24371ad4f | 355 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 356 | * ENUMERATE THE DEVICE |
JWitherstone | 0:6cf24371ad4f | 357 | * |
JWitherstone | 0:6cf24371ad4f | 358 | * Description: This function is used to enumerate the device connected |
JWitherstone | 0:6cf24371ad4f | 359 | * |
JWitherstone | 0:6cf24371ad4f | 360 | * Arguments : None |
JWitherstone | 0:6cf24371ad4f | 361 | * |
JWitherstone | 0:6cf24371ad4f | 362 | * Returns : None |
JWitherstone | 0:6cf24371ad4f | 363 | * |
JWitherstone | 0:6cf24371ad4f | 364 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 365 | */ |
JWitherstone | 0:6cf24371ad4f | 366 | |
JWitherstone | 0:6cf24371ad4f | 367 | USB_INT32S Host_EnumDev (void) |
JWitherstone | 0:6cf24371ad4f | 368 | { |
JWitherstone | 0:6cf24371ad4f | 369 | USB_INT32S rc; |
JWitherstone | 0:6cf24371ad4f | 370 | |
JWitherstone | 0:6cf24371ad4f | 371 | PRINT_Log("Connect a Mass Storage device\n"); |
JWitherstone | 0:6cf24371ad4f | 372 | while (!HOST_RhscIntr) |
JWitherstone | 0:6cf24371ad4f | 373 | __WFI(); |
JWitherstone | 0:6cf24371ad4f | 374 | Host_DelayMS(100); /* USB 2.0 spec says atleast 50ms delay beore port reset */ |
JWitherstone | 0:6cf24371ad4f | 375 | LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRS; // Initiate port reset |
JWitherstone | 0:6cf24371ad4f | 376 | while (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_PRS) |
JWitherstone | 0:6cf24371ad4f | 377 | __WFI(); // Wait for port reset to complete... |
JWitherstone | 0:6cf24371ad4f | 378 | LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRSC; // ...and clear port reset signal |
JWitherstone | 0:6cf24371ad4f | 379 | Host_DelayMS(200); /* Wait for 100 MS after port reset */ |
JWitherstone | 0:6cf24371ad4f | 380 | |
JWitherstone | 0:6cf24371ad4f | 381 | EDCtrl->Control = 8 << 16; /* Put max pkt size = 8 */ |
JWitherstone | 0:6cf24371ad4f | 382 | /* Read first 8 bytes of device desc */ |
JWitherstone | 0:6cf24371ad4f | 383 | rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_DEVICE, 0, TDBuffer, 8); |
JWitherstone | 0:6cf24371ad4f | 384 | if (rc != OK) { |
JWitherstone | 0:6cf24371ad4f | 385 | PRINT_Err(rc); |
JWitherstone | 0:6cf24371ad4f | 386 | return (rc); |
JWitherstone | 0:6cf24371ad4f | 387 | } |
JWitherstone | 0:6cf24371ad4f | 388 | EDCtrl->Control = TDBuffer[7] << 16; /* Get max pkt size of endpoint 0 */ |
JWitherstone | 0:6cf24371ad4f | 389 | rc = HOST_SET_ADDRESS(1); /* Set the device address to 1 */ |
JWitherstone | 0:6cf24371ad4f | 390 | if (rc != OK) { |
JWitherstone | 0:6cf24371ad4f | 391 | PRINT_Err(rc); |
JWitherstone | 0:6cf24371ad4f | 392 | return (rc); |
JWitherstone | 0:6cf24371ad4f | 393 | } |
JWitherstone | 0:6cf24371ad4f | 394 | Host_DelayMS(2); |
JWitherstone | 0:6cf24371ad4f | 395 | EDCtrl->Control = (EDCtrl->Control) | 1; /* Modify control pipe with address 1 */ |
JWitherstone | 0:6cf24371ad4f | 396 | /* Get the configuration descriptor */ |
JWitherstone | 0:6cf24371ad4f | 397 | rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_CONFIGURATION, 0, TDBuffer, 9); |
JWitherstone | 0:6cf24371ad4f | 398 | if (rc != OK) { |
JWitherstone | 0:6cf24371ad4f | 399 | PRINT_Err(rc); |
JWitherstone | 0:6cf24371ad4f | 400 | return (rc); |
JWitherstone | 0:6cf24371ad4f | 401 | } |
JWitherstone | 0:6cf24371ad4f | 402 | /* Get the first configuration data */ |
JWitherstone | 0:6cf24371ad4f | 403 | rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_CONFIGURATION, 0, TDBuffer, ReadLE16U(&TDBuffer[2])); |
JWitherstone | 0:6cf24371ad4f | 404 | if (rc != OK) { |
JWitherstone | 0:6cf24371ad4f | 405 | PRINT_Err(rc); |
JWitherstone | 0:6cf24371ad4f | 406 | return (rc); |
JWitherstone | 0:6cf24371ad4f | 407 | } |
JWitherstone | 0:6cf24371ad4f | 408 | rc = MS_ParseConfiguration(); /* Parse the configuration */ |
JWitherstone | 0:6cf24371ad4f | 409 | if (rc != OK) { |
JWitherstone | 0:6cf24371ad4f | 410 | PRINT_Err(rc); |
JWitherstone | 0:6cf24371ad4f | 411 | return (rc); |
JWitherstone | 0:6cf24371ad4f | 412 | } |
JWitherstone | 0:6cf24371ad4f | 413 | rc = USBH_SET_CONFIGURATION(1); /* Select device configuration 1 */ |
JWitherstone | 0:6cf24371ad4f | 414 | if (rc != OK) { |
JWitherstone | 0:6cf24371ad4f | 415 | PRINT_Err(rc); |
JWitherstone | 0:6cf24371ad4f | 416 | } |
JWitherstone | 0:6cf24371ad4f | 417 | Host_DelayMS(100); /* Some devices may require this delay */ |
JWitherstone | 0:6cf24371ad4f | 418 | return (rc); |
JWitherstone | 0:6cf24371ad4f | 419 | } |
JWitherstone | 0:6cf24371ad4f | 420 | |
JWitherstone | 0:6cf24371ad4f | 421 | /* |
JWitherstone | 0:6cf24371ad4f | 422 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 423 | * RECEIVE THE CONTROL INFORMATION |
JWitherstone | 0:6cf24371ad4f | 424 | * |
JWitherstone | 0:6cf24371ad4f | 425 | * Description: This function is used to receive the control information |
JWitherstone | 0:6cf24371ad4f | 426 | * |
JWitherstone | 0:6cf24371ad4f | 427 | * Arguments : bm_request_type |
JWitherstone | 0:6cf24371ad4f | 428 | * b_request |
JWitherstone | 0:6cf24371ad4f | 429 | * w_value |
JWitherstone | 0:6cf24371ad4f | 430 | * w_index |
JWitherstone | 0:6cf24371ad4f | 431 | * w_length |
JWitherstone | 0:6cf24371ad4f | 432 | * buffer |
JWitherstone | 0:6cf24371ad4f | 433 | * |
JWitherstone | 0:6cf24371ad4f | 434 | * Returns : OK if Success |
JWitherstone | 0:6cf24371ad4f | 435 | * ERROR if Failed |
JWitherstone | 0:6cf24371ad4f | 436 | * |
JWitherstone | 0:6cf24371ad4f | 437 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 438 | */ |
JWitherstone | 0:6cf24371ad4f | 439 | |
JWitherstone | 0:6cf24371ad4f | 440 | USB_INT32S Host_CtrlRecv ( USB_INT08U bm_request_type, |
JWitherstone | 0:6cf24371ad4f | 441 | USB_INT08U b_request, |
JWitherstone | 0:6cf24371ad4f | 442 | USB_INT16U w_value, |
JWitherstone | 0:6cf24371ad4f | 443 | USB_INT16U w_index, |
JWitherstone | 0:6cf24371ad4f | 444 | USB_INT16U w_length, |
JWitherstone | 0:6cf24371ad4f | 445 | volatile USB_INT08U *buffer) |
JWitherstone | 0:6cf24371ad4f | 446 | { |
JWitherstone | 0:6cf24371ad4f | 447 | USB_INT32S rc; |
JWitherstone | 0:6cf24371ad4f | 448 | |
JWitherstone | 0:6cf24371ad4f | 449 | |
JWitherstone | 0:6cf24371ad4f | 450 | Host_FillSetup(bm_request_type, b_request, w_value, w_index, w_length); |
JWitherstone | 0:6cf24371ad4f | 451 | rc = Host_ProcessTD(EDCtrl, TD_SETUP, TDBuffer, 8); |
JWitherstone | 0:6cf24371ad4f | 452 | if (rc == OK) { |
JWitherstone | 0:6cf24371ad4f | 453 | if (w_length) { |
JWitherstone | 0:6cf24371ad4f | 454 | rc = Host_ProcessTD(EDCtrl, TD_IN, TDBuffer, w_length); |
JWitherstone | 0:6cf24371ad4f | 455 | } |
JWitherstone | 0:6cf24371ad4f | 456 | if (rc == OK) { |
JWitherstone | 0:6cf24371ad4f | 457 | rc = Host_ProcessTD(EDCtrl, TD_OUT, NULL, 0); |
JWitherstone | 0:6cf24371ad4f | 458 | } |
JWitherstone | 0:6cf24371ad4f | 459 | } |
JWitherstone | 0:6cf24371ad4f | 460 | return (rc); |
JWitherstone | 0:6cf24371ad4f | 461 | } |
JWitherstone | 0:6cf24371ad4f | 462 | |
JWitherstone | 0:6cf24371ad4f | 463 | /* |
JWitherstone | 0:6cf24371ad4f | 464 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 465 | * SEND THE CONTROL INFORMATION |
JWitherstone | 0:6cf24371ad4f | 466 | * |
JWitherstone | 0:6cf24371ad4f | 467 | * Description: This function is used to send the control information |
JWitherstone | 0:6cf24371ad4f | 468 | * |
JWitherstone | 0:6cf24371ad4f | 469 | * Arguments : None |
JWitherstone | 0:6cf24371ad4f | 470 | * |
JWitherstone | 0:6cf24371ad4f | 471 | * Returns : OK if Success |
JWitherstone | 0:6cf24371ad4f | 472 | * ERR_INVALID_BOOTSIG if Failed |
JWitherstone | 0:6cf24371ad4f | 473 | * |
JWitherstone | 0:6cf24371ad4f | 474 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 475 | */ |
JWitherstone | 0:6cf24371ad4f | 476 | |
JWitherstone | 0:6cf24371ad4f | 477 | USB_INT32S Host_CtrlSend ( USB_INT08U bm_request_type, |
JWitherstone | 0:6cf24371ad4f | 478 | USB_INT08U b_request, |
JWitherstone | 0:6cf24371ad4f | 479 | USB_INT16U w_value, |
JWitherstone | 0:6cf24371ad4f | 480 | USB_INT16U w_index, |
JWitherstone | 0:6cf24371ad4f | 481 | USB_INT16U w_length, |
JWitherstone | 0:6cf24371ad4f | 482 | volatile USB_INT08U *buffer) |
JWitherstone | 0:6cf24371ad4f | 483 | { |
JWitherstone | 0:6cf24371ad4f | 484 | USB_INT32S rc; |
JWitherstone | 0:6cf24371ad4f | 485 | |
JWitherstone | 0:6cf24371ad4f | 486 | |
JWitherstone | 0:6cf24371ad4f | 487 | Host_FillSetup(bm_request_type, b_request, w_value, w_index, w_length); |
JWitherstone | 0:6cf24371ad4f | 488 | |
JWitherstone | 0:6cf24371ad4f | 489 | rc = Host_ProcessTD(EDCtrl, TD_SETUP, TDBuffer, 8); |
JWitherstone | 0:6cf24371ad4f | 490 | if (rc == OK) { |
JWitherstone | 0:6cf24371ad4f | 491 | if (w_length) { |
JWitherstone | 0:6cf24371ad4f | 492 | rc = Host_ProcessTD(EDCtrl, TD_OUT, TDBuffer, w_length); |
JWitherstone | 0:6cf24371ad4f | 493 | } |
JWitherstone | 0:6cf24371ad4f | 494 | if (rc == OK) { |
JWitherstone | 0:6cf24371ad4f | 495 | rc = Host_ProcessTD(EDCtrl, TD_IN, NULL, 0); |
JWitherstone | 0:6cf24371ad4f | 496 | } |
JWitherstone | 0:6cf24371ad4f | 497 | } |
JWitherstone | 0:6cf24371ad4f | 498 | return (rc); |
JWitherstone | 0:6cf24371ad4f | 499 | } |
JWitherstone | 0:6cf24371ad4f | 500 | |
JWitherstone | 0:6cf24371ad4f | 501 | /* |
JWitherstone | 0:6cf24371ad4f | 502 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 503 | * FILL SETUP PACKET |
JWitherstone | 0:6cf24371ad4f | 504 | * |
JWitherstone | 0:6cf24371ad4f | 505 | * Description: This function is used to fill the setup packet |
JWitherstone | 0:6cf24371ad4f | 506 | * |
JWitherstone | 0:6cf24371ad4f | 507 | * Arguments : None |
JWitherstone | 0:6cf24371ad4f | 508 | * |
JWitherstone | 0:6cf24371ad4f | 509 | * Returns : OK if Success |
JWitherstone | 0:6cf24371ad4f | 510 | * ERR_INVALID_BOOTSIG if Failed |
JWitherstone | 0:6cf24371ad4f | 511 | * |
JWitherstone | 0:6cf24371ad4f | 512 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 513 | */ |
JWitherstone | 0:6cf24371ad4f | 514 | |
JWitherstone | 0:6cf24371ad4f | 515 | void Host_FillSetup (USB_INT08U bm_request_type, |
JWitherstone | 0:6cf24371ad4f | 516 | USB_INT08U b_request, |
JWitherstone | 0:6cf24371ad4f | 517 | USB_INT16U w_value, |
JWitherstone | 0:6cf24371ad4f | 518 | USB_INT16U w_index, |
JWitherstone | 0:6cf24371ad4f | 519 | USB_INT16U w_length) |
JWitherstone | 0:6cf24371ad4f | 520 | { |
JWitherstone | 0:6cf24371ad4f | 521 | int i; |
JWitherstone | 0:6cf24371ad4f | 522 | for (i=0;i<w_length;i++) |
JWitherstone | 0:6cf24371ad4f | 523 | TDBuffer[i] = 0; |
JWitherstone | 0:6cf24371ad4f | 524 | |
JWitherstone | 0:6cf24371ad4f | 525 | TDBuffer[0] = bm_request_type; |
JWitherstone | 0:6cf24371ad4f | 526 | TDBuffer[1] = b_request; |
JWitherstone | 0:6cf24371ad4f | 527 | WriteLE16U(&TDBuffer[2], w_value); |
JWitherstone | 0:6cf24371ad4f | 528 | WriteLE16U(&TDBuffer[4], w_index); |
JWitherstone | 0:6cf24371ad4f | 529 | WriteLE16U(&TDBuffer[6], w_length); |
JWitherstone | 0:6cf24371ad4f | 530 | } |
JWitherstone | 0:6cf24371ad4f | 531 | |
JWitherstone | 0:6cf24371ad4f | 532 | |
JWitherstone | 0:6cf24371ad4f | 533 | |
JWitherstone | 0:6cf24371ad4f | 534 | /* |
JWitherstone | 0:6cf24371ad4f | 535 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 536 | * INITIALIZE THE TRANSFER DESCRIPTOR |
JWitherstone | 0:6cf24371ad4f | 537 | * |
JWitherstone | 0:6cf24371ad4f | 538 | * Description: This function initializes transfer descriptor |
JWitherstone | 0:6cf24371ad4f | 539 | * |
JWitherstone | 0:6cf24371ad4f | 540 | * Arguments : Pointer to TD structure |
JWitherstone | 0:6cf24371ad4f | 541 | * |
JWitherstone | 0:6cf24371ad4f | 542 | * Returns : None |
JWitherstone | 0:6cf24371ad4f | 543 | * |
JWitherstone | 0:6cf24371ad4f | 544 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 545 | */ |
JWitherstone | 0:6cf24371ad4f | 546 | |
JWitherstone | 0:6cf24371ad4f | 547 | void Host_TDInit (volatile HCTD *td) |
JWitherstone | 0:6cf24371ad4f | 548 | { |
JWitherstone | 0:6cf24371ad4f | 549 | |
JWitherstone | 0:6cf24371ad4f | 550 | td->Control = 0; |
JWitherstone | 0:6cf24371ad4f | 551 | td->CurrBufPtr = 0; |
JWitherstone | 0:6cf24371ad4f | 552 | td->Next = 0; |
JWitherstone | 0:6cf24371ad4f | 553 | td->BufEnd = 0; |
JWitherstone | 0:6cf24371ad4f | 554 | } |
JWitherstone | 0:6cf24371ad4f | 555 | |
JWitherstone | 0:6cf24371ad4f | 556 | /* |
JWitherstone | 0:6cf24371ad4f | 557 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 558 | * INITIALIZE THE ENDPOINT DESCRIPTOR |
JWitherstone | 0:6cf24371ad4f | 559 | * |
JWitherstone | 0:6cf24371ad4f | 560 | * Description: This function initializes endpoint descriptor |
JWitherstone | 0:6cf24371ad4f | 561 | * |
JWitherstone | 0:6cf24371ad4f | 562 | * Arguments : Pointer to ED strcuture |
JWitherstone | 0:6cf24371ad4f | 563 | * |
JWitherstone | 0:6cf24371ad4f | 564 | * Returns : None |
JWitherstone | 0:6cf24371ad4f | 565 | * |
JWitherstone | 0:6cf24371ad4f | 566 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 567 | */ |
JWitherstone | 0:6cf24371ad4f | 568 | |
JWitherstone | 0:6cf24371ad4f | 569 | void Host_EDInit (volatile HCED *ed) |
JWitherstone | 0:6cf24371ad4f | 570 | { |
JWitherstone | 0:6cf24371ad4f | 571 | |
JWitherstone | 0:6cf24371ad4f | 572 | ed->Control = 0; |
JWitherstone | 0:6cf24371ad4f | 573 | ed->TailTd = 0; |
JWitherstone | 0:6cf24371ad4f | 574 | ed->HeadTd = 0; |
JWitherstone | 0:6cf24371ad4f | 575 | ed->Next = 0; |
JWitherstone | 0:6cf24371ad4f | 576 | } |
JWitherstone | 0:6cf24371ad4f | 577 | |
JWitherstone | 0:6cf24371ad4f | 578 | /* |
JWitherstone | 0:6cf24371ad4f | 579 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 580 | * INITIALIZE HOST CONTROLLER COMMUNICATIONS AREA |
JWitherstone | 0:6cf24371ad4f | 581 | * |
JWitherstone | 0:6cf24371ad4f | 582 | * Description: This function initializes host controller communications area |
JWitherstone | 0:6cf24371ad4f | 583 | * |
JWitherstone | 0:6cf24371ad4f | 584 | * Arguments : Pointer to HCCA |
JWitherstone | 0:6cf24371ad4f | 585 | * |
JWitherstone | 0:6cf24371ad4f | 586 | * Returns : |
JWitherstone | 0:6cf24371ad4f | 587 | * |
JWitherstone | 0:6cf24371ad4f | 588 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 589 | */ |
JWitherstone | 0:6cf24371ad4f | 590 | |
JWitherstone | 0:6cf24371ad4f | 591 | void Host_HCCAInit (volatile HCCA *hcca) |
JWitherstone | 0:6cf24371ad4f | 592 | { |
JWitherstone | 0:6cf24371ad4f | 593 | USB_INT32U i; |
JWitherstone | 0:6cf24371ad4f | 594 | |
JWitherstone | 0:6cf24371ad4f | 595 | |
JWitherstone | 0:6cf24371ad4f | 596 | for (i = 0; i < 32; i++) { |
JWitherstone | 0:6cf24371ad4f | 597 | |
JWitherstone | 0:6cf24371ad4f | 598 | hcca->IntTable[i] = 0; |
JWitherstone | 0:6cf24371ad4f | 599 | hcca->FrameNumber = 0; |
JWitherstone | 0:6cf24371ad4f | 600 | hcca->DoneHead = 0; |
JWitherstone | 0:6cf24371ad4f | 601 | } |
JWitherstone | 0:6cf24371ad4f | 602 | |
JWitherstone | 0:6cf24371ad4f | 603 | } |
JWitherstone | 0:6cf24371ad4f | 604 | |
JWitherstone | 0:6cf24371ad4f | 605 | /* |
JWitherstone | 0:6cf24371ad4f | 606 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 607 | * WAIT FOR WDH INTERRUPT |
JWitherstone | 0:6cf24371ad4f | 608 | * |
JWitherstone | 0:6cf24371ad4f | 609 | * Description: This function is infinite loop which breaks when ever a WDH interrupt rises |
JWitherstone | 0:6cf24371ad4f | 610 | * |
JWitherstone | 0:6cf24371ad4f | 611 | * Arguments : None |
JWitherstone | 0:6cf24371ad4f | 612 | * |
JWitherstone | 0:6cf24371ad4f | 613 | * Returns : None |
JWitherstone | 0:6cf24371ad4f | 614 | * |
JWitherstone | 0:6cf24371ad4f | 615 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 616 | */ |
JWitherstone | 0:6cf24371ad4f | 617 | |
JWitherstone | 0:6cf24371ad4f | 618 | void Host_WDHWait (void) |
JWitherstone | 0:6cf24371ad4f | 619 | { |
JWitherstone | 0:6cf24371ad4f | 620 | while (!HOST_WdhIntr) |
JWitherstone | 0:6cf24371ad4f | 621 | __WFI(); |
JWitherstone | 0:6cf24371ad4f | 622 | |
JWitherstone | 0:6cf24371ad4f | 623 | HOST_WdhIntr = 0; |
JWitherstone | 0:6cf24371ad4f | 624 | } |
JWitherstone | 0:6cf24371ad4f | 625 | |
JWitherstone | 0:6cf24371ad4f | 626 | /* |
JWitherstone | 0:6cf24371ad4f | 627 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 628 | * READ LE 32U |
JWitherstone | 0:6cf24371ad4f | 629 | * |
JWitherstone | 0:6cf24371ad4f | 630 | * Description: This function is used to read an unsigned integer from a character buffer in the platform |
JWitherstone | 0:6cf24371ad4f | 631 | * containing little endian processor |
JWitherstone | 0:6cf24371ad4f | 632 | * |
JWitherstone | 0:6cf24371ad4f | 633 | * Arguments : pmem Pointer to the character buffer |
JWitherstone | 0:6cf24371ad4f | 634 | * |
JWitherstone | 0:6cf24371ad4f | 635 | * Returns : val Unsigned integer |
JWitherstone | 0:6cf24371ad4f | 636 | * |
JWitherstone | 0:6cf24371ad4f | 637 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 638 | */ |
JWitherstone | 0:6cf24371ad4f | 639 | |
JWitherstone | 0:6cf24371ad4f | 640 | USB_INT32U ReadLE32U (volatile USB_INT08U *pmem) |
JWitherstone | 0:6cf24371ad4f | 641 | { |
JWitherstone | 0:6cf24371ad4f | 642 | USB_INT32U val = *(USB_INT32U*)pmem; |
JWitherstone | 0:6cf24371ad4f | 643 | #ifdef __BIG_ENDIAN |
JWitherstone | 0:6cf24371ad4f | 644 | return __REV(val); |
JWitherstone | 0:6cf24371ad4f | 645 | #else |
JWitherstone | 0:6cf24371ad4f | 646 | return val; |
JWitherstone | 0:6cf24371ad4f | 647 | #endif |
JWitherstone | 0:6cf24371ad4f | 648 | } |
JWitherstone | 0:6cf24371ad4f | 649 | |
JWitherstone | 0:6cf24371ad4f | 650 | /* |
JWitherstone | 0:6cf24371ad4f | 651 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 652 | * WRITE LE 32U |
JWitherstone | 0:6cf24371ad4f | 653 | * |
JWitherstone | 0:6cf24371ad4f | 654 | * Description: This function is used to write an unsigned integer into a charecter buffer in the platform |
JWitherstone | 0:6cf24371ad4f | 655 | * containing little endian processor. |
JWitherstone | 0:6cf24371ad4f | 656 | * |
JWitherstone | 0:6cf24371ad4f | 657 | * Arguments : pmem Pointer to the charecter buffer |
JWitherstone | 0:6cf24371ad4f | 658 | * val Integer value to be placed in the charecter buffer |
JWitherstone | 0:6cf24371ad4f | 659 | * |
JWitherstone | 0:6cf24371ad4f | 660 | * Returns : None |
JWitherstone | 0:6cf24371ad4f | 661 | * |
JWitherstone | 0:6cf24371ad4f | 662 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 663 | */ |
JWitherstone | 0:6cf24371ad4f | 664 | |
JWitherstone | 0:6cf24371ad4f | 665 | void WriteLE32U (volatile USB_INT08U *pmem, |
JWitherstone | 0:6cf24371ad4f | 666 | USB_INT32U val) |
JWitherstone | 0:6cf24371ad4f | 667 | { |
JWitherstone | 0:6cf24371ad4f | 668 | #ifdef __BIG_ENDIAN |
JWitherstone | 0:6cf24371ad4f | 669 | *(USB_INT32U*)pmem = __REV(val); |
JWitherstone | 0:6cf24371ad4f | 670 | #else |
JWitherstone | 0:6cf24371ad4f | 671 | *(USB_INT32U*)pmem = val; |
JWitherstone | 0:6cf24371ad4f | 672 | #endif |
JWitherstone | 0:6cf24371ad4f | 673 | } |
JWitherstone | 0:6cf24371ad4f | 674 | |
JWitherstone | 0:6cf24371ad4f | 675 | /* |
JWitherstone | 0:6cf24371ad4f | 676 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 677 | * READ LE 16U |
JWitherstone | 0:6cf24371ad4f | 678 | * |
JWitherstone | 0:6cf24371ad4f | 679 | * Description: This function is used to read an unsigned short integer from a charecter buffer in the platform |
JWitherstone | 0:6cf24371ad4f | 680 | * containing little endian processor |
JWitherstone | 0:6cf24371ad4f | 681 | * |
JWitherstone | 0:6cf24371ad4f | 682 | * Arguments : pmem Pointer to the charecter buffer |
JWitherstone | 0:6cf24371ad4f | 683 | * |
JWitherstone | 0:6cf24371ad4f | 684 | * Returns : val Unsigned short integer |
JWitherstone | 0:6cf24371ad4f | 685 | * |
JWitherstone | 0:6cf24371ad4f | 686 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 687 | */ |
JWitherstone | 0:6cf24371ad4f | 688 | |
JWitherstone | 0:6cf24371ad4f | 689 | USB_INT16U ReadLE16U (volatile USB_INT08U *pmem) |
JWitherstone | 0:6cf24371ad4f | 690 | { |
JWitherstone | 0:6cf24371ad4f | 691 | USB_INT16U val = *(USB_INT16U*)pmem; |
JWitherstone | 0:6cf24371ad4f | 692 | #ifdef __BIG_ENDIAN |
JWitherstone | 0:6cf24371ad4f | 693 | return __REV16(val); |
JWitherstone | 0:6cf24371ad4f | 694 | #else |
JWitherstone | 0:6cf24371ad4f | 695 | return val; |
JWitherstone | 0:6cf24371ad4f | 696 | #endif |
JWitherstone | 0:6cf24371ad4f | 697 | } |
JWitherstone | 0:6cf24371ad4f | 698 | |
JWitherstone | 0:6cf24371ad4f | 699 | /* |
JWitherstone | 0:6cf24371ad4f | 700 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 701 | * WRITE LE 16U |
JWitherstone | 0:6cf24371ad4f | 702 | * |
JWitherstone | 0:6cf24371ad4f | 703 | * Description: This function is used to write an unsigned short integer into a charecter buffer in the |
JWitherstone | 0:6cf24371ad4f | 704 | * platform containing little endian processor |
JWitherstone | 0:6cf24371ad4f | 705 | * |
JWitherstone | 0:6cf24371ad4f | 706 | * Arguments : pmem Pointer to the charecter buffer |
JWitherstone | 0:6cf24371ad4f | 707 | * val Value to be placed in the charecter buffer |
JWitherstone | 0:6cf24371ad4f | 708 | * |
JWitherstone | 0:6cf24371ad4f | 709 | * Returns : None |
JWitherstone | 0:6cf24371ad4f | 710 | * |
JWitherstone | 0:6cf24371ad4f | 711 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 712 | */ |
JWitherstone | 0:6cf24371ad4f | 713 | |
JWitherstone | 0:6cf24371ad4f | 714 | void WriteLE16U (volatile USB_INT08U *pmem, |
JWitherstone | 0:6cf24371ad4f | 715 | USB_INT16U val) |
JWitherstone | 0:6cf24371ad4f | 716 | { |
JWitherstone | 0:6cf24371ad4f | 717 | #ifdef __BIG_ENDIAN |
JWitherstone | 0:6cf24371ad4f | 718 | *(USB_INT16U*)pmem = (__REV16(val) & 0xFFFF); |
JWitherstone | 0:6cf24371ad4f | 719 | #else |
JWitherstone | 0:6cf24371ad4f | 720 | *(USB_INT16U*)pmem = val; |
JWitherstone | 0:6cf24371ad4f | 721 | #endif |
JWitherstone | 0:6cf24371ad4f | 722 | } |
JWitherstone | 0:6cf24371ad4f | 723 | |
JWitherstone | 0:6cf24371ad4f | 724 | /* |
JWitherstone | 0:6cf24371ad4f | 725 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 726 | * READ BE 32U |
JWitherstone | 0:6cf24371ad4f | 727 | * |
JWitherstone | 0:6cf24371ad4f | 728 | * Description: This function is used to read an unsigned integer from a charecter buffer in the platform |
JWitherstone | 0:6cf24371ad4f | 729 | * containing big endian processor |
JWitherstone | 0:6cf24371ad4f | 730 | * |
JWitherstone | 0:6cf24371ad4f | 731 | * Arguments : pmem Pointer to the charecter buffer |
JWitherstone | 0:6cf24371ad4f | 732 | * |
JWitherstone | 0:6cf24371ad4f | 733 | * Returns : val Unsigned integer |
JWitherstone | 0:6cf24371ad4f | 734 | * |
JWitherstone | 0:6cf24371ad4f | 735 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 736 | */ |
JWitherstone | 0:6cf24371ad4f | 737 | |
JWitherstone | 0:6cf24371ad4f | 738 | USB_INT32U ReadBE32U (volatile USB_INT08U *pmem) |
JWitherstone | 0:6cf24371ad4f | 739 | { |
JWitherstone | 0:6cf24371ad4f | 740 | USB_INT32U val = *(USB_INT32U*)pmem; |
JWitherstone | 0:6cf24371ad4f | 741 | #ifdef __BIG_ENDIAN |
JWitherstone | 0:6cf24371ad4f | 742 | return val; |
JWitherstone | 0:6cf24371ad4f | 743 | #else |
JWitherstone | 0:6cf24371ad4f | 744 | return __REV(val); |
JWitherstone | 0:6cf24371ad4f | 745 | #endif |
JWitherstone | 0:6cf24371ad4f | 746 | } |
JWitherstone | 0:6cf24371ad4f | 747 | |
JWitherstone | 0:6cf24371ad4f | 748 | /* |
JWitherstone | 0:6cf24371ad4f | 749 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 750 | * WRITE BE 32U |
JWitherstone | 0:6cf24371ad4f | 751 | * |
JWitherstone | 0:6cf24371ad4f | 752 | * Description: This function is used to write an unsigned integer into a charecter buffer in the platform |
JWitherstone | 0:6cf24371ad4f | 753 | * containing big endian processor |
JWitherstone | 0:6cf24371ad4f | 754 | * |
JWitherstone | 0:6cf24371ad4f | 755 | * Arguments : pmem Pointer to the charecter buffer |
JWitherstone | 0:6cf24371ad4f | 756 | * val Value to be placed in the charecter buffer |
JWitherstone | 0:6cf24371ad4f | 757 | * |
JWitherstone | 0:6cf24371ad4f | 758 | * Returns : None |
JWitherstone | 0:6cf24371ad4f | 759 | * |
JWitherstone | 0:6cf24371ad4f | 760 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 761 | */ |
JWitherstone | 0:6cf24371ad4f | 762 | |
JWitherstone | 0:6cf24371ad4f | 763 | void WriteBE32U (volatile USB_INT08U *pmem, |
JWitherstone | 0:6cf24371ad4f | 764 | USB_INT32U val) |
JWitherstone | 0:6cf24371ad4f | 765 | { |
JWitherstone | 0:6cf24371ad4f | 766 | #ifdef __BIG_ENDIAN |
JWitherstone | 0:6cf24371ad4f | 767 | *(USB_INT32U*)pmem = val; |
JWitherstone | 0:6cf24371ad4f | 768 | #else |
JWitherstone | 0:6cf24371ad4f | 769 | *(USB_INT32U*)pmem = __REV(val); |
JWitherstone | 0:6cf24371ad4f | 770 | #endif |
JWitherstone | 0:6cf24371ad4f | 771 | } |
JWitherstone | 0:6cf24371ad4f | 772 | |
JWitherstone | 0:6cf24371ad4f | 773 | /* |
JWitherstone | 0:6cf24371ad4f | 774 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 775 | * READ BE 16U |
JWitherstone | 0:6cf24371ad4f | 776 | * |
JWitherstone | 0:6cf24371ad4f | 777 | * Description: This function is used to read an unsigned short integer from a charecter buffer in the platform |
JWitherstone | 0:6cf24371ad4f | 778 | * containing big endian processor |
JWitherstone | 0:6cf24371ad4f | 779 | * |
JWitherstone | 0:6cf24371ad4f | 780 | * Arguments : pmem Pointer to the charecter buffer |
JWitherstone | 0:6cf24371ad4f | 781 | * |
JWitherstone | 0:6cf24371ad4f | 782 | * Returns : val Unsigned short integer |
JWitherstone | 0:6cf24371ad4f | 783 | * |
JWitherstone | 0:6cf24371ad4f | 784 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 785 | */ |
JWitherstone | 0:6cf24371ad4f | 786 | |
JWitherstone | 0:6cf24371ad4f | 787 | USB_INT16U ReadBE16U (volatile USB_INT08U *pmem) |
JWitherstone | 0:6cf24371ad4f | 788 | { |
JWitherstone | 0:6cf24371ad4f | 789 | USB_INT16U val = *(USB_INT16U*)pmem; |
JWitherstone | 0:6cf24371ad4f | 790 | #ifdef __BIG_ENDIAN |
JWitherstone | 0:6cf24371ad4f | 791 | return val; |
JWitherstone | 0:6cf24371ad4f | 792 | #else |
JWitherstone | 0:6cf24371ad4f | 793 | return __REV16(val); |
JWitherstone | 0:6cf24371ad4f | 794 | #endif |
JWitherstone | 0:6cf24371ad4f | 795 | } |
JWitherstone | 0:6cf24371ad4f | 796 | |
JWitherstone | 0:6cf24371ad4f | 797 | /* |
JWitherstone | 0:6cf24371ad4f | 798 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 799 | * WRITE BE 16U |
JWitherstone | 0:6cf24371ad4f | 800 | * |
JWitherstone | 0:6cf24371ad4f | 801 | * Description: This function is used to write an unsigned short integer into the charecter buffer in the |
JWitherstone | 0:6cf24371ad4f | 802 | * platform containing big endian processor |
JWitherstone | 0:6cf24371ad4f | 803 | * |
JWitherstone | 0:6cf24371ad4f | 804 | * Arguments : pmem Pointer to the charecter buffer |
JWitherstone | 0:6cf24371ad4f | 805 | * val Value to be placed in the charecter buffer |
JWitherstone | 0:6cf24371ad4f | 806 | * |
JWitherstone | 0:6cf24371ad4f | 807 | * Returns : None |
JWitherstone | 0:6cf24371ad4f | 808 | * |
JWitherstone | 0:6cf24371ad4f | 809 | ************************************************************************************************************** |
JWitherstone | 0:6cf24371ad4f | 810 | */ |
JWitherstone | 0:6cf24371ad4f | 811 | |
JWitherstone | 0:6cf24371ad4f | 812 | void WriteBE16U (volatile USB_INT08U *pmem, |
JWitherstone | 0:6cf24371ad4f | 813 | USB_INT16U val) |
JWitherstone | 0:6cf24371ad4f | 814 | { |
JWitherstone | 0:6cf24371ad4f | 815 | #ifdef __BIG_ENDIAN |
JWitherstone | 0:6cf24371ad4f | 816 | *(USB_INT16U*)pmem = val; |
JWitherstone | 0:6cf24371ad4f | 817 | #else |
JWitherstone | 0:6cf24371ad4f | 818 | *(USB_INT16U*)pmem = (__REV16(val) & 0xFFFF); |
JWitherstone | 0:6cf24371ad4f | 819 | #endif |
JWitherstone | 0:6cf24371ad4f | 820 | } |