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 Bernardo Innocenti
36 * \author Bernardo Innocenti <bernie@develer.com>
37 * \author Stefano Fedrigo <aleph@develer.com>
38 * \author Francesco Sacchi <batt@develer.com>
40 * \brief Keyboard driver (implementation)
45 *#* Revision 1.9 2006/09/13 18:25:45 bernie
46 *#* Merge CONFIG_KBD_LONGPRESS.
48 *#* Revision 1.8 2006/07/19 12:56:25 bernie
49 *#* Convert to new Doxygen style.
51 *#* Revision 1.7 2006/06/03 13:57:36 bernie
52 *#* Make keyboard repeat mask run-time configurable.
54 *#* Revision 1.6 2006/03/20 17:50:17 bernie
55 *#* Add FreeRTOS and Observers support.
57 *#* Revision 1.5 2006/02/27 22:39:45 bernie
58 *#* Misc build and doc fixes from project_grl.
60 *#* Revision 1.4 2006/02/24 00:27:14 bernie
61 *#* Use new naming convention for list macros.
63 *#* Revision 1.3 2006/02/17 21:15:42 bernie
64 *#* Add MOD_CHECK() checks.
66 *#* Revision 1.2 2006/02/10 12:36:20 bernie
67 *#* Add preliminary FreeRTOS support; Enforce CONFIG_* definitions.
69 *#* Revision 1.1 2005/06/27 21:28:45 bernie
70 *#* Import generic keyboard driver.
76 #include <drv/timer.h>
79 #include <cfg/debug.h>
80 #include <cfg/module.h>
81 #include <appconfig.h>
83 /* Configuration sanity checks */
84 #if !defined(CONFIG_KBD_POLL) || (CONFIG_KBD_POLL != KBD_POLL_SOFTINT && CONFIG_KBD_POLL != CONFIG_POLL_FREERTOS)
85 #error CONFIG_KBD_POLL must be defined to either KBD_POLL_SOFTINT or CONFIG_POLL_FREERTOS
87 #if !defined(CONFIG_KBD_BEEP) || (CONFIG_KBD_BEEP != 0 && CONFIG_KBD_BEEP != 1)
88 #error CONFIG_KBD_BEEP must be defined to either 0 or 1
90 #if !defined(CONFIG_KBD_OBSERVER) || (CONFIG_KBD_OBSERVER != 0 && CONFIG_KBD_OBSERVER != 1)
91 #error CONFIG_KBD_OBSERVER must be defined to either 0 or 1
93 #if !defined(CONFIG_KBD_LONGPRESS) || (CONFIG_KBD_LONGPRESS != 0 && CONFIG_KBD_LONGPRESS != 1)
94 #error CONFIG_KBD_LONGPRESS must be defined to either 0 or 1
98 #include <drv/buzzer.h>
101 #define KBD_CHECK_INTERVAL 10 /**< (ms) Timing for kbd softint */
102 #define KBD_DEBOUNCE_TIME 30 /**< (ms) Debounce time */
103 #define KBD_BEEP_TIME 5 /**< (ms) Duration of keybeep */
105 #define KBD_REPEAT_DELAY 400 /**< (ms) Keyboard repeat delay for first character */
106 #define KBD_REPEAT_RATE 100 /**< (ms) Initial interchar delay for keyboard repeat */
107 #define KBD_REPEAT_MAXRATE 20 /**< (ms) Minimum delay for keyboard repeat */
108 #define KBD_REPEAT_ACCEL 5 /**< (ms) Keyboard repeat speed increase */
110 #define KBD_LNG_DELAY 1000 /**< (ms) Keyboard long pression keys delay */
113 /** Status for keyboard repeat state machine */
114 static enum { KS_IDLE, KS_REPDELAY, KS_REPEAT } kbd_rptStatus;
117 static volatile keymask_t kbd_buf; /**< Single entry keyboard buffer */
118 static volatile keymask_t kbd_cnt; /**< Number of keypress events in \c kbd_buf */
119 static keymask_t kbd_rpt_mask; /**< Mask of repeatable keys. */
121 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
122 static Timer kbd_timer; /**< Keyboard softtimer */
125 static List kbd_rawHandlers; /**< Raw keyboard handlers */
126 static List kbd_handlers; /**< Cooked keyboard handlers */
128 static KbdHandler kbd_defHandler; /**< The default keyboard handler */
129 static KbdHandler kbd_debHandler; /**< The debounce keyboard handler */
130 static KbdHandler kbd_rptHandler; /**< Auto-repeat keyboard handler */
132 #if CONFIG_KBD_LONGPRESS
133 static KbdHandler kbd_lngHandler; /**< Long pression keys handler */
136 #if CONFIG_KBD_OBSERVER
137 #include <mware/observer.h>
143 * Poll keyboard and dispatch keys to handlers.
145 * Read the key states and invoke all keyboard
146 * handlers to process the new state.
148 * Call this function periodically using a software
149 * timer, an interrupt or a process.
151 static void kbd_poll(void)
153 /** Currently depressed key */
154 static keymask_t current_key;
156 struct KbdHandler *handler;
157 keymask_t key = kbd_readkeys();
159 /* Call raw input handlers */
160 FOREACH_NODE(handler, &kbd_rawHandlers)
161 key = handler->hook(key);
163 /* If this key was not previously pressed */
164 if (key != current_key)
166 /* Remember last key */
169 /* Call cooked input handlers */
170 FOREACH_NODE(handler, &kbd_handlers)
171 key = handler->hook(key);
175 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
178 * Keyboard soft-irq handler.
180 static void kbd_softint(UNUSED_ARG(iptr_t, arg))
183 timer_add(&kbd_timer);
186 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
188 #include "FreeRTOS.h"
191 static portTASK_FUNCTION(kbd_task, arg)
196 timer_delay(KBD_CHECK_INTERVAL);
200 #endif /* CONFIG_KBD_POLL */
203 * \brief Read a key from the keyboard buffer.
205 * When a key is kept depressed between calls of this function a value
206 * is returned only after the time specified with KBD_REPAT_DELAY to
207 * avoid too fast keyboard repeat.
209 * \note Calls \c schedule() internally.
211 * \note This function is \b not interrupt safe!
213 * \return The mask of depressed keys or 0 if no keys are depressed.
216 keymask_t kbd_peek(void)
220 // FIXME: make it optional
221 /* Let other tasks run for a while */
222 extern void schedule(void);
225 /* Extract an event from the keyboard buffer */
238 * Wait for a keypress and return the mask of depressed keys.
240 * \note This function is \b not interrupt safe!
242 keymask_t kbd_get(void)
246 while (!(key = kbd_peek())) {}
253 * Wait up to \c timeout ms for a keypress
254 * and return the mask of depressed keys, or K_TIMEOUT
255 * if the timeout was reacked.
257 keymask_t kbd_get_timeout(mtime_t timeout)
261 ticks_t start = timer_clock();
262 ticks_t stop = ms_to_ticks(timeout);
265 if ((key = kbd_peek()))
268 while (timer_clock() - start < stop);
274 void kbd_addHandler(struct KbdHandler *handler)
280 IRQ_SAVE_DISABLE(flags);
282 /* Choose between raw and coocked handlers list */
283 list = (handler->flags & KHF_RAWKEYS) ?
284 &kbd_rawHandlers : &kbd_handlers;
287 * Search for the first node whose priority
288 * is lower than the timer we want to add.
290 FOREACH_NODE(node,list)
291 if (node->pri < handler->pri)
294 /* Enqueue handler in the handlers chain */
295 INSERT_BEFORE(&handler->link, &node->link);
301 void kbd_remHandler(struct KbdHandler *handler)
303 /* Remove the handler */
304 ATOMIC(REMOVE(&handler->link));
309 * This is the default key handler, called after
310 * all other handlers have had their chance to
311 * do their special processing. This handler
312 * pushes all input in the keyboard FIFO buffer.
314 static keymask_t kbd_defHandlerFunc(keymask_t key)
318 /* Force a single event in kbd buffer */
322 #if CONFIG_KBD_OBSERVER
323 observer_notify(&kbd_subject, KBD_EVENT_KEY, &key);
327 if (!(key & K_REPEAT))
328 buz_beep(KBD_BEEP_TIME);
337 * Handle keyboard debounce
339 static keymask_t kbd_debHandlerFunc(keymask_t key)
341 /** Buffer for debounce */
342 static keymask_t debounce_key;
344 /** Timer for keyboard debounce */
345 static ticks_t debounce_time;
347 /** Key aquired after debounce */
348 static keymask_t new_key;
351 ticks_t now = timer_clock();
353 if (key != debounce_key)
355 /* Reset debounce timer */
359 else if ((new_key != debounce_key)
360 && (now - debounce_time > ms_to_ticks(KBD_DEBOUNCE_TIME)))
362 new_key = debounce_key;
369 #if CONFIG_KBD_LONGPRESS
371 * Handle long pression keys.
373 static keymask_t kbd_lngHandlerFunc(keymask_t key)
375 static ticks_t start;
376 ticks_t now = timer_clock();
378 if (key & K_LNG_MASK)
380 if (now - start > ms_to_ticks(KBD_LNG_DELAY))
390 * Set current mask of repeatable keys.
392 keymask_t kbd_setRepeatMask(keymask_t mask)
394 keymask_t oldmask = kbd_rpt_mask;
395 ATOMIC(kbd_rpt_mask = mask);
400 * Handle keyboard repeat
402 static keymask_t kbd_rptHandlerFunc(keymask_t key)
404 /* Timer for keyboard repeat events. */
405 static ticks_t repeat_time;
407 /* Current repeat rate (for acceleration). */
408 static ticks_t repeat_rate; /** Current repeat rate (for acceleration) */
410 ticks_t now = timer_clock();
412 switch (kbd_rptStatus)
415 if (key & kbd_rpt_mask)
418 kbd_rptStatus = KS_REPDELAY;
423 if (key & kbd_rpt_mask)
425 if (now - repeat_time > ms_to_ticks(KBD_REPEAT_DELAY))
427 key = (key & kbd_rpt_mask) | K_REPEAT;
429 repeat_rate = ms_to_ticks(KBD_REPEAT_RATE);
430 kbd_rptStatus = KS_REPEAT;
436 kbd_rptStatus = KS_IDLE;
440 if (key & kbd_rpt_mask)
442 if (now - repeat_time > repeat_rate)
444 /* Enqueue a new event in the buffer */
445 key = (key & kbd_rpt_mask) | K_REPEAT;
448 /* Repeat rate acceleration */
449 if (repeat_rate > ms_to_ticks(KBD_REPEAT_MAXRATE))
450 repeat_rate -= ms_to_ticks(KBD_REPEAT_ACCEL);
456 kbd_rptStatus = KS_IDLE;
468 * Initialize keyboard ports and softtimer
478 /* Init handlers lists */
479 LIST_INIT(&kbd_handlers);
480 LIST_INIT(&kbd_rawHandlers);
482 /* Add debounce keyboard handler */
483 kbd_debHandler.hook = kbd_debHandlerFunc;
484 kbd_debHandler.pri = 100; /* high priority */
485 kbd_debHandler.flags = KHF_RAWKEYS;
486 kbd_addHandler(&kbd_debHandler);
488 #if CONFIG_KBD_LONGPRESS
489 /* Add long pression keyboard handler */
490 kbd_lngHandler.hook = kbd_lngHandlerFunc;
491 kbd_lngHandler.pri = 90; /* high priority */
492 kbd_lngHandler.flags = KHF_RAWKEYS;
493 kbd_addHandler(&kbd_lngHandler);
496 /* Add repeat keyboard handler */
497 kbd_rptHandler.hook = kbd_rptHandlerFunc;
498 kbd_rptHandler.pri = 80; /* high priority */
499 kbd_rptHandler.flags = KHF_RAWKEYS;
500 kbd_addHandler(&kbd_rptHandler);
502 /* Add default keyboard handler */
503 kbd_defHandler.hook = kbd_defHandlerFunc;
504 kbd_defHandler.pri = -128; /* lowest priority */
505 kbd_addHandler(&kbd_defHandler);
507 #if CONFIG_KBD_OBSERVER
508 observer_InitSubject(&kbd_subject);
511 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
515 /* Add kbd handler to soft timers list */
516 event_initSoftInt(&kbd_timer.expire, kbd_softint, NULL);
517 timer_setDelay(&kbd_timer, ms_to_ticks(KBD_CHECK_INTERVAL));
518 timer_add(&kbd_timer);
520 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
522 /* Create a timer specific thread */
523 xTaskCreate(kbd_task, "kbd", CONFIG_STACK_KBD,
524 NULL, CONFIG_PRI_KBD, NULL);
527 #error "Define keyboard poll method"