diff --git a/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/main.c b/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/main.c index 09a525931..c1d2cfea5 100644 --- a/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/main.c +++ b/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/main.c @@ -420,7 +420,7 @@ static void usb_event(USBDriver *usbp, usbevent_t event) { usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep2config); /* Resetting the state of the CDC subsystem.*/ - sduConfigureHookI(usbp); + sduConfigureHookI(&SDU1); chSysUnlockFromIsr(); return; diff --git a/os/hal/include/serial_usb.h b/os/hal/include/serial_usb.h index 3d2a7483a..28ab3a940 100644 --- a/os/hal/include/serial_usb.h +++ b/os/hal/include/serial_usb.h @@ -218,7 +218,7 @@ extern "C" { void sduObjectInit(SerialUSBDriver *sdp); void sduStart(SerialUSBDriver *sdup, const SerialUSBConfig *config); void sduStop(SerialUSBDriver *sdup); - void sduConfigureHookI(USBDriver *usbp); + void sduConfigureHookI(SerialUSBDriver *sdup); bool_t sduRequestsHook(USBDriver *usbp); void sduDataTransmitted(USBDriver *usbp, usbep_t ep); void sduDataReceived(USBDriver *usbp, usbep_t ep); diff --git a/os/hal/platforms/STM32/OTGv1/usb_lld.h b/os/hal/platforms/STM32/OTGv1/usb_lld.h index 087d1cb01..570309750 100644 --- a/os/hal/platforms/STM32/OTGv1/usb_lld.h +++ b/os/hal/platforms/STM32/OTGv1/usb_lld.h @@ -363,11 +363,6 @@ struct USBDriver { * @brief Current configuration data. */ const USBConfig *config; - /** - * @brief Field available to user, it can be used to associate an - * application-defined handler to the USB driver. - */ - void *param; /** * @brief Bit map of the transmitting IN endpoints. */ @@ -380,6 +375,20 @@ struct USBDriver { * @brief Active endpoints configurations. */ const USBEndpointConfig *epc[USB_MAX_ENDPOINTS + 1]; + /** + * @brief Fields available to user, it can be used to associate an + * application-defined handler to an IN endpoint. + * @note The base index is one, the endpoint zero does not have a + * reserved element in this array. + */ + void *in_params[USB_MAX_ENDPOINTS]; + /** + * @brief Fields available to user, it can be used to associate an + * application-defined handler to an OUT endpoint. + * @note The base index is one, the endpoint zero does not have a + * reserved element in this array. + */ + void *out_params[USB_MAX_ENDPOINTS]; /** * @brief Endpoint 0 state. */ diff --git a/os/hal/platforms/STM32/USBv1/usb_lld.h b/os/hal/platforms/STM32/USBv1/usb_lld.h index fbd44fafe..d4602f65f 100644 --- a/os/hal/platforms/STM32/USBv1/usb_lld.h +++ b/os/hal/platforms/STM32/USBv1/usb_lld.h @@ -301,6 +301,20 @@ struct USBDriver { * @brief Active endpoints configurations. */ const USBEndpointConfig *epc[USB_MAX_ENDPOINTS + 1]; + /** + * @brief Fields available to user, it can be used to associate an + * application-defined handler to an IN endpoint. + * @note The base index is one, the endpoint zero does not have a + * reserved element in this array. + */ + void *in_params[USB_MAX_ENDPOINTS]; + /** + * @brief Fields available to user, it can be used to associate an + * application-defined handler to an OUT endpoint. + * @note The base index is one, the endpoint zero does not have a + * reserved element in this array. + */ + void *out_params[USB_MAX_ENDPOINTS]; /** * @brief Endpoint 0 state. */ diff --git a/os/hal/src/serial_usb.c b/os/hal/src/serial_usb.c index 37b1d3fe6..ff6e94b07 100644 --- a/os/hal/src/serial_usb.c +++ b/os/hal/src/serial_usb.c @@ -206,6 +206,7 @@ void sduObjectInit(SerialUSBDriver *sdup) { * @api */ void sduStart(SerialUSBDriver *sdup, const SerialUSBConfig *config) { + USBDriver *usbp = config->usbp; chDbgCheck(sdup != NULL, "sduStart"); @@ -213,8 +214,10 @@ void sduStart(SerialUSBDriver *sdup, const SerialUSBConfig *config) { chDbgAssert((sdup->state == SDU_STOP) || (sdup->state == SDU_READY), "sduStart(), #1", "invalid state"); + usbp->in_params[config->bulk_in - 1] = sdup; + usbp->out_params[config->bulk_out - 1] = sdup; + usbp->in_params[config->int_in - 1] = sdup; sdup->config = config; - config->usbp->param = sdup; sdup->state = SDU_READY; chSysUnlock(); } @@ -229,32 +232,40 @@ void sduStart(SerialUSBDriver *sdup, const SerialUSBConfig *config) { * @api */ void sduStop(SerialUSBDriver *sdup) { + USBDriver *usbp = sdup->config->usbp; chDbgCheck(sdup != NULL, "sdStop"); chSysLock(); + chDbgAssert((sdup->state == SDU_STOP) || (sdup->state == SDU_READY), "sduStop(), #1", "invalid state"); + /* Driver in stopped state.*/ + usbp->in_params[sdup->config->bulk_in - 1] = NULL; + usbp->out_params[sdup->config->bulk_out - 1] = NULL; + usbp->in_params[sdup->config->int_in - 1] = NULL; + sdup->state = SDU_STOP; + + /* Queues reset in order to signal the driver stop to the application.*/ + chnAddFlagsI(sdup, CHN_DISCONNECTED); chIQResetI(&sdup->iqueue); chOQResetI(&sdup->oqueue); chSchRescheduleS(); - chnAddFlagsI(sdup, CHN_DISCONNECTED); - sdup->state = SDU_STOP; chSysUnlock(); } /** * @brief USB device configured handler. * - * @param[in] usbp pointer to the @p USBDriver object + * @param[in] sdup pointer to a @p SerialUSBDriver object * * @iclass */ -void sduConfigureHookI(USBDriver *usbp) { - SerialUSBDriver *sdup = usbp->param; +void sduConfigureHookI(SerialUSBDriver *sdup) { + USBDriver *usbp = sdup->config->usbp; chIQResetI(&sdup->iqueue); chOQResetI(&sdup->oqueue); @@ -312,9 +323,9 @@ bool_t sduRequestsHook(USBDriver *usbp) { */ void sduDataTransmitted(USBDriver *usbp, usbep_t ep) { size_t n; - SerialUSBDriver *sdup = usbp->param; + SerialUSBDriver *sdup = usbp->in_params[ep - 1]; - if (sdup->state != SDU_READY) + if (sdup == NULL) return; chSysLockFromIsr(); @@ -358,9 +369,9 @@ void sduDataTransmitted(USBDriver *usbp, usbep_t ep) { */ void sduDataReceived(USBDriver *usbp, usbep_t ep) { size_t n, maxsize; - SerialUSBDriver *sdup = usbp->param; + SerialUSBDriver *sdup = usbp->out_params[ep - 1]; - if (sdup->state != SDU_READY) + if (sdup == NULL) return; chSysLockFromIsr(); diff --git a/os/hal/src/usb.c b/os/hal/src/usb.c index 745b2d436..1da532abf 100644 --- a/os/hal/src/usb.c +++ b/os/hal/src/usb.c @@ -238,10 +238,14 @@ void usbInit(void) { * @init */ void usbObjectInit(USBDriver *usbp) { + unsigned i; usbp->state = USB_STOP; usbp->config = NULL; - usbp->param = NULL; + for (i = 0; i < USB_MAX_ENDPOINTS; i++) { + usbp->in_params[i] = NULL; + usbp->out_params[i] = NULL; + } usbp->transmitting = 0; usbp->receiving = 0; } diff --git a/os/hal/templates/usb_lld.h b/os/hal/templates/usb_lld.h index 6d2f2348f..7dbf8b4b9 100644 --- a/os/hal/templates/usb_lld.h +++ b/os/hal/templates/usb_lld.h @@ -226,11 +226,6 @@ struct USBDriver { * @brief Current configuration data. */ const USBConfig *config; - /** - * @brief Field available to user, it can be used to associate an - * application-defined handler to the USB driver. - */ - void *param; /** * @brief Bit map of the transmitting IN endpoints. */ @@ -243,6 +238,20 @@ struct USBDriver { * @brief Active endpoints configurations. */ const USBEndpointConfig *epc[USB_MAX_ENDPOINTS + 1]; + /** + * @brief Fields available to user, it can be used to associate an + * application-defined handler to an IN endpoint. + * @note The base index is one, the endpoint zero does not have a + * reserved element in this array. + */ + void *in_params[USB_MAX_ENDPOINTS]; + /** + * @brief Fields available to user, it can be used to associate an + * application-defined handler to an OUT endpoint. + * @note The base index is one, the endpoint zero does not have a + * reserved element in this array. + */ + void *out_params[USB_MAX_ENDPOINTS]; /** * @brief Endpoint 0 state. */ diff --git a/testhal/STM32F1xx/USB_CDC/main.c b/testhal/STM32F1xx/USB_CDC/main.c index 1bcba05fb..8b11e7ed4 100644 --- a/testhal/STM32F1xx/USB_CDC/main.c +++ b/testhal/STM32F1xx/USB_CDC/main.c @@ -294,7 +294,7 @@ static void usb_event(USBDriver *usbp, usbevent_t event) { usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep2config); /* Resetting the state of the CDC subsystem.*/ - sduConfigureHookI(usbp); + sduConfigureHookI(&SDU1); chSysUnlockFromIsr(); return; diff --git a/testhal/STM32F1xx/USB_CDC_F107/main.c b/testhal/STM32F1xx/USB_CDC_F107/main.c index 9e4399983..8dda559c0 100644 --- a/testhal/STM32F1xx/USB_CDC_F107/main.c +++ b/testhal/STM32F1xx/USB_CDC_F107/main.c @@ -294,7 +294,7 @@ static void usb_event(USBDriver *usbp, usbevent_t event) { usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep2config); /* Resetting the state of the CDC subsystem.*/ - sduConfigureHookI(usbp); + sduConfigureHookI(&SDU1); chSysUnlockFromIsr(); return; diff --git a/testhal/STM32F30x/USB_CDC/main.c b/testhal/STM32F30x/USB_CDC/main.c index 8c3f99a54..6555da8d3 100644 --- a/testhal/STM32F30x/USB_CDC/main.c +++ b/testhal/STM32F30x/USB_CDC/main.c @@ -301,7 +301,7 @@ static void usb_event(USBDriver *usbp, usbevent_t event) { usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep2config); /* Resetting the state of the CDC subsystem.*/ - sduConfigureHookI(usbp); + sduConfigureHookI(&SDU1); chSysUnlockFromIsr(); return; diff --git a/testhal/STM32F37x/USB_CDC/main.c b/testhal/STM32F37x/USB_CDC/main.c index 286d26c01..200a0f3d5 100644 --- a/testhal/STM32F37x/USB_CDC/main.c +++ b/testhal/STM32F37x/USB_CDC/main.c @@ -300,7 +300,7 @@ static void usb_event(USBDriver *usbp, usbevent_t event) { usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep2config); /* Resetting the state of the CDC subsystem.*/ - sduConfigureHookI(usbp); + sduConfigureHookI(&SDU1); chSysUnlockFromIsr(); return; diff --git a/testhal/STM32F4xx/USB_CDC/main.c b/testhal/STM32F4xx/USB_CDC/main.c index ecf575b4a..8bdf3c0ba 100644 --- a/testhal/STM32F4xx/USB_CDC/main.c +++ b/testhal/STM32F4xx/USB_CDC/main.c @@ -294,7 +294,7 @@ static void usb_event(USBDriver *usbp, usbevent_t event) { usbInitEndpointI(usbp, USBD2_INTERRUPT_REQUEST_EP, &ep2config); /* Resetting the state of the CDC subsystem.*/ - sduConfigureHookI(usbp); + sduConfigureHookI(&SDU2); chSysUnlockFromIsr(); return;