This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to use USB CDC transfer API in zephyr

Hello experts,

I would like to get my mind clear on this.

I am using NRF52833 for our custom board we are using Zephyr RTOS (NCS v1.4.99) and IDE is segger embedded studio.

I am using USBD_CDC driver. I am able to detect the USB power. I want to transfer data via USB. In our board UART provision is not provided. We would like to use USB debug.

I have used this following code. But not able to see the data on screen. What will be the end point Address? My code is below here. Is that right ?

static struct device *dev_usb;

void init_USB(void)
{
      int ret;

      dev_usb = device_get_binding("CDC_ACM_0");
      if (!dev_usb) {
              //LOG_ERR("CDC ACM device not found");
              return;
      }

      ret = usb_enable(NULL);
      if (ret != 0) {
              //LOG_ERR("Failed to enable USB");
              return;
      }
}

  ... 
  {
      usb_write(dev_usb, usb_data_buf, 5, NULL);
      k_sleep(K_MSEC(1000));
  }
  ...
/**
 * @brief Write data to the specified endpoint
 *
 * Function to write data to the specified endpoint. The supplied
 * usb_ep_callback will be called when transmission is done.
 *
 * @param[in]  ep        Endpoint address corresponding to the one listed in the
 *                       device configuration table
 * @param[in]  data      Pointer to data to write
 * @param[in]  data_len  Length of data requested to write. This may be zero for
 *                       a zero length status packet.
 * @param[out] bytes_ret Bytes written to the EP FIFO. This value may be NULL if
 *                       the application expects all bytes to be written
 *
 * @return 0 on success, negative errno code on fail
 */

Regards

Sudharsan

  • Hello,

    It looks correct. I suggest you check out the sample found in:

    NCS\zephyr\samples\subsys\usb\cdc_acm.

    This works as a loopback cdc_acm device. Test it with the USB peripheral on your custom board. Let me know whether it works or not.

    Best regards,

    Edvin

  • Hi Edvin,

    I have used the same example for nrf52833 build. It looks project build has been failed. Please look at the picture, down here.

    I have used the "NCS\zephyr\samples\subsys\usb\cdc_acm" driver in my project. It seems USB power has been detected.

    They had written USB-to-UART transfer right? In our custom board there is no UART pins.

    Earlier days I have developed my project with SDK17.0.2. In that, I was able to transfer via USB without UART config. The same I am expecting here to work with.

    This is my Proj.conf file

    CONFIG_STDOUT_CONSOLE=y
    CONFIG_USB_DEVICE_STACK=y
    CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y
    CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y
    CONFIG_NRFX_USBD=y
    CONFIG_NRFX_POWER=y
    CONFIG_USB_CDC_ACM=y
    CONFIG_USB_DEVICE_PRODUCT="Zephyr CDC ACM sample"
    CONFIG_USB=y

    Please suggest your answers as soon as possible.

    Regards

    Sudharsan

  • sudharsan said:
    They had written USB-to-UART transfer right? In our custom board there is no UART pins.

     That is the name of the project, but I tested it, and it doesn't react to my UART. The UART is only used for logging. I don't know why the sample is called that. This sample is from Zephyr, and not NCS (the Nordic part of Zephyr), so I can't say why they decided to call it what they did.

     

    sudharsan said:
    It looks project build has been failed. Please look at the picture, down here.

     What does the compiler say when it fails to build? I can't tell from your screenshot. At least one of the builds should not have the "build completed" message. What does that one say? Is it missing anything?

  • It looks all the directories built completely.

    This is an example project that I did not changed anything and I built it directly.

    Finally log says the following

    "combining zephyr/isr_tables.c"

    unrecognized option'--output-source'

    ]<value>[<flags>] Add a symbol

    <version>]

    supported targets: elf32-littlearm elf32-littlearm-fdpic elf32-bigarm elf32-bigarm-fdpic elf32-little elf32-big srec symbolsrec verilog tekhex binary ihex plugin

     

    Regards

    Sudharsan

  • could you please select nrf52833 in toolchain manager and build this project???

Related