Use new naming convention for list macros.
[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.4  2006/02/24 00:27:14  bernie
21  *#* Use new naming convention for list macros.
22  *#*
23  *#* Revision 1.3  2006/02/17 21:15:42  bernie
24  *#* Add MOD_CHECK() checks.
25  *#*
26  *#* Revision 1.2  2006/02/10 12:36:20  bernie
27  *#* Add preliminary FreeRTOS support; Enforce CONFIG_* definitions.
28  *#*
29  *#* Revision 1.1  2005/06/27 21:28:45  bernie
30  *#* Import generic keyboard driver.
31  *#*
32  *#*/
33
34 #include <hw_kbd.h>
35
36 #include <drv/timer.h>
37 #include <drv/kbd.h>
38 #include <drv/buzzer.h>
39
40 #include <cfg/debug.h>
41 #include <cfg/module.h>
42
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
46 #endif
47
48
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 */
52
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 */
57
58 #define KBD_LNG_DELAY     1000  /*!< (ms) Keyboard long pression keys delay */
59
60
61 /*! Status for keyboard repeat state machine */
62 static enum { KS_IDLE, KS_REPDELAY, KS_REPEAT } kbd_rptStatus;
63
64
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 */
67
68 static Timer kbd_timer;            /*!< Keyboard softtimer */
69
70 static List kbd_rawHandlers;       /*!< Raw keyboard handlers */
71 static List kbd_handlers;          /*!< Cooked keyboard handlers */
72
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 */
76 #ifdef  K_LNG_MASK
77 static KbdHandler kbd_lngHandler;  /*!< Long pression keys handler */
78 #endif
79
80
81
82 /*!
83  * Poll keyboard and dispatch keys to handlers.
84  *
85  * Read the key states and invoke all keyboard
86  * handlers to process the new state.
87  *
88  * Call this function periodically using a software
89  * timer, an interrupt or a process.
90  */
91 static void kbd_poll(void)
92 {
93         /*! Currently depressed key */
94         static keymask_t current_key;
95
96         struct KbdHandler *handler;
97         keymask_t key = kbd_readkeys();
98
99         /* Call raw input handlers */
100         FOREACH_NODE(handler, &kbd_rawHandlers)
101                 key = handler->hook(key);
102
103         /* If this key was not previously pressed */
104         if (key != current_key)
105         {
106                 /* Remember last key */
107                 current_key = key;
108
109                 /* Call cooked input handlers */
110                 FOREACH_NODE(handler, &kbd_handlers)
111                         key = handler->hook(key);
112         }
113 }
114
115 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
116
117 /*!
118  * Keyboard soft-irq handler.
119  */
120 static void kbd_softint(UNUSED_ARG(iptr_t, arg))
121 {
122         kbd_poll();
123         timer_add(&kbd_timer);
124 }
125
126 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
127
128 #include "FreeRTOS.h"
129 #include "task.h"
130
131 static portTASK_FUNCTION(kbd_poll, arg)
132 {
133         for (;;)
134         {
135                 kbd_poll(0);
136                 timer_delay(KBD_CHECK_INTERVAL);
137         }
138 }
139
140 #endif /* CONFIG_KBD_POLL */
141
142 /*!
143  * \brief Read a key from the keyboard buffer.
144  *
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.
148  *
149  * \note This function is \b not interrupt safe!
150  *
151  * \return The mask of depressed keys or 0 if no keys are depressed.
152  *
153  */
154 keymask_t kbd_peek(void)
155 {
156         keymask_t key = 0;
157
158 // FIXME
159         extern void schedule(void);
160         schedule();
161
162         /* Extract an event from the keyboard buffer */
163         IRQ_DISABLE;
164         if (kbd_cnt)
165         {
166                 --kbd_cnt;
167                 key = kbd_buf;
168         }
169         IRQ_ENABLE;
170
171         return key;
172 }
173
174
175 /*!
176  * Wait for a keypress and return the mask of depressed keys.
177  *
178  * \note This function is \b not interrupt safe!
179  */
180 keymask_t kbd_get(void)
181 {
182         keymask_t key;
183
184         while (!(key = kbd_peek())) {}
185         return key;
186 }
187
188
189 /*!
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.
193  */
194 keymask_t kbd_get_timeout(mtime_t timeout)
195 {
196         keymask_t key;
197
198         ticks_t start = timer_clock();
199         ticks_t stop  = ms_to_ticks(timeout);
200         do
201         {
202                 if ((key = kbd_peek()))
203                         return key;
204         }
205         while (timer_clock() - start < stop);
206
207         return K_TIMEOUT;
208 }
209
210
211 void kbd_addHandler(struct KbdHandler *handler)
212 {
213         KbdHandler *node;
214         List *list;
215
216         cpuflags_t flags;
217         IRQ_SAVE_DISABLE(flags);
218
219         /* Choose between raw and coocked handlers list */
220         list = (handler->flags & KHF_RAWKEYS) ?
221                 &kbd_rawHandlers : &kbd_handlers;
222
223         /*
224          * Search for the first node whose priority
225          * is lower than the timer we want to add.
226          */
227         FOREACH_NODE(node,list)
228                 if (node->pri < handler->pri)
229                         break;
230
231         /* Enqueue handler in the handlers chain */
232         INSERT_BEFORE(&handler->link, &node->link);
233
234         IRQ_RESTORE(flags);
235 }
236
237
238 void kbd_remHandler(struct KbdHandler *handler)
239 {
240         /* Remove the handler */
241         ATOMIC(REMOVE(&handler->link));
242 }
243
244
245 /*!
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.
250  */
251 static keymask_t kbd_defHandlerFunc(keymask_t key)
252 {
253         if (key)
254         {
255                 /* Force a single event in kbd buffer */
256                 kbd_buf = key;
257                 kbd_cnt = 1;
258
259 #if KBD_BEEP_TIME
260                 if (!(key & K_REPEAT))
261                         buz_beep(KBD_BEEP_TIME);
262 #endif
263         }
264
265         /* Eat all input */
266         return 0;
267 }
268
269 /*!
270  * Handle keyboard debounce
271  */
272 static keymask_t kbd_debHandlerFunc(keymask_t key)
273 {
274         /*! Buffer for debounce */
275         static keymask_t debounce_key;
276
277         /*! Timer for keyboard debounce */
278         static ticks_t debounce_time;
279
280         /*! Key aquired after debounce */
281         static keymask_t new_key;
282
283
284         ticks_t now = timer_clock();
285
286         if (key != debounce_key)
287         {
288                 /* Reset debounce timer */
289                 debounce_key = key;
290                 debounce_time = now;
291         }
292         else if ((new_key != debounce_key)
293                 && (now - debounce_time > ms_to_ticks(KBD_DEBOUNCE_TIME)))
294         {
295                 new_key = debounce_key;
296                 debounce_time = now;
297         }
298
299         return new_key;
300 }
301
302 #ifdef  K_LNG_MASK
303 /*!
304  * Handle long pression keys
305  */
306 static keymask_t kbd_lngHandlerFunc(keymask_t key)
307 {
308         static ticks_t stop;
309         ticks_t now = timer_clock();
310
311         if (key & K_LNG_MASK)
312         {
313                 if (now - stop > 0)
314                         key &= K_LNG_MASK;
315                 else
316                         key &= ~K_LNG_MASK;
317         }
318         else
319                 stop = now + ms_to_ticks(KBD_LNG_DELAY);
320         return key;
321 }
322 #endif
323
324 /*!
325  * Handle keyboard repeat
326  */
327 static keymask_t kbd_rptHandlerFunc(keymask_t key)
328 {
329         /*! Timer for keyboard repeat events */
330         static ticks_t repeat_time;
331
332         static ticks_t repeat_rate; /*! Current repeat rate (for acceleration) */
333
334         ticks_t now = timer_clock();
335
336         switch (kbd_rptStatus)
337         {
338                 case KS_IDLE:
339                         if (key & K_RPT_MASK)
340                         {
341                                 repeat_time = now;
342                                 kbd_rptStatus = KS_REPDELAY;
343                         }
344                         break;
345
346                 case KS_REPDELAY:
347                         if (key & K_RPT_MASK)
348                         {
349                                 if (now - repeat_time > ms_to_ticks(KBD_REPEAT_DELAY))
350                                 {
351                                         key = (key & K_RPT_MASK) | K_REPEAT;
352                                         repeat_time = now;
353                                         repeat_rate = ms_to_ticks(KBD_REPEAT_RATE);
354                                         kbd_rptStatus = KS_REPEAT;
355                                 }
356                                 else
357                                         key = 0;
358                         }
359                         else
360                                 kbd_rptStatus = KS_IDLE;
361                         break;
362
363                 case KS_REPEAT:
364                         if (key & K_RPT_MASK)
365                         {
366                                 if (now - repeat_time > repeat_rate)
367                                 {
368                                         /* Enqueue a new event in the buffer */
369                                         key = (key & K_RPT_MASK) | K_REPEAT;
370                                         repeat_time = now;
371
372                                         /* Repeat rate acceleration */
373                                         if (repeat_rate > ms_to_ticks(KBD_REPEAT_MAXRATE))
374                                                 repeat_rate -= ms_to_ticks(KBD_REPEAT_ACCEL);
375                                 }
376                                 else
377                                         key = 0;
378                         }
379                         else
380                                 kbd_rptStatus = KS_IDLE;
381
382                         break;
383         }
384
385         return key;
386 }
387
388
389 MOD_DEFINE(kbd)
390
391 /*!
392  * Initialize keyboard ports and softtimer
393  */
394 void kbd_init(void)
395 {
396         MOD_CHECK(buzzer);
397
398         KBD_HW_INIT;
399
400         /* Init handlers lists */
401         LIST_INIT(&kbd_handlers);
402         LIST_INIT(&kbd_rawHandlers);
403
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);
409
410         #ifdef  K_LNG_MASK
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);
416         #endif
417
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);
423
424         /* Add default keyboard handler */
425         kbd_defHandler.hook = kbd_defHandlerFunc;
426         kbd_defHandler.pri = -128; /* lowest priority */
427         kbd_addHandler(&kbd_defHandler);
428
429 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
430
431         MOD_CHECK(timer);
432
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);
437
438 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
439
440         /* Create a timer specific thread */
441         xTaskCreate(kbd_poll, "kbd_poll", CONFIG_STACK_KBD,
442                         NULL, CONFIG_PRI_KBD, NULL);
443
444 #else
445         #error "Define keyboard poll method"
446 #endif
447
448         MOD_INIT(kbd);
449 }