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);
Parents
  • Hi Anders,

    I am assuming that you're using the nRF52832, the multi-instance support in the UART driver is a nRF52840 specific feature since it has two UARTs. This is stated in the SDK v12.2.0 Release Notes, found here.

    So, if you need to change the configuration of your UART instance, you must un-initialize and then re-initialize the instance with the new configuration like you stated in your question. As far as I know the nrf_drv_uart_init and nrf_drv_uart_uninit calls are synchronous, so as soon as they have returned NRF_SUCCESS you should be able to TX/RX with the new configuration.

    Best regards

    Bjørn

  • thank you @Bjørn Spockeli , i have one more question..i kept Rx buffer size 256 and i am receiving continuous data on UART . what if my data exceeds 256 bytes will it roll over automatically? how can i disable reception when desired string is received?

Reply Children
No Data
Related