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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [sw/] [dev/] [keypad.c] - Blame information for rev 45

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 29 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    keypad.c
4
//
5
// Project:     CMod S6 System on a Chip, ZipCPU demonstration project
6
//
7
// Purpose:     A device driver (task) for the 16 character keypad.
8
//
9
// Creator:     Dan Gisselquist, Ph.D.
10
//              Gisselquist Technology, LLC
11
//
12
////////////////////////////////////////////////////////////////////////////////
13
//
14 45 dgisselq
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
15 29 dgisselq
//
16
// This program is free software (firmware): you can redistribute it and/or
17
// modify it under the terms of  the GNU General Public License as published
18
// by the Free Software Foundation, either version 3 of the License, or (at
19
// your option) any later version.
20
//
21
// This program is distributed in the hope that it will be useful, but WITHOUT
22
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
23
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24
// for more details.
25
//
26
// You should have received a copy of the GNU General Public License along
27
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
28
// target there if the PDF file isn't present.)  If not, see
29
// <http://www.gnu.org/licenses/> for a copy.
30
//
31
// License:     GPL, v3, as defined and found on www.gnu.org,
32
//              http://www.gnu.org/licenses/gpl.html
33
//
34
//
35
////////////////////////////////////////////////////////////////////////////////
36
//
37
//
38
#include "board.h"
39
#ifdef ZIPOS
40
#include "../zipos/ktraps.h"
41
#include "../zipos/kfildes.h"
42
#endif
43
 
44 45 dgisselq
static const char keymap[] = {
45 29 dgisselq
        0x01,0x02,0x03,0x0a,
46
        0x04,0x05,0x06,0x0b,
47
        0x07,0x08,0x09,0x0c,
48
        0x00,0x0f,0x0e,0x0d
49
};
50
 
51
int     keypadread(void) {
52
        int     row, col, key;
53 45 dgisselq
        volatile IOSPACE *const sys = _sys;
54 29 dgisselq
 
55
        row = sys->io_spio & 0x0f00;
56
        if (row != 0x0f00) {
57
                // If a button is still pressed .. 
58
                //
59
                // Check columns one and two to see if they were responsible
60
                // for the button
61
                sys->io_spio = 0x0cf00;
62
                // Get the result
63
                col = sys->io_spio;
64
                if ((col&0x0f00)!=0x0f00) {
65
                        // Column one or two is pressed
66
                        sys->io_spio = 0x0ef00;
67
                        col = sys->io_spio;
68
                        if ((col&0x0f00)!=0x0f00)
69
                                col = 0;
70
                        else
71
                                col = 1;
72
                } else {
73
                        // Must be column three or four
74
                        sys->io_spio = 0x7f00;
75
                        col = sys->io_spio;
76
                        if ((col & 0x0f00)!= 0x0f00) // Column 4
77
                                col = 3;
78
                        else
79
                                col = 2;
80
                } sys->io_spio = 0x0f00; // Reset column pins to zero
81
                if (row == (int)(sys->io_spio & 0x0f00)) {
82
                        // The key didn't change, so we might have something
83
                        row >>= 8;
84
                        row &= 0x0f;
85
                        row ^= 0x0f;
86
 
87
                        // Found the pin
88
                        if (row == 1)
89
                                row=0;
90
                        else if (row == 2)
91
                                row = 1;
92
                        else if (row == 4)
93
                                row = 2;
94
                        else if (row == 8)
95
                                row = 3;
96
                        else
97
                                // Two or more buttons were pressed
98
                                // -- declare an error
99
                                row = -1;
100
 
101
                        if (row>=0) {
102
                                key = (row |(col<<2));
103
                                key = keymap[key];
104
                        } else key = -1;
105
                } else key = -1;
106
        } else key = -1;
107
 
108
        if (sys->io_pic & INT_ENABLE)
109
                sys->io_pic = INT_ENABLE|INT_KEYPAD;
110
        else sys->io_pic = INT_KEYPAD;
111
 
112
        return key;
113
}
114
 
115
void    keypad_wait_for_release(void) {
116 45 dgisselq
        volatile IOSPACE * const sys = _sys;
117 29 dgisselq
        sys->io_spio = 0x0f00;
118
        while((sys->io_spio & 0x0f00)!=0x0f00)
119
#ifdef  ZIPOS
120
                wait(0,2);
121
#else
122
                ;
123
#endif
124
}
125
 
126
#ifdef ZIPOS
127
void keypad_task(void) {
128 45 dgisselq
        clear(INT_KEYPAD, 0);
129 29 dgisselq
        while(1) {
130 45 dgisselq
                int     key;    char ch;
131 29 dgisselq
                wait(INT_KEYPAD,-1); // Automatically clears
132
                key = keypadread();
133 45 dgisselq
                if (key >= 0) {
134
                        ch = key;
135
                        write(FILENO_STDOUT, &ch, 1);
136
                }
137 29 dgisselq
                // Prepare for the next key
138 45 dgisselq
                clear(INT_KEYPAD, 0);
139 29 dgisselq
        }
140
}
141
#endif

powered by: WebSVN 2.1.0

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