Fuzev2: fix screen corruption
[kugel-rb.git] / firmware / target / arm / as3525 / sansa-fuzev2 / button-fuzev2.c
blobd50df9f1c62b72bc7f79b5d7e21aa5e49cd3cbc6
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 volatile int delay;
62 static bool hold_button_old = false;
63 static long power_counter = 0;
64 unsigned gpiod6;
66 /* if we remove this delay, we see screen corruption (the higher the CPU
67 * frequency the higher the corruption) */
68 for(delay = 1000; delay; delay--)
69 nop;
71 get_scrollwheel();
73 CCU_IO &= ~(1<<12);
75 GPIOB_PIN(0) = 1<<0;
76 for(delay = 500; delay; delay--)
77 nop;
79 gpiod6 = GPIOD_PIN(6);
81 GPIOB_PIN(0) = 0;
82 for(delay = 240; delay; delay--)
83 nop;
85 if (GPIOC_PIN(1) & 1<<1)
86 btn |= BUTTON_DOWN;
87 if (GPIOC_PIN(2) & 1<<2)
88 btn |= BUTTON_UP;
89 if (GPIOC_PIN(3) & 1<<3)
90 btn |= BUTTON_LEFT;
91 if (GPIOC_PIN(4) & 1<<4)
92 btn |= BUTTON_SELECT;
93 if (GPIOC_PIN(5) & 1<<5)
94 btn |= BUTTON_RIGHT;
95 if (GPIOB_PIN(1) & 1<<1)
96 btn |= BUTTON_HOME;
97 if (gpiod6 & 1<<6)
98 { /* power/hold is on the same pin. we know it's hold if the bit isn't
99 * set now anymore */
100 if (GPIOD_PIN(6) & 1<<6)
102 hold_button = false;
103 btn |= BUTTON_POWER;
105 else
107 hold_button = true;
111 CCU_IO |= 1<<12;
113 #ifdef HAS_BUTTON_HOLD
114 #ifndef BOOTLOADER
115 /* light handling */
116 if (hold_button != hold_button_old)
118 hold_button_old = hold_button;
119 backlight_hold_changed(hold_button);
121 #else
122 (void)hold_button_old;
123 #endif
124 if (hold_button)
126 power_counter = HZ;
127 return 0;
129 /* read power, but not if hold button was just released, since
130 * you basically always hit power due to the slider mechanism after releasing
131 * (fuze only)
133 else if (power_counter > 0)
135 power_counter--;
136 btn &= ~BUTTON_POWER;
138 #endif
139 return btn;
142 #ifdef HAS_BUTTON_HOLD
143 bool button_hold(void)
145 return hold_button;
147 #endif