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

UART Interrupt mode undefined behaviour

Hi!

I'm running UART example and modified to run upon interrupt and its working fine, but in main() if condition (if (cr == 'q' || cr == 'Q') ) is not executing even condition is satisfied and it is printing q/Q upon sending through serially but not exiting. Can anyone tell me where i went wrong.

Regards,

Vijay Rakesh

static uint8_t data;

void uart_event_handle(app_uart_evt_t * p_event)
{
    uint8_t cr;
    if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_communication);
    }
    else if (p_event->evt_type == APP_UART_FIFO_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_code);
    }
    else if (p_event->evt_type == APP_UART_DATA_READY )
    {
         app_uart_get(&cr);
         data=cr;
         app_uart_put(cr); 
    }
}

int main(void)
{
    uint32_t err_code,i;

    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,
          NRF_UART_BAUDRATE_115200
      };

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

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

    while (true)
    {
      if (data == 'q' || data == 'Q')
       {
          printf(" \r\nExit!\r\n");
          while (true)
           {
            
           }
        }
    }

  • Hey!

    It should work if you change the definition of data from static to volatile as the value of data can be changed at any given time. Though be aware that the interrupt only handles inputs character by character so that data's resulting value after writing for example 'q' is '\n' (Newline). As the last character of the input is '\n' (Newline).

    I still got it to work with 'q' and 'Q', but there could be limitations.

    - Carl Richard

Related