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

How to properly switch between UART pins and baud rates with nRF52 (UARTE)

Hey.

I have a GPS chip which uses 57600 baud for its rx pin, and 115200 baud for its TX pin. I need to switch between the two baud rates, and also some times connect to a third RX pin.

I am using the uart driver (nrf_drv_uart), with UARTE and DMA enabled (both in the driver config struct as well in sdk_config.h).

The infocenter page about the UART driver mentions multi-instance support, but it is still unclear (to me) how to handle multiple instances. Should they be all be used at the same time, and the driver will do the switching of bauds and pins automagically? Must i uninitialize and initialize them manually when switching?

My current code is using one instance, but using nrf_drv_uart_uninit and nrf_drv_uart_init with different configs to change the pins and baud rates. I have a feeling that this is not the way to go. If it is the correct way, should i add some delays between unitializing and re-initializing?

Here is a snippet showing how i currently try to switch between the two configs.

//global declaration of instance
nrf_drv_uart_t dma_uart_driver_instance = NRF_DRV_UART_INSTANCE(UART0_INSTANCE_INDEX);

ret_code_t err_code = NRF_SUCCESS;

// unitialize previous config
nrf_drv_uart_uninit(&dma_uart_driver_instance);

// initialize new config
nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG;
config.use_easy_dma = true;
config.baudrate = UART_BAUDRATE_BAUDRATE_Baud57600;
config.hwfc = false;
config.pselrxd = PIN_GPS_TX1;
config.pseltxd = PIN_GPS_RX2;
config.use_easy_dma = true;
config.p_context = &dma_uart_driver_instance;

err_code = nrf_drv_uart_init(&dma_uart_driver_instance,&config, dma_uart_event_handler);
APP_ERROR_CHECK(err_code);
Related