cancel
Showing results for 
Search instead for 
Did you mean: 

Send SERIAL_STATE with STM32CubeMX USB CDC driver

stanypub
Associate
Posted on April 20, 2016 at 12:25

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,

#usb-cdc-serial_state
2 REPLIES 2
Amel NASRI
ST Employee
Posted on April 20, 2016 at 17:23

Hi Stany MARCEL,

Your question will be forwarded to our MX team for further checks.

Then thanks for sharing your solution.

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

stm32cube-t
Senior III
Posted on October 11, 2016 at 11:22

Dear user,

Unfortunately there is no solution to your problem for now. Indeed STM32CubeMX, inherits the file usb_cdc.h as a copy from STM32Cube firmware package.

We consider it as an enhancement of our solution.

Best regards