From 92cda14f7a30f14bb3836c2369d110b2c8b4cf75 Mon Sep 17 00:00:00 2001 From: tmk Date: Fri, 3 Jan 2014 07:05:41 +0900 Subject: [PATCH] Add ascii_usb ASCII Serial Console terminal converter --- README.md | 1 + converter/ascii_usb/Makefile | 82 +++++++++++++++ converter/ascii_usb/README | 33 ++++++ converter/ascii_usb/config.h | 70 +++++++++++++ converter/ascii_usb/keymap.c | 42 ++++++++ converter/ascii_usb/led.c | 25 +++++ converter/ascii_usb/matrix.c | 195 +++++++++++++++++++++++++++++++++++ 7 files changed, 448 insertions(+) create mode 100644 converter/ascii_usb/Makefile create mode 100644 converter/ascii_usb/README create mode 100644 converter/ascii_usb/config.h create mode 100644 converter/ascii_usb/keymap.c create mode 100644 converter/ascii_usb/led.c create mode 100644 converter/ascii_usb/matrix.c diff --git a/README.md b/README.md index 536cc753fb..bca1c13683 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ You can find some keyboard specific projects under `converter` and `keyboard` di * [sun_usb](converter/sun_usb/) - [Sun] to USB(type4, 5 and 3?) * [pc98_usb](converter/pc98_usb/) - [PC98] to USB * [usb_usb](converter/usb_usb/) - USB to USB(experimental) +* [ascii_usb](converter/ascii_usb/) - ASCII(Serial console terminal) to USB ### keyboard * [hhkb](keyboard/hhkb/) - [Happy Hacking Keyboard pro][GH_hhkb] **my main board** diff --git a/converter/ascii_usb/Makefile b/converter/ascii_usb/Makefile new file mode 100644 index 0000000000..408aa42375 --- /dev/null +++ b/converter/ascii_usb/Makefile @@ -0,0 +1,82 @@ +# Target file name (without extension). +TARGET = ascii_usb + +# Directory common source filess exist +TOP_DIR = ../.. + +# Directory keyboard dependent files exist +TARGET_DIR = . + +# keyboard dependent files +SRC = keymap.c \ + matrix.c \ + led.c \ + protocol/serial_uart.c + +CONFIG_H = config.h + + +# MCU name, you MUST set this to match the board you are using +# type "make clean" after changing this, so all files will be rebuilt +#MCU = at90usb162 # Teensy 1.0 +MCU = atmega32u4 # Teensy 2.0 +#MCU = at90usb646 # Teensy++ 1.0 +#MCU = at90usb1286 # Teensy++ 2.0 + + +# Processor frequency. +# Normally the first thing your program should do is set the clock prescaler, +# so your program will run at the correct speed. You should also set this +# variable to same clock speed. The _delay_ms() macro uses this, and many +# examples use this variable to calculate timings. Do not add a "UL" here. +F_CPU = 16000000 + + +# +# LUFA specific +# +# Target architecture (see library "Board Types" documentation). +ARCH = AVR8 + +# Input clock frequency. +# This will define a symbol, F_USB, in all source code files equal to the +# input clock frequency (before any prescaling is performed) in Hz. This value may +# differ from F_CPU if prescaling is used on the latter, and is required as the +# raw input clock is fed directly to the PLL sections of the AVR for high speed +# clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL' +# at the end, this will be done automatically to create a 32-bit value in your +# source code. +# +# If no clock division is performed on the input clock inside the AVR (via the +# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU. +F_USB = $(F_CPU) + +# Interrupt driven control endpoint task +OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT + + +# Build Options +# *Comment out* to disable the options. +# +#MOUSEKEY_ENABLE = yes # Mouse keys +#EXTRAKEY_ENABLE = yes # Audio control and System control +CONSOLE_ENABLE = yes # Console for debug +#NKRO_ENABLE = yes # USB Nkey Rollover + + +# Boot Section Size in bytes +# Teensy halfKay 512 +# Atmel DFU loader 4096 +# LUFA bootloader 4096 +OPT_DEFS += -DBOOTLOADER_SIZE=4096 + + +# Search Path +VPATH += $(TARGET_DIR) +VPATH += $(TOP_DIR) + + +include $(TOP_DIR)/protocol/lufa.mk +include $(TOP_DIR)/protocol.mk +include $(TOP_DIR)/common.mk +include $(TOP_DIR)/rules.mk diff --git a/converter/ascii_usb/README b/converter/ascii_usb/README new file mode 100644 index 0000000000..afd2e47f76 --- /dev/null +++ b/converter/ascii_usb/README @@ -0,0 +1,33 @@ +ASCII to USB keyboard protocol converter +======================================== +This converts serial console terminal into USB keyboard, tested with TRS-80 model 100 TELCOM application. +Target MCU is ATMega32u4 but other USB capable AVR will also work. + + +Hardware +-------- +Connect RX, TX and GND to UART pin of AVR. Note that you may need line drvier/level shfiter like MAX232 to interface high voltage of RS-232C. + + + +Build Firmware +-------------- +Configure UART setting and Just use 'make' + + $ cd ascii_usb + $ make + +Then, load the binary to MCU with your favorite programmer. + + + +Limitation +---------- +- This cannot see key up event, you cannot hold a key. +- Alt, Gui(Win/Mac) modifier key are not available. + + + +Scan code +--------- +ASCII code(0x01-7F) diff --git a/converter/ascii_usb/config.h b/converter/ascii_usb/config.h new file mode 100644 index 0000000000..0908997a53 --- /dev/null +++ b/converter/ascii_usb/config.h @@ -0,0 +1,70 @@ +/* +Copyright 2014 Jun Wako + +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 . +*/ + +#ifndef CONFIG_H +#define CONFIG_H + +#define VENDOR_ID 0xFEED +#define PRODUCT_ID 0x5C01 +#define DEVICE_VER 0x0100 +#define MANUFACTURER t.m.k. +#define PRODUCT ASCII keyboard converter +#define DESCRIPTION converts Serial Console Terminal into USB keyboard + + +/* matrix size */ +#define MATRIX_ROWS 16 +#define MATRIX_COLS 16 + +/* key combination for command */ +#define IS_COMMAND() ( \ + host_get_first_key() == KC_BRK \ +) + + +/* + * Serial(USART) configuration + * asynchronous, positive logic, 19200baud, bit order: LSB first + * 1-start bit, 8-data bit, odd parity, 1-stop bit + */ +#ifdef __AVR_ATmega32U4__ + #define SERIAL_UART_BAUD 19200 + #define SERIAL_UART_DATA UDR1 + #define SERIAL_UART_UBRR ((F_CPU/(16UL*SERIAL_UART_BAUD))-1) + #define SERIAL_UART_RXD_VECT USART1_RX_vect + #define SERIAL_UART_TXD_READY (UCSR1A&(1<>8); /* baud rate */ \ + UCSR1B |= (1< + +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 . +*/ + +#include +#include +#include +#include "keycode.h" +#include "action.h" +#include "action_macro.h" +#include "action_util.h" +#include "util.h" +#include "print.h" +#include "keymap.h" + + +// Keymap is not used. See matrix.c. + +/* translates key to keycode */ +uint8_t keymap_key_to_keycode(uint8_t layer, key_t key) +{ + return KC_NO; +} + +/* translates Fn keycode to action */ +action_t keymap_fn_to_action(uint8_t keycode) +{ + return (action_t) { .code = ACTION_NO }; +} diff --git a/converter/ascii_usb/led.c b/converter/ascii_usb/led.c new file mode 100644 index 0000000000..d1c39bc653 --- /dev/null +++ b/converter/ascii_usb/led.c @@ -0,0 +1,25 @@ +/* +Copyright 2012 Jun Wako + +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 . +*/ + +#include "stdint.h" +#include "serial.h" +#include "led.h" + + +void led_set(uint8_t usb_led) +{ +} diff --git a/converter/ascii_usb/matrix.c b/converter/ascii_usb/matrix.c new file mode 100644 index 0000000000..e1ea8e0ab8 --- /dev/null +++ b/converter/ascii_usb/matrix.c @@ -0,0 +1,195 @@ +/* +Copyright 2014 Jun Wako + +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 . +*/ + +#include +#include +#include +#include +#include "print.h" +#include "util.h" +#include "matrix.h" +#include "debug.h" +#include "action_util.h" +#include "protocol/serial.h" + + +/* + * Not use Matrix. + * + * ROW: 16(4bits) + * COL: 16(4bits) + * + * 8bit wide + * +---------+ + * 0|00 ... 0F| + * 1|08 ... 1F| + * :| ... | + * :| ... | + * E|E0 ... EF| + * F|F0 ... FF| + * +---------+ + */ + + +inline +uint8_t matrix_rows(void) +{ + return MATRIX_ROWS; +} + +inline +uint8_t matrix_cols(void) +{ + return MATRIX_COLS; +} + +void matrix_init(void) +{ + debug_matrix = true; + serial_init(); + + debug("init\n"); + return; +} + +static void type_key(uint16_t keycode) +{ + if (keycode == 0) return; + + uint8_t mods = keycode>>8; + uint8_t key = keycode&0xFF; + if (mods) { + add_mods(mods); + send_keyboard_report(); + } + + add_key(key); + send_keyboard_report(); + + del_key(key); + send_keyboard_report(); + + if (mods) { + del_mods(mods); + send_keyboard_report(); + } +} +static uint16_t code2key(uint8_t code) +{ + // ASCII to key combination in US laout + switch (code) { + case 0x01 ... 0x08: // Ctrl-[a-z] + return MOD_BIT(KC_LCTRL)<<8 | (KC_A + (code-0x01)); + case 0x09: return KC_TAB; // TAB(Ctrl-i) + case 0x0A ... 0x0C: // Ctrl-[a-z] + return MOD_BIT(KC_LCTRL)<<8 | (KC_A + (code-0x01)); + case 0x0D: return KC_ENTER; // Enter(Ctrl-m) + case 0x0E ... 0x1A: // Ctrl-[a-z] + return MOD_BIT(KC_LCTRL)<<8 | (KC_A + (code-0x01)); + case 0x1B: return KC_ESC; + case 0x1C: return KC_RIGHT; + case 0x1D: return KC_LEFT; + case 0x1E: return KC_UP; + case 0x1F: return KC_DOWN; + case 0x20: return KC_SPACE; + case 0x21: return MOD_BIT(KC_LSHIFT)<<8 | KC_1; // ! + case 0x22: return MOD_BIT(KC_LSHIFT)<<8 | KC_QUOTE; // " + case 0x23: return MOD_BIT(KC_LSHIFT)<<8 | KC_3; // # + case 0x24: return MOD_BIT(KC_LSHIFT)<<8 | KC_4; // $ + case 0x25: return MOD_BIT(KC_LSHIFT)<<8 | KC_5; // % + case 0x26: return MOD_BIT(KC_LSHIFT)<<8 | KC_7; // & + case 0x27: return KC_QUOTE; // ' + case 0x28: return MOD_BIT(KC_LSHIFT)<<8 | KC_9; // ( + case 0x29: return MOD_BIT(KC_LSHIFT)<<8 | KC_0; // ) + case 0x2A: return MOD_BIT(KC_LSHIFT)<<8 | KC_8; // * + case 0x2B: return MOD_BIT(KC_LSHIFT)<<8 | KC_EQUAL; // + + case 0x2C: return KC_COMMA; // , + case 0x2D: return KC_MINUS; // - + case 0x2E: return KC_DOT; // . + case 0x2F: return KC_SLASH; // / + case 0x30: return KC_0; + case 0x31 ... 0x39: // 1-9 + return KC_1 + (code-0x31); + case 0x3A: return MOD_BIT(KC_LSHIFT)<<8 | KC_SCLN; // : + case 0x3B: return KC_SCLN; // ; + case 0x3C: return MOD_BIT(KC_LSHIFT)<<8 | KC_COMMA; // < + case 0x3D: return KC_EQUAL; // = + case 0x3E: return MOD_BIT(KC_LSHIFT)<<8 | KC_DOT; // > + case 0x3F: return MOD_BIT(KC_LSHIFT)<<8 | KC_SLASH; // ? + case 0x40: return MOD_BIT(KC_LSHIFT)<<8 | KC_2; // @ + case 0x41 ... 0x5A: // A-Z + return MOD_BIT(KC_LSHIFT)<<8 | (KC_A + (code-0x41)); + case 0x5B: return KC_LBRACKET; // [ + case 0x5C: return KC_BSLASH; // + case 0x5D: return KC_RBRACKET; // ] + case 0x5E: return MOD_BIT(KC_LSHIFT)<<8 | KC_6; // ^ + case 0x5F: return MOD_BIT(KC_LSHIFT)<<8 | KC_MINUS; // _ + case 0x61 ... 0x7A: // a-z + return KC_A + (code-0x61); + case 0x7B: return MOD_BIT(KC_LSHIFT)<<8 | KC_LBRACKET; // { + case 0x7C: return MOD_BIT(KC_LSHIFT)<<8 | KC_BSLASH; // | + case 0x7D: return MOD_BIT(KC_LSHIFT)<<8 | KC_RBRACKET; // } + case 0x7E: return MOD_BIT(KC_LSHIFT)<<8 | KC_GRAVE; // } + case 0x7F: return KC_DELETE; // + } + return 0; +} + +uint8_t matrix_scan(void) +{ + uint16_t code = serial_recv2(); + if (code == -1) { + return 0; + } + + print_hex8(code); print(" "); + + // echo back + serial_send(code); + type_key(code2key(code)); + + + return code; +} + +inline +bool matrix_has_ghost(void) +{ + return false; +} + +inline +bool matrix_is_on(uint8_t row, uint8_t col) +{ + return false; +} + +inline +matrix_row_t matrix_get_row(uint8_t row) +{ + return 0; +} + +void matrix_print(void) +{ + print("\nr/c 0123456789ABCDEF\n"); + for (uint8_t row = 0; row < matrix_rows(); row++) { + phex(row); print(": "); + pbin_reverse(matrix_get_row(row)); + print("\n"); + } +}