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

Serial port interrupt

I have a serial port set upp with the serial port library like this:

#define UART_PIN_DISCONNECTED           0xFFFFFFFF /**< Value indicating that no pin is connected to this UART register. */
#define RX_PIN_NUMBER                   7
#define TX_PIN_NUMBER                   8
#define CTS_PIN_NUMBER                  UART_PIN_DISCONNECTED
#define RTS_PIN_NUMBER                  UART_PIN_DISCONNECTED
NRF_SERIAL_DRV_UART_CONFIG_DEF(m_uart0_drv_config,
                      RX_PIN_NUMBER, TX_PIN_NUMBER,
                      RTS_PIN_NUMBER, CTS_PIN_NUMBER,
                      NRF_UART_HWFC_DISABLED, NRF_UART_PARITY_EXCLUDED,
                      NRF_UART_BAUDRATE_9600,
                      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_UART_DEF(serial_uart, 0);
NRF_SERIAL_CONFIG_DEF(serial_config, NRF_SERIAL_MODE_IRQ,
                      &serial_queues, &serial_buffs, on_serial_evt, sleep_handler);

and in main:

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

In on_serial_evt i have the following code:

case NRF_SERIAL_EVENT_RX_DATA:
        err_code = app_sched_event_put(NULL, NULL, on_serial_evt_handler);
        APP_ERROR_CHECK(err_code);
        NRF_LOG_INFO("NRF_SERIAL_EVENT_RX_DATA\r\n");

And finally in on_serial_evt_handler:

static void on_serial_evt_handler()
{
    NRF_LOG_INFO("handler\r\n")
    ret_code_t err_code;
    uint8_t c[26];
    memset(c, 0, sizeof(c));
    size_t p_read;
    err_code = nrf_serial_read(&serial_uart, c, sizeof(c), &p_read, 3000);
    NRF_LOG_INFO("p_read:%d\r\n", p_read);
    NRF_LOG_INFO("c[0]:%c\r\n", c[0]);
    NRF_LOG_INFO("error%d\r\n", err_code);

When I start the app everything works as expected with nothing happening. Then I send 1 byte of data to the mcu and the NRF_SERIAL_EVENT_RX_DATA event gets triggered. I can then read the data as expected. Now the problem is that the nrf_serial_read seems to trigger another NRF_SERIAL_EVENT_RX_DATA event. This results in a infinite loop of 0's recieved and the . No matter if i set the timeout of nrf_serial_read to 0 it still always returns NRF_ERROR_TIMEOUT.

I understand the need for triggering NRF_SERIAL_EVENT_RX_DATA if i use none blocking mode but then how can i tell the difference between "new data in buffer" and "data fetched by nrf_serial_read ready"?

I use a nRF52832_xxaa with sdk 13 and S132 4.0.2.

Thankful for any pointers!

Parents
  • FormerMember
    0 FormerMember

    In NRF_SERIAL_EVENT_RX_DATA, the data is already received, could you pass the received data to on_serial_evt_handler() so that you don't need read the received data over again?

  • Okej so I managed to make it work by just accessing the data. However since the only things being passed to the handler is an nrf_serial_s and an nrf_serial_event_t i have to access the buffer by:

    uint8_t * buf = p_serial->p_ctx->p_config->p_buffers->p_rxb;
    

    which seems really weird. And if this really is the way it is supposed to be done, then maybe the examples needs to be updated. And why don't the documentation mention what events the functions generate or at least warn that if you have an serial defined with NRF_SERIAL_MODE_IRQ you can not use the read function?

Reply
  • Okej so I managed to make it work by just accessing the data. However since the only things being passed to the handler is an nrf_serial_s and an nrf_serial_event_t i have to access the buffer by:

    uint8_t * buf = p_serial->p_ctx->p_config->p_buffers->p_rxb;
    

    which seems really weird. And if this really is the way it is supposed to be done, then maybe the examples needs to be updated. And why don't the documentation mention what events the functions generate or at least warn that if you have an serial defined with NRF_SERIAL_MODE_IRQ you can not use the read function?

Children
No Data
Related