V

Dependencies:   mbed FatFileSystemCpp TextLCD

Committer:
bertonieto
Date:
Mon Aug 12 21:54:14 2019 +0000
Revision:
2:73cbcff90232
Export new

Who changed what in which revision?

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