AnsweredAssumed Answered

Send SERIAL_STATE with STM32CubeMX USB CDC driver

Question asked by Stany MARCEL on Apr 20, 2016
Latest reply on Oct 11, 2016 by STM32Cube-T
Hello,

In order to send SERIAL_STATE over USB IN interrupt endpoint I had to increase the CDC_CMD_PACKET_SIZE to 10 in usbd_cdc.h.

Is there a way to do this in cubemx ? I'd like to keep my modification at each code regeneration.

By the way, if someone is interrested to send SERIAL_STATE here are my modifications :

In usb_cdc_if.h add in USER CODE BEGIN EXPORTED_TYPES:

 typedef union
 {
     uint8_t val;
     struct{
         unsigned dcd         :1;
         unsigned dsr         :1;
         unsigned break_err   :1;
         unsigned ri          :1;
         unsigned frame_err   :1;
         unsigned parity_err  :1;
         unsigned overrun_err :1;
         unsigned reserved    :1;
     };
 } USBD_CDC_SerialStateTypeDef;


and in USER CODE BEGIN EXPORTED_FUNCTIONS:

uint8_t CDC_Serial_State(USBD_CDC_SerialStateTypeDef serial_state);

In usb_cdc_if.h add in USER CODE BEGIN PRIVATE_VARIABLES:

  uint8_t CDC_Serial_State_Buf[10] = {
          0xA1, // vmRequestType
          0x20, // SERIAL_STATE
          0x00, // wValue
          0x00,
          0x00, // wIndex
          0x00,
          0x02, // wLength
          0x00,
          0x00, // UART state bitmap
          0x00,
  };

and add in USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION (I know It's not private but there is no public user code section) :


/**
  * @brief  CDC_Serial_State
  *         Send SERIAL_STATE over USB IN interrupt endpoint through this function.
  *         @note
  *
  *
  * @param  serial_state: Uart State Bitmap Value
  * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
  */
uint8_t  CDC_Serial_State(USBD_CDC_SerialStateTypeDef serial_state) {
    USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDevice_0->pClassData;

    if (hcdc->TxState != 0){
        return USBD_BUSY;
    }
    CDC_Serial_State_Buf[8] = serial_state.val;

    hUsbDevice_0->ep0_state = USBD_EP0_STATUS_IN;
    hUsbDevice_0->ep_in[0].total_length = 10;
    hUsbDevice_0->ep_in[0].rem_length   = 10;
   /* Start the transfer */
    USBD_LL_Transmit (hUsbDevice_0, CDC_CMD_EP, CDC_Serial_State_Buf, 10);

    return USBD_OK;
}


For the moment it is only tested with RING state, on a small demo project with a stm32l4dicovery.

All improvement proposals or comments are welcome.

Regards,













Outcomes