USB device stack

Dependents:   mbed-mX-USB-TEST1 USBMSD_SD_HID_HelloWorld HidTest MIDI_usb_bridge ... more

Legacy Warning

This is an mbed 2 library. To learn more about mbed OS 5, visit the docs.

Pull requests against this repository are no longer supported. Please raise against mbed OS 5 as documented above.

Files at this revision

API Documentation at this revision

Comitter:
Kojto
Date:
Thu Jul 20 10:14:36 2017 +0100
Parent:
69:c5e178adb138
Child:
71:53949e6131f6
Commit message:
Update libraries (ed9d1da)

Changed in this revision

USBAudio/USBAudio.cpp Show annotated file Show diff for this revision Revisions of this file
USBAudio/USBAudio.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/USBHAL_STM32F103RB.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F2/TARGET_NUCLEO_F207ZG/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xE/TARGET_NUCLEO_F303ZE/USBHAL_STM32F303ZE.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xE/TARGET_NUCLEO_F303ZE/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F401xE/TARGET_NUCLEO_F401RE/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F407xG/TARGET_DISCO_F407VG/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F411xE/TARGET_NUCLEO_F411RE/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F412xG/TARGET_NUCLEO_F412ZG/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_NUCLEO_F429ZI/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_NUCLEO_F439ZI/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/TARGET_NUCLEO_F446RE/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/TARGET_NUCLEO_F446ZE/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/TARGET_NUCLEO_F746ZG/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F7/TARGET_STM32F756xG/TARGET_NUCLEO_F756ZG/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F7/TARGET_STM32F767xI/TARGET_NUCLEO_F767ZI/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F7/TARGET_STM32F769xI/TARGET_DISCO_F769NI/USBHAL_STM32F769NI.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32F7/TARGET_STM32F769xI/TARGET_DISCO_F769NI/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/USBHAL_STM32L053C8.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L072CZ_LRWAN1/USBHAL_STM32L072CZ.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L072CZ_LRWAN1/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/TARGET_DISCO_L475VG_IOT01A/USBHAL_STM32L475VG.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/TARGET_DISCO_L475VG_IOT01A/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/USBHAL_STM32L476VG.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/USBHAL_STM_TARGET.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/USBHAL_STM32.cpp Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_STM/USBHAL_STM_144_64pins.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/TARGET_Silicon_Labs/inc/em_usb.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBEndpoints.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBEndpoints_KL25Z.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBEndpoints_M453.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBEndpoints_NUC472.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBEndpoints_STM32.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBHAL.h Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBHAL_KL25Z.cpp Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBHAL_M453.cpp Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBHAL_NUC472.cpp Show annotated file Show diff for this revision Revisions of this file
USBDevice/USBHAL_STM32F4.cpp Show annotated file Show diff for this revision Revisions of this file
USBHID/USBHID.cpp Show annotated file Show diff for this revision Revisions of this file
USBHID/USBHID_Types.h Show annotated file Show diff for this revision Revisions of this file
USBHID/USBKeyboard.cpp Show annotated file Show diff for this revision Revisions of this file
USBHID/USBKeyboard.h Show annotated file Show diff for this revision Revisions of this file
USBHID/USBMouse.cpp Show annotated file Show diff for this revision Revisions of this file
USBSerial/USBCDC.cpp Show annotated file Show diff for this revision Revisions of this file
USBSerial/USBCDC.h Show annotated file Show diff for this revision Revisions of this file
USBSerial/USBSerial.cpp Show annotated file Show diff for this revision Revisions of this file
USBSerial/USBSerial.h Show annotated file Show diff for this revision Revisions of this file
--- a/USBAudio/USBAudio.cpp	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBAudio/USBAudio.cpp	Thu Jul 20 10:14:36 2017 +0100
@@ -85,7 +85,7 @@
     SOF_handler = false;
     writeIN = false;
     if (interruptIN) {
-        USBDevice::writeNB(EP3IN, buf_write, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
+        USBDevice::writeNB(EPISO_IN, buf_write, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
     } else {
         buf_stream_out = buf_write;
     }
@@ -102,7 +102,7 @@
     writeIN = false;
     SOF_handler = false;
     if (interruptIN) {
-        USBDevice::writeNB(EP3IN, buf, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
+        USBDevice::writeNB(EPISO_IN, buf, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
     } else {
         buf_stream_out = buf;
     }
@@ -113,6 +113,21 @@
     return true;
 }
 
+void USBAudio::writeSync(uint8_t *buf, AudioSampleCorrectType jitter_nb)
+{
+    if ((jitter_nb != RemoveOneSample) && (jitter_nb != AddOneSample)) {
+        jitter_nb = NoCorrection;
+    }
+    /* each sample is 2 bytes */
+    USBDevice::writeNB(EPISO_IN, buf, PACKET_SIZE_ISO_OUT + jitter_nb *(this->channel_nb_out*2), PACKET_SIZE_ISO_OUT+this->channel_nb_out*2);
+}
+
+uint32_t USBAudio::readSync(uint8_t *buf)
+{
+    uint32_t size = 0;
+    USBDevice::readEP(EPISO_OUT, (uint8_t *)buf, &size, PACKET_SIZE_ISO_IN);
+    return size;
+}
 
 float USBAudio::getVolume() {
     return (mute) ? 0.0 : volume;
@@ -123,11 +138,15 @@
     uint32_t size = 0;
     interruptOUT = true;
     if (buf_stream_in != NULL) {
-        readEP(EP3OUT, (uint8_t *)buf_stream_in, &size, PACKET_SIZE_ISO_IN);
+        readEP(EPISO_OUT, (uint8_t *)buf_stream_in, &size, PACKET_SIZE_ISO_IN);
         available = true;
         buf_stream_in = NULL;
     }
-    readStart(EP3OUT, PACKET_SIZE_ISO_IN);
+    else  {
+        if (rxDone)
+            rxDone.call();
+    }
+    readStart(EPISO_OUT, PACKET_SIZE_ISO_IN);
     return false;
 }
 
@@ -135,6 +154,8 @@
 bool USBAudio::EPISO_IN_callback() {
     interruptIN = true;
     writeIN = true;
+    if (txDone) 
+        txDone.call();
     return true;
 }
 
@@ -147,10 +168,10 @@
     if (!interruptOUT) {
         // read the isochronous endpoint
         if (buf_stream_in != NULL) {
-            if (USBDevice::readEP_NB(EP3OUT, (uint8_t *)buf_stream_in, &size, PACKET_SIZE_ISO_IN)) {
+            if (USBDevice::readEP_NB(EPISO_OUT, (uint8_t *)buf_stream_in, &size, PACKET_SIZE_ISO_IN)) {
                 if (size) {
                     available = true;
-                    readStart(EP3OUT, PACKET_SIZE_ISO_IN);
+                    readStart(EPISO_OUT, PACKET_SIZE_ISO_IN);
                     buf_stream_in = NULL;
                 }
             }
@@ -160,7 +181,7 @@
     if (!interruptIN) {
         // write if needed
         if (buf_stream_out != NULL) {
-            USBDevice::writeNB(EP3IN, (uint8_t *)buf_stream_out, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
+            USBDevice::writeNB(EPISO_IN, (uint8_t *)buf_stream_out, PACKET_SIZE_ISO_OUT, PACKET_SIZE_ISO_OUT);
             buf_stream_out = NULL;
         }
     }
@@ -177,11 +198,11 @@
     }
 
     // Configure isochronous endpoint
-    realiseEndpoint(EP3OUT, PACKET_SIZE_ISO_IN, ISOCHRONOUS);
-    realiseEndpoint(EP3IN, PACKET_SIZE_ISO_OUT, ISOCHRONOUS);
+    realiseEndpoint(EPISO_OUT, PACKET_SIZE_ISO_IN, ISOCHRONOUS);
+    realiseEndpoint(EPISO_IN, PACKET_SIZE_ISO_OUT+this->channel_nb_out*2, ISOCHRONOUS);
 
     // activate readings on this endpoint
-    readStart(EP3OUT, PACKET_SIZE_ISO_IN);
+    readStart(EPISO_OUT, PACKET_SIZE_ISO_IN);
     return true;
 }
 
@@ -313,7 +334,8 @@
                 switch (transfer->setup.bRequest) {
                     case REQUEST_SET_CUR:
                         mute = data & 0xff;
-                        updateVol.call();
+                        if (updateVol)
+                            updateVol.call();
                         break;
                     default:
                         break;
@@ -324,7 +346,8 @@
                     case REQUEST_SET_CUR:
                         volCur = data;
                         volume = (float)volCur/(float)volMax;
-                        updateVol.call();
+                        if (updateVol)
+                            updateVol.call();
                         break;
                     default:
                         break;
@@ -578,8 +601,8 @@
         ENDPOINT_DESCRIPTOR,                    // bDescriptorType
         PHY_TO_DESC(EPISO_IN),                  // bEndpointAddress
         E_ISOCHRONOUS,                          // bmAttributes
-        (uint8_t)(LSB(PACKET_SIZE_ISO_OUT)),                   // wMaxPacketSize
-        (uint8_t)(MSB(PACKET_SIZE_ISO_OUT)),                   // wMaxPacketSize
+        (uint8_t)(LSB(PACKET_SIZE_ISO_OUT+channel_nb_out*2)),                   // wMaxPacketSize
+        (uint8_t)(MSB(PACKET_SIZE_ISO_OUT+channel_nb_out*2)),                   // wMaxPacketSize
         0x01,                                   // bInterval
         0x00,                                   // bRefresh
         0x00,                                   // bSynchAddress
--- a/USBAudio/USBAudio.h	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBAudio/USBAudio.h	Thu Jul 20 10:14:36 2017 +0100
@@ -25,7 +25,7 @@
 #include "USBDevice_Types.h"
 
 #include "USBDevice.h"
-
+#include "Callback.h"
 
 /**
 * USBAudio example
@@ -108,6 +108,14 @@
     bool readNB(uint8_t * buf);
 
     /**
+     * read last received packet if some.
+     * @param buf pointer on a buffer which will be filled if an audio packet is available
+     *
+     * @returns the packet length
+     */
+    uint32_t readSync(uint8_t *buf);
+
+    /**
     * Write an audio packet. During a frame, only a single writing (you can't write and read an audio packet during the same frame)can be done using this method.
     *
     * @param buf pointer on the audio packet which will be sent
@@ -115,6 +123,19 @@
     */
     bool write(uint8_t * buf);
 
+    /** Audio Jitter value*/
+    enum AudioSampleCorrectType {
+        RemoveOneSample = -1,
+        NoCorrection = 0,
+        AddOneSample = 1
+    };
+    /**
+     * Write packet in endpoint fifo. assuming tx fifo is empty
+     * @param buf pointer on the audio packet which will be sent
+     * @param jitter_nb : AudioSampleCorrecttype 
+	 **/
+    void writeSync(uint8_t *buf, AudioSampleCorrectType jitter_nb = NoCorrection );
+
     /**
     * Write and read an audio packet at the same time (on the same frame)
     *
@@ -133,6 +154,22 @@
     void attach(void(*fptr)(void)) {
         updateVol.attach(fptr);
     }
+	/** attach a handler to Tx Done
+     *
+     * @param function Function to attach
+     *
+     */
+    void attachTx(void(*fptr)(void)) {
+        txDone.attach(fptr);
+    }
+    /** attach a handler to Rx Done
+     *
+     * @param function Function to attach
+     *
+     */
+    void attachRx(void(*fptr)(void)) {
+        rxDone.attach(fptr);
+    }
 
     /** Attach a nonstatic void/void member function to update the volume
      *
@@ -144,6 +181,14 @@
     void attach(T *tptr, void(T::*mptr)(void)) {
         updateVol.attach(tptr, mptr);
     }
+	template<typename T>
+	void attachTx(T *tptr, void(T::*mptr)(void)) {
+        txDone.attach(tptr, mptr);
+    }
+    template<typename T>
+	void attachRx(T *tptr, void(T::*mptr)(void)) {
+        rxDone.attach(tptr, mptr);
+    }
 
 
 protected:
@@ -275,7 +320,12 @@
     volatile uint8_t * buf_stream_out;
 
     // callback to update volume
-    FunctionPointer updateVol;
+    Callback<void()> updateVol;
+
+    // callback transmit Done
+    Callback<void()> txDone;
+    // callback transmit Done
+    Callback<void()> rxDone;
 
     // boolean showing that the SOF handler has been called. Useful for readNB.
     volatile bool SOF_handler;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/USBHAL_STM32F103RB.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,147 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#ifndef USBHAL_STM32F103RB
+#define USBHAL_STM32F103RB
+
+#define USBHAL_IRQn  USB_LP_CAN1_RX0_IRQn
+
+
+#define NB_ENDPOINT  8
+/*  must be multiple of 4 bytes */
+#define MAXTRANSFER_SIZE  0x200
+#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3)
+#if (FIFO_USB_RAM_SIZE > 0x500)
+#error "FIFO dimensioning incorrect"
+#endif
+
+typedef struct
+{
+    USBHAL *inst;
+    void (USBHAL::*bus_reset)(void);
+    void (USBHAL::*sof)(int frame);
+    void (USBHAL::*connect_change)(unsigned int  connected);
+    void (USBHAL::*suspend_change)(unsigned int suspended);
+    void (USBHAL::*ep0_setup)(void);
+    void (USBHAL::*ep0_in)(void);
+    void (USBHAL::*ep0_out)(void);
+    void (USBHAL::*ep0_read)(void);
+    bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
+    bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void);
+    uint8_t epComplete[8];
+    /*  memorize dummy buffer used for reception */
+    uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
+    uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
+    gpio_t usb_switch;
+}USBHAL_Private_t;
+
+void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    gpio_write(&(priv->usb_switch),!state);
+}
+
+uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo)
+{
+    return 1024;
+}
+void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    uint32_t sofnum = (hpcd->Instance->FNR) & USB_FNR_FN;
+    void (USBHAL::*func)(int frame) = priv->sof;
+    (obj->*func)(sofnum);
+}
+
+USBHAL * USBHAL::instance;
+
+USBHAL::USBHAL(void)
+{
+    /*  init parameter  */
+    USBHAL_Private_t *HALPriv = new(USBHAL_Private_t);
+    /*  initialized all field of init including 0 field  */
+    /*  constructor does not fill with zero */
+    hpcd.Instance = USB;
+    /*  initialized all field of init including 0 field  */
+    /*  constructor does not fill with zero */
+    memset(&hpcd.Init, 0, sizeof(hpcd.Init));
+    hpcd.Init.dev_endpoints = NB_ENDPOINT;
+    hpcd.Init.ep0_mps =   MAX_PACKET_SIZE_EP0;
+    hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
+    hpcd.Init.Sof_enable = 1;
+    hpcd.Init.speed = PCD_SPEED_FULL;
+    /*  pass instance for usage inside call back */
+    HALPriv->inst = this;
+    HALPriv->bus_reset = &USBHAL::busReset;
+    HALPriv->suspend_change = &USBHAL::suspendStateChanged;
+    HALPriv->connect_change = &USBHAL::connectStateChanged;
+    HALPriv->sof = &USBHAL::SOF;
+    HALPriv->ep0_setup = &USBHAL::EP0setupCallback;
+    HALPriv->ep_realise = &USBHAL::realiseEndpoint;
+    HALPriv->ep0_in = &USBHAL::EP0in;
+    HALPriv->ep0_out = &USBHAL::EP0out;
+    HALPriv->ep0_read = &USBHAL::EP0read;
+    hpcd.pData = (void*)HALPriv;
+    HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback;
+    HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback;
+    HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback;
+    HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback;
+    HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback;
+    HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback;
+    instance = this;
+
+
+    /* Configure USB VBUS GPIO */
+    gpio_init_out(&HALPriv->usb_switch,PB_14);
+    gpio_mode(&HALPriv->usb_switch,OpenDrain);
+    /* Configure USB FS GPIOs */
+
+    /* Configure DM DP Pins
+     *   - USB-DP (D+ of the USB connector) <======> PA12 (Nucleo board)
+     *   Make sure to connect a 1.5KOhm pull up to USB-DP PA12 pin
+     *   (permanent pull-up)
+     - USB-DM (D- of the USB connector) <======> PA11 (Nucleo board)
+     */
+
+    pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_MODE_AF_INPUT));
+    pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_MODE_AF_INPUT));
+
+    __HAL_RCC_USB_CLK_ENABLE();
+
+    hpcd.State = HAL_PCD_STATE_RESET;
+
+    HAL_PCD_Init(&hpcd);
+    /* hardcoded size of FIFO according definition*/
+    HAL_PCDEx_PMAConfig(&hpcd , 0x00 , PCD_SNG_BUF, 0x30);
+    HAL_PCDEx_PMAConfig(&hpcd , 0x80 , PCD_SNG_BUF, 0x70);
+    HAL_PCDEx_PMAConfig(&hpcd , 0x01 , PCD_SNG_BUF, 0x90);
+    HAL_PCDEx_PMAConfig(&hpcd , 0x81 , PCD_SNG_BUF, 0xb0);
+#if 0
+    HAL_PCDEx_PMAConfig(&hpcd , 0x2, PCD_DBL_BUF, 0x018000b0);
+#else
+    HAL_PCDEx_PMAConfig(&hpcd , 0x2, PCD_SNG_BUF, 0x100);
+#endif
+    HAL_PCDEx_PMAConfig(&hpcd , 0x82, PCD_SNG_BUF, 0x120);
+
+    NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr);
+    NVIC_SetPriority( USBHAL_IRQn, 1);
+
+    HAL_PCD_Start(&hpcd);
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,19 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM32F103RB.h"
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F2/TARGET_NUCLEO_F207ZG/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,19 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM_144_64pins.h"
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xE/TARGET_NUCLEO_F303ZE/USBHAL_STM32F303ZE.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,127 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#ifndef USBHAL_STM32F303ZE_H
+#define USBHAL_STM32F303ZE_H
+#define USBHAL_IRQn  USB_LP_CAN_RX0_IRQn
+/*  must be multiple of 4 bytes */
+#define NB_ENDPOINT 8
+#define MAXTRANSFER_SIZE  0x200
+#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3)
+#if (FIFO_USB_RAM_SIZE > 0x500)
+#error "FIFO dimensioning incorrect"
+#endif
+
+typedef struct
+{
+	USBHAL *inst;
+	void (USBHAL::*bus_reset)(void);
+	void (USBHAL::*sof)(int frame);
+	void (USBHAL::*connect_change)(unsigned int  connected);
+	void (USBHAL::*suspend_change)(unsigned int suspended);
+	void (USBHAL::*ep0_setup)(void);
+	void (USBHAL::*ep0_in)(void);
+	void (USBHAL::*ep0_out)(void);
+	void (USBHAL::*ep0_read)(void);
+	bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
+	bool (USBHAL::*epCallback[6])(void);
+	uint8_t epComplete[2*NB_ENDPOINT];
+	/*  memorize dummy buffer used for reception */
+	uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
+	uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
+    gpio_t usb_switch;
+}USBHAL_Private_t;
+
+uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo)
+{
+        return 1024;
+}
+
+void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state){
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    gpio_write(&(priv->usb_switch),state);
+}
+
+void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) {
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    uint32_t sofnum = (hpcd->Instance->FNR) & USB_FNR_FN;
+    void (USBHAL::*func)(int frame) = priv->sof;
+    (obj->*func)(sofnum);
+}
+
+USBHAL * USBHAL::instance;
+
+USBHAL::USBHAL(void) {
+    /*  init parameter  */
+    USBHAL_Private_t *HALPriv = new(USBHAL_Private_t);
+    hpcd.Instance = USB;
+    /*  initialized Init to zero (constructor does not zero initialized the
+     *  area */
+    /*  initialized all field of init including 0 field  */
+    /*  constructor does not fill with zero */
+    memset(&hpcd.Init, 0, sizeof(hpcd.Init));
+    hpcd.Init.dev_endpoints = NB_ENDPOINT;
+    hpcd.Init.ep0_mps =   MAX_PACKET_SIZE_EP0;
+    hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
+    hpcd.Init.Sof_enable = 1;
+    hpcd.Init.speed = PCD_SPEED_FULL;
+    /*  pass instance for usage inside call back */
+    HALPriv->inst = this;
+    HALPriv->bus_reset = &USBHAL::busReset;
+    HALPriv->suspend_change = &USBHAL::suspendStateChanged;
+    HALPriv->connect_change = &USBHAL::connectStateChanged;
+    HALPriv->sof = &USBHAL::SOF;
+    HALPriv->ep0_setup = &USBHAL::EP0setupCallback;
+    HALPriv->ep_realise = &USBHAL::realiseEndpoint;
+    HALPriv->ep0_in = &USBHAL::EP0in;
+    HALPriv->ep0_out = &USBHAL::EP0out;
+    HALPriv->ep0_read = &USBHAL::EP0read;
+    hpcd.pData = (void*)HALPriv;
+    HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback;
+    HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback;
+    HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback;
+    HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback;
+    HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback;
+    HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback;
+    instance = this;
+    __HAL_RCC_GPIOA_CLK_ENABLE();
+    /* Configure USB DM pin. This is optional, and maintained only for user guidance. */
+    pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF14_USB));
+    pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF14_USB));
+    __HAL_RCC_GPIOG_CLK_ENABLE();
+    gpio_init_out(&HALPriv->usb_switch,PG_6);
+    /* Enable USB Clock */
+    __HAL_RCC_USB_CLK_ENABLE();
+    /* Enable SYSCFG Clock */
+    __HAL_RCC_SYSCFG_CLK_ENABLE();
+    hpcd.State = HAL_PCD_STATE_RESET;
+    HAL_PCD_Init(&hpcd);
+    /* hardcoded size of FIFO according definition*/
+    HAL_PCDEx_PMAConfig(&hpcd , 0x00 , PCD_SNG_BUF, 0x30);
+    HAL_PCDEx_PMAConfig(&hpcd , 0x80 , PCD_SNG_BUF, 0x70);
+#if 1
+    HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_DBL_BUF, 0x018000b0);
+#else
+    HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_SNG_BUF, 0x180);
+#endif
+    HAL_PCDEx_PMAConfig(&hpcd , 0x83, PCD_SNG_BUF, 0xb0);
+    NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr);
+    NVIC_SetPriority(USBHAL_IRQn, 1);
+    HAL_PCD_Start(&hpcd);
+}
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xE/TARGET_NUCLEO_F303ZE/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,18 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM32F303ZE.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F401xE/TARGET_NUCLEO_F401RE/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,18 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM_144_64pins.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F407xG/TARGET_DISCO_F407VG/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,18 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM_144_64pins.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F411xE/TARGET_NUCLEO_F411RE/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,19 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "USBHAL_STM_144_64pins.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F412xG/TARGET_NUCLEO_F412ZG/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,18 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM_144_64pins.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_NUCLEO_F429ZI/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,18 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM_144_64pins.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_NUCLEO_F439ZI/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,18 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM_144_64pins.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/TARGET_NUCLEO_F446RE/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,18 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM_144_64pins.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/TARGET_NUCLEO_F446ZE/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,18 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM_144_64pins.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/TARGET_NUCLEO_F746ZG/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,19 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM_144_64pins.h"
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F7/TARGET_STM32F756xG/TARGET_NUCLEO_F756ZG/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,19 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM_144_64pins.h"
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F7/TARGET_STM32F767xI/TARGET_NUCLEO_F767ZI/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,18 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM_144_64pins.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F7/TARGET_STM32F769xI/TARGET_DISCO_F769NI/USBHAL_STM32F769NI.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,147 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#ifndef USBHAL_STM32F769NI_H
+#define USBHAL_STM32F769NI_H
+#define USBHAL_IRQn  OTG_HS_IRQn
+/*  must be multiple of 4 bytes */
+#define NB_ENDPOINT 4
+#define MAXTRANSFER_SIZE  0x200
+#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3)
+#if (FIFO_USB_RAM_SIZE > 0x500)
+#error "FIFO dimensioning incorrect"
+#endif
+
+typedef struct
+{
+    USBHAL *inst;
+    void (USBHAL::*bus_reset)(void);
+    void (USBHAL::*sof)(int frame);
+    void (USBHAL::*connect_change)(unsigned int  connected);
+    void (USBHAL::*suspend_change)(unsigned int suspended);
+    void (USBHAL::*ep0_setup)(void);
+    void (USBHAL::*ep0_in)(void);
+    void (USBHAL::*ep0_out)(void);
+    void (USBHAL::*ep0_read)(void);
+    bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
+    bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void);
+    /*  memorize dummy buffer used for reception */
+    uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
+    uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
+    uint8_t epComplete[2*NB_ENDPOINT];
+}USBHAL_Private_t;
+
+uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo)
+{
+    uint32_t len;
+    if (fifo == 0) len = hpcd->Instance->DIEPTXF0_HNPTXFSIZ>>16;
+    else
+        len =  hpcd->Instance->DIEPTXF[fifo - 1] >> 16;
+    return len*4;
+}
+void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) 
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
+    uint32_t sofnum = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF) >> 8;
+    void (USBHAL::*func)(int frame) = priv->sof;
+    /* fix me  call with same frame number */
+    (obj->*func)(sofnum);
+}
+
+
+USBHAL * USBHAL::instance;
+
+USBHAL::USBHAL(void) {
+    /*  init parameter  */
+    USBHAL_Private_t *HALPriv = new(USBHAL_Private_t);
+    hpcd.Instance = USB_OTG_HS;
+    memset(&hpcd.Init, 0, sizeof(hpcd.Init));
+    hpcd.Init.dev_endpoints = NB_ENDPOINT;
+    hpcd.Init.ep0_mps =   MAX_PACKET_SIZE_EP0;
+    hpcd.Init.phy_itface = PCD_PHY_ULPI;
+    hpcd.Init.Sof_enable = 0;
+
+    hpcd.Init.speed = PCD_SPEED_HIGH;
+    //hpcd.Init.vbus_sensing_enable = 0;
+    //hpcd.Init.lpm_enable = 0;
+    /*  pass instance for usage inside call back */
+    HALPriv->inst = this;
+    HALPriv->bus_reset = &USBHAL::busReset;
+    HALPriv->suspend_change = &USBHAL::suspendStateChanged;
+    HALPriv->connect_change = &USBHAL::connectStateChanged;
+    HALPriv->sof = &USBHAL::SOF;
+    HALPriv->ep0_setup = &USBHAL::EP0setupCallback;
+    HALPriv->ep_realise = &USBHAL::realiseEndpoint;
+    HALPriv->ep0_in = &USBHAL::EP0in;
+    HALPriv->ep0_out = &USBHAL::EP0out;
+    HALPriv->ep0_read = &USBHAL::EP0read;
+    hpcd.pData = (void*)HALPriv;
+    HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback;
+    HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback;
+    HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback;
+    HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback;
+    HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback;
+    HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback;
+    instance = this;
+    /* Enable power and clocking */
+    __HAL_RCC_GPIOA_CLK_ENABLE();
+    __HAL_RCC_GPIOB_CLK_ENABLE();
+    __HAL_RCC_GPIOC_CLK_ENABLE();
+    __HAL_RCC_GPIOH_CLK_ENABLE();
+    __HAL_RCC_GPIOI_CLK_ENABLE();
+
+    pin_function(PA_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // CLK
+    pin_function(PA_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D0
+
+    pin_function(PB_0, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D1
+    pin_function(PB_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D2
+    pin_function(PB_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D3
+    pin_function(PB_10, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D4
+    pin_function(PB_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D5
+    pin_function(PB_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D6
+    pin_function(PB_13, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D7
+
+    pin_function(PC_0, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // STP
+    pin_function(PH_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // NXT
+    pin_function(PI_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // DIR
+
+    __HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE();
+    __HAL_RCC_USB_OTG_HS_CLK_ENABLE();
+
+    __HAL_RCC_SYSCFG_CLK_ENABLE();
+    hpcd.State = HAL_PCD_STATE_RESET;
+    HAL_PCD_Init(&hpcd);
+    /* 1.25kbytes */
+    /* min value 16 (= 16 x 4 bytes) */
+    /*  max value 256 (= 1K bytes ) */
+    /*  maximum sum is 0x140 */
+    HAL_PCDEx_SetRxFiFo(&hpcd, (MAXTRANSFER_SIZE/4));
+    /*  bulk/int 64 bytes in FS  */
+    HAL_PCDEx_SetTxFiFo(&hpcd, 0, (MAX_PACKET_SIZE_EP0/4)+1);
+    /*  bulk/int bytes in FS */
+    HAL_PCDEx_SetTxFiFo(&hpcd, 1, (MAX_PACKET_SIZE_EP1/4)+1);
+    HAL_PCDEx_SetTxFiFo(&hpcd, 2, (MAX_PACKET_SIZE_EP2/4));
+    /* ISOchronous */
+    HAL_PCDEx_SetTxFiFo(&hpcd, 3, (MAX_PACKET_SIZE_EP3/4));
+    NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr);
+    NVIC_SetPriority(USBHAL_IRQn, 1);
+    HAL_PCD_Start(&hpcd);
+}
+#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32F7/TARGET_STM32F769xI/TARGET_DISCO_F769NI/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,19 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "USBHAL_STM32F769NI.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/USBHAL_STM32L053C8.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,134 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#ifndef USBHAL_STM32L053C8_H
+#define USBHAL_STM32L053C8_H
+
+#define USBHAL_IRQn  USB_IRQn
+
+/*  must be multiple of 4 bytes */
+#define NB_ENDPOINT 8
+#define MAXTRANSFER_SIZE  0x200
+#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3)
+#if (FIFO_USB_RAM_SIZE > 0x500)
+#error "FIFO dimensioning incorrect"
+#endif
+
+typedef struct
+{
+	USBHAL *inst;
+	void (USBHAL::*bus_reset)(void);
+	void (USBHAL::*sof)(int frame);
+	void (USBHAL::*connect_change)(unsigned int  connected);
+	void (USBHAL::*suspend_change)(unsigned int suspended);
+	void (USBHAL::*ep0_setup)(void);
+	void (USBHAL::*ep0_in)(void);
+	void (USBHAL::*ep0_out)(void);
+	void (USBHAL::*ep0_read)(void);
+	bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
+	bool (USBHAL::*epCallback[6])(void);
+	uint8_t epComplete[2*NB_ENDPOINT];
+	/*  memorize dummy buffer used for reception */
+	uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
+	uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
+    gpio_t usb_switch;
+}USBHAL_Private_t;
+
+uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo)
+{
+        return 1024;
+}
+
+void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    gpio_write(&(priv->usb_switch),state);
+}
+
+void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    uint32_t sofnum = (hpcd->Instance->FNR) & USB_FNR_FN;
+    void (USBHAL::*func)(int frame) = priv->sof;
+    (obj->*func)(sofnum);
+}
+
+USBHAL * USBHAL::instance;
+
+USBHAL::USBHAL(void)
+{
+    /*  init parameter  */
+    USBHAL_Private_t *HALPriv = new(USBHAL_Private_t);
+    hpcd.Instance = USB;
+    /*  initialized Init to zero (constructor does not zero initialized the
+     *  area */
+    /*  initialized all field of init including 0 field  */
+    /*  constructor does not fill with zero */
+    memset(&hpcd.Init, 0, sizeof(hpcd.Init));
+    hpcd.Init.dev_endpoints = NB_ENDPOINT;
+    hpcd.Init.ep0_mps =   MAX_PACKET_SIZE_EP0;
+    hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
+    hpcd.Init.Sof_enable = 1;
+    hpcd.Init.speed = PCD_SPEED_FULL;
+    /*  pass instance for usage inside call back */
+    HALPriv->inst = this;
+    HALPriv->bus_reset = &USBHAL::busReset;
+    HALPriv->suspend_change = &USBHAL::suspendStateChanged;
+    HALPriv->connect_change = &USBHAL::connectStateChanged;
+    HALPriv->sof = &USBHAL::SOF;
+    HALPriv->ep0_setup = &USBHAL::EP0setupCallback;
+    HALPriv->ep_realise = &USBHAL::realiseEndpoint;
+    HALPriv->ep0_in = &USBHAL::EP0in;
+    HALPriv->ep0_out = &USBHAL::EP0out;
+    HALPriv->ep0_read = &USBHAL::EP0read;
+    hpcd.pData = (void*)HALPriv;
+    HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback;
+    HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback;
+    HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback;
+    HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback;
+    HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback;
+    HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback;
+    instance = this;
+    
+    /* Configure USB DM pin. This is optional, and maintained only for user guidance. */
+    __HAL_RCC_GPIOA_CLK_ENABLE();
+    pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB));
+    pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB));
+    
+    /* Enable USB Clock */
+    __HAL_RCC_USB_CLK_ENABLE();
+    
+    /* Enable SYSCFG Clock */
+    __HAL_RCC_SYSCFG_CLK_ENABLE();
+    hpcd.State = HAL_PCD_STATE_RESET;
+    HAL_PCD_Init(&hpcd);
+    
+    /* hardcoded size of FIFO according definition*/
+    HAL_PCDEx_PMAConfig(&hpcd , 0x00 , PCD_SNG_BUF, 0x30);
+    HAL_PCDEx_PMAConfig(&hpcd , 0x80 , PCD_SNG_BUF, 0x70);
+#if 1
+    HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_DBL_BUF, 0x018000b0);
+#else
+    HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_SNG_BUF, 0x180);
+#endif
+    HAL_PCDEx_PMAConfig(&hpcd , 0x83, PCD_SNG_BUF, 0xb0);
+    NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr);
+    NVIC_SetPriority(USBHAL_IRQn, 1);
+    HAL_PCD_Start(&hpcd);
+}
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,18 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM32L053C8.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L072CZ_LRWAN1/USBHAL_STM32L072CZ.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,131 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#ifndef USBHAL_STM32L072CZ_H
+#define USBHAL_STM32L072CZ_H
+
+#define USBHAL_IRQn  USB_IRQn
+
+/*  must be multiple of 4 bytes */
+#define NB_ENDPOINT 8
+#define MAXTRANSFER_SIZE  0x200
+#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3)
+#if (FIFO_USB_RAM_SIZE > 0x500)
+#error "FIFO dimensioning incorrect"
+#endif
+
+typedef struct
+{
+	USBHAL *inst;
+	void (USBHAL::*bus_reset)(void);
+	void (USBHAL::*sof)(int frame);
+	void (USBHAL::*connect_change)(unsigned int  connected);
+	void (USBHAL::*suspend_change)(unsigned int suspended);
+	void (USBHAL::*ep0_setup)(void);
+	void (USBHAL::*ep0_in)(void);
+	void (USBHAL::*ep0_out)(void);
+	void (USBHAL::*ep0_read)(void);
+	bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
+	bool (USBHAL::*epCallback[6])(void);
+	uint8_t epComplete[2*NB_ENDPOINT];
+	/*  memorize dummy buffer used for reception */
+	uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
+	uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
+    gpio_t usb_switch;
+}USBHAL_Private_t;
+
+uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo)
+{
+        return 1024;
+}
+
+void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    gpio_write(&(priv->usb_switch),state);
+}
+
+void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    uint32_t sofnum = (hpcd->Instance->FNR) & USB_FNR_FN;
+    void (USBHAL::*func)(int frame) = priv->sof;
+    (obj->*func)(sofnum);
+}
+
+USBHAL * USBHAL::instance;
+
+USBHAL::USBHAL(void)
+{
+    /*  init parameter  */
+    USBHAL_Private_t *HALPriv = new(USBHAL_Private_t);
+    hpcd.Instance = USB;
+    /*  initialized Init to zero (constructor does not zero initialized the
+     *  area */
+    /*  initialized all field of init including 0 field  */
+    /*  constructor does not fill with zero */
+    memset(&hpcd.Init, 0, sizeof(hpcd.Init));
+    hpcd.Init.dev_endpoints = NB_ENDPOINT;
+    hpcd.Init.ep0_mps =   MAX_PACKET_SIZE_EP0;
+    hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
+    hpcd.Init.Sof_enable = 1;
+    hpcd.Init.speed = PCD_SPEED_FULL;
+    /*  pass instance for usage inside call back */
+    HALPriv->inst = this;
+    HALPriv->bus_reset = &USBHAL::busReset;
+    HALPriv->suspend_change = &USBHAL::suspendStateChanged;
+    HALPriv->connect_change = &USBHAL::connectStateChanged;
+    HALPriv->sof = &USBHAL::SOF;
+    HALPriv->ep0_setup = &USBHAL::EP0setupCallback;
+    HALPriv->ep_realise = &USBHAL::realiseEndpoint;
+    HALPriv->ep0_in = &USBHAL::EP0in;
+    HALPriv->ep0_out = &USBHAL::EP0out;
+    HALPriv->ep0_read = &USBHAL::EP0read;
+    hpcd.pData = (void*)HALPriv;
+    HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback;
+    HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback;
+    HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback;
+    HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback;
+    HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback;
+    HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback;
+    instance = this;
+    
+    /* Configure USB DM pin. This is optional, and maintained only for user guidance. */
+    __HAL_RCC_GPIOA_CLK_ENABLE();
+    pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB));
+    pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB));
+    
+    /* Enable USB Clock */
+    __HAL_RCC_USB_CLK_ENABLE();
+    
+    /* Enable SYSCFG Clock */
+    __HAL_RCC_SYSCFG_CLK_ENABLE();
+    hpcd.State = HAL_PCD_STATE_RESET;
+    HAL_PCD_Init(&hpcd);
+    
+    /* hardcoded size of FIFO according definition*/
+    HAL_PCDEx_PMAConfig(&hpcd , 0x00 , PCD_SNG_BUF, 0x30);
+    HAL_PCDEx_PMAConfig(&hpcd , 0x80 , PCD_SNG_BUF, 0x70);
+    HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_DBL_BUF, 0x018000b0);
+    HAL_PCDEx_PMAConfig(&hpcd , 0x83, PCD_SNG_BUF, 0xb0);
+
+    NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr);
+    NVIC_SetPriority(USBHAL_IRQn, 1);
+    HAL_PCD_Start(&hpcd);
+}
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L072CZ_LRWAN1/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,18 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM32L072CZ.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/TARGET_DISCO_L475VG_IOT01A/USBHAL_STM32L475VG.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,145 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#ifndef USBHAL_STM32L475VG
+#define USBHAL_STM32L475VG
+
+#define USBHAL_IRQn  OTG_FS_IRQn
+
+
+#define NB_ENDPOINT  4
+/*  must be multiple of 4 bytes */
+#define MAXTRANSFER_SIZE  0x200
+#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3)
+#if (FIFO_USB_RAM_SIZE > 0x500)
+#error "FIFO dimensioning incorrect"
+#endif
+
+typedef struct
+{
+    USBHAL *inst;
+    void (USBHAL::*bus_reset)(void);
+    void (USBHAL::*sof)(int frame);
+    void (USBHAL::*connect_change)(unsigned int  connected);
+    void (USBHAL::*suspend_change)(unsigned int suspended);
+    void (USBHAL::*ep0_setup)(void);
+    void (USBHAL::*ep0_in)(void);
+    void (USBHAL::*ep0_out)(void);
+    void (USBHAL::*ep0_read)(void);
+    bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
+    bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void);
+    uint8_t epComplete[8];
+    /*  memorize dummy buffer used for reception */
+    uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
+    uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
+}USBHAL_Private_t;
+
+uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo)
+{
+    uint32_t len;
+    if (fifo == 0) len = hpcd->Instance->DIEPTXF0_HNPTXFSIZ>>16;
+    else
+        len =  hpcd->Instance->DIEPTXF[fifo - 1] >> 16;
+    return len*4;
+}
+void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) {
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
+    uint32_t sofnum = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF) >> 8;
+    void (USBHAL::*func)(int frame) = priv->sof;
+    /* fix me  call with same frame number */
+    (obj->*func)(sofnum);
+}
+
+USBHAL * USBHAL::instance;
+
+USBHAL::USBHAL(void) {
+    /*  init parameter  */
+    USBHAL_Private_t *HALPriv = new(USBHAL_Private_t);
+    /*  initialized all field of init including 0 field  */
+    /*  constructor does not fill with zero */
+    hpcd.Instance = USB_OTG_FS;
+    /*  initialized all field of init including 0 field  */
+    /*  constructor does not fill with zero */
+    memset(&hpcd.Init, 0, sizeof(hpcd.Init));
+    hpcd.Init.dev_endpoints = NB_ENDPOINT;
+    hpcd.Init.ep0_mps =   MAX_PACKET_SIZE_EP0;
+    hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
+    hpcd.Init.Sof_enable = 1;
+    hpcd.Init.speed = PCD_SPEED_FULL;
+    /*  pass instance for usage inside call back */
+    HALPriv->inst = this;
+    HALPriv->bus_reset = &USBHAL::busReset;
+    HALPriv->suspend_change = &USBHAL::suspendStateChanged;
+    HALPriv->connect_change = &USBHAL::connectStateChanged;
+    HALPriv->sof = &USBHAL::SOF;
+    HALPriv->ep0_setup = &USBHAL::EP0setupCallback;
+    HALPriv->ep_realise = &USBHAL::realiseEndpoint;
+    HALPriv->ep0_in = &USBHAL::EP0in;
+    HALPriv->ep0_out = &USBHAL::EP0out;
+    HALPriv->ep0_read = &USBHAL::EP0read;
+    hpcd.pData = (void*)HALPriv;
+    HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback;
+    HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback;
+    HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback;
+    HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback;
+    HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback;
+    HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback;
+    instance = this;
+
+    __HAL_RCC_PWR_CLK_ENABLE();
+
+    HAL_PWREx_EnableVddUSB(); 
+    /* Configure USB VBUS GPIO */
+    __HAL_RCC_GPIOC_CLK_ENABLE();
+
+    /* Configure USB FS GPIOs */
+    __HAL_RCC_GPIOA_CLK_ENABLE();
+
+    /* Configure DM DP Pins */
+    pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
+    pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
+
+    /* Configure VBUS Pin */
+    pin_function(PC_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
+
+    __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
+
+    hpcd.State = HAL_PCD_STATE_RESET;
+
+    HAL_PCD_Init(&hpcd);
+    /* 1.25kbytes */
+    /* min value 16 (= 16 x 4 bytes) */
+    /*  max value 256 (= 1K bytes ) */
+    /*  maximum sum is 0x140 */
+    HAL_PCDEx_SetRxFiFo(&hpcd, (MAXTRANSFER_SIZE/4));
+    /*  bulk/int 64 bytes in FS  */
+    HAL_PCDEx_SetTxFiFo(&hpcd, 0, (MAX_PACKET_SIZE_EP0/4)+1);
+    /*  bulk/int bytes in FS */
+    HAL_PCDEx_SetTxFiFo(&hpcd, 1, (MAX_PACKET_SIZE_EP1/4)+1);
+    HAL_PCDEx_SetTxFiFo(&hpcd, 2, (MAX_PACKET_SIZE_EP2/4));
+    /* ISOchronous */
+    HAL_PCDEx_SetTxFiFo(&hpcd, 3, (MAX_PACKET_SIZE_EP3/4));
+
+    NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr);
+    NVIC_SetPriority( USBHAL_IRQn, 1);
+
+    HAL_PCD_Start(&hpcd);
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/TARGET_DISCO_L475VG_IOT01A/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,18 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM32L475VG.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/USBHAL_STM32L476VG.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,145 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#ifndef USBHAL_STM32L476VG
+#define USBHAL_STM32L476VG
+
+#define USBHAL_IRQn  OTG_FS_IRQn
+
+
+#define NB_ENDPOINT  4
+/*  must be multiple of 4 bytes */
+#define MAXTRANSFER_SIZE  0x200
+#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3)
+#if (FIFO_USB_RAM_SIZE > 0x500)
+#error "FIFO dimensioning incorrect"
+#endif
+
+typedef struct
+{
+    USBHAL *inst;
+    void (USBHAL::*bus_reset)(void);
+    void (USBHAL::*sof)(int frame);
+    void (USBHAL::*connect_change)(unsigned int  connected);
+    void (USBHAL::*suspend_change)(unsigned int suspended);
+    void (USBHAL::*ep0_setup)(void);
+    void (USBHAL::*ep0_in)(void);
+    void (USBHAL::*ep0_out)(void);
+    void (USBHAL::*ep0_read)(void);
+    bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
+    bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void);
+    uint8_t epComplete[8];
+    /*  memorize dummy buffer used for reception */
+    uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
+    uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
+}USBHAL_Private_t;
+
+uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo)
+{
+    uint32_t len;
+    if (fifo == 0) len = hpcd->Instance->DIEPTXF0_HNPTXFSIZ>>16;
+    else
+        len =  hpcd->Instance->DIEPTXF[fifo - 1] >> 16;
+    return len*4;
+}
+void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) {
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
+    uint32_t sofnum = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF) >> 8;
+    void (USBHAL::*func)(int frame) = priv->sof;
+    /* fix me  call with same frame number */
+    (obj->*func)(sofnum);
+}
+
+USBHAL * USBHAL::instance;
+
+USBHAL::USBHAL(void) {
+    /*  init parameter  */
+    USBHAL_Private_t *HALPriv = new(USBHAL_Private_t);
+    /*  initialized all field of init including 0 field  */
+    /*  constructor does not fill with zero */
+    hpcd.Instance = USB_OTG_FS;
+    /*  initialized all field of init including 0 field  */
+    /*  constructor does not fill with zero */
+    memset(&hpcd.Init, 0, sizeof(hpcd.Init));
+    hpcd.Init.dev_endpoints = NB_ENDPOINT;
+    hpcd.Init.ep0_mps =   MAX_PACKET_SIZE_EP0;
+    hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
+    hpcd.Init.Sof_enable = 1;
+    hpcd.Init.speed = PCD_SPEED_FULL;
+    /*  pass instance for usage inside call back */
+    HALPriv->inst = this;
+    HALPriv->bus_reset = &USBHAL::busReset;
+    HALPriv->suspend_change = &USBHAL::suspendStateChanged;
+    HALPriv->connect_change = &USBHAL::connectStateChanged;
+    HALPriv->sof = &USBHAL::SOF;
+    HALPriv->ep0_setup = &USBHAL::EP0setupCallback;
+    HALPriv->ep_realise = &USBHAL::realiseEndpoint;
+    HALPriv->ep0_in = &USBHAL::EP0in;
+    HALPriv->ep0_out = &USBHAL::EP0out;
+    HALPriv->ep0_read = &USBHAL::EP0read;
+    hpcd.pData = (void*)HALPriv;
+    HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback;
+    HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback;
+    HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback;
+    HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback;
+    HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback;
+    HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback;
+    instance = this;
+
+    __HAL_RCC_PWR_CLK_ENABLE();
+
+    HAL_PWREx_EnableVddUSB(); 
+    /* Configure USB VBUS GPIO */
+    __HAL_RCC_GPIOC_CLK_ENABLE();
+
+    /* Configure USB FS GPIOs */
+    __HAL_RCC_GPIOA_CLK_ENABLE();
+
+    /* Configure DM DP Pins */
+    pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
+    pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
+
+    /* Configure VBUS Pin */
+    pin_function(PC_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
+
+    __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
+
+    hpcd.State = HAL_PCD_STATE_RESET;
+
+    HAL_PCD_Init(&hpcd);
+    /* 1.25kbytes */
+    /* min value 16 (= 16 x 4 bytes) */
+    /*  max value 256 (= 1K bytes ) */
+    /*  maximum sum is 0x140 */
+    HAL_PCDEx_SetRxFiFo(&hpcd, (MAXTRANSFER_SIZE/4));
+    /*  bulk/int 64 bytes in FS  */
+    HAL_PCDEx_SetTxFiFo(&hpcd, 0, (MAX_PACKET_SIZE_EP0/4)+1);
+    /*  bulk/int bytes in FS */
+    HAL_PCDEx_SetTxFiFo(&hpcd, 1, (MAX_PACKET_SIZE_EP1/4)+1);
+    HAL_PCDEx_SetTxFiFo(&hpcd, 2, (MAX_PACKET_SIZE_EP2/4));
+    /* ISOchronous */
+    HAL_PCDEx_SetTxFiFo(&hpcd, 3, (MAX_PACKET_SIZE_EP3/4));
+
+    NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr);
+    NVIC_SetPriority( USBHAL_IRQn, 1);
+
+    HAL_PCD_Start(&hpcd);
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/USBHAL_STM_TARGET.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,18 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "USBHAL_STM32L476VG.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/USBHAL_STM32.cpp	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,332 @@
+/* Copyright (c) 2010-2011 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+/* TARGET NOT STM does not support this HAL */
+#ifndef TARGET_STM
+#define USBSTM_HAL_UNSUPPORTED
+#endif
+/* F4 famlily wihtout USB_STM_HAL use another HAL*/
+#if defined(TARGET_STM) && defined(TARGET_STM32F4) && !defined(USB_STM_HAL)
+#define USBSTM_HAL_UNSUPPORTED
+#endif
+
+#ifndef USBSTM_HAL_UNSUPPORTED
+#include "USBHAL.h"
+#include "pinmap.h"
+/* mbed endpoint definition to hal definition   */
+#define EP_ADDR(ep) (((ep) >> 1)|((ep) & 1) << 7)
+/* from hal definition to mbed definition */
+#define ADDR_EPIN(ep) (((ep) << 1) | 1)
+#define ADDR_EPOUT(ep) (((ep) << 1))
+/* id to detect if rx buffer is used or not  */
+
+#include "USBHAL_STM_TARGET.h"
+
+
+/*  this call at device reception completion on a Out Enpoint  */
+void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    uint8_t endpoint = ADDR_EPOUT(epnum);
+    priv->epComplete[endpoint] = 1;
+    /* -2 endpoint 0 In out are not in call back list */
+    if (epnum) {
+        bool (USBHAL::*func)(void) = priv->epCallback[endpoint-2];
+        (obj->*func)();
+    } else {
+        void (USBHAL::*func)(void) = priv->ep0_out;
+        (obj->*func)();
+    }
+}
+
+/*  this is call at device transmission completion on In endpoint */
+void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    uint8_t endpoint = ADDR_EPIN(epnum);
+    priv->epComplete[endpoint] = 1;
+    /*  -2 endpoint 0 In out are not in call back list */
+    if (epnum) {
+        bool (USBHAL::*func)(void) = priv->epCallback[endpoint-2];
+        (obj->*func)();
+    } else {
+        void (USBHAL::*func)(void) = priv->ep0_in;
+        (obj->*func)();
+    }
+}
+/*  This is call at device set up reception  */
+void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    void (USBHAL::*func)(void)=priv->ep0_setup;
+    void (USBHAL::*func1)(void)=priv->ep0_read;
+    (obj->*func)();
+    (obj->*func1)();
+}
+
+void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    void (USBHAL::*func)(unsigned int suspended) = priv->suspend_change;
+    (obj->*func)(1);
+}
+
+void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    void (USBHAL::*func)(unsigned int suspended) = priv->suspend_change;
+    (obj->*func)(0);
+}
+
+void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    void (USBHAL::*func)(unsigned int suspended) = priv->connect_change;
+    (obj->*func)(1);
+}
+
+void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    void (USBHAL::*func)(unsigned int suspended) = priv->connect_change;
+    (obj->*func)(0);
+}
+
+void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    unsigned int i;
+    for(i=0;i<hpcd->Init.dev_endpoints;i++) {
+        priv->epComplete[2*i]=0;
+        HAL_PCD_EP_Close(hpcd,EP_ADDR(2*i));
+        HAL_PCD_EP_Flush(hpcd,EP_ADDR(2*i));
+        priv->epComplete[2*i+1]=0;
+        HAL_PCD_EP_Close(hpcd,EP_ADDR(2*i+1));
+        HAL_PCD_EP_Flush(hpcd,EP_ADDR(2*i+1));
+
+    }
+    void (USBHAL::*func)(void)=priv->bus_reset;
+    bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags) = priv->ep_realise;
+    (obj->*func)();
+    (obj->*ep_realise)(EP0IN, MAX_PACKET_SIZE_EP0,0);
+    (obj->*ep_realise)(EP0OUT, MAX_PACKET_SIZE_EP0,0);
+}
+
+
+/* hal pcd handler , used for STM32 HAL PCD Layer */
+
+uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) {
+    return 0;
+}
+
+USBHAL::~USBHAL(void) {
+	USBHAL_Private_t *HALPriv = (USBHAL_Private_t *)(hpcd.pData);
+	HAL_PCD_DeInit(&hpcd);
+	delete HALPriv;
+}
+
+void USBHAL::connect(void) {
+    NVIC_EnableIRQ(USBHAL_IRQn);
+}
+
+void USBHAL::disconnect(void) {
+    NVIC_DisableIRQ(USBHAL_IRQn);
+}
+
+void USBHAL::configureDevice(void) {
+    // Not needed
+}
+
+void USBHAL::unconfigureDevice(void) {
+    // Not needed
+}
+
+void USBHAL::setAddress(uint8_t address) {
+	HAL_PCD_SetAddress(&hpcd, address);
+    EP0write(0, 0);
+}
+
+bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flags) {
+    uint32_t epIndex = EP_ADDR(endpoint);
+    uint32_t type;
+    uint32_t len;
+    HAL_StatusTypeDef ret;
+    switch (endpoint) {
+    case EP0IN:
+    case EP0OUT:
+        type =  0;
+        break;
+    case EPISO_IN:
+    case EPISO_OUT:
+        type = 1;
+        break;
+    case EPBULK_IN:
+    case EPBULK_OUT:
+        type =  2;
+        break;
+    case EPINT_IN:
+    case EPINT_OUT:
+        type =  3;
+        break;
+    }
+    if (maxPacket > MAXTRANSFER_SIZE) return false;
+    if (epIndex & 0x80) {
+        len = HAL_PCDEx_GetTxFiFo(&hpcd,epIndex & 0x7f);
+        MBED_ASSERT(len >= maxPacket);
+    }
+    ret = HAL_PCD_EP_Open(&hpcd, epIndex,  maxPacket, type);
+    MBED_ASSERT(ret!=HAL_BUSY);
+    return (ret == HAL_OK) ? true:false;
+}
+
+// read setup packet
+void USBHAL::EP0setup(uint8_t *buffer) {
+    memcpy(buffer,  hpcd.Setup, MAX_PACKET_SIZE_SETUP);
+	memset(hpcd.Setup,0,MAX_PACKET_SIZE_SETUP);
+}
+
+void USBHAL::EP0readStage(void) {
+}
+
+void USBHAL::EP0read(void) {
+     USBHAL_Private_t *HALPriv =  (USBHAL_Private_t *)hpcd.pData;
+	 uint32_t epIndex = EP_ADDR(EP0OUT);
+	 uint8_t *pBuf = (uint8_t *)HALPriv->pBufRx0;
+	 HAL_StatusTypeDef ret;
+	 HALPriv->epComplete[EP0OUT] = 2;
+	 ret = HAL_PCD_EP_Receive(&hpcd, epIndex, pBuf, MAX_PACKET_SIZE_EP0 );
+	 MBED_ASSERT(ret!=HAL_BUSY);
+
+}
+
+uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) {
+    USBHAL_Private_t *HALPriv =  (USBHAL_Private_t *)hpcd.pData;
+    uint32_t length = (uint32_t) HAL_PCD_EP_GetRxCount(&hpcd, 0);
+	HALPriv->epComplete[EP0OUT] = 0;
+    if (length) {
+        uint8_t  *buff = (uint8_t *)HALPriv->pBufRx0;
+        memcpy(buffer, buff, length);
+    }
+    return length;
+}
+
+void USBHAL::EP0write(uint8_t *buffer, uint32_t size) {
+    /*  check that endpoint maximum size is not exceeding TX fifo */
+    MBED_ASSERT(hpcd.IN_ep[0].maxpacket >= size);
+    endpointWrite(EP0IN, buffer, size);
+}
+
+void USBHAL::EP0getWriteResult(void) {
+
+}
+
+void USBHAL::EP0stall(void) {
+    stallEndpoint(EP0IN);
+}
+
+EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) {
+    USBHAL_Private_t *HALPriv =  (USBHAL_Private_t *)(hpcd.pData);
+    uint32_t epIndex = EP_ADDR(endpoint);
+    uint8_t* pBuf = (uint8_t *)HALPriv->pBufRx;
+	HAL_StatusTypeDef ret;
+    // clean reception end flag before requesting reception
+    HALPriv->epComplete[endpoint] = 2;
+    ret = HAL_PCD_EP_Receive(&hpcd, epIndex, pBuf, maximumSize);
+	MBED_ASSERT(ret!=HAL_BUSY);
+    return EP_PENDING;
+}
+
+EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) {
+    USBHAL_Private_t *HALPriv =  (USBHAL_Private_t *)(hpcd.pData);
+	if (HALPriv->epComplete[endpoint]==0) {
+		/*  no reception possible !!! */
+		bytesRead = 0;
+        return EP_COMPLETED;
+    }else if ((HALPriv->epComplete[endpoint]!=1))
+	return EP_PENDING;
+    uint32_t epIndex = EP_ADDR(endpoint);
+    uint8_t  *buff = (uint8_t *)HALPriv->pBufRx;
+    uint32_t length = (uint32_t) HAL_PCD_EP_GetRxCount(&hpcd, epIndex);
+    memcpy(buffer, buff, length);
+    *bytesRead = length;
+	HALPriv->epComplete[endpoint]= 0;
+    return EP_COMPLETED;
+}
+
+EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) {
+	USBHAL_Private_t *HALPriv =  (USBHAL_Private_t *)(hpcd.pData);
+    uint32_t epIndex = EP_ADDR(endpoint);
+    HAL_StatusTypeDef ret;
+    // clean transmission end flag before requesting transmission
+    HALPriv->epComplete[endpoint] = 2;
+    ret = HAL_PCD_EP_Transmit(&hpcd, epIndex, data, size);
+	MBED_ASSERT(ret!=HAL_BUSY);
+    // update the status
+    if (ret != HAL_OK) return EP_INVALID; 
+    // fix me return is too simple
+    return EP_PENDING;
+}
+
+EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) {
+	USBHAL_Private_t *HALPriv =  (USBHAL_Private_t *)(hpcd.pData);
+    if (HALPriv->epComplete[endpoint] == 1)
+        return EP_COMPLETED;
+    return EP_PENDING;
+}
+
+void USBHAL::stallEndpoint(uint8_t endpoint) {
+    USBHAL_Private_t *HALPriv =  (USBHAL_Private_t *)(hpcd.pData);
+	HAL_StatusTypeDef ret;
+	HALPriv->epComplete[endpoint] = 0;
+	ret = HAL_PCD_EP_SetStall(&hpcd, EP_ADDR(endpoint));
+	MBED_ASSERT(ret!=HAL_BUSY);
+}
+
+void USBHAL::unstallEndpoint(uint8_t endpoint) {
+	HAL_StatusTypeDef ret;
+    ret = HAL_PCD_EP_ClrStall(&hpcd, EP_ADDR(endpoint));
+	MBED_ASSERT(ret!=HAL_BUSY);
+
+}
+
+bool USBHAL::getEndpointStallState(uint8_t endpoint) {
+    return false;
+}
+
+void USBHAL::remoteWakeup(void) {
+}
+
+
+void USBHAL::_usbisr(void) {
+    instance->usbisr();
+}
+
+
+void USBHAL::usbisr(void) {
+
+    HAL_PCD_IRQHandler(&instance->hpcd);
+}
+#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/TARGET_STM/USBHAL_STM_144_64pins.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,132 @@
+/* Copyright (c) 2016 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#ifndef USBHAL_STM32_144_64
+#define USBHAL_STM32_144_64
+
+#define USBHAL_IRQn  OTG_FS_IRQn
+/*  must be multiple of 4 bytes */
+#define NB_ENDPOINT 4
+#define MAXTRANSFER_SIZE  0x200
+#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3)
+#if (FIFO_USB_RAM_SIZE > 0x500)
+#error "FIFO dimensioning incorrect"
+#endif
+
+typedef struct
+{
+    USBHAL *inst;
+    void (USBHAL::*bus_reset)(void);
+    void (USBHAL::*sof)(int frame);
+    void (USBHAL::*connect_change)(unsigned int  connected);
+    void (USBHAL::*suspend_change)(unsigned int suspended);
+    void (USBHAL::*ep0_setup)(void);
+    void (USBHAL::*ep0_in)(void);
+    void (USBHAL::*ep0_out)(void);
+    void (USBHAL::*ep0_read)(void);
+    bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
+    bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void);
+    /*  memorize dummy buffer used for reception */
+    uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
+    uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
+    uint8_t epComplete[2*NB_ENDPOINT];
+}USBHAL_Private_t;
+
+uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo)
+{
+    uint32_t len;
+    if (fifo == 0) len = hpcd->Instance->DIEPTXF0_HNPTXFSIZ>>16;
+    else
+        len =  hpcd->Instance->DIEPTXF[fifo - 1] >> 16;
+    return len*4;
+}
+void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) 
+{
+    USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
+    USBHAL *obj= priv->inst;
+    USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
+    uint32_t sofnum = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF) >> 8;
+    void (USBHAL::*func)(int frame) = priv->sof;
+    (obj->*func)(sofnum);
+}
+
+
+USBHAL * USBHAL::instance;
+
+USBHAL::USBHAL(void) {
+    /*  init parameter  */
+    USBHAL_Private_t *HALPriv = new(USBHAL_Private_t);
+    hpcd.Instance = USB_OTG_FS;
+    memset(&hpcd.Init, 0, sizeof(hpcd.Init));
+    hpcd.Init.dev_endpoints = NB_ENDPOINT;
+    hpcd.Init.ep0_mps =   MAX_PACKET_SIZE_EP0;
+    hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
+    hpcd.Init.Sof_enable = 1;
+
+    hpcd.Init.speed = PCD_SPEED_FULL;
+    //hpcd.Init.vbus_sensing_enable = 0;
+    //hpcd.Init.lpm_enable = 0;
+    /*  pass instance for usage inside call back */
+    HALPriv->inst = this;
+    HALPriv->bus_reset = &USBHAL::busReset;
+    HALPriv->suspend_change = &USBHAL::suspendStateChanged;
+    HALPriv->connect_change = &USBHAL::connectStateChanged;
+    HALPriv->sof = &USBHAL::SOF;
+    HALPriv->ep0_setup = &USBHAL::EP0setupCallback;
+    HALPriv->ep_realise = &USBHAL::realiseEndpoint;
+    HALPriv->ep0_in = &USBHAL::EP0in;
+    HALPriv->ep0_out = &USBHAL::EP0out;
+    HALPriv->ep0_read = &USBHAL::EP0read;
+    hpcd.pData = (void*)HALPriv;
+    HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback;
+    HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback;
+    HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback;
+    HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback;
+    HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback;
+    HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback;
+    instance = this;
+    // Enable power and clocking
+    /*  board 144 pin all similar  */
+    __HAL_RCC_GPIOA_CLK_ENABLE();
+    pin_function(PA_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
+    pin_function(PA_9, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, GPIO_AF10_OTG_FS));
+    pin_function(PA_10, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_PULLUP, GPIO_AF10_OTG_FS));
+    pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
+    pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
+
+    __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
+    __HAL_RCC_SYSCFG_CLK_ENABLE();
+    hpcd.State = HAL_PCD_STATE_RESET;
+    HAL_PCD_Init(&hpcd);
+    /* 1.25kbytes */
+    /* min value 16 (= 16 x 4 bytes) */
+    /*  max value 256 (= 1K bytes ) */
+    /*  maximum sum is 0x140 */
+    HAL_PCDEx_SetRxFiFo(&hpcd, (MAXTRANSFER_SIZE/4));
+    /*  bulk/int 64 bytes in FS  */
+    HAL_PCDEx_SetTxFiFo(&hpcd, 0, (MAX_PACKET_SIZE_EP0/4)+1);
+    /*  bulk/int bytes in FS */
+    HAL_PCDEx_SetTxFiFo(&hpcd, 1, (MAX_PACKET_SIZE_EP1/4)+1);
+    HAL_PCDEx_SetTxFiFo(&hpcd, 2, (MAX_PACKET_SIZE_EP2/4));
+    /* ISOchronous */
+    HAL_PCDEx_SetTxFiFo(&hpcd, 3, (MAX_PACKET_SIZE_EP3/4));
+    NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr);
+    NVIC_SetPriority(USBHAL_IRQn, 1);
+    HAL_PCD_Start(&hpcd);
+}
+#endif
+
--- a/USBDevice/TARGET_Silicon_Labs/inc/em_usb.h	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBDevice/TARGET_Silicon_Labs/inc/em_usb.h	Thu Jul 20 10:14:36 2017 +0100
@@ -33,6 +33,18 @@
 #include <string.h>
 #include <stddef.h>
 #include "em_common.h"
+/* Workaround for em_common naming change so that we don't need to rework the
+   entire USB HAL */
+#define EFM32_PACK_START(x)        SL_PACK_START(x)
+#define EFM32_PACK_END()           SL_PACK_END()
+#define EFM32_MIN(a, b)            SL_MIN(a, b)
+#define EFM32_MAX(a, b)            SL_MAX(a, b)
+#define EFM32_ATTRIBUTE_PACKED     SL_ATTRIBUTE_PACKED
+#define EFM32_ATTRIBUTE_ALIGN(X)   SL_ATTRIBUTE_ALIGN(X)
+#define EFM32_ALIGN(X)             SL_ALIGN(X)
+#define EFM32_WEAK                 SL_WEAK
+#define EFM32_ATTRIBUTE_SECTION(X) SL_ATTRIBUTE_SECTION(X)
+
 #include "em_int.h"
 
 #if defined( USB_USE_PRINTF )
--- a/USBDevice/USBEndpoints.h	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBDevice/USBEndpoints.h	Thu Jul 20 10:14:36 2017 +0100
@@ -43,14 +43,20 @@
 #include "USBEndpoints_LPC11U.h"
 #elif defined(TARGET_KL25Z) | defined(TARGET_KL26Z) | defined(TARGET_KL27Z) | defined(TARGET_KL43Z) | defined(TARGET_KL46Z) | defined(TARGET_K20D50M) | defined(TARGET_K64F) | defined(TARGET_K22F) | defined(TARGET_TEENSY3_1)
 #include "USBEndpoints_KL25Z.h"
-#elif defined (TARGET_STM32F4)
+#elif !defined(USB_STM_HAL) && defined(TARGET_STM32F4)
 #include "USBEndpoints_STM32F4.h"
+#elif defined (TARGET_STM32F4) || defined (TARGET_STM32F2) || defined (TARGET_STM32F7) || defined (TARGET_STM32F3) || defined(TARGET_STM32L0) || defined(TARGET_STM32L4) || defined(TARGET_STM32F1)
+#include "USBEndpoints_STM32.h"
 #elif defined (TARGET_RZ_A1H) || defined (TARGET_VK_RZ_A1H)
 #include "USBEndpoints_RZ_A1H.h"
 #elif defined(TARGET_Maxim)
 #include "USBEndpoints_Maxim.h"
 #elif defined(TARGET_EFM32GG_STK3700) || defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32HG_STK3400)
 #include "USBEndpoints_EFM32.h"
+#elif defined(TARGET_NUMAKER_PFM_NUC472)
+#include "USBEndpoints_NUC472.h"
+#elif defined(TARGET_NUMAKER_PFM_M453)
+#include "USBEndpoints_M453.h"
 #else
 #error "Unknown target type"
 #endif
--- a/USBDevice/USBEndpoints_KL25Z.h	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBDevice/USBEndpoints_KL25Z.h	Thu Jul 20 10:14:36 2017 +0100
@@ -16,7 +16,7 @@
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#define NUMBER_OF_LOGICAL_ENDPOINTS (16)
+#define NUMBER_OF_LOGICAL_ENDPOINTS (4)
 #define NUMBER_OF_PHYSICAL_ENDPOINTS (NUMBER_OF_LOGICAL_ENDPOINTS * 2)
 
 /* Define physical endpoint numbers */
@@ -31,30 +31,6 @@
 #define EP2IN       (5)
 #define EP3OUT      (6)
 #define EP3IN       (7)
-#define EP4OUT      (8)
-#define EP4IN       (9)
-#define EP5OUT      (10)
-#define EP5IN       (11)
-#define EP6OUT      (12)
-#define EP6IN       (13)
-#define EP7OUT      (14)
-#define EP7IN       (15)
-#define EP8OUT      (16)
-#define EP8IN       (17)
-#define EP9OUT      (18)
-#define EP9IN       (19)
-#define EP10OUT     (20)
-#define EP10IN      (21)
-#define EP11OUT     (22)
-#define EP11IN      (23)
-#define EP12OUT     (24)
-#define EP12IN      (25)
-#define EP13OUT     (26)
-#define EP13IN      (27)
-#define EP14OUT     (28)
-#define EP14IN      (29)
-#define EP15OUT     (30)
-#define EP15IN      (31)
 
 /* Maximum Packet sizes */
 
@@ -62,18 +38,6 @@
 #define MAX_PACKET_SIZE_EP1  (64)
 #define MAX_PACKET_SIZE_EP2  (64)
 #define MAX_PACKET_SIZE_EP3  (1023)
-#define MAX_PACKET_SIZE_EP4  (64)
-#define MAX_PACKET_SIZE_EP5  (64)
-#define MAX_PACKET_SIZE_EP6  (64)
-#define MAX_PACKET_SIZE_EP7  (64)
-#define MAX_PACKET_SIZE_EP8  (64)
-#define MAX_PACKET_SIZE_EP9  (64)
-#define MAX_PACKET_SIZE_EP10 (64)
-#define MAX_PACKET_SIZE_EP11 (64)
-#define MAX_PACKET_SIZE_EP12 (64)
-#define MAX_PACKET_SIZE_EP13 (64)
-#define MAX_PACKET_SIZE_EP14 (64)
-#define MAX_PACKET_SIZE_EP15 (64)
 
 /* Generic endpoints - intended to be portable accross devices */
 /* and be suitable for simple USB devices. */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/USBEndpoints_M453.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,79 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2015-2016 Nuvoton
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#define NU_MAX_EPX_BUFSIZE      4096
+#define NU_EP2EPL(ep)           ((ep) >> 1)
+#define NU_EP2EPH(ep)           (((ep) >> 1) + 1)
+#define NU_EPL2EPH(ep)          ((ep) + 1)
+#define NU_EPH2EPL(ep)          ((ep) - 1)
+#define NU_EP_DIR_Pos           0
+#define NU_EP_DIR_Msk           (1 << NU_EP_DIR_Pos)
+#define NU_EP_DIR_OUT           0
+#define NU_EP_DIR_IN            1
+
+#define NU_EP_TYPE(ep)          (((ep) & NU_EP_TYPE_Msk) >> NU_EP_TYPE_Pos)
+#define NU_EP_NUM(ep)           (((ep) & NU_EP_NUM_Msk) >> NU_EP_NUM_Pos)
+#define NU_EP_DIR(ep)           (((ep) & NU_EP_DIR_Msk) >> NU_EP_DIR_Pos)
+#define NU_EP_NUM_DIR(ep)       ((NU_EP_NUM(ep) << 1) | NU_EP_DIR(ep))
+
+#define NUMBER_OF_PHYSICAL_ENDPOINTS    8
+#define EP0OUT      (0)
+#define EP0IN       (1)
+#define EP1OUT      (2)
+#define EP1IN       (3)
+#define EP2OUT      (4)
+#define EP2IN       (5)
+#define EP3OUT      (6)
+#define EP3IN       (7)
+#define EP4OUT      (8)
+#define EP4IN       (9)
+#define EP5OUT     (10)
+#define EP5IN      (11)
+#define EP6OUT     (12)
+#define EP6IN      (13)
+
+/* Maximum Packet sizes */
+#define MAX_PACKET_SIZE_EP0     64
+#define MAX_PACKET_SIZE_EP1     64
+#define MAX_PACKET_SIZE_EP2     64
+#define MAX_PACKET_SIZE_EP3     0x60
+#define MAX_PACKET_SIZE_EP4     64
+#define MAX_PACKET_SIZE_EP5     64
+#define MAX_PACKET_SIZE_EP6     64
+#define MAX_PACKET_SIZE_EP7     64
+
+/* Generic endpoints - intended to be portable accross devices */
+/* and be suitable for simple USB devices. */
+
+/* Bulk endpoints */
+#define EPBULK_OUT            EP5OUT
+#define EPBULK_IN             EP6IN
+#define EPBULK_OUT_callback   EP5_OUT_callback
+#define EPBULK_IN_callback    EP6_IN_callback
+/* Interrupt endpoints */
+#define EPINT_OUT             EP1OUT
+#define EPINT_IN              EP2IN
+#define EPINT_OUT_callback    EP1_OUT_callback
+#define EPINT_IN_callback     EP2_IN_callback
+/* Isochronous endpoints */
+#define EPISO_OUT             EP3OUT
+#define EPISO_IN              EP4IN
+#define EPISO_OUT_callback    EP3_OUT_callback
+#define EPISO_IN_callback     EP4_IN_callback
+
+#define MAX_PACKET_SIZE_EPBULK  64
+#define MAX_PACKET_SIZE_EPINT   64
+#define MAX_PACKET_SIZE_EPISO   1023
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/USBEndpoints_NUC472.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,89 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2015-2016 Nuvoton
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#define NU_MAX_EPX_BUFSIZE      4096
+#define NU_EP2EPL(ep)           ((ep) >> 1)
+#define NU_EP2EPH(ep)           (((ep) >> 1) - 1)
+#define NU_EPX2EP(ep)           ((ep == CEP) ? EP0OUT : ((ep) - EPA + EP1OUT))
+#define NU_EPL2EPH(ep)          ((ep) - 1)
+#define NU_EPH2EPL(ep)          ((ep) + 1)
+#define NU_EP_DIR_Pos           0
+#define NU_EP_DIR_Msk           (1 << NU_EP_DIR_Pos)
+#define NU_EP_DIR_OUT           0
+#define NU_EP_DIR_IN            1
+
+#define NU_EP_TYPE(ep)          (((ep) & NU_EP_TYPE_Msk) >> NU_EP_TYPE_Pos)
+#define NU_EP_NUM(ep)           (((ep) & NU_EP_NUM_Msk) >> NU_EP_NUM_Pos)
+#define NU_EP_DIR(ep)           (((ep) & NU_EP_DIR_Msk) >> NU_EP_DIR_Pos)
+#define NU_EP_NUM_DIR(ep)       ((NU_EP_NUM(ep) << 1) | NU_EP_DIR(ep))
+
+#define NUMBER_OF_PHYSICAL_ENDPOINTS    12
+
+#define EP0OUT      (0)
+#define EP0IN       (1)
+#define EP1OUT      (2)
+#define EP1IN       (3)
+#define EP2OUT      (4)
+#define EP2IN       (5)
+#define EP3OUT      (6)
+#define EP3IN       (7)
+#define EP4OUT      (8)
+#define EP4IN       (9)
+#define EP5OUT     (10)
+#define EP5IN      (11)
+#define EP6OUT     (12)
+#define EP6IN      (13)
+
+/* Maximum Packet sizes */
+#define MAX_PACKET_SIZE_EP0     64
+#define MAX_PACKET_SIZE_EP1     64
+#define MAX_PACKET_SIZE_EP2     64
+#define MAX_PACKET_SIZE_EP3     0x60
+#define MAX_PACKET_SIZE_EP4     64
+#define MAX_PACKET_SIZE_EP5     64
+#define MAX_PACKET_SIZE_EP6     64
+#define MAX_PACKET_SIZE_EP7     64
+#define MAX_PACKET_SIZE_EP8     64
+#define MAX_PACKET_SIZE_EP9     64
+#define MAX_PACKET_SIZE_EP10    64
+#define MAX_PACKET_SIZE_EP11    64
+
+/* Generic endpoints - intended to be portable accross devices */
+/* and be suitable for simple USB devices. */
+
+/* Bulk endpoints */
+#define EPBULK_OUT            EP5OUT
+#define EPBULK_IN             EP6IN
+#define EPBULK_OUT_callback   EP5_OUT_callback
+#define EPBULK_IN_callback    EP6_IN_callback
+/* Interrupt endpoints */
+#define EPINT_OUT             EP1OUT
+#define EPINT_IN              EP2IN
+#define EPINT_OUT_callback    EP1_OUT_callback
+#define EPINT_IN_callback     EP2_IN_callback
+/* Isochronous endpoints */
+#define EPISO_OUT             EP3OUT
+#define EPISO_IN              EP4IN
+#define EPISO_OUT_callback    EP3_OUT_callback
+#define EPISO_IN_callback     EP4_IN_callback
+
+#define MAX_PACKET_SIZE_EPBULK  64
+#define MAX_PACKET_SIZE_EPINT   64
+#define MAX_PACKET_SIZE_EPISO   1023
+
+#define USBD_GET_EP_MAX_PAYLOAD(ep)     (*((__IO uint32_t *) ((uint32_t)&USBD->EPAMPS + (uint32_t)((ep)*0x28)))) 
+#define USBD_GET_EP_DATA_COUNT(ep)      ((*((__IO uint32_t *) ((uint32_t)&USBD->EPADATCNT + (uint32_t)((ep)*0x28)))) & 0xFFFFF)
+#define USBD_SET_EP_SHORT_PACKET(ep)    (*((__IO uint32_t *) ((uint32_t)&USBD->EPARSPCTL + (uint32_t)((ep)*0x28))) = (*((__IO uint32_t *) ((uint32_t)&USBD->EPARSPCTL + (uint32_t)((ep)*0x28)))) & 0x10 | 0x40)
+#define USBD_GET_EP_INT_EN(ep)          (*((__IO uint32_t *) ((uint32_t)&USBD->EPAINTEN + (uint32_t)((ep)*0x28))))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/USBEndpoints_STM32.h	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,67 @@
+/* Copyright (c) 2010-2011 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#define NUMBER_OF_LOGICAL_ENDPOINTS (4)
+#define NUMBER_OF_PHYSICAL_ENDPOINTS (NUMBER_OF_LOGICAL_ENDPOINTS * 2)
+
+/* Define physical endpoint numbers */
+
+/*      Endpoint    No.     Type(s)       MaxPacket   DoubleBuffer  */
+/*      ----------------    ------------  ----------  ---           */
+#define EP0OUT      (0)  /* Control       64          No            */
+#define EP0IN       (1)  /* Control       64          No            */
+#define EP1OUT      (2)  /* Int/Bulk/Iso  64/64/1023  Yes           */
+#define EP1IN       (3)  /* Int/Bulk/Iso  64/64/1023  Yes           */
+#define EP2OUT      (4)  /* Int/Bulk/Iso  64/64/1023  Yes           */
+#define EP2IN       (5)  /* Int/Bulk/Iso  64/64/1023  Yes           */
+#define EP3OUT      (6)  /* Int/Bulk/Iso  64/64/1023  Yes           */
+#define EP3IN       (7)  /* Int/Bulk/Iso  64/64/1023  Yes           */
+
+/* Maximum Packet sizes */
+#define MAX_PACKET_SIZE_SETUP (48)
+#define MAX_PACKET_SIZE_EP0 (64)
+#define MAX_PACKET_SIZE_EP1 (64) /* Int/Bulk */
+#define MAX_PACKET_SIZE_EP2 (64) /* Int/Bulk */
+#define MAX_PACKET_SIZE_EP3 (200) /* Int/Bulk/iso (44100 stereo 16 bits) */
+
+#define MAX_PACKET_SIZE_EP1_ISO (1023) /* Isochronous */
+#define MAX_PACKET_SIZE_EP2_ISO (1023) /* Isochronous */
+#define MAX_PACKET_SIZE_EP3_ISO (1023) /* Isochronous */
+
+/* Generic endpoints - intended to be portable accross devices */
+/* and be suitable for simple USB devices. */
+
+/* Bulk endpoint */
+#define EPBULK_OUT  (EP2OUT)
+#define EPBULK_IN   (EP2IN)
+#define EPBULK_OUT_callback   EP2_OUT_callback
+#define EPBULK_IN_callback    EP2_IN_callback
+/* Interrupt endpoint */
+#define EPINT_OUT   (EP1OUT)
+#define EPINT_IN    (EP1IN)
+#define EPINT_OUT_callback    EP1_OUT_callback
+#define EPINT_IN_callback     EP1_IN_callback
+/* Isochronous endpoint */
+#define EPISO_OUT   (EP3OUT)
+#define EPISO_IN    (EP3IN)
+#define EPISO_OUT_callback    EP3_OUT_callback
+#define EPISO_IN_callback     EP3_IN_callback
+
+#define MAX_PACKET_SIZE_EPBULK  (MAX_PACKET_SIZE_EP2)
+#define MAX_PACKET_SIZE_EPINT   (MAX_PACKET_SIZE_EP1)
+#define MAX_PACKET_SIZE_EPISO   (MAX_PACKET_SIZE_EP3_ISO)
--- a/USBDevice/USBHAL.h	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBDevice/USBHAL.h	Thu Jul 20 10:14:36 2017 +0100
@@ -21,7 +21,7 @@
 
 #include "mbed.h"
 #include "USBEndpoints.h"
-#include "toolchain.h"
+#include "mbed_toolchain.h"
 
 //#ifdef __GNUC__
 //#define __packed __attribute__ ((__packed__))
@@ -68,6 +68,23 @@
     virtual void suspendStateChanged(unsigned int suspended){};
     virtual void SOF(int frameNumber){};
 
+#if defined(TARGET_NUMAKER_PFM_NUC472) || defined(TARGET_NUMAKER_PFM_M453)
+    // NUC472/M453 USB doesn't support configuration of the same EP number for IN/OUT simultaneously.
+    virtual bool EP1_OUT_callback(){return false;};
+    virtual bool EP2_IN_callback(){return false;};
+    virtual bool EP3_OUT_callback(){return false;};
+    virtual bool EP4_IN_callback(){return false;};
+    virtual bool EP5_OUT_callback(){return false;};
+    virtual bool EP6_IN_callback(){return false;};
+#if ! (defined(TARGET_NUMAKER_PFM_M453))
+    virtual bool EP7_OUT_callback(){return false;};
+    virtual bool EP8_IN_callback(){return false;};
+    virtual bool EP9_OUT_callback(){return false;};
+    virtual bool EP10_IN_callback(){return false;};
+    virtual bool EP11_OUT_callback(){return false;};
+    virtual bool EP12_IN_callback(){return false;};
+#endif
+#else
     virtual bool EP1_OUT_callback(){return false;};
     virtual bool EP1_IN_callback(){return false;};
     virtual bool EP2_OUT_callback(){return false;};
@@ -102,6 +119,7 @@
     virtual bool EP15_IN_callback(){return false;};
 #endif
 #endif
+#endif
 
 private:
     void usbisr(void);
@@ -109,11 +127,15 @@
     static USBHAL * instance;
 
 #if defined(TARGET_LPC11UXX) || defined(TARGET_LPC11U6X) || defined(TARGET_LPC1347) || defined(TARGET_LPC1549)
-        bool (USBHAL::*epCallback[10 - 2])(void);
-#elif defined(TARGET_STM32F4)
-        bool (USBHAL::*epCallback[8 - 2])(void);
+    bool (USBHAL::*epCallback[10 - 2])(void);
+#elif (defined(TARGET_STM32F4) && !defined(USB_STM_HAL)) || defined(TARGET_NUMAKER_PFM_M453)
+    bool (USBHAL::*epCallback[8 - 2])(void);
+#elif defined(TARGET_STM)
+    PCD_HandleTypeDef hpcd;
+#elif defined(TARGET_NUMAKER_PFM_NUC472)
+    bool (USBHAL::*epCallback[14 - 2])(void);
 #else
-        bool (USBHAL::*epCallback[32 - 2])(void);
+    bool (USBHAL::*epCallback[32 - 2])(void);
 #endif
 
 
--- a/USBDevice/USBHAL_KL25Z.cpp	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBDevice/USBHAL_KL25Z.cpp	Thu Jul 20 10:14:36 2017 +0100
@@ -18,6 +18,9 @@
 
 #if defined(TARGET_KL25Z) | defined(TARGET_KL43Z) | defined(TARGET_KL46Z) | defined(TARGET_K20D50M) | defined(TARGET_K64F) | defined(TARGET_K22F) | defined(TARGET_TEENSY3_1)
 
+#if defined(TARGET_KSDK2_MCUS)
+#include "fsl_common.h"
+#endif
 #include "USBHAL.h"
 
 USBHAL * USBHAL::instance;
@@ -61,13 +64,12 @@
     uint32_t  address;    // Addr
 } BDT;
 
+// there are:
+//    * 4 bidirectionnal endpt -> 8 physical endpt
+//    * as there are ODD and EVEN buffer -> 8*2 bdt
+MBED_ALIGN(512) BDT bdt[NUMBER_OF_PHYSICAL_ENDPOINTS * 2];  // 512 bytes aligned!
 
-// there are:
-//    * 16 bidirectionnal endpt -> 32 physical endpt
-//    * as there are ODD and EVEN buffer -> 32*2 bdt
-__attribute__((__aligned__(512))) BDT bdt[NUMBER_OF_PHYSICAL_ENDPOINTS * 2];
-uint8_t * endpoint_buffer[(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2];
-uint8_t * endpoint_buffer_iso[2*2];
+uint8_t * endpoint_buffer[NUMBER_OF_PHYSICAL_ENDPOINTS * 2];
 
 static uint8_t set_addr = 0;
 static uint8_t addr = 0;
@@ -86,7 +88,7 @@
     // Disable IRQ
     NVIC_DisableIRQ(USB0_IRQn);
 
-#if defined(TARGET_K64F)
+#if (defined(FSL_FEATURE_SOC_MPU_COUNT) && (FSL_FEATURE_SOC_MPU_COUNT > 0U))
     MPU->CESR=0;
 #endif
     // fill in callback array
@@ -98,41 +100,10 @@
     epCallback[5] = &USBHAL::EP3_IN_callback;
     epCallback[6] = &USBHAL::EP4_OUT_callback;
     epCallback[7] = &USBHAL::EP4_IN_callback;
-    epCallback[8] = &USBHAL::EP5_OUT_callback;
-    epCallback[9] = &USBHAL::EP5_IN_callback;
-    epCallback[10] = &USBHAL::EP6_OUT_callback;
-    epCallback[11] = &USBHAL::EP6_IN_callback;
-    epCallback[12] = &USBHAL::EP7_OUT_callback;
-    epCallback[13] = &USBHAL::EP7_IN_callback;
-    epCallback[14] = &USBHAL::EP8_OUT_callback;
-    epCallback[15] = &USBHAL::EP8_IN_callback;
-    epCallback[16] = &USBHAL::EP9_OUT_callback;
-    epCallback[17] = &USBHAL::EP9_IN_callback;
-    epCallback[18] = &USBHAL::EP10_OUT_callback;
-    epCallback[19] = &USBHAL::EP10_IN_callback;
-    epCallback[20] = &USBHAL::EP11_OUT_callback;
-    epCallback[21] = &USBHAL::EP11_IN_callback;
-    epCallback[22] = &USBHAL::EP12_OUT_callback;
-    epCallback[23] = &USBHAL::EP12_IN_callback;
-    epCallback[24] = &USBHAL::EP13_OUT_callback;
-    epCallback[25] = &USBHAL::EP13_IN_callback;
-    epCallback[26] = &USBHAL::EP14_OUT_callback;
-    epCallback[27] = &USBHAL::EP14_IN_callback;
-    epCallback[28] = &USBHAL::EP15_OUT_callback;
-    epCallback[29] = &USBHAL::EP15_IN_callback;
 
-#if defined(TARGET_KL43Z)
+#if defined(TARGET_KL43Z) || defined(TARGET_K22F) || defined(TARGET_K64F)
     // enable USBFS clock
-    SIM->SCGC4 |= SIM_SCGC4_USBFS_MASK;
-
-    // enable the IRC48M clock
-    USB0->CLK_RECOVER_IRC_EN |= USB_CLK_RECOVER_IRC_EN_IRC_EN_MASK;
-
-    // enable the USB clock recovery tuning
-    USB0->CLK_RECOVER_CTRL |= USB_CLK_RECOVER_CTRL_CLOCK_RECOVER_EN_MASK;
-
-    // choose usb src clock
-    SIM->SOPT2 |= SIM_SOPT2_USBSRC_MASK;
+    CLOCK_EnableUsbfs0Clock(kCLOCK_UsbSrcIrc48M, 48000000U);
 #else
     // choose usb src as PLL
     SIM->SOPT2 &= ~SIM_SOPT2_PLLFLLSEL_MASK;
@@ -148,10 +119,6 @@
     NVIC_EnableIRQ(USB0_IRQn);
 
     // USB Module Configuration
-    // Reset USB Module
-    USB0->USBTRC0 |= USB_USBTRC0_USBRESET_MASK;
-    while(USB0->USBTRC0 & USB_USBTRC0_USBRESET_MASK);
-
     // Set BDT Base Register
     USB0->BDTPAGE1 = (uint8_t)((uint32_t)bdt>>8);
     USB0->BDTPAGE2 = (uint8_t)((uint32_t)bdt>>16);
@@ -170,6 +137,10 @@
     USB0->USBCTRL &= ~(USB_USBCTRL_PDE_MASK | USB_USBCTRL_SUSP_MASK);
 
     USB0->USBTRC0 |= 0x40;
+
+    /* Allocate control endpoint buffers */
+    endpoint_buffer[EP_BDT_IDX(0, TX, ODD)] = (uint8_t *)malloc(MAX_PACKET_SIZE_EP0);
+    endpoint_buffer[EP_BDT_IDX(0, RX, ODD)] = (uint8_t *)malloc(MAX_PACKET_SIZE_EP0);
 }
 
 USBHAL::~USBHAL(void) { }
@@ -179,6 +150,15 @@
     USB0->CTL |= USB_CTL_USBENSOFEN_MASK;
     // Pull up enable
     USB0->CONTROL |= USB_CONTROL_DPPULLUPNONOTG_MASK;
+
+    // Allocate endpoint buffers; do allocate control endpoint buffers
+    for (int i = 4; i < (NUMBER_OF_PHYSICAL_ENDPOINTS * 2); i++) {
+        if ((i == EPISO_OUT) || (i == EPISO_IN)) {
+            endpoint_buffer[i] = (uint8_t *)malloc(MAX_PACKET_SIZE_EPISO);
+        } else {
+            endpoint_buffer[i] = (uint8_t *)malloc(MAX_PACKET_SIZE_EPBULK);
+        }
+    }
 }
 
 void USBHAL::disconnect(void) {
@@ -187,15 +167,11 @@
     // Pull up disable
     USB0->CONTROL &= ~USB_CONTROL_DPPULLUPNONOTG_MASK;
 
-    //Free buffers if required:
-    for (int i = 0; i<(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2; i++) {
+    //Free buffers if required; do not free the control endpoint buffers
+    for (int i = 4; i < (NUMBER_OF_PHYSICAL_ENDPOINTS * 2); i++) {
         free(endpoint_buffer[i]);
         endpoint_buffer[i] = NULL;
     }
-    free(endpoint_buffer_iso[2]);
-    endpoint_buffer_iso[2] = NULL;
-    free(endpoint_buffer_iso[0]);
-    endpoint_buffer_iso[0] = NULL;
 }
 
 void USBHAL::configureDevice(void) {
@@ -226,25 +202,12 @@
 
     if ((flags & ISOCHRONOUS) == 0) {
         handshake_flag = USB_ENDPT_EPHSHK_MASK;
-        if (IN_EP(endpoint)) {
-            if (endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] == NULL)
-                endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] = (uint8_t *) malloc (64);
-            buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)][0];
-        } else {
-            if (endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] == NULL)
-                endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] = (uint8_t *) malloc (64);
-            buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)][0];
-        }
+    }
+
+    if (IN_EP(endpoint)) {
+        buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)][0];
     } else {
-        if (IN_EP(endpoint)) {
-            if (endpoint_buffer_iso[2] == NULL)
-                endpoint_buffer_iso[2] = (uint8_t *) malloc (1023);
-            buf = &endpoint_buffer_iso[2][0];
-        } else {
-            if (endpoint_buffer_iso[0] == NULL)
-                endpoint_buffer_iso[0] = (uint8_t *) malloc (1023);
-            buf = &endpoint_buffer_iso[0][0];
-        }
+        buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)][0];
     }
 
     // IN endpt -> device to host (TX)
@@ -338,12 +301,7 @@
         setup = 1;
     }
 
-    // non iso endpoint
-    if (not_iso) {
-        ep_buf = endpoint_buffer[idx];
-    } else {
-        ep_buf = endpoint_buffer_iso[0];
-    }
+    ep_buf = endpoint_buffer[idx];
 
     for (n = 0; n < sz; n++) {
         buffer[n] = ep_buf[n];
@@ -386,13 +344,7 @@
     idx = EP_BDT_IDX(PHY_TO_LOG(endpoint), TX, 0);
     bdt[idx].byte_count = size;
 
-
-    // non iso endpoint
-    if (USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT & USB_ENDPT_EPHSHK_MASK) {
-        ep_buf = endpoint_buffer[idx];
-    } else {
-        ep_buf = endpoint_buffer_iso[2];
-    }
+    ep_buf = endpoint_buffer[idx];
 
     for (n = 0; n < size; n++) {
         ep_buf[n] = data[n];
@@ -464,6 +416,9 @@
         USB0->ERREN   =  0xFF;  // enable error interrupt sources
         USB0->ADDR    =  0x00;  // set default address
 
+        // reset bus for USBDevice layer
+        busReset();
+
         return;
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/USBHAL_M453.cpp	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,465 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2015-2016 Nuvoton
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#if defined(TARGET_NUMAKER_PFM_M453)
+
+#include "USBHAL.h"
+#include "M451Series.h"
+#include "pinmap.h"
+
+/**
+ * EP: mbed USBD defined endpoint, e.g. EP0OUT/IN, EP1OUT/IN, EP2OUT/IN.
+ * EPX: BSP defined endpoint, e.g. CEP, EPA, EPB, EPC.
+ */
+
+USBHAL * USBHAL::instance;
+
+/* Global variables for Control Pipe */
+extern uint8_t g_usbd_SetupPacket[];        /*!< Setup packet buffer */
+
+static volatile uint32_t s_ep_compl = 0;
+static volatile uint32_t s_ep_buf_ind = 8;
+static volatile uint8_t s_usb_addr = 0;
+static volatile uint8_t s_ep_data_bit[NUMBER_OF_PHYSICAL_ENDPOINTS] = {1};
+static volatile uint8_t s_ep_mxp[NUMBER_OF_PHYSICAL_ENDPOINTS] = {0};
+
+extern volatile uint8_t *g_usbd_CtrlInPointer;
+extern volatile uint32_t g_usbd_CtrlInSize;
+extern volatile uint8_t *g_usbd_CtrlOutPointer;
+extern volatile uint32_t g_usbd_CtrlOutSize;
+extern volatile uint32_t g_usbd_CtrlOutSizeLimit;
+extern volatile uint32_t g_usbd_UsbConfig;
+extern volatile uint32_t g_usbd_CtrlMaxPktSize;
+extern volatile uint32_t g_usbd_UsbAltInterface;
+volatile uint32_t g_usbd_CepTransferLen = 0;
+volatile uint32_t frame_cnt = 0;
+USBHAL::USBHAL(void)
+{
+    SYS_UnlockReg();
+
+    s_ep_buf_ind = 8;
+
+    memset(epCallback, 0x00, sizeof (epCallback));
+    epCallback[0] = &USBHAL::EP1_OUT_callback;
+    epCallback[1] = &USBHAL::EP2_IN_callback;
+    epCallback[2] = &USBHAL::EP3_OUT_callback;
+    epCallback[3] = &USBHAL::EP4_IN_callback;
+    epCallback[4] = &USBHAL::EP5_OUT_callback;
+    epCallback[5] = &USBHAL::EP6_IN_callback;
+
+    instance = this;
+    /* Enable USBD module clock */
+    CLK_EnableModuleClock(USBD_MODULE);
+
+    CLK_SetModuleClock(USBD_MODULE, 0, CLK_CLKDIV0_USB(3));
+
+    /* Enable USB LDO33 */
+    SYS->USBPHY = SYS_USBPHY_LDO33EN_Msk;
+
+    /* Initial USB engine */
+    USBD->ATTR = 0x7D0;
+
+    /* Set SE0 (disconnect) */
+    USBD_SET_SE0();
+
+    //NVIC_SetVector(OTG_FS_IRQn, (uint32_t) &_usbisr);
+    NVIC_SetVector(USBD_IRQn, (uint32_t) &_usbisr);
+    NVIC_EnableIRQ(USBD_IRQn);
+}
+
+USBHAL::~USBHAL(void)
+{
+    NVIC_DisableIRQ(USBD_IRQn);
+    USBD_SET_SE0();
+    USBD_DISABLE_PHY();
+}
+
+void USBHAL::connect(void)
+{
+    USBD->STBUFSEG = 0;
+    frame_cnt = 0;
+    /* EP0 ==> control IN endpoint, address 0 */
+    USBD_CONFIG_EP(EP0, USBD_CFG_CSTALL | USBD_CFG_EPMODE_IN | 0);
+    /* Buffer range for EP0 */
+    USBD_SET_EP_BUF_ADDR(EP0, s_ep_buf_ind);
+
+    /* EP1 ==> control OUT endpoint, address 0 */
+    USBD_CONFIG_EP(EP1, USBD_CFG_CSTALL | USBD_CFG_EPMODE_OUT | 0);
+    /* Buffer range for EP1 */
+    USBD_SET_EP_BUF_ADDR(EP1, s_ep_buf_ind);
+
+    s_ep_buf_ind += MAX_PACKET_SIZE_EP0;
+
+    /* Disable software-disconnect function */
+    USBD_CLR_SE0();
+
+    /* Clear USB-related interrupts before enable interrupt */
+    USBD_CLR_INT_FLAG(USBD_INT_BUS | USBD_INT_USB | USBD_INT_FLDET | USBD_INT_WAKEUP);
+
+    /* Enable USB-related interrupts. */
+    USBD_ENABLE_INT(USBD_INT_BUS | USBD_INT_USB | USBD_INT_FLDET | USBD_INT_WAKEUP);
+}
+
+void USBHAL::disconnect(void)
+{
+    /* Set SE0 (disconnect) */
+    USBD_SET_SE0();
+}
+
+void USBHAL::configureDevice(void)
+{
+    /**
+     * In USBDevice.cpp > USBDevice::requestSetConfiguration, configureDevice() is called after realiseEndpoint() (in USBCallback_setConfiguration()).
+     * So we have the following USB buffer management policy:
+     *  1. Allocate for CEP on connect().
+     *  2. Allocate for EPX in realiseEndpoint().
+     *  3. Deallocate all except for CEP in unconfigureDevice().
+     */
+}
+
+void USBHAL::unconfigureDevice(void)
+{
+    s_ep_buf_ind = 8;
+}
+
+void USBHAL::setAddress(uint8_t address)
+{
+    // NOTE: Delay address setting; otherwise, USB controller won't ack.
+    s_usb_addr = address;
+}
+
+void USBHAL::remoteWakeup(void)
+{
+#if 0
+    USBD->OPER |= USBD_OPER_RESUMEEN_Msk;
+#endif
+}
+
+bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t options)
+{
+    uint32_t ep_type = 0;
+    uint32_t ep_hw_index = NU_EP2EPH(endpoint);
+    uint32_t ep_logic_index = NU_EP2EPL(endpoint);
+    uint32_t ep_dir = (NU_EP_DIR(endpoint) == NU_EP_DIR_IN) ? USBD_CFG_EPMODE_IN : USBD_CFG_EPMODE_OUT;
+
+    if (ep_logic_index == 3 || ep_logic_index == 4)
+        ep_type = USBD_CFG_TYPE_ISO;
+
+    USBD_CONFIG_EP(ep_hw_index, ep_dir | ep_type | ep_logic_index);
+    /* Buffer range */
+    USBD_SET_EP_BUF_ADDR(ep_hw_index, s_ep_buf_ind);
+
+    if (ep_dir == USBD_CFG_EPMODE_OUT)
+        USBD_SET_PAYLOAD_LEN(ep_hw_index, maxPacket);
+
+    s_ep_mxp[ep_logic_index] = maxPacket;
+
+    s_ep_buf_ind += maxPacket;
+
+    return true;
+}
+
+void USBHAL::EP0setup(uint8_t *buffer)
+{
+    uint32_t sz;
+    endpointReadResult(EP0OUT, buffer, &sz);
+}
+
+void USBHAL::EP0read(void)
+{
+
+
+}
+
+void USBHAL::EP0readStage(void)
+{
+    // N/A
+
+    USBD_PrepareCtrlOut(0,0);
+}
+
+uint32_t USBHAL::EP0getReadResult(uint8_t *buffer)
+{
+    uint32_t i;
+    uint8_t *buf = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP1));
+    uint32_t ceprxcnt = USBD_GET_PAYLOAD_LEN(EP1);
+    for (i = 0; i < ceprxcnt; i ++)
+        buffer[i] = buf[i];
+    USBD_SET_PAYLOAD_LEN(EP1, MAX_PACKET_SIZE_EP0);
+    return ceprxcnt;
+}
+
+void USBHAL::EP0write(uint8_t *buffer, uint32_t size)
+{
+    if (buffer && size)
+    {
+        if (s_ep_data_bit[0] & 1)
+            USBD_SET_DATA1(EP0);
+        else
+            USBD_SET_DATA0(EP0);
+        s_ep_data_bit[0]++;
+
+        USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0), buffer, size);
+        USBD_SET_PAYLOAD_LEN(EP0, size);
+        if (size < MAX_PACKET_SIZE_EP0)
+            s_ep_data_bit[0] = 1;
+
+    }
+    else
+    {
+        if (g_usbd_SetupPacket[0] & 0x80)   //Device to Host
+        {
+            // Status stage
+            //          USBD_PrepareCtrlOut(0,0);
+        } else
+        {
+            USBD_SET_DATA1(EP0);
+            USBD_SET_PAYLOAD_LEN(EP0, 0);
+        }
+    }
+}
+
+void USBHAL::EP0getWriteResult(void)
+{
+    // N/A
+}
+
+void USBHAL::EP0stall(void)
+{
+    stallEndpoint(EP0OUT);
+}
+
+EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize)
+{
+    return EP_PENDING;
+}
+
+EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) //spcheng
+{
+    if (endpoint == EP0OUT)
+    {
+        USBD_MemCopy(g_usbd_SetupPacket, (uint8_t *)USBD_BUF_BASE, 8);
+        if (buffer) {
+            USBD_MemCopy(buffer, g_usbd_SetupPacket, 8);
+        }
+        USBD_SET_PAYLOAD_LEN(EP1, MAX_PACKET_SIZE_EP0);
+    }
+    else
+    {
+        uint32_t i;
+        uint8_t *buf = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(NU_EP2EPH(endpoint)));
+        uint32_t eprxcnt = USBD_GET_PAYLOAD_LEN(NU_EP2EPH(endpoint));
+        for (i = 0; i < eprxcnt; i ++)
+            buffer[i] = buf[i];
+
+        *bytesRead = eprxcnt;
+
+        USBD_SET_PAYLOAD_LEN(NU_EP2EPH(endpoint),s_ep_mxp[NU_EPH2EPL(NU_EP2EPL(endpoint))]);
+    }
+    return EP_COMPLETED;
+}
+
+
+uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer)
+{
+    return 0;
+}
+
+EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size)
+{
+    uint32_t ep_logic_index = NU_EP2EPL(endpoint);
+    if (ep_logic_index == 0)
+        return EP_INVALID;
+    else
+    {
+        uint8_t *buf;
+        uint32_t i=0;
+        uint32_t ep_hw_index = NU_EP2EPH(endpoint);
+        s_ep_compl |= (1 << ep_logic_index);
+        buf = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(ep_hw_index));
+        for (i=0;i<size;i++)
+            buf[i] = data[i];
+
+        /* Set transfer length and trigger IN transfer */
+        USBD_SET_PAYLOAD_LEN(ep_hw_index, size);
+
+    }
+    return EP_PENDING;
+}
+
+EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint)
+{
+    if (!(s_ep_compl & (1 << NU_EP2EPL(endpoint))))
+        return EP_COMPLETED;
+    return EP_PENDING;
+}
+
+void USBHAL::stallEndpoint(uint8_t endpoint)
+{
+    uint32_t ep_hw_index = NU_EP2EPH(endpoint);
+    if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS)
+        return;
+
+    USBD_SetStall(NU_EPH2EPL(ep_hw_index));
+
+}
+
+void USBHAL::unstallEndpoint(uint8_t endpoint)
+{
+    uint32_t ep_hw_index = NU_EP2EPH(endpoint);
+    if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS)
+        return;
+    USBD_ClearStall(NU_EPH2EPL(ep_hw_index));
+}
+
+bool USBHAL::getEndpointStallState(uint8_t endpoint)
+{
+    uint32_t ep_hw_index = NU_EP2EPH(endpoint);
+    if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS)
+        return false;
+
+    return USBD_GetStall(NU_EPH2EPL(ep_hw_index)) ? 1 : 0;
+}
+
+void USBHAL::_usbisr(void)
+{
+    MBED_ASSERT(instance);
+    instance->usbisr();
+}
+
+void USBHAL::usbisr(void)
+{
+    uint32_t u32IntSts = USBD_GET_INT_FLAG();
+    uint32_t u32State = USBD_GET_BUS_STATE();
+
+//------------------------------------------------------------------
+    if (u32IntSts & USBD_INTSTS_VBDETIF_Msk)
+    {
+        // Floating detect
+        USBD_CLR_INT_FLAG(USBD_INTSTS_VBDETIF_Msk);
+
+        if (USBD_IS_ATTACHED())
+        {
+            /* USB Plug In */
+            USBD_ENABLE_USB();
+        }
+        else
+        {
+            /* USB Un-plug */
+            USBD_DISABLE_USB();
+        }
+    }
+
+//------------------------------------------------------------------
+    if (u32IntSts & USBD_INTSTS_BUSIF_Msk)
+    {
+        /* Clear event flag */
+        USBD_CLR_INT_FLAG(USBD_INTSTS_BUSIF_Msk);
+
+        if (u32State & USBD_ATTR_USBRST_Msk)
+        {
+            /* Bus reset */
+            USBD_ENABLE_USB();
+            USBD_SwReset();
+        }
+        if (u32State & USBD_ATTR_SUSPEND_Msk)
+        {
+            /* Enable USB but disable PHY */
+            USBD_DISABLE_PHY();
+        }
+        if (u32State & USBD_ATTR_RESUME_Msk)
+        {
+            /* Enable USB and enable PHY */
+            USBD_ENABLE_USB();
+        }
+    }
+
+    if (u32IntSts & USBD_INTSTS_USBIF_Msk)
+    {
+        // USB event
+        if (u32IntSts & USBD_INTSTS_SETUP_Msk)
+        {
+            // Setup packet
+            /* Clear event flag */
+            USBD_CLR_INT_FLAG(USBD_INTSTS_SETUP_Msk);
+
+            /* Clear the data IN/OUT ready flag of control end-points */
+            USBD_STOP_TRANSACTION(EP0);
+            USBD_STOP_TRANSACTION(EP1);
+            EP0setupCallback();
+        }
+
+        // EP events
+        if (u32IntSts & USBD_INTSTS_EP0)
+        {
+            /* Clear event flag */
+            USBD_CLR_INT_FLAG(USBD_INTSTS_EP0);
+            // control IN
+            EP0in();
+
+            // In ACK for Set address
+            if ((g_usbd_SetupPacket[0] == REQ_STANDARD) && (g_usbd_SetupPacket[1] == USBD_SET_ADDRESS))
+            {
+                if ((USBD_GET_ADDR() != s_usb_addr) && (USBD_GET_ADDR() == 0))
+                {
+                    USBD_SET_ADDR(s_usb_addr);
+                }
+            }
+        }
+        if (u32IntSts & USBD_INTSTS_EP1)
+        {
+            /* Clear event flag */
+            USBD_CLR_INT_FLAG(USBD_INTSTS_EP1);
+
+            // control OUT
+            EP0out();
+        }
+
+        uint32_t gintsts_epx = (u32IntSts >> 18) & 0x3F;
+        uint32_t ep_hw_index = 2;
+        while (gintsts_epx) {
+            if (gintsts_epx & 0x01)
+            {
+                uint32_t ep_status = (USBD_GET_EP_FLAG() >> (ep_hw_index * 3 + 8)) & 0x7;
+                /* Clear event flag */
+                USBD_CLR_INT_FLAG(1 << (ep_hw_index + 16));
+
+                if (ep_status == 0x02 || ep_status == 0x06 || (ep_status == 0x07 && NU_EPH2EPL(ep_hw_index) == 3))  //RX
+                {
+                    if (ep_status == 0x07)
+                        SOF(frame_cnt++);
+                    if ((instance->*(epCallback[ep_hw_index-2]))())
+                    {
+
+                    }
+                    USBD_SET_PAYLOAD_LEN(ep_hw_index,s_ep_mxp[NU_EPH2EPL(ep_hw_index)]);
+                }
+                else if (ep_status == 0x00 || ep_status == 0x07)    //TX
+                {
+                    s_ep_compl &= ~(1 << (NU_EPH2EPL(ep_hw_index)));
+                    if ((instance->*(epCallback[ep_hw_index-2]))())
+                    {
+                    }
+                }
+            }
+
+            gintsts_epx = gintsts_epx >> 1;
+            ep_hw_index++;
+        }
+    }
+}
+#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice/USBHAL_NUC472.cpp	Thu Jul 20 10:14:36 2017 +0100
@@ -0,0 +1,730 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2015-2016 Nuvoton
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#if defined(TARGET_NUMAKER_PFM_NUC472)
+
+#include "USBHAL.h"
+#include "NUC472_442.h"
+#include "pinmap.h"
+
+/**
+ * EP: mbed USBD defined endpoint, e.g. EP0OUT/IN, EP1OUT/IN, EP2OUT/IN.
+ * EPX: BSP defined endpoint, e.g. CEP, EPA, EPB, EPC.
+ */
+
+USBHAL * USBHAL::instance;
+
+static volatile uint32_t s_ep_compl = 0;
+static volatile uint32_t s_ep_buf_ind = 0;
+static volatile uint8_t s_usb_addr = 0;
+static volatile S_USBD_CMD_T s_setup;
+static volatile uint16_t s_ctrlin_packetsize;
+static uint8_t *g_usbd_CtrlInPointer = 0;
+static uint32_t g_usbd_CtrlMaxPktSize = 64;
+static uint32_t g_usbd_ShortPkt = 0;
+static uint32_t gEpRead = 0;
+static uint32_t gEpReadCnt = 0;
+
+void USBD_CtrlInput(void)
+{
+    int volatile i;
+    uint32_t volatile count;
+
+    // Process remained data
+    if (g_usbd_CtrlInSize >= g_usbd_CtrlMaxPktSize)
+    {
+        // Data size > MXPLD
+        for (i=0; i<(g_usbd_CtrlMaxPktSize >> 2); i++, g_usbd_CtrlInPointer+=4)
+            USBD->CEPDAT = *(uint32_t *)g_usbd_CtrlInPointer;
+        USBD_START_CEP_IN(g_usbd_CtrlMaxPktSize);
+        g_usbd_CtrlInSize -= g_usbd_CtrlMaxPktSize;
+    }
+    else
+    {
+        // Data size <= MXPLD
+        for (i=0; i<(g_usbd_CtrlInSize >> 2); i++, g_usbd_CtrlInPointer+=4)
+            USBD->CEPDAT = *(uint32_t *)g_usbd_CtrlInPointer;
+
+        count = g_usbd_CtrlInSize % 4;
+        for (i=0; i<count; i++)
+            USBD->CEPDAT_BYTE = *(uint8_t *)(g_usbd_CtrlInPointer + i);
+
+        USBD_START_CEP_IN(g_usbd_CtrlInSize);
+        g_usbd_CtrlInPointer = 0;
+        g_usbd_CtrlInSize = 0;
+    }
+}
+
+USBHAL::USBHAL(void)
+{
+    SYS_UnlockReg();
+
+    s_ep_buf_ind = 0;
+
+    memset(epCallback, 0x00, sizeof (epCallback));
+    epCallback[0] = &USBHAL::EP1_OUT_callback;
+    epCallback[1] = &USBHAL::EP2_IN_callback;
+    epCallback[2] = &USBHAL::EP3_OUT_callback;
+    epCallback[3] = &USBHAL::EP4_IN_callback;
+    epCallback[4] = &USBHAL::EP5_OUT_callback;
+    epCallback[5] = &USBHAL::EP6_IN_callback;
+    epCallback[6] = &USBHAL::EP7_OUT_callback;
+    epCallback[7] = &USBHAL::EP8_IN_callback;
+    epCallback[8] = &USBHAL::EP9_OUT_callback;
+    epCallback[9] = &USBHAL::EP10_IN_callback;
+    epCallback[10] = &USBHAL::EP11_OUT_callback;
+    epCallback[11] = &USBHAL::EP12_IN_callback;
+
+    instance = this;
+
+    /* Enable USBD module clock */
+    CLK_EnableModuleClock(USBD_MODULE);
+
+    /* Enable USB PHY's LDO33. Run as USB device. */
+    SYS->USBPHY = SYS_USBPHY_USBROLE_OTG_V33_EN | SYS_USBPHY_USBROLE_STD_USBD;
+
+    /* Enable USB PHY and wait for it ready */
+    USBD_ENABLE_PHY();
+    while (1)
+    {
+        USBD->EPAMPS = 0x20;
+        if (USBD->EPAMPS == 0x20)
+            break;
+    }
+
+    /* Force to full-speed */
+    USBD->OPER = 0;//USBD_OPER_HISPDEN_Msk;
+
+    /* Set SE0 (disconnect) */
+    USBD_SET_SE0();
+
+    NVIC_SetVector(USBD_IRQn, (uint32_t) &_usbisr);
+    NVIC_EnableIRQ(USBD_IRQn);
+}
+
+USBHAL::~USBHAL(void)
+{
+    NVIC_DisableIRQ(USBD_IRQn);
+    USBD_SET_SE0();
+    USBD_DISABLE_PHY();
+}
+
+void USBHAL::connect(void)
+{
+    USBD_ResetDMA();
+    USBD_SET_ADDR(0);
+
+    /**
+      * Control Transfer Packet Size Constraints
+      * low-speed: 8
+      * full-speed: 8, 16, 32, 64
+      * high-speed: 64
+      */
+    /* Control endpoint */
+    USBD_SetEpBufAddr(CEP, s_ep_buf_ind, MAX_PACKET_SIZE_EP0);
+    s_ep_buf_ind = MAX_PACKET_SIZE_EP0;
+
+    /* Enable USB/CEP interrupt */
+    USBD_ENABLE_USB_INT(USBD_GINTEN_USBIE_Msk | USBD_GINTEN_CEPIE_Msk);
+    USBD_ENABLE_CEP_INT(USBD_CEPINTEN_SETUPPKIEN_Msk|USBD_CEPINTEN_STSDONEIEN_Msk);
+
+    /* Enable BUS interrupt */
+    USBD_ENABLE_BUS_INT(
+        USBD_BUSINTEN_DMADONEIEN_Msk |
+        USBD_BUSINTEN_RESUMEIEN_Msk |
+        USBD_BUSINTEN_RSTIEN_Msk |
+        USBD_BUSINTEN_VBUSDETIEN_Msk |
+        USBD_BUSINTEN_SOFIEN_Msk
+    );
+
+    /* Clear SE0 (connect) */
+    USBD_CLR_SE0();
+}
+
+void USBHAL::disconnect(void)
+{
+    /* Set SE0 (disconnect) */
+    USBD_SET_SE0();
+}
+
+void USBHAL::configureDevice(void)
+{
+    /**
+     * In USBDevice.cpp > USBDevice::requestSetConfiguration, configureDevice() is called after realiseEndpoint() (in USBCallback_setConfiguration()).
+     * So we have the following USB buffer management policy:
+     *  1. Allocate for CEP on connect().
+     *  2. Allocate for EPX in realiseEndpoint().
+     *  3. Deallocate all except for CEP in unconfigureDevice().
+     */
+}
+
+void USBHAL::unconfigureDevice(void)
+{
+    s_ep_buf_ind = MAX_PACKET_SIZE_EP0;
+}
+
+void USBHAL::setAddress(uint8_t address)
+{
+    // NOTE: Delay address setting; otherwise, USB controller won't ack.
+    s_usb_addr = address;
+}
+
+void USBHAL::remoteWakeup(void)
+{
+    USBD->OPER |= USBD_OPER_RESUMEEN_Msk;
+}
+
+bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t options)
+{
+    uint32_t ep_type;
+    uint32_t ep_hw_index = NU_EP2EPH(endpoint);
+
+    USBD_SetEpBufAddr(ep_hw_index, s_ep_buf_ind, maxPacket);
+    s_ep_buf_ind += maxPacket;
+    USBD_SET_MAX_PAYLOAD(ep_hw_index, maxPacket);
+
+    switch (NU_EP2EPL(endpoint))
+    {
+    case 1:    case 2:
+        ep_type = USB_EP_CFG_TYPE_INT;
+        break;
+
+    case 3:    case 4:
+        ep_type = USB_EP_CFG_TYPE_ISO;
+        break;
+
+    default:
+        ep_type = USB_EP_CFG_TYPE_BULK;
+    }
+    uint32_t ep_dir = (NU_EP_DIR(endpoint) == NU_EP_DIR_IN) ? USB_EP_CFG_DIR_IN : USB_EP_CFG_DIR_OUT;
+    USBD_ConfigEp(ep_hw_index, NU_EP2EPL(endpoint), ep_type, ep_dir);
+
+    /* Enable USB/EPX interrupt */
+    // NOTE: Require USBD_GINTEN_EPAIE_Pos, USBD_GINTEN_EPBIE_Pos, ... USBD_GINTEN_EPLIE_Pos to be consecutive.
+    USBD_ENABLE_USB_INT(USBD->GINTEN | USBD_GINTEN_USBIE_Msk |
+                        USBD_GINTEN_CEPIE_Msk |
+                        1 << (ep_hw_index + USBD_GINTEN_EPAIE_Pos));  // Added USB/EPX interrupt
+
+    if (ep_dir == 0)
+        USBD_ENABLE_EP_INT(ep_hw_index, USBD_EPINTEN_RXPKIEN_Msk);
+    else
+        USBD_ENABLE_EP_INT(ep_hw_index, USBD_EPINTEN_TXPKIEN_Msk);
+    return true;
+}
+
+void USBHAL::EP0setup(uint8_t *buffer)
+{
+    uint32_t sz;
+    endpointReadResult(EP0OUT, buffer, &sz);
+}
+
+void USBHAL::EP0read(void)
+{
+    if (s_setup.wLength && ! (s_setup.bmRequestType & 0x80))
+    {
+        // Control OUT
+        USBD_ENABLE_CEP_INT(USBD_CEPINTEN_SETUPPKIEN_Msk | USBD_CEPINTEN_RXPKIEN_Msk);
+    }
+    else
+    {
+        // Status stage
+        USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk);
+        USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR);
+        USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
+    }
+}
+
+void USBHAL::EP0readStage(void)
+{
+    // N/A
+}
+
+uint32_t USBHAL::EP0getReadResult(uint8_t *buffer)
+{
+    uint32_t i;
+    uint32_t ceprxcnt = USBD->CEPRXCNT;
+    for (i = 0; i < ceprxcnt; i ++)
+        *buffer ++ = USBD->CEPDAT_BYTE;
+    return ceprxcnt;
+}
+
+void USBHAL::EP0write(uint8_t *buffer, uint32_t size)
+{
+    if (buffer && size)
+    {
+        g_usbd_CtrlInPointer = buffer;
+        g_usbd_CtrlInSize = size;
+        USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_INTKIF_Msk);
+        USBD_ENABLE_CEP_INT(USBD_CEPINTEN_INTKIEN_Msk);
+    }
+    else
+    {
+        /* Status stage */
+        s_ctrlin_packetsize = 0;
+        USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk);
+        USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR);
+        USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
+    }
+}
+
+void USBHAL::EP0getWriteResult(void)
+{
+    // N/A
+}
+
+void USBHAL::EP0stall(void)
+{
+    stallEndpoint(EP0OUT);
+}
+
+EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize)
+{
+    return EP_PENDING;
+}
+
+EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) //spcheng
+{
+    if (endpoint == EP0OUT)
+    {
+        if (buffer) {
+            *((uint16_t *) (buffer + 0)) = (uint16_t) USBD->SETUP1_0;
+            *((uint16_t *) (buffer + 2)) = (uint16_t) USBD->SETUP3_2;
+            *((uint16_t *) (buffer + 4)) = (uint16_t) USBD->SETUP5_4;
+            *((uint16_t *) (buffer + 6)) = (uint16_t) USBD->SETUP7_6;
+        }
+
+        s_setup.bmRequestType = (uint8_t) (USBD->SETUP1_0 & 0xff);
+        s_setup.bRequest = (int8_t) (USBD->SETUP1_0 >> 8) & 0xff;
+        s_setup.wValue = (uint16_t) USBD->SETUP3_2;
+        s_setup.wIndex = (uint16_t) USBD->SETUP5_4;
+        s_setup.wLength = (uint16_t) USBD->SETUP7_6;
+    }
+    else
+    {
+        if (!(s_ep_compl & (1 << NU_EP2EPL(endpoint))))
+        {
+            while (1)
+            {
+                if (!(USBD->DMACTL & USBD_DMACTL_DMAEN_Msk))
+                    break;
+                else
+                    if (!USBD_IS_ATTACHED())
+                        break;
+            }
+            gEpReadCnt = USBD_GET_EP_DATA_COUNT(NU_EP2EPH(endpoint));
+            if (gEpReadCnt == 0)
+            {
+                *bytesRead = 0;
+                return EP_COMPLETED;
+            }
+            s_ep_compl |= (1 << NU_EP2EPL(endpoint));
+            USBD_SET_DMA_LEN(gEpReadCnt);
+            USBD_SET_DMA_ADDR((uint32_t)buffer);
+            USBD_SET_DMA_WRITE(NU_EP2EPL(endpoint));
+            USBD_ENABLE_DMA();
+            return EP_PENDING;;
+
+        }
+        else
+        {
+            if ((USBD->DMACTL & USBD_DMACTL_DMAEN_Msk))
+                return EP_PENDING;;
+
+            USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_DMADONEIF_Msk);
+            s_ep_compl &= ~(1 << NU_EP2EPL(endpoint));
+            *bytesRead = gEpReadCnt;
+        }
+    }
+    return EP_COMPLETED;
+}
+
+
+uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer)
+{
+    return 0;
+}
+
+EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size)
+{
+    uint32_t ep_logic_index = NU_EP2EPL(endpoint);
+    if (ep_logic_index == 0)
+        return EP_INVALID;
+    else
+    {
+        uint32_t ep_hw_index = NU_EP2EPH(endpoint);
+        uint32_t mps = USBD_GET_EP_MAX_PAYLOAD(ep_hw_index);
+        if (size > mps) {
+            return EP_INVALID;
+        }
+        if (size < mps)
+            g_usbd_ShortPkt = 1;
+        if (!(s_ep_compl & (1 << NU_EP2EPL(endpoint))))
+        {
+            s_ep_compl |= (1 << ep_logic_index);
+
+            while (1)
+            {
+                if (!(USBD->DMACTL & USBD_DMACTL_DMAEN_Msk))
+                    break;
+                else
+                    if (!USBD_IS_ATTACHED())
+                        break;
+            }
+            USBD_SET_DMA_LEN(size);
+            USBD_SET_DMA_ADDR((uint32_t)data);
+            USBD_SET_DMA_READ(ep_logic_index);
+            USBD_ENABLE_DMA();
+        }
+    }
+    return EP_PENDING;
+}
+
+EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint)
+{
+    if (!(s_ep_compl & (1 << NU_EP2EPL(endpoint))))
+        return EP_COMPLETED;
+    else
+    {
+        if ((USBD_GET_EP_DATA_COUNT(NU_EP2EPH(endpoint))) == 0 && !(USBD->DMACTL & USBD_DMACTL_DMAEN_Msk))
+        {
+            s_ep_compl &= ~(s_ep_compl & (1 << NU_EP2EPL(endpoint)));
+            return EP_COMPLETED;
+        }
+    }
+    return EP_PENDING;
+}
+
+void USBHAL::stallEndpoint(uint8_t endpoint)
+{
+    uint32_t ep_hw_index = NU_EP2EPH(endpoint);
+    if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS)
+        return;
+    USBD_SetStall(ep_hw_index);
+}
+
+void USBHAL::unstallEndpoint(uint8_t endpoint)
+{
+    uint32_t ep_hw_index = NU_EP2EPH(endpoint);
+    if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS)
+        return;
+    USBD_ClearStall(ep_hw_index);
+}
+
+bool USBHAL::getEndpointStallState(uint8_t endpoint)
+{
+    uint32_t ep_hw_index = NU_EP2EPH(endpoint);
+    if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS)
+        return false;
+    return USBD_GetStall(ep_hw_index) ? 1 : 0;
+}
+
+void USBHAL::_usbisr(void)
+{
+    MBED_ASSERT(instance);
+    instance->usbisr();
+}
+
+void USBHAL::usbisr(void)
+{
+    uint32_t gintsts = USBD->GINTSTS & USBD->GINTEN;
+    if (! gintsts)
+        return;
+
+    if (gintsts & USBD_GINTSTS_USBIF_Msk)
+    {
+        uint32_t busintsts = USBD->BUSINTSTS & USBD->BUSINTEN;
+
+        /* SOF */
+        if (busintsts & USBD_BUSINTSTS_SOFIF_Msk)
+        {
+            USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_SOFIF_Msk);
+            // TODO
+            SOF(USBD->FRAMECNT >> 3);
+        }
+
+        /* Reset */
+        if (busintsts & USBD_BUSINTSTS_RSTIF_Msk)
+        {
+            connect();
+            USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_RSTIF_Msk);
+            USBD_CLR_CEP_INT_FLAG(0x1ffc);
+        }
+
+        /* Resume */
+        if (busintsts & USBD_BUSINTSTS_RESUMEIF_Msk)
+        {
+            USBD_ENABLE_BUS_INT(USBD_BUSINTEN_RSTIEN_Msk|USBD_BUSINTEN_SUSPENDIEN_Msk | USBD_BUSINTEN_SOFIEN_Msk | USBD_BUSINTEN_SOFIEN_Msk);
+            USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_RESUMEIF_Msk);
+        }
+
+        /* Suspend */
+        if (busintsts & USBD_BUSINTSTS_SUSPENDIF_Msk)
+        {
+            USBD_ENABLE_BUS_INT(USBD_BUSINTEN_RSTIEN_Msk | USBD_BUSINTEN_RESUMEIEN_Msk |USBD_BUSINTEN_SOFIEN_Msk);
+            USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_SUSPENDIF_Msk);
+        }
+
+        /* High-speed */
+        if (busintsts & USBD_BUSINTSTS_HISPDIF_Msk)
+        {
+            USBD_ENABLE_CEP_INT(USBD_CEPINTEN_SETUPPKIEN_Msk);
+            USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_HISPDIF_Msk);
+        }
+
+        /* DMA */
+        if (busintsts & USBD_BUSINTSTS_DMADONEIF_Msk)
+        {
+            if (USBD->DMACTL & 0x10) /* IN - Read */
+            {
+                if (g_usbd_ShortPkt)
+                {
+                    uint32_t ep_hw_index = NU_EPL2EPH((USBD->DMACTL & 0xF));
+                    USBD_SET_EP_SHORT_PACKET(ep_hw_index);
+                    g_usbd_ShortPkt = 0;
+                }
+            }
+            USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_DMADONEIF_Msk);
+        }
+
+        /* PHY clock available */
+        if (busintsts & USBD_BUSINTSTS_PHYCLKVLDIF_Msk)
+        {
+            USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_PHYCLKVLDIF_Msk);
+        }
+
+        /* VBUS plug-in */
+        if (busintsts & USBD_BUSINTSTS_VBUSDETIF_Msk)
+        {
+            if (USBD_IS_ATTACHED())
+            {
+                // USB plug-in
+                USBD_ENABLE_USB();
+            }
+            else
+            {
+                // USB unplug-out
+                USBD_DISABLE_USB();
+            }
+            USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_VBUSDETIF_Msk);
+        }
+    }
+
+    /* CEP interrupts */
+    if (gintsts & USBD_GINTSTS_CEPIF_Msk)
+    {
+        uint32_t cepintsts =  USBD->CEPINTSTS & USBD->CEPINTEN;
+
+        /* SETUP token packet */
+        if (cepintsts & USBD_CEPINTSTS_SETUPTKIF_Msk)
+        {
+            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_SETUPTKIF_Msk);
+            return;
+        }
+
+        /* SETUP transaction */
+        if (cepintsts & USBD_CEPINTSTS_SETUPPKIF_Msk)
+        {
+            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_SETUPPKIF_Msk);
+            EP0setupCallback();
+            return;
+        }
+
+        /* OUT token packet */
+        if (cepintsts & USBD_CEPINTSTS_OUTTKIF_Msk)
+        {
+            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_OUTTKIF_Msk);
+            USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
+            return;
+        }
+
+        /* IN token packet */
+        if (cepintsts & USBD_CEPINTSTS_INTKIF_Msk)
+        {
+            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_INTKIF_Msk);
+            if (!(cepintsts & USBD_CEPINTSTS_STSDONEIF_Msk))
+            {
+                USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_TXPKIF_Msk);
+                USBD_ENABLE_CEP_INT(USBD_CEPINTEN_TXPKIEN_Msk);
+                USBD_CtrlInput();
+            }
+            else
+            {
+                USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_TXPKIF_Msk);
+                USBD_ENABLE_CEP_INT(USBD_CEPINTEN_TXPKIEN_Msk|USBD_CEPINTEN_STSDONEIEN_Msk);
+            }
+            return;
+        }
+
+        /* PING packet */
+        if (cepintsts & USBD_CEPINTSTS_PINGIF_Msk)
+        {
+            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_PINGIF_Msk);
+            return;
+        }
+
+        /* IN transaction */
+        if (cepintsts & USBD_CEPINTSTS_TXPKIF_Msk)
+        {
+            EP0in();
+            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_TXPKIF_Msk);
+            return;
+        }
+
+        /* OUT transaction */
+        if (cepintsts & USBD_CEPINTSTS_RXPKIF_Msk)
+        {
+            EP0out();
+            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_RXPKIF_Msk);
+            return;
+        }
+
+        /* NAK handshake packet */
+        if (cepintsts & USBD_CEPINTSTS_NAKIF_Msk)
+        {
+            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_NAKIF_Msk);
+            return;
+        }
+
+        /* STALL handshake packet */
+        if (cepintsts & USBD_CEPINTSTS_STALLIF_Msk)
+        {
+            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STALLIF_Msk);
+            return;
+        }
+
+        /* ERR special packet */
+        if (cepintsts & USBD_CEPINTSTS_ERRIF_Msk)
+        {
+            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_ERRIF_Msk);
+            return;
+        }
+
+        /* Status stage transaction */
+        if (cepintsts & USBD_CEPINTSTS_STSDONEIF_Msk)
+        {
+            if (s_usb_addr)
+            {
+                USBD_SET_ADDR(s_usb_addr);
+                s_usb_addr = 0;
+            }
+            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk);
+            USBD_ENABLE_CEP_INT(USBD_CEPINTEN_SETUPPKIEN_Msk);
+            return;
+        }
+
+        /* Buffer Full */
+        if (cepintsts & USBD_CEPINTSTS_BUFFULLIF_Msk)
+        {
+            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_BUFFULLIF_Msk);
+            return;
+        }
+
+        /* Buffer Empty */
+        if (cepintsts & USBD_CEPINTSTS_BUFEMPTYIF_Msk)
+        {
+            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_BUFEMPTYIF_Msk);
+            return;
+        }
+    }
+    /* EPA, EPB, EPC, ... EPL interrupts */
+    uint32_t gintsts_epx = gintsts >> 2;
+    uint32_t ep_hw_index = 0;
+    while (gintsts_epx) {
+        if (gintsts_epx & 0x01)
+        {
+            uint32_t epxintsts = USBD_GET_EP_INT_FLAG(ep_hw_index) & USBD_GET_EP_INT_EN(ep_hw_index);
+
+            USBD_CLR_EP_INT_FLAG(ep_hw_index, epxintsts);
+
+            /* Buffer Full */
+            if (epxintsts & USBD_EPINTSTS_BUFFULLIF_Msk)
+            {
+            }
+
+            /* Buffer Empty */
+            if (epxintsts & USBD_EPINTSTS_BUFEMPTYIF_Msk)
+            {
+            }
+
+            /* Short Packet Transferred */
+            if (epxintsts & USBD_EPINTSTS_SHORTTXIF_Msk)
+            {
+            }
+
+            /* Data Packet Transmitted */
+            if (epxintsts & USBD_EPINTSTS_TXPKIF_Msk)
+            {
+                s_ep_compl &= ~(1 << (NU_EPH2EPL(ep_hw_index)));
+                if ((instance->*(epCallback[ep_hw_index]))())
+                {
+                }
+            }
+
+            /* Data Packet Received */
+            if (epxintsts & USBD_EPINTSTS_RXPKIF_Msk)
+            {
+                if ((instance->*(epCallback[ep_hw_index]))())
+                {
+
+                }
+            }
+
+            /* OUT token packet */
+            if (epxintsts & USBD_EPINTSTS_OUTTKIF_Msk)
+            {
+            }
+
+            /* IN token packet */
+            if (epxintsts & USBD_EPINTSTS_INTKIF_Msk)
+            {
+            }
+
+            /* PING packet */
+            if (epxintsts & USBD_EPINTSTS_PINGIF_Msk)
+            {
+            }
+
+            /* NAK handshake packet sent to Host */
+            if (epxintsts & USBD_EPINTSTS_NAKIF_Msk)
+            {
+            }
+
+            /* STALL handshake packet sent to Host */
+            if (epxintsts & USBD_EPINTSTS_STALLIF_Msk)
+            {
+            }
+
+            /* NYET handshake packet sent to Host */
+            if (epxintsts & USBD_EPINTSTS_NYETIF_Msk)
+            {
+            }
+
+            /* ERR packet sent to Host */
+            if (epxintsts & USBD_EPINTSTS_ERRIF_Msk)
+            {
+            }
+
+            /* Bulk Out Short Packet Received */
+            if (epxintsts & USBD_EPINTSTS_SHORTRXIF_Msk)
+            {
+            }
+        }
+        gintsts_epx = gintsts_epx >> 1;
+        ep_hw_index++;
+    }
+}
+#endif
+
--- a/USBDevice/USBHAL_STM32F4.cpp	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBDevice/USBHAL_STM32F4.cpp	Thu Jul 20 10:14:36 2017 +0100
@@ -16,7 +16,7 @@
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#if defined(TARGET_STM32F4)
+#if defined(TARGET_STM32F4) && !defined(USB_STM_HAL)
 
 #include "USBHAL.h"
 #include "USBRegs_STM32.h"
@@ -48,7 +48,7 @@
     // Enable power and clocking
     RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
 
-#if defined(TARGET_STM32F407VG) || defined(TARGET_STM32F401RE) || defined(TARGET_STM32F411RE) || defined(TARGET_STM32F429ZI)
+#if defined(TARGET_STM32F407VG) || defined(TARGET_STM32F401RE) || defined(TARGET_STM32F411RE) || defined(TARGET_STM32F412ZG) || defined(TARGET_STM32F429ZI)
     pin_function(PA_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
     pin_function(PA_9, STM_PIN_DATA(STM_MODE_INPUT, GPIO_PULLDOWN, GPIO_AF10_OTG_FS));
     pin_function(PA_10, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_PULLUP, GPIO_AF10_OTG_FS));
@@ -367,7 +367,7 @@
             else {
                 epComplete |= (1 << endpoint);
                 if ((instance->*(epCallback[endpoint - 2]))()) {
-                    epComplete &= (1 << endpoint);
+                    epComplete &= ~(1 << endpoint);
                 }
             }
         }
--- a/USBHID/USBHID.cpp	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBHID/USBHID.cpp	Thu Jul 20 10:14:36 2017 +0100
@@ -199,20 +199,23 @@
 
 uint8_t * USBHID::reportDesc() {
     static uint8_t reportDescriptor[] = {
-        0x06, LSB(0xFFAB), MSB(0xFFAB),
-        0x0A, LSB(0x0200), MSB(0x0200),
-        0xA1, 0x01,         // Collection 0x01
-        0x75, 0x08,         // report size = 8 bits
-        0x15, 0x00,         // logical minimum = 0
-        0x26, 0xFF, 0x00,   // logical maximum = 255
-        0x95, input_length, // report count
-        0x09, 0x01,         // usage
-        0x81, 0x02,         // Input (array)
-        0x95, output_length,// report count
-        0x09, 0x02,         // usage
-        0x91, 0x02,         // Output (array)
-        0xC0                // end collection
+        USAGE_PAGE(2), LSB(0xFFAB), MSB(0xFFAB),
+        USAGE(2), LSB(0x0200), MSB(0x0200),
+        COLLECTION(1), 0x01, // Collection (Application)
+
+        REPORT_SIZE(1), 0x08, // 8 bits
+        LOGICAL_MINIMUM(1), 0x00,
+        LOGICAL_MAXIMUM(1), 0xFF,
 
+        REPORT_COUNT(1), input_length,
+        USAGE(1), 0x01,
+        INPUT(1), 0x02, // Data, Var, Abs
+
+        REPORT_COUNT(1), output_length,
+        USAGE(1), 0x02,
+        OUTPUT(1), 0x02, // Data, Var, Abs
+
+        END_COLLECTION(0),
     };
     reportLength = sizeof(reportDescriptor);
     return reportDescriptor;
@@ -226,51 +229,51 @@
 
 uint8_t * USBHID::configurationDesc() {
     static uint8_t configurationDescriptor[] = {
-        CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
-        CONFIGURATION_DESCRIPTOR,       // bDescriptorType
-        LSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (LSB)
-        MSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (MSB)
-        0x01,                           // bNumInterfaces
-        DEFAULT_CONFIGURATION,          // bConfigurationValue
-        0x00,                           // iConfiguration
-        C_RESERVED | C_SELF_POWERED,    // bmAttributes
-        C_POWER(0),                     // bMaxPower
+        CONFIGURATION_DESCRIPTOR_LENGTH,    // bLength
+        CONFIGURATION_DESCRIPTOR,           // bDescriptorType
+        LSB(TOTAL_DESCRIPTOR_LENGTH),       // wTotalLength (LSB)
+        MSB(TOTAL_DESCRIPTOR_LENGTH),       // wTotalLength (MSB)
+        0x01,                               // bNumInterfaces
+        DEFAULT_CONFIGURATION,              // bConfigurationValue
+        0x00,                               // iConfiguration
+        C_RESERVED | C_SELF_POWERED,        // bmAttributes
+        C_POWER(0),                         // bMaxPower
 
-        INTERFACE_DESCRIPTOR_LENGTH,    // bLength
-        INTERFACE_DESCRIPTOR,           // bDescriptorType
-        0x00,                           // bInterfaceNumber
-        0x00,                           // bAlternateSetting
-        0x02,                           // bNumEndpoints
-        HID_CLASS,                      // bInterfaceClass
-        HID_SUBCLASS_NONE,              // bInterfaceSubClass
-        HID_PROTOCOL_NONE,              // bInterfaceProtocol
-        0x00,                           // iInterface
+        INTERFACE_DESCRIPTOR_LENGTH,        // bLength
+        INTERFACE_DESCRIPTOR,               // bDescriptorType
+        0x00,                               // bInterfaceNumber
+        0x00,                               // bAlternateSetting
+        0x02,                               // bNumEndpoints
+        HID_CLASS,                          // bInterfaceClass
+        HID_SUBCLASS_NONE,                  // bInterfaceSubClass
+        HID_PROTOCOL_NONE,                  // bInterfaceProtocol
+        0x00,                               // iInterface
 
-        HID_DESCRIPTOR_LENGTH,          // bLength
-        HID_DESCRIPTOR,                 // bDescriptorType
-        LSB(HID_VERSION_1_11),          // bcdHID (LSB)
-        MSB(HID_VERSION_1_11),          // bcdHID (MSB)
-        0x00,                           // bCountryCode
-        0x01,                           // bNumDescriptors
-        REPORT_DESCRIPTOR,              // bDescriptorType
-        (uint8_t)(LSB(this->reportDescLength())),  // wDescriptorLength (LSB)
-        (uint8_t)(MSB(this->reportDescLength())),  // wDescriptorLength (MSB)
+        HID_DESCRIPTOR_LENGTH,              // bLength
+        HID_DESCRIPTOR,                     // bDescriptorType
+        LSB(HID_VERSION_1_11),              // bcdHID (LSB)
+        MSB(HID_VERSION_1_11),              // bcdHID (MSB)
+        0x00,                               // bCountryCode
+        0x01,                               // bNumDescriptors
+        REPORT_DESCRIPTOR,                  // bDescriptorType
+        (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB)
+        (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB)
 
-        ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
-        ENDPOINT_DESCRIPTOR,            // bDescriptorType
-        PHY_TO_DESC(EPINT_IN),          // bEndpointAddress
-        E_INTERRUPT,                    // bmAttributes
-        LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
-        MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
-        1,                             // bInterval (milliseconds)
+        ENDPOINT_DESCRIPTOR_LENGTH,         // bLength
+        ENDPOINT_DESCRIPTOR,                // bDescriptorType
+        PHY_TO_DESC(EPINT_IN),              // bEndpointAddress
+        E_INTERRUPT,                        // bmAttributes
+        LSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (LSB)
+        MSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (MSB)
+        1,                                  // bInterval (milliseconds)
 
-        ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
-        ENDPOINT_DESCRIPTOR,            // bDescriptorType
-        PHY_TO_DESC(EPINT_OUT),          // bEndpointAddress
-        E_INTERRUPT,                    // bmAttributes
-        LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
-        MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
-        1,                             // bInterval (milliseconds)
+        ENDPOINT_DESCRIPTOR_LENGTH,         // bLength
+        ENDPOINT_DESCRIPTOR,                // bDescriptorType
+        PHY_TO_DESC(EPINT_OUT),             // bEndpointAddress
+        E_INTERRUPT,                        // bmAttributes
+        LSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (LSB)
+        MSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (MSB)
+        1,                                  // bInterval (milliseconds)
     };
     return configurationDescriptor;
 }
--- a/USBHID/USBHID_Types.h	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBHID/USBHID_Types.h	Thu Jul 20 10:14:36 2017 +0100
@@ -25,9 +25,12 @@
 #define HID_VERSION_1_11    (0x0111)
 
 /* HID Class */
-#define HID_CLASS           (3)
-#define HID_SUBCLASS_NONE   (0)
-#define HID_PROTOCOL_NONE   (0)
+#define HID_CLASS             (3)
+#define HID_SUBCLASS_NONE     (0)
+#define HID_SUBCLASS_BOOT     (1)
+#define HID_PROTOCOL_NONE     (0)
+#define HID_PROTOCOL_KEYBOARD (1)
+#define HID_PROTOCOL_MOUSE    (2)
 
 /* Descriptors */
 #define HID_DESCRIPTOR          (33)
--- a/USBHID/USBKeyboard.cpp	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBHID/USBKeyboard.cpp	Thu Jul 20 10:14:36 2017 +0100
@@ -503,51 +503,51 @@
 
 uint8_t * USBKeyboard::configurationDesc() {
     static uint8_t configurationDescriptor[] = {
-        CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
-        CONFIGURATION_DESCRIPTOR,       // bDescriptorType
-        LSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (LSB)
-        MSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (MSB)
-        0x01,                           // bNumInterfaces
-        DEFAULT_CONFIGURATION,          // bConfigurationValue
-        0x00,                           // iConfiguration
-        C_RESERVED | C_SELF_POWERED,    // bmAttributes
-        C_POWER(0),                     // bMaxPowerHello World from Mbed
+        CONFIGURATION_DESCRIPTOR_LENGTH,    // bLength
+        CONFIGURATION_DESCRIPTOR,           // bDescriptorType
+        LSB(TOTAL_DESCRIPTOR_LENGTH),       // wTotalLength (LSB)
+        MSB(TOTAL_DESCRIPTOR_LENGTH),       // wTotalLength (MSB)
+        0x01,                               // bNumInterfaces
+        DEFAULT_CONFIGURATION,              // bConfigurationValue
+        0x00,                               // iConfiguration
+        C_RESERVED | C_SELF_POWERED,        // bmAttributes
+        C_POWER(0),                         // bMaxPower
 
-        INTERFACE_DESCRIPTOR_LENGTH,    // bLength
-        INTERFACE_DESCRIPTOR,           // bDescriptorType
-        0x00,                           // bInterfaceNumber
-        0x00,                           // bAlternateSetting
-        0x02,                           // bNumEndpoints
-        HID_CLASS,                      // bInterfaceClass
-        1,                              // bInterfaceSubClass
-        1,                              // bInterfaceProtocol (keyboard)
-        0x00,                           // iInterface
+        INTERFACE_DESCRIPTOR_LENGTH,        // bLength
+        INTERFACE_DESCRIPTOR,               // bDescriptorType
+        0x00,                               // bInterfaceNumber
+        0x00,                               // bAlternateSetting
+        0x02,                               // bNumEndpoints
+        HID_CLASS,                          // bInterfaceClass
+        HID_SUBCLASS_BOOT,                  // bInterfaceSubClass
+        HID_PROTOCOL_KEYBOARD,              // bInterfaceProtocol
+        0x00,                               // iInterface
 
-        HID_DESCRIPTOR_LENGTH,          // bLength
-        HID_DESCRIPTOR,                 // bDescriptorType
-        LSB(HID_VERSION_1_11),          // bcdHID (LSB)
-        MSB(HID_VERSION_1_11),          // bcdHID (MSB)
-        0x00,                           // bCountryCode
-        0x01,                           // bNumDescriptors
-        REPORT_DESCRIPTOR,              // bDescriptorType
-        (uint8_t)(LSB(reportDescLength())),  // wDescriptorLength (LSB)
-        (uint8_t)(MSB(reportDescLength())),  // wDescriptorLength (MSB)
+        HID_DESCRIPTOR_LENGTH,              // bLength
+        HID_DESCRIPTOR,                     // bDescriptorType
+        LSB(HID_VERSION_1_11),              // bcdHID (LSB)
+        MSB(HID_VERSION_1_11),              // bcdHID (MSB)
+        0x00,                               // bCountryCode
+        0x01,                               // bNumDescriptors
+        REPORT_DESCRIPTOR,                  // bDescriptorType
+        (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB)
+        (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB)
 
-        ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
-        ENDPOINT_DESCRIPTOR,            // bDescriptorType
-        PHY_TO_DESC(EPINT_IN),          // bEndpointAddress
-        E_INTERRUPT,                    // bmAttributes
-        LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
-        MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
-        1,                             // bInterval (milliseconds)
+        ENDPOINT_DESCRIPTOR_LENGTH,         // bLength
+        ENDPOINT_DESCRIPTOR,                // bDescriptorType
+        PHY_TO_DESC(EPINT_IN),              // bEndpointAddress
+        E_INTERRUPT,                        // bmAttributes
+        LSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (LSB)
+        MSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (MSB)
+        1,                                  // bInterval (milliseconds)
 
-        ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
-        ENDPOINT_DESCRIPTOR,            // bDescriptorType
-        PHY_TO_DESC(EPINT_OUT),          // bEndpointAddress
-        E_INTERRUPT,                    // bmAttributes
-        LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
-        MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
-        1,                             // bInterval (milliseconds)
+        ENDPOINT_DESCRIPTOR_LENGTH,         // bLength
+        ENDPOINT_DESCRIPTOR,                // bDescriptorType
+        PHY_TO_DESC(EPINT_OUT),             // bEndpointAddress
+        E_INTERRUPT,                        // bmAttributes
+        LSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (LSB)
+        MSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (MSB)
+        1,                                  // bInterval (milliseconds)
     };
     return configurationDescriptor;
 }
--- a/USBHID/USBKeyboard.h	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBHID/USBKeyboard.h	Thu Jul 20 10:14:36 2017 +0100
@@ -22,11 +22,16 @@
 #include "USBHID.h"
 #include "Stream.h"
 
-/* Modifiers */
+/* Modifiers, left keys then right keys. */
 enum MODIFIER_KEY {
-    KEY_CTRL = 1,
-    KEY_SHIFT = 2,
-    KEY_ALT = 4,
+    KEY_CTRL = 0x01,
+    KEY_SHIFT = 0x02,
+    KEY_ALT = 0x04,
+    KEY_LOGO = 0x08,
+    KEY_RCTRL = 0x10,
+    KEY_RSHIFT = 0x20,
+    KEY_RALT = 0x40,
+    KEY_RLOGO = 0x80,
 };
 
 
--- a/USBHID/USBMouse.cpp	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBHID/USBMouse.cpp	Thu Jul 20 10:14:36 2017 +0100
@@ -142,7 +142,6 @@
         return reportDescriptor;
     } else if (mouse_type == ABS_MOUSE) {
         static uint8_t reportDescriptor[] = {
-
             USAGE_PAGE(1), 0x01,           // Generic Desktop
             USAGE(1), 0x02,                // Mouse
             COLLECTION(1), 0x01,           // Application
@@ -195,51 +194,51 @@
 
 uint8_t * USBMouse::configurationDesc() {
     static uint8_t configurationDescriptor[] = {
-        CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
-        CONFIGURATION_DESCRIPTOR,       // bDescriptorType
-        LSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (LSB)
-        MSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (MSB)
-        0x01,                           // bNumInterfaces
-        DEFAULT_CONFIGURATION,          // bConfigurationValue
-        0x00,                           // iConfiguration
-        C_RESERVED | C_SELF_POWERED,    // bmAttributes
-        C_POWER(0),                     // bMaxPowerHello World from Mbed
+        CONFIGURATION_DESCRIPTOR_LENGTH,    // bLength
+        CONFIGURATION_DESCRIPTOR,           // bDescriptorType
+        LSB(TOTAL_DESCRIPTOR_LENGTH),       // wTotalLength (LSB)
+        MSB(TOTAL_DESCRIPTOR_LENGTH),       // wTotalLength (MSB)
+        0x01,                               // bNumInterfaces
+        DEFAULT_CONFIGURATION,              // bConfigurationValue
+        0x00,                               // iConfiguration
+        C_RESERVED | C_SELF_POWERED,        // bmAttributes
+        C_POWER(0),                         // bMaxPower
 
-        INTERFACE_DESCRIPTOR_LENGTH,    // bLength
-        INTERFACE_DESCRIPTOR,           // bDescriptorType
-        0x00,                           // bInterfaceNumber
-        0x00,                           // bAlternateSetting
-        0x02,                           // bNumEndpoints
-        HID_CLASS,                      // bInterfaceClass
-        1,                              // bInterfaceSubClass
-        2,                              // bInterfaceProtocol (mouse)
-        0x00,                           // iInterface
+        INTERFACE_DESCRIPTOR_LENGTH,        // bLength
+        INTERFACE_DESCRIPTOR,               // bDescriptorType
+        0x00,                               // bInterfaceNumber
+        0x00,                               // bAlternateSetting
+        0x02,                               // bNumEndpoints
+        HID_CLASS,                          // bInterfaceClass
+        HID_SUBCLASS_BOOT,                  // bInterfaceSubClass
+        HID_PROTOCOL_MOUSE,                 // bInterfaceProtocol
+        0x00,                               // iInterface
 
-        HID_DESCRIPTOR_LENGTH,          // bLength
-        HID_DESCRIPTOR,                 // bDescriptorType
-        LSB(HID_VERSION_1_11),          // bcdHID (LSB)
-        MSB(HID_VERSION_1_11),          // bcdHID (MSB)
-        0x00,                           // bCountryCode
-        0x01,                           // bNumDescriptors
-        REPORT_DESCRIPTOR,              // bDescriptorType
-        (uint8_t)(LSB(reportDescLength())),        // wDescriptorLength (LSB)
-        (uint8_t)(MSB(reportDescLength())),        // wDescriptorLength (MSB)
+        HID_DESCRIPTOR_LENGTH,              // bLength
+        HID_DESCRIPTOR,                     // bDescriptorType
+        LSB(HID_VERSION_1_11),              // bcdHID (LSB)
+        MSB(HID_VERSION_1_11),              // bcdHID (MSB)
+        0x00,                               // bCountryCode
+        0x01,                               // bNumDescriptors
+        REPORT_DESCRIPTOR,                  // bDescriptorType
+        (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB)
+        (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB)
 
-        ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
-        ENDPOINT_DESCRIPTOR,            // bDescriptorType
-        PHY_TO_DESC(EPINT_IN),          // bEndpointAddress
-        E_INTERRUPT,                    // bmAttributes
-        LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
-        MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
-        1,                              // bInterval (milliseconds)
+        ENDPOINT_DESCRIPTOR_LENGTH,         // bLength
+        ENDPOINT_DESCRIPTOR,                // bDescriptorType
+        PHY_TO_DESC(EPINT_IN),              // bEndpointAddress
+        E_INTERRUPT,                        // bmAttributes
+        LSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (LSB)
+        MSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (MSB)
+        1,                                  // bInterval (milliseconds)
 
-        ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
-        ENDPOINT_DESCRIPTOR,            // bDescriptorType
-        PHY_TO_DESC(EPINT_OUT),         // bEndpointAddress
-        E_INTERRUPT,                    // bmAttributes
-        LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
-        MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
-        1,                              // bInterval (milliseconds)
+        ENDPOINT_DESCRIPTOR_LENGTH,         // bLength
+        ENDPOINT_DESCRIPTOR,                // bDescriptorType
+        PHY_TO_DESC(EPINT_OUT),             // bEndpointAddress
+        E_INTERRUPT,                        // bmAttributes
+        LSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (LSB)
+        MSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (MSB)
+        1,                                  // bInterval (milliseconds)
     };
     return configurationDescriptor;
 }
--- a/USBSerial/USBCDC.cpp	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBSerial/USBCDC.cpp	Thu Jul 20 10:14:36 2017 +0100
@@ -38,6 +38,10 @@
     USBDevice::connect(connect_blocking);
 }
 
+void USBCDC::USBCallback_busReset(void) {
+    terminal_connected = false;
+};
+
 bool USBCDC::USBCallback_request(void) {
     /* Called in ISR context */
 
--- a/USBSerial/USBCDC.h	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBSerial/USBCDC.h	Thu Jul 20 10:14:36 2017 +0100
@@ -116,6 +116,7 @@
     virtual bool USBCallback_request();
     virtual void USBCallback_requestCompleted(uint8_t *buf, uint32_t length);
     virtual bool USBCallback_setConfiguration(uint8_t configuration);
+    virtual void USBCallback_busReset(void);
     volatile bool terminal_connected;
 
 };
--- a/USBSerial/USBSerial.cpp	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBSerial/USBSerial.cpp	Thu Jul 20 10:14:36 2017 +0100
@@ -56,8 +56,9 @@
         buf.queue(c[i]);
     }
 
-    //call a potential handler
-    rx.call();
+    //call a potential handlenr
+    if (rx)
+        rx.call();
 
     return true;
 }
@@ -65,3 +66,7 @@
 uint8_t USBSerial::available() {
     return buf.available();
 }
+
+bool USBSerial::connected() {
+    return terminal_connected;
+}
--- a/USBSerial/USBSerial.h	Fri Nov 11 17:59:00 2016 +0000
+++ b/USBSerial/USBSerial.h	Thu Jul 20 10:14:36 2017 +0100
@@ -22,7 +22,7 @@
 #include "USBCDC.h"
 #include "Stream.h"
 #include "CircBuffer.h"
-
+#include "Callback.h"
 
 /**
 * USBSerial example
@@ -83,6 +83,13 @@
     */
     uint8_t available();
 
+     /**
+    * Check if the terminal is connected.
+    *
+    * @returns connection status
+    */
+    bool connected();
+
     /** Determine if there is a character available to read
      *
      *  @returns
@@ -153,7 +160,7 @@
     }
 
 private:
-    FunctionPointer rx;
+    Callback<void()> rx;
     CircBuffer<uint8_t,128> buf;
     void (*settingsChangedCallback)(int baud, int bits, int parity, int stop);
 };