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.

USB CDC example for cc2540 (or other chip with similar usb)

I have a cc2540-based design that is battery powered and includes a usb interface for charging and data downloading.  I would like to use the usb cdc code that TI has provided however there is no example of how to configure the USB and use the cdc code provided.   The only example I found is the HostTestRelease but most of this example is contained in the network processor library which of course is only distributed in binary format. 

Is there any example of how to use the usb cdc or is it possible to have a more complete release of the HostTestApp?

a*

  • The HAL has abstracted the USB-CDC as just another "serial port" and the only thing "hiding" in the library use of it in the HostTestRelease build target "CC2540USB" is the actual calls to the public HAL UART API's such as a HalUARTOpen(), HalUARTRead(), etc.

    Note how when you setup your project like the build target above, you have the 'CC2540USB' HAL target directory and in there, the hal_board_cfg.h defines this:#ifndef HAL_UART_USB
    # if HAL_UART
    #  define HAL_UART_USB 1
    # else
    #  define HAL_UART_USB 0
    # endif
    #endif

    So, with HAL_UART=TRUE, you get HAL_UART_USB set to a non-zero value and with that, all of the USB-CDC support just springs to life. For instance in hal_drivers.c, HalDriverInit(), this will kick in:

    #if (defined HAL_UART) && (HAL_UART == TRUE)
      HalUARTInit();
    #endif

    and when you look in HalUARTInit(), you see that with the above defines it results in this:

    #if HAL_UART_USB
      HalUARTInitUSB();
    #endif

    and so on.

    Pretty cool and easy-to-use abstraction of the USB CDC class, no?

     

  • Thanks for the explanation Dirty Harry! 

    One part of the halUartUSB that seems irregular...  with other halUart* devices the communication speeds and other settings are determined when the port is opened, however with halUartUSB the communication settings are hardcoded in the HalUARTInitUSB.    The settings are fixed and cannot be adjusted without modifying common code. 

    Is this required by USB or is it possible to change the communication settings?  Are other baud rates possible?

  • Yes, you make a completely accurate observation - the "virtual HAL UART port" abstraction is incomplete wrt the CDC port settings ... it has been "left as an exercise at the end of the chapter" :)

    Just cut & paste the details from HalUARTInitUSB():

      currentLineCoding.dteRate = HAL_UART_BAUD_RATE;
      currentLineCoding.charFormat = CDC_CHAR_FORMAT_1_STOP_BIT;
      currentLineCoding.parityType = CDC_PARITY_TYPE_NONE;
      currentLineCoding.dataBits = 8;

    And past them into HalUARTOpenUSB(), where you can use the settings in the parameter to set the corresponding CDC line coding values.

    But remember to also move the pullup enable from HalUARTInitUSB() to after all configuration in HalUARTOpenUSB() so that the device does not enumerate with the Windows host until after all configuration is properly made.

     

  • I just did what you said .

    I want to make a project base on SimpleBLEObserver , It can communicate with PC using USB-cdc , I'm doing like these:

    in "SimpleBLEObserver_Init" add:

    halUARTCfg_t uartCfg;

    uartCfg.callBackFunc = &usbUartCB;

    HalUARTOpen(0,&uartCfg);

    Add a uart call back function:

    void usbUartCB(uint8 port,uint8 event)

    Put defines in project options:

     HAL_UART = TRUE

    HAL_UART_USB

    Then I connect  my device to pc-usb port , it shows a unknow device.

    is anything I did wrong? how to so solve the problem?thank you!