]> git.donarmstrong.com Git - tmk_firmware.git/commitdiff
Port ps2_usb to mbed
authortmk <nobody@nowhere>
Thu, 19 Jun 2014 07:13:35 +0000 (16:13 +0900)
committertmk <nobody@nowhere>
Wed, 30 Jul 2014 05:07:43 +0000 (14:07 +0900)
15 files changed:
common/progmem.h
converter/ps2_usb/Makefile.mbed [new file with mode: 0644]
converter/ps2_usb/config_mbed.h [new file with mode: 0644]
converter/ps2_usb/keymap_common.c
converter/ps2_usb/keymap_common.h
converter/ps2_usb/main.cpp [new file with mode: 0644]
converter/ps2_usb/matrix.c
keyboard/mbed_onekey/Makefile
protocol.mk
protocol/ps2.h
protocol/ps2_busywait.c
protocol/ps2_io.h [new file with mode: 0644]
protocol/ps2_io_avr.c [new file with mode: 0644]
protocol/ps2_io_mbed.c [new file with mode: 0644]
tool/mbed/common.mk

index 09aeb8b7c428bab28e77f1dff3bc27fc91cfefd2..199b1bedfea9f7018903420ddca146da38482a60 100644 (file)
@@ -6,6 +6,7 @@
 #elif defined(__arm__)
 #   define PROGMEM
 #   define pgm_read_byte(p)     *(p)
+#   define pgm_read_word(p)     *(p)
 #endif
 
 #endif
diff --git a/converter/ps2_usb/Makefile.mbed b/converter/ps2_usb/Makefile.mbed
new file mode 100644 (file)
index 0000000..631f270
--- /dev/null
@@ -0,0 +1,44 @@
+PROJECT = ps2_usb
+
+TMK_DIR = ../..
+MBED_DIR = $(TMK_DIR)/mbed-sdk
+
+#VPATH += $(MBED_DIR):$(TMK_DIR)
+vpath %.s .:$(MBED_DIR):$(TMK_DIR)
+vpath %.c .:$(MBED_DIR):$(TMK_DIR)
+vpath %.cpp .:$(MBED_DIR):$(TMK_DIR)
+
+OBJDIR = ./build
+
+OBJECTS = \
+       $(OBJDIR)/protocol/ps2_busywait.o \
+       $(OBJDIR)/protocol/ps2_io_mbed.o \
+       $(OBJDIR)/./keymap_common.o \
+       $(OBJDIR)/./matrix.o \
+       $(OBJDIR)/./led.o \
+       $(OBJDIR)/./main.o
+
+ifdef KEYMAP
+    OBJECTS := $(OBJDIR)/keymap_$(KEYMAP).o $(OBJECTS)
+else
+    OBJECTS := $(OBJDIR)/keymap_plain.o $(OBJECTS)
+endif
+
+CONFIG_H = config_mbed.h
+
+SYS_OBJECTS = 
+
+INCLUDE_PATHS = -I.
+
+LIBRARY_PATHS = 
+LIBRARIES = 
+
+# Build Options
+#   Comment out to disable
+#BOOTMAGIC_ENABLE = yes
+MOUSEKEY_ENABLE = yes
+
+
+include $(TMK_DIR)/tool/mbed/mbed.mk
+include $(TMK_DIR)/tool/mbed/common.mk
+include $(TMK_DIR)/tool/mbed/gcc.mk
diff --git a/converter/ps2_usb/config_mbed.h b/converter/ps2_usb/config_mbed.h
new file mode 100644 (file)
index 0000000..5819763
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+Copyright 2012 Jun Wako <wakojun@gmail.com>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef CONFIG_MBED_H
+#define CONFIG_MBED_H
+
+
+#if 0
+// duplicated name against mbed USBDeivce
+#define VENDOR_ID       0xFEED
+#define PRODUCT_ID      0x6512
+#endif
+#define DEVICE_VER      0x0001
+#define MANUFACTURER    t.m.k.
+#define PRODUCT         PS/2 keyboard converter
+#define DESCRIPTION     convert PS/2 keyboard to USB
+
+
+/* matrix size */
+#define MATRIX_ROWS 32  // keycode bit: 3-0
+#define MATRIX_COLS 8   // keycode bit: 6-4
+
+
+/* key combination for command */
+#define IS_COMMAND() ( \
+    keyboard_report->mods == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT)) || \
+    keyboard_report->mods == (MOD_BIT(KC_LCTRL) | MOD_BIT(KC_RSHIFT)) \
+)
+
+
+/*
+ * PS/2 Busywait
+ */
+#ifdef PS2_USE_BUSYWAIT
+#   define PS2_CLOCK_PORT  PORTD
+#   define PS2_CLOCK_PIN   PIND
+#   define PS2_CLOCK_DDR   DDRD
+#   define PS2_CLOCK_BIT   5
+#   define PS2_DATA_PORT   PORTD
+#   define PS2_DATA_PIN    PIND
+#   define PS2_DATA_DDR    DDRD
+#   define PS2_DATA_BIT    2
+#endif
+
+
+#endif
index 241d2e33b1607d99b32a5efe3606927642366606..e344fb41670f7bfca29f03337d47ad7695c815ea 100644 (file)
@@ -15,10 +15,11 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 #include "keymap_common.h"
+#include "progmem.h"
 
 
 /* translates key to keycode */
-uint8_t keymap_key_to_keycode(uint8_t layer, key_t key)
+uint8_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
 {
     return pgm_read_byte(&keymaps[(layer)][(key.row)][(key.col)]);
 }
index 216a8dc020c317582517b80bd70bce36ef9c631b..d783e01dedd8a28a38d8d12dbed2f1500072ce93 100644 (file)
@@ -19,7 +19,6 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <stdint.h>
 #include <stdbool.h>
-#include <avr/pgmspace.h>
 #include "keycode.h"
 #include "action.h"
 #include "action_macro.h"
diff --git a/converter/ps2_usb/main.cpp b/converter/ps2_usb/main.cpp
new file mode 100644 (file)
index 0000000..860af14
--- /dev/null
@@ -0,0 +1,46 @@
+#include "mbed.h"
+#include "debug.h"
+#include "timer.h"
+#include "action.h"
+#include "keycode.h"
+#include "host.h"
+#include "host_driver.h"
+#include "mbed_driver.h"
+
+// Button and LEDs of LPC11U35 board
+DigitalIn isp(P0_1);            // ISP button
+DigitalOut led_red(P0_20);
+DigitalOut led_green(P0_21);
+
+
+int main(void) {
+    isp.mode(PullUp);
+    led_red = 1;
+    led_green = 0;
+
+    timer_init();
+    host_set_driver(&mbed_driver);
+    keyboard_init();
+
+    //debug_enable = true;
+    xprintf("mbed_onekey ver.eee:\r\n");
+
+
+    bool last_isp = isp;
+    while (1) {
+        keyboard_task();
+
+        //led_green = !led_green;
+        if (last_isp == isp) continue;
+        last_isp = isp;
+        if (last_isp == 0) {
+            led_red = 0;    // on
+            dprintf("timer: %i\r\n", timer_read());
+            //register_code(KC_A);
+        } else {
+            led_red = 1;    // off
+            //unregister_code(KC_A);
+        }
+    }
+}
index 45344c0f759c20aa3491bd7972bce228efddbe6b..45cf2a4a90fa87e2305bf5622e26a4d2f6bba5b0 100644 (file)
@@ -17,8 +17,6 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <stdint.h>
 #include <stdbool.h>
-#include <avr/io.h>
-#include <util/delay.h>
 #include "action.h"
 #include "print.h"
 #include "util.h"
@@ -189,6 +187,7 @@ uint8_t matrix_scan(void)
     }
 
     uint8_t code = ps2_host_recv();
+    if (code) xprintf("%i\r\n", code);
     if (!ps2_error) {
         switch (state) {
             case INIT:
index d0d1148ce231ab335ef5021fab4dfa999878843b..2f7399ba02f23d491300e31fee6ba0084189881a 100644 (file)
@@ -1,7 +1,7 @@
 PROJECT = mbed_onekey
 
 TMK_DIR = ../..
-MBED_DIR = ./mbed-sdk
+MBED_DIR = $(TMK_DIR)/mbed-sdk
 
 #VPATH += $(MBED_DIR):$(TMK_DIR)
 vpath %.s .:$(MBED_DIR):$(TMK_DIR)
index 7f561e62d60dc0a5dc7c49ca0cb9a9fee25d781e..ca435ba43ca1acc2b7c3bb67705ea9eaccd851e0 100644 (file)
@@ -9,6 +9,7 @@ endif
 
 ifdef PS2_USE_BUSYWAIT
     SRC += protocol/ps2_busywait.c
+    SRC += protocol/ps2_io_avr.c
     OPT_DEFS += -DPS2_USE_BUSYWAIT
 endif
 
index 483eea7209a769d10790f19952728f974ab72ccf..acde679cf42352fc4c7750bfdbf33ae77c5b19d0 100644 (file)
@@ -39,8 +39,9 @@ POSSIBILITY OF SUCH DAMAGE.
 #define PS2_H
 
 #include <stdbool.h>
-#include <util/delay.h>
-#include <avr/io.h>
+#include "wait.h"
+#include "ps2_io.h"
+#include "print.h"
 
 /*
  * Primitive PS/2 Library for AVR
@@ -92,79 +93,27 @@ uint8_t ps2_host_recv(void);
 void ps2_host_set_led(uint8_t usb_led);
 
 
-/* Check port settings for clock and data line */
-#if !(defined(PS2_CLOCK_PORT) && \
-      defined(PS2_CLOCK_PIN) && \
-      defined(PS2_CLOCK_DDR) && \
-      defined(PS2_CLOCK_BIT))
-#   error "PS/2 clock port setting is required in config.h"
-#endif
-
-#if !(defined(PS2_DATA_PORT) && \
-      defined(PS2_DATA_PIN) && \
-      defined(PS2_DATA_DDR) && \
-      defined(PS2_DATA_BIT))
-#   error "PS/2 data port setting is required in config.h"
-#endif
-
 /*--------------------------------------------------------------------
  * static functions
  *------------------------------------------------------------------*/
-static inline void clock_lo(void)
-{
-    PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
-    PS2_CLOCK_DDR  |=  (1<<PS2_CLOCK_BIT);
-}
-static inline void clock_hi(void)
-{
-    /* input with pull up */
-    PS2_CLOCK_DDR  &= ~(1<<PS2_CLOCK_BIT);
-    PS2_CLOCK_PORT |=  (1<<PS2_CLOCK_BIT);
-}
-static inline bool clock_in(void)
-{
-    PS2_CLOCK_DDR  &= ~(1<<PS2_CLOCK_BIT);
-    PS2_CLOCK_PORT |=  (1<<PS2_CLOCK_BIT);
-    _delay_us(1);
-    return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
-}
-static inline void data_lo(void)
-{
-    PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
-    PS2_DATA_DDR  |=  (1<<PS2_DATA_BIT);
-}
-static inline void data_hi(void)
-{
-    /* input with pull up */
-    PS2_DATA_DDR  &= ~(1<<PS2_DATA_BIT);
-    PS2_DATA_PORT |=  (1<<PS2_DATA_BIT);
-}
-static inline bool data_in(void)
-{
-    PS2_DATA_DDR  &= ~(1<<PS2_DATA_BIT);
-    PS2_DATA_PORT |=  (1<<PS2_DATA_BIT);
-    _delay_us(1);
-    return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
-}
-
 static inline uint16_t wait_clock_lo(uint16_t us)
 {
-    while (clock_in()  && us) { asm(""); _delay_us(1); us--; }
+    while (clock_in()  && us) { asm(""); wait_us(1); us--; }
     return us;
 }
 static inline uint16_t wait_clock_hi(uint16_t us)
 {
-    while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
+    while (!clock_in() && us) { asm(""); wait_us(1); us--; }
     return us;
 }
 static inline uint16_t wait_data_lo(uint16_t us)
 {
-    while (data_in() && us)  { asm(""); _delay_us(1); us--; }
+    while (data_in() && us)  { asm(""); wait_us(1); us--; }
     return us;
 }
 static inline uint16_t wait_data_hi(uint16_t us)
 {
-    while (!data_in() && us)  { asm(""); _delay_us(1); us--; }
+    while (!data_in() && us)  { asm(""); wait_us(1); us--; }
     return us;
 }
 
index 05dd7b27e695762cb50e0befadc60395e6a1ca3f..a64933219ec96e1b48d4f30edf5eb60191306a31 100644 (file)
@@ -40,8 +40,9 @@ POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include <stdbool.h>
-#include <util/delay.h>
+#include "wait.h"
 #include "ps2.h"
+#include "ps2_io.h"
 #include "debug.h"
 
 
@@ -58,8 +59,11 @@ uint8_t ps2_error = PS2_ERR_NONE;
 
 void ps2_host_init(void)
 {
+    clock_init();
+    data_init();
+
     // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
-    _delay_ms(2500);
+    wait_ms(2500);
 
     inhibit();
 }
@@ -71,7 +75,7 @@ uint8_t ps2_host_send(uint8_t data)
 
     /* terminate a transmission if we have */
     inhibit();
-    _delay_us(100); // 100us [4]p.13, [5]p.50
+    wait_us(100); // 100us [4]p.13, [5]p.50
 
     /* 'Request to Send' and Start bit */
     data_lo();
@@ -80,7 +84,7 @@ uint8_t ps2_host_send(uint8_t data)
 
     /* Data bit */
     for (uint8_t i = 0; i < 8; i++) {
-        _delay_us(15);
+        wait_us(15);
         if (data&(1<<i)) {
             parity = !parity;
             data_hi();
@@ -92,13 +96,13 @@ uint8_t ps2_host_send(uint8_t data)
     }
 
     /* Parity bit */
-    _delay_us(15);
+    wait_us(15);
     if (parity) { data_hi(); } else { data_lo(); }
     WAIT(clock_hi, 50, 4);
     WAIT(clock_lo, 50, 5);
 
     /* Stop bit */
-    _delay_us(15);
+    wait_us(15);
     data_hi();
 
     /* Ack */
diff --git a/protocol/ps2_io.h b/protocol/ps2_io.h
new file mode 100644 (file)
index 0000000..a46a358
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef PS2_IO_H
+#define PS2_IO_H
+
+
+void clock_init(void);
+void clock_lo(void);
+void clock_hi(void);
+bool clock_in(void);
+
+void data_init(void);
+void data_lo(void);
+void data_hi(void);
+bool data_in(void);
+
+#endif
diff --git a/protocol/ps2_io_avr.c b/protocol/ps2_io_avr.c
new file mode 100644 (file)
index 0000000..be13d66
--- /dev/null
@@ -0,0 +1,74 @@
+#include <stdbool.h>
+#include <util/delay.h>
+
+/* Check port settings for clock and data line */
+#if !(defined(PS2_CLOCK_PORT) && \
+      defined(PS2_CLOCK_PIN) && \
+      defined(PS2_CLOCK_DDR) && \
+      defined(PS2_CLOCK_BIT))
+#   error "PS/2 clock port setting is required in config.h"
+#endif
+
+#if !(defined(PS2_DATA_PORT) && \
+      defined(PS2_DATA_PIN) && \
+      defined(PS2_DATA_DDR) && \
+      defined(PS2_DATA_BIT))
+#   error "PS/2 data port setting is required in config.h"
+#endif
+
+
+/*
+ * Clock
+ */
+void clock_init(void)
+{
+}
+
+void clock_lo(void)
+{
+    PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
+    PS2_CLOCK_DDR  |=  (1<<PS2_CLOCK_BIT);
+}
+
+void clock_hi(void)
+{
+    /* input with pull up */
+    PS2_CLOCK_DDR  &= ~(1<<PS2_CLOCK_BIT);
+    PS2_CLOCK_PORT |=  (1<<PS2_CLOCK_BIT);
+}
+
+bool clock_in(void)
+{
+    PS2_CLOCK_DDR  &= ~(1<<PS2_CLOCK_BIT);
+    PS2_CLOCK_PORT |=  (1<<PS2_CLOCK_BIT);
+    _delay_us(1);
+    return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
+}
+
+/*
+ * Data
+ */
+void data_init(void)
+{
+}
+
+void data_lo(void)
+{
+    PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
+    PS2_DATA_DDR  |=  (1<<PS2_DATA_BIT);
+}
+
+void data_hi(void)
+{
+    /* input with pull up */
+    PS2_DATA_DDR  &= ~(1<<PS2_DATA_BIT);
+    PS2_DATA_PORT |=  (1<<PS2_DATA_BIT);
+}
+
+bool data_in(void)
+{
+    PS2_DATA_DDR  &= ~(1<<PS2_DATA_BIT);
+    PS2_DATA_PORT |=  (1<<PS2_DATA_BIT);
+    _delay_us(1);
+    return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
+}
diff --git a/protocol/ps2_io_mbed.c b/protocol/ps2_io_mbed.c
new file mode 100644 (file)
index 0000000..83bdcef
--- /dev/null
@@ -0,0 +1,60 @@
+#include <stdbool.h>
+#include "ps2_io.h"
+#include "gpio_api.h"
+
+
+static gpio_t clock;
+static gpio_t data;
+
+/*
+ * Clock
+ */
+void clock_init(void)
+{
+    gpio_init(&clock, P0_9);
+    gpio_mode(&clock, OpenDrain|PullNone);
+}
+
+void clock_lo(void)
+{
+    gpio_dir(&clock, PIN_OUTPUT);
+    gpio_write(&clock, 0);
+}
+void clock_hi(void)
+{
+    gpio_dir(&clock, PIN_OUTPUT);
+    gpio_write(&clock, 1);
+}
+
+bool clock_in(void)
+{
+    gpio_dir(&clock, PIN_INPUT);
+    return gpio_read(&clock);
+}
+
+/*
+ * Data
+ */
+void data_init(void)
+{
+    gpio_init(&data, P0_8);
+    gpio_mode(&data, OpenDrain|PullNone);
+}
+
+void data_lo(void)
+{
+    gpio_dir(&data, PIN_OUTPUT);
+    gpio_write(&data, 0);
+}
+
+void data_hi(void)
+{
+    gpio_dir(&data, PIN_OUTPUT);
+    gpio_write(&data, 1);
+}
+
+bool data_in(void)
+{
+    gpio_dir(&data, PIN_INPUT);
+    return gpio_read(&data);
+}
index 1bd7d6edbee588039ed35055367165fd2b85c11b..93a927a3e852cfaa0f587c2820569649c9d9bbf3 100644 (file)
@@ -1,21 +1,21 @@
-COMMON_DIR = common
 OBJECTS += \
-       $(OBJDIR)/$(COMMON_DIR)/action.o \
-       $(OBJDIR)/$(COMMON_DIR)/action_tapping.o \
-       $(OBJDIR)/$(COMMON_DIR)/action_macro.o \
-       $(OBJDIR)/$(COMMON_DIR)/action_layer.o \
-       $(OBJDIR)/$(COMMON_DIR)/action_util.o \
-       $(OBJDIR)/$(COMMON_DIR)/host.o \
-       $(OBJDIR)/$(COMMON_DIR)/keymap.o \
-       $(OBJDIR)/$(COMMON_DIR)/keyboard.o \
-       $(OBJDIR)/$(COMMON_DIR)/util.o \
-       $(OBJDIR)/$(COMMON_DIR)/mbed/suspend.o \
-       $(OBJDIR)/$(COMMON_DIR)/mbed/timer.o \
-       $(OBJDIR)/$(COMMON_DIR)/mbed/xprintf.o \
-       $(OBJDIR)/$(COMMON_DIR)/mbed/bootloader.o \
+       $(OBJDIR)/common/action.o \
+       $(OBJDIR)/common/action_tapping.o \
+       $(OBJDIR)/common/action_macro.o \
+       $(OBJDIR)/common/action_layer.o \
+       $(OBJDIR)/common/action_util.o \
+       $(OBJDIR)/common/host.o \
+       $(OBJDIR)/common/keymap.o \
+       $(OBJDIR)/common/keyboard.o \
+       $(OBJDIR)/common/util.o \
+       $(OBJDIR)/common/mbed/suspend.o \
+       $(OBJDIR)/common/mbed/timer.o \
+       $(OBJDIR)/common/mbed/xprintf.o \
+       $(OBJDIR)/common/mbed/bootloader.o \
 
 INCLUDE_PATHS += \
-       -I$(TMK_DIR)/$(COMMON_DIR)
+       -I$(TMK_DIR)/common \
+       -I$(TMK_DIR)/protocol
 
 CC_FLAGS += -include $(CONFIG_H)
 
@@ -24,13 +24,13 @@ CC_FLAGS += -include $(CONFIG_H)
 # Option modules
 ifdef BOOTMAGIC_ENABLE
     $(error Not Supported)
-    OBJECTS += $(OBJDIR)/$(COMMON_DIR)/bootmagic.o
-    OBJECTS += $(OBJDIR)/$(COMMON_DIR)/mbed/eeprom.o
+    OBJECTS += $(OBJDIR)/common/bootmagic.o
+    OBJECTS += $(OBJDIR)/common/mbed/eeprom.o
     OPT_DEFS += -DBOOTMAGIC_ENABLE
 endif
 
 ifdef MOUSEKEY_ENABLE
-    OBJECTS += $(OBJDIR)/$(COMMON_DIR)/mousekey.o
+    OBJECTS += $(OBJDIR)/common/mousekey.o
     OPT_DEFS += -DMOUSEKEY_ENABLE
     OPT_DEFS += -DMOUSE_ENABLE
 endif
@@ -50,7 +50,7 @@ endif
 
 ifdef COMMAND_ENABLE
     $(error Not Supported)
-    SRC += $(COMMON_DIR)/command.c
+    SRC += common/command.c
     OPT_DEFS += -DCOMMAND_ENABLE
 endif
 
@@ -61,14 +61,14 @@ endif
 
 ifdef SLEEP_LED_ENABLE
     $(error Not Supported)
-    SRC += $(COMMON_DIR)/sleep_led.c
+    SRC += common/sleep_led.c
     OPT_DEFS += -DSLEEP_LED_ENABLE
     OPT_DEFS += -DNO_SUSPEND_POWER_DOWN
 endif
 
 ifdef BACKLIGHT_ENABLE
     $(error Not Supported)
-    SRC += $(COMMON_DIR)/backlight.c
+    SRC += common/backlight.c
     OPT_DEFS += -DBACKLIGHT_ENABLE
 endif