]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - converter/pc98_usb/matrix.c
pc98_usb: Add keyboard identify support
[max/tmk_keyboard.git] / converter / pc98_usb / matrix.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include <avr/io.h>
21 #include <util/delay.h>
22 #include "print.h"
23 #include "util.h"
24 #include "matrix.h"
25 #include "led.h"
26 #include "debug.h"
27 #include "protocol/serial.h"
28
29
30 /*
31  * Matrix Array usage:
32  *
33  * ROW: 16(4bits)
34  * COL:  8(3bits)
35  *
36  *    8bit wide
37  *   +---------+
38  *  0|00 ... 07|
39  *  1|08 ... 0F|
40  *  :|   ...   |
41  *  :|   ...   |
42  *  E|70 ... 77|
43  *  F|78 ... 7F|
44  *   +---------+
45  */
46 static uint8_t matrix[MATRIX_ROWS];
47 #define ROW(code)      ((code>>3)&0xF)
48 #define COL(code)      (code&0x07)
49
50
51 static void pc98_send(uint8_t data)
52 {
53     xprintf("s%02X ", data);
54     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
55     _delay_ms(1);
56     serial_send(data);
57     _delay_ms(1);
58     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
59 }
60
61 static int16_t pc98_wait_response(void)
62 {
63     int16_t code = -1;
64     uint8_t timeout = 255;
65     while (timeout-- && (code = serial_recv2()) == -1) _delay_ms(1);
66     xprintf("r%04X ", code);
67     return code;
68 }
69
70 static void pc98_inhibit_repeat(void)
71 {
72     uint16_t code;
73 RETRY:
74     pc98_send(0x9C);
75     code = pc98_wait_response();
76     if (code != -1) dprintf("send 9C: %02X\n", code);
77     if (code != 0xFA) return;
78
79     pc98_send(0x70);
80     code = pc98_wait_response();
81     if (code != -1) dprintf("send 70: %02X\n", code);
82     if (code != 0xFA) goto RETRY;
83 }
84
85 static bool pc98_is_newtype(void)
86 {
87     uint16_t code;
88     pc98_send(0x9F);
89     code = pc98_wait_response();
90     if (code != 0xFA) return false;
91
92     code = pc98_wait_response();
93     if (code != 0xA0) return false;
94
95     code = pc98_wait_response();
96     if (code != 0x80) return false;
97
98     return true;
99 }
100
101 static uint8_t pc98_led = 0;
102 static void pc98_led_set(void)
103 {
104     uint16_t code;
105 RETRY:
106     pc98_send(0x9D);
107     code = pc98_wait_response();
108     if (code != -1) dprintf("send 9D: %02X\n", code);
109     if (code != 0xFA) return;
110
111     pc98_send(pc98_led);
112     code = pc98_wait_response();
113     if (code != -1) dprintf("send %02X: %02X\n", pc98_led, code);
114     if (code != 0xFA) goto RETRY;
115 }
116
117 void matrix_init(void)
118 {
119     PC98_RST_DDR |= (1<<PC98_RST_BIT);
120     PC98_RDY_DDR |= (1<<PC98_RDY_BIT);
121     PC98_RTY_DDR |= (1<<PC98_RTY_BIT);
122     PC98_RST_PORT |= (1<<PC98_RST_BIT);
123     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
124     PC98_RTY_PORT |= (1<<PC98_RTY_BIT);
125
126
127     serial_init();
128
129     // PC98 reset
130     // https://archive.org/stream/PC9800TechnicalDataBookHARDWARE1993/PC-9800TechnicalDataBook_HARDWARE1993#page/n359
131     PC98_RDY_PORT |=  (1<<PC98_RDY_BIT);    // RDY: high
132     PC98_RST_PORT &= ~(1<<PC98_RST_BIT);    // RST: low
133     _delay_us(15);                          // > 13us
134     PC98_RST_PORT |= (1<<PC98_RST_BIT);     // RST: high
135
136     _delay_ms(50);
137     if (pc98_is_newtype()) xprintf("new type\n"); else xprintf("old type\n");
138     pc98_inhibit_repeat();
139
140     // initialize matrix state: all keys off
141     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
142
143     // ready to receive from keyboard
144     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);    // RDY: low
145
146     return;
147 }
148
149 uint8_t matrix_scan(void)
150 {
151     uint16_t code;
152     code = serial_recv2();
153     if (code == -1) {
154 #ifdef PC98_LED_CONTROL
155         // Before sending command  we have to make sure that there is no unprocessed key in queue
156         // otherwise keys will be missed during sending command
157         if (pc98_led) {
158             pc98_led_set();
159             pc98_led = 0;
160         }
161 #endif
162         return 0;
163     }
164
165     xprintf("r%02X ", code);
166
167     if (code&0x80) {
168         // break code
169         if (matrix_is_on(ROW(code), COL(code))) {
170             matrix[ROW(code)] &= ~(1<<COL(code));
171         }
172     } else {
173         // make code
174         if (!matrix_is_on(ROW(code), COL(code))) {
175             matrix[ROW(code)] |=  (1<<COL(code));
176         }
177     }
178
179     // PC-9801V keyboard requires RDY pulse.
180     // This is not optimal place though, it works.
181     PC98_RDY_PORT |=  (1<<PC98_RDY_BIT);    // RDY: high
182     _delay_us(20);
183     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);    // RDY: low
184     return code;
185 }
186
187 inline
188 uint8_t matrix_get_row(uint8_t row)
189 {
190     return matrix[row];
191 }
192
193 void led_set(uint8_t usb_led)
194 {
195     // https://archive.org/stream/PC9800TechnicalDataBookHARDWARE1993/PC-9800TechnicalDataBook_HARDWARE1993#page/n161
196     // http://www.webtech.co.jp/company/doc/undocumented_mem/io_kb.txt
197     pc98_led = 0x70;
198     if (usb_led & (1<<USB_LED_NUM_LOCK))    pc98_led |= (1<<0);
199     if (usb_led & (1<<USB_LED_CAPS_LOCK))   pc98_led |= (1<<2);
200     dprintf("usb_led: %02X\n", usb_led);
201     dprintf("pc98_led: %02X\n", pc98_led);
202 }