summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Sevakis <jethead71@rockbox.org>2008-12-24 23:36:33 +0000
committerMichael Sevakis <jethead71@rockbox.org>2008-12-24 23:36:33 +0000
commit4f2bc16f5cf1fd708883328fbd2b5d5f3b6eb314 (patch)
treeb50ab38accc6b6f45833eb9f59fe80d36c7ac3ff
parent377b42b63bf49e72f49969ad96eeeef8cc3c4ce2 (diff)
downloadrockbox-4f2bc16f5cf1fd708883328fbd2b5d5f3b6eb314.tar.gz
rockbox-4f2bc16f5cf1fd708883328fbd2b5d5f3b6eb314.zip
Gigabeat S: Simplify and use a thermistor lookup table (ADC value => deg C) . Saves some binsize.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@19583 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--firmware/target/arm/imx31/gigabeat-s/powermgmt-imx31.c86
1 files changed, 44 insertions, 42 deletions
diff --git a/firmware/target/arm/imx31/gigabeat-s/powermgmt-imx31.c b/firmware/target/arm/imx31/gigabeat-s/powermgmt-imx31.c
index b4a6c61fbb..e563fede43 100644
--- a/firmware/target/arm/imx31/gigabeat-s/powermgmt-imx31.c
+++ b/firmware/target/arm/imx31/gigabeat-s/powermgmt-imx31.c
@@ -53,36 +53,6 @@ const unsigned short percent_to_volt_charge[11] =
53 3480, 3550, 3590, 3610, 3630, 3650, 3700, 3760, 3800, 3910, 3990 53 3480, 3550, 3590, 3610, 3630, 3650, 3700, 3760, 3800, 3910, 3990
54}; 54};
55 55
56/**
57 * Fixed-point natural log
58 * taken from http://www.quinapalus.com/efunc.html
59 * "The code assumes integers are at least 32 bits long. The (positive)
60 * argument and the result of the function are both expressed as fixed-point
61 * values with 16 fractional bits, although intermediates are kept with 28
62 * bits of precision to avoid loss of accuracy during shifts."
63 */
64static long flog(int x)
65{
66 long t,y;
67
68 y=0xa65af;
69 if(x<0x00008000) x<<=16, y-=0xb1721;
70 if(x<0x00800000) x<<= 8, y-=0x58b91;
71 if(x<0x08000000) x<<= 4, y-=0x2c5c8;
72 if(x<0x20000000) x<<= 2, y-=0x162e4;
73 if(x<0x40000000) x<<= 1, y-=0x0b172;
74 t=x+(x>>1); if((t&0x80000000)==0) x=t,y-=0x067cd;
75 t=x+(x>>2); if((t&0x80000000)==0) x=t,y-=0x03920;
76 t=x+(x>>3); if((t&0x80000000)==0) x=t,y-=0x01e27;
77 t=x+(x>>4); if((t&0x80000000)==0) x=t,y-=0x00f85;
78 t=x+(x>>5); if((t&0x80000000)==0) x=t,y-=0x007e1;
79 t=x+(x>>6); if((t&0x80000000)==0) x=t,y-=0x003f8;
80 t=x+(x>>7); if((t&0x80000000)==0) x=t,y-=0x001fe;
81 x=0x80000000-x;
82 y-=x>>15;
83 return y;
84}
85
86/* Returns battery voltage from ADC [millivolts] */ 56/* Returns battery voltage from ADC [millivolts] */
87unsigned int battery_adc_voltage(void) 57unsigned int battery_adc_voltage(void)
88{ 58{
@@ -134,30 +104,62 @@ unsigned int cccv_regulator_dissipation(void)
134/* Returns battery temperature from ADC [deg-C] */ 104/* Returns battery temperature from ADC [deg-C] */
135int battery_adc_temp(void) 105int battery_adc_temp(void)
136{ 106{
137 unsigned int value = adc_read(ADC_BATTERY_TEMP);
138 /* E[volts] = value * 2.3V / 1023 107 /* E[volts] = value * 2.3V / 1023
139 * R[ohms] = E/I = E[volts] / 0.00002[A] (Thermistor bias current source) 108 * R[ohms] = E/I = E[volts] / 0.00002[A] (Thermistor bias current source)
140 * 109 *
141 * Steinhart-Hart thermistor equation (sans "C*ln^2(R)" term because it 110 * Steinhart-Hart thermistor equation:
142 * has negligible effect):
143 * [A + B*ln(R) + D*ln^3(R)] = 1 / T[°K] 111 * [A + B*ln(R) + D*ln^3(R)] = 1 / T[°K]
144 * 112 *
145 * Coeffients that fit experimental data (one thermistor so far, one run): 113 * Coeffients that fit experimental data (one thermistor so far, one run):
146 * A = 0.0013002631685462800 114 * A = 0.0013002631685462800
147 * B = 0.0002000841932612330 115 * B = 0.0002000841932612330
148 * D = 0.0000000640446750919 116 * D = 0.0000000640446750919
149 *
150 * Fixed-point output matches the floating-point version for each ADC
151 * value.
152 */ 117 */
118 static const unsigned short ntc_table[] =
119 {
120#if 0 /* These have degree deltas > 1 (except the final two) so leave them
121 * out. 70 deg C upper limit is quite sufficient. */
122 0, /* INF */ 1, /* 171 */ 2, /* 145 */ 3, /* 130 */
123 4, /* 121 */ 5, /* 114 */ 6, /* 108 */ 7, /* 104 */
124 8, /* 100 */ 9, /* 96 */ 10, /* 93 */ 11, /* 91 */
125 12, /* 88 */ 13, /* 86 */ 14, /* 84 */ 15, /* 82 */
126 16, /* 81 */ 17, /* 79 */ 18, /* 78 */ 19, /* 76 */
127 20, /* 75 */ 21, /* 74 */ 22, /* 72 */ 23, /* 71 */
128#endif
129 24, /* 70 */ 25, /* 69 */ 26, /* 68 */ 27, /* 67 */
130 28, /* 66 */ 30, /* 65 */ 31, /* 64 */ 32, /* 63 */
131 33, /* 62 */ 35, /* 61 */ 36, /* 60 */ 38, /* 59 */
132 39, /* 58 */ 41, /* 57 */ 43, /* 56 */ 45, /* 55 */
133 47, /* 54 */ 49, /* 53 */ 51, /* 52 */ 53, /* 51 */
134 56, /* 50 */ 58, /* 49 */ 61, /* 48 */ 63, /* 47 */
135 66, /* 46 */ 69, /* 45 */ 73, /* 44 */ 76, /* 43 */
136 80, /* 42 */ 83, /* 41 */ 87, /* 40 */ 92, /* 39 */
137 96, /* 38 */ 101, /* 37 */ 106, /* 36 */ 111, /* 35 */
138 116, /* 34 */ 122, /* 33 */ 128, /* 32 */ 135, /* 31 */
139 142, /* 30 */ 149, /* 29 */ 156, /* 28 */ 164, /* 27 */
140 173, /* 26 */ 182, /* 25 */ 192, /* 24 */ 202, /* 23 */
141 212, /* 22 */ 224, /* 21 */ 236, /* 20 */ 249, /* 19 */
142 262, /* 18 */ 277, /* 17 */ 292, /* 16 */ 308, /* 15 */
143 325, /* 14 */ 344, /* 13 */ 363, /* 12 */ 384, /* 11 */
144 406, /* 10 */ 429, /* 9 */ 454, /* 8 */ 480, /* 7 */
145 509, /* 6 */ 539, /* 5 */ 571, /* 4 */ 605, /* 3 */
146 641, /* 2 */ 680, /* 1 */ 722, /* 0 */ 766, /* -1 */
147 813, /* -2 */ 864, /* -3 */ 918, /* -4 */ 976, /* -5 */
148 };
149
150 unsigned int value = adc_read(ADC_BATTERY_TEMP);
151
153 if (value > 0) 152 if (value > 0)
154 { 153 {
155 int R = 2070000 * value; 154 unsigned i;
156 long long ln = flog(R) + 83196; 155
157 long long t0 = 425890304133ll; 156 for (i = 1; i < ARRAYLEN(ntc_table); i++)
158 long long t1 = 1000000*ln; 157 {
159 long long t3 = ln*ln*ln / 13418057; 158 if (ntc_table[i] > value)
160 return ((32754211579494400ll / (t0 + t1 + t3)) - 27315) / 100; 159 break;
160 }
161
162 return 71 - i;
161 } 163 }
162 164
163 return INT_MIN; 165 return INT_MIN;