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.4 2006/02/24 00:27:14 bernie
21 *#* Use new naming convention for list macros.
23 *#* Revision 1.3 2006/02/17 21:15:42 bernie
24 *#* Add MOD_CHECK() checks.
26 *#* Revision 1.2 2006/02/10 12:36:20 bernie
27 *#* Add preliminary FreeRTOS support; Enforce CONFIG_* definitions.
29 *#* Revision 1.1 2005/06/27 21:28:45 bernie
30 *#* Import generic keyboard driver.
36 #include <drv/timer.h>
38 #include <drv/buzzer.h>
40 #include <cfg/debug.h>
41 #include <cfg/module.h>
43 /* Configuration sanity checks */
44 #if !defined(CONFIG_KBD_POLL) || (CONFIG_KBD_POLL != KBD_POLL_SOFTINT && CONFIG_KBD_POLL != CONFIG_POLL_FREERTOS)
45 #error CONFIG_KBD_POLL must be defined to either KBD_POLL_SOFTINT or CONFIG_POLL_FREERTOS
49 #define KBD_CHECK_INTERVAL 10 /*!< (ms) Timing for kbd softint */
50 #define KBD_DEBOUNCE_TIME 30 /*!< (ms) Debounce time */
51 #define KBD_BEEP_TIME 5 /*!< (ms) Duration of keybeep */
53 #define KBD_REPEAT_DELAY 400 /*!< (ms) Keyboard repeat delay for first character */
54 #define KBD_REPEAT_RATE 100 /*!< (ms) Initial interchar delay for keyboard repeat */
55 #define KBD_REPEAT_MAXRATE 20 /*!< (ms) Minimum delay for keyboard repeat */
56 #define KBD_REPEAT_ACCEL 5 /*!< (ms) Keyboard repeat speed increase */
58 #define KBD_LNG_DELAY 1000 /*!< (ms) Keyboard long pression keys delay */
61 /*! Status for keyboard repeat state machine */
62 static enum { KS_IDLE, KS_REPDELAY, KS_REPEAT } kbd_rptStatus;
65 static volatile keymask_t kbd_buf; /*!< Single entry keyboard buffer */
66 static volatile keymask_t kbd_cnt; /*!< Number of keypress events in \c kbd_buf */
68 static Timer kbd_timer; /*!< Keyboard softtimer */
70 static List kbd_rawHandlers; /*!< Raw keyboard handlers */
71 static List kbd_handlers; /*!< Cooked keyboard handlers */
73 static KbdHandler kbd_defHandler; /*!< The default keyboard handler */
74 static KbdHandler kbd_debHandler; /*!< The debounce keyboard handler */
75 static KbdHandler kbd_rptHandler; /*!< Auto-repeat keyboard handler */
77 static KbdHandler kbd_lngHandler; /*!< Long pression keys handler */
83 * Poll keyboard and dispatch keys to handlers.
85 * Read the key states and invoke all keyboard
86 * handlers to process the new state.
88 * Call this function periodically using a software
89 * timer, an interrupt or a process.
91 static void kbd_poll(void)
93 /*! Currently depressed key */
94 static keymask_t current_key;
96 struct KbdHandler *handler;
97 keymask_t key = kbd_readkeys();
99 /* Call raw input handlers */
100 FOREACH_NODE(handler, &kbd_rawHandlers)
101 key = handler->hook(key);
103 /* If this key was not previously pressed */
104 if (key != current_key)
106 /* Remember last key */
109 /* Call cooked input handlers */
110 FOREACH_NODE(handler, &kbd_handlers)
111 key = handler->hook(key);
115 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
118 * Keyboard soft-irq handler.
120 static void kbd_softint(UNUSED_ARG(iptr_t, arg))
123 timer_add(&kbd_timer);
126 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
128 #include "FreeRTOS.h"
131 static portTASK_FUNCTION(kbd_poll, arg)
136 timer_delay(KBD_CHECK_INTERVAL);
140 #endif /* CONFIG_KBD_POLL */
143 * \brief Read a key from the keyboard buffer.
145 * When a key is kept depressed between calls of this function a value
146 * is returned only after the time specified with KBD_REPAT_DELAY to
147 * avoid too fast keyboard repeat.
149 * \note This function is \b not interrupt safe!
151 * \return The mask of depressed keys or 0 if no keys are depressed.
154 keymask_t kbd_peek(void)
159 extern void schedule(void);
162 /* Extract an event from the keyboard buffer */
176 * Wait for a keypress and return the mask of depressed keys.
178 * \note This function is \b not interrupt safe!
180 keymask_t kbd_get(void)
184 while (!(key = kbd_peek())) {}
190 * Wait up to \c timeout ms for a keypress
191 * and return the mask of depressed keys, or K_TIMEOUT
192 * if the timeout was reacked.
194 keymask_t kbd_get_timeout(mtime_t timeout)
198 ticks_t start = timer_clock();
199 ticks_t stop = ms_to_ticks(timeout);
202 if ((key = kbd_peek()))
205 while (timer_clock() - start < stop);
211 void kbd_addHandler(struct KbdHandler *handler)
217 IRQ_SAVE_DISABLE(flags);
219 /* Choose between raw and coocked handlers list */
220 list = (handler->flags & KHF_RAWKEYS) ?
221 &kbd_rawHandlers : &kbd_handlers;
224 * Search for the first node whose priority
225 * is lower than the timer we want to add.
227 FOREACH_NODE(node,list)
228 if (node->pri < handler->pri)
231 /* Enqueue handler in the handlers chain */
232 INSERT_BEFORE(&handler->link, &node->link);
238 void kbd_remHandler(struct KbdHandler *handler)
240 /* Remove the handler */
241 ATOMIC(REMOVE(&handler->link));
246 * This is the default key handler, called after
247 * all other handlers have had their chance to
248 * do their special processing. This handler
249 * pushes all input in the keyboard FIFO buffer.
251 static keymask_t kbd_defHandlerFunc(keymask_t key)
255 /* Force a single event in kbd buffer */
260 if (!(key & K_REPEAT))
261 buz_beep(KBD_BEEP_TIME);
270 * Handle keyboard debounce
272 static keymask_t kbd_debHandlerFunc(keymask_t key)
274 /*! Buffer for debounce */
275 static keymask_t debounce_key;
277 /*! Timer for keyboard debounce */
278 static ticks_t debounce_time;
280 /*! Key aquired after debounce */
281 static keymask_t new_key;
284 ticks_t now = timer_clock();
286 if (key != debounce_key)
288 /* Reset debounce timer */
292 else if ((new_key != debounce_key)
293 && (now - debounce_time > ms_to_ticks(KBD_DEBOUNCE_TIME)))
295 new_key = debounce_key;
304 * Handle long pression keys
306 static keymask_t kbd_lngHandlerFunc(keymask_t key)
309 ticks_t now = timer_clock();
311 if (key & K_LNG_MASK)
319 stop = now + ms_to_ticks(KBD_LNG_DELAY);
325 * Handle keyboard repeat
327 static keymask_t kbd_rptHandlerFunc(keymask_t key)
329 /*! Timer for keyboard repeat events */
330 static ticks_t repeat_time;
332 static ticks_t repeat_rate; /*! Current repeat rate (for acceleration) */
334 ticks_t now = timer_clock();
336 switch (kbd_rptStatus)
339 if (key & K_RPT_MASK)
342 kbd_rptStatus = KS_REPDELAY;
347 if (key & K_RPT_MASK)
349 if (now - repeat_time > ms_to_ticks(KBD_REPEAT_DELAY))
351 key = (key & K_RPT_MASK) | K_REPEAT;
353 repeat_rate = ms_to_ticks(KBD_REPEAT_RATE);
354 kbd_rptStatus = KS_REPEAT;
360 kbd_rptStatus = KS_IDLE;
364 if (key & K_RPT_MASK)
366 if (now - repeat_time > repeat_rate)
368 /* Enqueue a new event in the buffer */
369 key = (key & K_RPT_MASK) | K_REPEAT;
372 /* Repeat rate acceleration */
373 if (repeat_rate > ms_to_ticks(KBD_REPEAT_MAXRATE))
374 repeat_rate -= ms_to_ticks(KBD_REPEAT_ACCEL);
380 kbd_rptStatus = KS_IDLE;
392 * Initialize keyboard ports and softtimer
400 /* Init handlers lists */
401 LIST_INIT(&kbd_handlers);
402 LIST_INIT(&kbd_rawHandlers);
404 /* Add debounce keyboard handler */
405 kbd_debHandler.hook = kbd_debHandlerFunc;
406 kbd_debHandler.pri = 100; /* high priority */
407 kbd_debHandler.flags = KHF_RAWKEYS;
408 kbd_addHandler(&kbd_debHandler);
411 /* Add long pression keyboard handler */
412 kbd_lngHandler.hook = kbd_lngHandlerFunc;
413 kbd_lngHandler.pri = 90; /* high priority */
414 kbd_lngHandler.flags = KHF_RAWKEYS;
415 kbd_addHandler(&kbd_lngHandler);
418 /* Add repeat keyboard handler */
419 kbd_rptHandler.hook = kbd_rptHandlerFunc;
420 kbd_rptHandler.pri = 80; /* high priority */
421 kbd_rptHandler.flags = KHF_RAWKEYS;
422 kbd_addHandler(&kbd_rptHandler);
424 /* Add default keyboard handler */
425 kbd_defHandler.hook = kbd_defHandlerFunc;
426 kbd_defHandler.pri = -128; /* lowest priority */
427 kbd_addHandler(&kbd_defHandler);
429 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
433 /* Add kbd handler to soft timers list */
434 event_initSoftInt(&kbd_timer.expire, kbd_softint, 0);
435 timer_setDelay(&kbd_timer, ms_to_ticks(KBD_CHECK_INTERVAL));
436 timer_add(&kbd_timer);
438 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
440 /* Create a timer specific thread */
441 xTaskCreate(kbd_poll, "kbd_poll", CONFIG_STACK_KBD,
442 NULL, CONFIG_PRI_KBD, NULL);
445 #error "Define keyboard poll method"