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

How to read data from UART RX interrupt? (SDK 17.0.2)

Hi

I want to receive data through UART RX interrupt.

--> An interruption occurs to notify me when one character(byte) is received, and i store it to my memory buffer

The example project I used is "examples/peripheral/uart/main.c"

But this example seems to have only polling mode way.

How can I use the interrupt method to receive data one character by character?

Can you give me some example? 

thanks!!

uart initi:

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);

while loop

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("tick1 = %d\r\n", nrf_drv_rtc_counter_get(&rtc));

   }
}

Parents
  • Applay as this

    static void uart_init(void)
    {
    uint32_t err_code;

    const app_uart_comm_params_t comm_params =
    {
    .rx_pin_no = RX_PIN_NUMBER,
    .tx_pin_no = TX_PIN_NUMBER,
    //.rts_pin_no = RTS_PIN_NUMBER,
    //.cts_pin_no = CTS_PIN_NUMBER,
    .flow_control = APP_UART_FLOW_CONTROL_DISABLED, //APP_UART_FLOW_CONTROL_ENABLED,
    .use_parity = false,
    .baud_rate = UART_BAUDRATE_BAUDRATE_Baud115200
    };

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

    APP_ERROR_CHECK(err_code);
    }

    void uart_event_handle(app_uart_evt_t * p_event)
    {
    static uint8_t data_array[128];
    static uint8_t index = 0;

    switch (p_event->evt_type)
    {
    /**@snippet [Handling data from UART] */
    case APP_UART_DATA_READY:
    //UNUSED_VARIABLE(app_uart_get(&data_array[index]));
    app_uart_get(&data_array[index]);
    index++;

    if ((data_array[index - 1] == '\n') || (index >=127))
    {
    data_array[index]=0;
    index = 0;
    memcpy(uart_rx_buff,data_array,sizeof(data_array));
    }
    break;
    /**@snippet [Handling data from UART] */
    case APP_UART_COMMUNICATION_ERROR:
    APP_ERROR_HANDLER(p_event->data.error_communication);
    break;

    case APP_UART_FIFO_ERROR:
    APP_ERROR_HANDLER(p_event->data.error_code);
    break;

    default:
    break;
    }
    }

Reply
  • Applay as this

    static void uart_init(void)
    {
    uint32_t err_code;

    const app_uart_comm_params_t comm_params =
    {
    .rx_pin_no = RX_PIN_NUMBER,
    .tx_pin_no = TX_PIN_NUMBER,
    //.rts_pin_no = RTS_PIN_NUMBER,
    //.cts_pin_no = CTS_PIN_NUMBER,
    .flow_control = APP_UART_FLOW_CONTROL_DISABLED, //APP_UART_FLOW_CONTROL_ENABLED,
    .use_parity = false,
    .baud_rate = UART_BAUDRATE_BAUDRATE_Baud115200
    };

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

    APP_ERROR_CHECK(err_code);
    }

    void uart_event_handle(app_uart_evt_t * p_event)
    {
    static uint8_t data_array[128];
    static uint8_t index = 0;

    switch (p_event->evt_type)
    {
    /**@snippet [Handling data from UART] */
    case APP_UART_DATA_READY:
    //UNUSED_VARIABLE(app_uart_get(&data_array[index]));
    app_uart_get(&data_array[index]);
    index++;

    if ((data_array[index - 1] == '\n') || (index >=127))
    {
    data_array[index]=0;
    index = 0;
    memcpy(uart_rx_buff,data_array,sizeof(data_array));
    }
    break;
    /**@snippet [Handling data from UART] */
    case APP_UART_COMMUNICATION_ERROR:
    APP_ERROR_HANDLER(p_event->data.error_communication);
    break;

    case APP_UART_FIFO_ERROR:
    APP_ERROR_HANDLER(p_event->data.error_code);
    break;

    default:
    break;
    }
    }

Children
Related