USB device stack for NUCLEO-F042K6, NUCLEO-L152RE and NUCLEO-F103RB.

Dependents:   LPE-SEM01

Fork of L152RE_USBDevice by Norimasa Okamoto

I tried USB device using HAL_PCD.

/media/uploads/va009039/f042k6_usbdevice_vin.jpg

Nucleo-F042K6USB
PA11 (CN3-13)DM  (2 WHITE)
PA12 (CN3-5)DP  (3 GREEN)
GND (CN3-4)GND (5 BLACK)
VIN (CN4-1)VBUS(1 RED)

Examples

Import programF042K6_USBDevice_example

NUCLEO-F042K6 USBDevice example code

Import programL152RE_USBDevice_example

L152RE_USBDevice example code

Import programF042K6_Simple-CMSIS-DAP

cmsis-dap debug adapter

Import programL152RE_Simple-CMSIS-DAP

cmsis-dap debug adapter

Files at this revision

API Documentation at this revision

Comitter:
va009039
Date:
Tue Jun 16 18:01:11 2015 +0900
Parent:
58:68fad4f36f1c
Child:
60:04a69c36260e
Commit message:
status stage not response STALL.

Changed in this revision

USBDevice/USBHAL_STM32L1.cpp Show annotated file Show diff for this revision Revisions of this file
--- 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);