Lipo Battery Glitch: No USB, No Battery

For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
CYMotorsport
 
Posts: 121
Joined: Wed Nov 24, 2021 6:17 pm

Re: Lipo Battery Glitch: No USB, No Battery

Post by CYMotorsport »

Understood - let me get that together for you.

I know for now you are just speculating - but if that was indeed the case, it wouldn't be resolved from simply unplugging the lipo and reinserting, correct? Or would that clear the issue? Once I unplug and plug back in, it's like nothing happened and all is well.

User avatar
hathach
 
Posts: 1270
Joined: Tue Apr 23, 2013 1:02 am

Re: Lipo Battery Glitch: No USB, No Battery

Post by hathach »

CYMotorsport wrote:Understood - let me get that together for you.

I know for now you are just speculating - but if that was indeed the case, it wouldn't be resolved from simply unplugging the lipo and reinserting, correct? Or would that clear the issue? Once I unplug and plug back in, it's like nothing happened and all is well.
It hard to say, but still within the possibility of software issue. E.g it is a if condition, and you only reach that with usb attached first etc.. The hardware seems to work well, are you able to view my short clip of testing ? I think we should best to look at the code, try to make it as minimal as possible with the issue, that will help to narrow down the scope and time for analyzing.

User avatar
CYMotorsport
 
Posts: 121
Joined: Wed Nov 24, 2021 6:17 pm

Re: Lipo Battery Glitch: No USB, No Battery

Post by CYMotorsport »

Posting the raw code is going to be impossible as it's done via a proprietary system but I'll contact the code author for proper expertise on the matter as they may want to opine but I'll revert either way.

I did see your short clip - thank you very much for sending.

User avatar
hathach
 
Posts: 1270
Joined: Tue Apr 23, 2013 1:02 am

Re: Lipo Battery Glitch: No USB, No Battery

Post by hathach »

I may not have time to check all of your code. I think
- you should try to narrow it down as much as possible
- paying attention to the usb related code, especially and Serial printf/logging to see if there is any while/wait loop
- Update Adafruit_TinyUSB lib and/or core or other lib to the latest
- may be try a mass comment on every USB code (as if usb is not available) to see if that work with your existing code

Another way is the opposite of above way, trying to incrementally add some of your code to an known good working like stock example until you got to the troublesome code. Either way is time consuming, but it at least help to narrow it down.

User avatar
bidouilleur
 
Posts: 3
Joined: Tue Jul 19, 2022 10:16 am

Re: Lipo Battery Glitch: No USB, No Battery

Post by bidouilleur »

Hi, I'm Etienne, the guy who is providing the code that is behind the problem :D
Thanks for testing this and providing a working proof (and also thanks for the huge work behind TinyUSB)
Actually I'm not using BLE, I'm using Nordic's Enhanced ShockBurst proprietary protocol, which is not available in the Adafruit NRF52 core, so it's going to be difficult to give the code that is behind this without giving all the esb code (that I had to modify and correct to make it work with NRF24 modules).
But I have tested your sample with my modified core and it works when USB is disconnected, so there's definitely something wrong on my side. As the only difference is the radio protocol, I'll search on the esb side.
I'll let you know if I solve the problem and if it comes from esb.

Edit : I've tried with a debugger hooked (with VisualCode/PlatformIO + Same core + Same TinyUSB library), and I can't reproduce the problem (i.e. it's still working when unplugging USB). Uploading in release mode without debugger hooked also works. So I'm going to dig into compiling options.

User avatar
hathach
 
Posts: 1270
Joined: Tue Apr 23, 2013 1:02 am

Re: Lipo Battery Glitch: No USB, No Battery

Post by hathach »

Great to hear that testing help. Unfortunately, I have no idea about ESP protocol, but I could definitely help with USB if you could narrow it down to it.

User avatar
bidouilleur
 
Posts: 3
Joined: Tue Jul 19, 2022 10:16 am

Re: Lipo Battery Glitch: No USB, No Battery

Post by bidouilleur »

Ok, what happens is that when I unplug USB, radio packets are not sent anymore, and when I plug USB again, they're being sent again.

It seems that TinyUSB code touches the clock events when USB is plugged/unplugged because when I add this piece of code before the sending part, it works again :

Code: Select all

	NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
	NRF_CLOCK->TASKS_HFCLKSTART = 1;
	while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
I don't know what this code is doing, I found it in the init of the ESB radio protocol, but maybe you know how it interferes with USB.

Edit : It seems this code starts the high frequency clock.

And I found what is stopping the clock :
in dcd_nrf5x.c, line 1114, the call to hfclk_disable()
Is there any reason why you are disabling this clock when USB is unplugged ? (I guess it's to save power)

User avatar
hathach
 
Posts: 1270
Joined: Tue Apr 23, 2013 1:02 am

Re: Lipo Battery Glitch: No USB, No Battery

Post by hathach »

ah I see, you are spot-on with the finding. USB like radio requires HighFreq clock (HFCLK), It starts the hfclk when usb connection is detected and disable it when removed. Radio also requires hfclk as well, though I know for sure that BLE stack will check and enable hfclk on-demand, therefore it is safe for usb to disable it. Look like ESP does not.

Thinking about this a bit more since hfclk is a shared resource, usb should not try to disable it on its own, though that need a application code with system-wide knowledge to decide whether it should still be on or off. I will spend a bit more time to look at this and make changes to tinyusb later on if needed.

For now, I think you should just comment out the line with hfclk_disable(); and leave the lock on/off to ESP stack. It is a bit of annoying, but I don't use ESP and didn't test with this scenario before :). Watch out for tinyusb update/release in the future for the fix.

User avatar
bidouilleur
 
Posts: 3
Joined: Tue Jul 19, 2022 10:16 am

Re: Lipo Battery Glitch: No USB, No Battery

Post by bidouilleur »

Thanks, I agree that USB should not disable this clock since it can be used by many peripherals (SPI, etc).
I'll comment out the line with hfclk_disable(); , no problem.

Locked
Please be positive and constructive with your questions and comments.

Return to “Wireless: WiFi and Bluetooth”