[U-Boot] [PATCH v4 4/5] samsung: misc: keys: fix gpio key bouncing by adding 50 ms delay

Gerhard Sittig
2014-04-28T14:03:28Z

On Mon, 2014-04-28 at 15:44 +0200, Gerhard Sittig wrote:
>
> [ ... ] What you
> need is some kind of "trigger" where you notice that the line
> levels are changing, and a delayed fetch of the lines' values
> after they have settled. Without the first condition, you always
> have the risk of sampling arbitrary data that does not reflect
> the keys' status.

Let me mention another approach here for completeness.

You might want to fetch samples into some buffer (a "FIFO" of a
certain depth), and only pass along "pressed" or "released"
conditions when a certain number of successive samples have the
same level and this level is different from what you have passed
along previously. That is, you only report changes in the keys'
condition after they have become stable.

This approach might be the least intrusive in the context of the
routine that you modify in your patch. And it can be acceptably
cheap both in terms of computation as well as in storage.

bool last_reported = 0;
int last_sampled = 0;

last_sampled <<= 1;
last_sampled &= 0xfff;
last_sampled |= (key_pressed() ? 1 : 0;

if (last_reported && last_sampled == 0) {
/* report the "released" edge */
last_reported = false;
}
if (!last_reported && last_sampled == 0xfff) {
/* report the "pressed" edge */
last_reported = true;
}
/* other cases either are no changes, or changes that
* have not yet become stable */

Just pick a depth and sampling frequency that matches the
characteristics of the mechanical bouncing of the switches.
Several ten milliseconds is a good estimate (and most users
won't notice the delay). This might be a useful helper for
other boards to use, too.

virtually yours
Gerhard Sittig
--
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr. 5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-0 Fax: +49-8142-66989-80 Email: office@denx.de