as3525(v2): Add a somewhat inaccurate udelay (-0.5/+1.0µs). It should work good for...
[kugel-rb.git] / firmware / target / arm / as3525 / sansa-fuzev2 / button-fuzev2.c
blobdb08414ae5a65fb94ccc62a2945fb422cb268abe
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 by Thomas Martitz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
23 #include "system.h"
24 #include "button.h"
25 #include "backlight.h"
27 extern void scrollwheel(unsigned wheel_value);
29 #ifdef HAS_BUTTON_HOLD
30 static bool hold_button = false;
31 #endif
32 void button_init_device(void)
34 GPIOA_DIR &= ~(1<<6|1<<7);
35 GPIOC_DIR = 0;
36 GPIOB_DIR |= (1<<4)|(1<<0);
38 GPIOB_PIN(4) = 1<<4; /* activate the wheel */
41 void get_scrollwheel(void)
43 #if defined(HAVE_SCROLLWHEEL) && !defined(BOOTLOADER)
44 /* scroll wheel handling */
46 #define GPIOA_PIN76_offset ((1<<(6+2)) | (1<<(7+2)))
47 #define GPIOA_PIN76 (*(volatile unsigned char*)(GPIOA_BASE+GPIOA_PIN76_offset))
48 scrollwheel(GPIOA_PIN76 >> 6);
50 #endif
54 * Get button pressed from hardware
58 int button_read_device(void)
60 int btn = 0;
61 static bool hold_button_old = false;
62 static long power_counter = 0;
63 unsigned gpiod6;
66 /* if we don't wait for the fifo to empty, we'll see screen corruption
67 * (the higher the CPU frequency the higher the corruption) */
68 while ((DBOP_STAT & (1<<10)) == 0);
70 get_scrollwheel();
72 CCU_IO &= ~(1<<12);
74 GPIOB_PIN(0) = 1<<0;
75 udelay(1);
77 gpiod6 = GPIOD_PIN(6);
79 GPIOB_PIN(0) = 0;
80 udelay(1);
82 if (GPIOC_PIN(1) & 1<<1)
83 btn |= BUTTON_DOWN;
84 if (GPIOC_PIN(2) & 1<<2)
85 btn |= BUTTON_UP;
86 if (GPIOC_PIN(3) & 1<<3)
87 btn |= BUTTON_LEFT;
88 if (GPIOC_PIN(4) & 1<<4)
89 btn |= BUTTON_SELECT;
90 if (GPIOC_PIN(5) & 1<<5)
91 btn |= BUTTON_RIGHT;
92 if (GPIOB_PIN(1) & 1<<1)
93 btn |= BUTTON_HOME;
94 if (gpiod6 & 1<<6)
95 { /* power/hold is on the same pin. we know it's hold if the bit isn't
96 * set now anymore */
97 if (GPIOD_PIN(6) & 1<<6)
99 hold_button = false;
100 btn |= BUTTON_POWER;
102 else
104 hold_button = true;
108 CCU_IO |= 1<<12;
110 #ifdef HAS_BUTTON_HOLD
111 #ifndef BOOTLOADER
112 /* light handling */
113 if (hold_button != hold_button_old)
115 hold_button_old = hold_button;
116 backlight_hold_changed(hold_button);
118 #else
119 (void)hold_button_old;
120 #endif
121 if (hold_button)
123 power_counter = HZ;
124 return 0;
126 /* read power, but not if hold button was just released, since
127 * you basically always hit power due to the slider mechanism after releasing
128 * (fuze only)
130 else if (power_counter > 0)
132 power_counter--;
133 btn &= ~BUTTON_POWER;
135 #endif
136 return btn;
139 #ifdef HAS_BUTTON_HOLD
140 bool button_hold(void)
142 return hold_button;
144 #endif