OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [arm/] [arm9/] [aaed2000/] [current/] [src/] [kbd_drvr.c] - Blame information for rev 853

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//        kbd_drvr.c
4
//
5
//        Agilent AAED2000 - keyboard driver
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
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      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):     gthomas
43
// Contributors:  gthomas
44
// Date:          2001-11-03
45
// Description:   Keyboard driver
46
//####DESCRIPTIONEND####
47
 
48
#include <pkgconf/hal.h>
49
 
50
#include <cyg/infra/diag.h>
51
#include <cyg/hal/hal_io.h>       // IO macros
52
#include <cyg/hal/hal_if.h>       // Virtual vector support
53
#include <cyg/hal/hal_arch.h>     // Register state info
54
#include <cyg/hal/hal_intr.h>     // HAL interrupt macros
55
 
56
#include <cyg/hal/aaed2000.h>  // Board definitions
57
#include <cyg/hal/hal_cache.h>
58
 
59
//-----------------------------------------------------------------------------
60
// Keyboard definitions
61
 
62
// Standard [PC keyboard] scan codes
63
 
64
#define LSHIFT          0x2a
65
#define RSHIFT          0x36
66
#define CTRL            0x1d
67
#define ALT             0x38
68
#define CAPS            0x3a
69
#define NUMS            0x45
70
 
71
#define BREAK           0x80
72
 
73
// Bits for KBFlags
74
 
75
#define KBNormal        0x0000
76
#define KBShift         0x0001
77
#define KBCtrl          0x0002
78
#define KBAlt           0x0004
79
#define KBIndex         0x0007  // mask for the above
80
 
81
#define KBExtend        0x0010
82
#define KBAck           0x0020
83
#define KBResend        0x0040
84
#define KBShiftL        (0x0080 | KBShift)
85
#define KBShiftR        (0x0100 | KBShift)
86
#define KBCtrlL         (0x0200 | KBCtrl)
87
#define KBCtrlR         (0x0400 | KBCtrl)
88
#define KBAltL          (0x0800 | KBAlt)
89
#define KBAltR          (0x1000 | KBAlt)
90
#define KBCapsLock      0x2000
91
#define KBNumLock       0x4000
92
 
93
#define KBArrowUp       0x48
94
#define KBArrowRight    0x4D
95
#define KBArrowLeft     0x4B
96
#define KBArrowDown     0x50
97
 
98
//-----------------------------------------------------------------------------
99
// Keyboard Variables
100
 
101
static  int     KBFlags = 0;
102
 
103
static  CYG_BYTE        KBPending = 0xFF;
104
 
105
static  CYG_BYTE        KBScanTable[128][4] = {
106
//      Normal          Shift           Control         Alt
107
// 0x00
108
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
109
    {   0x1b,           0x1b,           0x1b,           0xFF,   },
110
    {   '1',            '!',            0xFF,           0xFF,   },
111
    {   '2',            '@',            0xFF,           0xFF,   },
112
    {   '3',            '#',            0xFF,           0xFF,   },
113
    {   '4',            '$',            0xFF,           0xFF,   },
114
    {   '5',            '%',            0xFF,           0xFF,   },
115
    {   '6',            '^',            0xFF,           0xFF,   },
116
    {   '7',            '&',            0xFF,           0xFF,   },
117
    {   '8',            '*',            0xFF,           0xFF,   },
118
    {   '9',            '(',            0xFF,           0xFF,   },
119
    {   '0',            ')',            0xFF,           0xFF,   },
120
    {   '-',            '_',            0xFF,           0xFF,   },
121
    {   '=',            '+',            0xFF,           0xFF,   },
122
    {   '\b',           '\b',           0xFF,           0xFF,   },
123
    {   '\t',           '\t',           0xFF,           0xFF,   },
124
// 0x10
125
    {   'q',            'Q',            0x11,           0xFF,   },
126
    {   'w',            'W',            0x17,           0xFF,   },
127
    {   'e',            'E',            0x05,           0xFF,   },
128
    {   'r',            'R',            0x12,           0xFF,   },
129
    {   't',            'T',            0x14,           0xFF,   },
130
    {   'y',            'Y',            0x19,           0xFF,   },
131
    {   'u',            'U',            0x15,           0xFF,   },
132
    {   'i',            'I',            0x09,           0xFF,   },
133
    {   'o',            'O',            0x0F,           0xFF,   },
134
    {   'p',            'P',            0x10,           0xFF,   },
135
    {   '[',            '{',            0x1b,           0xFF,   },
136
    {   ']',            '}',            0x1d,           0xFF,   },
137
    {   '\r',           '\r',           '\n',           0xFF,   },
138
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
139
    {   'a',            'A',            0x01,           0xFF,   },
140
    {   's',            'S',            0x13,           0xFF,   },
141
// 0x20
142
    {   'd',            'D',            0x04,           0xFF,   },
143
    {   'f',            'F',            0x06,           0xFF,   },
144
    {   'g',            'G',            0x07,           0xFF,   },
145
    {   'h',            'H',            0x08,           0xFF,   },
146
    {   'j',            'J',            0x0a,           0xFF,   },
147
    {   'k',            'K',            0x0b,           0xFF,   },
148
    {   'l',            'L',            0x0c,           0xFF,   },
149
    {   ';',            ':',            0xFF,           0xFF,   },
150
    {   0x27,           '"',            0xFF,           0xFF,   },
151
    {   '`',            '~',            0xFF,           0xFF,   },
152
    {   '`',            '~',            0xFF,           0xFF,   },
153
    {   '\\',           '|',            0x1C,           0xFF,   },
154
    {   'z',            'Z',            0x1A,           0xFF,   },
155
    {   'x',            'X',            0x18,           0xFF,   },
156
    {   'c',            'C',            0x03,           0xFF,   },
157
    {   'v',            'V',            0x16,           0xFF,   },
158
// 0x30
159
    {   'b',            'B',            0x02,           0xFF,   },
160
    {   'n',            'N',            0x0E,           0xFF,   },
161
    {   'm',            'M',            0x0D,           0xFF,   },
162
    {   ',',            '<',            0xFF,           0xFF,   },
163
    {   '.',            '>',            0xFF,           0xFF,   },
164
    {   '/',            '?',            0xFF,           0xFF,   },
165
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
166
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
167
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
168
    {   ' ',            ' ',            ' ',            ' ',    },
169
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
170
    {   0xF1,           0xE1,           0xFF,           0xFF,   },
171
    {   0xF2,           0xE2,           0xFF,           0xFF,   },
172
    {   0xF3,           0xE3,           0xFF,           0xFF,   },
173
    {   0xF4,           0xE4,           0xFF,           0xFF,   },
174
    {   0xF5,           0xE5,           0xFF,           0xFF,   },
175
// 0x40
176
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
177
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
178
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
179
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
180
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
181
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
182
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
183
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
184
 
185
    {   0x15,           0x15,           0x15,           0x15,   },
186
    {   0x10,           0x10,           0x10,           0x10,   },
187
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
188
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
189
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
190
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
191
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
192
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
193
// 0x50
194
    {   0x04,           0x04,           0x04,           0x04,   },
195
    {   0x0e,           0x0e,           0x0e,           0x0e,   },
196
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
197
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
198
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
199
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
200
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
201
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
202
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
203
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
204
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
205
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
206
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
207
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
208
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
209
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
210
// 0x60
211
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
212
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
213
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
214
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
215
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
216
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
217
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
218
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
219
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
220
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
221
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
222
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
223
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
224
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
225
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
226
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
227
// 0x70
228
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
229
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
230
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
231
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
232
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
233
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
234
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
235
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
236
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
237
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
238
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
239
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
240
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
241
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
242
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
243
    {   0xFF,           0xFF,           0xFF,           0xFF,   },
244
};
245
 
246
static int KBIndexTab[8] = { 0, 1, 2, 2, 3, 3, 3, 3 };
247
 
248
//-----------------------------------------------------------------------------
249
 
250
#define AAED2000_KBD_COLS 8
251
#define AAED2000_KBD_ROWS 12
252
 
253
static unsigned int KB_rowState[AAED2000_KBD_COLS];  // Last known state Row x Col
254
 
255
// Map AAED2000 row/col to [PC standard] scancodes
256
static const int scancode[AAED2000_KBD_ROWS][AAED2000_KBD_COLS] =
257
{
258
    {0x0A/*9*/, 0x25/*k*/, 0x17/*i*/, 0x09/*8*/, 0x24/*j*/, 0x15/*y*/, 0x08/*7*/, 0x00/*?*/},
259
    {0x0B/*0*/, 0x32/*m*/, 0x19/*p*/, 0x26/*l*/, 0x23/*h*/, 0x16/*u*/, 0x07/*8*/, 0x00/*?*/},
260
    {0x0C/*-*/, 0x18/*o*/, 0x1A/*{*/, 0x27/*:*/, 0x30/*b*/, 0x31/*n*/, 0x22/*g*/, 0x33/*<*/},
261
    {0x0D/*+*/, 0x34/*>*/, 0x1B/*}*/, 0x28/*'*/, 0x48/*?*/, 0x14/*t*/, 0x00/*?*/, 0x00/*?*/},
262
    {0x0E/*?*/, 0x35/*?*/, 0x2B/*|*/, 0x1C/*?*/, 0x4B/*?*/, 0x00/*?*/, 0x06/*5*/, 0x50/*?*/},
263
    {0x00/*?*/, 0x00/*?*/, 0x00/*?*/, 0x00/*?*/, 0x00/*?*/, 0x00/*?*/, 0x00/*?*/, 0x00/*?*/},
264
    {0x39/* */, 0x21/*f*/, 0x00/*?*/, 0x00/*?*/, 0x2E/*c*/, 0x13/*r*/, 0x05/*4*/, 0x2F/*v*/},
265
    {0x4D/*?*/, 0x00/*?*/, 0x00/*?*/, 0x00/*?*/, 0x20/*d*/, 0x12/*e*/, 0x04/*3*/, 0x38/*?*/},
266
    {0x00/*?*/, 0x00/*?*/, 0x00/*?*/, 0x00/*?*/, 0x1F/*s*/, 0x11/*w*/, 0x03/*2*/, 0x2D/*x*/},
267
    {0x53/*?*/, 0x00/*?*/, 0x00/*?*/, 0x00/*?*/, 0x1E/*a*/, 0x10/*q*/, 0x02/*1*/, 0x2C/*z*/},
268
    {0x00/*?*/, 0x2A/*?*/, 0x36/*?*/, 0x00/*?*/, 0x00/*?*/, 0x00/*?*/, 0x00/*?*/, 0x00/*?*/},
269
    {0x00/*?*/, 0x00/*?*/, 0x00/*?*/, 0x00/*?*/, 0x3A/*?*/, 0x0F/*?*/, 0x29/*~*/, 0x1D/*?*/}
270
};
271
 
272
static bool
273
KeyboardScan(CYG_BYTE *code)
274
{
275
    int row, col, i, down, up;
276
    unsigned int state, prev_state;
277
    int stable;
278
 
279
    for (col = 0;  col < AAED2000_KBD_COLS;  col++) {
280
        HAL_WRITE_UINT8(AAEC_KSCAN, col+8);  // Assert column #n
281
 
282
        for (i = 0, stable = 0, prev_state = 0;
283
             (i < 1000) && (stable < 120);  i++) {
284
            HAL_READ_UINT32(AAED_EXT_GPIO, state);
285
            if (state != prev_state) {
286
                prev_state = state;
287
                stable = 0;
288
            } else {
289
                stable++;
290
            }
291
        }
292
        state &= (1<<AAED2000_KBD_ROWS)-1;   // Mask off unused bits
293
        if (state != KB_rowState[col]) {
294
            // State changed
295
            down = state & ~KB_rowState[col];  // Bits that went 0->1
296
            up = ~state & KB_rowState[col];    // Bits that went 1->0
297
            for (row = 0;  row < AAED2000_KBD_ROWS;  row++) {
298
                if ((up|down) & (1<<row)) {
299
                    *code = scancode[row][col] | (up ? BREAK : 0);
300
                    // Only remember the bit we reported
301
                    KB_rowState[col] &= ~(1<<row);
302
                    state &= (1<<row);
303
                    KB_rowState[col] |= state;
304
                    return true;
305
                }
306
            }
307
        }
308
    }
309
    HAL_WRITE_UINT8(AAEC_KSCAN, 0x07);  // Turn off drivers
310
    return false;
311
}
312
 
313
//-----------------------------------------------------------------------------
314
 
315
static CYG_BYTE
316
KeyboardAscii(CYG_BYTE scancode)
317
{
318
    CYG_BYTE ascii = 0xFF;
319
 
320
    // Start by handling all shift/ctl keys:
321
 
322
    switch( scancode ) {
323
    case 0xe0:
324
        KBFlags |= KBExtend;
325
        return 0xFF;
326
 
327
    case 0xfa:
328
        KBFlags |= KBAck;
329
        return 0xFF;
330
 
331
    case 0xfe:
332
        KBFlags |= KBResend;
333
        return 0xFF;
334
 
335
    case LSHIFT:
336
        KBFlags |= KBShiftL;
337
        return 0xFF;
338
 
339
    case LSHIFT | BREAK:
340
        KBFlags &= ~KBShiftL;
341
        return 0xFF;
342
 
343
    case RSHIFT:
344
        KBFlags |= KBShiftR;
345
        return 0xFF;
346
 
347
    case RSHIFT | BREAK:
348
        KBFlags &= ~KBShiftR;
349
        return 0xFF;
350
 
351
    case CTRL:
352
        if( KBFlags & KBExtend )
353
        {
354
            KBFlags |= KBCtrlR;
355
            KBFlags &= ~KBExtend;
356
        }
357
        else    KBFlags |= KBCtrlL;
358
        return 0xFF;
359
 
360
    case CTRL | BREAK:
361
        if( KBFlags & KBExtend )
362
        {
363
            KBFlags &= ~KBCtrlR;
364
            KBFlags &= ~KBExtend;
365
        }
366
        else    KBFlags &= ~KBCtrlL;
367
        return 0xFF;
368
 
369
 
370
    case ALT:
371
        if( KBFlags & KBExtend )
372
        {
373
            KBFlags |= KBAltR;
374
            KBFlags &= ~KBExtend;
375
        }
376
        else    KBFlags |= KBAltL;
377
        return 0xFF;
378
 
379
    case ALT | BREAK:
380
        if( KBFlags & KBExtend )
381
        {
382
            KBFlags &= ~KBAltR;
383
            KBFlags &= ~KBExtend;
384
        }
385
        else    KBFlags &= ~KBAltL;
386
        return 0xFF;
387
 
388
    case CAPS:
389
        KBFlags ^= KBCapsLock;
390
    case CAPS | BREAK:
391
        return 0xFF;
392
 
393
    case NUMS:
394
        KBFlags ^= KBNumLock;
395
    case NUMS | BREAK:
396
        return 0xFF;
397
 
398
    case KBArrowUp:
399
    case KBArrowDown:
400
#if 0 // Not really needed on this display
401
        screen_pan = 0;
402
        lcd_refresh();
403
#endif
404
        break;
405
    case KBArrowLeft:
406
#if 0 // Not really needed on this display
407
        screen_pan -= SCREEN_PAN;
408
        if (screen_pan < 0) screen_pan = 0;
409
        lcd_refresh();
410
#endif
411
        break;
412
    case KBArrowRight:
413
#if 0 // Not really needed on this display
414
        screen_pan += SCREEN_PAN;
415
        if (screen_pan > (SCREEN_WIDTH-SCREEN_PAN)) screen_pan = SCREEN_WIDTH-SCREEN_PAN;
416
        lcd_refresh();
417
#endif
418
        break;
419
 
420
    }
421
 
422
    // Clear Extend flag if set
423
    KBFlags &= ~KBExtend;
424
 
425
    // Ignore all other BREAK codes
426
    if( scancode & 0x80 ) return 0xFF;
427
 
428
    // Here the scancode is for something we can turn
429
    // into an ASCII value
430
 
431
    ascii = KBScanTable[scancode & 0x7F][KBIndexTab[KBFlags & KBIndex]];
432
 
433
    return ascii;
434
} /* KeyboardAscii */
435
 
436
//-----------------------------------------------------------------------------
437
 
438
int
439
aaed2000_KeyboardTest(void)
440
{
441
    // If there is a pending character, return True
442
    if( KBPending != 0xFF ) return true;
443
 
444
    // If there is something waiting at the port, get it
445
    for(;;) {
446
        CYG_BYTE code, c;
447
 
448
        if (!KeyboardScan(&code)) {
449
            // No new activity
450
            break;
451
        }
452
 
453
        // Translate to ASCII
454
        c = KeyboardAscii(code);
455
 
456
        // if it is a real ASCII char, save it and
457
        // return True.
458
        if( c != 0xFF ) {
459
            KBPending = c;
460
            return true;
461
        }
462
    }
463
 
464
    // Otherwise return False
465
    return false;
466
 
467
} /* KeyboardTest */
468
 
469
int
470
aaed2000_KeyboardGetc(void)
471
{
472
    unsigned char ch = KBPending;
473
    KBPending = 0xFF;
474
    return ch;
475
}
476
 
477
cyg_bool
478
aaed2000_KeyboardInit(void)
479
{
480
    int i;
481
 
482
    for (i = 0; i < AAED2000_KBD_COLS;  i++) {
483
        KB_rowState[i] = 0;
484
    }
485
    KBFlags = 0;
486
 
487
    return true;
488
} /* KeyboardInit */

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.