There is no good example for UART handler.

I can see that there is one example for UART and it uses application uart module (APP_UART_FIFO_INIT ...). I used this module for printing strings on terminal window, but now i need to communicate with sensor over UART and it just does not work for me. I tried blocking and non blocking mode (by passing handler function).

Here is my code:

void uart_error_handle(app_uart_evt_t * p_event)
{
	switch (p_event->evt_type)
	{
		case APP_UART_DATA_READY:
			APP_ERROR_CHECK(nrf_drv_uart_rx((uint8_t *)rx_data, LENGHT_RX));
		break;
		
		case APP_UART_TX_EMPTY:
		break;
		
		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;
	}
}

void uart_config(void)
{   
	const app_uart_comm_params_t comm_params =
  {
      UART_RX,
      UART_TX,
      RTS_PIN_NUMBER,
      CTS_PIN_NUMBER,
      APP_UART_FLOW_CONTROL_DISABLED,
      false,
      UART_BAUDRATE_BAUDRATE_Baud921600
    };

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

  APP_ERROR_CHECK(err_code);
    
  enable_uart_peripheral();
}

//I am calling tx function in my sensor read function in non blocking mode:
APP_ERROR_CHECK(nrf_drv_uart_tx(tx_data, LENGHT_TX));

Configuration with UART driver (NRF_DRV_UART_INIT ...) works, but it is much harder to write handler function over UART driver, and there is no good example.

Here is my code:

void uart_config(void)
{       
    nrf_drv_uart_config_t config;
    config.pselrxd = UART_RX;
    config.pseltxd = UART_TX;
    config.pselrts = RTS_PIN_NUMBER;
    config.pselcts = CTS_PIN_NUMBER;
    config.baudrate = NRF_UART_BAUDRATE_921600;
    config.hwfc = NRF_UART_HWFC_DISABLED;
    config.parity = NRF_UART_PARITY_EXCLUDED;
    config.interrupt_priority = APP_IRQ_PRIORITY_LOWEST;
    config.p_context = NULL;
    
    APP_ERROR_CHECK(nrf_drv_uart_init(&config, NULL));
}

//I am calling tx and rx functions in my sensor read function in blocking mode:
APP_ERROR_CHECK(nrf_drv_uart_tx(tx_data, LENGHT_TX));
APP_ERROR_CHECK(nrf_drv_uart_rx((uint8_t *)rx_data, LENGHT_RX));

I would like if sobebody can point me in the good direction. Example code would help.

Sorry for my bad english.

  • You may want to enable h/w flow control with this high baudrate?

  • I too was surprised with that high baud rate. But that is what manufacturer was send to me. Sensor is still in prototype phase and i do not have much information about sensor. I do know that sensor does not have HW control. But if it is unusuall to have that high baud whit no HW control i can ask for more informations.

  • Ok. I owe you all an apology. Handler function for NRF_DRV_UART_INIT is not so difficult to write, it is similar to handler function for APP_UART_FIFO_INIT. With this code it simes to work fine:

    void uart_co2_handler(nrf_drv_uart_event_t * p_event, void * p_context)
    {
        switch(p_event->type)
        {
            case NRF_DRV_UART_EVT_RX_DONE:
            //Here i get the sensor data and do some short modification whit it
            break;
    		
    	case NRF_DRV_UART_EVT_TX_DONE:
                APP_ERROR_CHECK(nrf_drv_uart_rx((uint8_t *)rx_data, LENGHT_RX));
    	break;
    		
    	case NRF_DRV_UART_EVT_ERROR:  	
    	    APP_ERROR_HANDLER(p_event->data.error.error_mask);	
    	break;
    		
    	default:
    	break;
        }
    }
    

    There is still mystery why APP_UART_FIFO_INIT function does not work. If somebody have any experience with this, please do comment.