How can I enable and disable UART devices at run time on nRF9160?

I need to enable UART1 in runtime in order to be able to read some data coming on that device.

In order to be able to use the uart functionality, I need to set CONFIG_SERIAL=y, otherwise I get compilation errors. So I wonder how can I disable all uart devices and then enable them back on runtime.

I read this thread, but when I tried to implement it for both UART0 and UART1, the application hang.


This how I try to do it:

void disable_uart()
{
    NRF_UARTE1_NS->TASKS_STOPRX=1;
    while(NRF_UARTE1_NS->EVENTS_RXTO == 0);
    NRF_UARTE1_NS->EVENTS_RXTO = 0;

    NRF_UARTE1_NS->TASKS_STOPTX = 1;
    while(NRF_UARTE1_NS->EVENTS_TXSTOPPED == 0);
    NRF_UARTE1_NS->EVENTS_TXSTOPPED = 0;

    NRF_UARTE1_NS->ENABLE = 0;
    NRF_UARTE1_NS->PSEL.TXD = 0xFFFFFFFF;
    NRF_P0_NS->OUTCLR = (1 << 1);



    NRF_UARTE0_NS->TASKS_STOPRX=1;
    while(NRF_UARTE0_NS->EVENTS_RXTO == 0);
    NRF_UARTE0_NS->EVENTS_RXTO = 0;

    NRF_UARTE0_NS->TASKS_STOPTX = 1;
    while(NRF_UARTE0_NS->EVENTS_TXSTOPPED == 0);
    NRF_UARTE0_NS->EVENTS_TXSTOPPED = 0;

    NRF_UARTE0_NS->ENABLE = 0;
    NRF_UARTE0_NS->PSEL.TXD = 0xFFFFFFFF;
    NRF_P0_NS->OUTCLR = (1 << 29);
}

void enable_uart()
{

    NRF_UARTE1_NS->ENABLE = 8;
    NRF_UARTE1_NS->TASKS_STARTRX = 1;
    NRF_UARTE1_NS->TASKS_STARTTX = 1;

    NRF_UARTE0_NS->ENABLE = 8;
    NRF_UARTE0_NS->TASKS_STARTRX = 1;
    NRF_UARTE0_NS->TASKS_STARTTX = 1;
}


So what am I missing?

Thank you very much for your assistance!

Related