4dabd3b3b942931931d0cfed67ed388e19c95851
[bertos.git] / drv / kbd.c
1 /*!
2  * \file
3  * <!--
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.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  * \author Stefano Fedrigo <aleph@develer.com>
13  * \author Francesco Sacchi <batt@develer.com>
14  *
15  * \brief Keyboard driver (implementation)
16  */
17
18 /*#*
19  *#* $Log$
20  *#* Revision 1.7  2006/06/03 13:57:36  bernie
21  *#* Make keyboard repeat mask run-time configurable.
22  *#*
23  *#* Revision 1.6  2006/03/20 17:50:17  bernie
24  *#* Add FreeRTOS and Observers support.
25  *#*
26  *#* Revision 1.5  2006/02/27 22:39:45  bernie
27  *#* Misc build and doc fixes from project_grl.
28  *#*
29  *#* Revision 1.4  2006/02/24 00:27:14  bernie
30  *#* Use new naming convention for list macros.
31  *#*
32  *#* Revision 1.3  2006/02/17 21:15:42  bernie
33  *#* Add MOD_CHECK() checks.
34  *#*
35  *#* Revision 1.2  2006/02/10 12:36:20  bernie
36  *#* Add preliminary FreeRTOS support; Enforce CONFIG_* definitions.
37  *#*
38  *#* Revision 1.1  2005/06/27 21:28:45  bernie
39  *#* Import generic keyboard driver.
40  *#*
41  *#*/
42
43 #include <hw_kbd.h>
44
45 #include <drv/timer.h>
46 #include <drv/kbd.h>
47
48 #include <cfg/debug.h>
49 #include <cfg/module.h>
50 #include <appconfig.h>
51
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
55 #endif
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
58 #endif
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
61 #endif
62
63 #if CONFIG_KBD_BEEP
64         #include <drv/buzzer.h>
65 #endif
66
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 */
70
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 */
75
76 #define KBD_LNG_DELAY     1000  /*!< (ms) Keyboard long pression keys delay */
77
78
79 /*! Status for keyboard repeat state machine */
80 static enum { KS_IDLE, KS_REPDELAY, KS_REPEAT } kbd_rptStatus;
81
82
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. */
86
87 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
88 static Timer kbd_timer;            /*!< Keyboard softtimer */
89 #endif
90
91 static List kbd_rawHandlers;       /*!< Raw keyboard handlers */
92 static List kbd_handlers;          /*!< Cooked keyboard handlers */
93
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 */
97 #ifdef  K_LNG_MASK
98 static KbdHandler kbd_lngHandler;  /*!< Long pression keys handler */
99 #endif
100
101 #if CONFIG_KBD_OBSERVER
102         #include <mware/observer.h>
103         Subject kbd_subject;
104 #endif
105
106
107 /*!
108  * Poll keyboard and dispatch keys to handlers.
109  *
110  * Read the key states and invoke all keyboard
111  * handlers to process the new state.
112  *
113  * Call this function periodically using a software
114  * timer, an interrupt or a process.
115  */
116 static void kbd_poll(void)
117 {
118         /*! Currently depressed key */
119         static keymask_t current_key;
120
121         struct KbdHandler *handler;
122         keymask_t key = kbd_readkeys();
123
124         /* Call raw input handlers */
125         FOREACH_NODE(handler, &kbd_rawHandlers)
126                 key = handler->hook(key);
127
128         /* If this key was not previously pressed */
129         if (key != current_key)
130         {
131                 /* Remember last key */
132                 current_key = key;
133
134                 /* Call cooked input handlers */
135                 FOREACH_NODE(handler, &kbd_handlers)
136                         key = handler->hook(key);
137         }
138 }
139
140 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
141
142 /*!
143  * Keyboard soft-irq handler.
144  */
145 static void kbd_softint(UNUSED_ARG(iptr_t, arg))
146 {
147         kbd_poll();
148         timer_add(&kbd_timer);
149 }
150
151 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
152
153 #include "FreeRTOS.h"
154 #include "task.h"
155
156 static portTASK_FUNCTION(kbd_task, arg)
157 {
158         for (;;)
159         {
160                 kbd_poll();
161                 timer_delay(KBD_CHECK_INTERVAL);
162         }
163 }
164
165 #endif /* CONFIG_KBD_POLL */
166
167 /*!
168  * \brief Read a key from the keyboard buffer.
169  *
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.
173  *
174  * \note This function is \b not interrupt safe!
175  *
176  * \return The mask of depressed keys or 0 if no keys are depressed.
177  *
178  */
179 keymask_t kbd_peek(void)
180 {
181         keymask_t key = 0;
182
183 // FIXME
184         /* Let other tasks run for a while */
185         extern void schedule(void);
186         schedule();
187
188         /* Extract an event from the keyboard buffer */
189         IRQ_DISABLE;
190         if (kbd_cnt)
191         {
192                 --kbd_cnt;
193                 key = kbd_buf;
194         }
195         IRQ_ENABLE;
196
197         return key;
198 }
199
200 /*!
201  * Wait for a keypress and return the mask of depressed keys.
202  *
203  * \note This function is \b not interrupt safe!
204  */
205 keymask_t kbd_get(void)
206 {
207         keymask_t key;
208
209         while (!(key = kbd_peek())) {}
210
211         return key;
212 }
213
214
215 /*!
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.
219  */
220 keymask_t kbd_get_timeout(mtime_t timeout)
221 {
222         keymask_t key;
223
224         ticks_t start = timer_clock();
225         ticks_t stop  = ms_to_ticks(timeout);
226         do
227         {
228                 if ((key = kbd_peek()))
229                         return key;
230         }
231         while (timer_clock() - start < stop);
232
233         return K_TIMEOUT;
234 }
235
236
237 void kbd_addHandler(struct KbdHandler *handler)
238 {
239         KbdHandler *node;
240         List *list;
241
242         cpuflags_t flags;
243         IRQ_SAVE_DISABLE(flags);
244
245         /* Choose between raw and coocked handlers list */
246         list = (handler->flags & KHF_RAWKEYS) ?
247                 &kbd_rawHandlers : &kbd_handlers;
248
249         /*
250          * Search for the first node whose priority
251          * is lower than the timer we want to add.
252          */
253         FOREACH_NODE(node,list)
254                 if (node->pri < handler->pri)
255                         break;
256
257         /* Enqueue handler in the handlers chain */
258         INSERT_BEFORE(&handler->link, &node->link);
259
260         IRQ_RESTORE(flags);
261 }
262
263
264 void kbd_remHandler(struct KbdHandler *handler)
265 {
266         /* Remove the handler */
267         ATOMIC(REMOVE(&handler->link));
268 }
269
270
271 /*!
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.
276  */
277 static keymask_t kbd_defHandlerFunc(keymask_t key)
278 {
279         if (key)
280         {
281                 /* Force a single event in kbd buffer */
282                 kbd_buf = key;
283                 kbd_cnt = 1;
284
285                 #if CONFIG_KBD_OBSERVER
286                         observer_notify(&kbd_subject, KBD_EVENT_KEY, &key);
287                 #endif
288
289                 #if CONFIG_KBD_BEEP
290                         if (!(key & K_REPEAT))
291                                 buz_beep(KBD_BEEP_TIME);
292                 #endif
293         }
294
295         /* Eat all input */
296         return 0;
297 }
298
299 /*!
300  * Handle keyboard debounce
301  */
302 static keymask_t kbd_debHandlerFunc(keymask_t key)
303 {
304         /*! Buffer for debounce */
305         static keymask_t debounce_key;
306
307         /*! Timer for keyboard debounce */
308         static ticks_t debounce_time;
309
310         /*! Key aquired after debounce */
311         static keymask_t new_key;
312
313
314         ticks_t now = timer_clock();
315
316         if (key != debounce_key)
317         {
318                 /* Reset debounce timer */
319                 debounce_key = key;
320                 debounce_time = now;
321         }
322         else if ((new_key != debounce_key)
323                 && (now - debounce_time > ms_to_ticks(KBD_DEBOUNCE_TIME)))
324         {
325                 new_key = debounce_key;
326                 debounce_time = now;
327         }
328
329         return new_key;
330 }
331
332 #ifdef  K_LNG_MASK
333 /**
334  * Handle long pression keys.
335  */
336 static keymask_t kbd_lngHandlerFunc(keymask_t key)
337 {
338         static ticks_t start;
339         ticks_t now = timer_clock();
340
341         if (key & K_LNG_MASK)
342         {
343                 if (now - start > ms_to_ticks(KBD_LNG_DELAY))
344                         key |= K_LONG;
345         }
346         else
347                 start = now;
348         return key;
349 }
350 #endif
351
352 /**
353  * Set current mask of repeatable keys.
354  */
355 keymask_t kbd_setRepeatMask(keymask_t mask)
356 {
357         keymask_t oldmask = kbd_rpt_mask;
358         ATOMIC(kbd_rpt_mask = mask);
359         return oldmask;
360 }
361
362 /*!
363  * Handle keyboard repeat
364  */
365 static keymask_t kbd_rptHandlerFunc(keymask_t key)
366 {
367         /* Timer for keyboard repeat events. */
368         static ticks_t repeat_time;
369
370         /* Current repeat rate (for acceleration). */
371         static ticks_t repeat_rate; /*! Current repeat rate (for acceleration) */
372
373         ticks_t now = timer_clock();
374
375         switch (kbd_rptStatus)
376         {
377                 case KS_IDLE:
378                         if (key & kbd_rpt_mask)
379                         {
380                                 repeat_time = now;
381                                 kbd_rptStatus = KS_REPDELAY;
382                         }
383                         break;
384
385                 case KS_REPDELAY:
386                         if (key & kbd_rpt_mask)
387                         {
388                                 if (now - repeat_time > ms_to_ticks(KBD_REPEAT_DELAY))
389                                 {
390                                         key = (key & kbd_rpt_mask) | K_REPEAT;
391                                         repeat_time = now;
392                                         repeat_rate = ms_to_ticks(KBD_REPEAT_RATE);
393                                         kbd_rptStatus = KS_REPEAT;
394                                 }
395                                 else
396                                         key = 0;
397                         }
398                         else
399                                 kbd_rptStatus = KS_IDLE;
400                         break;
401
402                 case KS_REPEAT:
403                         if (key & kbd_rpt_mask)
404                         {
405                                 if (now - repeat_time > repeat_rate)
406                                 {
407                                         /* Enqueue a new event in the buffer */
408                                         key = (key & kbd_rpt_mask) | K_REPEAT;
409                                         repeat_time = now;
410
411                                         /* Repeat rate acceleration */
412                                         if (repeat_rate > ms_to_ticks(KBD_REPEAT_MAXRATE))
413                                                 repeat_rate -= ms_to_ticks(KBD_REPEAT_ACCEL);
414                                 }
415                                 else
416                                         key = 0;
417                         }
418                         else
419                                 kbd_rptStatus = KS_IDLE;
420
421                         break;
422         }
423
424         return key;
425 }
426
427
428 MOD_DEFINE(kbd)
429
430 /*!
431  * Initialize keyboard ports and softtimer
432  */
433 void kbd_init(void)
434 {
435 #if CONFIG_KBD_BEEP
436         MOD_CHECK(buzzer);
437 #endif
438
439         KBD_HW_INIT;
440
441         /* Init handlers lists */
442         LIST_INIT(&kbd_handlers);
443         LIST_INIT(&kbd_rawHandlers);
444
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);
450
451         #ifdef  K_LNG_MASK
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);
457         #endif
458
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);
464
465         /* Add default keyboard handler */
466         kbd_defHandler.hook = kbd_defHandlerFunc;
467         kbd_defHandler.pri = -128; /* lowest priority */
468         kbd_addHandler(&kbd_defHandler);
469
470 #if CONFIG_KBD_OBSERVER
471         observer_InitSubject(&kbd_subject);
472 #endif
473
474 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
475
476         MOD_CHECK(timer);
477
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);
482
483 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
484
485         /* Create a timer specific thread */
486         xTaskCreate(kbd_task, "kbd", CONFIG_STACK_KBD,
487                         NULL, CONFIG_PRI_KBD, NULL);
488
489 #else
490         #error "Define keyboard poll method"
491 #endif
492
493         MOD_INIT(kbd);
494 }