Fork of https://developer.mbed.org/users/bscott/code/STM32_USBDevice/
Fork of STM32_USBDevice by
Diff: USBDevice/USBHAL_STM32L1.cpp
- Revision:
- 59:5d5e3685bd60
- Parent:
- 58:68fad4f36f1c
- Child:
- 60:04a69c36260e
--- a/USBDevice/USBHAL_STM32L1.cpp Tue Jun 16 06:20:17 2015 +0900 +++ b/USBDevice/USBHAL_STM32L1.cpp Tue Jun 16 18:01:11 2015 +0900 @@ -80,12 +80,12 @@ class PacketBufferAreaManager { public: - PacketBufferAreaManager() { + PacketBufferAreaManager(int bufsize_):bufsize(bufsize_) { reset(); } void reset() { head = 0; - tail = 512; + tail = bufsize; } int allocBuf(int maxPacketSize) { head += 4; @@ -97,7 +97,8 @@ } private: int head,tail; -} PktBufArea; + int bufsize; +} PktBufArea(512); bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flags) { int pmaadress = PktBufArea.allocBuf(maxPacket); @@ -161,29 +162,45 @@ endpointRead(EP0OUT, MAX_PACKET_SIZE_EP0); } -static uint8_t* rxTempBuffer(uint8_t endpoint) { - switch(endpoint) { - case EP0OUT: - static uint8_t buf0[MAX_PACKET_SIZE_EP0]; - return buf0; - case EP1OUT: - static uint8_t buf1[MAX_PACKET_SIZE_EP1]; - return buf1; - case EP2OUT: - static uint8_t buf2[MAX_PACKET_SIZE_EP2]; - return buf2; - case EP3OUT: - static uint8_t buf3[MAX_PACKET_SIZE_EP3_ISO]; - return buf3; +class rxTempBufferManager { + uint8_t buf0[MAX_PACKET_SIZE_EP0]; + uint8_t buf1[MAX_PACKET_SIZE_EP1]; + uint8_t buf2[MAX_PACKET_SIZE_EP2]; + uint8_t buf3[MAX_PACKET_SIZE_EP3_ISO]; +public: + uint8_t* ptr(uint8_t endpoint, int maxPacketSize) { + switch(endpoint) { + case EP0OUT: + MBED_ASSERT(maxPacketSize <= MAX_PACKET_SIZE_EP0); + break; + case EP1OUT: + MBED_ASSERT(maxPacketSize <= MAX_PACKET_SIZE_EP1); + break; + case EP2OUT: + MBED_ASSERT(maxPacketSize <= MAX_PACKET_SIZE_EP2); + break; + case EP3OUT: + MBED_ASSERT(maxPacketSize <= MAX_PACKET_SIZE_EP3_ISO); + break; + } + return ptr(endpoint); } - MBED_ASSERT(0); - return NULL; -} + uint8_t* ptr(uint8_t endpoint) { + switch(endpoint) { + case EP0OUT: return buf0; + case EP1OUT: return buf1; + case EP2OUT: return buf2; + case EP3OUT: return buf3; + } + MBED_ASSERT(0); + return NULL; + } +} rxtmp; uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) { const uint8_t endpoint = EP0OUT; uint32_t length = HAL_PCD_EP_GetRxCount(&hpcd_USB_FS, endpoint>>1); - memcpy(buffer, rxTempBuffer(endpoint), length); + memcpy(buffer, rxtmp.ptr(endpoint), length); return length; } @@ -204,7 +221,7 @@ } EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) { - HAL_PCD_EP_Receive(&hpcd_USB_FS, endpoint>>1, rxTempBuffer(endpoint), maximumSize); + HAL_PCD_EP_Receive(&hpcd_USB_FS, endpoint>>1, rxtmp.ptr(endpoint, maximumSize), maximumSize); epComplete &= ~(1 << endpoint); return EP_PENDING; } @@ -215,7 +232,7 @@ } int len = HAL_PCD_EP_GetRxCount(&hpcd_USB_FS, endpoint>>1); MBED_ASSERT(len <= 64); - memcpy(buffer, rxTempBuffer(endpoint), len); + memcpy(buffer, rxtmp.ptr(endpoint), len); *bytesRead = len; return EP_COMPLETED; } @@ -295,7 +312,9 @@ void USBHAL::DataOutStageCallback(uint8_t epnum) { switch(epnum) { case 0: // EP0OUT - EP0out(); + if ((hpcd_USB_FS.Setup[0]&0x80) == 0x00) { // host to device ? + EP0out(); + } break; case 1: epComplete |= (1<<EP1OUT);