KIT Solar Car Project / Mbed 2 deprecated BMS_v1

Dependencies:   mbed INA226

Committer:
takuma1
Date:
Thu Apr 08 10:04:55 2021 +0000
Revision:
5:f07de56debf3
210408_BMS_v1;

Who changed what in which revision?

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