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

nRF52840: splicing UART project with usbd_cdc_acm - how to set up the pins?

Hi,

usbd_cdc_acm project implemented UART over usb, which is great. However it cannot send bytes from PC to DK board, only DK board back to PC.

In order to implement sending bytes from PC to DK board, I looked into another project "uart"

If I spliced the following code into usbd_cdc_acm project, would i be able to do double direction communication? Like press 'Q' to exit.

int main(void)
{
    uint32_t err_code;

    bsp_board_init(BSP_INIT_LEDS);

    const app_uart_comm_params_t comm_params =
      {
          RX_PIN_NUMBER,
          TX_PIN_NUMBER,
          RTS_PIN_NUMBER,
          CTS_PIN_NUMBER,
          UART_HWFC,
          false,
#if defined (UART_PRESENT)
          NRF_UART_BAUDRATE_115200
#else
          NRF_UARTE_BAUDRATE_115200
#endif
      };

    APP_UART_FIFO_INIT(&comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_error_handle,
                         APP_IRQ_PRIORITY_LOWEST,
                         err_code);

    APP_ERROR_CHECK(err_code);

#ifndef ENABLE_LOOPBACK_TEST
    printf("\r\nUART example started.\r\n");

    while (true)
    {
        uint8_t cr;
        while (app_uart_get(&cr) != NRF_SUCCESS);
        while (app_uart_put(cr) != NRF_SUCCESS);

        if (cr == 'q' || cr == 'Q')
        {
            printf(" \r\nExit!\r\n");

            while (true)
            {
                // Do nothing.
            }
        }
    }
}

if yes, what should I set RX_PIN_NUMBER to?

  • Hello,

     

    If I spliced the following code into usbd_cdc_acm project, would i be able to do double direction communication? Like press 'Q' to exit.

     I don't know, did it work?

     

    if yes, what should I set RX_PIN_NUMBER to?

     depends on what pin you want to use as the RX pin.

    I am a bit confused here. You mention usbd_cdc_asm and UART. Do you intend to use both? Or do you really want to use only one of them?

    Are you aware that the snippet that you pasted is configuring the UART and not the USB peripheral?

    Are you running all this on a Development Kit (DK)? I suspect that you actually are trying to transfer data via UART over the USB cable that is used for the programming, and not the USB contact that is on the long side of the DK. Is that correct?

  • Hi thanks for responding. 

    "I am a bit confused here. You mention usbd_cdc_asm and UART. Do you intend to use both? Or do you really want to use only one of them?"

    I was trying to merge both in order to get two way communication between pc and dk (I though usbd_cdc_acm does uart over usb but not two way communication). Then I realized that's just dumb. Because usbd_cdc_acm project has two way communication functions already. They are

    app_usbd_cdc_acm_write

    app_usbd_cdc_acm_read

    "I suspect that you actually are trying to transfer data via UART over the USB cable that is used for the programming, and not the USB contact that is on the long side of the DK. Is that correct?"

    That's correct! I got the transfer data via UART over the USB cable that is used for the programming working by using usbd_cdc_asm project. And it does contain write and read function for 2 way communication as mentioned above.

    Thanks for your reply! Hope this post is helpful for whoever needs help on this.

  • cpeng said:
    (I though usbd_cdc_acm does uart over usb but not two way communication)

     I see. It doesn't.

    All the examples that uses UART can be monitored over the USB cable connected to the short end of the DK when you use the default UART pins on the DK. The reason for this is that the UART is translated to USB in the programming chip (the Segger chip on the DK). You can use this for two way communication. Just check the ble_app_uart example. It can both read and write to the UART, which can be monitored via the USB connected to the Segger Chip (the short end).

    The usbd_cdc_acm use the other USB port on the long end of the nRF52840 DK. This is a pure USB peripheral. This USB peripheral, which is used in the app_usbd_cdc_acm_write/read can't be assigned to other pins than this USB port. That is because these pins are hardcoded in the nRF52840 chip. 

     I hope that your doubts are a bit clearer now Slight smile And yes, hopefully this can also be useful for other users.

    Best regards,

    Edvin

Related