Sansa e200v2: use = instead of |= when setting GPIO level
[kugel-rb.git] / firmware / target / arm / as3525 / sansa-e200v2 / button-e200v2.c
blob8170c1b895f3865848537fc7cb37783532d3d0a2
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Barry Wardell
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 /* Taken from button-h10.c by Barry Wardell and reverse engineering by MrH. */
24 #include "system.h"
25 #include "button.h"
26 #include "backlight.h"
27 #include "powermgmt.h"
30 static bool hold_button = false;
31 #ifndef BOOTLOADER
32 static bool hold_button_old = false;
33 #endif
34 static unsigned short _dbop_din = 0;
35 /* read_missed is true if buttons could not
36 * be read (see lcd_button_support) */
37 static bool read_missed = false;
39 #define WHEEL_REPEAT_INTERVAL (HZ/4)
40 /* in the lcd driver */
41 extern bool lcd_button_support(void);
43 void button_init_device(void)
48 bool button_hold(void)
50 return hold_button;
53 #if !defined(BOOTLOADER) && defined(HAVE_SCROLLWHEEL)
54 static void scrollwheel(unsigned short dbop_din)
56 /* current wheel values, parsed from dbop and the resulting button */
57 unsigned wheel_value = 0;
58 unsigned btn = BUTTON_NONE;
59 /* old wheel values */
60 static unsigned old_wheel_value = 0;
61 static unsigned old_btn = BUTTON_NONE;
63 /* getting BUTTON_REPEAT works like this: Remember when the btn value was
64 * posted to the button_queue last, and if it was recent enough, generate
65 * BUTTON_REPEAT
67 static long last_wheel_post = 0;
68 /* Repeat is used for the scrollwheel acceleration. If high enough then
69 * jump over some items */
70 static unsigned repeat = 0;
71 /* we omit 1 of 2 posts to the button_queue, that works better, so count */
72 static int counter = 0;
73 /* Read wheel
74 * Bits 13 and 14 of DBOP_DIN change as follows:
75 * Clockwise rotation 00 -> 01 -> 11 -> 10 -> 00
76 * Counter-clockwise 00 -> 10 -> 11 -> 01 -> 00
78 static const unsigned char wheel_tbl[2][4] =
80 { 2, 0, 3, 1 }, /* Clockwise rotation */
81 { 1, 3, 0, 2 }, /* Counter-clockwise */
84 if(hold_button)
86 repeat = counter = 0;
87 return;
90 wheel_value = dbop_din & (1<<13|1<<14);
91 wheel_value >>= 13;
93 if (old_wheel_value == wheel_tbl[0][wheel_value])
94 btn = BUTTON_SCROLL_FWD;
95 else if (old_wheel_value == wheel_tbl[1][wheel_value])
96 btn = BUTTON_SCROLL_BACK;
98 if (btn != BUTTON_NONE)
100 if (btn != old_btn)
102 /* direction reversals nullify repeats */
103 old_btn = btn;
104 repeat = counter = 0;
106 /* wheel_delta will cause lists to jump over items,
107 * we want this for fast scrolling, but we must keep it accurate
108 * for slow scrolling */
109 int wheel_delta = 0;
110 /* generate repeats if quick enough, scroll slightly too */
111 if (TIME_BEFORE(current_tick, last_wheel_post + WHEEL_REPEAT_INTERVAL))
113 btn |= BUTTON_REPEAT;
114 wheel_delta = repeat>>1;
117 repeat += 3;
118 /* the wheel is more reliable if we don't send ever change,
119 * every 2th is basically one "physical click" is
120 * 1 item in the rockbox menus */
121 if (++counter >= 2 && queue_empty(&button_queue))
123 buttonlight_on();
124 backlight_on();
125 queue_post(&button_queue, btn, ((wheel_delta+1)<<24));
126 /* message posted - reset count & last post to the queue */
127 counter = 0;
128 last_wheel_post = current_tick;
131 if (repeat > 0 && !read_missed)
132 repeat--;
134 old_wheel_value = wheel_value;
136 #endif /* !defined(BOOTLOADER) && defined(HAVE_SCROLLWHEEL) */
138 unsigned short button_read_dbop(void)
140 /*write a red pixel */
141 if (!lcd_button_support())
143 read_missed = true;
145 if (!read_missed)
147 /* Set up dbop for input */
148 while (!(DBOP_STAT & (1<<10))); /* Wait for fifo to empty */
149 DBOP_CTRL |= (1<<19); /* Tri-state DBOP on read cycle */
150 DBOP_CTRL &= ~(1<<16); /* disable output (1:write enabled) */
151 DBOP_TIMPOL_01 = 0xe167e167; /* Set Timing & Polarity regs 0 & 1 */
152 DBOP_TIMPOL_23 = 0xe167006e; /* Set Timing & Polarity regs 2 & 3 */
154 DBOP_CTRL |= (1<<15); /* start read */
155 while (!(DBOP_STAT & (1<<16))); /* wait for valid data */
157 int delay=10;
158 while(delay--); /* short delay before reading */
160 _dbop_din = DBOP_DIN; /* Read dbop data*/
162 /* Reset dbop for output */
163 DBOP_TIMPOL_01 = 0x6e167; /* Set Timing & Polarity regs 0 & 1 */
164 DBOP_TIMPOL_23 = 0xa167e06f; /* Set Timing & Polarity regs 2 & 3 */
165 DBOP_CTRL |= (1<<16); /* Enable output (0:write disable) */
166 DBOP_CTRL &= ~(1<<19); /* Tri-state when no active write */
168 #if !defined(BOOTLOADER) && defined(HAVE_SCROLLWHEEL)
169 scrollwheel(_dbop_din);
170 #endif
171 read_missed = false;
172 return _dbop_din;
175 unsigned short button_dbop_data(void)
177 return _dbop_din;
181 * Get button pressed from hardware
183 int button_read_device(void)
185 int btn = BUTTON_NONE;
186 /* read buttons from dbop */
187 unsigned short dbop = button_read_dbop();
189 /* hold button */
190 if(dbop & (1<<12))
192 hold_button = true;
193 return btn;
195 else
197 hold_button = false;
200 if (dbop & (1<<8))
201 btn |= BUTTON_POWER;
203 if (!(dbop & (1<<15)))
204 btn |= BUTTON_REC;
206 /* Set afsel, so that we can read our buttons */
207 GPIOC_AFSEL &= ~(1<<2|1<<3|1<<4|1<<5|1<<6);
208 /* set dir so we can read our buttons (but reset the C pins first) */
209 GPIOB_DIR &= ~(1<<4);
210 GPIOC_DIR |= (1<<2|1<<3|1<<4|1<<5|1<<6);
211 GPIOC_PIN(2) = (1<<2);
212 GPIOC_PIN(3) = (1<<3);
213 GPIOC_PIN(4) = (1<<4);
214 GPIOC_PIN(5) = (1<<5);
215 GPIOC_PIN(6) = (1<<6);
217 GPIOC_DIR &= ~(1<<2|1<<3|1<<4|1<<5|1<<6);
219 int delay = 8; /* small delay needed to read buttons correctly */
220 while(delay--);
222 /* direct GPIO connections */
223 if (!GPIOC_PIN(2))
224 btn |= BUTTON_UP;
225 if (!GPIOC_PIN(3))
226 btn |= BUTTON_LEFT;
227 if (!GPIOC_PIN(4))
228 btn |= BUTTON_SELECT;
229 if (!GPIOC_PIN(5))
230 btn |= BUTTON_RIGHT;
231 if (!GPIOC_PIN(6))
232 btn |= BUTTON_DOWN;
234 /* return to settings needed for lcd */
235 GPIOC_DIR |= (1<<2|1<<3|1<<4|1<<5|1<<6);
236 GPIOC_AFSEL |= (1<<2|1<<3|1<<4|1<<5|1<<6);
238 #ifndef BOOTLOADER
239 /* light handling */
240 if (hold_button != hold_button_old)
242 hold_button_old = hold_button;
243 backlight_hold_changed(hold_button);
245 #endif /* BOOTLOADER */
246 /* The int_btn variable is set in the button interrupt handler */
247 return btn;