Detecting USB Power on VBUS pin

I am using a nrf52833 with NCS and was wondering if there are events I can subscribe to (or polling if events don't exist) that will notify me when USB power is connected/plugged in ? This would have to detect the USB power supplied to the device (on the VBUS pin). I can detect when a device is connected using CDC to the nrf52833, but this doesn't solve the issue when a power supply is powering the device (no CDC communications).

Thanks in advance.

  • Hi

    Yes, there are two VBUS detection events you can use. We have the USBDETECTED event and USBREMOVED event that gets reported when a rising or decreasing voltage on VBUS is detected. When n VBUS rises into its valid range, the software is notified through a USBDETECTED event. A USBREMOVED event is sent when VBUS goes below its valid range. Use these events to implement the USBD start-up sequence described in the USBD chapter of the nRF52833 product specifigation.

    Best regards,

    Simon

  • How do you subscribe to these two events ?

  • Hi

    To subscribe to the USBDETECTED event you need to enable the interrupt for it. From the HW level registers you could do something like the following:

    • Enable the USBDETECTED interrupt using the INTENSET register:
      Set the USBDETECTED bit in the INTENSET register of the POWER peripheral. This can be done by writing a 1 to bit 4 of the INTENSET register.
      NRF_POWER->INTENSET = POWER_INTENSET_USBDETECTED_Msk;
    • Implement an interrupt handler for the POWER peripheral events.
    • In your interrupt handler, check if the USBDETECTED event has occurred:
      if (NRF_POWER->EVENTS_USBDETECTED)
      {
      // Handle the USBDETECTED event
      NRF_POWER->EVENTS_USBDETECTED = 0; // Clear the event
      }
    • Remember to enable the POWER peripheral interrupt in the NVIC (Nested Vectored Interrupt Controller).

    Also, please check out the pull request here that aims to implements this functionality directly in Zephyr. https://github.com/zephyrproject-rtos/zephyr/issues/51034 

    Best regards,

    Simon

Related