4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 1999, 2003 Bernie Innocenti
34 * \brief Keyboard driver (implementation)
37 * \author Bernie Innocenti <bernie@codewiz.org>
38 * \author Stefano Fedrigo <aleph@develer.com>
39 * \author Francesco Sacchi <batt@develer.com>
43 #include "hw/hw_kbd.h"
45 #include "cfg/cfg_kbd.h"
46 #include <cfg/debug.h>
47 #include <cfg/module.h>
49 #include <drv/timer.h>
53 /* Configuration sanity checks */
54 #if !defined(CONFIG_KBD_POLL) || (CONFIG_KBD_POLL != KBD_POLL_SOFTINT)
55 #error CONFIG_KBD_POLL must be defined to either KBD_POLL_SOFTINT
57 #if !defined(CONFIG_KBD_BEEP) || (CONFIG_KBD_BEEP != 0 && CONFIG_KBD_BEEP != 1)
58 #error CONFIG_KBD_BEEP must be defined to either 0 or 1
60 #if !defined(CONFIG_KBD_OBSERVER) || (CONFIG_KBD_OBSERVER != 0 && CONFIG_KBD_OBSERVER != 1)
61 #error CONFIG_KBD_OBSERVER must be defined to either 0 or 1
63 #if !defined(CONFIG_KBD_LONGPRESS) || (CONFIG_KBD_LONGPRESS != 0 && CONFIG_KBD_LONGPRESS != 1)
64 #error CONFIG_KBD_LONGPRESS must be defined to either 0 or 1
68 #include <drv/buzzer.h>
71 #define KBD_CHECK_INTERVAL 10 /**< (ms) Timing for kbd softint */
72 #define KBD_DEBOUNCE_TIME 30 /**< (ms) Debounce time */
73 #define KBD_BEEP_TIME 5 /**< (ms) Duration of keybeep */
75 #define KBD_REPEAT_DELAY 400 /**< (ms) Keyboard repeat delay for first character */
76 #define KBD_REPEAT_RATE 100 /**< (ms) Initial interchar delay for keyboard repeat */
77 #define KBD_REPEAT_MAXRATE 20 /**< (ms) Minimum delay for keyboard repeat */
78 #define KBD_REPEAT_ACCEL 5 /**< (ms) Keyboard repeat speed increase */
80 #define KBD_LNG_DELAY 1000 /**< (ms) Keyboard long pression keys delay */
83 /** Status for keyboard repeat state machine */
84 static enum { KS_IDLE, KS_REPDELAY, KS_REPEAT } kbd_rptStatus;
87 static volatile keymask_t kbd_buf; /**< Single entry keyboard buffer */
88 static volatile keymask_t kbd_cnt; /**< Number of keypress events in \c kbd_buf */
89 static keymask_t kbd_rpt_mask; /**< Mask of repeatable keys. */
91 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
92 static Timer kbd_timer; /**< Keyboard softtimer */
95 static List kbd_rawHandlers; /**< Raw keyboard handlers */
96 static List kbd_handlers; /**< Cooked keyboard handlers */
98 static KbdHandler kbd_defHandler; /**< The default keyboard handler */
99 static KbdHandler kbd_debHandler; /**< The debounce keyboard handler */
100 static KbdHandler kbd_rptHandler; /**< Auto-repeat keyboard handler */
102 #if CONFIG_KBD_LONGPRESS
103 static KbdHandler kbd_lngHandler; /**< Long pression keys handler */
106 #if CONFIG_KBD_OBSERVER
107 #include <mware/observer.h>
113 * Poll keyboard and dispatch keys to handlers.
115 * Read the key states and invoke all keyboard
116 * handlers to process the new state.
118 * Call this function periodically using a software
119 * timer, an interrupt or a process.
121 static void kbd_poll(void)
123 /** Currently depressed key */
124 static keymask_t current_key;
126 struct KbdHandler *handler;
127 keymask_t key = kbd_readkeys();
129 /* Call raw input handlers */
130 FOREACH_NODE(handler, &kbd_rawHandlers)
131 key = handler->hook(key);
133 /* If this key was not previously pressed */
134 if (key != current_key)
136 /* Remember last key */
139 /* Call cooked input handlers */
140 FOREACH_NODE(handler, &kbd_handlers)
141 key = handler->hook(key);
145 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
148 * Keyboard soft-irq handler.
150 static void kbd_softint(UNUSED_ARG(iptr_t, arg))
153 timer_add(&kbd_timer);
157 #error "Define keyboard poll method"
159 #endif /* CONFIG_KBD_POLL */
162 * \brief Read a key from the keyboard buffer.
164 * When a key is kept depressed between calls of this function a value
165 * is returned only after the time specified with KBD_REPAT_DELAY to
166 * avoid too fast keyboard repeat.
168 * \note Calls \c schedule() internally.
170 * \note This function is \b not interrupt safe!
172 * \return The mask of depressed keys or 0 if no keys are depressed.
175 keymask_t kbd_peek(void)
180 /* Let other tasks run for a while */
181 extern void schedule(void);
185 /* Extract an event from the keyboard buffer */
198 * Wait for a keypress and return the mask of depressed keys.
200 * \note This function is \b not interrupt safe!
202 keymask_t kbd_get(void)
206 while (!(key = kbd_peek())) {}
213 * Wait up to \c timeout ms for a keypress
214 * and return the mask of depressed keys, or K_TIMEOUT
215 * if the timeout was reacked.
217 keymask_t kbd_get_timeout(mtime_t timeout)
221 ticks_t start = timer_clock();
222 ticks_t stop = ms_to_ticks(timeout);
225 if ((key = kbd_peek()))
228 while (timer_clock() - start < stop);
234 void kbd_addHandler(struct KbdHandler *handler)
240 IRQ_SAVE_DISABLE(flags);
242 /* Choose between raw and coocked handlers list */
243 list = (handler->flags & KHF_RAWKEYS) ?
244 &kbd_rawHandlers : &kbd_handlers;
247 * Search for the first node whose priority
248 * is lower than the timer we want to add.
250 FOREACH_NODE(node,list)
251 if (node->pri < handler->pri)
254 /* Enqueue handler in the handlers chain */
255 INSERT_BEFORE(&handler->link, &node->link);
261 void kbd_remHandler(struct KbdHandler *handler)
263 /* Remove the handler */
264 ATOMIC(REMOVE(&handler->link));
269 * This is the default key handler, called after
270 * all other handlers have had their chance to
271 * do their special processing. This handler
272 * pushes all input in the keyboard FIFO buffer.
274 static keymask_t kbd_defHandlerFunc(keymask_t key)
278 /* Force a single event in kbd buffer */
282 #if CONFIG_KBD_OBSERVER
283 observer_notify(&kbd_subject, KBD_EVENT_KEY, &key);
287 if (!(key & K_REPEAT))
288 buz_beep(KBD_BEEP_TIME);
297 * Handle keyboard debounce
299 static keymask_t kbd_debHandlerFunc(keymask_t key)
301 /** Buffer for debounce */
302 static keymask_t debounce_key;
304 /** Timer for keyboard debounce */
305 static ticks_t debounce_time;
307 /** Key aquired after debounce */
308 static keymask_t new_key;
311 ticks_t now = timer_clock();
313 if (key != debounce_key)
315 /* Reset debounce timer */
319 else if ((new_key != debounce_key)
320 && (now - debounce_time > ms_to_ticks(KBD_DEBOUNCE_TIME)))
322 new_key = debounce_key;
329 #if CONFIG_KBD_LONGPRESS
331 * Handle long pression keys.
333 static keymask_t kbd_lngHandlerFunc(keymask_t key)
335 static ticks_t start;
336 ticks_t now = timer_clock();
338 if (key & K_LNG_MASK)
340 if (now - start > ms_to_ticks(KBD_LNG_DELAY))
350 * Set current mask of repeatable keys.
352 keymask_t kbd_setRepeatMask(keymask_t mask)
354 keymask_t oldmask = kbd_rpt_mask;
355 ATOMIC(kbd_rpt_mask = mask);
360 * Handle keyboard repeat
362 static keymask_t kbd_rptHandlerFunc(keymask_t key)
364 /* Timer for keyboard repeat events. */
365 static ticks_t repeat_time;
367 /* Current repeat rate (for acceleration). */
368 static ticks_t repeat_rate; /** Current repeat rate (for acceleration) */
370 ticks_t now = timer_clock();
372 switch (kbd_rptStatus)
375 if (key & kbd_rpt_mask)
378 kbd_rptStatus = KS_REPDELAY;
383 if (key & kbd_rpt_mask)
385 if (now - repeat_time > ms_to_ticks(KBD_REPEAT_DELAY))
387 key = (key & kbd_rpt_mask) | K_REPEAT;
389 repeat_rate = ms_to_ticks(KBD_REPEAT_RATE);
390 kbd_rptStatus = KS_REPEAT;
396 kbd_rptStatus = KS_IDLE;
400 if (key & kbd_rpt_mask)
402 if (now - repeat_time > repeat_rate)
404 /* Enqueue a new event in the buffer */
405 key = (key & kbd_rpt_mask) | K_REPEAT;
408 /* Repeat rate acceleration */
409 if (repeat_rate > ms_to_ticks(KBD_REPEAT_MAXRATE))
410 repeat_rate -= ms_to_ticks(KBD_REPEAT_ACCEL);
416 kbd_rptStatus = KS_IDLE;
428 * Initialize keyboard ports and softtimer
438 /* Init handlers lists */
439 LIST_INIT(&kbd_handlers);
440 LIST_INIT(&kbd_rawHandlers);
442 /* Add debounce keyboard handler */
443 kbd_debHandler.hook = kbd_debHandlerFunc;
444 kbd_debHandler.pri = 100; /* high priority */
445 kbd_debHandler.flags = KHF_RAWKEYS;
446 kbd_addHandler(&kbd_debHandler);
448 #if CONFIG_KBD_LONGPRESS
449 /* Add long pression keyboard handler */
450 kbd_lngHandler.hook = kbd_lngHandlerFunc;
451 kbd_lngHandler.pri = 90; /* high priority */
452 kbd_lngHandler.flags = KHF_RAWKEYS;
453 kbd_addHandler(&kbd_lngHandler);
456 /* Add repeat keyboard handler */
457 kbd_rptHandler.hook = kbd_rptHandlerFunc;
458 kbd_rptHandler.pri = 80; /* high priority */
459 kbd_rptHandler.flags = KHF_RAWKEYS;
460 kbd_addHandler(&kbd_rptHandler);
462 /* Add default keyboard handler */
463 kbd_defHandler.hook = kbd_defHandlerFunc;
464 kbd_defHandler.pri = -128; /* lowest priority */
465 kbd_addHandler(&kbd_defHandler);
467 #if CONFIG_KBD_OBSERVER
468 observer_InitSubject(&kbd_subject);
471 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
475 /* Add kbd handler to soft timers list */
476 event_initSoftint(&kbd_timer.expire, kbd_softint, NULL);
477 timer_setDelay(&kbd_timer, ms_to_ticks(KBD_CHECK_INTERVAL));
478 timer_add(&kbd_timer);
481 #error "Define keyboard poll method"