]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - converter/pc98_usb/matrix.c
lufa: Fix comment on INTERRUPT_CONTROL_ENDPOINT
[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     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
54     _delay_ms(1);
55     serial_send(data);
56     _delay_ms(1);
57     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
58 }
59
60 static int16_t pc98_wait_response(void)
61 {
62     int16_t code = -1;
63     uint8_t timeout = 255;
64     while (timeout-- && (code = serial_recv2()) == -1) _delay_ms(1);
65     return code;
66 }
67
68 static void pc98_inhibit_repeat(void)
69 {
70     uint16_t code;
71 RETRY:
72     pc98_send(0x9C);
73     code = pc98_wait_response();
74     if (code != -1) dprintf("send 9C: %02X\n", code);
75     if (code != 0xFA) return;
76
77     pc98_send(0x70);
78     code = pc98_wait_response();
79     if (code != -1) dprintf("send 70: %02X\n", code);
80     if (code != 0xFA) goto RETRY;
81 }
82
83 static uint8_t pc98_led = 0;
84 static void pc98_led_set(void)
85 {
86     uint16_t code;
87 RETRY:
88     pc98_send(0x9D);
89     code = pc98_wait_response();
90     if (code != -1) dprintf("send 9D: %02X\n", code);
91     if (code != 0xFA) return;
92
93     pc98_send(pc98_led);
94     code = pc98_wait_response();
95     if (code != -1) dprintf("send %02X: %02X\n", pc98_led, code);
96     if (code != 0xFA) goto RETRY;
97 }
98
99 void matrix_init(void)
100 {
101     PC98_RST_DDR |= (1<<PC98_RST_BIT);
102     PC98_RDY_DDR |= (1<<PC98_RDY_BIT);
103     PC98_RTY_DDR |= (1<<PC98_RTY_BIT);
104     PC98_RST_PORT |= (1<<PC98_RST_BIT);
105     PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
106     PC98_RTY_PORT |= (1<<PC98_RTY_BIT);
107
108
109     serial_init();
110
111     // PC98 reset
112     // https://archive.org/stream/PC9800TechnicalDataBookHARDWARE1993/PC-9800TechnicalDataBook_HARDWARE1993#page/n359
113     PC98_RDY_PORT |=  (1<<PC98_RDY_BIT);    // RDY: high
114     PC98_RST_PORT &= ~(1<<PC98_RST_BIT);    // RST: low
115     _delay_us(15);                          // > 13us
116     PC98_RST_PORT |= (1<<PC98_RST_BIT);     // RST: high
117
118     _delay_ms(50);
119     pc98_inhibit_repeat();
120
121     // initialize matrix state: all keys off
122     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
123
124     // ready to receive from keyboard
125     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);    // RDY: low
126
127     return;
128 }
129
130 uint8_t matrix_scan(void)
131 {
132     uint16_t code;
133     code = serial_recv2();
134     if (code == -1) {
135 #ifdef PC98_LED_CONTROL
136         // Before sending command  we have to make sure that there is no unprocessed key in queue
137         // otherwise keys will be missed during sending command
138         if (pc98_led) {
139             pc98_led_set();
140             pc98_led = 0;
141         }
142 #endif
143         return 0;
144     }
145
146     dprintf("%02X ", code);
147
148     if (code&0x80) {
149         // break code
150         if (matrix_is_on(ROW(code), COL(code))) {
151             matrix[ROW(code)] &= ~(1<<COL(code));
152         }
153     } else {
154         // make code
155         if (!matrix_is_on(ROW(code), COL(code))) {
156             matrix[ROW(code)] |=  (1<<COL(code));
157         }
158     }
159
160     // PC-9801V keyboard requires RDY pulse.
161     // This is not optimal place though, it works.
162     PC98_RDY_PORT |=  (1<<PC98_RDY_BIT);    // RDY: high
163     _delay_us(20);
164     PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);    // RDY: low
165     return code;
166 }
167
168 inline
169 uint8_t matrix_get_row(uint8_t row)
170 {
171     return matrix[row];
172 }
173
174 void led_set(uint8_t usb_led)
175 {
176     // https://archive.org/stream/PC9800TechnicalDataBookHARDWARE1993/PC-9800TechnicalDataBook_HARDWARE1993#page/n161
177     // http://www.webtech.co.jp/company/doc/undocumented_mem/io_kb.txt
178     pc98_led = 0x70;
179     if (usb_led & (1<<USB_LED_NUM_LOCK))    pc98_led |= (1<<0);
180     if (usb_led & (1<<USB_LED_CAPS_LOCK))   pc98_led |= (1<<2);
181     dprintf("usb_led: %02X\n", usb_led);
182     dprintf("pc98_led: %02X\n", pc98_led);
183 }