1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Heikki Hannikainen, Uwe Freese
11 * Revisions copyright (C) 2005 by Gerald Van Baren
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 /* FIXME: This is just the Gigabeat F/X file with a different name... */
25 #include "powermgmt.h"
28 * Fixed-point natural log
29 * taken from http://www.quinapalus.com/efunc.html
30 * "The code assumes integers are at least 32 bits long. The (positive)
31 * argument and the result of the function are both expressed as fixed-point
32 * values with 16 fractional bits, although intermediates are kept with 28
33 * bits of precision to avoid loss of accuracy during shifts."
35 static long flog(int x
)
40 if(x
<0x00008000) x
<<=16, y
-=0xb1721;
41 if(x
<0x00800000) x
<<= 8, y
-=0x58b91;
42 if(x
<0x08000000) x
<<= 4, y
-=0x2c5c8;
43 if(x
<0x20000000) x
<<= 2, y
-=0x162e4;
44 if(x
<0x40000000) x
<<= 1, y
-=0x0b172;
45 t
=x
+(x
>>1); if((t
&0x80000000)==0) x
=t
,y
-=0x067cd;
46 t
=x
+(x
>>2); if((t
&0x80000000)==0) x
=t
,y
-=0x03920;
47 t
=x
+(x
>>3); if((t
&0x80000000)==0) x
=t
,y
-=0x01e27;
48 t
=x
+(x
>>4); if((t
&0x80000000)==0) x
=t
,y
-=0x00f85;
49 t
=x
+(x
>>5); if((t
&0x80000000)==0) x
=t
,y
-=0x007e1;
50 t
=x
+(x
>>6); if((t
&0x80000000)==0) x
=t
,y
-=0x003f8;
51 t
=x
+(x
>>7); if((t
&0x80000000)==0) x
=t
,y
-=0x001fe;
58 const unsigned short battery_level_dangerous
[BATTERY_TYPES_COUNT
] =
63 const unsigned short battery_level_shutoff
[BATTERY_TYPES_COUNT
] =
68 /* voltages (millivolt) of 0%, 10%, ... 100% when charging disabled */
69 const unsigned short percent_to_volt_discharge
[BATTERY_TYPES_COUNT
][11] =
71 /* Toshiba Gigabeat Li Ion 830mAH figured from discharge curve */
72 { 3480, 3550, 3590, 3610, 3630, 3650, 3700, 3760, 3800, 3910, 3990 },
75 /* voltages (millivolt) of 0%, 10%, ... 100% when charging enabled */
76 const unsigned short percent_to_volt_charge
[11] =
78 /* Toshiba Gigabeat Li Ion 830mAH */
79 3480, 3550, 3590, 3610, 3630, 3650, 3700, 3760, 3800, 3910, 3990
82 /* Returns battery voltage from ADC [millivolts] */
83 unsigned int battery_adc_voltage(void)
85 /* ADC reading 0-1023 = 2400mV-4700mV */
86 return ((adc_read(ADC_BATTERY
) * 2303) >> 10) + 2400;
89 /* Returns battery charge current from ADC [milliamps] */
90 int battery_adc_charge_current(void)
92 /* Positive reading = charger to battery
93 * Negative reading = battery to charger terminal
94 * ADC reading -512-511 = -2875mA-2875mA */
95 unsigned int value
= adc_read(ADC_CHARGER_CURRENT
);
96 return (((int)value
<< 22) >> 22) * 2881 >> 9;
99 /* Returns battery temperature from ADC [deg-C] */
100 unsigned int battery_adc_temp(void)
102 unsigned int value
= adc_read(ADC_BATTERY_TEMP
);
103 /* E[volts] = value * 2.3V / 1023
104 * R[ohms] = E/I = E[volts] / 0.00002[A] (Thermistor bias current source)
106 * Steinhart-Hart thermistor equation (sans "C*ln^2(R)" term because it
107 * has negligible effect):
108 * [A + B*ln(R) + D*ln^3(R)] = 1 / T[°K]
110 * Coeffients that fit experimental data (one thermistor so far, one run):
111 * A = 0.0013002631685462800
112 * B = 0.0002000841932612330
113 * D = 0.0000000640446750919
115 * Fixed-point output matches the floating-point version for each ADC
118 int R
= 2070000 * value
;
119 long long ln
= flog(R
) + 83196;
120 long long t0
= 425890304133ll;
121 long long t1
= 1000000*ln
;
122 long long t3
= ln
*ln
*ln
/ 13418057;
123 return ((32754211579494400ll / (t0
+ t1
+ t3
)) - 27315) / 100;