blob: 796c781f73d6952bebd5e3ee4a157504694da23e [file] [log] [blame]
Will Robertson590501c2007-09-21 15:51:53 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Heikki Hannikainen, Uwe Freese
11 * Revisions copyright (C) 2005 by Gerald Van Baren
12 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000013 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
Will Robertson590501c2007-09-21 15:51:53 +000017 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22
23/* FIXME: This is just the Gigabeat F/X file with a different name... */
24
25#include "config.h"
26#include "adc.h"
27#include "powermgmt.h"
28
Michael Sevakisd8922142008-05-25 01:38:08 +000029/**
30 * Fixed-point natural log
31 * taken from http://www.quinapalus.com/efunc.html
32 * "The code assumes integers are at least 32 bits long. The (positive)
33 * argument and the result of the function are both expressed as fixed-point
34 * values with 16 fractional bits, although intermediates are kept with 28
35 * bits of precision to avoid loss of accuracy during shifts."
36 */
37static long flog(int x)
38{
39 long t,y;
40
41 y=0xa65af;
42 if(x<0x00008000) x<<=16, y-=0xb1721;
43 if(x<0x00800000) x<<= 8, y-=0x58b91;
44 if(x<0x08000000) x<<= 4, y-=0x2c5c8;
45 if(x<0x20000000) x<<= 2, y-=0x162e4;
46 if(x<0x40000000) x<<= 1, y-=0x0b172;
47 t=x+(x>>1); if((t&0x80000000)==0) x=t,y-=0x067cd;
48 t=x+(x>>2); if((t&0x80000000)==0) x=t,y-=0x03920;
49 t=x+(x>>3); if((t&0x80000000)==0) x=t,y-=0x01e27;
50 t=x+(x>>4); if((t&0x80000000)==0) x=t,y-=0x00f85;
51 t=x+(x>>5); if((t&0x80000000)==0) x=t,y-=0x007e1;
52 t=x+(x>>6); if((t&0x80000000)==0) x=t,y-=0x003f8;
53 t=x+(x>>7); if((t&0x80000000)==0) x=t,y-=0x001fe;
54 x=0x80000000-x;
55 y-=x>>15;
56 return y;
57}
58
59
Will Robertson590501c2007-09-21 15:51:53 +000060const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] =
61{
62 3450
63};
64
65const unsigned short battery_level_shutoff[BATTERY_TYPES_COUNT] =
66{
67 3400
68};
69
70/* voltages (millivolt) of 0%, 10%, ... 100% when charging disabled */
71const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] =
72{
73 /* Toshiba Gigabeat Li Ion 830mAH figured from discharge curve */
74 { 3480, 3550, 3590, 3610, 3630, 3650, 3700, 3760, 3800, 3910, 3990 },
75};
76
77/* voltages (millivolt) of 0%, 10%, ... 100% when charging enabled */
78const unsigned short percent_to_volt_charge[11] =
79{
80 /* Toshiba Gigabeat Li Ion 830mAH */
81 3480, 3550, 3590, 3610, 3630, 3650, 3700, 3760, 3800, 3910, 3990
82};
83
Will Robertson590501c2007-09-21 15:51:53 +000084/* Returns battery voltage from ADC [millivolts] */
85unsigned int battery_adc_voltage(void)
86{
Michael Sevakis02575472008-04-14 06:52:38 +000087 /* ADC reading 0-1023 = 2400mV-4700mV */
88 return ((adc_read(ADC_BATTERY) * 2303) >> 10) + 2400;
Will Robertson590501c2007-09-21 15:51:53 +000089}
90
Michael Sevakisd8922142008-05-25 01:38:08 +000091/* Returns battery charge current from ADC [milliamps] */
92int battery_adc_charge_current(void)
93{
94 /* Positive reading = charger to battery
95 * Negative reading = battery to charger terminal
96 * ADC reading -512-511 = -2875mA-2875mA */
97 unsigned int value = adc_read(ADC_CHARGER_CURRENT);
98 return (((int)value << 22) >> 22) * 2881 >> 9;
99}
100
101/* Returns battery temperature from ADC [deg-C] */
102unsigned int battery_adc_temp(void)
103{
104 unsigned int value = adc_read(ADC_BATTERY_TEMP);
105 /* E[volts] = value * 2.3V / 1023
106 * R[ohms] = E/I = E[volts] / 0.00002[A] (Thermistor bias current source)
107 *
108 * Steinhart-Hart thermistor equation (sans "C*ln^2(R)" term because it
109 * has negligible effect):
Michael Sevakis69f4ffe2008-05-25 02:43:51 +0000110 * [A + B*ln(R) + D*ln^3(R)] = 1 / T[°K]
Michael Sevakisd8922142008-05-25 01:38:08 +0000111 *
112 * Coeffients that fit experimental data (one thermistor so far, one run):
113 * A = 0.0013002631685462800
114 * B = 0.0002000841932612330
115 * D = 0.0000000640446750919
116 *
117 * Fixed-point output matches the floating-point version for each ADC
118 * value.
119 */
120 int R = 2070000 * value;
121 long long ln = flog(R) + 83196;
122 long long t0 = 425890304133ll;
123 long long t1 = 1000000*ln;
124 long long t3 = ln*ln*ln / 13418057;
125 return ((32754211579494400ll / (t0 + t1 + t3)) - 27315) / 100;
126}