How to disable USB CDC-ACM during running

Hello,

I am currently developing a program that uses USB CDC-ACM for USB serial communication.

The USB serial communication is only used when the device is powered via USB, and it should be disabled when powered by battery (i.e., when nothing is connected to the USB connector). I want to achieve this power-saving behavior by disabling USB CDC-ACM during runtime, rather than excluding it from the build configuration (similar to CONFIG_SERIAL=n).

Here’s what I’ve tried so far:

1.I defined the USB CDC-ACM device using
 const struct device *uartdev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart);


2.I attempted to turn off the power for uartdev using
int ret = pm_device_action_run(uartdev, PM_DEVICE_ACTION_TURN_OFF);

Results:

    The return value of pm_device_action_run was -88 (Function not implemented), indicating failure. The uartdev->pm_device is NULL, so this result is correct. However, why is uartdev->pm_device NULL?

    How can I ensure that uartdev->pm_device includes the appropriate structure at build time?

    If there are any other methods to disable the power of usb_cdc_acm during program execution rather than at build time, please let me know.

    Additional information:

    • Nordic Connect SDK (NCS) version: 2.4.2
    • SoC: nRF52833

    Best regards,

    Related