aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--converter/ps2_usb/Makefile111
-rw-r--r--converter/ps2_usb/Makefile.pjrc75
-rw-r--r--converter/ps2_usb/config.h87
-rw-r--r--converter/ps2_usb/keymap.c379
-rw-r--r--converter/ps2_usb/keymap_common.c30
-rw-r--r--converter/ps2_usb/keymap_common.h174
-rw-r--r--converter/ps2_usb/keymap_plain.c50
-rw-r--r--converter/ps2_usb/matrix.c49
-rw-r--r--keyboard/onekey/Makefile (renamed from keyboard/onekey/Makefile.lufa)4
-rw-r--r--keyboard/onekey/README.md2
-rw-r--r--keyboard/onekey/config.h4
-rw-r--r--keyboard/onekey/matrix.c18
-rw-r--r--protocol.mk4
-rw-r--r--protocol/lufa/lufa.c4
-rw-r--r--protocol/pjrc/usb.c3
-rw-r--r--protocol/ps2.h146
-rw-r--r--protocol/ps2_busywait.c185
-rw-r--r--protocol/ps2_interrupt.c (renamed from protocol/ps2.c)334
-rw-r--r--protocol/ps2_usart.c174
19 files changed, 932 insertions, 901 deletions
diff --git a/converter/ps2_usb/Makefile b/converter/ps2_usb/Makefile
index 0c6442374..04bf28a00 100644
--- a/converter/ps2_usb/Makefile
+++ b/converter/ps2_usb/Makefile
@@ -1,5 +1,5 @@
1# Target file name (without extension). 1# Target file name (without extension).
2TARGET = ps2_usb 2TARGET = ps2_usb_lufa
3 3
4# Directory common source filess exist 4# Directory common source filess exist
5TOP_DIR = ../.. 5TOP_DIR = ../..
@@ -7,69 +7,96 @@ TOP_DIR = ../..
7# Directory keyboard dependent files exist 7# Directory keyboard dependent files exist
8TARGET_DIR = . 8TARGET_DIR = .
9 9
10# project specific files
11SRC = keymap_common.c \
12 matrix.c \
13 led.c
14
15ifdef KEYMAP
16 SRC := keymap_$(KEYMAP).c $(SRC)
17else
18 SRC := keymap_plain.c $(SRC)
19endif
20
21CONFIG_H = config.h
10 22
11# MCU name, you MUST set this to match the board you are using
12# type "make clean" after changing this, so all files will be rebuilt
13#MCU = at90usb162 # Teensy 1.0
14MCU = atmega32u4 # Teensy 2.0
15#MCU = at90usb646 # Teensy++ 1.0
16#MCU = at90usb1286 # Teensy++ 2.0
17 23
24# MCU name
25#MCU = at90usb1287
26MCU = atmega32u4
18 27
19# Processor frequency. 28# Processor frequency.
20# Normally the first thing your program should do is set the clock prescaler, 29# This will define a symbol, F_CPU, in all source code files equal to the
21# so your program will run at the correct speed. You should also set this 30# processor frequency in Hz. You can then use this symbol in your source code to
22# variable to same clock speed. The _delay_ms() macro uses this, and many 31# calculate timings. Do NOT tack on a 'UL' at the end, this will be done
23# examples use this variable to calculate timings. Do not add a "UL" here. 32# automatically to create a 32-bit value in your source code.
33#
34# This will be an integer division of F_USB below, as it is sourced by
35# F_USB after it has run through any CPU prescalers. Note that this value
36# does not *change* the processor frequency - it should merely be updated to
37# reflect the processor speed set externally so that the code can use accurate
38# software delays.
24F_CPU = 16000000 39F_CPU = 16000000
25 40
26 41
27# Build Options
28# *Comment out* to disable the options.
29# 42#
30MOUSEKEY_ENABLE = yes # Mouse keys 43# LUFA specific
31EXTRAKEY_ENABLE = yes # Audio control and System control 44#
32NKRO_ENABLE = yes # USB Nkey Rollover 45# Target architecture (see library "Board Types" documentation).
46ARCH = AVR8
47
48# Input clock frequency.
49# This will define a symbol, F_USB, in all source code files equal to the
50# input clock frequency (before any prescaling is performed) in Hz. This value may
51# differ from F_CPU if prescaling is used on the latter, and is required as the
52# raw input clock is fed directly to the PLL sections of the AVR for high speed
53# clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL'
54# at the end, this will be done automatically to create a 32-bit value in your
55# source code.
56#
57# If no clock division is performed on the input clock inside the AVR (via the
58# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
59F_USB = $(F_CPU)
33 60
34PS2_USE_USART = yes # uses hardware USART engine for PS/2 signal receive(recomened) 61# Interrupt driven control endpoint task(+60)
35#PS2_USE_INT = yes # uses external interrupt for falling edge of PS/2 clock pin 62OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT
36#PS2_USE_BUSYWAIT = yes # uses primitive reference code
37 63
38 64
39# keyboard dependent files 65# Boot Section Size in *bytes*
40SRC = keymap.c \ 66# Teensy halfKay 512
41 matrix.c \ 67# Teensy++ halfKay 1024
42 led.c 68# Atmel DFU loader 4096
43 69# LUFA bootloader 4096
70# USBaspLoader 2048
71OPT_DEFS += -DBOOTLOADER_SIZE=4096
44 72
45ifdef PS2_USE_USART
46 SRC += protocol/ps2_usart.c
47 OPT_DEFS += -DPS2_USE_USART
48endif
49ifdef PS2_USE_INT
50 SRC += protocol/ps2.c
51 OPT_DEFS += -DPS2_USE_INT
52endif
53ifdef PS2_USE_BUSYWAIT
54 SRC += protocol/ps2.c
55 OPT_DEFS += -DPS2_USE_BUSYWAIT
56endif
57 73
74# Build Options
75# comment out to disable the options.
76#
77#BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000)
78MOUSEKEY_ENABLE = yes # Mouse keys(+4700)
79EXTRAKEY_ENABLE = yes # Audio control and System control(+450)
80CONSOLE_ENABLE = yes # Console for debug(+400)
81COMMAND_ENABLE = yes # Commands for debug and configuration
82NKRO_ENABLE = yes # USB Nkey Rollover - not yet supported in LUFA
58 83
59#CONFIG_H = config_pjrc_usart.h
60CONFIG_H = config.h
61 84
85# PS/2 Options
86#
87#PS2_USE_USART = yes # uses hardware USART engine for PS/2 signal receive(recomened)
88#PS2_USE_INT = yes # uses external interrupt for falling edge of PS/2 clock pin
89PS2_USE_BUSYWAIT = yes # uses primitive reference code
62 90
63#---------------- Programming Options --------------------------
64PROGRAM_CMD = teensy_loader_cli -mmcu=$(MCU) -w -v $(TARGET).hex
65 91
92# Optimize size but this may cause error "relocation truncated to fit"
93#EXTRALDFLAGS = -Wl,--relax
66 94
67# Search Path 95# Search Path
68VPATH += $(TARGET_DIR) 96VPATH += $(TARGET_DIR)
69VPATH += $(TOP_DIR) 97VPATH += $(TOP_DIR)
70 98
71
72include $(TOP_DIR)/protocol/pjrc.mk
73include $(TOP_DIR)/protocol.mk 99include $(TOP_DIR)/protocol.mk
100include $(TOP_DIR)/protocol/lufa.mk
74include $(TOP_DIR)/common.mk 101include $(TOP_DIR)/common.mk
75include $(TOP_DIR)/rules.mk 102include $(TOP_DIR)/rules.mk
diff --git a/converter/ps2_usb/Makefile.pjrc b/converter/ps2_usb/Makefile.pjrc
new file mode 100644
index 000000000..0e175f8b4
--- /dev/null
+++ b/converter/ps2_usb/Makefile.pjrc
@@ -0,0 +1,75 @@
1# Target file name (without extension).
2TARGET = ps2_usb_pjrc
3
4# Directory common source filess exist
5TOP_DIR = ../..
6
7# Directory keyboard dependent files exist
8TARGET_DIR = .
9
10# keyboard dependent files
11SRC = keymap_common.c \
12 matrix.c \
13 led.c
14
15ifdef KEYMAP
16 SRC := keymap_$(KEYMAP).c $(SRC)
17else
18 SRC := keymap_plain.c $(SRC)
19endif
20
21CONFIG_H = config.h
22
23
24# MCU name, you MUST set this to match the board you are using
25# type "make clean" after changing this, so all files will be rebuilt
26#MCU = at90usb162 # Teensy 1.0
27MCU = atmega32u4 # Teensy 2.0
28#MCU = at90usb646 # Teensy++ 1.0
29#MCU = at90usb1286 # Teensy++ 2.0
30
31
32# Processor frequency.
33# Normally the first thing your program should do is set the clock prescaler,
34# so your program will run at the correct speed. You should also set this
35# variable to same clock speed. The _delay_ms() macro uses this, and many
36# examples use this variable to calculate timings. Do not add a "UL" here.
37F_CPU = 16000000
38
39
40# Boot Section Size in *bytes*
41# Teensy halfKay 512
42# Teensy++ halfKay 1024
43# Atmel DFU loader 4096
44# LUFA bootloader 4096
45# USBaspLoader 2048
46OPT_DEFS += -DBOOTLOADER_SIZE=4096
47
48
49# Build Options
50# *Comment out* to disable the options.
51#
52#BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000)
53MOUSEKEY_ENABLE = yes # Mouse keys
54EXTRAKEY_ENABLE = yes # Audio control and System control
55CONSOLE_ENABLE = yes # Console for debug(+400)
56COMMAND_ENABLE = yes # Commands for debug and configuration
57NKRO_ENABLE = yes # USB Nkey Rollover
58
59
60# PS/2 Options
61#
62#PS2_USE_USART = yes # uses hardware USART engine for PS/2 signal receive(recomened)
63#PS2_USE_INT = yes # uses external interrupt for falling edge of PS/2 clock pin
64PS2_USE_BUSYWAIT = yes # uses primitive reference code
65
66
67# Search Path
68VPATH += $(TARGET_DIR)
69VPATH += $(TOP_DIR)
70
71
72include $(TOP_DIR)/protocol.mk
73include $(TOP_DIR)/protocol/pjrc.mk
74include $(TOP_DIR)/common.mk
75include $(TOP_DIR)/rules.mk
diff --git a/converter/ps2_usb/config.h b/converter/ps2_usb/config.h
index 51cd271d7..c9bab1b07 100644
--- a/converter/ps2_usb/config.h
+++ b/converter/ps2_usb/config.h
@@ -22,6 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
22 22
23#define VENDOR_ID 0xFEED 23#define VENDOR_ID 0xFEED
24#define PRODUCT_ID 0x6512 24#define PRODUCT_ID 0x6512
25#define DEVICE_VER 0x0001
25#define MANUFACTURER t.m.k. 26#define MANUFACTURER t.m.k.
26#define PRODUCT PS/2 keyboard converter 27#define PRODUCT PS/2 keyboard converter
27#define DESCRIPTION convert PS/2 keyboard to USB 28#define DESCRIPTION convert PS/2 keyboard to USB
@@ -39,10 +40,52 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
39) 40)
40 41
41 42
42/* legacy keymap support */ 43//#define NO_SUSPEND_POWER_DOWN
43#define USE_LEGACY_KEYMAP
44 44
45 45
46/*
47 * PS/2 Busywait
48 */
49#ifdef PS2_USE_BUSYWAIT
50#define PS2_CLOCK_PORT PORTD
51#define PS2_CLOCK_PIN PIND
52#define PS2_CLOCK_DDR DDRD
53#define PS2_CLOCK_BIT 5
54#define PS2_DATA_PORT PORTD
55#define PS2_DATA_PIN PIND
56#define PS2_DATA_DDR DDRD
57#define PS2_DATA_BIT 2
58#endif
59
60/*
61 * PS/2 Pin interrupt
62 */
63#ifdef PS2_USE_INT
64/* uses INT1 for clock line(ATMega32U4) */
65#define PS2_CLOCK_PORT PORTD
66#define PS2_CLOCK_PIN PIND
67#define PS2_CLOCK_DDR DDRD
68#define PS2_CLOCK_BIT 1
69#define PS2_DATA_PORT PORTD
70#define PS2_DATA_PIN PIND
71#define PS2_DATA_DDR DDRD
72#define PS2_DATA_BIT 2
73#define PS2_INT_INIT() do { \
74 EICRA |= ((1<<ISC11) | \
75 (0<<ISC10)); \
76} while (0)
77#define PS2_INT_ON() do { \
78 EIMSK |= (1<<INT1); \
79} while (0)
80#define PS2_INT_OFF() do { \
81 EIMSK &= ~(1<<INT1); \
82} while (0)
83#define PS2_INT_VECT INT1_vect
84#endif
85
86/*
87 * PS/2 USART
88 */
46#ifdef PS2_USE_USART 89#ifdef PS2_USE_USART
47#if defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) 90#if defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)
48/* XCK for clock line and RXD for data line */ 91/* XCK for clock line and RXD for data line */
@@ -54,7 +97,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
54#define PS2_DATA_PIN PIND 97#define PS2_DATA_PIN PIND
55#define PS2_DATA_DDR DDRD 98#define PS2_DATA_DDR DDRD
56#define PS2_DATA_BIT 2 99#define PS2_DATA_BIT 2
57
58/* synchronous, odd parity, 1-bit stop, 8-bit data, sample at falling edge */ 100/* synchronous, odd parity, 1-bit stop, 8-bit data, sample at falling edge */
59/* set DDR of CLOCK as input to be slave */ 101/* set DDR of CLOCK as input to be slave */
60#define PS2_USART_INIT() do { \ 102#define PS2_USART_INIT() do { \
@@ -85,7 +127,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
85#define PS2_USART_RX_DATA UDR1 127#define PS2_USART_RX_DATA UDR1
86#define PS2_USART_ERROR (UCSR1A & ((1<<FE1) | (1<<DOR1) | (1<<UPE1))) 128#define PS2_USART_ERROR (UCSR1A & ((1<<FE1) | (1<<DOR1) | (1<<UPE1)))
87#define PS2_USART_RX_VECT USART1_RX_vect 129#define PS2_USART_RX_VECT USART1_RX_vect
88
89#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) 130#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
90/* XCK for clock line and RXD for data line */ 131/* XCK for clock line and RXD for data line */
91#define PS2_CLOCK_PORT PORTD 132#define PS2_CLOCK_PORT PORTD
@@ -96,7 +137,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
96#define PS2_DATA_PIN PIND 137#define PS2_DATA_PIN PIND
97#define PS2_DATA_DDR DDRD 138#define PS2_DATA_DDR DDRD
98#define PS2_DATA_BIT 0 139#define PS2_DATA_BIT 0
99
100/* synchronous, odd parity, 1-bit stop, 8-bit data, sample at falling edge */ 140/* synchronous, odd parity, 1-bit stop, 8-bit data, sample at falling edge */
101/* set DDR of CLOCK as input to be slave */ 141/* set DDR of CLOCK as input to be slave */
102#define PS2_USART_INIT() do { \ 142#define PS2_USART_INIT() do { \
@@ -130,41 +170,4 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
130#endif 170#endif
131#endif 171#endif
132 172
133
134#ifdef PS2_USE_INT
135/* uses INT1 for clock line(ATMega32U4) */
136#define PS2_CLOCK_PORT PORTD
137#define PS2_CLOCK_PIN PIND
138#define PS2_CLOCK_DDR DDRD
139#define PS2_CLOCK_BIT 1
140#define PS2_DATA_PORT PORTD
141#define PS2_DATA_PIN PIND
142#define PS2_DATA_DDR DDRD
143#define PS2_DATA_BIT 2
144
145#define PS2_INT_INIT() do { \
146 EICRA |= ((1<<ISC11) | \
147 (0<<ISC10)); \
148} while (0)
149#define PS2_INT_ON() do { \
150 EIMSK |= (1<<INT1); \
151} while (0)
152#define PS2_INT_OFF() do { \
153 EIMSK &= ~(1<<INT1); \
154} while (0)
155#define PS2_INT_VECT INT1_vect
156#endif
157
158
159#ifdef PS2_USE_BUSYWAIT
160#define PS2_CLOCK_PORT PORTF
161#define PS2_CLOCK_PIN PINF
162#define PS2_CLOCK_DDR DDRF
163#define PS2_CLOCK_BIT 0
164#define PS2_DATA_PORT PORTF
165#define PS2_DATA_PIN PINF
166#define PS2_DATA_DDR DDRF
167#define PS2_DATA_BIT 1
168#endif
169
170#endif 173#endif
diff --git a/converter/ps2_usb/keymap.c b/converter/ps2_usb/keymap.c
deleted file mode 100644
index c97783fa5..000000000
--- a/converter/ps2_usb/keymap.c
+++ /dev/null
@@ -1,379 +0,0 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18/*
19 * Keymap for PS/2 keyboard
20 */
21#include <stdint.h>
22#include <stdbool.h>
23#include <avr/pgmspace.h>
24#include "keycode.h"
25#include "print.h"
26#include "debug.h"
27#include "util.h"
28#include "keymap.h"
29
30
31
32
33// Following macros help you to define a keymap with the form of actual keyboard layout.
34
35/* US layout plus all other various keys */
36#define KEYMAP_ALL( \
37 K76,K05,K06,K04,K0C,K03,K0B,K83,K0A,K01,K09,K78,K07, KFC,K7E,KFE, \
38 K0E,K16,K1E,K26,K25,K2E,K36,K3D,K3E,K46,K45,K4E,K55,K66, KF0,KEC,KFD, K77,KCA,K7C,K7B, \
39 K0D,K15,K1D,K24,K2D,K2C,K35,K3C,K43,K44,K4D,K54,K5B,K5D, KF1,KE9,KFA, K6C,K75,K7D, \
40 K58,K1C,K1B,K23,K2B,K34,K33,K3B,K42,K4B,K4C,K52, K5A, K6B,K73,K74,K79, \
41 K12,K1A,K22,K21,K2A,K32,K31,K3A,K41,K49,K4A, K59, KF5, K69,K72,K7A, \
42 K14,K9F,K11, K29, K91,KA7,KAF,K94, KEB,KF2,KF4, K70, K71,KDA, \
43 \
44 K61, /* for European ISO */ \
45 K51, K13, K6A, K64, K67, /* for Japanese JIS */ \
46 K08, K10, K18, K20, K28, K30, K38, K40, K48, K50, K57, K5F, /* F13-24 */ \
47 KB7, KBF, KDE, /* System Power, Sleep, Wake */ \
48 KA3, KB2, KA1, /* Mute, Volume Up, Volume Down */ \
49 KCD, K95, KBB, KB4, KD0, /* Next, Previous, Stop, Pause, Media Select */ \
50 KC8, KAB, KC0, /* Mail, Calculator, My Computer */ \
51 K90, KBA, KB8, KB0, /* WWW Search, Home, Back, Forward */ \
52 KA8, KA0, K98 /* WWW Stop, Refresh, Favorites */ \
53) { \
54 { KC_NO, KC_##K01, KC_NO, KC_##K03, KC_##K04, KC_##K05, KC_##K06, KC_##K07 }, \
55 { KC_##K08, KC_##K09, KC_##K0A, KC_##K0B, KC_##K0C, KC_##K0D, KC_##K0E, KC_NO }, \
56 { KC_##K10, KC_##K11, KC_##K12, KC_##K13, KC_##K14, KC_##K15, KC_##K16, KC_NO }, \
57 { KC_##K18, KC_NO, KC_##K1A, KC_##K1B, KC_##K1C, KC_##K1D, KC_##K1E, KC_NO }, \
58 { KC_##K20, KC_##K21, KC_##K22, KC_##K23, KC_##K24, KC_##K25, KC_##K26, KC_NO }, \
59 { KC_##K28, KC_##K29, KC_##K2A, KC_##K2B, KC_##K2C, KC_##K2D, KC_##K2E, KC_NO }, \
60 { KC_##K30, KC_##K31, KC_##K32, KC_##K33, KC_##K34, KC_##K35, KC_##K36, KC_NO }, \
61 { KC_##K38, KC_NO, KC_##K3A, KC_##K3B, KC_##K3C, KC_##K3D, KC_##K3E, KC_NO }, \
62 { KC_##K40, KC_##K41, KC_##K42, KC_##K43, KC_##K44, KC_##K45, KC_##K46, KC_NO }, \
63 { KC_##K48, KC_##K49, KC_##K4A, KC_##K4B, KC_##K4C, KC_##K4D, KC_##K4E, KC_NO }, \
64 { KC_##K50, KC_##K51, KC_##K52, KC_NO, KC_##K54, KC_##K55, KC_NO, KC_##K57 }, \
65 { KC_##K58, KC_##K59, KC_##K5A, KC_##K5B, KC_NO, KC_##K5D, KC_NO, KC_##K5F }, \
66 { KC_NO, KC_##K61, KC_NO, KC_NO, KC_##K64, KC_NO, KC_##K66, KC_##K67 }, \
67 { KC_NO, KC_##K69, KC_##K6A, KC_##K6B, KC_##K6C, KC_NO, KC_NO, KC_NO }, \
68 { KC_##K70, KC_##K71, KC_##K72, KC_##K73, KC_##K74, KC_##K75, KC_##K76, KC_##K77 }, \
69 { KC_##K78, KC_##K79, KC_##K7A, KC_##K7B, KC_##K7C, KC_##K7D, KC_##K7E, KC_NO }, \
70 { KC_NO, KC_NO, KC_NO, KC_##K83, KC_NO, KC_NO, KC_NO, KC_NO }, \
71 { KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO }, \
72 { KC_##K90, KC_##K91, KC_NO, KC_NO, KC_##K94, KC_##K95, KC_NO, KC_NO }, \
73 { KC_##K98, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_##K9F }, \
74 { KC_##KA0, KC_##KA1, KC_NO, KC_##KA3, KC_NO, KC_NO, KC_NO, KC_##KA7 }, \
75 { KC_##KA8, KC_NO, KC_NO, KC_##KAB, KC_NO, KC_NO, KC_NO, KC_##KAF }, \
76 { KC_##KB0, KC_NO, KC_##KB2, KC_NO, KC_##KB4, KC_NO, KC_NO, KC_##KB7 }, \
77 { KC_##KB8, KC_NO, KC_##KBA, KC_##KBB, KC_NO, KC_NO, KC_NO, KC_##KBF }, \
78 { KC_##KC0, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO }, \
79 { KC_##KC8, KC_NO, KC_##KCA, KC_NO, KC_NO, KC_##KCD, KC_NO, KC_NO }, \
80 { KC_##KD0, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO }, \
81 { KC_NO, KC_NO, KC_##KDA, KC_NO, KC_NO, KC_NO, KC_##KDE, KC_NO }, \
82 { KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO }, \
83 { KC_NO, KC_##KE9, KC_NO, KC_##KEB, KC_##KEC, KC_NO, KC_NO, KC_NO }, \
84 { KC_##KF0, KC_##KF1, KC_##KF2, KC_NO, KC_##KF4, KC_##KF5, KC_NO, KC_NO }, \
85 { KC_NO, KC_NO, KC_##KFA, KC_NO, KC_##KFC, KC_##KFD, KC_##KFE, KC_NO }, \
86}
87
88/* US layout */
89#define KEYMAP( \
90 K76,K05,K06,K04,K0C,K03,K0B,K83,K0A,K01,K09,K78,K07, KFC,K7E,KFE, \
91 K0E,K16,K1E,K26,K25,K2E,K36,K3D,K3E,K46,K45,K4E,K55,K66, KF0,KEC,KFD, K77,KCA,K7C,K7B, \
92 K0D,K15,K1D,K24,K2D,K2C,K35,K3C,K43,K44,K4D,K54,K5B,K5D, KF1,KE9,KFA, K6C,K75,K7D, \
93 K58,K1C,K1B,K23,K2B,K34,K33,K3B,K42,K4B,K4C,K52, K5A, K6B,K73,K74,K79, \
94 K12,K1A,K22,K21,K2A,K32,K31,K3A,K41,K49,K4A, K59, KF5, K69,K72,K7A, \
95 K14,K9F,K11, K29, K91,KA7,KAF,K94, KEB,KF2,KF4, K70, K71,KDA \
96) \
97KEYMAP_ALL( \
98 K76,K05,K06,K04,K0C,K03,K0B,K83,K0A,K01,K09,K78,K07, KFC,K7E,KFE, \
99 K0E,K16,K1E,K26,K25,K2E,K36,K3D,K3E,K46,K45,K4E,K55,K66, KF0,KEC,KFD, K77,KCA,K7C,K7B, \
100 K0D,K15,K1D,K24,K2D,K2C,K35,K3C,K43,K44,K4D,K54,K5B,K5D, KF1,KE9,KFA, K6C,K75,K7D, \
101 K58,K1C,K1B,K23,K2B,K34,K33,K3B,K42,K4B,K4C,K52, K5A, K6B,K73,K74,K79, \
102 K12,K1A,K22,K21,K2A,K32,K31,K3A,K41,K49,K4A, K59, KF5, K69,K72,K7A, \
103 K14,K9F,K11, K29, K91,KA7,KAF,K94, KEB,KF2,KF4, K70, K71,KDA, \
104 \
105 NUBS, \
106 RO, KANA, JYEN, HENK, MHEN, \
107 F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23, F24, \
108 SYSTEM_POWER, SYSTEM_SLEEP, SYSTEM_WAKE, \
109 AUDIO_MUTE, AUDIO_VOL_UP, AUDIO_VOL_DOWN, \
110 MEDIA_NEXT_TRACK, MEDIA_PREV_TRACK, MEDIA_STOP, MEDIA_PLAY_PAUSE, MEDIA_SELECT, \
111 MAIL, CALCULATOR, MY_COMPUTER, \
112 WWW_SEARCH, WWW_HOME, WWW_BACK, WWW_FORWARD, \
113 WWW_STOP, WWW_REFRESH, WWW_FAVORITES \
114)
115
116/* ISO layout */
117#define KEYMAP_ISO( \
118 K76,K05,K06,K04,K0C,K03,K0B,K83,K0A,K01,K09,K78,K07, KFC,K7E,KFE, \
119 K0E,K16,K1E,K26,K25,K2E,K36,K3D,K3E,K46,K45,K4E,K55,K66, KF0,KEC,KFD, K77,KCA,K7C,K7B, \
120 K0D,K15,K1D,K24,K2D,K2C,K35,K3C,K43,K44,K4D,K54,K5B, KF1,KE9,KFA, K6C,K75,K7D, \
121 K58,K1C,K1B,K23,K2B,K34,K33,K3B,K42,K4B,K4C,K52,K5D,K5A, K6B,K73,K74,K79, \
122 K12,K61,K1A,K22,K21,K2A,K32,K31,K3A,K41,K49,K4A, K59, KF5, K69,K72,K7A, \
123 K14,K9F,K11, K29, K91,KA7,KAF,K94, KEB,KF2,KF4, K70, K71,KDA \
124) \
125KEYMAP_ALL( \
126 K76,K05,K06,K04,K0C,K03,K0B,K83,K0A,K01,K09,K78,K07, KFC,K7E,KFE, \
127 K0E,K16,K1E,K26,K25,K2E,K36,K3D,K3E,K46,K45,K4E,K55,K66, KF0,KEC,KFD, K77,KCA,K7C,K7B, \
128 K0D,K15,K1D,K24,K2D,K2C,K35,K3C,K43,K44,K4D,K54,K5B,K5D, KF1,KE9,KFA, K6C,K75,K7D, \
129 K58,K1C,K1B,K23,K2B,K34,K33,K3B,K42,K4B,K4C,K52, K5A, K6B,K73,K74,K79, \
130 K12,K1A,K22,K21,K2A,K32,K31,K3A,K41,K49,K4A, K59, KF5, K69,K72,K7A, \
131 K14,K9F,K11, K29, K91,KA7,KAF,K94, KEB,KF2,KF4, K70, K71,KDA, \
132 \
133 K61, \
134 RO, KANA, JYEN, HENK, MHEN, \
135 F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23, F24, \
136 SYSTEM_POWER, SYSTEM_SLEEP, SYSTEM_WAKE, \
137 AUDIO_MUTE, AUDIO_VOL_UP, AUDIO_VOL_DOWN, \
138 MEDIA_NEXT_TRACK, MEDIA_PREV_TRACK, MEDIA_STOP, MEDIA_PLAY_PAUSE, MEDIA_SELECT, \
139 MAIL, CALCULATOR, MY_COMPUTER, \
140 WWW_SEARCH, WWW_HOME, WWW_BACK, WWW_FORWARD, \
141 WWW_STOP, WWW_REFRESH, WWW_FAVORITES \
142)
143
144/* JIS layout */
145#define KEYMAP_JIS( \
146 K76,K05,K06,K04,K0C,K03,K0B,K83,K0A,K01,K09,K78,K07, KFC,K7E,KFE, \
147 K0E,K16,K1E,K26,K25,K2E,K36,K3D,K3E,K46,K45,K4E,K55,K6A,K66, KF0,KEC,KFD, K77,KCA,K7C,K7B, \
148 K0D,K15,K1D,K24,K2D,K2C,K35,K3C,K43,K44,K4D,K54,K5B, KF1,KE9,KFA, K6C,K75,K7D, \
149 K58,K1C,K1B,K23,K2B,K34,K33,K3B,K42,K4B,K4C,K52,K5D, K5A, K6B,K73,K74,K79, \
150 K12,K1A,K22,K21,K2A,K32,K31,K3A,K41,K49,K4A,K51, K59, KF5, K69,K72,K7A, \
151 K14,K9F,K11, K67,K29,K64,K13, K91,KA7,KAF,K94, KEB,KF2,KF4, K70, K71,KDA \
152) \
153KEYMAP_ALL( \
154 K76,K05,K06,K04,K0C,K03,K0B,K83,K0A,K01,K09,K78,K07, KFC,K7E,KFE, \
155 K0E,K16,K1E,K26,K25,K2E,K36,K3D,K3E,K46,K45,K4E,K55,K66, KF0,KEC,KFD, K77,KCA,K7C,K7B, \
156 K0D,K15,K1D,K24,K2D,K2C,K35,K3C,K43,K44,K4D,K54,K5B,K5D, KF1,KE9,KFA, K6C,K75,K7D, \
157 K58,K1C,K1B,K23,K2B,K34,K33,K3B,K42,K4B,K4C,K52, K5A, K6B,K73,K74,K79, \
158 K12,K1A,K22,K21,K2A,K32,K31,K3A,K41,K49,K4A, K59, KF5, K69,K72,K7A, \
159 K14,K9F,K11, K29, K91,KA7,KAF,K94, KEB,KF2,KF4, K70, K71,KDA, \
160 \
161 NUBS, \
162 K51, K13, K6A, K64, K67, \
163 F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23, F24, \
164 SYSTEM_POWER, SYSTEM_SLEEP, SYSTEM_WAKE, \
165 AUDIO_MUTE, AUDIO_VOL_UP, AUDIO_VOL_DOWN, \
166 MEDIA_NEXT_TRACK, MEDIA_PREV_TRACK, MEDIA_STOP, MEDIA_PLAY_PAUSE, MEDIA_SELECT, \
167 MAIL, CALCULATOR, MY_COMPUTER, \
168 WWW_SEARCH, WWW_HOME, WWW_BACK, WWW_FORWARD, \
169 WWW_STOP, WWW_REFRESH, WWW_FAVORITES \
170)
171
172
173// Assign Fn key(0-7) to a layer to which switch with the Fn key pressed.
174static const uint8_t PROGMEM fn_layer[] = {
175 5, // Fn0
176 6, // Fn1
177 0, // Fn2
178 0, // Fn3
179 0, // Fn4
180 0, // Fn5
181 0, // Fn6
182 0 // Fn7
183};
184
185// Assign Fn key(0-7) to a keycode sent when release Fn key without use of the layer.
186// See layer.c for details.
187static const uint8_t PROGMEM fn_keycode[] = {
188 KC_SCLN, // Fn0
189 KC_SLSH, // Fn1
190 KC_NO, // Fn2
191 KC_NO, // Fn3
192 KC_NO, // Fn4
193 KC_NO, // Fn5
194 KC_NO, // Fn6
195 KC_NO // Fn7
196};
197
198
199// The keymap is a 32*8 byte array which convert a PS/2 scan code into a USB keycode.
200// See keycode.h for USB keycodes. You should omit a 'KC_' prefix of USB keycodes in keymap macro.
201// Use KEYMAP_ISO() or KEYMAP_JIS() instead of KEYMAP() if your keyboard is ISO or JIS.
202static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
203 /* 0: default
204 * ,---. ,---------------. ,---------------. ,---------------. ,-----------. ,-----------.
205 * |Esc| |F1 |F2 |F3 |F4 | |F5 |F6 |F7 |F8 | |F9 |F10|F11|F12| |PrS|ScL|Pau| |Pwr|Slp|Wak|
206 * `---' `---------------' `---------------' `---------------' `-----------' `-----------'
207 * ,-----------------------------------------------------------. ,-----------. ,---------------.
208 * | `| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Backspa| |Ins|Hom|PgU| |NmL| /| *| -|
209 * |-----------------------------------------------------------| |-----------| |---------------|
210 * |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]| \| |Del|End|PgD| | 7| 8| 9| |
211 * |-----------------------------------------------------------| `-----------' |-----------| +|
212 * |CapsLo| A| S| D| F| G| H| J| K| L| ;| '|Return | | 4| 5| 6| |
213 * |-----------------------------------------------------------| ,---. |---------------|
214 * |Shift | Z| X| C| V| B| N| M| ,| ,| /|Shift | |Up | | 1| 2| 3| |
215 * |-----------------------------------------------------------| ,-----------. |-----------|Ent|
216 * |Ctrl |Gui |Alt | Space |Alt |Gui |Menu|Ctrl| |Lef|Dow|Rig| | 0| .| |
217 * `-----------------------------------------------------------' `-----------' `---------------'
218 * ; = Fn0(to Layer 5)
219 * / = Fn1(to Layer 6)
220 */
221 KEYMAP(
222 ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PSCR,SLCK,BRK,
223 GRV, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS,EQL, BSPC, INS, HOME,PGUP, NLCK,PSLS,PAST,PMNS,
224 TAB, Q, W, E, R, T, Y, U, I, O, P, LBRC,RBRC,BSLS, DEL, END, PGDN, P7, P8, P9,
225 CAPS,A, S, D, F, G, H, J, K, L, FN0, QUOT, ENT, P4, P5, P6, PPLS,
226 LSFT,Z, X, C, V, B, N, M, COMM,DOT, FN1, RSFT, UP, P1, P2, P3,
227 LCTL,LGUI,LALT, SPC, RALT,RGUI,APP, RCTL, LEFT,DOWN,RGHT, P0, PDOT,PENT
228 ),
229
230 /* 1: plain Qwerty without layer switching
231 * ,-----------------------------------------------------------.
232 * | `| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Backspa|
233 * |-----------------------------------------------------------|
234 * |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]| \|
235 * |-----------------------------------------------------------|
236 * |CapsLo| A| S| D| F| G| H| J| K| L| ;| '|Return |
237 * |-----------------------------------------------------------|
238 * |Shift | Z| X| C| V| B| N| M| ,| ,| /|Shift |
239 * |-----------------------------------------------------------|
240 * |Ctrl |Gui |Alt | Space |Alt |Gui |Menu|Ctrl|
241 * `-----------------------------------------------------------'
242 */
243 KEYMAP(
244 ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PSCR,SLCK,BRK,
245 GRV, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS,EQL, BSPC, INS, HOME,PGUP, NLCK,PSLS,PAST,PMNS,
246 TAB, Q, W, E, R, T, Y, U, I, O, P, LBRC,RBRC,BSLS, DEL, END, PGDN, P7, P8, P9,
247 CAPS,A, S, D, F, G, H, J, K, L, SCLN,QUOT, ENT, P4, P5, P6, PPLS,
248 LSFT,Z, X, C, V, B, N, M, COMM,DOT, SLSH, RSFT, UP, P1, P2, P3,
249 LCTL,LGUI,LALT, SPC, RALT,RGUI,APP, RCTL, LEFT,DOWN,RGHT, P0, PDOT,PENT
250 ),
251
252 /* 2: Colemak http://colemak.com
253 * ,-----------------------------------------------------------.
254 * | `| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Backspa|
255 * |-----------------------------------------------------------|
256 * |Tab | Q| W| F| P| G| J| L| U| Y| ;| [| ]| \|
257 * |-----------------------------------------------------------|
258 * |BackSp| A| R| S| T| D| H| N| E| I| O| '|Return |
259 * |-----------------------------------------------------------|
260 * |Shift | Z| X| C| V| B| K| M| ,| ,| /|Shift |
261 * |-----------------------------------------------------------|
262 * |Ctrl |Gui |Alt | Space |Alt |Gui |Menu|Ctrl|
263 * `----------------------------------------------------------'
264 */
265 KEYMAP(
266 ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PSCR,SLCK,BRK,
267 GRV, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS,EQL, BSPC, INS, HOME,PGUP, NLCK,PSLS,PAST,PMNS,
268 TAB, Q, W, F, P, G, J, L, U, Y, SCLN,LBRC,RBRC,BSLS, DEL, END, PGDN, P7, P8, P9,
269 BSPC,A, R, S, T, D, H, N, E, I, O, QUOT, ENT, P4, P5, P6, PPLS,
270 LSFT,Z, X, C, V, B, K, M, COMM,DOT, SLSH, RSFT, UP, P1, P2, P3,
271 LCTL,LGUI,LALT, SPC, RALT,RGUI,APP, RCTL, LEFT,DOWN,RGHT, P0, PDOT,PENT
272 ),
273
274 /* 3: Dvorak http://en.wikipedia.org/wiki/Dvorak_Simplified_Keyboard
275 * ,-----------------------------------------------------------.
276 * | `| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| [| ]|Backspa|
277 * |-----------------------------------------------------------|
278 * |Tab | '| ,| .| P| Y| F| G| C| R| L| /| =| \|
279 * |-----------------------------------------------------------|
280 * |BackSp| A| O| E| U| I| D| H| T| N| S| -|Return |
281 * |-----------------------------------------------------------|
282 * |Shift | ;| Q| J| K| X| B| M| Wl V| Z|Shift |
283 * |-----------------------------------------------------------|
284 * |Ctrl |Gui |Alt | Space |Alt |Gui |Menu|Ctrl|
285 * `-----------------------------------------------------------'
286 */
287 KEYMAP(
288 ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PSCR,SLCK,BRK,
289 GRV, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, LBRC,RBRC,BSPC, INS, HOME,PGUP, NLCK,PSLS,PAST,PMNS,
290 TAB, QUOT,COMM,DOT, P, Y, F, G, C, R, L, SLSH,EQL, BSLS, DEL, END, PGDN, P7, P8, P9,
291 CAPS,A, O, E, U, I, D, H, T, N, S, MINS, ENT, P4, P5, P6, PPLS,
292 LSFT,SCLN,Q, J, K, X, B, M, W, V, Z, RSFT, UP, P1, P2, P3,
293 LCTL,LGUI,LALT, SPC, RALT,RGUI,APP, RCTL, LEFT,DOWN,RGHT, P0, PDOT,PENT
294 ),
295
296 /* 4: Workman http://viralintrospection.wordpress.com/2010/09/06/a-different-philosophy-in-designing-keyboard-layouts/
297 * ,-----------------------------------------------------------.
298 * | `| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Backspa|
299 * |-----------------------------------------------------------|
300 * |Tab | Q| D| R| W| B| J| F| U| P| ;| [| ]| \|
301 * |-----------------------------------------------------------|
302 * |CapsLo| A| S| H| T| G| Y| N| E| O| I| '|Return |
303 * |-----------------------------------------------------------|
304 * |Shift | Z| X| M| C| V| K| L| ,| ,| /|Shift |
305 * |-----------------------------------------------------------|
306 * |Ctrl |Gui |Alt | Space |Alt |Gui |Menu|Ctrl|
307 * `-----------------------------------------------------------'
308 */
309 KEYMAP(
310 ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PSCR,SLCK,BRK,
311 GRV, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS,EQL, BSPC, INS, HOME,PGUP, NLCK,PSLS,PAST,PMNS,
312 TAB, Q, D, R, W, B, J, F, U, P, SCLN,LBRC,RBRC,BSLS, DEL, END, PGDN, P7, P8, P9,
313 BSPC,A, S, H, T, G, Y, N, E, O, I, QUOT, ENT, P4, P5, P6, PPLS,
314 LSFT,Z, X, M, C, V, K, L, COMM,DOT, SLSH, RSFT, UP, P1, P2, P3,
315 LCTL,LGUI,LALT, SPC, RALT,RGUI,APP, RCTL, LEFT,DOWN,RGHT, P0, PDOT,PENT
316 ),
317
318 /* 5: Mouse keys
319 * ,-----------------------------------------------------------.
320 * |Esc| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|Backspa|
321 * |-----------------------------------------------------------|
322 * |Tab |MwL|MwU|McU|WwU|WwR|MwL|MwD|MwU|MwR| | | | \|
323 * |-----------------------------------------------------------|
324 * |CapsLo| |McL|McD|McR| |McL|McD|McU|McR|Fn0| |Return |
325 * |-----------------------------------------------------------|
326 * |Shift |VoD|VoU|Mut|Mb2|Mb3|Mb2|Mb1|VoD|VoU|Mut|Shift |
327 * |-----------------------------------------------------------|
328 * |Ctrl |Gui |Alt | Mb1 |Alt |Gui |Menu|Ctrl|
329 * `-----------------------------------------------------------'
330 * Mc = mouse cursor, Mw = mouse wheel, Mb = mouse button
331 * Vo = Volume, Mut = Mute
332 */
333 KEYMAP(
334 ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PSCR,SLCK,BRK,
335 ESC, F1, F2, F3, F4, F5, F6, F7, F8, F8, F10, F11, F12, BSPC, INS, HOME,PGUP, NLCK,PSLS,PAST,PMNS,
336 TAB, WH_L,WH_D,MS_U,WH_U,WH_R,WH_L,WH_D,WH_U,WH_R,NO, NO, NO, BSLS, DEL, END, PGDN, P7, P8, P9,
337 CAPS,NO, MS_L,MS_D,MS_R,NO, MS_L,MS_D,MS_U,MS_R,FN0, NO, ENT, P4, P5, P6, PPLS,
338 LSFT,VOLD,VOLU,MUTE,BTN2,BTN3,BTN2,BTN1,VOLD,VOLU,MUTE, RSFT, UP, P1, P2, P3,
339 LCTL,LGUI,LALT, BTN1, RALT,RGUI,APP, RCTL, LEFT,DOWN,RGHT, P0, PDOT,PENT
340 ),
341
342 /* 6: Cursor keys
343 * ,-----------------------------------------------------------.
344 * |Esc| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|Backspa|
345 * |-----------------------------------------------------------|
346 * |Tab |Hom|PgU| Up|PgU|End|Hom|PgD|PgU|End| | | | \|
347 * |-----------------------------------------------------------|
348 * |CapsLo| |Lef|Dow|Rig| |Lef|Dow| Up|Rig| | |Return |
349 * |-----------------------------------------------------------|
350 * |Shift | | | | | |Hom|PgD|PgU|End|Fn1|Shift |
351 * |-----------------------------------------------------------|
352 * |Ctrl |Gui |Alt | Space |Alt |Gui |Menu|Ctrl|
353 * `-----------------------------------------------------------'
354 */
355 KEYMAP(
356 ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PSCR,SLCK,BRK,
357 ESC, F1, F2, F3, F4, F5, F6, F7, F8, F8, F10, F11, F12, BSPC, INS, HOME,PGUP, NLCK,PSLS,PAST,PMNS,
358 TAB, NO, NO, NO, NO, NO, HOME,PGDN,PGUP,END, NO, NO, NO, BSLS, DEL, END, PGDN, P7, P8, P9,
359 CAPS,NO, NO, NO, NO, NO, LEFT,DOWN,UP, RGHT,NO, NO, ENT, P4, P5, P6, PPLS,
360 LSFT,VOLD,VOLU,MUTE,NO, NO, HOME,PGDN,PGUP,END, FN1, RSFT, UP, P1, P2, P3,
361 LCTL,LGUI,LALT, SPC, RALT,RGUI,APP, RCTL, LEFT,DOWN,RGHT, P0, PDOT,PENT
362 ),
363};
364
365
366uint8_t keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t col)
367{
368 return pgm_read_byte(&keymaps[(layer)][(row)][(col)]);
369}
370
371uint8_t keymap_fn_layer(uint8_t index)
372{
373 return pgm_read_byte(&fn_layer[index]);
374}
375
376uint8_t keymap_fn_keycode(uint8_t index)
377{
378 return pgm_read_byte(&fn_keycode[index]);
379}
diff --git a/converter/ps2_usb/keymap_common.c b/converter/ps2_usb/keymap_common.c
new file mode 100644
index 000000000..241d2e33b
--- /dev/null
+++ b/converter/ps2_usb/keymap_common.c
@@ -0,0 +1,30 @@
1/*
2Copyright 2011,2012,2013 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#include "keymap_common.h"
18
19
20/* translates key to keycode */
21uint8_t keymap_key_to_keycode(uint8_t layer, key_t key)
22{
23 return pgm_read_byte(&keymaps[(layer)][(key.row)][(key.col)]);
24}
25
26/* translates Fn keycode to action */
27action_t keymap_fn_to_action(uint8_t keycode)
28{
29 return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
30}
diff --git a/converter/ps2_usb/keymap_common.h b/converter/ps2_usb/keymap_common.h
new file mode 100644
index 000000000..216a8dc02
--- /dev/null
+++ b/converter/ps2_usb/keymap_common.h
@@ -0,0 +1,174 @@
1/*
2Copyright 2011,2012,2013 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#ifndef KEYMAP_COMMON_H
18#define KEYMAP_COMMON_H
19
20#include <stdint.h>
21#include <stdbool.h>
22#include <avr/pgmspace.h>
23#include "keycode.h"
24#include "action.h"
25#include "action_macro.h"
26#include "report.h"
27#include "print.h"
28#include "debug.h"
29#include "keymap.h"
30
31
32// 32*8(256) byte array which converts PS/2 code into USB code
33extern const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
34extern const uint16_t fn_actions[];
35
36
37/* All keys */
38#define KEYMAP_ALL( \
39 K76,K05,K06,K04,K0C,K03,K0B,K83,K0A,K01,K09,K78,K07, KFC,K7E,KFE, \
40 K0E,K16,K1E,K26,K25,K2E,K36,K3D,K3E,K46,K45,K4E,K55,K66, KF0,KEC,KFD, K77,KCA,K7C,K7B, \
41 K0D,K15,K1D,K24,K2D,K2C,K35,K3C,K43,K44,K4D,K54,K5B,K5D, KF1,KE9,KFA, K6C,K75,K7D, \
42 K58,K1C,K1B,K23,K2B,K34,K33,K3B,K42,K4B,K4C,K52, K5A, K6B,K73,K74,K79, \
43 K12,K1A,K22,K21,K2A,K32,K31,K3A,K41,K49,K4A, K59, KF5, K69,K72,K7A, \
44 K14,K9F,K11, K29, K91,KA7,KAF,K94, KEB,KF2,KF4, K70, K71,KDA, \
45 \
46 K61, /* for European ISO */ \
47 K51, K13, K6A, K64, K67, /* for Japanese JIS */ \
48 K08, K10, K18, K20, K28, K30, K38, K40, K48, K50, K57, K5F, /* F13-24 */ \
49 KB7, KBF, KDE, /* System Power, Sleep, Wake */ \
50 KA3, KB2, KA1, /* Mute, Volume Up, Volume Down */ \
51 KCD, K95, KBB, KB4, KD0, /* Next, Previous, Stop, Pause, Media Select */ \
52 KC8, KAB, KC0, /* Mail, Calculator, My Computer */ \
53 K90, KBA, KB8, KB0, /* WWW Search, Home, Back, Forward */ \
54 KA8, KA0, K98 /* WWW Stop, Refresh, Favorites */ \
55) { \
56 { KC_NO, KC_##K01, KC_NO, KC_##K03, KC_##K04, KC_##K05, KC_##K06, KC_##K07 }, \
57 { KC_##K08, KC_##K09, KC_##K0A, KC_##K0B, KC_##K0C, KC_##K0D, KC_##K0E, KC_NO }, \
58 { KC_##K10, KC_##K11, KC_##K12, KC_##K13, KC_##K14, KC_##K15, KC_##K16, KC_NO }, \
59 { KC_##K18, KC_NO, KC_##K1A, KC_##K1B, KC_##K1C, KC_##K1D, KC_##K1E, KC_NO }, \
60 { KC_##K20, KC_##K21, KC_##K22, KC_##K23, KC_##K24, KC_##K25, KC_##K26, KC_NO }, \
61 { KC_##K28, KC_##K29, KC_##K2A, KC_##K2B, KC_##K2C, KC_##K2D, KC_##K2E, KC_NO }, \
62 { KC_##K30, KC_##K31, KC_##K32, KC_##K33, KC_##K34, KC_##K35, KC_##K36, KC_NO }, \
63 { KC_##K38, KC_NO, KC_##K3A, KC_##K3B, KC_##K3C, KC_##K3D, KC_##K3E, KC_NO }, \
64 { KC_##K40, KC_##K41, KC_##K42, KC_##K43, KC_##K44, KC_##K45, KC_##K46, KC_NO }, \
65 { KC_##K48, KC_##K49, KC_##K4A, KC_##K4B, KC_##K4C, KC_##K4D, KC_##K4E, KC_NO }, \
66 { KC_##K50, KC_##K51, KC_##K52, KC_NO, KC_##K54, KC_##K55, KC_NO, KC_##K57 }, \
67 { KC_##K58, KC_##K59, KC_##K5A, KC_##K5B, KC_NO, KC_##K5D, KC_NO, KC_##K5F }, \
68 { KC_NO, KC_##K61, KC_NO, KC_NO, KC_##K64, KC_NO, KC_##K66, KC_##K67 }, \
69 { KC_NO, KC_##K69, KC_##K6A, KC_##K6B, KC_##K6C, KC_NO, KC_NO, KC_NO }, \
70 { KC_##K70, KC_##K71, KC_##K72, KC_##K73, KC_##K74, KC_##K75, KC_##K76, KC_##K77 }, \
71 { KC_##K78, KC_##K79, KC_##K7A, KC_##K7B, KC_##K7C, KC_##K7D, KC_##K7E, KC_NO }, \
72 { KC_NO, KC_NO, KC_NO, KC_##K83, KC_NO, KC_NO, KC_NO, KC_NO }, \
73 { KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO }, \
74 { KC_##K90, KC_##K91, KC_NO, KC_NO, KC_##K94, KC_##K95, KC_NO, KC_NO }, \
75 { KC_##K98, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_##K9F }, \
76 { KC_##KA0, KC_##KA1, KC_NO, KC_##KA3, KC_NO, KC_NO, KC_NO, KC_##KA7 }, \
77 { KC_##KA8, KC_NO, KC_NO, KC_##KAB, KC_NO, KC_NO, KC_NO, KC_##KAF }, \
78 { KC_##KB0, KC_NO, KC_##KB2, KC_NO, KC_##KB4, KC_NO, KC_NO, KC_##KB7 }, \
79 { KC_##KB8, KC_NO, KC_##KBA, KC_##KBB, KC_NO, KC_NO, KC_NO, KC_##KBF }, \
80 { KC_##KC0, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO }, \
81 { KC_##KC8, KC_NO, KC_##KCA, KC_NO, KC_NO, KC_##KCD, KC_NO, KC_NO }, \
82 { KC_##KD0, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO }, \
83 { KC_NO, KC_NO, KC_##KDA, KC_NO, KC_NO, KC_NO, KC_##KDE, KC_NO }, \
84 { KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO }, \
85 { KC_NO, KC_##KE9, KC_NO, KC_##KEB, KC_##KEC, KC_NO, KC_NO, KC_NO }, \
86 { KC_##KF0, KC_##KF1, KC_##KF2, KC_NO, KC_##KF4, KC_##KF5, KC_NO, KC_NO }, \
87 { KC_NO, KC_NO, KC_##KFA, KC_NO, KC_##KFC, KC_##KFD, KC_##KFE, KC_NO }, \
88}
89
90/* US layout */
91#define KEYMAP( \
92 K76,K05,K06,K04,K0C,K03,K0B,K83,K0A,K01,K09,K78,K07, KFC,K7E,KFE, \
93 K0E,K16,K1E,K26,K25,K2E,K36,K3D,K3E,K46,K45,K4E,K55,K66, KF0,KEC,KFD, K77,KCA,K7C,K7B, \
94 K0D,K15,K1D,K24,K2D,K2C,K35,K3C,K43,K44,K4D,K54,K5B,K5D, KF1,KE9,KFA, K6C,K75,K7D, \
95 K58,K1C,K1B,K23,K2B,K34,K33,K3B,K42,K4B,K4C,K52, K5A, K6B,K73,K74,K79, \
96 K12,K1A,K22,K21,K2A,K32,K31,K3A,K41,K49,K4A, K59, KF5, K69,K72,K7A, \
97 K14,K9F,K11, K29, K91,KA7,KAF,K94, KEB,KF2,KF4, K70, K71,KDA \
98) \
99KEYMAP_ALL( \
100 K76,K05,K06,K04,K0C,K03,K0B,K83,K0A,K01,K09,K78,K07, KFC,K7E,KFE, \
101 K0E,K16,K1E,K26,K25,K2E,K36,K3D,K3E,K46,K45,K4E,K55,K66, KF0,KEC,KFD, K77,KCA,K7C,K7B, \
102 K0D,K15,K1D,K24,K2D,K2C,K35,K3C,K43,K44,K4D,K54,K5B,K5D, KF1,KE9,KFA, K6C,K75,K7D, \
103 K58,K1C,K1B,K23,K2B,K34,K33,K3B,K42,K4B,K4C,K52, K5A, K6B,K73,K74,K79, \
104 K12,K1A,K22,K21,K2A,K32,K31,K3A,K41,K49,K4A, K59, KF5, K69,K72,K7A, \
105 K14,K9F,K11, K29, K91,KA7,KAF,K94, KEB,KF2,KF4, K70, K71,KDA, \
106 \
107 NUBS, \
108 RO, KANA, JYEN, HENK, MHEN, \
109 F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23, F24, \
110 SYSTEM_POWER, SYSTEM_SLEEP, SYSTEM_WAKE, \
111 AUDIO_MUTE, AUDIO_VOL_UP, AUDIO_VOL_DOWN, \
112 MEDIA_NEXT_TRACK, MEDIA_PREV_TRACK, MEDIA_STOP, MEDIA_PLAY_PAUSE, MEDIA_SELECT, \
113 MAIL, CALCULATOR, MY_COMPUTER, \
114 WWW_SEARCH, WWW_HOME, WWW_BACK, WWW_FORWARD, \
115 WWW_STOP, WWW_REFRESH, WWW_FAVORITES \
116)
117
118/* ISO layout */
119#define KEYMAP_ISO( \
120 K76,K05,K06,K04,K0C,K03,K0B,K83,K0A,K01,K09,K78,K07, KFC,K7E,KFE, \
121 K0E,K16,K1E,K26,K25,K2E,K36,K3D,K3E,K46,K45,K4E,K55,K66, KF0,KEC,KFD, K77,KCA,K7C,K7B, \
122 K0D,K15,K1D,K24,K2D,K2C,K35,K3C,K43,K44,K4D,K54,K5B, KF1,KE9,KFA, K6C,K75,K7D, \
123 K58,K1C,K1B,K23,K2B,K34,K33,K3B,K42,K4B,K4C,K52,K5D,K5A, K6B,K73,K74,K79, \
124 K12,K61,K1A,K22,K21,K2A,K32,K31,K3A,K41,K49,K4A, K59, KF5, K69,K72,K7A, \
125 K14,K9F,K11, K29, K91,KA7,KAF,K94, KEB,KF2,KF4, K70, K71,KDA \
126) \
127KEYMAP_ALL( \
128 K76,K05,K06,K04,K0C,K03,K0B,K83,K0A,K01,K09,K78,K07, KFC,K7E,KFE, \
129 K0E,K16,K1E,K26,K25,K2E,K36,K3D,K3E,K46,K45,K4E,K55,K66, KF0,KEC,KFD, K77,KCA,K7C,K7B, \
130 K0D,K15,K1D,K24,K2D,K2C,K35,K3C,K43,K44,K4D,K54,K5B,K5D, KF1,KE9,KFA, K6C,K75,K7D, \
131 K58,K1C,K1B,K23,K2B,K34,K33,K3B,K42,K4B,K4C,K52, K5A, K6B,K73,K74,K79, \
132 K12,K1A,K22,K21,K2A,K32,K31,K3A,K41,K49,K4A, K59, KF5, K69,K72,K7A, \
133 K14,K9F,K11, K29, K91,KA7,KAF,K94, KEB,KF2,KF4, K70, K71,KDA, \
134 \
135 K61, \
136 RO, KANA, JYEN, HENK, MHEN, \
137 F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23, F24, \
138 SYSTEM_POWER, SYSTEM_SLEEP, SYSTEM_WAKE, \
139 AUDIO_MUTE, AUDIO_VOL_UP, AUDIO_VOL_DOWN, \
140 MEDIA_NEXT_TRACK, MEDIA_PREV_TRACK, MEDIA_STOP, MEDIA_PLAY_PAUSE, MEDIA_SELECT, \
141 MAIL, CALCULATOR, MY_COMPUTER, \
142 WWW_SEARCH, WWW_HOME, WWW_BACK, WWW_FORWARD, \
143 WWW_STOP, WWW_REFRESH, WWW_FAVORITES \
144)
145
146/* JIS layout */
147#define KEYMAP_JIS( \
148 K76,K05,K06,K04,K0C,K03,K0B,K83,K0A,K01,K09,K78,K07, KFC,K7E,KFE, \
149 K0E,K16,K1E,K26,K25,K2E,K36,K3D,K3E,K46,K45,K4E,K55,K6A,K66, KF0,KEC,KFD, K77,KCA,K7C,K7B, \
150 K0D,K15,K1D,K24,K2D,K2C,K35,K3C,K43,K44,K4D,K54,K5B, KF1,KE9,KFA, K6C,K75,K7D, \
151 K58,K1C,K1B,K23,K2B,K34,K33,K3B,K42,K4B,K4C,K52,K5D, K5A, K6B,K73,K74,K79, \
152 K12,K1A,K22,K21,K2A,K32,K31,K3A,K41,K49,K4A,K51, K59, KF5, K69,K72,K7A, \
153 K14,K9F,K11, K67,K29,K64,K13, K91,KA7,KAF,K94, KEB,KF2,KF4, K70, K71,KDA \
154) \
155KEYMAP_ALL( \
156 K76,K05,K06,K04,K0C,K03,K0B,K83,K0A,K01,K09,K78,K07, KFC,K7E,KFE, \
157 K0E,K16,K1E,K26,K25,K2E,K36,K3D,K3E,K46,K45,K4E,K55,K66, KF0,KEC,KFD, K77,KCA,K7C,K7B, \
158 K0D,K15,K1D,K24,K2D,K2C,K35,K3C,K43,K44,K4D,K54,K5B,K5D, KF1,KE9,KFA, K6C,K75,K7D, \
159 K58,K1C,K1B,K23,K2B,K34,K33,K3B,K42,K4B,K4C,K52, K5A, K6B,K73,K74,K79, \
160 K12,K1A,K22,K21,K2A,K32,K31,K3A,K41,K49,K4A, K59, KF5, K69,K72,K7A, \
161 K14,K9F,K11, K29, K91,KA7,KAF,K94, KEB,KF2,KF4, K70, K71,KDA, \
162 \
163 NUBS, \
164 K51, K13, K6A, K64, K67, \
165 F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23, F24, \
166 SYSTEM_POWER, SYSTEM_SLEEP, SYSTEM_WAKE, \
167 AUDIO_MUTE, AUDIO_VOL_UP, AUDIO_VOL_DOWN, \
168 MEDIA_NEXT_TRACK, MEDIA_PREV_TRACK, MEDIA_STOP, MEDIA_PLAY_PAUSE, MEDIA_SELECT, \
169 MAIL, CALCULATOR, MY_COMPUTER, \
170 WWW_SEARCH, WWW_HOME, WWW_BACK, WWW_FORWARD, \
171 WWW_STOP, WWW_REFRESH, WWW_FAVORITES \
172)
173
174#endif
diff --git a/converter/ps2_usb/keymap_plain.c b/converter/ps2_usb/keymap_plain.c
new file mode 100644
index 000000000..9c6e7d9ff
--- /dev/null
+++ b/converter/ps2_usb/keymap_plain.c
@@ -0,0 +1,50 @@
1/*
2Copyright 2011,2012,2013 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#include "keymap_common.h"
18
19
20const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
21 /* 0: default
22 * ,---. ,---------------. ,---------------. ,---------------. ,-----------. ,-----------.
23 * |Esc| |F1 |F2 |F3 |F4 | |F5 |F6 |F7 |F8 | |F9 |F10|F11|F12| |PrS|ScL|Pau| |Pwr|Slp|Wak|
24 * `---' `---------------' `---------------' `---------------' `-----------' `-----------'
25 * ,-----------------------------------------------------------. ,-----------. ,---------------.
26 * | `| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Backspa| |Ins|Hom|PgU| |NmL| /| *| -|
27 * |-----------------------------------------------------------| |-----------| |---------------|
28 * |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]| \| |Del|End|PgD| | 7| 8| 9| |
29 * |-----------------------------------------------------------| `-----------' |-----------| +|
30 * |CapsLo| A| S| D| F| G| H| J| K| L| ;| '|Return | | 4| 5| 6| |
31 * |-----------------------------------------------------------| ,---. |---------------|
32 * |Shift | Z| X| C| V| B| N| M| ,| ,| /|Shift | |Up | | 1| 2| 3| |
33 * |-----------------------------------------------------------| ,-----------. |-----------|Ent|
34 * |Ctrl |Gui |Alt | Space |Alt |Gui |Menu|Ctrl| |Lef|Dow|Rig| | 0| .| |
35 * `-----------------------------------------------------------' `-----------' `---------------'
36 * ; = Fn0(to Layer 5)
37 * / = Fn1(to Layer 6)
38 */
39 KEYMAP(
40 ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PSCR,SLCK,BRK,
41 GRV, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS,EQL, BSPC, INS, HOME,PGUP, NLCK,PSLS,PAST,PMNS,
42 TAB, Q, W, E, R, T, Y, U, I, O, P, LBRC,RBRC,BSLS, DEL, END, PGDN, P7, P8, P9,
43 CAPS,A, S, D, F, G, H, J, K, L, SCLN,QUOT, ENT, P4, P5, P6, PPLS,
44 LSFT,Z, X, C, V, B, N, M, COMM,DOT, SLSH, RSFT, UP, P1, P2, P3,
45 LCTL,LGUI,LALT, SPC, RALT,RGUI,APP, RCTL, LEFT,DOWN,RGHT, P0, PDOT,PENT
46 ),
47};
48
49const uint16_t PROGMEM fn_actions[] = {
50};
diff --git a/converter/ps2_usb/matrix.c b/converter/ps2_usb/matrix.c
index 4187ea060..45344c0f7 100644
--- a/converter/ps2_usb/matrix.c
+++ b/converter/ps2_usb/matrix.c
@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
19#include <stdbool.h> 19#include <stdbool.h>
20#include <avr/io.h> 20#include <avr/io.h>
21#include <util/delay.h> 21#include <util/delay.h>
22#include "action.h"
22#include "print.h" 23#include "print.h"
23#include "util.h" 24#include "util.h"
24#include "debug.h" 25#include "debug.h"
@@ -28,6 +29,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
28 29
29static void matrix_make(uint8_t code); 30static void matrix_make(uint8_t code);
30static void matrix_break(uint8_t code); 31static void matrix_break(uint8_t code);
32static void matrix_clear(void);
31#ifdef MATRIX_HAS_GHOST 33#ifdef MATRIX_HAS_GHOST
32static bool matrix_has_ghost_in_row(uint8_t row); 34static bool matrix_has_ghost_in_row(uint8_t row);
33#endif 35#endif
@@ -83,6 +85,7 @@ uint8_t matrix_cols(void)
83 85
84void matrix_init(void) 86void matrix_init(void)
85{ 87{
88 debug_enable = true;
86 ps2_host_init(); 89 ps2_host_init();
87 90
88 // initialize matrix state: all keys off 91 // initialize matrix state: all keys off
@@ -185,8 +188,8 @@ uint8_t matrix_scan(void)
185 matrix_break(PAUSE); 188 matrix_break(PAUSE);
186 } 189 }
187 190
188 uint8_t code; 191 uint8_t code = ps2_host_recv();
189 while ((code = ps2_host_recv())) { 192 if (!ps2_error) {
190 switch (state) { 193 switch (state) {
191 case INIT: 194 case INIT:
192 switch (code) { 195 switch (code) {
@@ -207,11 +210,19 @@ uint8_t matrix_scan(void)
207 matrix_make(PRINT_SCREEN); 210 matrix_make(PRINT_SCREEN);
208 state = INIT; 211 state = INIT;
209 break; 212 break;
213 case 0x00: // Overrun [3]p.25
214 matrix_clear();
215 clear_keyboard();
216 print("Overrun\n");
217 state = INIT;
218 break;
210 default: // normal key make 219 default: // normal key make
211 if (code < 0x80) { 220 if (code < 0x80) {
212 matrix_make(code); 221 matrix_make(code);
213 } else { 222 } else {
214 debug("unexpected scan code at INIT: "); debug_hex(code); debug("\n"); 223 matrix_clear();
224 clear_keyboard();
225 xprintf("unexpected scan code at INIT: %02X\n", code);
215 } 226 }
216 state = INIT; 227 state = INIT;
217 } 228 }
@@ -232,7 +243,9 @@ uint8_t matrix_scan(void)
232 if (code < 0x80) { 243 if (code < 0x80) {
233 matrix_make(code|0x80); 244 matrix_make(code|0x80);
234 } else { 245 } else {
235 debug("unexpected scan code at E0: "); debug_hex(code); debug("\n"); 246 matrix_clear();
247 clear_keyboard();
248 xprintf("unexpected scan code at E0: %02X\n", code);
236 } 249 }
237 state = INIT; 250 state = INIT;
238 } 251 }
@@ -247,11 +260,18 @@ uint8_t matrix_scan(void)
247 matrix_break(PRINT_SCREEN); 260 matrix_break(PRINT_SCREEN);
248 state = INIT; 261 state = INIT;
249 break; 262 break;
263 case 0xF0:
264 matrix_clear();
265 clear_keyboard();
266 xprintf("unexpected scan code at F0: F0(clear and cont.)\n");
267 break;
250 default: 268 default:
251 if (code < 0x80) { 269 if (code < 0x80) {
252 matrix_break(code); 270 matrix_break(code);
253 } else { 271 } else {
254 debug("unexpected scan code at F0: "); debug_hex(code); debug("\n"); 272 matrix_clear();
273 clear_keyboard();
274 xprintf("unexpected scan code at F0: %02X\n", code);
255 } 275 }
256 state = INIT; 276 state = INIT;
257 } 277 }
@@ -266,7 +286,9 @@ uint8_t matrix_scan(void)
266 if (code < 0x80) { 286 if (code < 0x80) {
267 matrix_break(code|0x80); 287 matrix_break(code|0x80);
268 } else { 288 } else {
269 debug("unexpected scan code at E0_F0: "); debug_hex(code); debug("\n"); 289 matrix_clear();
290 clear_keyboard();
291 xprintf("unexpected scan code at E0_F0: %02X\n", code);
270 } 292 }
271 state = INIT; 293 state = INIT;
272 } 294 }
@@ -357,8 +379,15 @@ uint8_t matrix_scan(void)
357 default: 379 default:
358 state = INIT; 380 state = INIT;
359 } 381 }
360 phex(code);
361 } 382 }
383
384 // TODO: request RESEND when error occurs?
385/*
386 if (PS2_IS_FAILED(ps2_error)) {
387 uint8_t ret = ps2_host_send(PS2_RESEND);
388 xprintf("Resend: %02X\n", ret);
389 }
390*/
362 return 1; 391 return 1;
363} 392}
364 393
@@ -450,3 +479,9 @@ static void matrix_break(uint8_t code)
450 is_modified = true; 479 is_modified = true;
451 } 480 }
452} 481}
482
483inline
484static void matrix_clear(void)
485{
486 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
487}
diff --git a/keyboard/onekey/Makefile.lufa b/keyboard/onekey/Makefile
index 60a84ba00..78732e470 100644
--- a/keyboard/onekey/Makefile.lufa
+++ b/keyboard/onekey/Makefile
@@ -116,8 +116,8 @@ CONSOLE_ENABLE = yes # Console for debug(+400)
116#SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend 116#SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend
117#NKRO_ENABLE = yes # USB Nkey Rollover - not yet supported in LUFA 117#NKRO_ENABLE = yes # USB Nkey Rollover - not yet supported in LUFA
118 118
119PS2_MOUSE_ENABLE = yes # PS/2 mouse(TrackPoint) support 119#PS2_MOUSE_ENABLE = yes # PS/2 mouse(TrackPoint) support
120PS2_USE_BUSYWAIT = yes # uses primitive reference code 120#PS2_USE_BUSYWAIT = yes # uses primitive reference code
121#PS2_USE_INT = yes # uses external interrupt for falling edge of PS/2 clock pin 121#PS2_USE_INT = yes # uses external interrupt for falling edge of PS/2 clock pin
122#PS2_USE_USART = yes # uses hardware USART engine for PS/2 signal receive(recomened) 122#PS2_USE_USART = yes # uses hardware USART engine for PS/2 signal receive(recomened)
123 123
diff --git a/keyboard/onekey/README.md b/keyboard/onekey/README.md
index 6ccc99929..7413f3880 100644
--- a/keyboard/onekey/README.md
+++ b/keyboard/onekey/README.md
@@ -1,5 +1,5 @@
1Onekey 1Onekey
2====== 2======
3Just one key keyboard for example. It sends 'a' key if pins PD0 and PD1 are short-circuited. 3Just one key keyboard for example. It sends 'a' key if pins PB0 and PB1 are short-circuited.
4 4
5https://github.com/tmk/tmk_keyboard/issues/56 5https://github.com/tmk/tmk_keyboard/issues/56
diff --git a/keyboard/onekey/config.h b/keyboard/onekey/config.h
index 1d2e052bf..bf1d2b1ed 100644
--- a/keyboard/onekey/config.h
+++ b/keyboard/onekey/config.h
@@ -73,7 +73,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
73# define PS2_CLOCK_PORT PORTD 73# define PS2_CLOCK_PORT PORTD
74# define PS2_CLOCK_PIN PIND 74# define PS2_CLOCK_PIN PIND
75# define PS2_CLOCK_DDR DDRD 75# define PS2_CLOCK_DDR DDRD
76# define PS2_CLOCK_BIT 5 76# define PS2_CLOCK_BIT 1
77# define PS2_DATA_PORT PORTD 77# define PS2_DATA_PORT PORTD
78# define PS2_DATA_PIN PIND 78# define PS2_DATA_PIN PIND
79# define PS2_DATA_DDR DDRD 79# define PS2_DATA_DDR DDRD
@@ -87,7 +87,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
87#define PS2_CLOCK_PORT PORTD 87#define PS2_CLOCK_PORT PORTD
88#define PS2_CLOCK_PIN PIND 88#define PS2_CLOCK_PIN PIND
89#define PS2_CLOCK_DDR DDRD 89#define PS2_CLOCK_DDR DDRD
90#define PS2_CLOCK_BIT 5 90#define PS2_CLOCK_BIT 1
91#define PS2_DATA_PORT PORTD 91#define PS2_DATA_PORT PORTD
92#define PS2_DATA_PIN PIND 92#define PS2_DATA_PIN PIND
93#define PS2_DATA_DDR DDRD 93#define PS2_DATA_DDR DDRD
diff --git a/keyboard/onekey/matrix.c b/keyboard/onekey/matrix.c
index a0a14ff82..29df86832 100644
--- a/keyboard/onekey/matrix.c
+++ b/keyboard/onekey/matrix.c
@@ -139,29 +139,29 @@ uint8_t matrix_key_count(void)
139 139
140/* Column pin configuration 140/* Column pin configuration
141 * col: 0 141 * col: 0
142 * pin: D0 142 * pin: B0
143 */ 143 */
144static void init_cols(void) 144static void init_cols(void)
145{ 145{
146 // Input with pull-up(DDR:0, PORT:1) 146 // Input with pull-up(DDR:0, PORT:1)
147 DDRD &= ~(1<<0); 147 DDRB &= ~(1<<0);
148 PORTD |= (1<<0); 148 PORTB |= (1<<0);
149} 149}
150 150
151static matrix_row_t read_cols(void) 151static matrix_row_t read_cols(void)
152{ 152{
153 return (PIND&(1<<0) ? 0 : (1<<0)); 153 return (PINB&(1<<0) ? 0 : (1<<0));
154} 154}
155 155
156/* Row pin configuration 156/* Row pin configuration
157 * row: 0 157 * row: 0
158 * pin: D1 158 * pin: B1
159 */ 159 */
160static void unselect_rows(void) 160static void unselect_rows(void)
161{ 161{
162 // Hi-Z(DDR:0, PORT:0) to unselect 162 // Hi-Z(DDR:0, PORT:0) to unselect
163 DDRD &= ~0b00000010; 163 DDRB &= ~0b00000010;
164 PORTD &= ~0b00000010; 164 PORTB &= ~0b00000010;
165} 165}
166 166
167static void select_row(uint8_t row) 167static void select_row(uint8_t row)
@@ -169,8 +169,8 @@ static void select_row(uint8_t row)
169 // Output low(DDR:1, PORT:0) to select 169 // Output low(DDR:1, PORT:0) to select
170 switch (row) { 170 switch (row) {
171 case 0: 171 case 0:
172 DDRD |= (1<<1); 172 DDRB |= (1<<1);
173 PORTD &= ~(1<<1); 173 PORTB &= ~(1<<1);
174 break; 174 break;
175 } 175 }
176} 176}
diff --git a/protocol.mk b/protocol.mk
index 0d5f06c7e..7f561e62d 100644
--- a/protocol.mk
+++ b/protocol.mk
@@ -8,12 +8,12 @@ ifdef PS2_MOUSE_ENABLE
8endif 8endif
9 9
10ifdef PS2_USE_BUSYWAIT 10ifdef PS2_USE_BUSYWAIT
11 SRC += protocol/ps2.c 11 SRC += protocol/ps2_busywait.c
12 OPT_DEFS += -DPS2_USE_BUSYWAIT 12 OPT_DEFS += -DPS2_USE_BUSYWAIT
13endif 13endif
14 14
15ifdef PS2_USE_INT 15ifdef PS2_USE_INT
16 SRC += protocol/ps2.c 16 SRC += protocol/ps2_interrupt.c
17 OPT_DEFS += -DPS2_USE_INT 17 OPT_DEFS += -DPS2_USE_INT
18endif 18endif
19 19
diff --git a/protocol/lufa/lufa.c b/protocol/lufa/lufa.c
index 04e8e78f3..eca51c878 100644
--- a/protocol/lufa/lufa.c
+++ b/protocol/lufa/lufa.c
@@ -148,7 +148,6 @@ static void Console_Task(void)
148*/ 148*/
149void EVENT_USB_Device_Connect(void) 149void EVENT_USB_Device_Connect(void)
150{ 150{
151 led_set(0x1f); // all on
152} 151}
153 152
154void EVENT_USB_Device_Disconnect(void) 153void EVENT_USB_Device_Disconnect(void)
@@ -172,8 +171,9 @@ void EVENT_USB_Device_WakeUp()
172 171
173#ifdef SLEEP_LED_ENABLE 172#ifdef SLEEP_LED_ENABLE
174 sleep_led_disable(); 173 sleep_led_disable();
175#endif 174 // NOTE: converters may not accept this
176 led_set(host_keyboard_leds()); 175 led_set(host_keyboard_leds());
176#endif
177} 177}
178 178
179void EVENT_USB_Device_StartOfFrame(void) 179void EVENT_USB_Device_StartOfFrame(void)
diff --git a/protocol/pjrc/usb.c b/protocol/pjrc/usb.c
index 84c99972f..393b36f78 100644
--- a/protocol/pjrc/usb.c
+++ b/protocol/pjrc/usb.c
@@ -662,8 +662,9 @@ ISR(USB_GEN_vect)
662 suspend_wakeup_init(); 662 suspend_wakeup_init();
663#ifdef SLEEP_LED_ENABLE 663#ifdef SLEEP_LED_ENABLE
664 sleep_led_disable(); 664 sleep_led_disable();
665#endif 665 // NOTE: converters may not accept this
666 led_set(host_keyboard_leds()); 666 led_set(host_keyboard_leds());
667#endif
667 668
668 UDIEN |= (1<<SUSPE); 669 UDIEN |= (1<<SUSPE);
669 UDIEN &= ~(1<<WAKEUPE); 670 UDIEN &= ~(1<<WAKEUPE);
diff --git a/protocol/ps2.h b/protocol/ps2.h
index 834165356..483eea720 100644
--- a/protocol/ps2.h
+++ b/protocol/ps2.h
@@ -1,5 +1,5 @@
1/* 1/*
2Copyright 2010,2011 Jun WAKO <wakojun@gmail.com> 2Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com>
3 3
4This software is licensed with a Modified BSD License. 4This software is licensed with a Modified BSD License.
5All of this is supposed to be Free Software, Open Source, DFSG-free, 5All of this is supposed to be Free Software, Open Source, DFSG-free,
@@ -37,32 +37,46 @@ POSSIBILITY OF SUCH DAMAGE.
37 37
38#ifndef PS2_H 38#ifndef PS2_H
39#define PS2_H 39#define PS2_H
40
41#include <stdbool.h>
42#include <util/delay.h>
43#include <avr/io.h>
44
40/* 45/*
41 * Primitive PS/2 Library for AVR 46 * Primitive PS/2 Library for AVR
47 *
48 * PS/2 Resources
49 * --------------
50 * [1] The PS/2 Mouse/Keyboard Protocol
51 * http://www.computer-engineering.org/ps2protocol/
52 * Concise and thorough primer of PS/2 protocol.
53 *
54 * [2] Keyboard and Auxiliary Device Controller
55 * http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
56 * Signal Timing and Format
57 *
58 * [3] Keyboards(101- and 102-key)
59 * http://www.mcamafia.de/pdf/ibm_hitrc11.pdf
60 * Keyboard Layout, Scan Code Set, POR, and Commands.
61 *
62 * [4] PS/2 Reference Manuals
63 * http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
64 * Collection of IBM Personal System/2 documents.
65 *
66 * [5] TrackPoint Engineering Specifications for version 3E
67 * https://web.archive.org/web/20100526161812/http://wwwcssrv.almaden.ibm.com/trackpoint/download.html
42 */ 68 */
43
44
45/* port settings for clock and data line */
46#if !(defined(PS2_CLOCK_PORT) && \
47 defined(PS2_CLOCK_PIN) && \
48 defined(PS2_CLOCK_DDR) && \
49 defined(PS2_CLOCK_BIT))
50# error "PS/2 clock port setting is required in config.h"
51#endif
52
53#if !(defined(PS2_DATA_PORT) && \
54 defined(PS2_DATA_PIN) && \
55 defined(PS2_DATA_DDR) && \
56 defined(PS2_DATA_BIT))
57# error "PS/2 data port setting is required in config.h"
58#endif
59
60#define PS2_ACK 0xFA 69#define PS2_ACK 0xFA
61#define PS2_RESEND 0xFE 70#define PS2_RESEND 0xFE
62#define PS2_SET_LED 0xED 71#define PS2_SET_LED 0xED
63 72
64#define PS2_ERR_NONE 0 73// TODO: error numbers
65#define PS2_ERR_PARITY 0x10 74#define PS2_ERR_NONE 0
75#define PS2_ERR_STARTBIT1 1
76#define PS2_ERR_STARTBIT2 2
77#define PS2_ERR_STARTBIT3 3
78#define PS2_ERR_PARITY 0x10
79#define PS2_ERR_NODATA 0x20
66 80
67#define PS2_LED_SCROLL_LOCK 0 81#define PS2_LED_SCROLL_LOCK 0
68#define PS2_LED_NUM_LOCK 1 82#define PS2_LED_NUM_LOCK 1
@@ -71,13 +85,101 @@ POSSIBILITY OF SUCH DAMAGE.
71 85
72extern uint8_t ps2_error; 86extern uint8_t ps2_error;
73 87
74/* host role */
75void ps2_host_init(void); 88void ps2_host_init(void);
76uint8_t ps2_host_send(uint8_t data); 89uint8_t ps2_host_send(uint8_t data);
77uint8_t ps2_host_recv_response(void); 90uint8_t ps2_host_recv_response(void);
78uint8_t ps2_host_recv(void); 91uint8_t ps2_host_recv(void);
79void ps2_host_set_led(uint8_t usb_led); 92void ps2_host_set_led(uint8_t usb_led);
80 93
81/* device role */ 94
95/* Check port settings for clock and data line */
96#if !(defined(PS2_CLOCK_PORT) && \
97 defined(PS2_CLOCK_PIN) && \
98 defined(PS2_CLOCK_DDR) && \
99 defined(PS2_CLOCK_BIT))
100# error "PS/2 clock port setting is required in config.h"
101#endif
102
103#if !(defined(PS2_DATA_PORT) && \
104 defined(PS2_DATA_PIN) && \
105 defined(PS2_DATA_DDR) && \
106 defined(PS2_DATA_BIT))
107# error "PS/2 data port setting is required in config.h"
108#endif
109
110/*--------------------------------------------------------------------
111 * static functions
112 *------------------------------------------------------------------*/
113static inline void clock_lo(void)
114{
115 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
116 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
117}
118static inline void clock_hi(void)
119{
120 /* input with pull up */
121 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
122 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
123}
124static inline bool clock_in(void)
125{
126 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
127 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
128 _delay_us(1);
129 return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
130}
131static inline void data_lo(void)
132{
133 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
134 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
135}
136static inline void data_hi(void)
137{
138 /* input with pull up */
139 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
140 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
141}
142static inline bool data_in(void)
143{
144 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
145 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
146 _delay_us(1);
147 return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
148}
149
150static inline uint16_t wait_clock_lo(uint16_t us)
151{
152 while (clock_in() && us) { asm(""); _delay_us(1); us--; }
153 return us;
154}
155static inline uint16_t wait_clock_hi(uint16_t us)
156{
157 while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
158 return us;
159}
160static inline uint16_t wait_data_lo(uint16_t us)
161{
162 while (data_in() && us) { asm(""); _delay_us(1); us--; }
163 return us;
164}
165static inline uint16_t wait_data_hi(uint16_t us)
166{
167 while (!data_in() && us) { asm(""); _delay_us(1); us--; }
168 return us;
169}
170
171/* idle state that device can send */
172static inline void idle(void)
173{
174 clock_hi();
175 data_hi();
176}
177
178/* inhibit device to send */
179static inline void inhibit(void)
180{
181 clock_lo();
182 data_hi();
183}
82 184
83#endif 185#endif
diff --git a/protocol/ps2_busywait.c b/protocol/ps2_busywait.c
new file mode 100644
index 000000000..05dd7b27e
--- /dev/null
+++ b/protocol/ps2_busywait.c
@@ -0,0 +1,185 @@
1/*
2Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com>
3
4This software is licensed with a Modified BSD License.
5All of this is supposed to be Free Software, Open Source, DFSG-free,
6GPL-compatible, and OK to use in both free and proprietary applications.
7Additions and corrections to this file are welcome.
8
9
10Redistribution and use in source and binary forms, with or without
11modification, are permitted provided that the following conditions are met:
12
13* Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16* Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in
18 the documentation and/or other materials provided with the
19 distribution.
20
21* Neither the name of the copyright holders nor the names of
22 contributors may be used to endorse or promote products derived
23 from this software without specific prior written permission.
24
25THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35POSSIBILITY OF SUCH DAMAGE.
36*/
37
38/*
39 * PS/2 protocol busywait version
40 */
41
42#include <stdbool.h>
43#include <util/delay.h>
44#include "ps2.h"
45#include "debug.h"
46
47
48#define WAIT(stat, us, err) do { \
49 if (!wait_##stat(us)) { \
50 ps2_error = err; \
51 goto ERROR; \
52 } \
53} while (0)
54
55
56uint8_t ps2_error = PS2_ERR_NONE;
57
58
59void ps2_host_init(void)
60{
61 // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
62 _delay_ms(2500);
63
64 inhibit();
65}
66
67uint8_t ps2_host_send(uint8_t data)
68{
69 bool parity = true;
70 ps2_error = PS2_ERR_NONE;
71
72 /* terminate a transmission if we have */
73 inhibit();
74 _delay_us(100); // 100us [4]p.13, [5]p.50
75
76 /* 'Request to Send' and Start bit */
77 data_lo();
78 clock_hi();
79 WAIT(clock_lo, 10000, 10); // 10ms [5]p.50
80
81 /* Data bit */
82 for (uint8_t i = 0; i < 8; i++) {
83 _delay_us(15);
84 if (data&(1<<i)) {
85 parity = !parity;
86 data_hi();
87 } else {
88 data_lo();
89 }
90 WAIT(clock_hi, 50, 2);
91 WAIT(clock_lo, 50, 3);
92 }
93
94 /* Parity bit */
95 _delay_us(15);
96 if (parity) { data_hi(); } else { data_lo(); }
97 WAIT(clock_hi, 50, 4);
98 WAIT(clock_lo, 50, 5);
99
100 /* Stop bit */
101 _delay_us(15);
102 data_hi();
103
104 /* Ack */
105 WAIT(data_lo, 50, 6);
106 WAIT(clock_lo, 50, 7);
107
108 /* wait for idle state */
109 WAIT(clock_hi, 50, 8);
110 WAIT(data_hi, 50, 9);
111
112 inhibit();
113 return ps2_host_recv_response();
114ERROR:
115 inhibit();
116 return 0;
117}
118
119/* receive data when host want else inhibit communication */
120uint8_t ps2_host_recv_response(void)
121{
122 // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
123 // 250 * 100us(wait for start bit in ps2_host_recv)
124 uint8_t data = 0;
125 uint8_t try = 250;
126 do {
127 data = ps2_host_recv();
128 } while (try-- && ps2_error);
129 return data;
130}
131
132/* called after start bit comes */
133uint8_t ps2_host_recv(void)
134{
135 uint8_t data = 0;
136 bool parity = true;
137 ps2_error = PS2_ERR_NONE;
138
139 /* release lines(idle state) */
140 idle();
141
142 /* start bit [1] */
143 WAIT(clock_lo, 100, 1); // TODO: this is enough?
144 WAIT(data_lo, 1, 2);
145 WAIT(clock_hi, 50, 3);
146
147 /* data [2-9] */
148 for (uint8_t i = 0; i < 8; i++) {
149 WAIT(clock_lo, 50, 4);
150 if (data_in()) {
151 parity = !parity;
152 data |= (1<<i);
153 }
154 WAIT(clock_hi, 50, 5);
155 }
156
157 /* parity [10] */
158 WAIT(clock_lo, 50, 6);
159 if (data_in() != parity) {
160 ps2_error = PS2_ERR_PARITY;
161 goto ERROR;
162 }
163 WAIT(clock_hi, 50, 7);
164
165 /* stop bit [11] */
166 WAIT(clock_lo, 50, 8);
167 WAIT(data_hi, 1, 9);
168 WAIT(clock_hi, 50, 10);
169
170 inhibit();
171 return data;
172ERROR:
173 if (ps2_error > PS2_ERR_STARTBIT3) {
174 xprintf("x%02X\n", ps2_error);
175 }
176 inhibit();
177 return 0;
178}
179
180/* send LED state to keyboard */
181void ps2_host_set_led(uint8_t led)
182{
183 ps2_host_send(0xED);
184 ps2_host_send(led);
185}
diff --git a/protocol/ps2.c b/protocol/ps2_interrupt.c
index e5873a9bf..259d25400 100644
--- a/protocol/ps2.c
+++ b/protocol/ps2_interrupt.c
@@ -35,47 +35,15 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35POSSIBILITY OF SUCH DAMAGE. 35POSSIBILITY OF SUCH DAMAGE.
36*/ 36*/
37 37
38/*
39 * PS/2 protocol Pin interrupt version
40 */
41
38#include <stdbool.h> 42#include <stdbool.h>
39#include <avr/io.h>
40#include <avr/interrupt.h> 43#include <avr/interrupt.h>
41#include <util/delay.h> 44#include <util/delay.h>
42#include "ps2.h" 45#include "ps2.h"
43#include "debug.h" 46#include "print.h"
44
45
46#ifndef PS2_USE_INT
47static uint8_t recv_data(void);
48#endif
49static inline void clock_lo(void);
50static inline void clock_hi(void);
51static inline bool clock_in(void);
52static inline void data_lo(void);
53static inline void data_hi(void);
54static inline bool data_in(void);
55static inline uint16_t wait_clock_lo(uint16_t us);
56static inline uint16_t wait_clock_hi(uint16_t us);
57static inline uint16_t wait_data_lo(uint16_t us);
58static inline uint16_t wait_data_hi(uint16_t us);
59static inline void idle(void);
60static inline void inhibit(void);
61
62
63/*
64Primitive PS/2 Library for AVR
65==============================
66Host side is only supported now.
67
68
69I/O control
70-----------
71High state is asserted by input with pull up.
72
73
74PS/2 References
75---------------
76http://www.computer-engineering.org/ps2protocol/
77http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
78*/
79 47
80 48
81#define WAIT(stat, us, err) do { \ 49#define WAIT(stat, us, err) do { \
@@ -89,35 +57,38 @@ http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
89uint8_t ps2_error = PS2_ERR_NONE; 57uint8_t ps2_error = PS2_ERR_NONE;
90 58
91 59
60static inline uint8_t pbuf_dequeue(void);
61static inline void pbuf_enqueue(uint8_t data);
62static inline bool pbuf_has_data(void);
63static inline void pbuf_clear(void);
64
65
92void ps2_host_init(void) 66void ps2_host_init(void)
93{ 67{
94#ifdef PS2_USE_INT 68 idle();
95 PS2_INT_INIT(); 69 PS2_INT_INIT();
96 PS2_INT_ON(); 70 PS2_INT_ON();
97 idle(); 71 // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
98#else 72 //_delay_ms(2500);
99 inhibit();
100#endif
101} 73}
102 74
103// TODO: send using interrupt if available
104uint8_t ps2_host_send(uint8_t data) 75uint8_t ps2_host_send(uint8_t data)
105{ 76{
106 uint8_t res = 0;
107 bool parity = true; 77 bool parity = true;
108 ps2_error = PS2_ERR_NONE; 78 ps2_error = PS2_ERR_NONE;
109#ifdef PS2_USE_INT 79
110 PS2_INT_OFF(); 80 PS2_INT_OFF();
111#endif 81
112 /* terminate a transmission if we have */ 82 /* terminate a transmission if we have */
113 inhibit(); 83 inhibit();
114 _delay_us(200); // at least 100us 84 _delay_us(100); // 100us [4]p.13, [5]p.50
115 85
116 /* start bit [1] */ 86 /* 'Request to Send' and Start bit */
117 data_lo(); 87 data_lo();
118 clock_hi(); 88 clock_hi();
119 WAIT(clock_lo, 20000, 10); // may take 15ms at most until device starts clocking 89 WAIT(clock_lo, 10000, 10); // 10ms [5]p.50
120 /* data [2-9] */ 90
91 /* Data bit[2-9] */
121 for (uint8_t i = 0; i < 8; i++) { 92 for (uint8_t i = 0; i < 8; i++) {
122 _delay_us(15); 93 _delay_us(15);
123 if (data&(1<<i)) { 94 if (data&(1<<i)) {
@@ -129,15 +100,18 @@ uint8_t ps2_host_send(uint8_t data)
129 WAIT(clock_hi, 50, 2); 100 WAIT(clock_hi, 50, 2);
130 WAIT(clock_lo, 50, 3); 101 WAIT(clock_lo, 50, 3);
131 } 102 }
132 /* parity [10] */ 103
104 /* Parity bit */
133 _delay_us(15); 105 _delay_us(15);
134 if (parity) { data_hi(); } else { data_lo(); } 106 if (parity) { data_hi(); } else { data_lo(); }
135 WAIT(clock_hi, 50, 4); 107 WAIT(clock_hi, 50, 4);
136 WAIT(clock_lo, 50, 5); 108 WAIT(clock_lo, 50, 5);
137 /* stop bit [11] */ 109
110 /* Stop bit */
138 _delay_us(15); 111 _delay_us(15);
139 data_hi(); 112 data_hi();
140 /* ack [12] */ 113
114 /* Ack */
141 WAIT(data_lo, 50, 6); 115 WAIT(data_lo, 50, 6);
142 WAIT(clock_lo, 50, 7); 116 WAIT(clock_lo, 50, 7);
143 117
@@ -145,116 +119,35 @@ uint8_t ps2_host_send(uint8_t data)
145 WAIT(clock_hi, 50, 8); 119 WAIT(clock_hi, 50, 8);
146 WAIT(data_hi, 50, 9); 120 WAIT(data_hi, 50, 9);
147 121
148#ifdef PS2_USE_INT 122 idle();
149 PS2_INT_ON(); 123 PS2_INT_ON();
150#endif 124 return ps2_host_recv_response();
151 res = ps2_host_recv_response();
152ERROR: 125ERROR:
153#ifdef PS2_USE_INT
154 PS2_INT_ON();
155 idle(); 126 idle();
156#else 127 PS2_INT_ON();
157 inhibit(); 128 return 0;
158#endif
159 return res;
160} 129}
161 130
162#ifndef PS2_USE_INT
163/* receive data when host want else inhibit communication */
164uint8_t ps2_host_recv_response(void) 131uint8_t ps2_host_recv_response(void)
165{ 132{
166 uint8_t data = 0; 133 // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
167 134 uint8_t retry = 25;
168#ifdef PS2_USE_INT 135 while (retry-- && !pbuf_has_data()) {
169 PS2_INT_OFF(); 136 _delay_ms(1);
170#endif
171 /* terminate a transmission if we have */
172 inhibit();
173 _delay_us(100);
174
175 /* release lines(idle state) */
176 idle();
177
178 /* wait start bit */
179 wait_clock_lo(25000); // command response may take 20 ms at most
180 data = recv_data();
181
182 inhibit();
183 return data;
184}
185#endif
186
187#ifndef PS2_USE_INT
188uint8_t ps2_host_recv(void)
189{
190 return ps2_host_recv_response();
191}
192#else
193/* ring buffer to store ps/2 key data */
194#define PBUF_SIZE 32
195static uint8_t pbuf[PBUF_SIZE];
196static uint8_t pbuf_head = 0;
197static uint8_t pbuf_tail = 0;
198static inline void pbuf_enqueue(uint8_t data)
199{
200 uint8_t sreg = SREG;
201 cli();
202 uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
203 if (next != pbuf_tail) {
204 pbuf[pbuf_head] = data;
205 pbuf_head = next;
206 } else {
207 debug("pbuf: full\n");
208 }
209 SREG = sreg;
210}
211static inline uint8_t pbuf_dequeue(void)
212{
213 uint8_t val = 0;
214
215 uint8_t sreg = SREG;
216 cli();
217 if (pbuf_head != pbuf_tail) {
218 val = pbuf[pbuf_tail];
219 pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
220 } 137 }
221 SREG = sreg; 138 return pbuf_dequeue();
222
223 return val;
224}
225static inline bool pbuf_has_data(void)
226{
227 uint8_t sreg = SREG;
228 cli();
229 bool has_data = (pbuf_head != pbuf_tail);
230 SREG = sreg;
231 return has_data;
232}
233static inline void pbuf_clear(void)
234{
235 uint8_t sreg = SREG;
236 cli();
237 pbuf_head = pbuf_tail = 0;
238 SREG = sreg;
239} 139}
240 140
241/* get data received by interrupt */ 141/* get data received by interrupt */
242uint8_t ps2_host_recv(void) 142uint8_t ps2_host_recv(void)
243{ 143{
244 if (ps2_error) { 144 if (pbuf_has_data()) {
245 print("x");
246 phex(ps2_error);
247 ps2_host_send(0xFE); // request to resend
248 ps2_error = PS2_ERR_NONE; 145 ps2_error = PS2_ERR_NONE;
146 return pbuf_dequeue();
147 } else {
148 ps2_error = PS2_ERR_NODATA;
149 return 0;
249 } 150 }
250 idle();
251 return pbuf_dequeue();
252}
253
254uint8_t ps2_host_recv_response(void)
255{
256 while (!pbuf_has_data()) ;
257 return pbuf_dequeue();
258} 151}
259 152
260ISR(PS2_INT_VECT) 153ISR(PS2_INT_VECT)
@@ -309,7 +202,6 @@ ISR(PS2_INT_VECT)
309 if (!data_in()) 202 if (!data_in())
310 goto ERROR; 203 goto ERROR;
311 pbuf_enqueue(data); 204 pbuf_enqueue(data);
312//phex(data);
313 goto DONE; 205 goto DONE;
314 break; 206 break;
315 default: 207 default:
@@ -317,7 +209,6 @@ ISR(PS2_INT_VECT)
317 } 209 }
318 goto RETURN; 210 goto RETURN;
319ERROR: 211ERROR:
320 inhibit();
321 ps2_error = state; 212 ps2_error = state;
322DONE: 213DONE:
323 state = INIT; 214 state = INIT;
@@ -326,8 +217,6 @@ DONE:
326RETURN: 217RETURN:
327 return; 218 return;
328} 219}
329#endif
330
331 220
332/* send LED state to keyboard */ 221/* send LED state to keyboard */
333void ps2_host_set_led(uint8_t led) 222void ps2_host_set_led(uint8_t led)
@@ -337,116 +226,53 @@ void ps2_host_set_led(uint8_t led)
337} 226}
338 227
339 228
340#ifndef PS2_USE_INT 229/*--------------------------------------------------------------------
341/* called after start bit comes */ 230 * Ring buffer to store scan codes from keyboard
342static uint8_t recv_data(void) 231 *------------------------------------------------------------------*/
232#define PBUF_SIZE 32
233static uint8_t pbuf[PBUF_SIZE];
234static uint8_t pbuf_head = 0;
235static uint8_t pbuf_tail = 0;
236static inline void pbuf_enqueue(uint8_t data)
343{ 237{
344 uint8_t data = 0; 238 uint8_t sreg = SREG;
345 bool parity = true; 239 cli();
346 ps2_error = PS2_ERR_NONE; 240 uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
347 241 if (next != pbuf_tail) {
348 /* start bit [1] */ 242 pbuf[pbuf_head] = data;
349 WAIT(clock_lo, 1, 1); 243 pbuf_head = next;
350 WAIT(data_lo, 1, 2); 244 } else {
351 WAIT(clock_hi, 50, 3); 245 print("pbuf: full\n");
352
353 /* data [2-9] */
354 for (uint8_t i = 0; i < 8; i++) {
355 WAIT(clock_lo, 50, 4);
356 if (data_in()) {
357 parity = !parity;
358 data |= (1<<i);
359 }
360 WAIT(clock_hi, 50, 5);
361 }
362
363 /* parity [10] */
364 WAIT(clock_lo, 50, 6);
365 if (data_in() != parity) {
366 ps2_error = PS2_ERR_PARITY;
367 goto ERROR;
368 } 246 }
369 WAIT(clock_hi, 50, 7); 247 SREG = sreg;
370
371 /* stop bit [11] */
372 WAIT(clock_lo, 50, 8);
373 WAIT(data_hi, 1, 9);
374 WAIT(clock_hi, 50, 10);
375
376 return data;
377ERROR:
378 return 0;
379}
380#endif
381
382static inline void clock_lo()
383{
384 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
385 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
386}
387static inline void clock_hi()
388{
389 /* input with pull up */
390 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
391 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
392}
393static inline bool clock_in()
394{
395 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
396 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
397 _delay_us(1);
398 return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
399}
400static inline void data_lo()
401{
402 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
403 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
404}
405static inline void data_hi()
406{
407 /* input with pull up */
408 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
409 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
410} 248}
411static inline bool data_in() 249static inline uint8_t pbuf_dequeue(void)
412{ 250{
413 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT); 251 uint8_t val = 0;
414 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
415 _delay_us(1);
416 return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
417}
418 252
419static inline uint16_t wait_clock_lo(uint16_t us) 253 uint8_t sreg = SREG;
420{ 254 cli();
421 while (clock_in() && us) { asm(""); _delay_us(1); us--; } 255 if (pbuf_head != pbuf_tail) {
422 return us; 256 val = pbuf[pbuf_tail];
423} 257 pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
424static inline uint16_t wait_clock_hi(uint16_t us) 258 }
425{ 259 SREG = sreg;
426 while (!clock_in() && us) { asm(""); _delay_us(1); us--; } 260
427 return us; 261 return val;
428}
429static inline uint16_t wait_data_lo(uint16_t us)
430{
431 while (data_in() && us) { asm(""); _delay_us(1); us--; }
432 return us;
433} 262}
434static inline uint16_t wait_data_hi(uint16_t us) 263static inline bool pbuf_has_data(void)
435{ 264{
436 while (!data_in() && us) { asm(""); _delay_us(1); us--; } 265 uint8_t sreg = SREG;
437 return us; 266 cli();
267 bool has_data = (pbuf_head != pbuf_tail);
268 SREG = sreg;
269 return has_data;
438} 270}
439 271static inline void pbuf_clear(void)
440/* idle state that device can send */
441static inline void idle(void)
442{ 272{
443 clock_hi(); 273 uint8_t sreg = SREG;
444 data_hi(); 274 cli();
275 pbuf_head = pbuf_tail = 0;
276 SREG = sreg;
445} 277}
446 278
447/* inhibit device to send */
448static inline void inhibit(void)
449{
450 clock_lo();
451 data_hi();
452}
diff --git a/protocol/ps2_usart.c b/protocol/ps2_usart.c
index 40c46c497..c2d9d0a20 100644
--- a/protocol/ps2_usart.c
+++ b/protocol/ps2_usart.c
@@ -36,32 +36,14 @@ POSSIBILITY OF SUCH DAMAGE.
36*/ 36*/
37 37
38/* 38/*
39Primitive PS/2 Library for AVR 39 * PS/2 protocol USART version
40============================== 40 */
41Host side is only supported now.
42Synchronous USART is used to receive data by hardware process
43rather than interrupt. During V-USB interrupt runs, CLOCK interrupt
44cannot interpose. In the result it is prone to lost CLOCK edge.
45 41
46
47I/O control
48-----------
49High state is asserted by internal pull-up.
50If you have a signaling problem, you may need to have
51external pull-up resisters on CLOCK and DATA line.
52
53
54PS/2 References
55---------------
56http://www.computer-engineering.org/ps2protocol/
57http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
58*/
59#include <stdbool.h> 42#include <stdbool.h>
60#include <avr/io.h>
61#include <avr/interrupt.h> 43#include <avr/interrupt.h>
62#include <util/delay.h> 44#include <util/delay.h>
63#include "ps2.h" 45#include "ps2.h"
64#include "debug.h" 46#include "print.h"
65 47
66 48
67#define WAIT(stat, us, err) do { \ 49#define WAIT(stat, us, err) do { \
@@ -75,18 +57,6 @@ http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
75uint8_t ps2_error = PS2_ERR_NONE; 57uint8_t ps2_error = PS2_ERR_NONE;
76 58
77 59
78static inline void clock_lo(void);
79static inline void clock_hi(void);
80static inline bool clock_in(void);
81static inline void data_lo(void);
82static inline void data_hi(void);
83static inline bool data_in(void);
84static inline uint16_t wait_clock_lo(uint16_t us);
85static inline uint16_t wait_clock_hi(uint16_t us);
86static inline uint16_t wait_data_lo(uint16_t us);
87static inline uint16_t wait_data_hi(uint16_t us);
88static inline void idle(void);
89static inline void inhibit(void);
90static inline uint8_t pbuf_dequeue(void); 60static inline uint8_t pbuf_dequeue(void);
91static inline void pbuf_enqueue(uint8_t data); 61static inline void pbuf_enqueue(uint8_t data);
92static inline bool pbuf_has_data(void); 62static inline bool pbuf_has_data(void);
@@ -95,14 +65,15 @@ static inline void pbuf_clear(void);
95 65
96void ps2_host_init(void) 66void ps2_host_init(void)
97{ 67{
98 idle(); 68 idle(); // without this many USART errors occur when cable is disconnected
99 PS2_USART_INIT(); 69 PS2_USART_INIT();
100 PS2_USART_RX_INT_ON(); 70 PS2_USART_RX_INT_ON();
71 // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
72 //_delay_ms(2500);
101} 73}
102 74
103uint8_t ps2_host_send(uint8_t data) 75uint8_t ps2_host_send(uint8_t data)
104{ 76{
105 uint8_t res = 0;
106 bool parity = true; 77 bool parity = true;
107 ps2_error = PS2_ERR_NONE; 78 ps2_error = PS2_ERR_NONE;
108 79
@@ -110,13 +81,14 @@ uint8_t ps2_host_send(uint8_t data)
110 81
111 /* terminate a transmission if we have */ 82 /* terminate a transmission if we have */
112 inhibit(); 83 inhibit();
113 _delay_us(100); 84 _delay_us(100); // [4]p.13
114 85
115 /* start bit [1] */ 86 /* 'Request to Send' and Start bit */
116 data_lo(); 87 data_lo();
117 clock_hi(); 88 clock_hi();
118 WAIT(clock_lo, 15000, 1); 89 WAIT(clock_lo, 10000, 10); // 10ms [5]p.50
119 /* data [2-9] */ 90
91 /* Data bit[2-9] */
120 for (uint8_t i = 0; i < 8; i++) { 92 for (uint8_t i = 0; i < 8; i++) {
121 _delay_us(15); 93 _delay_us(15);
122 if (data&(1<<i)) { 94 if (data&(1<<i)) {
@@ -128,15 +100,18 @@ uint8_t ps2_host_send(uint8_t data)
128 WAIT(clock_hi, 50, 2); 100 WAIT(clock_hi, 50, 2);
129 WAIT(clock_lo, 50, 3); 101 WAIT(clock_lo, 50, 3);
130 } 102 }
131 /* parity [10] */ 103
104 /* Parity bit */
132 _delay_us(15); 105 _delay_us(15);
133 if (parity) { data_hi(); } else { data_lo(); } 106 if (parity) { data_hi(); } else { data_lo(); }
134 WAIT(clock_hi, 50, 4); 107 WAIT(clock_hi, 50, 4);
135 WAIT(clock_lo, 50, 5); 108 WAIT(clock_lo, 50, 5);
136 /* stop bit [11] */ 109
110 /* Stop bit */
137 _delay_us(15); 111 _delay_us(15);
138 data_hi(); 112 data_hi();
139 /* ack [12] */ 113
114 /* Ack */
140 WAIT(data_lo, 50, 6); 115 WAIT(data_lo, 50, 6);
141 WAIT(clock_lo, 50, 7); 116 WAIT(clock_lo, 50, 7);
142 117
@@ -144,127 +119,55 @@ uint8_t ps2_host_send(uint8_t data)
144 WAIT(clock_hi, 50, 8); 119 WAIT(clock_hi, 50, 8);
145 WAIT(data_hi, 50, 9); 120 WAIT(data_hi, 50, 9);
146 121
122 idle();
147 PS2_USART_INIT(); 123 PS2_USART_INIT();
148 PS2_USART_RX_INT_ON(); 124 PS2_USART_RX_INT_ON();
149 res = ps2_host_recv_response(); 125 return ps2_host_recv_response();
150ERROR: 126ERROR:
151 idle(); 127 idle();
152 PS2_USART_INIT(); 128 PS2_USART_INIT();
153 PS2_USART_RX_INT_ON(); 129 PS2_USART_RX_INT_ON();
154 return res; 130 return 0;
155} 131}
156 132
157// Do polling data from keyboard to get response to last command.
158uint8_t ps2_host_recv_response(void) 133uint8_t ps2_host_recv_response(void)
159{ 134{
160 while (!pbuf_has_data()) { 135 // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
161 _delay_ms(1); // without this delay it seems to fall into deadlock 136 uint8_t retry = 25;
137 while (retry-- && !pbuf_has_data()) {
138 _delay_ms(1);
162 } 139 }
163 return pbuf_dequeue(); 140 return pbuf_dequeue();
164} 141}
165 142
166uint8_t ps2_host_recv(void) 143uint8_t ps2_host_recv(void)
167{ 144{
168 return pbuf_dequeue(); 145 if (pbuf_has_data()) {
146 ps2_error = PS2_ERR_NONE;
147 return pbuf_dequeue();
148 } else {
149 ps2_error = PS2_ERR_NODATA;
150 return 0;
151 }
169} 152}
170 153
171ISR(PS2_USART_RX_VECT) 154ISR(PS2_USART_RX_VECT)
172{ 155{
173 uint8_t error = PS2_USART_ERROR; 156 // TODO: request RESEND when error occurs?
157 uint8_t error = PS2_USART_ERROR; // USART error should be read before data
174 uint8_t data = PS2_USART_RX_DATA; 158 uint8_t data = PS2_USART_RX_DATA;
175 if (!error) { 159 if (!error) {
176 pbuf_enqueue(data); 160 pbuf_enqueue(data);
161 } else {
162 xprintf("PS2 USART error: %02X data: %02X\n", error, data);
177 } 163 }
178} 164}
179 165
180/* send LED state to keyboard */ 166/* send LED state to keyboard */
181void ps2_host_set_led(uint8_t led) 167void ps2_host_set_led(uint8_t led)
182{ 168{
183 // send 0xED then keyboard keeps waiting for next LED data 169 ps2_host_send(0xED);
184 // and keyboard does not send any scan codes during waiting. 170 ps2_host_send(led);
185 // If fail to send LED data keyboard looks like being freezed.
186 uint8_t retry = 3;
187 while (retry-- && ps2_host_send(PS2_SET_LED) != PS2_ACK)
188 ;
189 retry = 3;
190 while (retry-- && ps2_host_send(led) != PS2_ACK)
191 ;
192}
193
194
195/*--------------------------------------------------------------------
196 * static functions
197 *------------------------------------------------------------------*/
198static inline void clock_lo()
199{
200 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
201 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
202}
203static inline void clock_hi()
204{
205 /* input with pull up */
206 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
207 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
208}
209static inline bool clock_in()
210{
211 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
212 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
213 _delay_us(1);
214 return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
215}
216static inline void data_lo()
217{
218 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
219 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
220}
221static inline void data_hi()
222{
223 /* input with pull up */
224 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
225 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
226}
227static inline bool data_in()
228{
229 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
230 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
231 _delay_us(1);
232 return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
233}
234
235static inline uint16_t wait_clock_lo(uint16_t us)
236{
237 while (clock_in() && us) { asm(""); _delay_us(1); us--; }
238 return us;
239}
240static inline uint16_t wait_clock_hi(uint16_t us)
241{
242 while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
243 return us;
244}
245static inline uint16_t wait_data_lo(uint16_t us)
246{
247 while (data_in() && us) { asm(""); _delay_us(1); us--; }
248 return us;
249}
250static inline uint16_t wait_data_hi(uint16_t us)
251{
252 while (!data_in() && us) { asm(""); _delay_us(1); us--; }
253 return us;
254}
255
256/* idle state that device can send */
257static inline void idle(void)
258{
259 clock_hi();
260 data_hi();
261}
262
263/* inhibit device to send */
264static inline void inhibit(void)
265{
266 clock_lo();
267 data_hi();
268} 171}
269 172
270 173
@@ -284,11 +187,10 @@ static inline void pbuf_enqueue(uint8_t data)
284 pbuf[pbuf_head] = data; 187 pbuf[pbuf_head] = data;
285 pbuf_head = next; 188 pbuf_head = next;
286 } else { 189 } else {
287 debug("pbuf: full\n"); 190 print("pbuf: full\n");
288 } 191 }
289 SREG = sreg; 192 SREG = sreg;
290} 193}
291
292static inline uint8_t pbuf_dequeue(void) 194static inline uint8_t pbuf_dequeue(void)
293{ 195{
294 uint8_t val = 0; 196 uint8_t val = 0;