Details

Based on current measurements charging appears to work properly even when Rockbox is locked up. The following issues are just with the charging screen, so I would consider this low severity. (I can't set severity, I see "CriticalHighMediumLowVery Low" by the listbox instead of in it.)

If I plug in a charger while my V2 Recorder is off I get the charging screen, a brief flash of the Rockbox splash screen, and then the charging screen again. The voltage is <0.1 V but otherwise the screen appears fine, with the battery filling animation at the top and the wave animation in the large battery. After the battery animation at the top fills 5 times, all animation stops and the only thing I can do is hold the OFF button to shut down. At this point I can either disconnect the charger to turn off the device and leave it off, or hold OFF to turn off, after which the process repeats.

If I press ON while the charging animations are still moving I get a brief flash of the splash screen, a brief flash of "ATA error: -11 press ON to debug" and then the I/O ports debug screen. The battery voltage is again <0.1 V. If I attempt to get out of this screen I get a "*PANIC* ata: -11". If I stay at the screen, it locks up. If instead of pressing ON I hold ON, the "ATA error" screen stays and I need to press ON again to get to the I/O ports screen.

The BootBox charging screen (accessed via holding F1 when plugging in the charger) also reports an incorrect battery voltage. Initially it is 0.02 V or so and it soon becomes 0.00 V. However, the screen doesn't seem locked up and if I press ON that leads to a normal rescue boot.

Charging works properly if the charger or USB cable are plugged in after Rockbox is running. Battery voltage measurement also works. Charger detection on the "power status" debug screen ("view battery", press UP) works though the external voltage measurement is questionable because it starts at >6V, higher than the open circuit voltage on the charger, and goes down to 4.3 or so.

I just realized the battery was pretty full now, on the constant voltage topping charge. I will try again when the battery is empty and the battery voltage while charging is lower, in case the lockup happens when Rockbox thinks the battery is full. It shouldn't matter though if Rockbox can't measure the voltage.

Trying it when the battery is more discharged produces the same results.

firmware/export/config-recorderv2.h defines NEED_ATA_POWER_BATT_MEASURE and in apps/screens.c in charging_display_info there is a comment that "FM and V2 can only measure when ATA power is on". Since the measured voltage is near zero and there is an ATA error when trying to turn on I suppose that ATA power is off.

charging_display_info() in apps/screens.c won't read the voltage if NEED_ATA_POWER_BATT_MEASURE is defined and ide_powered() returns false. However, HAVE_ATA_POWER_OFF isn't defined and so in firmware\target\sh\archos\fm_v2\power-fm_v2.c ide_powered() always returns true. Lack of HAVE_ATA_POWER_OFF also makes ide_power_enable(false) do nothing but ide_power_enable(true) does something.

So why does a normal power-on with ON turn on ATA power?. I think this may have to do with the bootloader or maybe how the hardware works. It doesn't seem that anything other than the bootloader will do ide_power_enable(true) before ata_init(). (ata_init() would if ide_powered() returned false.)

When powered on from the charging screen: After Rockbox locks up removing the charger powers off the device. If the charger is removed after Rockbox boots but before it locks up, the device powers off when it would otherwise lock up. The power-off is instant and not an orderly Rockbox shutdown.

Perhaps the hardware supplies battery power for only 20 seconds unless something is done to make it stay on? The lockup might not be due to something the software does at the time but due to a glitch in power or shutdown of certain components as battery power is switched off and only charger power remains. However, power use from the charger remains virtually constant and the hard drive, backlight and LCD remain on with no disruption.

I compared the I/O ports debug screen when Rockbox was started from off and when it was started from the charging screen. I didn't see any differences other than the voltage ADC values.

Searching for 20 multiplied by HZ in the code I found something very interesting: in sys_poweroff() in firmware\powermgmt.c the shutdown timeout is set to HZ*20 if it was zero. When shutdown_timeout expires, this leads to an immediate powerdown. (Earlier I noted that the lockup or powerdown takes very close to 20 seconds.)

It seemed to me the lockup or power-off might be happening because in firmware/powermgmt., handle_auto_poweroff() saw a ridiculously low battery voltage (due to ATA power being off) and called sys_poweroff(). I changed handle_auto_poweroff() to not power off if NEED_ATA_POWER_BATT_MEASURE is defined and the charger is connected. This causes the charging screen to not lock up. However, if I turn on the device from the charging screen a lockup or powerdown happens 20 seconds after I turn it on. (Before it happened 20 seconds after the charger was initially connected.)

I suppose this may happen because when IDE power is turned on the inrush current drops charger voltage sufficiently for Rockbox to believe the charger was disconnected. I don't have any way to support this hypothesis now.

I'm attaching the patch I used. It was originally from a somewhat older version and I had to alter it a bit to work with today's rockbox-bleeding. I do not recommend use of this patch other than for experimentation. Disabling low battery shutdown if a charger is connected might be dangerous. What if the charger can't provide enough power to charge the battery and/or the hard drive is used a lot so the battery is discharging? It could end up deeply discharged and recovery from that state may be difficult because battery power is needed to turn on when charging and deep discharges can damage LiIon batteries. Some things would be easier if ide_powered() actually returned the correct answer. The code seems to be there. Is there any reason why it has to always return true instead?

Note that simply turning on IDE power when the charger is connected is probably a bad idea. The hard drive would try to spin up, and so charging would only be possible if there is enough power to spin up the hard drive.

It might be a good idea to investigate why the queue_broadcast(SYS_POWEROFF, 0) in sys_poweroff() isn't triggering an orderly shutdown. I suppose it's sent before the relevant queue is initialized.

(1) The voltage reading only works if ata is powered on fm/v2 recorders, but in the charging screen ata is disabled, so a reading (close to) 0V is normal

(2) Most archoses can't be powered down while being connected to the charger. If the code for fm/v2 doesn't obey this and tries to shutdown, it will hang. Charging is hardware controlled in the fm/v2, so it's not dangerous. The low-bat shutdown should probably be disabled for fm/v2, at least it needs to be limited to only consider proper readings.

I can't do this myself as I don't have an fm or v2 recorder, but I am about to rework powermanagement somewhat.

I just found another related bug in bleeding. In the charging screen loop, usb_detect() is used as a boolean when in fact it isn't and it always returns non-zero values. This causes the charging screen to exit immediately as if USB was plugged in. It seems that usb_detect() should return USB_INSESRTED when USB is actually connected and that's the value that should be checked for. A short patch is attached.

It seems this bug should affect the charging screen on other platforms as well but I haven't tested that.

For my own use I'm totally satisfied with how I disabled low battery shutdown. However, I still don't think that Rockbox should be distributed that way.

One thing I think I did wrong earlier: It's not enough to just prevent a low battery shutdown from occurring while ATA power is turned off. The battery voltage filter (avgbat in powermgmt.c) goes very low from all those 0V readings. I have to initialize it after ATA power is turned on.

I moved powermgmt_init() to just after where I turn on ATA power after the charging screen loop in main(). The initial battery reading was still near 0V, probably because it was taken too soon after ATA power was turned on. So I added sleep(HZ/4) at the start of power_thread(). I finally got a reasonable battery reading after starting from the charging screen.

I did this on an old version from last November. Sorry about that. I did look at the current source and it seems these ideas still apply. I will integrate these patches into the current version if someone tells me it is okay to delay power management initialization as described on FM and V2 Recorder targets. (There is no need to disable low battery shutdown if this fix is used.)

I just left my V2 recorder on and idle (with idle shutdown disabled) and I found that the battery voltage can go below 2.0V. Because of this I feel like adding the following regarding disabling low battery shutdown:

WARNING: If low battery shutdown is disabled and the device is left on but not playing, it may be possible to overdischarge the battery. According to various sources, overdischarge damages lithium-ion batteries. Also, recovery from that state may require charging the battery externally or jump-starting the device. Under normal use, the device will shut down when the hard drive is used before the battery is overdischarged, and if not playing, idle shutoff will shut it down.

Note that disabling of low battery shutdown is not necessary to fix this bug. If the power management thread is delayed as described in my previous message it won't see false low battery readings due to ATA power being off.

Technical details:
- The ATA error problem exists because when HAVE_ATA_POWER_OFF is not defined, ide_powered() always returns true. This causes ata_init() to not turn on power when necessary.
- Hard drive power for all Archos HWCODEC devices is handled this way. This fix is for all of them, though I only have a V2 Recorder to test.
- I chose to use the ata_is_coldstart() ((PACR2 & 0x4000) != 0) test for determining whether IDE power needs to be turned on because I know it works. I am not sure if the unused ide_powered() code is reliable. I call the function instead of doing the test in main because that seems nicer.
- It seems the same problem exists in BootBox code, so I also call ide_power_enable() there if HAVE_ATA_POWER_OFF is not defined. I haven't fully tested BootBox, and I note it is larger and would go beyond ROM_START. Just use the pre-built BootBox on the Wiki.
- ATA_POWER_PLAYERSTYLE seems useless when HAVE_ATA_POWER_OFF is not defined, and ide_powered() does not rely "on probing result from bootloader". I am not removing it because I don't have a player to test. If the charging screen works properly on players now, it may be removed.
- NEEDS_ATA_POWER_ON apparently existed at some time, but not anymore. I've removed it from the comments in power-*.c for the these devices.
- To avoid reading the battery voltage while ATA power is disabled, I delayed powermgmt_init() on the FM/v2 recorder. (This would be a problem if NEED_ATA_POWER_BATT_MEASURE && CONFIG_CHARGING == CHARGING_CONTROL, but no such targets exist. Should I consider such situations?)
- The charging screen displayed battery voltage conditional on ide_powered(). Since that returns true, it displayed 0V on V2/FM Recorders. I changed it to display battery voltage on targets without NEED_ATA_POWER_BATT_MEASURE and input current on V2/FM recorders.
- At the charging screen the status bar displays inaccurate settings because only NVRAM settings have been loaded at that point. I didn't do anything about this.

Regarding the current measurement:
I chose to use it so there is some indication of charging at the charging screen. The debug screen displays it as external voltage, but it's clearly input current on my V2 recorder. Current going into the battery is less because some of it is used to power the device. When charging from DC IN, divide the "voltage" by 10 to get current in amps. When charging from USB multiply by 5/70 (7V = 0.5A). I use the DC IN scale factor because USB ought to be disconnected at the charging screen. I show just the units (mA) because I don't know what label would be appropriate considering the many languages supported.

The bug I thought might be in BootBox has been confirmed in firmware_v2.bin from flash-recorderv2-v3.zip. If I hold F1 and connect the charger, I enter the BootBox charger screen. Then if I press on, the rescue boot attempt fails with ATA error -11. Fortunately, a rescue boot is possible if the charger is not connected. This is because on V1, V2 and FM recorders, flash/bootloader/bootloader.c turns off IDE power if the charger is connected, and later, neither BootBox nor a full build of Rockbox turn it on.

Edit: Now I'm unable to enter the BootBox charging screen; I go straight to rescue boot and the -11 error. I think that's FS#10091.

Changes in r17955, r20634, and r25593 should have improved the situation. (I listed them in http://www.rockbox.org/tracker/task/7960#comment41235 ) I expect the only issue that remains is low battery shutdown happening due to IDE power being off. Postponing powermgmt_init() on the V2 and FM recorder until after ata_init() should fix this problem. The conditional could be "defined(NEED_ATA_POWER_BATT_MEASURE) && CONFIG_CHARGING <= CHARGING_MONITOR", or just the a test for the particular devices themselves. Would this be an acceptable solution?

Edit: I just confirmed that the problem still exists in r31398 and that delaying power management initialization fixes it. I intend to commit this soon.