00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00045
00046
00047
00048
00049
00050 #include "UsbFunction.h"
00051
00052 #if (CPU_TYPE == CPU_TYPE_32BIT)
00053 #include "_fr.h" only for FR80 (MB91F662) controller
00054 #include "mon.h" only for FR80 (MB91F662) controller
00055 #endif
00056
00057 #include "base_type.h"
00058 #include "UsbDescriptors.h"
00059 #include "dbg.h"
00060
00061 #if (CPU_TYPE == CPU_TYPE_32BIT)
00062 #include "conv16FX_FR80.h" only for FR80 (MB91F662) controller
00063 #endif
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 uint8_t (* pu8HandlerClassEvent)(usb_event_t * stcEvent) = 0;
00090 uint8_t (* pu8HandlerStatusEvent)(usb_event_t * stcEvent) = 0;
00091 uint8_t (* pu8HandlerBeforeStatusChange)(usb_event_t * stcEvent) = 0;
00092
00093 uint16_t u16FrameNumber = 0;
00094 void (* SOFHandler)(uint16_t u16FrameNumber) = 0;
00095
00096 fnuint8_t au8ReceiveBuffer[USB_FUNC_EP0_SIZE];
00097 usb_buffer_t stcEndpointBuffer0 = {au8ReceiveBuffer,USB_FUNC_EP0_SIZE,0,0};
00098 usb_buffer_t stcEndpointBuffer1 = {au8ReceiveBuffer,USB_FUNC_EP0_SIZE,0,0};
00099 usb_buffer_t stcEndpointBuffer2 = {au8ReceiveBuffer,USB_FUNC_EP0_SIZE,0,0};
00100 usb_buffer_t stcEndpointBuffer3 = {au8ReceiveBuffer,USB_FUNC_EP0_SIZE,0,0};
00101 usb_buffer_t stcEndpointBuffer4 = {au8ReceiveBuffer,USB_FUNC_EP0_SIZE,0,0};
00102 usb_buffer_t stcEndpointBuffer5 = {au8ReceiveBuffer,USB_FUNC_EP0_SIZE,0,0};
00103
00104 uint8_t au8TransmittBuffer[MAX_RECV_BUFFER];
00105
00106
00107 uint32_t u32TransmissionLength0;
00108
00109 uint8_t flag_sendEP0 = 0;
00110
00111
00112
00113 uint8_t u8UsbStatus = 0;
00114
00115 usb_EP_t aEndpoint[6] =
00116 {
00117 {
00118 EP_STATUS_IDLE,
00119 &EP0DTH0,
00120 &EP0DTL0,
00121 &EP0IS0,
00122 &EP0C0,
00123 USB_FUNC_EP0_SIZE,
00124 &stcEndpointBuffer0,
00125 DummyEventHandler,
00126 {0,&DISEL0,(volatile uint8_t*)0x00100}
00127 },
00128 {
00129 EP_STATUS_IDLE,
00130 &EP1DTH0,
00131 &EP1DTL0,
00132 &EP1S0,
00133 &EP1C0,
00134 0,
00135 &stcEndpointBuffer1,
00136 DummyEventHandler,
00137 {1,&DISEL1,(volatile uint8_t*)0x00108}
00138 },
00139 {
00140 EP_STATUS_IDLE,
00141 &EP2DTH0,
00142 &EP2DTL0,
00143 &EP2S0,
00144 &EP2C0,
00145 0,
00146 &stcEndpointBuffer2,
00147 DummyEventHandler,
00148 {2,&DISEL2,(volatile uint8_t*)0x00110}
00149 },
00150 {
00151 EP_STATUS_IDLE,
00152 &EP3DTH0,
00153 &EP3DTL0,
00154 &EP3S0,
00155 &EP3C0,
00156 0,
00157 &stcEndpointBuffer3,
00158 DummyEventHandler,
00159 {3,&DISEL3,(volatile uint8_t*)0x00118}
00160 },
00161 {
00162 EP_STATUS_IDLE,
00163 &EP4DTH0,
00164 &EP4DTL0,
00165 &EP4S0,
00166 &EP4C0,
00167 0,
00168 &stcEndpointBuffer4,
00169 DummyEventHandler,
00170 {4,&DISEL4,(volatile uint8_t*)0x00120}
00171 },
00172 {
00173 EP_STATUS_IDLE,
00174 &EP5DTH0,
00175 &EP5DTL0,
00176 &EP5S0,
00177 &EP5C0,
00178 0,
00179 &stcEndpointBuffer5,
00180 DummyEventHandler,
00181 {5,&DISEL5,(volatile uint8_t*)0x00128}
00182 },
00183 };
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 #if (USB_USE_DMA == 1)
00195
00201 void UsbFunction_ChangeDmaSettings(uint8_t u8EndpointNumber, volatile uint8_t * DISELn,volatile uint8_t * pu8DmaDescriptor)
00202 {
00203 aEndpoint[u8EndpointNumber].stcDmaHandler.DISELn = DISELn;
00204 aEndpoint[u8EndpointNumber].stcDmaHandler.pu8DmaDescriptor = pu8DmaDescriptor;
00205 }
00206
00222 void SetupDma(uint8_t u8EndpointNumber,uint8_t u8InterruptNumber, uint16_t u16DataSize)
00223 {
00224 uint32_t u32StartAt = (uint32_t)aEndpoint[u8EndpointNumber].pstcEndpointBuffer->pu8Buffer;
00225 uint8_t u8Direction = DMA_DIR_HOST_OUT;
00226
00227 CLEAR_MASK(EP_STATUS(u8EndpointNumber),MASK_EPS_DRQIE);
00228
00229 if ((EP_CONTROL(u8EndpointNumber) & MASK_EPC_DIR))
00230 {
00231 u8Direction = DMA_DIR_HOST_IN;
00232 }
00233 u32StartAt += aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position;
00234
00235 if (u16DataSize == 0)
00236 {
00237 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position += 0x10000;
00238 }
00239 else
00240 {
00241 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position += u16DataSize;
00242 }
00243
00244 dbg ("DMA |->DMA Setup\n");
00245 dbgval("DMA |->for Endpoint: ", u8EndpointNumber);
00246 dbgval("DMA |->ICR: ", u8InterruptNumber);
00247 dbgval("DMA |->Size: ", u16DataSize);
00248 dbgval("DMA |->StartAt: ", u32StartAt);
00249
00250 aEndpoint[u8EndpointNumber].status |= EP_STATUS_DMA_TRANSFER;
00251 DSR &= ~(1 << aEndpoint[u8EndpointNumber].stcDmaHandler.u8DmaNumber);
00252 DER &= ~(1 << aEndpoint[u8EndpointNumber].stcDmaHandler.u8DmaNumber);
00253 *(aEndpoint[u8EndpointNumber].stcDmaHandler.DISELn) = u8InterruptNumber;
00254 *(uint16_t*)(aEndpoint[u8EndpointNumber].stcDmaHandler.pu8DmaDescriptor + DMA_OFFSET_DCTL) = u16DataSize;
00255
00256
00257 *(aEndpoint[u8EndpointNumber].stcDmaHandler.pu8DmaDescriptor + DMA_OFFSET_IOAH) = (uint8_t)aEndpoint[u8EndpointNumber].EPnDTL >> 8;
00258 *(aEndpoint[u8EndpointNumber].stcDmaHandler.pu8DmaDescriptor + DMA_OFFSET_IOAL) = (uint8_t)aEndpoint[u8EndpointNumber].EPnDTL & 0xFF;
00259 *(aEndpoint[u8EndpointNumber].stcDmaHandler.pu8DmaDescriptor + DMA_OFFSET_DMACS) = 0x18 + (u8Direction << 1);
00260 *(aEndpoint[u8EndpointNumber].stcDmaHandler.pu8DmaDescriptor + DMA_OFFSET_BAPH) = (uint8_t)(u32StartAt >> 16);
00261 *(aEndpoint[u8EndpointNumber].stcDmaHandler.pu8DmaDescriptor + DMA_OFFSET_BAPM) = (uint8_t)(u32StartAt >> 8);
00262 *(aEndpoint[u8EndpointNumber].stcDmaHandler.pu8DmaDescriptor + DMA_OFFSET_BAPL) = (uint8_t)(u32StartAt & 0xFF);
00263 DSR &= ~(1 << aEndpoint[u8EndpointNumber].stcDmaHandler.u8DmaNumber);
00264 SET_MASK(EP_CONTROL(u8EndpointNumber),MASK_EPC_DMAE);
00265
00266 DER |= (1 << aEndpoint[u8EndpointNumber].stcDmaHandler.u8DmaNumber);
00267 SET_MASK(EP_STATUS(u8EndpointNumber),MASK_EPS_DRQIE);
00268 }
00269
00281 void ReleaseDma(uint8_t u8EndpointNumber)
00282 {
00283 uint8_t u8Direction = DMA_DIR_HOST_OUT;
00284
00285 DER &= ~(1 << aEndpoint[u8EndpointNumber].stcDmaHandler.u8DmaNumber);
00286 DSR &= ~(1 << aEndpoint[u8EndpointNumber].stcDmaHandler.u8DmaNumber);
00287
00288 if ((EP_CONTROL(u8EndpointNumber) & MASK_EPC_DIR))
00289 {
00290 u8Direction = DMA_DIR_HOST_IN;
00291 }
00292
00293 dbg("DMA |->DMA Clear Setup\n");
00294 dbg("DMA |->for Endpoint: ");
00295 dbghex(u8EndpointNumber);
00296 dbg("\n");
00297 aEndpoint[u8EndpointNumber].status &= ~EP_STATUS_DMA_TRANSFER;
00298
00299 CLEAR_MASK(EP_STATUS(u8EndpointNumber),MASK_EPS_DRQIE);
00300 CLEAR_MASK(EP_CONTROL(u8EndpointNumber),MASK_EPC_NULE);
00301 CLEAR_MASK(EP_CONTROL(u8EndpointNumber),MASK_EPC_DMAE);
00302
00303 if (u8Direction == DMA_DIR_HOST_OUT)
00304 {
00305 SET_MASK(EP_STATUS(u8EndpointNumber),MASK_EPS_DRQIE);
00306 }
00307 }
00308
00322 void UsbFunction_EnableReceiveDma(uint8_t u8EndpointNumber, uint32_t u32ReceiveSize)
00323 {
00324 ReleaseDma(u8EndpointNumber);
00325 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position = 0;
00326 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32DataLength = u32ReceiveSize;
00327 aEndpoint[u8EndpointNumber].status |= EP_STATUS_DMA_ENABLED;
00328 dbg ("\nDMA UsbFunction_EnableReceiveDma()\n");
00329 dbgval("DMA |->u8EndpointNumber",u8EndpointNumber);
00330 dbgval("DMA |->u32ReceiveSize",u32ReceiveSize);
00331 SendReceiveBufferViaDma(u8EndpointNumber);
00332 }
00333
00334 void SendReceiveBufferViaDma(uint8_t u8EndpointNumber)
00335 {
00336 volatile uint16_t u16Size;
00337 uint8_t u8Direction = DMA_DIR_HOST_OUT;
00338 if ((EP_CONTROL(u8EndpointNumber) & MASK_EPC_DIR))
00339 {
00340 u8Direction = DMA_DIR_HOST_IN;
00341 }
00342
00343 dbg ("\nDMA SendReceiveBufferViaDma()\n");
00344
00345 if (u8Direction == DMA_DIR_HOST_OUT)
00346 {
00347 CLEAR_MASK(EP_STATUS(u8EndpointNumber),MASK_EPS_DRQ);
00348 }
00349
00350 ReleaseDma(u8EndpointNumber);
00351
00352 #if (debug_enabled == 1) //DEBUG
00353 dbg ("DMA |->New DMA Package\n");
00354 if (u8Direction == DMA_DIR_HOST_OUT)
00355 {
00356 dbg ("DMA |->Type: MCU receive\n");
00357 }
00358 else
00359 {
00360 dbg ("DMA |->Type: MCU receive\n");
00361 }
00362 #endif //DEBUG
00363
00364 dbgval("DMA |->Position: ", aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position);
00365 dbgval("DMA |->Size: ", aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32DataLength);
00366
00367
00368 if ((aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position) >= (aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32DataLength))
00369 {
00370 if (u8Direction == DMA_DIR_HOST_OUT)
00371 {
00372 dbg("DMA |->Received end...\n");
00373 aEndpoint[u8EndpointNumber].status &= ~EP_STATUS_DMA_ENABLED;
00374 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position = 0;
00375 }
00376 else
00377 {
00378 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position = aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32DataLength;
00379 dbg("TX |->Transfer finished...\n");
00380 if ((aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32DataLength % aEndpoint[u8EndpointNumber].u16Size) == 0)
00381 {
00382 dbg("sending 0 byte\n");
00383 UsbFunction_SendData(u8EndpointNumber,aEndpoint[u8EndpointNumber].pstcEndpointBuffer->pu8Buffer,aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32DataLength,USB_SENDING_MODE_INTERRUPT);
00384 }
00385 }
00386 return;
00387 }
00388
00389
00390 if ((aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position) >= (aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32DataLength - 1))
00391 {
00392 if (u8Direction == DMA_DIR_HOST_OUT)
00393 {
00394 dbg("DMA |->receiving last byte\n");
00395 aEndpoint[u8EndpointNumber].status &= ~EP_STATUS_DMA_ENABLED;
00396 }
00397 else
00398 {
00399 dbg("DMA |->sending last byte\n");
00400 UsbFunction_SendData(u8EndpointNumber,aEndpoint[u8EndpointNumber].pstcEndpointBuffer->pu8Buffer,aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32DataLength,USB_SENDING_MODE_INTERRUPT);
00401 }
00402 return;
00403 }
00404
00405
00406 dbg("DMA |->Creating new DMA package\n");
00407
00408
00409 if ((aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32DataLength - aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position) <= 0x10000)
00410 {
00411
00412 dbg("DMA >>>Package <= 64KByte\n");
00413
00414
00415 u16Size = (uint16_t)(aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32DataLength - aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position);
00416
00417
00418 if (u16Size > aEndpoint[u8EndpointNumber].u16Size)
00419 {
00420
00421
00422 u16Size = u16Size / aEndpoint[u8EndpointNumber].u16Size;
00423 u16Size = u16Size * aEndpoint[u8EndpointNumber].u16Size;
00424 }
00425 else
00426 {
00427
00428
00429 u16Size = u16Size / 2;
00430 u16Size = u16Size * 2;
00431 }
00432 SetupDma(u8EndpointNumber,u8EndpointNumber + 113,u16Size);
00433 }
00434 else
00435 {
00436
00437 dbg("DMA >>>Package > 64KByte\n");
00438 SetupDma(u8EndpointNumber,u8EndpointNumber + 113, 0);
00439 }
00440 }
00441 #endif
00442
00448 uint8_t UsbFunction_GetStatus(void) {
00449 return u8UsbStatus;
00450 }
00451
00460 uint8_t UsbFunction_GetEndpointStatus(uint8_t u8EndpointNumber) {
00461 return aEndpoint[u8EndpointNumber].status;
00462 }
00463
00471 boolean_t UsbFunction_AllEndpointsIdle(void)
00472 {
00473 uint8_t u8EndpointNumber;
00474 for(u8EndpointNumber=1;u8EndpointNumber<6;u8EndpointNumber++)
00475 {
00476 if ((aEndpoint[u8EndpointNumber].status & 0xF8) > 0)
00477 {
00478 dbg("\n\nNOT IDLE ENDPOINT");
00479 dbgval("EP No.:", u8EndpointNumber);
00480 dbgval("status:", aEndpoint[u8EndpointNumber].status);
00481
00482 return FALSE;
00483 }
00484 }
00485 return TRUE;
00486 }
00487
00488
00489
00515 void UsbFunction_SetEventHandler(uint8_t u8Target, uint8_t u8Event, uint8_t u8Options, uint8_t (* handler)(usb_event_t * stcEvent))
00516 {
00517 switch (u8Target)
00518 {
00519 case USB_EVENT_TARGET_VENDOR:
00520 if (((u8Event & USB_EVENT_RECEIVE) != 0) || ((u8Event & USB_EVENT_SENT) != 0))
00521 {
00522 aEndpoint[u8Options].handler = handler;
00523 }
00524 if ((u8Event & USB_EVENT_CLASSREQUEST) != 0)
00525 {
00526 pu8HandlerClassEvent = handler;
00527 }
00528 break;
00529 case USB_EVENT_TARGET_MAINAPP:
00530 if (((u8Event & USB_EVENT_CONNECT) != 0) || ((u8Event & USB_EVENT_DISCONNECT) != 0))
00531 {
00532 pu8HandlerStatusEvent = handler;
00533 }
00534 break;
00535 case USB_EVENT_TARGET_GENERAL:
00536 if (((u8Event & USB_EVENT_CONNECT) != 0) || ((u8Event & USB_EVENT_DISCONNECT) != 0))
00537 {
00538 pu8HandlerBeforeStatusChange = handler;
00539 }
00540 break;
00541 default:
00542 break;
00543 }
00544 }
00545
00555 void UsbFunction_EnableSOF(void)
00556 {
00557 UDCIE0_SOFIE = 1;
00558 }
00559
00568 void UsbFunction_DisableSOF(void)
00569 {
00570 UDCIE0_SOFIE = 0;
00571 }
00572
00582 void UsbFunction_SetStartOfFrameHandler(void(* handler)(uint16_t u16FrameNumber))
00583 {
00584 SOFHandler = handler;
00585 }
00586
00595 void UsbFunction_ClearStartOfFrameHandler(void)
00596 {
00597 SOFHandler = 0;
00598 }
00599
00612 void UsbFunction_SetCustomEndpointBuffer(uint8_t u8Endpoint, usb_buffer_t * pstcEndpointBuffer)
00613 {
00614 aEndpoint[u8Endpoint].pstcEndpointBuffer = pstcEndpointBuffer;
00615 }
00616
00617
00625 static void SetupEndpoints(void)
00626 {
00627 uint32_t i;
00628 uint8_t u8EndpointNumber;
00629 EP0C0 = USB_FUNC_EP0_SIZE;
00630 aEndpoint[0].u16Size = USB_FUNC_EP0_SIZE;
00631 DisableEndpoints();
00632 for(i=0;i<sizeof(au8ConfigDescriptor);i++)
00633 {
00634 if (au8ConfigDescriptor[i+1] == USBDESCR_ENDPOINT)
00635 {
00636 u8EndpointNumber = au8ConfigDescriptor[i+2] & 0x0F;
00637
00638 EP_CONTROL(u8EndpointNumber) = au8ConfigDescriptor[i+4] | (au8ConfigDescriptor[i+5] << 8);
00639 aEndpoint[u8EndpointNumber].u16Size = au8ConfigDescriptor[i+4] | (au8ConfigDescriptor[i+5] << 8);
00640
00641 aEndpoint[u8EndpointNumber].status |= EP_STATUS_CONFIGURED;
00642
00643
00644 dbg("EP ");
00645 dbghex(u8EndpointNumber);
00646 dbg("\n Dir: ");
00647
00648
00649 if (au8ConfigDescriptor[i+2] & 0x80)
00650 {
00651 dbg("IN(PC)\n Type: ");
00652 SET_MASK(EP_CONTROL(u8EndpointNumber),MASK_EPC_DIR);
00653 }
00654 #if (debug_enabled == 1)
00655 else
00656 {
00657 dbg("OUT(PC)\n Type: ");
00658 }
00659 #endif
00660
00661 EP_CONTROL(u8EndpointNumber) |= (au8ConfigDescriptor[i+3] & 0x03) << 13;
00662
00663 #if (debug_enabled == 1) //DEBUG
00664 switch(au8ConfigDescriptor[i+3] & 0x03)
00665 {
00666 case 0x00:
00667 dbg("Control\n");
00668 break;
00669 case 0x01:
00670 dbg("Isochronous\n");
00671 break;
00672 case 0x02:
00673 dbg("Bulk\n");
00674 break;
00675 case 0x03:
00676 dbg("Interrupt\n");
00677 break;
00678 default:
00679 break;
00680 }
00681 #endif //DEBUG
00682 dbg(" Config: ");
00683 dbghex(EP_CONTROL(u8EndpointNumber));
00684 dbg("\n");
00685 }
00686
00687 i += au8ConfigDescriptor[i] - 1;
00688 }
00689 }
00690
00697 static void EnableEndpoints(void)
00698 {
00699 uint8_t i;
00700 for(i=1;i<6;i++)
00701 {
00702 if (aEndpoint[i].status != 0)
00703 {
00704 dbg(" |->Endpoint ");
00705 dbghex(i);
00706 dbg(" enabled ");
00707
00708 aEndpoint[i].status |= EP_STATUS_ENABLED;
00709 SET_MASK(EP_CONTROL(i),MASK_EPC_EPEN);
00710 if (!IS_SET(EP_CONTROL(i),MASK_EPC_DIR))
00711 {
00712 SET_MASK(EP_STATUS(i),MASK_EPS_DRQIE);
00713 dbg("; input interrupt enabled ");
00714 }
00715 dbg("\n");
00716 }
00717 }
00718 dbg("\n");
00719 }
00720
00727 static void DisableEndpoints(void)
00728 {
00729 uint8_t i;
00730 for(i=1;i<6;i++)
00731 {
00732 if (aEndpoint[i].status != 0)
00733 {
00734 dbg(" |->Endpoint ");
00735 dbghex(i);
00736 dbg(" disabled\n");
00737 aEndpoint[i].status = EP_STATUS_DISABLED;
00738 CLEAR_MASK(EP_CONTROL(i),MASK_EPC_EPEN);
00739 CLEAR_MASK(EP_STATUS(i),MASK_EPS_DRQIE);
00740 }
00741 }
00742 dbg("\n");
00743 }
00744
00752 static void ConnectEvent(void) {
00753 usb_event_t stcEvent;
00754 uint8_t i;
00755
00756 CLEAR_USB_STATUS_FLAGS();
00757
00758 dbg("\n\n### UsbFunction: Connect Event Detected ###\n\n");
00759
00760 SET_USB_STATUS_FLAG(USB_STATUS_CONNECTED);
00761
00762 stcEvent.u8UsbStatus = u8UsbStatus;
00763 stcEvent.u8Event = USB_EVENT_CONNECT;
00764
00765 if (pu8HandlerBeforeStatusChange != 0)
00766 {
00767 pu8HandlerBeforeStatusChange(&stcEvent);
00768 }
00769
00770 UDCC0_RST=1;
00771 HCNT0_URST=1;
00772 HCNT0=0;
00773 UDCC0_USTP=0;
00774 UDCC0_RESUM=0;
00775 UDCC0_RFBK=0;
00776
00777 if (au8ConfigDescriptor[7] == USBATTR_SELFPOWER)
00778 {
00779 UDCC0_PWC = 1;
00780 }
00781 else
00782 {
00783 UDCC0_PWC = 0;
00784 }
00785 for(i=0;i<6;i++)
00786 {
00787 aEndpoint[i].pstcEndpointBuffer->u32Position = 0;
00788 aEndpoint[i].pstcEndpointBuffer->u32DataLength = 0;
00789 }
00790 SetupEndpoints();
00791
00792 if( UDCC0_RST )
00793 {
00794 UDCC0_RST=0;
00795 __wait_nop();
00796 __wait_nop();
00797 __wait_nop();
00798 }
00799
00800 EP1S0_BFINI = 0;
00801 EP2S0_BFINI = 0;
00802 EP3S0_BFINI = 0;
00803 EP4S0_BFINI = 0;
00804 EP5S0_BFINI = 0;
00805 EP0OS0_BFINI = 0;
00806 EP0IS0_BFINI = 0;
00807
00808 UDCC0_HCONX = 0;
00809
00810 #ifdef USB_FR80
00811 PDRH_PDRH2 = 0;
00812 #endif
00813
00814
00815 UDCIE0 = 0x08;
00816 EP0IS0_DRQIIE = 0;
00817 EP0OS0_DRQOIE = 1;
00818
00819 UDCIE0_CONFIE = 1;
00820 UDCIE0_SUSPIE = 1;
00821 UDCIE0_WKUPIE = 1;
00822
00823
00824 if (pu8HandlerClassEvent != 0)
00825 {
00826 pu8HandlerClassEvent(&stcEvent);
00827 }
00828
00829 if (pu8HandlerStatusEvent != 0)
00830 {
00831 pu8HandlerStatusEvent(&stcEvent);
00832 }
00833 }
00834
00842 static void DisconnectEvent(void) {
00843 usb_event_t stcEvent;
00844
00845 CLEAR_USB_STATUS_FLAGS();
00846 dbg("\n\n### UsbFunction: Disconnect Event Detected ###\n\n");
00847
00848 stcEvent.u8UsbStatus = u8UsbStatus;
00849 stcEvent.u8Event = USB_EVENT_DISCONNECT;
00850
00851 if (pu8HandlerBeforeStatusChange != 0)
00852 {
00853 pu8HandlerBeforeStatusChange(&stcEvent);
00854 }
00855
00856 DisableEndpoints();
00857
00858 UDCC0_HCONX = 1;
00859
00860 #ifdef USB_FR80
00861 PDRH_PDRH2 = 1;
00862 #endif
00863
00864 UDCIE0 = 0x00;
00865 EP0IS0_DRQIIE = 0;
00866 EP0OS0_DRQOIE = 0;
00867
00868 UDCC0_RST=1;
00869
00870 if (pu8HandlerClassEvent != 0)
00871 {
00872 pu8HandlerClassEvent(&stcEvent);
00873 }
00874
00875 if (pu8HandlerStatusEvent != 0)
00876 {
00877 pu8HandlerStatusEvent(&stcEvent);
00878 }
00879 }
00880
00887 void UsbFunction_Initialize(boolean_t bAutoConnect)
00888 {
00889
00890 uint16_t cnt;
00891
00892 #ifdef USB_FR80
00893 PDRH_PDRH2 = 1;
00894 PDRH_PDRH3 = 0;
00895 PORT1_DDRH_DDRH2 = 1;
00896 PORT1_DDRH_DDRH3 = 0;
00897 USBEN_USBEN = 1;
00898 #endif
00899
00900 for(cnt=0; cnt<(USB_FUNC_EP0_SIZE); cnt++)
00901 {
00902 au8ReceiveBuffer[cnt] = 0xFF;
00903 }
00904
00905 #ifdef USB_FR80
00906 if (bAutoConnect == TRUE)
00907 {
00908 PORT3_EPFR28_INT3E = 1;
00909 ENIR0_EN3 = 0;
00910 ELVR0_LBLA3 = 0;
00911 EIRR0_ER3 = 0;
00912 if (PDRH_PDRH3 == 1) {
00913 ELVR0_LBLA3 = 0;
00914 function_connect();
00915 } else
00916 {
00917 ELVR0_LBLA3 = 1;
00918 function_disconnect();
00919 }
00920 if (bAutoConnect == TRUE)
00921 {
00922 EIRR0_ER3 = 0;
00923 ENIR0_EN3 = 1;
00924 }
00925 }
00926 #else
00927 REG_PIER_VBUS |= VBUS_MASK;
00928 REG_DDR_VBUS &= ~VBUS_MASK;
00929 #ifdef RELOCATE_VBUS
00930 RELOCATE_VBUS;
00931 #endif
00932 if (bAutoConnect == TRUE)
00933 {
00934 ENIR0_EN2 = 1;
00935 ELVR0_LA2 = 1;
00936 ELVR0_LB2 = 0;
00937 }
00938 #endif
00939 }
00940
00949 boolean_t UsbFunction_Connect(boolean_t bAutoConnect)
00950 {
00951 if (bAutoConnect == TRUE)
00952 {
00953 UsbFunction_Initialize(TRUE);
00954 return TRUE;
00955 }
00956 if ((PORT_VBUS & VBUS_MASK) > 0)
00957 {
00958 ConnectEvent();
00959 return TRUE;
00960 }
00961 else
00962 {
00963 return FALSE;
00964 }
00965 }
00966
00975 boolean_t UsbFunction_Disconnect(boolean_t bForceDisconnect)
00976 {
00977 uint32_t u32Timeout = 1000000;
00978 if (bForceDisconnect == TRUE)
00979 {
00980 DisconnectEvent();
00981 return TRUE;
00982 }
00983 while((u32Timeout > 0) && (UsbFunction_AllEndpointsIdle() == FALSE))
00984 {
00985 u32Timeout--;
00986 }
00987 if (u32Timeout == 0)
00988 {
00989 return FALSE;
00990 }
00991 DisconnectEvent();
00992 return TRUE;
00993 }
01004 void UsbFunction_SendData(uint8_t u8EndpointNumber,puint8_t buffer, uint32_t buffersize, uint8_t u8PollingMode)
01005 {
01006 volatile uint8_t * pu8EndpointDataRegHigh;
01007 volatile uint8_t * pu8EndpointDataRegLow;
01008 volatile uint16_t * pu16EndpointStatusReg;
01009 uint32_t i = 0;
01010 uint16_t u16EndpointSize = aEndpoint[u8EndpointNumber].u16Size;
01011 volatile uint16_t u16Size = 0;
01012 dbg("\n\nTX> UsbFunction_SendData()\n");
01013 aEndpoint[u8EndpointNumber].status |= EP_STATUS_SENDING;
01014 if (aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position == 0)
01015 {
01016 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->pu8Buffer = buffer;
01017 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32DataLength = buffersize;
01018
01019 dbg("TX> Beginning new package");
01020 dbg("\nTX |->Endpoint: ");
01021 dbghex(u8EndpointNumber);
01022 dbg("\nTX |->Endpoint Size: ");
01023 dbghex(u16EndpointSize);
01024 dbg("\nTX |->Size: ");
01025 dbghex(aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32DataLength);
01026 dbg("\nTX |->Start At: ");
01027 dbghex(aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position);
01028 dbg("\n");
01029 }
01030 else
01031 {
01032 dbg("TX> Resuming Transfer: ");
01033 dbg("\nTX |->Endpoint: ");
01034 dbghex(u8EndpointNumber);
01035 dbg("\nTX |->Start At: ");
01036 dbghex(aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position);
01037 dbg("\n");
01038 }
01039 #if (debug_enabled == 1)
01040 dbg("TX |->TYPE: ");
01041 switch (u8PollingMode)
01042 {
01043 case USB_SENDING_MODE_INTERRUPT:
01044 dbg("INTERRUPT\n");
01045 break;
01046 case USB_SENDING_MODE_DMA:
01047 dbg("DMA\n");
01048 break;
01049 case USB_SENDING_MODE_POLLED:
01050 dbg("POLLED\n");
01051 break;
01052 default:
01053 dbg("unknown\n");
01054 break;
01055 }
01056 #endif
01057
01058 #if (USB_USE_DMA == 1)
01059 if (u8PollingMode == USB_SENDING_MODE_DMA)
01060 {
01061 SendReceiveBufferViaDma(u8EndpointNumber);
01062 return;
01063 }
01064 #endif
01065
01066 pu8EndpointDataRegHigh = aEndpoint[u8EndpointNumber].EPnDTH;
01067 pu8EndpointDataRegLow = aEndpoint[u8EndpointNumber].EPnDTL;
01068 pu16EndpointStatusReg = aEndpoint[u8EndpointNumber].EPnS;
01069
01070 if (u8EndpointNumber == 0)
01071 {
01072 EP0IS0_DRQIIE = 1;
01073 }
01074 while(pu16EndpointStatusReg_DRQ == 0)
01075 {
01076 __wait_nop();
01077 }
01078
01079 for(i = aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position; i<aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32DataLength; i++)
01080 {
01081 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position = i;
01082 if((i & 0x01)==1)
01083 {
01084 *pu8EndpointDataRegHigh = buffer[i];
01085 }
01086 else
01087 {
01088 *pu8EndpointDataRegLow = buffer[i];
01089 }
01090
01091 #if (use_printbuffer == 1)
01092 dbghex(buffer[i]);
01093 dbg(" ");
01094 #endif
01095
01096 if ((i + 1) % u16EndpointSize == 0)
01097 {
01098 #if (use_printbuffer == 1)
01099 dbg("T ");
01100 #endif
01101
01102 if (u8PollingMode == USB_SENDING_MODE_INTERRUPT)
01103 {
01104 dbg("TX |->Break: ");
01105 dbghex(aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position);
01106 dbg("\n");
01107 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position += 1;
01108 SET_pu16EndpointStatusReg_DRQ(0);
01109 SET_MASK(*pu16EndpointStatusReg,MASK_EPS_DRQIE);
01110 return;
01111 }
01112 SET_pu16EndpointStatusReg_DRQ(0);
01113 while(pu16EndpointStatusReg_DRQ == 0)
01114 {
01115 __wait_nop();
01116 }
01117 }
01118 }
01119
01120 if (((i % u16EndpointSize != 0) || (i == 0)))
01121 {
01122 #if (use_printbuffer == 1)
01123 dbg("T ");
01124 #endif
01125
01126 if (u8PollingMode == USB_SENDING_MODE_INTERRUPT)
01127 {
01128 dbg("TX |->Break: ");
01129 dbghex(aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position);
01130 dbg("\n");
01131 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position += 1;
01132 SET_pu16EndpointStatusReg_DRQ(0);
01133 SET_MASK(*pu16EndpointStatusReg,MASK_EPS_DRQIE);
01134 return;
01135 }
01136 SET_pu16EndpointStatusReg_DRQ(0);
01137 while(pu16EndpointStatusReg_DRQ == 0) __wait_nop();
01138
01139 }
01140
01141 if (((buffersize % u16EndpointSize) == 0) || (buffersize == 0) || (aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position > aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32DataLength))
01142 {
01143
01144 dbg("Sending null terminated end\n");
01145 if (u8PollingMode == USB_SENDING_MODE_INTERRUPT)
01146 {
01147 dbg("TX |->Break: ");
01148 dbghex(aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position);
01149 dbg("\n");
01150 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position += 1;
01151 SET_pu16EndpointStatusReg_DRQ(0);
01152 SET_MASK(*pu16EndpointStatusReg,MASK_EPS_DRQIE);
01153 return;
01154 }
01155 SET_pu16EndpointStatusReg_DRQ(0);
01156 while(pu16EndpointStatusReg_DRQ == 0) __wait_nop();
01157 }
01158
01159
01160 #if (use_printbuffer == 1)
01161 dbg("\n");
01162 #endif
01163
01164 if (u8EndpointNumber == 0)
01165 {
01166 EP0IS0_DRQIIE = 0;
01167 }
01168 aEndpoint[u8EndpointNumber].status &= ~EP_STATUS_SENDING;
01169 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position = 0;
01170 }
01171
01181 void UsbFunction_SetDataToSendEndpoint0(uint8_t *buffer, uint32_t size)
01182 {
01183 uint32_t i;
01184 flag_sendEP0 = 1;
01185 for(i=0;i<size;i++)
01186 {
01187 au8TransmittBuffer[i] = *buffer++;
01188 }
01189 u32TransmissionLength0 = size;
01190
01191 }
01192
01199 void UsbFunction_Stall(uint8_t u8EndpointNumber)
01200 {
01201 SET_MASK(*aEndpoint[u8EndpointNumber].EPnC,MASK_EPC_STAL);
01202 dbgval("Stall EP: ", u8EndpointNumber);
01203 }
01204
01213 void UsbFunction_EnableWakeup(boolean_t bEnableDisable)
01214 {
01215 if (bEnableDisable)
01216 {
01217 UDCC0_RESUM=1;
01218 }
01219 else
01220 {
01221 UDCC0_RESUM=0;
01222 }
01223 }
01230 void UsbFunction_StallRemove(uint8_t u8EndpointNumber) {
01231 if (u8EndpointNumber == 0)
01232 {
01233 if (EP0C0_STAL == 1)
01234 {
01235 EP0C0_STAL = 0;
01236
01237 if (EP0OS0_BFINI == 0)
01238 {
01239 EP0OS0_BFINI = 1;
01240 EP0OS0_BFINI = 0;
01241 }
01242
01243 if (EP0IS0_BFINI == 0)
01244 {
01245 EP0IS0_BFINI = 1;
01246 EP0IS0_BFINI = 0;
01247 }
01248 }
01249 }
01250 CLEAR_MASK(*aEndpoint[u8EndpointNumber].EPnC,MASK_EPC_STAL);
01251 dbgval("Unstall EP: ", u8EndpointNumber);
01252 }
01253
01260 static void UsbReceiveSetup(void)
01261 {
01262 uint8_t setup_handled = 0;
01263 uint16_t i;
01264 uint32_t u32ReceiveSize;
01265
01266
01267 u32ReceiveSize = EP0OS0_SIZE;
01268 for(i=0; i<u32ReceiveSize; i++){
01269 if((i & 0x01) == 1){
01270 au8ReceiveBuffer[i] = EP0DTH0;
01271 }
01272 else{
01273 au8ReceiveBuffer[i] = EP0DTL0;
01274 }
01275 }
01276
01277 setup_handled = UsbRequestDecode(au8ReceiveBuffer,u32ReceiveSize);
01278
01279 EP0OS0_DRQO = 0;
01280 #ifdef USB_FR80
01281 EP0OS0_BFINI = 0;
01282 #endif
01283 UDCS0_SETP = 0;
01284
01285
01286
01287
01288
01289
01290 if ((au8ReceiveBuffer[0] & 0x60) == USB_DEVREQ_CLSTYPE)
01291 {
01292 if(au8ReceiveBuffer[1] == USB_DEVREQ_SET_IDLE)
01293 {
01294
01295 EP0IS0_DRQI = 0;
01296 }
01297 }
01298 if (flag_sendEP0) {
01299 sendEP0((puint8_t)au8TransmittBuffer,u32TransmissionLength0);
01300
01301 flag_sendEP0 = 0;
01302 }
01303 if (setup_handled == 1) {
01304
01305 } else {
01306 UsbFunction_Stall(USB_EP0);
01307 }
01308 }
01309
01318 static void SendStringDescriptor(usb_request_t * ptRequest)
01319 {
01320 uint8_t u8DataToSend[255];
01321 uint8_t u8Size = 0;
01322 switch ((ptRequest->wValue) & 0xFF)
01323 {
01324 case 0:
01325
01326 u8DataToSend[0] = 0x04;
01327 u8DataToSend[1] = 0x03;
01328 u8DataToSend[2] = 0x09;
01329 u8DataToSend[3] = 0x04;
01330 break;
01331 case 1:
01332
01333 u8DataToSend[0] = 2;
01334 u8DataToSend[1] = 0x03;
01335 for(u8Size=0;(pcManufacturerStringDescriptor[u8Size] != 0);u8Size++)
01336 {
01337 u8DataToSend[2 + u8Size * 2] = pcManufacturerStringDescriptor[u8Size];
01338 u8DataToSend[3 + u8Size * 2] = 0;
01339 }
01340 u8DataToSend[0] = u8DataToSend[0] + 2 * u8Size;
01341 break;
01342 case 2:
01343
01344 u8DataToSend[0] = 2;
01345 u8DataToSend[1] = 0x03;
01346 for(u8Size=0;(pcProductStringDescriptor[u8Size] != 0);u8Size++)
01347 {
01348 u8DataToSend[2 + u8Size * 2] = pcProductStringDescriptor[u8Size];
01349 u8DataToSend[3 + u8Size * 2] = 0;
01350 }
01351 u8DataToSend[0] = u8DataToSend[0] + 2 * u8Size;
01352 break;
01353 case 3:
01354
01355 u8DataToSend[0] = 2;
01356 u8DataToSend[1] = 0x03;
01357 for(u8Size=0;(pcSerialnumberStringDescriptor[u8Size] != 0);u8Size++)
01358 {
01359 u8DataToSend[2 + u8Size * 2] = pcSerialnumberStringDescriptor[u8Size];
01360 u8DataToSend[3 + u8Size * 2] = 0;
01361 }
01362 u8DataToSend[0] = u8DataToSend[0] + 2 * u8Size;
01363 break;
01364 default:
01365 u8DataToSend[0] = 0;
01366 break;
01367 }
01368 UsbFunction_SetDataToSendEndpoint0(u8DataToSend,u8DataToSend[0]);
01369 }
01370
01379 static uint8_t UsbGetDescriptor(usb_request_t * ptRequest)
01380 {
01381 uint16_t rsp_size = 0;
01382
01383 switch (((ptRequest->wValue) >> 8) & 0x00FF)
01384 {
01385 case USB_REQUEST_DEVICE_DESCRIPTOR:
01386 SET_USB_STATUS_FLAG(USB_STATUS_DEVDESC);
01387 if(ptRequest->wLength > USB_DEVDESC_SIZE){
01388 rsp_size = USB_DEVDESC_SIZE;
01389 }
01390 else{
01391 rsp_size = ptRequest->wLength;
01392 }
01393 if (!rsp_size) {
01394 dbg("###### WARNING: WRONG LENGTH\n #####");
01395 }
01396 if((ptRequest->Type == 0) && (EP0IS0_DRQI == 1) && (rsp_size))
01397 {
01398 UsbFunction_SetDataToSendEndpoint0((uint8_t*)au8DeviceDescriptor,rsp_size);
01399 }
01400 return 1;
01401 case USB_REQUEST_CONFIG_DESCRIPTOR:
01402 SET_USB_STATUS_FLAG(USB_STATUS_CONFDESC);
01403 if (ptRequest->wLength > USB_CNFGDESC_SIZE){
01404 rsp_size = USB_CNFGDESC_SIZE;
01405 }
01406 else
01407 {
01408 rsp_size = ptRequest->wLength;
01409 }
01410 if (!rsp_size)
01411 {
01412 dbg("WARNING: WRONG LENGTH\n");
01413 }
01414 if((ptRequest->Type == 0) && (EP0IS0_DRQI == 1) && (rsp_size))
01415 {
01416 UsbFunction_SetDataToSendEndpoint0((uint8_t*)au8ConfigDescriptor,rsp_size);
01417 }
01418 return 1;
01419 case USB_REQUEST_STRING_DESCRIPTOR:
01420 SendStringDescriptor(ptRequest);
01421 return 1;
01422 case USB_REQUEST_REPORT_DESCRIPTOR:
01423 if((ptRequest->Type == 0) && (EP0IS0_DRQI == 1))
01424 {
01425 UsbFunction_SetDataToSendEndpoint0((uint8_t*)au8ReportDescriptor,USB_RPTDESC_SIZE);
01426 }
01427 return 1;
01428
01429 default:
01430 UsbFunction_Stall(USB_EP0);
01431 break;
01432 }
01433 return 0;
01434 }
01435
01445 static uint8_t UsbRequestDecode(puint8_t buffer,uint32_t size)
01446 {
01447 usb_event_t stcEvent;
01448 usb_request_t stcRequest;
01449 size=size;
01450
01451 stcEvent.Request = &stcRequest;
01452
01453 dbg("\n");
01454 printBuffer(buffer,size);
01455 dbg("\n>USB-Setup:\n");
01456
01457
01458 stcEvent.Request->Direction = buffer[0] & 0x80;
01459 stcEvent.Request->Type = (buffer[0] & 0x60) >> 5;
01460 stcEvent.Request->Target = buffer[0] & 0x1F;
01461 stcEvent.Request->Request = buffer[1];
01462 stcEvent.Request->wValue = (uint16_t)(buffer[3] << 8) | ((uint16_t)(buffer[2] & 0x00FF));
01463 stcEvent.Request->wIndex = (uint16_t)buffer[4] | ((uint16_t)buffer[5] >> 8);
01464 stcEvent.Request->wLength = (uint16_t)((uint8_t)buffer[6]) | (uint16_t)((uint8_t)buffer[7] << 8);
01465
01466 #if (debug_enabled == 1)
01467 DbgUsbRequest(stcEvent.Request);
01468 #endif
01469
01470 if (stcEvent.Request->Type == USB_REQUEST_TYPE_CLASS)
01471 {
01472 stcEvent.u8Event = USB_EVENT_CLASSREQUEST;
01473 if (pu8HandlerClassEvent != 0)
01474 {
01475 if (pu8HandlerClassEvent(&stcEvent))
01476 {
01477 return 1;
01478 }
01479 }
01480 }
01481 switch (stcEvent.Request->Request)
01482 {
01483
01484
01485
01486
01487
01488
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501
01502
01503
01504 case USB_GET_DESCRIPTOR:
01505 #if (debug_enabled == 1)
01506 DbgUsbGetDescriptor(stcEvent.Request);
01507 #endif
01508 return UsbGetDescriptor(stcEvent.Request);
01509 case USB_SET_DESCRIPTOR:
01510 break;
01511 case USB_SYNCH_FRAME:
01512 break;
01513 default:
01514 break;
01515 }
01516 return 0;
01517
01518
01519
01520 }
01521
01522
01529 static void UsbReceiveDataEndpoint0(void)
01530 {
01531 uint16_t i;
01532 uint32_t u32ReceiveSize;
01533
01534 #if (use_printbuffer == 1)
01535 dbg("### USB DATA RECEIVE ###\n length:");
01536 #endif
01537
01538 u32ReceiveSize = EP0OS0_SIZE;
01539
01540 #if (use_printbuffer == 1)
01541 dbghex(u32ReceiveSize);
01542 dbg("\n");
01543 #endif
01544
01545 if (u32ReceiveSize == 0)
01546 {
01547 au8ReceiveBuffer[0] = EP0DTL0;
01548 au8ReceiveBuffer[0] = EP0DTH0;
01549 }
01550 else
01551 {
01552 for(i=0; i<u32ReceiveSize; i++)
01553 {
01554 if((i & 0x01) == 1){
01555 au8ReceiveBuffer[i] = EP0DTH0;
01556 }
01557 else
01558 {
01559 au8ReceiveBuffer[i] = EP0DTL0;
01560 }
01561 }
01562 }
01563
01564 EP0OS0_DRQO = 0;
01565
01566 #ifdef USB_FR80
01567 EP0OS0_BFINI = 0;
01568 #endif
01569 #if (use_printbuffer == 1)
01570 if (u32ReceiveSize)
01571 {
01572 printBuffer(au8ReceiveBuffer,(uint32_t)u32ReceiveSize);
01573 }
01574 #endif
01575
01576 }
01577
01578
01588 static uint32_t UsbReceiveData(uint8_t u8EndpointNumber, puint8_t buffer) {
01589 uint32_t u32ReceiveSize;
01590 uint32_t u32Offset;
01591 volatile uint8_t * pu8EndpointDataRegHigh;
01592 volatile uint8_t * pu8EndpointDataRegLow;
01593 volatile uint16_t * pu16EndpointStatusReg;
01594 uint32_t i = 0;
01595 aEndpoint[u8EndpointNumber].status |= EP_STATUS_RECEIVING;
01596 u32ReceiveSize = (*aEndpoint[u8EndpointNumber].EPnS) & 0x01FF;
01597 u32Offset = aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position;
01598
01599 pu8EndpointDataRegHigh = aEndpoint[u8EndpointNumber].EPnDTH;
01600 pu8EndpointDataRegLow = aEndpoint[u8EndpointNumber].EPnDTL;
01601 pu16EndpointStatusReg = aEndpoint[u8EndpointNumber].EPnS;
01602
01603
01604 dbg("RX> Receiving Data\n");
01605 dbg("\nRX |->Endpoint: ");
01606 dbghex(u8EndpointNumber);
01607 dbg("\nRX |->Length: ");
01608 dbghex(u32ReceiveSize);
01609 dbg("\n");
01610
01611 if (u32ReceiveSize == 0)
01612 {
01613 buffer[0] = *pu8EndpointDataRegLow;
01614 buffer[0] = *pu8EndpointDataRegHigh;
01615 }
01616 else
01617 {
01618 for(i=u32Offset; i<u32ReceiveSize+u32Offset; i++)
01619 {
01620 if(((i-u32Offset) & 0x01) == 1){
01621 buffer[i] = *pu8EndpointDataRegHigh;
01622 }
01623 else
01624 {
01625 buffer[i] = *pu8EndpointDataRegLow;
01626 }
01627 }
01628 dbg("Status-Reg: ");
01629 dbghex(*pu16EndpointStatusReg);
01630 dbg("\n\n");
01631
01632 if (!IS_SET(*pu16EndpointStatusReg,MASK_EPS_SPK))
01633 {
01634 dbg("Full Package Offset ");
01635 dbghex(i);
01636 dbg("\n\n");
01637 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position = i;
01638
01639 }
01640 else
01641 {
01642 aEndpoint[u8EndpointNumber].pstcEndpointBuffer->u32Position = 0;
01643 CLEAR_MASK(*pu16EndpointStatusReg,MASK_EPS_SPK);
01644 }
01645 }
01646 buffer[i] = 0;
01647
01648 SET_pu16EndpointStatusReg_DRQ(0);
01649
01650
01651
01652
01653
01654
01655
01656
01657
01658
01659
01660
01661
01662 #ifdef USB_FR80
01663 SET_pu16EndpointStatusReg_BFINI(0);
01664 #endif
01665
01666 return i;
01667 }
01668
01669
01670
01671
01672
01673
01684 static uint8_t DummyEventHandler(usb_event_t * pstcEvent) {
01685 pstcEvent = pstcEvent;
01686 #if (debug_enabled == 1)
01687 dbg("Not handled Event:");
01688 switch ((*pstcEvent).u8Event)
01689 {
01690 case USB_EVENT_CONNECT:
01691 dbg("USB_EVENT_CONNECT\n");
01692 break;
01693 case USB_EVENT_DISCONNECT:
01694 dbg("USB_EVENT_DISCONNECT\n");
01695 break;
01696 case USB_EVENT_RECEIVE:
01697 dbg("USB_EVENT_RECEIVE:");
01698 dbg(" Endpoint ");
01699 dbghex((*pstcEvent).u8Endpoint);
01700 CLEAR_MASK(EP_STATUS((*pstcEvent).u8Endpoint),MASK_EPS_DRQO);
01701 puts("\n");
01702 break;
01703 case USB_EVENT_SENT:
01704 dbg("USB_EVENT_SENT\n");
01705 break;
01706 case USB_EVENT_CLASSREQUEST:
01707 dbg("USB_EVENT_CASSREQUEST\n");
01708 break;
01709 default:
01710 dbg("Not known Event...\n");
01711 break;
01712 }
01713 #endif
01714 __wait_nop();
01715 return 1;
01716 }
01717
01718
01719
01720
01728 __interrupt void UsbFunction_EndpointIsr(void)
01729 {
01730 uint32_t u32ReceiveSize;
01731 usb_event_t stcSentReceiveEvent;
01732 stcSentReceiveEvent.u8Endpoint = 0;
01733
01734 for(stcSentReceiveEvent.u8Endpoint = 5;stcSentReceiveEvent.u8Endpoint > 0;stcSentReceiveEvent.u8Endpoint--)
01735 {
01736 if (IS_SET(EP_STATUS(stcSentReceiveEvent.u8Endpoint),MASK_EPS_DRQ) && (IS_CLEARED(EP_CONTROL(stcSentReceiveEvent.u8Endpoint),MASK_EPC_DIR) || (aEndpoint[stcSentReceiveEvent.u8Endpoint].pstcEndpointBuffer->u32Position != 0)))
01737 {
01738 if (IS_CLEARED(EP_CONTROL(stcSentReceiveEvent.u8Endpoint),MASK_EPC_DIR))
01739 {
01740 aEndpoint[stcSentReceiveEvent.u8Endpoint].status |= EP_STATUS_RECEIVING;
01741 stcSentReceiveEvent.u8Event = USB_EVENT_RECEIVE;
01742 }
01743 else
01744 {
01745 aEndpoint[stcSentReceiveEvent.u8Endpoint].status |= EP_STATUS_SENDING;
01746 stcSentReceiveEvent.u8Event = USB_EVENT_SENT;
01747 }
01748 break;
01749 }
01750 }
01751
01752 if (stcSentReceiveEvent.u8Endpoint != 0) {
01753
01754 if (aEndpoint[stcSentReceiveEvent.u8Endpoint].pstcEndpointBuffer != 0)
01755 {
01756 if (stcSentReceiveEvent.u8Event == USB_EVENT_RECEIVE)
01757 {
01758 dbgval("RX >>> ISR Enpoint ", stcSentReceiveEvent.u8Endpoint);
01759 #if (USB_USE_DMA == 1)
01760 if ((aEndpoint[stcSentReceiveEvent.u8Endpoint].status & EP_STATUS_DMA_ENABLED) > 0)
01761 {
01762 dbg("RX >>> DMA enabled\n");
01763 SendReceiveBufferViaDma(stcSentReceiveEvent.u8Endpoint);
01764 u32ReceiveSize = aEndpoint[stcSentReceiveEvent.u8Endpoint].pstcEndpointBuffer->u32DataLength;
01765 }
01766 else
01767 #endif //End USE_DMA
01768 {
01769 u32ReceiveSize = UsbReceiveData(stcSentReceiveEvent.u8Endpoint,aEndpoint[stcSentReceiveEvent.u8Endpoint].pstcEndpointBuffer->pu8Buffer);
01770 }
01771 stcSentReceiveEvent.pstcEndpointBuffer = aEndpoint[stcSentReceiveEvent.u8Endpoint].pstcEndpointBuffer;
01772 stcSentReceiveEvent.u32DataSize = u32ReceiveSize;
01773
01774 if (stcSentReceiveEvent.pstcEndpointBuffer->u32Position == 0)
01775 {
01776 aEndpoint[stcSentReceiveEvent.u8Endpoint].status &= ~EP_STATUS_RECEIVING;
01777 dbg("RX >>> Executing received event\n");
01778 aEndpoint[stcSentReceiveEvent.u8Endpoint].handler(&stcSentReceiveEvent);
01779 }
01780 }
01781 else
01782 {
01783 dbgval("TX >>> ISR Enpoint ", stcSentReceiveEvent.u8Endpoint);
01784 stcSentReceiveEvent.pstcEndpointBuffer = aEndpoint[stcSentReceiveEvent.u8Endpoint].pstcEndpointBuffer;
01785 dbg("TX> Transfer State: ");
01786 dbghex(stcSentReceiveEvent.pstcEndpointBuffer->u32Position);
01787 dbg(" Size: ");
01788 dbghex(stcSentReceiveEvent.pstcEndpointBuffer->u32DataLength);
01789 dbg("\n");
01790 #if (USB_USE_DMA == 1)
01791 if ((aEndpoint[stcSentReceiveEvent.u8Endpoint].status & EP_STATUS_DMA_TRANSFER) > 0)
01792 {
01793 dbg("TX> Transfer Type: DMA\n");
01794 SendReceiveBufferViaDma(stcSentReceiveEvent.u8Endpoint);
01795 if (stcSentReceiveEvent.pstcEndpointBuffer->u32Position == 0)
01796 {
01797 aEndpoint[stcSentReceiveEvent.u8Endpoint].status &= ~EP_STATUS_SENDING;
01798 aEndpoint[stcSentReceiveEvent.u8Endpoint].handler(&stcSentReceiveEvent);
01799 }
01800
01801 }
01802 else
01803 #endif //End USE_DMA
01804 {
01805 if ((stcSentReceiveEvent.pstcEndpointBuffer->u32Position) >= (stcSentReceiveEvent.pstcEndpointBuffer->u32DataLength))
01806 {
01807 if ((stcSentReceiveEvent.pstcEndpointBuffer->u32DataLength % aEndpoint[stcSentReceiveEvent.u8Endpoint].u16Size) == 0)
01808 {
01809 stcSentReceiveEvent.pstcEndpointBuffer->u32Position += 1;
01810 UsbFunction_SendData(stcSentReceiveEvent.u8Endpoint,stcSentReceiveEvent.pstcEndpointBuffer->pu8Buffer,stcSentReceiveEvent.pstcEndpointBuffer->u32DataLength,USB_SENDING_MODE_INTERRUPT);
01811 }
01812 CLEAR_MASK(*aEndpoint[stcSentReceiveEvent.u8Endpoint].EPnS,MASK_EPS_DRQIE);
01813 stcSentReceiveEvent.pstcEndpointBuffer->u32Position = 0;
01814 dbg("TX |->Transfer finished...\n");
01815 aEndpoint[stcSentReceiveEvent.u8Endpoint].status &= ~EP_STATUS_SENDING;
01816 aEndpoint[stcSentReceiveEvent.u8Endpoint].handler(&stcSentReceiveEvent);
01817 }
01818 else
01819 {
01820 dbg("TX |->Sending next data block...\n");
01821 stcSentReceiveEvent.pstcEndpointBuffer = aEndpoint[stcSentReceiveEvent.u8Endpoint].pstcEndpointBuffer;
01822 UsbFunction_SendData(stcSentReceiveEvent.u8Endpoint,stcSentReceiveEvent.pstcEndpointBuffer->pu8Buffer,stcSentReceiveEvent.pstcEndpointBuffer->u32DataLength,USB_SENDING_MODE_INTERRUPT);
01823 }
01824 }
01825 }
01826 }
01827 }
01828 }
01829
01839 __interrupt void UsbFunction_StatusControlIsr(void)
01840 {
01841 usb_event_t stcEvent;
01842
01843 if (EP0C0_STAL == 1)
01844 {
01845 UsbFunction_StallRemove(USB_EP0);
01846 }
01847 if(UDCS0_CONF != 0 ? 1 : 0)
01848 {
01849 UDCS0_CONF = 0;
01850 if (UDCIE0_CONFN == 1)
01851 {
01852 stcEvent.u8Event = USB_EVENT_CONFIGURED;
01853 SET_USB_STATUS_FLAG(USB_STATUS_CONFIGURED);
01854 dbg("\n--> Configure Flag was set\n");
01855 EnableEndpoints();
01856 }
01857 else
01858 {
01859 stcEvent.u8Event = USB_EVENT_DECONFIGURED;
01860 CLEAR_USB_STATUS_FLAG(USB_STATUS_CONFIGURED);
01861 dbg("\n--> Configure Flag was cleared\n");
01862 DisableEndpoints();
01863 }
01864 stcEvent.u8UsbStatus = u8UsbStatus;
01865 if (pu8HandlerClassEvent != 0)
01866 {
01867 pu8HandlerClassEvent(&stcEvent);
01868 }
01869 if (pu8HandlerStatusEvent != 0)
01870 {
01871 pu8HandlerStatusEvent(&stcEvent);
01872 }
01873 }
01874
01875 if(EP0OS0_DRQO == 1)
01876 {
01877 if(UDCS0_SETP == 1)
01878 {
01879 UsbReceiveSetup();
01880 }
01881 else
01882 {
01883 UsbReceiveDataEndpoint0();
01884 }
01885 }
01886
01887 if(UDCS0_SUSP == 1){
01888 dbg("--> SUSP was set\n");
01889 UDCS0_SUSP = 0;
01890 }
01891
01892 if(UDCS0_SOF == 1){
01893 UDCS0_SOF = 0;
01894 u16FrameNumber = TMSP0 & 0x7FF;
01895 if (SOFHandler != 0)
01896 {
01897 SOFHandler(u16FrameNumber);
01898 }
01899 }
01900
01901 if(UDCS0_WKUP == 1){
01902 dbg("--> WKUP was set\n");
01903 UDCS0_WKUP = 0;
01904 }
01905
01906 if(UDCS0_BRST == 1){
01907 dbg("--> Bus reset\n");
01908 UDCS0_BRST = 0;
01909 }
01910
01911 }
01912
01920 __interrupt void UsbFunction_VbusIsr(void)
01921 {
01922 #ifdef USB_FR80
01923 EIRR0_ER3 = 0;
01924 if (PDRH_PDRH3 == 1)
01925 {
01926 ELVR0_LBLA3 = 0;
01927 ConnectEvent();
01928 }
01929 else
01930 {
01931 ELVR0_LBLA3 = 1;
01932 DisconnectEvent();
01933 }
01934 EIRR0_ER3 = 0;
01935 #else
01936 if (ELVR0_LA2 == 1)
01937 {
01938 ELVR0_LA2 = 0;
01939 ConnectEvent();
01940 }
01941 else
01942 {
01943 ELVR0_LA2 = 1;
01944 DisconnectEvent();
01945 }
01946 EIRR0_ER2 = 0;
01947 #endif
01948 }
01949
01950
01951
01952
01953
01954
01955
01956 #if (debug_enabled == 1)
01957 static void DbgUsbRequest(usb_request_t * Request)
01958 {
01959 dbg(" |->USB Request:\n");
01960
01961 dbg(" | |-> wValue: ");
01962 dbghex(Request->wValue);
01963 dbg("\n");
01964
01965 dbg(" | |-> wIndex: ");
01966 dbghex(Request->wIndex);
01967 dbg("\n");
01968
01969 dbg(" | |-> wLength: ");
01970 dbghex(Request->wLength);
01971 dbg("\n");
01972
01973 dbg(" | |-> Direction: ");
01974 if (Request->Direction)
01975 {
01976 dbg("Device -> Host\n");
01977 } else
01978 {
01979 dbg("Host -> Device\n");
01980 }
01981
01982 dbg(" | |-> Target: ");
01983 switch (Request->Target) {
01984 case USB_REQUEST_TYPE_STANDARD:
01985 dbg("STANDARD\n");
01986 break;
01987 case USB_REQUEST_TYPE_CLASS:
01988 dbg("CLASS\n");
01989 break;
01990 case USB_REQUEST_TYPE_VENDOR:
01991 dbg("VENDOR SPECIFIC\n");
01992 break;
01993 }
01994 dbg(" | |-> Request: ");
01995 switch (Request->Request)
01996 {
01997 case USB_GET_STATUS:
01998 dbg("GET_STATUS\n");
01999 break;
02000 case USB_CLEAR_FEATURE:
02001 dbg("CLEAR_FEATURE\n");
02002 break;
02003 case USB_SET_FEATURE:
02004 dbg("SET_FEATURE\n");
02005 break;
02006 case USB_SET_ADDRESS:
02007 dbg("SET_ADDRESS\n");
02008 break;
02009 case USB_GET_DESCRIPTOR:
02010 dbg("GET_DESCRIPTOR\n");
02011 break;
02012 case USB_SET_DESCRIPTOR:
02013 dbg("SET_DESCRIPTOR\n");
02014 break;
02015 case USB_GET_CONFIGURATION:
02016 dbg("GET_CONFIGURATION\n");
02017 break;
02018 case USB_SET_CONFIGURATION:
02019 dbg("SET_CONFIGURATION\n");
02020 break;
02021 case USB_SET_INTERFACE:
02022 dbg("SET_INTERFACE\n");
02023 break;
02024 case USB_GET_INTERFACE:
02025 dbg("GET_INTERFACE\n");
02026 break;
02027 case USB_SYNCH_FRAME:
02028 dbg("SYNCH_FRAME\n");
02029 break;
02030 default:
02031 dbg("UNKNOWN\n");
02032 break;
02033 }
02034
02035 }
02036
02037 static void DbgUsbGetDescriptor(usb_request_t * ptRequest)
02038 {
02039 dbg(" |-> Send Descriptor: ");
02040 switch (((ptRequest->wValue) >> 8) & 0x00FF)
02041 {
02042 case USB_REQUEST_DEVICE_DESCRIPTOR:
02043 dbg("DEVICE\n");
02044 break;
02045 case USB_REQUEST_CONFIG_DESCRIPTOR:
02046 dbg("CONFIGURATION\n");
02047 break;
02048 case USB_REQUEST_STRING_DESCRIPTOR:
02049 dbg("STRING\n");
02050 break;
02051 case USB_REQUEST_INTERF_DESCRIPTOR:
02052 dbg("INTERFACE\n");
02053 break;
02054 case USB_REQUEST_ENDPNT_DESCRIPTOR:
02055 dbg("Endpoint\n");
02056 break;
02057 case USB_REQUEST_DEVQUA_DESCRIPTOR:
02058 dbg("DEVICE_QUALIFIER\n");
02059 break;
02060 case USB_REQUEST_OTHSPE_DESCRIPTOR:
02061 dbg("OTHER_SPEED_CONFIGURATION\n");
02062 break;
02063 case USB_REQUEST_IFPOWR_DESCRIPTOR:
02064 dbg("INTERFACE_POWER\n");
02065 break;
02066 case USB_REQUEST_REPORT_DESCRIPTOR:
02067 dbg("REPORT_DESCRIPTOR\n");
02068 break;
02069 default:
02070 dbg("NOT FOUND: ");
02071 dbghex(((ptRequest->wValue) >> 8) & 0x00FF);
02072 dbg("\n");
02073 break;
02074 }
02075 }
02076 #endif
02077
02078