ncs1.61 peripheral_uart : enable uart when connected and disable uart when disconnected

Hi,

I work with win10 laptop, Toolchain  nrf Connect SDK 1.6.1 on a nrf5340dk.

On peripheral_uart sample, I would like to enable uart when a device is connected and I want to disable uart when device is disconnected to reduce consumption.

There is the consumption mesure by PPK2:

It's seem that between two advertisings the consumption is due to the enable uart.

I need to have the lowest consumption when the nrf5340dk is advertising.

First step, I tryed to disable uart from prj.conf:

CONFIG_SERIAL=n & CONFIG_LOG=n

but after build and download the 4 led were ON and the was no more advertising.

Second step, I tryed to disable uart from the overlay file:

&uart0 {
    status = "disable";

};

but after build and download there was no led on and no more advertising.

There is my project:

5040.peripheral_uart.zip

Could you give me, please,  an example how to enable uart when a device is connected and disable uart when device is disconnected ?

Best regards,

Rob.

  • Additionnal information:

    Hi,

    I found this ticket "UART Enable/Disable with sdk v1.3.0": https://devzone.nordicsemi.com/f/nordic-q-a/63714/uart-enable-disable-with-sdk-v1-3-0

    I tryed to use this code:

    "

    void uart_0_disable(void)
    {
       struct device *uart0_dev = device_get_binding("UART_0");
        device_set_power_state(uart0_dev, DEVICE_PM_OFF_STATE, NULL, NULL);
        
        gpio_pin_configure(gpio_dev, PIN_UART_0_RX, GPIO_OUTPUT);
        gpio_pin_set(gpio_dev, PIN_UART_0_RX, 0);

        gpio_pin_configure(gpio_dev, PIN_UART_0_TX, GPIO_OUTPUT);
        gpio_pin_set(gpio_dev, PIN_UART_0_TX, 0);
    }

    void uart_0_enable(void)
    {
        struct device *uart0_dev = device_get_binding("UART_0");
        device_set_power_state(uart0_dev, DEVICE_PM_ACTIVE_STATE, NULL, NULL);
    }

    "

    I modified the code for sdk 1.6.1 like this:

    "

    #include <pm/state.h>
    #include <pm/device.h>
    #include <pm/pm.h>

    #include <drivers/gpio.h>

    void uart_0_disable(void)

       uart = device_get_binding(DT_LABEL(DT_NODELABEL(uart0)));
       pm_device_state_set(uart, PM_DEVICE_STATE_OFF, NULL, NULL);

       gpio_pin_configure(uart, 22, GPIO_DISCONNECTED);
       gpio_pin_set(uart, 22, 0);

       gpio_pin_configure(uart, 20, GPIO_DISCONNECTED);
       gpio_pin_set(uart, 20, 0);
       
    }

    void uart_0_enable(void)
    {
        uart = device_get_binding(DT_LABEL(DT_NODELABEL(uart0)));
        pm_device_state_set(uart, PM_STATE_ACTIVE, NULL, NULL);
    }

    "

    But I still have this error: 

    /main.c:116: 'undefined reference to `pm_device_state_set'

    collect2.exe: error: ld returned 1 exit status

    Could anyone help me to resolve this error, please ?

    Best Regards,

    Rob.

  • Hello Heine,

    Good Morning.

    I have looked at your code and configuration file. The error you get 'undefined reference to 'pm_device_state_set'. The issues may be not related to the UART. It seems that pm_device_state_set() is only defined if CONFIG_PM is set the prj.conf. You can try this first. Set the CONFIG_PM and see if it defined then.

    Best Regards,

    Kazi Afroza Sultana

  • Good morning Kazi Afroza Sultana,

    Thank you for your answer. 

    Just before your answer, I made progress.

    I discover that the error disappears when I put "CONFIG_SERIAL=n".

    Then I discover the blog : Optimizing Power on nRF5340 SoC Designs

    https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/optimizing-power-on-nrf53-designs

    I tryed to put into prj.conf:

    "CONFIG_PM=y
    CONFIG_PM_DEVICE=y"

    I still have no error but there is no more advertising ...

    What should I do to continu advertising ?

    peripheral_uart_disable_b.zip

    Best Regards,

    Rob.

  • Helo Robert,

    I think you have disable the serial from Application core as well. By disabling CONFIG_SERIAL = n from prj.conf file. You just need to disable SERIAL from Network core ( you have done this in hci_rpmsg.conf). That's why it has lost connection from Application core and you are not getting advertising data. 

    I tested your code with this set up and got the following output from during current measurement. 

     

    Could you please try this and observe what is the output?

    Best Regards

    Kazi Afroza Sultana

  • Good Morning Kazi,

    Thank very much you for your answer and your help.

    I'm not sure to understand your reply, I tryed to coment  "#CONFIG_SERIAL=y" and  "#CONFIG_SERIAL=n" into prj.conf, so I have no config_serial.

    After that advertising restart and I have a wonder consumption during advertising, arround 110uA !

    But I can not use the uart when I'm connected, I tryed to send "azerty" from Rx Characteristic nothing was received by putty, after the third try it make my an error 133 Gatt Error and error 8 Gatt Conn Timeout.

    When I made a reset, the consumtion start to be around 770uA and after connect and disconnect it fall around 117uA.

    It look like the uart do not wake up after connected. I think I forgot something to wake up the uart when I'm connected. And I should maybe put the function uart_0_disable() into uart_init() to start at a low consumption after reset.

    Do you know what I should do to wake up the uart ?

    peripheral_uart_disable_b (2).zip

    Best Regards,

    Rob.

Related