Boron hibernate behaviour on USB unplug

I’ve noticed some unusual behaviour when I unplug my Boron from USB while it has a battery connected and while it is in hibernation mode.

Code is here if you want to look:

I have set up a battery charge mode, which shows battery state while charging on an epaper display. When charging is complete, it shows a battery charged image and then goes to hibernate mode. This seems to work properly.

I want to be able to unplug it and have it stay in hibernate mode until I push a power button, and then when I push the button I want it to start from a reset state.

However, when I unplug the USB cable, the Boron resets. Will a Boron always reset on USB connect and disconnect?

I currently have nothing connected to the pin I have assigned as the wake up pin.

There’s a second problem in that this line in my code isn’t working right:

 if (System.powerSource() == POWER_SOURCE_BATTERY)

even when it is on battery power only, and the battery is directly connected to the ground and Li+ pins (no ammeter in series), that line seems to fail. I can’t debug this, because connecting the debugger and USB cable would make this line not do what I want.

The battery I’m using to test is this 18650:

It’s not a battery problem, I tested that cell in a flashlight and it works just fine for several hours.

Any suggestions would be appreciated,

Thanks!

Another problem I noticed:

~21 seconds after it goes into hibernate, it resets. This occurs both on USB and battery power, and with the wakeup pin both unconnected and externally tied to ground.

I’ve solved part of it. When I disable system thread, it stays in hibernate and doesn’t reset every ~20 seconds.

Is there a way to get hibernate to work properly while using system_thread? I can work around not having it, but I have some long delays, and using multithreading allows me to use delay() without losing the network connection (or doing a big awkward workaround with timers and interrupts).

I still don’t know why it resets when USB is unplugged, or why the line

 if (System.powerSource() == POWER_SOURCE_BATTERY)

does not work

I solved the hibernate problem by removing the Cellular.off() call. Now hibernate works properly.

I have done some more investigation, and the power source problem is a bug in DeviceOS.

I still don’t know why my application resets when I unplug USB.