fuzev2: remove some delays from button driver
[kugel-rb.git] / firmware / target / arm / as3525 / sansa-fuzev2 / button-fuzev2.c
blobb9139ef555dbcf072e227078f87b6470d91b669d
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 unsigned read_GPIOA_67(void)
43 unsigned ret = 0;
44 volatile int i;
45 DBOP_CTRL |= 1<<19;
46 for(i = 20; i; i--) nop;
47 GPIOA_DIR &= ~0xc0;
48 for(i = 20; i; i--) nop;
49 if (GPIOA_PIN(6) != 0)
50 ret = 1<<0;
51 for(i = 20; i; i--) nop;
52 if (GPIOA_PIN(7) != 0)
53 ret |= 1<<1;
54 DBOP_CTRL &= ~(1<<19);
55 for(i = 20; i; i--) nop;
56 return ret;
59 void get_scrollwheel(void)
61 #if defined(HAVE_SCROLLWHEEL) && !defined(BOOTLOADER)
62 /* scroll wheel handling */
63 scrollwheel(read_GPIOA_67());
64 #endif
68 * Get button pressed from hardware
72 int button_read_device(void)
74 int btn = 0;
75 volatile int delay;
76 static bool hold_button_old = false;
77 static long power_counter = 0;
78 unsigned gpiod6;
80 get_scrollwheel();
82 CCU_IO &= ~(1<<12);
84 GPIOB_PIN(0) = 1<<0;
85 for(delay = 500; delay; delay--)
86 nop;
88 gpiod6 = GPIOD_PIN(6);
90 GPIOB_PIN(0) = 0;
91 for(delay = 240; delay; delay--)
92 nop;
94 if (GPIOC_PIN(1) & 1<<1)
95 btn |= BUTTON_DOWN;
96 if (GPIOC_PIN(2) & 1<<2)
97 btn |= BUTTON_UP;
98 if (GPIOC_PIN(3) & 1<<3)
99 btn |= BUTTON_LEFT;
100 if (GPIOC_PIN(4) & 1<<4)
101 btn |= BUTTON_SELECT;
102 if (GPIOC_PIN(5) & 1<<5)
103 btn |= BUTTON_RIGHT;
104 if (GPIOB_PIN(1) & 1<<1)
105 btn |= BUTTON_HOME;
106 if (gpiod6 & 1<<6)
107 { /* power/hold is on the same pin. we know it's hold if the bit isn't
108 * set now anymore */
109 if (GPIOD_PIN(6) & 1<<6)
111 hold_button = false;
112 btn |= BUTTON_POWER;
114 else
116 hold_button = true;
120 CCU_IO |= 1<<12;
122 #ifdef HAS_BUTTON_HOLD
123 #ifndef BOOTLOADER
124 /* light handling */
125 if (hold_button != hold_button_old)
127 hold_button_old = hold_button;
128 backlight_hold_changed(hold_button);
130 #else
131 (void)hold_button_old;
132 #endif
133 if (hold_button)
135 power_counter = HZ;
136 return 0;
138 /* read power, but not if hold button was just released, since
139 * you basically always hit power due to the slider mechanism after releasing
140 * (fuze only)
142 else if (power_counter > 0)
144 power_counter--;
145 btn &= ~BUTTON_POWER;
147 #endif
148 return btn;
151 #ifdef HAS_BUTTON_HOLD
152 bool button_hold(void)
154 return hold_button;
156 #endif