Index: firmware/export/config-c200.h =================================================================== --- firmware/export/config-c200.h (revision 17614) +++ firmware/export/config-c200.h (working copy) @@ -123,8 +123,8 @@ #define BATTERY_CAPACITY_INC 0 /* capacity increment */ #define BATTERY_TYPES_COUNT 1 /* only one type */ -/* Hardware controlled charging? FIXME */ -#define CONFIG_CHARGING CHARGING_SIMPLE +/* Hardware controlled charging with monitoring of charge status */ +#define CONFIG_CHARGING CHARGING_MONITOR /* define this if the unit can be powered or charged via USB */ #define HAVE_USB_POWER Index: firmware/export/config-e200.h =================================================================== --- firmware/export/config-e200.h (revision 17614) +++ firmware/export/config-e200.h (working copy) @@ -120,8 +120,8 @@ #define BATTERY_CAPACITY_INC 0 /* capacity increment */ #define BATTERY_TYPES_COUNT 1 /* only one type */ -/* Hardware controlled charging? FIXME */ -#define CONFIG_CHARGING CHARGING_SIMPLE +/* Hardware controlled charging with monitoring of charge status */ +#define CONFIG_CHARGING CHARGING_MONITOR /* define this if the unit can be powered or charged via USB */ #define HAVE_USB_POWER Index: firmware/target/arm/sandisk/adc-target.h =================================================================== --- firmware/target/arm/sandisk/adc-target.h (revision 17614) +++ firmware/target/arm/sandisk/adc-target.h (working copy) @@ -38,6 +38,8 @@ #define ADC_I_MICSUP2 11 /* Current of MicSup2 for remote control detection */ #define ADC_VBAT 12 /* Single cell battery voltage */ -#define ADC_UNREG_POWER ADC_BVDD /* For compatibility */ +/* ADC_RTCSUP seems to represent battery voltage better than ADC_BVDD during + charging (ADC_BVDD is way too high) and appears the same in normal use. */ +#define ADC_UNREG_POWER ADC_RTCSUP /* For compatibility */ #endif Index: firmware/target/arm/sandisk/power-c200_e200.c =================================================================== --- firmware/target/arm/sandisk/power-c200_e200.c (revision 17614) +++ firmware/target/arm/sandisk/power-c200_e200.c (working copy) @@ -25,8 +25,36 @@ #include "as3514.h" #include "power.h" +/* mA, 50 - 400 in steps of 50 */ +#define CHARGE_CURRENT (BATTERY_CAPACITY_DEFAULT/3) +#if (CHARGE_CURRENT < 50) || (CHARGE_CURRENT > 400) +#error "Charging current out of range" +#endif + +/* mV, 3900 - 4250 in steps of 50 */ +#define CHARGE_VOLTAGE 4100 +#if (CHARGE_VOLTAGE < 3900) || (CHARGE_VOLTAGE > 4200) +#error "Charging voltage out of range" +#endif + +/* charger status bits in AS3514_IRQ_ENRD0 */ +#define CHG_tmphigh (1<<7) +#define CHG_endofch (1<<6) +#define CHG_status (1<<5) +#define CHG_changed (1<<4) + +/* charger control bits in AS3514_SYSTEM */ +#define CHG_I (1<<4) +#define CHG_V (1<<1) +#define CHG_OFF (1<<0) + void power_init(void) { + /* configure charger */ + pp_i2c_send(AS3514_I2C_ADDR, AS3514_CHARGER, (((CHARGE_CURRENT - 50) / 50) << 4) | + (((CHARGE_VOLTAGE - 3900) / 50) << 1)); + /* enable charger interrupts */ + pp_i2c_send(AS3514_I2C_ADDR, AS3514_IRQ_ENRD0, 0xF0); } void power_off(void) @@ -62,6 +90,32 @@ return false; } +bool charging_state(void) +{ + unsigned char status; + + /* check if charging cable is inserted */ + if (!charger_inserted()) { + return false; + } + + /* check CHG_status bit in charger status */ + status = i2c_readbyte(AS3514_I2C_ADDR, AS3514_IRQ_ENRD0); + if ((status & CHG_status) == 0) + { + return false; + } + /* check CHG_endofch bit in charger status */ + if (status & CHG_endofch) + { + /* set CHG_OFF bit to disable charger */ + pp_i2c_send(AS3514_I2C_ADDR, AS3514_CHARGER, CHG_OFF); + return false; + } + + return true; +} + void ide_power_enable(bool on) { (void)on;