summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/sandisk/sansa-e200/button-e200.c
blob: 1f108b9b9079f2040029a833985f25b75c74b239 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 by Barry Wardell
 *
 * 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.
 *
 ****************************************************************************/

/* Taken from button-h10.c by Barry Wardell and reverse engineering by MrH. */

#include "system.h"
#include "kernel.h"
#include "button.h"
#include "backlight.h"
#include "powermgmt.h"

#define WHEEL_REPEAT_VELOCITY       35 /* deg/s */
#define WHEEL_SMOOTHING_VELOCITY    50 /* deg/s */
#define WHEEL_FAST_ON_VELOCITY     350 /* deg/s */
#define WHEEL_FAST_OFF_VELOCITY    150 /* deg/s */
#define WHEEL_BASE_SENSITIVITY       2
#define WHEELCLICKS_PER_ROTATION    48 /* wheelclicks per full rotation */

#ifndef BOOTLOADER
/* Wheel */
static int read_wheel_keycode(void);
/* Buttons */
static bool hold_button     = false;
static bool hold_button_old = false;
#define _button_hold() hold_button
#else
#define _button_hold() ((GPIOF_INPUT_VAL & 0x80) != 0)
#endif /* BOOTLOADER */
static int  int_btn         = BUTTON_NONE;

void button_init_device(void)
{
    /* Enable all buttons */
    GPIO_CLEAR_BITWISE(GPIOF_OUTPUT_EN, 0xff);
    GPIO_SET_BITWISE(GPIOF_ENABLE, 0xff);

    /* Scrollwheel light - enable control through GPIOG pin 7 and set timeout */
    GPIO_SET_BITWISE(GPIOG_OUTPUT_EN, 0x80);
    GPIO_SET_BITWISE(GPIOG_ENABLE, 0x80);

#ifndef BOOTLOADER
    /* Mask these before performing init ... because init has possibly
       occurred before */
    GPIO_CLEAR_BITWISE(GPIOF_INT_EN, 0xff);
    GPIO_CLEAR_BITWISE(GPIOH_INT_EN, 0xc0);

    GPIO_CLEAR_BITWISE(GPIOH_OUTPUT_EN, 0xc0);
    GPIO_SET_BITWISE(GPIOH_ENABLE, 0xc0);

    /* Read initial buttons */
    button_int();

    /* Read initial wheel value (bit 6-7 of GPIOH) */
    read_wheel_keycode();

    /* Enable button interrupts */
    GPIO_SET_BITWISE(GPIOF_INT_EN, 0xff);
    GPIO_SET_BITWISE(GPIOH_INT_EN, 0xc0);

    CPU_INT_EN = HI_MASK;
    CPU_HI_INT_EN = GPIO1_MASK;
#endif /* BOOTLOADER */
}

bool button_hold(void)
{
    return _button_hold();
}

/* clickwheel */
#ifndef BOOTLOADER
static int read_wheel_keycode(void)
{
    /* Read wheel 
     * Bits 6 and 7 of GPIOH change as follows:
     * Clockwise rotation   01 -> 00 -> 10 -> 11
     * Counter-clockwise    11 -> 10 -> 00 -> 01
     *
     * This is equivalent to wheel_value of:
     * Clockwise rotation   0x40 -> 0x00 -> 0x80 -> 0xc0
     * Counter-clockwise    0xc0 -> 0x80 -> 0x00 -> 0x40
     */
    static const unsigned char wheel_tbl[2][4] =
    {
       /* 0x00  0x40  0x80  0xc0 */ /* Wheel value        */
        { 0x40, 0xc0, 0x00, 0x80 }, /* Clockwise rotation */
        { 0x80, 0x00, 0xc0, 0x40 }, /* Counter-clockwise  */ 
    };

    static unsigned long prev_value;

    int keycode = BUTTON_NONE;

    unsigned long value = GPIOH_INPUT_VAL & 0xc0;
    GPIO_WRITE_BITWISE(GPIOH_INT_LEV, value ^ 0xc0, 0xc0);
    GPIOH_INT_CLR = GPIOH_INT_STAT & 0xc0;

    if (!hold_button)
    {
        unsigned long prev = prev_value;
        int scroll = value >> 6;

        if (prev == wheel_tbl[0][scroll])
            keycode = BUTTON_SCROLL_FWD;
        else if (prev == wheel_tbl[1][scroll])
            keycode = BUTTON_SCROLL_BACK;
    }

    prev_value = value;

    return keycode;
}

void clickwheel_int(void)
{
    static int prev_keycode = BUTTON_NONE;
    static int prev_keypost = BUTTON_NONE;
    static int count = 0;
    static int fast_mode = 0;
    static long nextbacklight_hw_on = 0;

    static unsigned long prev_usec[WHEEL_BASE_SENSITIVITY] = { 0 };
    static unsigned long delta = 1ul << 24;
    static unsigned long velocity = 0; /* Velocity smoothed or unsmoothed */

    unsigned long usec = USEC_TIMER;
    unsigned long v; /* Raw velocity */

    int keycode = read_wheel_keycode();

    if (keycode == BUTTON_NONE)
        return;

    /* Spurious wheel "reversals" are not uncommon. Resetting also helps
     * cover them up. As such, prev_keypost is also not reset or else false
     * non-repeats are generated when it happens. */
    if (keycode != prev_keycode)
    {
        /* Direction reversals reset state */
        unsigned long usec_back = 360000000ul /
                        (WHEEL_REPEAT_VELOCITY*WHEELCLICKS_PER_ROTATION);
        prev_keycode = keycode;
        count = 0;
        fast_mode = 0;
        prev_usec[0] = usec - usec_back;
        prev_usec[1] = prev_usec[0] - usec_back;
        delta = 1ul << 24;
        velocity = 0;
    }

    if (TIME_AFTER(current_tick, nextbacklight_hw_on))
    {
        /* Poke backlight to turn it on or maintain it no more often
         * than every 1/4 second */
        nextbacklight_hw_on = current_tick + HZ/4;
        backlight_on();
#ifdef HAVE_BUTTON_LIGHT
        buttonlight_on();
#endif
        reset_poweroff_timer();
    }

    /* Calculate deg/s based upon interval and number of clicks in that
     * interval - FIR moving average */
    v = usec - prev_usec[1];

    if ((long)v <= 0)
    {
        /* timer wrapped (no activity for awhile), skip acceleration */
        v = 0;
        delta = 1ul << 24;
    }
    else
    {
        /* Check overflow below */
        if (v > 0xfffffffful / WHEELCLICKS_PER_ROTATION)
            v = 0xfffffffful / WHEELCLICKS_PER_ROTATION;

        v = 360000000ul*WHEEL_BASE_SENSITIVITY / (v*WHEELCLICKS_PER_ROTATION);

        if (v > 0xfffffful)
            v = 0xfffffful; /* limit to 24 bits */
    }

    prev_usec[1] = prev_usec[0];
    prev_usec[0] = usec;

    if (v < WHEEL_SMOOTHING_VELOCITY)
    {
        /* Very slow - no smoothing */
        velocity = v;
        /* Ensure backlight never gets stuck for an extended period if tick
         * wrapped such that next poke is very far ahead */
        nextbacklight_hw_on = current_tick - 1;
    }
    else
    {
        /* Some velocity filtering to smooth things out */
        velocity = (7*velocity + v) / 8;
    }

    if (fast_mode != 0)
    {
        /* Fast OFF happens immediately when velocity drops below
           threshold */
        if (v < WHEEL_FAST_OFF_VELOCITY)
        {
            fast_mode = 0; /* moving out of fast mode */
            velocity = v;

            /* delta is always 1 in slow mode */
            delta = 1ul << 24;
        }
    }
    else
    {
        /* Fast ON gets filtered to avoid inadvertent jumps to fast mode */
        if (velocity >= WHEEL_FAST_ON_VELOCITY)
            fast_mode = 1; /* moving into fast mode */

        /* delta is always 1 in slow mode */
        delta = 1ul << 24;
    }

    count += fast_mode + 1;
    if (count < WHEEL_BASE_SENSITIVITY)
        return;

    count = 0;

    if (button_queue_empty())
    {
        /* Post wheel keycode with wheel data */
        int key = keycode;

        if (v >= WHEEL_REPEAT_VELOCITY && prev_keypost == key)
        {
            /* Quick enough and same key is being posted more than once in a
             * row - generate repeats - use unsmoothed v */
            key |= BUTTON_REPEAT;
        }

        prev_keypost = keycode;

        button_queue_post(key, (fast_mode << 31) | delta | velocity);
        /* Message posted - reset delta */
        delta = 1ul << 24;
    }
    else
    {
        /* Skipped post - increment delta and limit to 7 bits */
        delta += 1ul << 24;

        if (delta > (0x7ful << 24))
            delta = 0x7ful << 24;
    }
}
#else
void clickwheel_int(void)
{
}
#endif /* BOOTLOADER */

/* device buttons */
void button_int(void)
{
    unsigned long state = GPIOF_INPUT_VAL & 0xff;

#ifndef BOOTLOADER
    unsigned long status = GPIOF_INT_STAT;

    GPIO_WRITE_BITWISE(GPIOF_INT_LEV, state ^ 0xff, 0xff);
    GPIOF_INT_CLR = status;

    hold_button = (state & 0x80) != 0;
#endif

    int_btn = BUTTON_NONE;

    if (!_button_hold())
    {
        /* Read normal buttons */
        if ((state & 0x01) == 0) int_btn |= BUTTON_REC;
        if ((state & 0x02) == 0) int_btn |= BUTTON_DOWN;
        if ((state & 0x04) == 0) int_btn |= BUTTON_RIGHT;
        if ((state & 0x08) == 0) int_btn |= BUTTON_LEFT;
        if ((state & 0x10) == 0) int_btn |= BUTTON_SELECT; /* The centre button */
        if ((state & 0x20) == 0) int_btn |= BUTTON_UP; /* The "play" button */
        if ((state & 0x40) != 0) int_btn |= BUTTON_POWER;
    }
}

/*
 * Get button pressed from hardware
 */
int button_read_device(void)
{
#ifdef BOOTLOADER
    /* Read buttons directly in the bootloader */
    button_int();
#else
    /* light handling */
    if (hold_button != hold_button_old)
    {
        hold_button_old = hold_button;
        backlight_hold_changed(hold_button);
    }
#endif /* BOOTLOADER */

    /* The int_btn variable is set in the button interrupt handler */
    return int_btn;
}