Programme d'une sonde de température DHT 11 associée à de la sauvegarde de données sur clé USB et à l'affichage de ces données sur afficheur LCD

Dependencies:   FatFileSystemCpp mbed

Committer:
Fanta025
Date:
Tue Jun 02 14:19:54 2015 +0000
Revision:
0:ed0b4e66d2ad
Programme d'une sonde de temp?rature DHT 11 associ?e ? de la sauvegarde de donn?es sur USB et un affichage sur ?cran LCD

Who changed what in which revision?

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