This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Setting CDC baud rate as USB HOST

Other Parts Discussed in Thread: EK-TM4C1294XL

I cannot find any examples of using the Tiva processor as a host and setting a CDC channel baud rate

Below is what I think is the correct code, would really like some confirmation if this is correct.

//*****************************************************************************
//
// USB_CDC_GET_LINE_CODING
//
//*****************************************************************************
#define USB_CDC_STOP_BITS_1     0x00
#define USB_CDC_STOP_BITS_1_5   0x01
#define USB_CDC_STOP_BITS_2     0x02

#define USB_CDC_PARITY_NONE     0x00
#define USB_CDC_PARITY_ODD      0x01
#define USB_CDC_PARITY_EVEN     0x02
#define USB_CDC_PARITY_MARK     0x03
#define USB_CDC_PARITY_SPACE    0x04

//*****************************************************************************
//
//! USB_CDC_GET/SET_LINE_CODING request-specific data.
//
//*****************************************************************************
typedef struct
{
    //
    //! The data terminal rate in bits per second.
    //
    uint32_t ui32Rate;

    //
    //! The number of stop bits.  Valid values are USB_CDC_STOP_BITS_1,
    //! USB_CDC_STOP_BITS_1_5 or USB_CDC_STOP_BITS_2
    //
    uint8_t ui8Stop;

    //
    //! The parity setting.  Valid values are USB_CDC_PARITY_NONE,
    //! USB_CDC_PARITY_ODD, USB_CDC_PARITY_EVEN, USB_CDC_PARITY_MARK and
    //! USB_CDC_PARITY_SPACE.
    //
    uint8_t ui8Parity;

    //
    //! The number of data bits per character.  Valid values are 5, 6, 7 and 8
    //! in this implementation.
    //
    uint8_t ui8Databits;
}
PACKED tLineCoding;
static uint16_t USBHVENDSetLineCoding(void *pvInstance, uint16_t interface)
{
    tUSBRequest sSetupPacket;
    tVENDInstance *psInst;
    tLineCoding LineConfig;

    if (USBMDMH_Connected())
    {
    	// Get our instance pointer.
    	psInst = (tVENDInstance *)pvInstance;

    	// This is a Standard Device IN request.
    	sSetupPacket.bmRequestType = USB_RTYPE_DIR_OUT | USB_RTYPE_CLASS | USB_RTYPE_INTERFACE;
    	sSetupPacket.bRequest = 0x20;         // Set Line Coding
    	sSetupPacket.wValue   = 0;    				// Line Coding
    	sSetupPacket.wIndex   = interface;    // Interface number
    	sSetupPacket.wLength  = sizeof(tLineCoding);

    	// Set the config
    	LineConfig.ui32Rate = 115200;
    	LineConfig.ui8Databits = 8;									// 8 databits
    	LineConfig.ui8Parity = USB_CDC_PARITY_NONE;	// No parity
    	LineConfig.ui8Stop = USB_CDC_STOP_BITS_1;		// 1 stop bit

    	// Send the command
    	USBHCDControlTransfer(0, &sSetupPacket, psInst->psDevice, (uint8_t *)&LineConfig, sizeof(tLineCoding),
                          psInst->psDevice->sDeviceDescriptor.bMaxPacketSize0);
    }
    return(0);
}

  • Hello Andrews,

    C:\ti\TivaWare_C_Series-2.1.0.12573\examples\boards\ek-tm4c1294xl\usb_dev_cserial

    This is a CDC example.

    Regards,

    QJ

  • Thanks QJ

    I already have the CDC device example.

    I am implementing a host connected to a CDC device and wish to set the baud rate from the host side.

    Regards Barry.

  • From all my research the above command should be correct to set the interface baud rate, however when I try to send this the interface simply freezes in USBHCDControlTransfer waiting for while(g_sUSBHEP0State.iState != eEP0StateIdle)

    Setting Control line state works fine
    // This is a Standard Device IN request.
    sSetupPacket.bmRequestType = USB_RTYPE_DIR_OUT | USB_RTYPE_CLASS | USB_RTYPE_INTERFACE;
    sSetupPacket.bRequest = 0x22; // Set Control Line State
    sSetupPacket.wValue = ui16State; // control signals
    sSetupPacket.wIndex = interface; // Interface number
    sSetupPacket.wLength = 0; // Always 0

    // Send the command
    BytesSent = USBHCDControlTransfer(0, &sSetupPacket, psInst->psDevice, 0, 0,
    psInst->psDevice->sDeviceDescriptor.bMaxPacketSize0);


    But Setting the baud rate just hangs

    // This is a Standard Device IN request.
    sSetupPacket.bmRequestType = USB_RTYPE_DIR_OUT | USB_RTYPE_CLASS | USB_RTYPE_INTERFACE;
    sSetupPacket.bRequest = 0x20; // Set Line Coding
    sSetupPacket.wValue = 0; // Always 0
    sSetupPacket.wIndex = interface; // Interface number
    sSetupPacket.wLength = sizeof(LineConfig);

    // Set the config
    LineConfig.ui32Rate = 115200;
    LineConfig.ui8Databits = 8; // 8 databits
    LineConfig.ui8Parity = USB_CDC_PARITY_NONE; // No parity
    LineConfig.ui8Stop = USB_CDC_STOP_BITS_1; // 1 stop bit

    // Send the command
    BytesSent = USBHCDControlTransfer(0, &sSetupPacket, psInst->psDevice,
    (uint8_t *)&LineConfig, sizeof(LineConfig),
    psInst->psDevice->sDeviceDescriptor.bMaxPacketSize0);
  • I now have this working correctly.

    A couple of important bits of information in case anyone else tries this.

    1 - The interface number you need to target is the INTERRUPT interface for the comport, not the interface with the BULK(IN/OUT)
    2 - If you target the wrong interface number some modems will simply not respond and USBHCDControlTransfer will hang waiting for a return to idle.
    3 - Most CDC devices seem to initialise their port to working defaults. Some however don't (Telit HE910 for example). In this case you must set both the Line Coding and also the RTS and DTR in order to get the AT port to respond.

    Hopefully this helps anyone else trying to do the configuration of line coding.