Dependencies:   mbed

Committer:
joe
Date:
Fri Aug 20 15:38:52 2010 +0000
Revision:
2:a079de4fd5b9
Parent:
0:960b355eaa84

        

Who changed what in which revision?

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