summaryrefslogtreecommitdiffstats
path: root/firmware/target/sh/archos/recorder/powermgmt-recorder.c
blob: 1e78b8d155aef6577e2a07ce974ef04367537f6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Heikki Hannikainen, Uwe Freese
 * Revisions copyright (C) 2005 by Gerald Van Baren
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"
#include "system.h"
#include <stdio.h>
#include "debug.h"
#include "storage.h"
#include "adc.h"
#include "power.h"
#include "powermgmt.h"

const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] =
{
    4750
};

const unsigned short battery_level_shutoff[BATTERY_TYPES_COUNT] =
{
    4400
};

/* voltages (millivolt) of 0%, 10%, ... 100% when charging disabled */
const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] =
{
    /* original values were taken directly after charging, but it should show
       100% after turning off the device for some hours, too */
    { 4500, 4810, 4910, 4970, 5030, 5070, 5120, 5140, 5170, 5250, 5400 }
                                            /* orig. values: ...,5280,5600 */
};

/* voltages (millivolt) of 0%, 10%, ... 100% when charging enabled */
const unsigned short percent_to_volt_charge[11] =
{
    /* values guessed, see
       http://www.seattlerobotics.org/encoder/200210/LiIon2.pdf until someone
       measures voltages over a charging cycle */
    4760, 5440, 5510, 5560, 5610, 5640, 5660, 5760, 5820, 5840, 5850 /* NiMH */
};

#define BATTERY_SCALE_FACTOR 6620
/* full-scale ADC readout (2^10) in millivolt */

/* Returns battery voltage from ADC [millivolts] */
int _battery_voltage(void)
{
    return (adc_read(ADC_UNREG_POWER) * BATTERY_SCALE_FACTOR) >> 10;
}

void powermgmt_init_target(void)
{
}

/** Charger control **/
#ifdef CHARGING_DEBUG_FILE
#include "file.h"
#define DEBUG_FILE_NAME "/powermgmt.csv"
#define DEBUG_MESSAGE_LEN 133
static char debug_message[DEBUG_MESSAGE_LEN];
static int fd = -1;          /* write debug information to this file */
static int wrcount = 0;
#endif /* CHARGING_DEBUG_FILE */

/*
 * For a complete description of the charging algorithm read
 * docs/CHARGING_ALGORITHM.
 */
int long_delta;                     /* long term delta battery voltage */
int short_delta;                    /* short term delta battery voltage */
bool disk_activity_last_cycle = false;  /* flag set to aid charger time
                                         * calculation */
char power_message[POWER_MESSAGE_LEN] = ""; /* message that's shown in
                                           debug menu */
                                        /* percentage at which charging
                                           starts */
int powermgmt_last_cycle_startstop_min = 0; /* how many minutes ago was the
                                           charging started or
                                           stopped? */
int powermgmt_last_cycle_level = 0;     /* which level had the
                                           batteries at this time? */
int trickle_sec = 0;                    /* how many seconds should the
                                           charger be enabled per
                                           minute for trickle
                                           charging? */
int pid_p = 0;                      /* PID proportional term */
int pid_i = 0;                      /* PID integral term */

static unsigned int target_voltage = TRICKLE_VOLTAGE; /* desired topoff/trickle
                                                       * voltage level */
static int charge_max_time_idle = 0; /* max. charging duration, calculated at
                                      * beginning of charging */
static int charge_max_time_now = 0; /* max. charging duration including
                                     * hdd activity */
static int minutes_disk_activity = 0; /* count minutes of hdd use during
                                       * charging */
static int last_disk_activity = CHARGE_END_LONGD + 1; /* last hdd use x mins ago */

#ifdef CHARGING_DEBUG_FILE
static void debug_file_close(void)
{
    if (fd >= 0) {
        close(fd);
        fd = -1;
    }
}

static void debug_file_log(void)
{
    if (usb_inserted()) {
        /* It is probably too late to close the file but we can try... */
        debug_file_close();
    }
    else if (fd < 0) {
        fd = open(DEBUG_FILE_NAME, O_WRONLY | O_APPEND | O_CREAT, 0666);

        if (fd >= 0) {
            snprintf(debug_message, DEBUG_MESSAGE_LEN,
            "cycle_min, bat_millivolts, bat_percent, chgr_state"
            " ,charge_state, pid_p, pid_i, trickle_sec\n");
            write(fd, debug_message, strlen(debug_message));
            wrcount = 99;   /* force a flush */
        }
    }
    else {
        snprintf(debug_message, DEBUG_MESSAGE_LEN,
                "%d, %d, %d, %d, %d, %d, %d, %d\n",
            powermgmt_last_cycle_startstop_min, battery_voltage(),
            battery_level(), charger_input_state, charge_state,
            pid_p, pid_i, trickle_sec);
        write(fd, debug_message, strlen(debug_message));
        wrcount++;
    }
}

static void debug_file_sync(void)
{
    /*
     * If we have a lot of pending writes or if the disk is spining,
     * fsync the debug log file.
     */
    if (wrcount > 10 || (wrcount > 0 && storage_disk_is_active())) {
        if (fd >= 0)
            fsync(fd);

        wrcount = 0;
    }
}
#else /* !CHARGING_DEBUG_FILE */
#define debug_file_close()
#define debug_file_log()
#define debug_file_sync()
#endif /* CHARGING_DEBUG_FILE */

/*
 * Do tasks that should be done every step.
 */
static void do_frequent_tasks(void)
{
    if (storage_disk_is_active()) {
        /* flag hdd use for charging calculation */
        disk_activity_last_cycle = true;
    }

    debug_file_sync();
}

/*
 * The charger was just plugged in.  If the battery level is
 * nearly charged, just trickle.  If the battery is low, start
 * a full charge cycle.  If the battery level is in between,
 * top-off and then trickle.
 */
static void charger_plugged(void)
{
    int battery_percent = battery_level();

    pid_p = 0;
    pid_i = 0;
    powermgmt_last_cycle_level = battery_percent;
    powermgmt_last_cycle_startstop_min = 0;

    snprintf(power_message, POWER_MESSAGE_LEN, "Charger plugged in");

    if (battery_percent > START_TOPOFF_CHG) {

        if (battery_percent >= START_TRICKLE_CHG) {
            charge_state = TRICKLE;
            target_voltage = TRICKLE_VOLTAGE;
        }
        else {
            charge_state = TOPOFF;
            target_voltage = TOPOFF_VOLTAGE;
        }
    }
    else {
        /*
         * Start the charger full strength
         */
        int i = CHARGE_MAX_MIN_1500 * get_battery_capacity() / 1500;
        charge_max_time_idle = i * (100 + 35 - battery_percent) / 100;

        if (charge_max_time_idle > i)
            charge_max_time_idle = i;

        charge_max_time_now = charge_max_time_idle;

        snprintf(power_message, POWER_MESSAGE_LEN,
                 "ChgAt %d%% max %dm", battery_percent,
                 charge_max_time_now);

        /*
         * Enable the charger after the max time calc is done,
         * because battery_level depends on if the charger is
         * on.
         */
        DEBUGF("power: charger inserted and battery"
               " not full, charging\n");
        trickle_sec  = 60;
        long_delta   = short_delta = 999999;
        charge_state = CHARGING;
    }
}

/*
 * The charger was just unplugged.
 */
static void charger_unplugged(void)
{
    DEBUGF("power: charger disconnected, disabling\n");

    charger_enable(false);
    powermgmt_last_cycle_level = battery_level();
    powermgmt_last_cycle_startstop_min = 0;
    trickle_sec  = 0;
    pid_p        = 0;
    pid_i        = 0;
    charge_state = DISCHARGING;
    snprintf(power_message, POWER_MESSAGE_LEN, "Charger: discharge");
}

static void charging_step(void)
{
    int i;

    /* alter charge time max length with extra disk use */
    if (disk_activity_last_cycle) {
        minutes_disk_activity++;
        charge_max_time_now = charge_max_time_idle +
                              minutes_disk_activity*2 / 5;
        disk_activity_last_cycle = false;
        last_disk_activity = 0;
    }
    else {
        last_disk_activity++;
    }

    /*
     * Check the delta voltage over the last X minutes so we can do
     * our end-of-charge logic based on the battery level change
     * (no longer use minimum time as logic for charge end has 50
     * minutes minimum charge built in).
     */
    if (powermgmt_last_cycle_startstop_min > CHARGE_END_SHORTD) {
        short_delta = power_history[0] -
                      power_history[CHARGE_END_SHORTD - 1];
    }

    if (powermgmt_last_cycle_startstop_min > CHARGE_END_LONGD) {
        /*
         * Scan the history: the points where measurement is taken need to
         * be fairly static. Check prior to short delta 'area'. Also only
         * check first and last 10 cycles (delta in middle OK).
         */
        long_delta = power_history[0] -
                     power_history[CHARGE_END_LONGD - 1];

        for (i = CHARGE_END_SHORTD; i < CHARGE_END_SHORTD + 10; i++)
        {
            if ((power_history[i] - power_history[i+1]) >  50 ||
                (power_history[i] - power_history[i+1]) < -50) {
                long_delta = 777777;
                break;
            }
        }

        for (i = CHARGE_END_LONGD - 11; i < CHARGE_END_LONGD - 1 ; i++)
        {
            if ((power_history[i] - power_history[i+1]) >  50 ||
                (power_history[i] - power_history[i+1]) < -50) {
                long_delta = 888888;
                break;
            }
        }
    }

    snprintf(power_message, POWER_MESSAGE_LEN,
             "Chg %dm, max %dm", powermgmt_last_cycle_startstop_min,
             charge_max_time_now);

    /*
     * End of charge criteria (any qualify):
     * 1) Charged a long time
     * 2) DeltaV went negative for a short time ( & long delta static)
     * 3) DeltaV was negative over a longer period (no disk use only)
     *
     * Note: short_delta and long_delta are millivolts
     */
    if (powermgmt_last_cycle_startstop_min >= charge_max_time_now ||
        (short_delta <= -50 && long_delta < 50) ||
        (long_delta  < -20 && last_disk_activity > CHARGE_END_LONGD)) {

        int battery_percent = battery_level();

        if (powermgmt_last_cycle_startstop_min > charge_max_time_now) {
            DEBUGF("power: powermgmt_last_cycle_startstop_min > charge_max_time_now, "
                   "enough!\n");
            /*
             * Have charged too long and deltaV detection did not
             * work!
             */
             snprintf(power_message, POWER_MESSAGE_LEN,
                     "Chg tmout %d min", charge_max_time_now);
            /*
             * Switch to trickle charging.  We skip the top-off
             * since we've effectively done the top-off operation
             * already since we charged for the maximum full
             * charge time.
             */
            powermgmt_last_cycle_level = battery_percent;
            powermgmt_last_cycle_startstop_min = 0;
            charge_state = TRICKLE;

            /*
             * Set trickle charge target to a relative voltage instead
             * of an arbitrary value - the fully charged voltage may
             * vary according to ambient temp, battery condition etc.
             * Trickle target is -0.15v from full voltage acheived.
             * Topup target is -0.05v from full voltage.
             */
            target_voltage = power_history[0] - 150;

        }
        else {
            if(short_delta <= -5) {
                DEBUGF("power: short-term negative"
                       " delta, enough!\n");
                snprintf(power_message, POWER_MESSAGE_LEN,
                         "end negd %d %dmin", short_delta,
                         powermgmt_last_cycle_startstop_min);
                target_voltage = power_history[CHARGE_END_SHORTD - 1] - 50;
            }
            else {
                DEBUGF("power: long-term small "
                       "positive delta, enough!\n");
                snprintf(power_message, POWER_MESSAGE_LEN,
                         "end lowd %d %dmin", long_delta,
                         powermgmt_last_cycle_startstop_min);
                target_voltage = power_history[CHARGE_END_LONGD - 1] - 50;
            }

            /*
             * Switch to top-off charging.
             */
            powermgmt_last_cycle_level = battery_percent;
            powermgmt_last_cycle_startstop_min = 0;
            charge_state = TOPOFF;
        }
    }
}

static void topoff_trickle_step(void)
{
    unsigned int millivolts;

    /*
     *Time to switch from topoff to trickle?
     */
    if (charge_state == TOPOFF &&
        powermgmt_last_cycle_startstop_min > TOPOFF_MAX_MIN) {

        powermgmt_last_cycle_level = battery_level();
        powermgmt_last_cycle_startstop_min = 0;
        charge_state = TRICKLE;
        target_voltage = target_voltage - 100;
    }
    /*
     * Adjust trickle charge time (proportional and integral terms).
     * Note: I considered setting the level higher if the USB is
     * plugged in, but it doesn't appear to be necessary and will
     * generate more heat [gvb].
     */
    millivolts = battery_voltage();

    pid_p = ((signed)target_voltage - (signed)millivolts) / 5;
    if (pid_p <= PID_DEADZONE && pid_p >= -PID_DEADZONE)
        pid_p = 0;

    if ((unsigned)millivolts < target_voltage) {
        if (pid_i < 60)
            pid_i++; /* limit so it doesn't "wind up" */
    }
    else {
        if (pid_i > 0)
            pid_i--; /* limit so it doesn't "wind up" */
    }

    trickle_sec = pid_p + pid_i;

    if (trickle_sec > 60)
        trickle_sec = 60;

    if (trickle_sec < 0)
        trickle_sec = 0;
}

void charging_algorithm_step(void)
{
    static int pwm_counter = 0; /* PWM total cycle in steps */
    static int pwm_duty = 0;    /* PWM duty cycle in steps */

    switch (charger_input_state)
    {
    case CHARGER_PLUGGED:
        charger_plugged();
        break;

    case CHARGER_UNPLUGGED:
        charger_unplugged();
        break;

    case CHARGER:
    case NO_CHARGER:
        do_frequent_tasks();

        if (pwm_counter > 0) {
            if (pwm_duty > 0 && --pwm_duty <= 0)
                charger_enable(false); /* Duty cycle expired */

            if (--pwm_counter > 0)
                return;

            /* PWM cycle is complete */
            powermgmt_last_cycle_startstop_min++;
            debug_file_log();
        }
        break;
    }

    switch (charge_state)
    {
    case CHARGING:
        charging_step();
        break;

    case TOPOFF:
    case TRICKLE:
        topoff_trickle_step();        
        break;

    case DISCHARGING:
    default:
        break;
    }

    /* If 100%, ensure pwm_on never expires and briefly disables the
     * charger. */
    pwm_duty = (trickle_sec < 60) ? trickle_sec*2 : 0;
    pwm_counter = 60*2;
    charger_enable(trickle_sec > 0);
}

void charging_algorithm_close(void)
{
#ifdef CHARGING_DEBUG_FILE
    debug_file_close();
#endif /* CHARGING_DEBUG_FILE */
}

/* Returns true if the unit is charging the batteries. */
bool charging_state(void)
{
    return charge_state == CHARGING;
}