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