cancel
Showing results for 
Search instead for 
Did you mean: 

How to transmit and receive data with USB Custom HID device?

VCapu
Associate III

I am using a custom board with a STM32F4. It seems that I can receive the data through this function that is called by interrupt and adding the pointer read_buffer, that I use inside the RxUSB() function to get the data and do the necessary operations on it.

USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint32_t size)

{

 HAL_StatusTypeDef hal_status = HAL_OK;

 USBD_StatusTypeDef usb_status = USBD_OK;

 read_buffer = pbuf;

 hal_status = HAL_PCD_EP_Receive(pdev->pData, ep_addr, pbuf, size);

 usb_status = USBD_Get_USB_Status(hal_status);

if(usb_status == USBD_OK) 

RxUSB(); 

 return usb_status;

}

Instead I transmit using in every point I need (for example in the functions that are in the Main) the function USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, ArrayTxUSB, CODA_USB);. It's not called by an interrupt.

The problem that I am seeing is that the communication stops to work soon and sometimes I can't even transmit. Maybe this is not the right way to trasmit and receive.

Could anyone help me? I'm stuck on this problem.

1 ACCEPTED SOLUTION

Accepted Solutions

Hello @VCapu​,

You can refer to following example available under STM32CubeF4 package (path: Projects\STM32446E_EVAL\Applications\USB_Device\CustomHID_Standalone).

You could use this as inspiration.

BeST Regards,

Walid

View solution in original post

5 REPLIES 5

Hello @VCapu​,

You can refer to following example available under STM32CubeF4 package (path: Projects\STM32446E_EVAL\Applications\USB_Device\CustomHID_Standalone).

You could use this as inspiration.

BeST Regards,

Walid

VCapu
Associate III

Now I can receive without problems inside the function USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint32_t size) called by interrupt. Iside this function I placed my rxusb function that manage the receive buffer.

For the transmission, I see that not all the messages sent works, maybe I should use also a transmission called by interrupt. How could I do it? Thank you very much.

Hello @VCapu​ ,

You could reuse the same method from the example I sent you.

Indeed, the transmission was done in the USBD_CUSTOM_HID_SendReport() function, and you mentioned that you implemented it in the interrupt.

So it should work fine for you.

Try to reproduce the same process as in the example. 

BeST Regards,

Walid

Thank you. I am using for transmission the function USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, ArrayTxUSB, CODA_USB); but without interrupt.

Where I should put this function to transmit through interrupt? And how should I enable this interrupt? Which is the event that triggers it?

It depends on your application.

In fact, in our example we use the potentiometer of the STM32446E-EVAL board to transfer the result of the converted voltage

 (via the ADC) the to the PC host (these values are sent to the PC using the endpoint1 IN) , so you will find USBD_CUSTOM_HID_SendReport() function called in the ADCx_DMA_IRQHandler() function.

Try to go into the details of the example and you will have all the information you need.