cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L452 USB CID problem when I unplug and again plug in the USB connector

AbbasK
Associate II

Hello all,

When I run the code, it works as expected, but when I unplug and then plug-in again, the system halts and when I debug it, it is like the USB_IRQHandler() is being called in succession very fast as if the IRQ pending bit is not reset. The value if the USB->ISTR register is seen 0 and NVIC->ICPR[USB_IRQn] is also seen 0. I can't figure out who is triggering the USB and why the pending bit is not reset after the interrupt handling.

I appreciate any help from you

 


I am using stm32CubeMX auto generated code for Keil using USB Middleware

I have attached the setting of the StmCubeMX project

AbbasK_0-1691142223701.png

AbbasK_1-1691142261989.png

 

 

3 REPLIES 3
AbbasK
Associate II

I have found the cause but I don't know how it causes the problem
Since in the USB Middleware, in the function "USBD_CUSTOM_HID_Setup", the command "CUSTOM_HID_REQ_GET_REPORT" is not implemented! I replaced the setup function by by custom setup function in which, for the command "CUSTOM_HID_REQ_GET_REPORT", it serves it itself but for the rest, it calls the original setup function. I did as follows:

static uint8_t USBD_CUSTOM_HID_Setup_GET_REPORT_implement(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);

static int8_t CUSTOM_HID_Init_FS(void)
{
/* USER CODE BEGIN 4 */
/**
* replace the original setup function (USBD_CUSTOM_HID.Setup) with the local
* defined setup function (USBD_CUSTOM_HID_Setup_GET_REPORT_implement), because
* the request "CUSTOM_HID_REQ_GET_REPORT" is not implemented in the original
* setup function and we need to implement it. This local function implements
* the behaviour for this request and for the rest requests calls the original
* setup function
*/
pfOriginal_USBD_CUSTOM_HID_Setup = USBD_CUSTOM_HID.Setup;
USBD_CUSTOM_HID.Setup = USBD_CUSTOM_HID_Setup_GET_REPORT_implement;

return (USBD_OK);
/* USER CODE END 4 */
}

...

static uint8_t USBD_CUSTOM_HID_Setup_GET_REPORT_implement(USBD_HandleTypeDef *pdev,
USBD_SetupReqTypedef *req)
{
USBD_StatusTypeDef ret;

if ((req->bmRequest & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_CLASS &&
(req->bRequest == CUSTOM_HID_REQ_GET_REPORT))
{
vLocal_Setup_GET_REPORT_implement(pdev);
ret = USBD_OK;
}
else
{
ret = (USBD_StatusTypeDef) (pfOriginal_USBD_CUSTOM_HID_Setup)(pdev, req);
}
return ret;
}

I did the above mentioned modification in order that, I do not change the generated library from the stm32CubeMx, so that, after every code regeneration from the Stm32CubeMx, I don't have to modify this part again.
But somehow this code doesn't work when the USB connection is unplugged and plugged in again and it gets crazy!
I would still appreciate if anyone explain it!
Thanks in advance

 

 
AbbasK
Associate II

I have found the cause!
After plugging in again, the function "CUSTOM_HID_Init_FS" is called again, but this time, the value of "USBD_CUSTOM_HID.Setup" has already been changed. So by copying it again, the original setup function is lost!!!
Very studpid!
The solution would be like this:

static int8_t CUSTOM_HID_Init_FS(void)
{
/* USER CODE BEGIN 4 */
/**
* replace the original setup function (USBD_CUSTOM_HID.Setup) with the local
* defined setup function (USBD_CUSTOM_HID_Setup_GET_REPORT_implement), because
* the request "CUSTOM_HID_REQ_GET_REPORT" is not implemented in the original
* setup function and we need to implement it. This local function implements
* the behaviour for this request and for the rest requests calls the original
* setup function
*/
if (pfOriginal_USBD_CUSTOM_HID_Setup == NULL) // It must only be done once at first. because the second time, the setup function is already replaced with my function
{
pfOriginal_USBD_CUSTOM_HID_Setup = USBD_CUSTOM_HID.Setup;
}
USBD_CUSTOM_HID.Setup = USBD_CUSTOM_HID_Setup_GET_REPORT_implement;

return (USBD_OK);
/* USER CODE END 4 */
}

ABOUA
ST Employee

Hello AbbasK,

If understood you are trying to implement the CUSTOM_HID_REQ_GET_REPORT,

please note that this feature has been already implemented and part of our latest device lib v2.11.2 available under github stm32_mw_usb_device/Class/CustomHID/Src/usbd_customhid.c at master · STMicroelectronics/stm32_mw_usb_device · GitHub

Regards