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.7 2006/06/03 13:57:36 bernie
21 *#* Make keyboard repeat mask run-time configurable.
23 *#* Revision 1.6 2006/03/20 17:50:17 bernie
24 *#* Add FreeRTOS and Observers support.
26 *#* Revision 1.5 2006/02/27 22:39:45 bernie
27 *#* Misc build and doc fixes from project_grl.
29 *#* Revision 1.4 2006/02/24 00:27:14 bernie
30 *#* Use new naming convention for list macros.
32 *#* Revision 1.3 2006/02/17 21:15:42 bernie
33 *#* Add MOD_CHECK() checks.
35 *#* Revision 1.2 2006/02/10 12:36:20 bernie
36 *#* Add preliminary FreeRTOS support; Enforce CONFIG_* definitions.
38 *#* Revision 1.1 2005/06/27 21:28:45 bernie
39 *#* Import generic keyboard driver.
45 #include <drv/timer.h>
48 #include <cfg/debug.h>
49 #include <cfg/module.h>
50 #include <appconfig.h>
52 /* Configuration sanity checks */
53 #if !defined(CONFIG_KBD_POLL) || (CONFIG_KBD_POLL != KBD_POLL_SOFTINT && CONFIG_KBD_POLL != CONFIG_POLL_FREERTOS)
54 #error CONFIG_KBD_POLL must be defined to either KBD_POLL_SOFTINT or CONFIG_POLL_FREERTOS
56 #if !defined(CONFIG_KBD_BEEP) || (CONFIG_KBD_BEEP != 0 && CONFIG_KBD_BEEP != 1)
57 #error CONFIG_KBD_BEEP must be defined to either 0 or 1
59 #if !defined(CONFIG_KBD_OBSERVER) || (CONFIG_KBD_OBSERVER != 0 && CONFIG_KBD_OBSERVER != 1)
60 #error CONFIG_KBD_OBSERVER must be defined to either 0 or 1
64 #include <drv/buzzer.h>
67 #define KBD_CHECK_INTERVAL 10 /*!< (ms) Timing for kbd softint */
68 #define KBD_DEBOUNCE_TIME 30 /*!< (ms) Debounce time */
69 #define KBD_BEEP_TIME 5 /*!< (ms) Duration of keybeep */
71 #define KBD_REPEAT_DELAY 400 /*!< (ms) Keyboard repeat delay for first character */
72 #define KBD_REPEAT_RATE 100 /*!< (ms) Initial interchar delay for keyboard repeat */
73 #define KBD_REPEAT_MAXRATE 20 /*!< (ms) Minimum delay for keyboard repeat */
74 #define KBD_REPEAT_ACCEL 5 /*!< (ms) Keyboard repeat speed increase */
76 #define KBD_LNG_DELAY 1000 /*!< (ms) Keyboard long pression keys delay */
79 /*! Status for keyboard repeat state machine */
80 static enum { KS_IDLE, KS_REPDELAY, KS_REPEAT } kbd_rptStatus;
83 static volatile keymask_t kbd_buf; /*!< Single entry keyboard buffer */
84 static volatile keymask_t kbd_cnt; /*!< Number of keypress events in \c kbd_buf */
85 static keymask_t kbd_rpt_mask; /**< Mask of repeatable keys. */
87 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
88 static Timer kbd_timer; /*!< Keyboard softtimer */
91 static List kbd_rawHandlers; /*!< Raw keyboard handlers */
92 static List kbd_handlers; /*!< Cooked keyboard handlers */
94 static KbdHandler kbd_defHandler; /*!< The default keyboard handler */
95 static KbdHandler kbd_debHandler; /*!< The debounce keyboard handler */
96 static KbdHandler kbd_rptHandler; /*!< Auto-repeat keyboard handler */
98 static KbdHandler kbd_lngHandler; /*!< Long pression keys handler */
101 #if CONFIG_KBD_OBSERVER
102 #include <mware/observer.h>
108 * Poll keyboard and dispatch keys to handlers.
110 * Read the key states and invoke all keyboard
111 * handlers to process the new state.
113 * Call this function periodically using a software
114 * timer, an interrupt or a process.
116 static void kbd_poll(void)
118 /*! Currently depressed key */
119 static keymask_t current_key;
121 struct KbdHandler *handler;
122 keymask_t key = kbd_readkeys();
124 /* Call raw input handlers */
125 FOREACH_NODE(handler, &kbd_rawHandlers)
126 key = handler->hook(key);
128 /* If this key was not previously pressed */
129 if (key != current_key)
131 /* Remember last key */
134 /* Call cooked input handlers */
135 FOREACH_NODE(handler, &kbd_handlers)
136 key = handler->hook(key);
140 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
143 * Keyboard soft-irq handler.
145 static void kbd_softint(UNUSED_ARG(iptr_t, arg))
148 timer_add(&kbd_timer);
151 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
153 #include "FreeRTOS.h"
156 static portTASK_FUNCTION(kbd_task, arg)
161 timer_delay(KBD_CHECK_INTERVAL);
165 #endif /* CONFIG_KBD_POLL */
168 * \brief Read a key from the keyboard buffer.
170 * When a key is kept depressed between calls of this function a value
171 * is returned only after the time specified with KBD_REPAT_DELAY to
172 * avoid too fast keyboard repeat.
174 * \note This function is \b not interrupt safe!
176 * \return The mask of depressed keys or 0 if no keys are depressed.
179 keymask_t kbd_peek(void)
184 /* Let other tasks run for a while */
185 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 while (!(key = kbd_peek())) {}
216 * Wait up to \c timeout ms for a keypress
217 * and return the mask of depressed keys, or K_TIMEOUT
218 * if the timeout was reacked.
220 keymask_t kbd_get_timeout(mtime_t timeout)
224 ticks_t start = timer_clock();
225 ticks_t stop = ms_to_ticks(timeout);
228 if ((key = kbd_peek()))
231 while (timer_clock() - start < stop);
237 void kbd_addHandler(struct KbdHandler *handler)
243 IRQ_SAVE_DISABLE(flags);
245 /* Choose between raw and coocked handlers list */
246 list = (handler->flags & KHF_RAWKEYS) ?
247 &kbd_rawHandlers : &kbd_handlers;
250 * Search for the first node whose priority
251 * is lower than the timer we want to add.
253 FOREACH_NODE(node,list)
254 if (node->pri < handler->pri)
257 /* Enqueue handler in the handlers chain */
258 INSERT_BEFORE(&handler->link, &node->link);
264 void kbd_remHandler(struct KbdHandler *handler)
266 /* Remove the handler */
267 ATOMIC(REMOVE(&handler->link));
272 * This is the default key handler, called after
273 * all other handlers have had their chance to
274 * do their special processing. This handler
275 * pushes all input in the keyboard FIFO buffer.
277 static keymask_t kbd_defHandlerFunc(keymask_t key)
281 /* Force a single event in kbd buffer */
285 #if CONFIG_KBD_OBSERVER
286 observer_notify(&kbd_subject, KBD_EVENT_KEY, &key);
290 if (!(key & K_REPEAT))
291 buz_beep(KBD_BEEP_TIME);
300 * Handle keyboard debounce
302 static keymask_t kbd_debHandlerFunc(keymask_t key)
304 /*! Buffer for debounce */
305 static keymask_t debounce_key;
307 /*! Timer for keyboard debounce */
308 static ticks_t debounce_time;
310 /*! Key aquired after debounce */
311 static keymask_t new_key;
314 ticks_t now = timer_clock();
316 if (key != debounce_key)
318 /* Reset debounce timer */
322 else if ((new_key != debounce_key)
323 && (now - debounce_time > ms_to_ticks(KBD_DEBOUNCE_TIME)))
325 new_key = debounce_key;
334 * Handle long pression keys.
336 static keymask_t kbd_lngHandlerFunc(keymask_t key)
338 static ticks_t start;
339 ticks_t now = timer_clock();
341 if (key & K_LNG_MASK)
343 if (now - start > ms_to_ticks(KBD_LNG_DELAY))
353 * Set current mask of repeatable keys.
355 keymask_t kbd_setRepeatMask(keymask_t mask)
357 keymask_t oldmask = kbd_rpt_mask;
358 ATOMIC(kbd_rpt_mask = mask);
363 * Handle keyboard repeat
365 static keymask_t kbd_rptHandlerFunc(keymask_t key)
367 /* Timer for keyboard repeat events. */
368 static ticks_t repeat_time;
370 /* Current repeat rate (for acceleration). */
371 static ticks_t repeat_rate; /*! Current repeat rate (for acceleration) */
373 ticks_t now = timer_clock();
375 switch (kbd_rptStatus)
378 if (key & kbd_rpt_mask)
381 kbd_rptStatus = KS_REPDELAY;
386 if (key & kbd_rpt_mask)
388 if (now - repeat_time > ms_to_ticks(KBD_REPEAT_DELAY))
390 key = (key & kbd_rpt_mask) | K_REPEAT;
392 repeat_rate = ms_to_ticks(KBD_REPEAT_RATE);
393 kbd_rptStatus = KS_REPEAT;
399 kbd_rptStatus = KS_IDLE;
403 if (key & kbd_rpt_mask)
405 if (now - repeat_time > repeat_rate)
407 /* Enqueue a new event in the buffer */
408 key = (key & kbd_rpt_mask) | K_REPEAT;
411 /* Repeat rate acceleration */
412 if (repeat_rate > ms_to_ticks(KBD_REPEAT_MAXRATE))
413 repeat_rate -= ms_to_ticks(KBD_REPEAT_ACCEL);
419 kbd_rptStatus = KS_IDLE;
431 * Initialize keyboard ports and softtimer
441 /* Init handlers lists */
442 LIST_INIT(&kbd_handlers);
443 LIST_INIT(&kbd_rawHandlers);
445 /* Add debounce keyboard handler */
446 kbd_debHandler.hook = kbd_debHandlerFunc;
447 kbd_debHandler.pri = 100; /* high priority */
448 kbd_debHandler.flags = KHF_RAWKEYS;
449 kbd_addHandler(&kbd_debHandler);
452 /* Add long pression keyboard handler */
453 kbd_lngHandler.hook = kbd_lngHandlerFunc;
454 kbd_lngHandler.pri = 90; /* high priority */
455 kbd_lngHandler.flags = KHF_RAWKEYS;
456 kbd_addHandler(&kbd_lngHandler);
459 /* Add repeat keyboard handler */
460 kbd_rptHandler.hook = kbd_rptHandlerFunc;
461 kbd_rptHandler.pri = 80; /* high priority */
462 kbd_rptHandler.flags = KHF_RAWKEYS;
463 kbd_addHandler(&kbd_rptHandler);
465 /* Add default keyboard handler */
466 kbd_defHandler.hook = kbd_defHandlerFunc;
467 kbd_defHandler.pri = -128; /* lowest priority */
468 kbd_addHandler(&kbd_defHandler);
470 #if CONFIG_KBD_OBSERVER
471 observer_InitSubject(&kbd_subject);
474 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
478 /* Add kbd handler to soft timers list */
479 event_initSoftInt(&kbd_timer.expire, kbd_softint, 0);
480 timer_setDelay(&kbd_timer, ms_to_ticks(KBD_CHECK_INTERVAL));
481 timer_add(&kbd_timer);
483 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
485 /* Create a timer specific thread */
486 xTaskCreate(kbd_task, "kbd", CONFIG_STACK_KBD,
487 NULL, CONFIG_PRI_KBD, NULL);
490 #error "Define keyboard poll method"