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>
50 #include <mware/event.h>
54 /* Configuration sanity checks */
55 #if !defined(CONFIG_KBD_POLL) || (CONFIG_KBD_POLL != KBD_POLL_SOFTINT)
56 #error CONFIG_KBD_POLL must be defined to either KBD_POLL_SOFTINT
58 #if !defined(CONFIG_KBD_BEEP) || (CONFIG_KBD_BEEP != 0 && CONFIG_KBD_BEEP != 1)
59 #error CONFIG_KBD_BEEP must be defined to either 0 or 1
61 #if !defined(CONFIG_KBD_OBSERVER) || (CONFIG_KBD_OBSERVER != 0 && CONFIG_KBD_OBSERVER != 1)
62 #error CONFIG_KBD_OBSERVER must be defined to either 0 or 1
64 #if !defined(CONFIG_KBD_LONGPRESS) || (CONFIG_KBD_LONGPRESS != 0 && CONFIG_KBD_LONGPRESS != 1)
65 #error CONFIG_KBD_LONGPRESS must be defined to either 0 or 1
69 #include <drv/buzzer.h>
72 #define KBD_CHECK_INTERVAL 10 /**< (ms) Timing for kbd softint */
73 #define KBD_DEBOUNCE_TIME 30 /**< (ms) Debounce time */
74 #define KBD_BEEP_TIME 5 /**< (ms) Duration of keybeep */
76 #define KBD_REPEAT_DELAY 400 /**< (ms) Keyboard repeat delay for first character */
77 #define KBD_REPEAT_RATE 100 /**< (ms) Initial interchar delay for keyboard repeat */
78 #define KBD_REPEAT_MAXRATE 20 /**< (ms) Minimum delay for keyboard repeat */
79 #define KBD_REPEAT_ACCEL 5 /**< (ms) Keyboard repeat speed increase */
81 #define KBD_LNG_DELAY 1000 /**< (ms) Keyboard long pression keys delay */
84 /** Status for keyboard repeat state machine */
85 static enum { KS_IDLE, KS_REPDELAY, KS_REPEAT } kbd_rptStatus;
87 /** Used to notify the occurrence of a key pressed event */
88 static Event key_pressed;
90 static volatile keymask_t kbd_buf; /**< Single entry keyboard buffer */
91 static volatile keymask_t kbd_cnt; /**< Number of keypress events in \c kbd_buf */
92 static keymask_t kbd_rpt_mask; /**< Mask of repeatable keys. */
94 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
95 static Timer kbd_timer; /**< Keyboard softtimer */
98 static List kbd_rawHandlers; /**< Raw keyboard handlers */
99 static List kbd_handlers; /**< Cooked keyboard handlers */
101 static KbdHandler kbd_defHandler; /**< The default keyboard handler */
102 static KbdHandler kbd_debHandler; /**< The debounce keyboard handler */
103 static KbdHandler kbd_rptHandler; /**< Auto-repeat keyboard handler */
105 #if CONFIG_KBD_LONGPRESS
106 static KbdHandler kbd_lngHandler; /**< Long pression keys handler */
109 #if CONFIG_KBD_OBSERVER
110 #include <mware/observer.h>
116 * Poll keyboard and dispatch keys to handlers.
118 * Read the key states and invoke all keyboard
119 * handlers to process the new state.
121 * Call this function periodically using a software
122 * timer, an interrupt or a process.
124 static void kbd_poll(void)
126 /** Currently depressed key */
127 static keymask_t current_key;
129 struct KbdHandler *handler;
130 keymask_t key = kbd_readkeys();
132 /* Call raw input handlers */
133 FOREACH_NODE(handler, &kbd_rawHandlers)
134 key = handler->hook(key);
136 /* If this key was not previously pressed */
137 if (key != current_key)
139 /* Remember last key */
142 /* Call cooked input handlers */
143 FOREACH_NODE(handler, &kbd_handlers)
144 key = handler->hook(key);
148 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
151 * Keyboard soft-irq handler.
153 static void kbd_softint(UNUSED_ARG(iptr_t, arg))
156 timer_add(&kbd_timer);
160 #error "Define keyboard poll method"
162 #endif /* CONFIG_KBD_POLL */
165 * \brief Read a key from the keyboard buffer.
167 * When a key is kept depressed between calls of this function a value
168 * is returned only after the time specified with KBD_REPAT_DELAY to
169 * avoid too fast keyboard repeat.
171 * \note Calls \c schedule() internally.
173 * \note This function is \b not interrupt safe!
175 * \return The mask of depressed keys or 0 if no keys are depressed.
178 keymask_t kbd_peek(void)
183 /* Let other tasks run for a while */
184 extern void schedule(void);
188 /* Extract an event from the keyboard buffer */
201 * Wait for a keypress and return the mask of depressed keys.
203 * \note This function is \b not interrupt safe!
205 keymask_t kbd_get(void)
209 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
210 event_wait(&key_pressed);
213 while (!(key = kbd_peek()))
222 * Wait up to \c timeout ms for a keypress
223 * and return the mask of depressed keys, or K_TIMEOUT
224 * if the timeout was reacked.
226 keymask_t kbd_get_timeout(mtime_t timeout)
228 if (event_waitTimeout(&key_pressed, timeout))
235 void kbd_addHandler(struct KbdHandler *handler)
241 IRQ_SAVE_DISABLE(flags);
243 /* Choose between raw and coocked handlers list */
244 list = (handler->flags & KHF_RAWKEYS) ?
245 &kbd_rawHandlers : &kbd_handlers;
248 * Search for the first node whose priority
249 * is lower than the timer we want to add.
251 FOREACH_NODE(node,list)
252 if (node->pri < handler->pri)
255 /* Enqueue handler in the handlers chain */
256 INSERT_BEFORE(&handler->link, &node->link);
262 void kbd_remHandler(struct KbdHandler *handler)
264 /* Remove the handler */
265 ATOMIC(REMOVE(&handler->link));
270 * This is the default key handler, called after
271 * all other handlers have had their chance to
272 * do their special processing. This handler
273 * pushes all input in the keyboard FIFO buffer.
275 static keymask_t kbd_defHandlerFunc(keymask_t key)
279 /* Force a single event in kbd buffer */
282 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
283 event_do(&key_pressed);
286 #if CONFIG_KBD_OBSERVER
287 observer_notify(&kbd_subject, KBD_EVENT_KEY, &key);
291 if (!(key & K_REPEAT))
292 buz_beep(KBD_BEEP_TIME);
301 * Handle keyboard debounce
303 static keymask_t kbd_debHandlerFunc(keymask_t key)
305 /** Buffer for debounce */
306 static keymask_t debounce_key;
308 /** Timer for keyboard debounce */
309 static ticks_t debounce_time;
311 /** Key aquired after debounce */
312 static keymask_t new_key;
315 ticks_t now = timer_clock();
317 if (key != debounce_key)
319 /* Reset debounce timer */
323 else if ((new_key != debounce_key)
324 && (now - debounce_time > ms_to_ticks(KBD_DEBOUNCE_TIME)))
326 new_key = debounce_key;
333 #if CONFIG_KBD_LONGPRESS
335 * Handle long pression keys.
337 static keymask_t kbd_lngHandlerFunc(keymask_t key)
339 static ticks_t start;
340 ticks_t now = timer_clock();
342 if (key & K_LNG_MASK)
344 if (now - start > ms_to_ticks(KBD_LNG_DELAY))
354 * Set current mask of repeatable keys.
356 keymask_t kbd_setRepeatMask(keymask_t mask)
358 keymask_t oldmask = kbd_rpt_mask;
359 ATOMIC(kbd_rpt_mask = mask);
364 * Handle keyboard repeat
366 static keymask_t kbd_rptHandlerFunc(keymask_t key)
368 /* Timer for keyboard repeat events. */
369 static ticks_t repeat_time;
371 /* Current repeat rate (for acceleration). */
372 static ticks_t repeat_rate; /** Current repeat rate (for acceleration) */
374 ticks_t now = timer_clock();
376 switch (kbd_rptStatus)
379 if (key & kbd_rpt_mask)
382 kbd_rptStatus = KS_REPDELAY;
387 if (key & kbd_rpt_mask)
389 if (now - repeat_time > ms_to_ticks(KBD_REPEAT_DELAY))
391 key = (key & kbd_rpt_mask) | K_REPEAT;
393 repeat_rate = ms_to_ticks(KBD_REPEAT_RATE);
394 kbd_rptStatus = KS_REPEAT;
400 kbd_rptStatus = KS_IDLE;
404 if (key & kbd_rpt_mask)
406 if (now - repeat_time > repeat_rate)
408 /* Enqueue a new event in the buffer */
409 key = (key & kbd_rpt_mask) | K_REPEAT;
412 /* Repeat rate acceleration */
413 if (repeat_rate > ms_to_ticks(KBD_REPEAT_MAXRATE))
414 repeat_rate -= ms_to_ticks(KBD_REPEAT_ACCEL);
420 kbd_rptStatus = KS_IDLE;
432 * Initialize keyboard ports and softtimer
442 /* Init handlers lists */
443 LIST_INIT(&kbd_handlers);
444 LIST_INIT(&kbd_rawHandlers);
446 /* Add debounce keyboard handler */
447 kbd_debHandler.hook = kbd_debHandlerFunc;
448 kbd_debHandler.pri = 100; /* high priority */
449 kbd_debHandler.flags = KHF_RAWKEYS;
450 kbd_addHandler(&kbd_debHandler);
452 #if CONFIG_KBD_LONGPRESS
453 /* Add long pression keyboard handler */
454 kbd_lngHandler.hook = kbd_lngHandlerFunc;
455 kbd_lngHandler.pri = 90; /* high priority */
456 kbd_lngHandler.flags = KHF_RAWKEYS;
457 kbd_addHandler(&kbd_lngHandler);
460 /* Add repeat keyboard handler */
461 kbd_rptHandler.hook = kbd_rptHandlerFunc;
462 kbd_rptHandler.pri = 80; /* high priority */
463 kbd_rptHandler.flags = KHF_RAWKEYS;
464 kbd_addHandler(&kbd_rptHandler);
466 /* Add default keyboard handler */
467 kbd_defHandler.hook = kbd_defHandlerFunc;
468 kbd_defHandler.pri = -128; /* lowest priority */
469 kbd_addHandler(&kbd_defHandler);
471 #if CONFIG_KBD_OBSERVER
472 observer_InitSubject(&kbd_subject);
475 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
482 /* Initialize the keyboard event (key pressed) */
483 event_initGeneric(&key_pressed);
485 /* Add kbd handler to soft timers list */
486 event_initSoftint(&kbd_timer.expire, kbd_softint, NULL);
487 timer_setDelay(&kbd_timer, ms_to_ticks(KBD_CHECK_INTERVAL));
488 timer_add(&kbd_timer);
491 #error "Define keyboard poll method"