Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

The function that could get the data from the computer in nRF5 SDK at any time

Hi,

I use the modified version of the USBD example(nRF5_SDK_17.1.0_ddde560\examples\peripheral\usbd) in nRF5 SDK from here. (My development kit is nRF52840DK.)

And I would like to add some funtions like the peripheral_uart sample(ncs\v1.7.0\nrf\samples\bluetooth\peripheral_uart) in NCS shown in the picture below.

I have to write the data from the keyboard to the development kit through the endpoint, so I need a similar function to use in nRF5 SDK like k_fifo_get and ble_write_thread that could let the development kit monitor whether there is data from the computer at any time.

Is there any funtion I could use in nRF5 SDK? Or is there any example I could refer to?

With kind regards

Ning

  • Hi,

    Could you verify what functions you would like to add to your nRF5 project? Is it the thread functionality that handles data transmission over Bluetooth with NUS, or is it the k_fifo-functionality? If it is the latter, then I would recommend you take a look at the fifo library in nRF5 documentation for more info! 

    Let me know if this solves your issue!

    Kind regarrds,
    Andreas

  • Sorry for the late reply...

    Both of the functions I would like to add to my project.

    According to your suggestions, I add the program below to my main function.

    int main(void)
    {
        ...
        // Create a FIFO structure
        app_fifo_t my_fifo;
        // Create a buffer for the FIFO
        uint16_t buffer_size = 8;
        uint8_t buffer[buffer_size];
        // Initialize FIFO structure
        uint32_t err_code = app_fifo_init(&my_fifo, buffer, (uint16_t)sizeof(buffer));
        if(!app_fifo_get(&my_fifo, buffer))
        {
            NRF_LOG_INFO("fifoget %d = NRF_SUCCESS", buffer[0]);
        }
        ...
    }

    Then I test it on RTT Viewer to confirm whether the board receive the data that I send.

    I select the "USB" option, and then click "OK" as the picture below.

    However, it only shows the warning messages but not the success messages.

    It seems like the board didn't get any data from the PC.

    How could I solve this problem? Or...Should I use another function?

    And I also need a thread functionality. Thanks!!

    Kind regards,

    Ning

  • Hi Ning,

    I am a bit confused as I see both nRF5 SDK and nRF Connect SDK code referred here. What do you want to use? And is it so that the question is how you can use a buffer/queue/FIFO? Please elaborate.

    Also, I see you added screenshots of RTT logger, but not of what else you do nor what you would expect to be logged.

    In short, please try to describe in more detail:

    • What do you want to accomplish?
    • How have you done it?
    • In which way does it not work?
    • What have you found while debugging?
  • Hi,

    I use nRF5 SDK. And the example that I use is from here.

    I found that the use of the FIFO library in nRF5 is for UART data transfer, but what I need is

    • a function that can handle data transfer between the computer and the board through the USB endpoint

    or

    • any action or function that could call the NRF_DRV_USBD_EVT_EPTRANSFER event, so that the receive_custom_data() function can work

    static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event)
    {
        switch (p_event->type)
        {
        ...
        case NRF_DRV_USBD_EVT_EPTRANSFER:
            if (NRF_DRV_USBD_EPIN2 == p_event->data.eptransfer.ep)
            {
                NRF_LOG_INFO("EP2IN transfer complete");
            }
            else if (NRF_DRV_USBD_EPOUT2 == p_event->data.eptransfer.ep)
            {
                NRF_LOG_INFO("EP2OUT transfer complete");
                receive_custom_data();  //here
            }
            ...
            break;
        ...
    }

    Another problem I have is that the RTT Viewer seems to only be able to transfer data through the JLINK CDC UART Port and cannot transfer data through the nRF5 USB port.

    Is my understanding correct?

    If I understand it correctly, is there any other tool that can transfer data through the nRF5 USB port?

    If I understand it wrong, how should I use it correctly?

    Kind regards,

    Ning

  • 425Ning said:

    I found that the use of the FIFO library in nRF5 is for UART data transfer, but what I need is

    • a function that can handle data transfer between the computer and the board through the USB endpoint

    or

    • any action or function that could call the NRF_DRV_USBD_EVT_EPTRANSFER event, so that the receive_custom_data() function can work

    I see, so the question here is about buffering data that is to be sent via USB, and/or received via USB? In that case, you can use same as you did with UART. For instance, for outgoing data you have:

    <data source> --> <FIFO buffer> --> <Transport out (e.g. UART or USB)>

    The buffering part here is independent.

    Referring to your code snippet from the example I see you have a receive_custom_data() function where you added a comment "//here". Does that mean that it is at this point you wan to buffer incoming data for instance? If so, you can use the FIFO library. The logic around it to do whatever you want to do with the data is up to you.

    425Ning said:
    Another problem I have is that the RTT Viewer seems to only be able to transfer data through the JLINK CDC UART Port and cannot transfer data through the nRF5 USB port.

    RTT works by storing data in RAM, and the Segger debugger reading it out. It is not possible to use RTT via the USB port.

    425Ning said:
    If I understand it correctly, is there any other tool that can transfer data through the nRF5 USB port?

    If you want to do logging via USB you need something else, for instance CDC-ACM, as demonstrated by the USB CDC ACM Example.

Related