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