How to know usb connection is lost
On page 258 of the datasheet:


  • 0 – VBUS: VBus flag
  The value read from this bit indicates the state of
  the VBUS pin. This bit can be used in device
  mode to monitor the USB bus connection state of the application. See
  Section 22.10, page 255
  for more details.


On page 255:


  22.10 Plug-in detection
  The USB connection is detected by the VBUS pad, thanks to the following architecture:
    
  The control logic of the VBUS pad outputs a signal regarding the VBUS
  voltage level:
  • The “Session_valid” signal is active high
  when the voltage on the VBUS pad is higher or
  equal to 1.4V. If lower than 1.4V, the signal is not active
  •The “Vbus_valid” signal is active high when the
  voltage on the VBUS pad is higher or equal to 4.4V. If lower than 4.4V, the signal is not active
  • The VBUS status bit is set
  when VBUS is greater than “Vbus_
  valid”. The VBUS status bit is cleared
  when VBUS falls below
  “Session_valid” (hysteresis behavior)
  • The VBUSTI flag is set each time the VBUS bit state changes


Not sure if this Is what your looking for but figured I would post it anyway.