4 * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 1999, 2003 Bernardo Innocenti
6 * This file is part of DevLib - See README.devlib for information.
11 * \author Bernardo Innocenti <bernie@develer.com>
12 * \author Stefano Fedrigo <aleph@develer.com>
13 * \author Francesco Sacchi <batt@develer.com>
15 * \brief Keyboard driver (implementation)
20 *#* Revision 1.8 2006/07/19 12:56:25 bernie
21 *#* Convert to new Doxygen style.
23 *#* Revision 1.7 2006/06/03 13:57:36 bernie
24 *#* Make keyboard repeat mask run-time configurable.
26 *#* Revision 1.6 2006/03/20 17:50:17 bernie
27 *#* Add FreeRTOS and Observers support.
29 *#* Revision 1.5 2006/02/27 22:39:45 bernie
30 *#* Misc build and doc fixes from project_grl.
32 *#* Revision 1.4 2006/02/24 00:27:14 bernie
33 *#* Use new naming convention for list macros.
35 *#* Revision 1.3 2006/02/17 21:15:42 bernie
36 *#* Add MOD_CHECK() checks.
38 *#* Revision 1.2 2006/02/10 12:36:20 bernie
39 *#* Add preliminary FreeRTOS support; Enforce CONFIG_* definitions.
41 *#* Revision 1.1 2005/06/27 21:28:45 bernie
42 *#* Import generic keyboard driver.
48 #include <drv/timer.h>
51 #include <cfg/debug.h>
52 #include <cfg/module.h>
53 #include <appconfig.h>
55 /* Configuration sanity checks */
56 #if !defined(CONFIG_KBD_POLL) || (CONFIG_KBD_POLL != KBD_POLL_SOFTINT && CONFIG_KBD_POLL != CONFIG_POLL_FREERTOS)
57 #error CONFIG_KBD_POLL must be defined to either KBD_POLL_SOFTINT or CONFIG_POLL_FREERTOS
59 #if !defined(CONFIG_KBD_BEEP) || (CONFIG_KBD_BEEP != 0 && CONFIG_KBD_BEEP != 1)
60 #error CONFIG_KBD_BEEP must be defined to either 0 or 1
62 #if !defined(CONFIG_KBD_OBSERVER) || (CONFIG_KBD_OBSERVER != 0 && CONFIG_KBD_OBSERVER != 1)
63 #error CONFIG_KBD_OBSERVER must be defined to either 0 or 1
67 #include <drv/buzzer.h>
70 #define KBD_CHECK_INTERVAL 10 /**< (ms) Timing for kbd softint */
71 #define KBD_DEBOUNCE_TIME 30 /**< (ms) Debounce time */
72 #define KBD_BEEP_TIME 5 /**< (ms) Duration of keybeep */
74 #define KBD_REPEAT_DELAY 400 /**< (ms) Keyboard repeat delay for first character */
75 #define KBD_REPEAT_RATE 100 /**< (ms) Initial interchar delay for keyboard repeat */
76 #define KBD_REPEAT_MAXRATE 20 /**< (ms) Minimum delay for keyboard repeat */
77 #define KBD_REPEAT_ACCEL 5 /**< (ms) Keyboard repeat speed increase */
79 #define KBD_LNG_DELAY 1000 /**< (ms) Keyboard long pression keys delay */
82 /** Status for keyboard repeat state machine */
83 static enum { KS_IDLE, KS_REPDELAY, KS_REPEAT } kbd_rptStatus;
86 static volatile keymask_t kbd_buf; /**< Single entry keyboard buffer */
87 static volatile keymask_t kbd_cnt; /**< Number of keypress events in \c kbd_buf */
88 static keymask_t kbd_rpt_mask; /**< Mask of repeatable keys. */
90 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
91 static Timer kbd_timer; /**< Keyboard softtimer */
94 static List kbd_rawHandlers; /**< Raw keyboard handlers */
95 static List kbd_handlers; /**< Cooked keyboard handlers */
97 static KbdHandler kbd_defHandler; /**< The default keyboard handler */
98 static KbdHandler kbd_debHandler; /**< The debounce keyboard handler */
99 static KbdHandler kbd_rptHandler; /**< Auto-repeat keyboard handler */
101 static KbdHandler kbd_lngHandler; /**< Long pression keys handler */
104 #if CONFIG_KBD_OBSERVER
105 #include <mware/observer.h>
111 * Poll keyboard and dispatch keys to handlers.
113 * Read the key states and invoke all keyboard
114 * handlers to process the new state.
116 * Call this function periodically using a software
117 * timer, an interrupt or a process.
119 static void kbd_poll(void)
121 /** Currently depressed key */
122 static keymask_t current_key;
124 struct KbdHandler *handler;
125 keymask_t key = kbd_readkeys();
127 /* Call raw input handlers */
128 FOREACH_NODE(handler, &kbd_rawHandlers)
129 key = handler->hook(key);
131 /* If this key was not previously pressed */
132 if (key != current_key)
134 /* Remember last key */
137 /* Call cooked input handlers */
138 FOREACH_NODE(handler, &kbd_handlers)
139 key = handler->hook(key);
143 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
146 * Keyboard soft-irq handler.
148 static void kbd_softint(UNUSED_ARG(iptr_t, arg))
151 timer_add(&kbd_timer);
154 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
156 #include "FreeRTOS.h"
159 static portTASK_FUNCTION(kbd_task, arg)
164 timer_delay(KBD_CHECK_INTERVAL);
168 #endif /* CONFIG_KBD_POLL */
171 * \brief Read a key from the keyboard buffer.
173 * When a key is kept depressed between calls of this function a value
174 * is returned only after the time specified with KBD_REPAT_DELAY to
175 * avoid too fast keyboard repeat.
177 * \note This function is \b not interrupt safe!
179 * \return The mask of depressed keys or 0 if no keys are depressed.
182 keymask_t kbd_peek(void)
187 /* Let other tasks run for a while */
188 extern void schedule(void);
191 /* Extract an event from the keyboard buffer */
204 * Wait for a keypress and return the mask of depressed keys.
206 * \note This function is \b not interrupt safe!
208 keymask_t kbd_get(void)
212 while (!(key = kbd_peek())) {}
219 * Wait up to \c timeout ms for a keypress
220 * and return the mask of depressed keys, or K_TIMEOUT
221 * if the timeout was reacked.
223 keymask_t kbd_get_timeout(mtime_t timeout)
227 ticks_t start = timer_clock();
228 ticks_t stop = ms_to_ticks(timeout);
231 if ((key = kbd_peek()))
234 while (timer_clock() - start < stop);
240 void kbd_addHandler(struct KbdHandler *handler)
246 IRQ_SAVE_DISABLE(flags);
248 /* Choose between raw and coocked handlers list */
249 list = (handler->flags & KHF_RAWKEYS) ?
250 &kbd_rawHandlers : &kbd_handlers;
253 * Search for the first node whose priority
254 * is lower than the timer we want to add.
256 FOREACH_NODE(node,list)
257 if (node->pri < handler->pri)
260 /* Enqueue handler in the handlers chain */
261 INSERT_BEFORE(&handler->link, &node->link);
267 void kbd_remHandler(struct KbdHandler *handler)
269 /* Remove the handler */
270 ATOMIC(REMOVE(&handler->link));
275 * This is the default key handler, called after
276 * all other handlers have had their chance to
277 * do their special processing. This handler
278 * pushes all input in the keyboard FIFO buffer.
280 static keymask_t kbd_defHandlerFunc(keymask_t key)
284 /* Force a single event in kbd buffer */
288 #if CONFIG_KBD_OBSERVER
289 observer_notify(&kbd_subject, KBD_EVENT_KEY, &key);
293 if (!(key & K_REPEAT))
294 buz_beep(KBD_BEEP_TIME);
303 * Handle keyboard debounce
305 static keymask_t kbd_debHandlerFunc(keymask_t key)
307 /** Buffer for debounce */
308 static keymask_t debounce_key;
310 /** Timer for keyboard debounce */
311 static ticks_t debounce_time;
313 /** Key aquired after debounce */
314 static keymask_t new_key;
317 ticks_t now = timer_clock();
319 if (key != debounce_key)
321 /* Reset debounce timer */
325 else if ((new_key != debounce_key)
326 && (now - debounce_time > ms_to_ticks(KBD_DEBOUNCE_TIME)))
328 new_key = debounce_key;
337 * Handle long pression keys.
339 static keymask_t kbd_lngHandlerFunc(keymask_t key)
341 static ticks_t start;
342 ticks_t now = timer_clock();
344 if (key & K_LNG_MASK)
346 if (now - start > ms_to_ticks(KBD_LNG_DELAY))
356 * Set current mask of repeatable keys.
358 keymask_t kbd_setRepeatMask(keymask_t mask)
360 keymask_t oldmask = kbd_rpt_mask;
361 ATOMIC(kbd_rpt_mask = mask);
366 * Handle keyboard repeat
368 static keymask_t kbd_rptHandlerFunc(keymask_t key)
370 /* Timer for keyboard repeat events. */
371 static ticks_t repeat_time;
373 /* Current repeat rate (for acceleration). */
374 static ticks_t repeat_rate; /** Current repeat rate (for acceleration) */
376 ticks_t now = timer_clock();
378 switch (kbd_rptStatus)
381 if (key & kbd_rpt_mask)
384 kbd_rptStatus = KS_REPDELAY;
389 if (key & kbd_rpt_mask)
391 if (now - repeat_time > ms_to_ticks(KBD_REPEAT_DELAY))
393 key = (key & kbd_rpt_mask) | K_REPEAT;
395 repeat_rate = ms_to_ticks(KBD_REPEAT_RATE);
396 kbd_rptStatus = KS_REPEAT;
402 kbd_rptStatus = KS_IDLE;
406 if (key & kbd_rpt_mask)
408 if (now - repeat_time > repeat_rate)
410 /* Enqueue a new event in the buffer */
411 key = (key & kbd_rpt_mask) | K_REPEAT;
414 /* Repeat rate acceleration */
415 if (repeat_rate > ms_to_ticks(KBD_REPEAT_MAXRATE))
416 repeat_rate -= ms_to_ticks(KBD_REPEAT_ACCEL);
422 kbd_rptStatus = KS_IDLE;
434 * Initialize keyboard ports and softtimer
444 /* Init handlers lists */
445 LIST_INIT(&kbd_handlers);
446 LIST_INIT(&kbd_rawHandlers);
448 /* Add debounce keyboard handler */
449 kbd_debHandler.hook = kbd_debHandlerFunc;
450 kbd_debHandler.pri = 100; /* high priority */
451 kbd_debHandler.flags = KHF_RAWKEYS;
452 kbd_addHandler(&kbd_debHandler);
455 /* Add long pression keyboard handler */
456 kbd_lngHandler.hook = kbd_lngHandlerFunc;
457 kbd_lngHandler.pri = 90; /* high priority */
458 kbd_lngHandler.flags = KHF_RAWKEYS;
459 kbd_addHandler(&kbd_lngHandler);
462 /* Add repeat keyboard handler */
463 kbd_rptHandler.hook = kbd_rptHandlerFunc;
464 kbd_rptHandler.pri = 80; /* high priority */
465 kbd_rptHandler.flags = KHF_RAWKEYS;
466 kbd_addHandler(&kbd_rptHandler);
468 /* Add default keyboard handler */
469 kbd_defHandler.hook = kbd_defHandlerFunc;
470 kbd_defHandler.pri = -128; /* lowest priority */
471 kbd_addHandler(&kbd_defHandler);
473 #if CONFIG_KBD_OBSERVER
474 observer_InitSubject(&kbd_subject);
477 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
481 /* Add kbd handler to soft timers list */
482 event_initSoftInt(&kbd_timer.expire, kbd_softint, 0);
483 timer_setDelay(&kbd_timer, ms_to_ticks(KBD_CHECK_INTERVAL));
484 timer_add(&kbd_timer);
486 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
488 /* Create a timer specific thread */
489 xTaskCreate(kbd_task, "kbd", CONFIG_STACK_KBD,
490 NULL, CONFIG_PRI_KBD, NULL);
493 #error "Define keyboard poll method"