1 //==========================================================================
5 // Cirrus Logic EDB7xxx eval board ASCII keyboard support functions
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
44 // Contributors: gthomas
46 // Description: Functions used to support ASCII keyboard
47 //####DESCRIPTIONEND####
49 static char kbd_server_stack[STACK_SIZE];
50 static cyg_thread kbd_server_thread_data;
51 static cyg_handle_t kbd_server_thread_handle;
53 static cyg_interrupt kbd_interrupt;
54 static cyg_handle_t kbd_interrupt_handle;
56 static cyg_mbox kbd_events_mbox;
57 static cyg_handle_t kbd_events_mbox_handle;
58 static cyg_sem_t kbd_sem;
60 static cyg_uint8 col_state[8];
61 static cyg_uint8 ext_state[8];
67 for (i = 0; i < 250; i++) ;
74 cyg_uint8 port_data, ext_data;
75 // Turn off drive (de-select) all columns
76 *(volatile cyg_uint32 *)SYSCON1 = (*(volatile cyg_uint32 *)SYSCON1 & ~SYSCON1_KBD_CTL) |
78 for (i = 0; i < 8; i++) {
80 *(volatile cyg_uint32 *)SYSCON1 = (*(volatile cyg_uint32 *)SYSCON1 & ~SYSCON1_KBD_CTL) |
82 // Small pause to let the wires charge up :-)
85 col_state[i] = port_data = *(volatile cyg_uint8 *)PADR;
86 ext_state[i] = ext_data = *(volatile cyg_uint8 *)KBD_PORT;
87 // De-Select column 'i'
88 *(volatile cyg_uint32 *)SYSCON1 = (*(volatile cyg_uint32 *)SYSCON1 & ~SYSCON1_KBD_CTL) |
90 // Allow line to go slack
93 // Turn on drive (select) all columns - necessary to see interrupts
94 *(volatile cyg_uint32 *)SYSCON1 = (*(volatile cyg_uint32 *)SYSCON1 & ~SYSCON1_KBD_CTL) |
98 static cyg_uint8 kbd_state[128]; // Known state of each key
99 static cyg_uint8 kbd_new_state[128]; // Current state of each key
102 #define KBD_Escape 0x00
113 #define KBD_NumLock 0x0B
114 #define KBD_SysReq 0x0C
115 #define KBD_ScrlLock 0x0D
116 #define KBD_Break 0x0E
128 #define KBD_Hyphen 0x1A
129 #define KBD_Equals 0x1B
130 #define KBD_BackSpace 0x1C
131 #define KBD_Home 0x1D
144 #define KBD_LeftBrkt 0x2B
145 #define KBD_RightBrkt 0x2C
146 #define KBD_BackSlash 0x2D
147 #define KBD_PageUp 0x2E
149 #define KBD_CapsLock 0x30
159 #define KBD_SemiColon 0x3A
160 #define KBD_Quote 0x3B
161 #define KBD_Enter 0x3C
162 #define KBD_PageDown 0x3D
164 #define KBD_LeftShift 0x40
172 #define KBD_Comma 0x48
173 #define KBD_Period 0x49
174 #define KBD_Slash 0x4A
175 #define KBD_RightShift 0x4B
176 #define KBD_UpArrow 0x4C
179 #define KBD_Ctrl 0x50
180 #define KBD_Function 0x51
181 #define KBD_LeftAlt 0x52
182 #define KBD_Accent 0x53
183 #define KBD_Space 0x54
184 #define KBD_RightAlt 0x55
187 #define KBD_LeftArrow 0x58
188 #define KBD_DownArrow 0x59
189 #define KBD_RightArrow 0x5A
191 #define KBD_Press 0x80 // Event has this bit when the key is pressed
192 #define KBD_Empty 0xFF
194 // Map column, bit -> keycode
195 static cyg_uint32 kbd_map[8][16] = {
197 {KBD_Escape, KBD_1, KBD_Tab, KBD_CapsLock,
198 KBD_BackSlash, KBD_Space, KBD_LeftArrow, KBD_UpArrow,
199 KBD_DownArrow, KBD_RightArrow, KBD_LeftShift, KBD_Ctrl,
200 KBD_Function, KBD_LeftAlt, KBD_RightAlt, KBD_RightShift },
202 {KBD_F5, KBD_6, KBD_T, KBD_G,
203 KBD_B, KBD_Slash, KBD_SemiColon, KBD_P,
204 KBD_Hyphen, KBD_F10, KBD_Empty, KBD_Empty,
205 KBD_Empty, KBD_Empty, KBD_Empty, KBD_Empty },
207 {KBD_F4, KBD_5, KBD_R, KBD_F,
208 KBD_V, KBD_Del, KBD_Quote, KBD_LeftBrkt,
209 KBD_Equals, KBD_NumLock, KBD_Empty, KBD_Empty,
210 KBD_Empty, KBD_Empty, KBD_Empty, KBD_Empty },
212 {KBD_F3, KBD_4, KBD_E, KBD_D,
213 KBD_C, KBD_Ins, KBD_Empty, KBD_RightBrkt,
214 KBD_BackSpace, KBD_SysReq, KBD_Empty, KBD_Empty,
215 KBD_Empty, KBD_Empty, KBD_Empty, KBD_Empty },
217 {KBD_F2, KBD_3, KBD_W, KBD_S,
218 KBD_X, KBD_Empty, KBD_Enter, KBD_Empty,
219 KBD_Empty, KBD_ScrlLock, KBD_Empty, KBD_Empty,
220 KBD_Empty, KBD_Empty, KBD_Empty, KBD_Empty },
222 {KBD_F1, KBD_2, KBD_Q, KBD_A,
223 KBD_Z, KBD_End, KBD_PageDown, KBD_PageUp,
224 KBD_Home, KBD_Break, KBD_Empty, KBD_Empty,
225 KBD_Empty, KBD_Empty, KBD_Empty, KBD_Empty },
227 {KBD_F6, KBD_7, KBD_Y, KBD_H,
228 KBD_N, KBD_Period, KBD_L, KBD_O,
229 KBD_0, KBD_F9, KBD_Empty, KBD_Empty,
230 KBD_Empty, KBD_Empty, KBD_Empty, KBD_Empty },
232 {KBD_F7, KBD_8, KBD_U, KBD_J,
233 KBD_M, KBD_Comma, KBD_K, KBD_I,
234 KBD_9, KBD_F8, KBD_Empty, KBD_Empty,
235 KBD_Empty, KBD_Empty, KBD_Empty, KBD_Empty },
238 static cyg_uint8 kbd_chars[96] = {
240 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
242 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08, 0x00, 0x00, 0x00,
244 '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\\', 0x00, 0x00,
246 0x00, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', 0x0D, 0x00, 0x00, 0x00,
248 0x00, 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0x00, 0x00, 0x00, 0x00, 0x00,
250 0x00, 0x00, 0x00, 0x00, ' ', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
253 static cyg_uint8 kbd_shifted_chars[96] = {
255 '\b', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
257 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', 0x08, 0x00, 0x00, 0x00,
259 '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '|', 0x00, 0x00,
261 0x00, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', 0x0D, 0x00, 0x00, 0x00,
263 0x00, 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 0x00, 0x00, 0x00, 0x00, 0x00,
265 0x00, 0x00, 0x00, 0x00, ' ', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
268 // This ISR is called when the keyboard interrupt occurs
270 keyboard_isr(cyg_vector_t vector, cyg_addrword_t data, HAL_SavedRegisters *regs)
272 cyg_drv_interrupt_mask(CYGNUM_HAL_INTERRUPT_KBDINT);
273 return (CYG_ISR_HANDLED|CYG_ISR_CALL_DSR); // Run the DSR
276 // This DSR handles the keyboard [logical] processing
278 keyboard_dsr(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data)
280 // Tell the keyboard processing thread to give it a shot
281 cyg_semaphore_post(&kbd_sem);
285 kbd_server(cyg_addrword_t p)
287 int col, bit, key, event, timeout;
288 diag_printf("KBD server here\n");
290 cyg_semaphore_wait(&kbd_sem);
291 // As long as keys are pressed, scan and update
294 // Wait for 20ms - time to debounce keyboard
299 for (key = 0; key < sizeof(kbd_new_state); key++) {
300 kbd_new_state[key] = 0;
302 // Check state of all keys and send events for those that change
303 for (col = 0; col < 8; col++) {
304 for (bit = 0; bit < 8; bit++) {
305 if (col_state[col] & (1<<bit)) {
306 key = kbd_map[col][bit];
307 if (key != KBD_Empty) {
308 kbd_new_state[key] = 1;
311 if (ext_state[col] & (1<<bit)) {
312 key = kbd_map[col][bit+8];
313 if (key != KBD_Empty) {
314 kbd_new_state[key] = 1;
319 // Compare new and old (known) states, generate events for changes
320 // Send events for modifier keys first.
321 for (key = 0; key < sizeof(kbd_new_state); key++) {
322 if (kbd_state[key] != kbd_new_state[key]) {
332 if (kbd_state[key]) {
337 event = key + KBD_Press;
339 kbd_state[key] = kbd_new_state[key];
342 if (!cyg_mbox_tryput(kbd_events_mbox_handle, (void *)event)) {
343 diag_printf("KBD event lost: %x\n", event);
348 // First key up events
349 for (key = 0; key < sizeof(kbd_new_state); key++) {
350 if (kbd_state[key] != kbd_new_state[key]) {
351 if (kbd_state[key]) {
356 event = key + KBD_Press;
358 if (!cyg_mbox_tryput(kbd_events_mbox_handle, (void *)event)) {
359 diag_printf("KBD event lost: %x\n", event);
362 kbd_state[key] = kbd_new_state[key];
364 // Clear interrupt (true when keys are pressed)
365 cyg_drv_interrupt_acknowledge(CYGNUM_HAL_INTERRUPT_KBDINT);
367 if (*(volatile cyg_uint32 *)INTSR2 & INTSR2_KBDINT) {
369 } else if (++timeout == 5) {
375 // Allow interrupts to happen again
376 cyg_drv_interrupt_acknowledge(CYGNUM_HAL_INTERRUPT_KBDINT);
377 cyg_drv_interrupt_unmask(CYGNUM_HAL_INTERRUPT_KBDINT);
384 // Initialize environment, setup interrupt handler
385 cyg_drv_interrupt_create(CYGNUM_HAL_INTERRUPT_KBDINT,
386 99, // Priority - what goes here?
387 0, // Data item passed to interrupt handler
388 (cyg_ISR_t *)keyboard_isr,
389 (cyg_DSR_t *)keyboard_dsr,
390 &kbd_interrupt_handle,
392 cyg_drv_interrupt_attach(kbd_interrupt_handle);
393 cyg_drv_interrupt_acknowledge(CYGNUM_HAL_INTERRUPT_KBDINT);
394 cyg_drv_interrupt_unmask(CYGNUM_HAL_INTERRUPT_KBDINT);
395 // Set up the mbox for keyboard data
396 cyg_mbox_create(&kbd_events_mbox_handle, &kbd_events_mbox);
397 // This semaphore is set when there is a keypress
398 cyg_semaphore_init(&kbd_sem, 0);
399 // Create a thread whose job it is to de-bounce the keyboard and
400 // actually process the input, turning it into a series of events
401 cyg_thread_create(10, // Priority - just a number
403 0, // initial parameter
404 "KBD_server", // Name
405 &kbd_server_stack[0], // Stack
407 &kbd_server_thread_handle, // Handle
408 &kbd_server_thread_data // Thread data structure
410 cyg_thread_resume(kbd_server_thread_handle); // Start it
413 #define MOD_Shift 0x40
414 #define MOD_Ctrl 0x20
415 #define MOD_CapsLock 0x10
416 static cyg_uint32 kbd_modifiers;
422 cyg_uint32 kbd_event;
424 kbd_event = (cyg_uint32)cyg_mbox_get(kbd_events_mbox_handle);
425 switch (kbd_event & 0x7F) {
428 if (kbd_event & KBD_Press) {
429 kbd_modifiers |= MOD_Shift;
431 kbd_modifiers &= ~MOD_Shift;
435 if (kbd_event & KBD_Press) {
436 kbd_modifiers |= MOD_Ctrl;
438 kbd_modifiers &= ~MOD_Ctrl;
442 if (kbd_event & KBD_Press) {
443 kbd_modifiers ^= MOD_CapsLock;
451 // Return character [if one has arrived]
452 if (kbd_event & KBD_Press) {
453 if (kbd_modifiers & (MOD_Shift|MOD_CapsLock)) {
454 ch = kbd_shifted_chars[kbd_event & 0x7F];
456 ch = kbd_chars[kbd_event & 0x7F];
458 if (kbd_modifiers & MOD_Ctrl) {