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

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!

Parents Reply
  • Using this implementation I have seen the power consumption of the device drop substantially so the UARTs are being disabled.
    This implimentation surpresses the printk() yes. This means that you won't accidentally trigger a UART0 requiring printk() that will cause a fault. If you re-enable the UART0 before you printk() then it should be fine.

    I designed this implementation as I don't need the power consuming UART-USB bridge and only need the communication between the nRF9160 and an external soruce via UART1. In my test case, the Otii Arc power analyser's UART control pins. I was then able to see the device receive and send UART messages to the anaylser when the UARTs are enabled and see no UART interrupt triggering when the UART1 was disabled.

    I use this code to disable UART0 immedately at boot to stop the serial UART wasting current and then call the code to disable/enable UART1 during runtime depending on sleep or wake states.

Children
Related