alambe94 / I-CUBE-USBD-Composite

Create STM32 USB Composite devices with ease.
MIT License
142 stars 30 forks source link

CDC_Receive() and USBD_CUSTOM_HID_SendReport() usage #26

Open msiweka opened 1 year ago

msiweka commented 1 year ago

I tried to use this wrapper on STM32G0Bxx. Wrapper is initialized in main function with this functions:

MX_USB_DRD_FS_PCD_Init();
MX_USB_DEVICE_Init();

I can send data by CDC_Transmit(), but I don't know how to get data from callback function CDC_Receive(). Also USBD_CUSTOM_HID_SendReport() returns 0(USBD_OK) in first run, but in all next returns 1(USBD_BUSY).

I can't find specification for this wrapper Same here: https://stackoverflow.com/questions/75885164/how-to-get-data-from-callback-function-cdc-receive

paspartu22 commented 10 months ago

Sooo, maybe its not intended way to do it, but i did this. I added uint16_t dataRxLength; to the USBD_CDC_ACM_HandleTypeDef; after i saved buffer to hcdc->data and len to hcdc->dataRxLength (hcdc->RxLength is len of whole buffer, but hcdc->dataRxLen is len of current message).

        static int8_t CDC_Receive(uint8_t cdc_ch, uint8_t *Buf, uint32_t *Len){
    /* USER CODE BEGIN 6 */

    CDC_Transmit(cdc_ch, Buf, *Len); // echo back on same channel
    USBD_CDC_SetRxBuffer(cdc_ch, &hUsbDevice, Buf);

    extern USBD_CDC_ACM_HandleTypeDef CDC_ACM_Class_Data[];
    USBD_CDC_ACM_HandleTypeDef *hcdc = NULL;

    hcdc = &CDC_ACM_Class_Data[cdc_ch];
    hcdc->dataRxLength = *Len;
    memcpy(hcdc->data, Buf, *Len);

    USBD_CDC_ReceivePacket(cdc_ch, &hUsbDevice);

    return (USBD_OK);

    /* USER CODE END 6 */

And when i want to read buffer i did

void CDC_Event()
{
    extern USBD_CDC_ACM_HandleTypeDef CDC_ACM_Class_Data[];
    USBD_CDC_ACM_HandleTypeDef *hcdc = NULL;
    hcdc = &CDC_ACM_Class_Data[0];
    if (hcdc->dataRxLength != 0)
    {
    //////////// work with hcdc->data here///////
        hcdc->dataRxLength = 0;
    }
}
russeree commented 8 months ago

void CDC_Event()
{
    extern USBD_CDC_ACM_HandleTypeDef CDC_ACM_Class_Data[];
    USBD_CDC_ACM_HandleTypeDef *hcdc = NULL;
    hcdc = &CDC_ACM_Class_Data[0];
    if (hcdc->dataRxLength != 0)
    {
    //////////// work with hcdc->data here///////
        hcdc->dataRxLength = 0;
    }
}

Is the above blocking code? Is interrupt based execution possible, such as data comes in, interrupt handler takes care of the event then moves back into main execution?