nRF5 SDK is not maintained anymore
More Info: Consider nRF Connect SDK for new designs
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

nrf_serial_read never receive data

Hello !

I was trying to communicate with my Acsip chip, when I saw that the problem was coming from the nrf_serial_read() function. And here is my setup :

- I use the example of Nordic Semi : the serial_pca10040

- My SDK is 16.0.0

- I'm codding with Segger embedded studio 5.42 release

*Fact :

- My colleague is using the same setup, code and SDK and everything is working fine for him.

- the nrf_serial_read() always timeout (so the timeout is working)

My problem is that the function always timeout and the buffer is always empty. I have tried several timeout but that does not work. Also, with my setup I see that I write some command and that I receive some other command from my chip, but the nrf_serial_read() don't catch up this data.

Here is the code of the serial_pca10040 and my initializing of nrf_serial_init :

#define OP_QUEUES_SIZE          3
//#define APP_TIMER_PRESCALER     NRF_SERIAL_APP_TIMER_PRESCALER

static void sleep_handler(void)
{
    __WFE();
    __SEV();
    __WFE();
}

NRF_SERIAL_DRV_UART_CONFIG_DEF(m_uart0_drv_config,
                      14, 15,
                      0xFF, 0xFF,
                      NRF_UART_HWFC_ENABLED, NRF_UART_PARITY_EXCLUDED,
                      NRF_UART_BAUDRATE_115200,
                      UART_DEFAULT_CONFIG_IRQ_PRIORITY);

#define SERIAL_FIFO_TX_SIZE 32
#define SERIAL_FIFO_RX_SIZE 32

NRF_SERIAL_QUEUES_DEF(serial_queues, SERIAL_FIFO_TX_SIZE, SERIAL_FIFO_RX_SIZE);


#define SERIAL_BUFF_TX_SIZE 1
#define SERIAL_BUFF_RX_SIZE 1

NRF_SERIAL_BUFFERS_DEF(serial_buffs, SERIAL_BUFF_TX_SIZE, SERIAL_BUFF_RX_SIZE);

NRF_SERIAL_CONFIG_DEF(serial_config, NRF_SERIAL_MODE_IRQ,
                      &serial_queues, &serial_buffs, NULL, sleep_handler);


NRF_SERIAL_UART_DEF(serial_uart, 0);

int main(void)
{
    ret_code_t ret;

   ret = nrf_drv_clock_init();
    APP_ERROR_CHECK(ret);
    //ret = nrf_drv_power_init(NULL);
    //APP_ERROR_CHECK(ret);

    nrf_drv_clock_lfclk_request(NULL);
    ret = app_timer_init();
    APP_ERROR_CHECK(ret);

    // Initialize LEDs and buttons.
    bsp_board_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS);

    ret = nrf_serial_init(&serial_uart, &m_uart0_drv_config, &serial_config);
    APP_ERROR_CHECK(ret);

    static char tx_message[] = "Hello nrf_serial!\n\r";

    ret = nrf_serial_write(&serial_uart,
                           tx_message,
                           strlen(tx_message),
                           NULL,
                           NRF_SERIAL_MAX_TIMEOUT);
    APP_ERROR_CHECK(ret);

    while (true)
    {
        char c;
        ret = nrf_serial_read(&serial_uart, &c, sizeof(c), NULL, 1000);
        if (ret != NRF_SUCCESS)
        {
            continue;
        }
        (void)nrf_serial_write(&serial_uart, &c, sizeof(c), NULL, 0);
        (void)nrf_serial_flush(&serial_uart, 0);

    }
}

Here is the nrf_serial_read function :

ret_code_t nrf_serial_read(nrf_serial_t const * p_serial,
                           void * p_data,
                           size_t size,
                           size_t * p_read,
                           uint32_t timeout_ms)
{
    ret_code_t ret;

    ASSERT(p_serial);
    if (!p_serial->p_ctx->p_config)
    {
        return NRF_ERROR_MODULE_NOT_INITIALIZED;
    }

    if (!(p_serial->p_ctx->flags & NRF_SERIAL_RX_ENABLED_FLAG))
    {
        return NRF_ERROR_INVALID_STATE;
    }

    if (size == 0)
    {
        return NRF_SUCCESS;
    }

    if (!nrf_mtx_trylock(&p_serial->p_ctx->read_lock))
    {
        return NRF_ERROR_BUSY;
    }

    nrf_serial_timeout_ctx_t tout_ctx = {
            .expired = false,
    };

    if (timeout_ms != NRF_SERIAL_MAX_TIMEOUT)
    {
        ret = timeout_setup(p_serial,
                            p_serial->p_rx_timer,
                            timeout_ms,
                            &tout_ctx);

        if (ret != NRF_SUCCESS)
        {
            nrf_mtx_unlock(&p_serial->p_ctx->read_lock);
            return ret;
        }
    }

    size_t left = size;
    uint8_t * p_buff = p_data;
    do
    {
        size_t rcnt = serial_rx(p_serial, p_buff, left);
        left -= rcnt;
        p_buff += rcnt;
        if (!left)
        {
            break;
        }

        if (tout_ctx.expired)
        {
            if (p_serial->p_ctx->p_config->mode != NRF_SERIAL_MODE_POLLING)
            {
                nrf_drv_uart_rx_abort(&p_serial->instance);
            }
            break;
        }

        sleep_handler(p_serial);
    } while (1);

    if (p_read)
    {
        *p_read = size - left;
    }

    if (!tout_ctx.expired && (timeout_ms != NRF_SERIAL_MAX_TIMEOUT))
    {
        (void)app_timer_stop(*p_serial->p_rx_timer);
    }

    nrf_mtx_unlock(&p_serial->p_ctx->read_lock);
    if (left && tout_ctx.expired)
    {
        return NRF_ERROR_TIMEOUT;
    }

    return NRF_SUCCESS;
}

So I was wondering if you would have some idea about the source of the problem ?

** It's my first post on DevZone, so if there is anything missing, just tell me and I'll be glad to add it !

Parents Reply Children
  • Sorry for a late response, I was OoO.

    I do not see any issues with your code. It looks like you have taken the code snippet as a template from one of our examples in the SDK (SDK\examples\peripheral\serial\main.c). It looks like you commented out nrf_drv_power_init. Is there a reason that you commented it out?

    Do you have any logs enabled (check your sdk_config.h file). Can you show if there are any error that are showed on the logs?

  • Hi Susheel ! 
    Thank you for your time, it was really appreciated. Unfortunately, I never found any sort of error and I tried a lot of things. So, the last solution was to wipe my computer and start on a fresh installation of windows, and that's what I've done. Everything is now working ! I am an intern and the setup have been changed multiple times by other intern before me ad maybe they changed some settings in the computer. It's just an idea that I have, but we will never really now what happened ! Again, thank you for the help and have a good day !

Related