From 80c3ff5fa03429f1e4ea15032f665ceb88c9b8c3 Mon Sep 17 00:00:00 2001 From: tmk Date: Wed, 18 Jun 2014 02:22:59 +0900 Subject: Fix build files for mbed --- protocol/mbed/HIDKeyboard.cpp | 256 ++++++++++++++++++++++++++++++++++++++++++ protocol/mbed/HIDKeyboard.h | 27 +++++ protocol/mbed/mbed_driver.cpp | 41 +++++++ protocol/mbed/mbed_driver.h | 3 + 4 files changed, 327 insertions(+) create mode 100644 protocol/mbed/HIDKeyboard.cpp create mode 100644 protocol/mbed/HIDKeyboard.h create mode 100644 protocol/mbed/mbed_driver.cpp create mode 100644 protocol/mbed/mbed_driver.h (limited to 'protocol') diff --git a/protocol/mbed/HIDKeyboard.cpp b/protocol/mbed/HIDKeyboard.cpp new file mode 100644 index 000000000..f40c2d2f2 --- /dev/null +++ b/protocol/mbed/HIDKeyboard.cpp @@ -0,0 +1,256 @@ +#include +#include "USBHID.h" +#include "USBHID_Types.h" +#include "USBDescriptor.h" +#include "HIDKeyboard.h" + +#define DEFAULT_CONFIGURATION (1) + + +HIDKeyboard::HIDKeyboard(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) +{ + USBDevice::connect(); +} + +bool HIDKeyboard::sendReport(report_keyboard_t report) { + USBDevice::write(EP1IN, report.raw, sizeof(report), MAX_PACKET_SIZE_EP1); + return true; +} + +bool HIDKeyboard::USBCallback_setConfiguration(uint8_t configuration) { + if (configuration != DEFAULT_CONFIGURATION) { + return false; + } + + // Configure endpoints > 0 + addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT); + //addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT); + + // We activate the endpoint to be able to recceive data + //readStart(EPINT_OUT, MAX_PACKET_SIZE_EPINT); + return true; +} + + +uint8_t * HIDKeyboard::stringImanufacturerDesc() { + static uint8_t stringImanufacturerDescriptor[] = { + 0x18, /*bLength*/ + STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ + 't',0,'m',0,'k',0,'-',0,'k',0,'b',0,'d',0,'.',0,'c',0,'o',0,'m',0 /*bString iManufacturer*/ + }; + return stringImanufacturerDescriptor; +} + +uint8_t * HIDKeyboard::stringIproductDesc() { + static uint8_t stringIproductDescriptor[] = { + 0x0a, /*bLength*/ + STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ + 'm',0,'b',0,'e',0,'d',0 /*bString iProduct*/ + }; + return stringIproductDescriptor; +} + +uint8_t * HIDKeyboard::stringIserialDesc() { + static uint8_t stringIserialDescriptor[] = { + 0x04, /*bLength*/ + STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ + '0',0 /*bString iSerial*/ + }; + return stringIserialDescriptor; +} + +uint8_t * HIDKeyboard::reportDesc() { + static uint8_t reportDescriptor[] = { + USAGE_PAGE(1), 0x01, // Generic Desktop + USAGE(1), 0x06, // Keyboard + COLLECTION(1), 0x01, // Application + + USAGE_PAGE(1), 0x07, // Key Codes + USAGE_MINIMUM(1), 0xE0, + USAGE_MAXIMUM(1), 0xE7, + LOGICAL_MINIMUM(1), 0x00, + LOGICAL_MAXIMUM(1), 0x01, + REPORT_SIZE(1), 0x01, + REPORT_COUNT(1), 0x08, + INPUT(1), 0x02, // Data, Variable, Absolute + + REPORT_COUNT(1), 0x01, + REPORT_SIZE(1), 0x08, + INPUT(1), 0x01, // Constant + + REPORT_COUNT(1), 0x05, + REPORT_SIZE(1), 0x01, + USAGE_PAGE(1), 0x08, // LEDs + USAGE_MINIMUM(1), 0x01, + USAGE_MAXIMUM(1), 0x05, + OUTPUT(1), 0x02, // Data, Variable, Absolute + + REPORT_COUNT(1), 0x01, + REPORT_SIZE(1), 0x03, + OUTPUT(1), 0x01, // Constant + + + REPORT_COUNT(1), 0x06, + REPORT_SIZE(1), 0x08, + LOGICAL_MINIMUM(1), 0x00, + LOGICAL_MAXIMUM(1), 0xFF, + USAGE_PAGE(1), 0x07, // Key Codes + USAGE_MINIMUM(1), 0x00, + USAGE_MAXIMUM(1), 0xFF, + INPUT(1), 0x00, // Data, Array + END_COLLECTION(0), + }; + reportLength = sizeof(reportDescriptor); + return reportDescriptor; +} + +uint16_t HIDKeyboard::reportDescLength() { + reportDesc(); + return reportLength; +} + +#define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \ + + (1 * INTERFACE_DESCRIPTOR_LENGTH) \ + + (1 * HID_DESCRIPTOR_LENGTH) \ + + (1 * ENDPOINT_DESCRIPTOR_LENGTH)) +uint8_t * HIDKeyboard::configurationDesc() { + static uint8_t configurationDescriptor[] = { + CONFIGURATION_DESCRIPTOR_LENGTH,// bLength + CONFIGURATION_DESCRIPTOR, // bDescriptorType + LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB) + MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB) + 0x01, // bNumInterfaces + DEFAULT_CONFIGURATION, // bConfigurationValue + 0x00, // iConfiguration + C_RESERVED | C_REMOTE_WAKEUP, // bmAttributes + C_POWER(100), // bMaxPowerHello World from Mbed + + INTERFACE_DESCRIPTOR_LENGTH, // bLength + INTERFACE_DESCRIPTOR, // bDescriptorType + 0x00, // bInterfaceNumber + 0x00, // bAlternateSetting + 0x01, // bNumEndpoints + HID_CLASS, // bInterfaceClass + 1, // bInterfaceSubClass (boot) + 1, // bInterfaceProtocol (keyboard) + 0x00, // iInterface + + HID_DESCRIPTOR_LENGTH, // bLength + HID_DESCRIPTOR, // bDescriptorType + LSB(HID_VERSION_1_11), // bcdHID (LSB) + MSB(HID_VERSION_1_11), // bcdHID (MSB) + 0x00, // bCountryCode + 0x01, // bNumDescriptors + REPORT_DESCRIPTOR, // bDescriptorType + (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB) + (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB) + + ENDPOINT_DESCRIPTOR_LENGTH, // bLength + ENDPOINT_DESCRIPTOR, // bDescriptorType + PHY_TO_DESC(EP1IN), // bEndpointAddress + E_INTERRUPT, // bmAttributes + LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) + MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) + 1, // bInterval (milliseconds) + }; + return configurationDescriptor; +} + +#if 0 +uint8_t * HIDKeyboard::deviceDesc() { + static uint8_t deviceDescriptor[] = { + DEVICE_DESCRIPTOR_LENGTH, /* bLength */ + DEVICE_DESCRIPTOR, /* bDescriptorType */ + LSB(USB_VERSION_2_0), /* bcdUSB (LSB) */ + MSB(USB_VERSION_2_0), /* bcdUSB (MSB) */ + 0x00, /* bDeviceClass */ + 0x00, /* bDeviceSubClass */ + 0x00, /* bDeviceprotocol */ + MAX_PACKET_SIZE_EP0, /* bMaxPacketSize0 */ + (uint8_t)(LSB(0xfeed)), /* idVendor (LSB) */ + (uint8_t)(MSB(0xfeed)), /* idVendor (MSB) */ + (uint8_t)(LSB(0x1bed)), /* idProduct (LSB) */ + (uint8_t)(MSB(0x1bed)), /* idProduct (MSB) */ + (uint8_t)(LSB(0x0002)), /* bcdDevice (LSB) */ + (uint8_t)(MSB(0x0002)), /* bcdDevice (MSB) */ + 0, /* iManufacturer */ + 0, /* iProduct */ + 0, /* iSerialNumber */ + 0x01 /* bNumConfigurations */ + }; + return deviceDescriptor; +} +#endif + +bool HIDKeyboard::USBCallback_request() { + bool success = false; + CONTROL_TRANSFER * transfer = getTransferPtr(); + uint8_t *hidDescriptor; + + // Process additional standard requests + + if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE)) + { + switch (transfer->setup.bRequest) + { + case GET_DESCRIPTOR: + switch (DESCRIPTOR_TYPE(transfer->setup.wValue)) + { + case REPORT_DESCRIPTOR: + if ((reportDesc() != NULL) \ + && (reportDescLength() != 0)) + { + transfer->remaining = reportDescLength(); + transfer->ptr = reportDesc(); + transfer->direction = DEVICE_TO_HOST; + success = true; + } + break; + case HID_DESCRIPTOR: + // Find the HID descriptor, after the configuration descriptor + hidDescriptor = findDescriptor(HID_DESCRIPTOR); + if (hidDescriptor != NULL) + { + transfer->remaining = HID_DESCRIPTOR_LENGTH; + transfer->ptr = hidDescriptor; + transfer->direction = DEVICE_TO_HOST; + success = true; + } + break; + + default: + break; + } + break; + default: + break; + } + } + + // Process class-specific requests +/* + if (transfer->setup.bmRequestType.Type == CLASS_TYPE) + { + switch (transfer->setup.bRequest) + { + case SET_REPORT: + // First byte will be used for report ID + //outputReport.data[0] = transfer->setup.wValue & 0xff; + //outputReport.length = transfer->setup.wLength + 1; + outputReport.length = transfer->setup.wLength; + + //transfer->remaining = sizeof(outputReport.data) - 1; + //transfer->ptr = &outputReport.data[1]; + transfer->remaining = sizeof(outputReport.data); + transfer->ptr = &outputReport.data[0]; + transfer->direction = HOST_TO_DEVICE; + transfer->notify = true; + success = true; + default: + break; + } + } +*/ + + return success; +} diff --git a/protocol/mbed/HIDKeyboard.h b/protocol/mbed/HIDKeyboard.h new file mode 100644 index 000000000..4ebe610a6 --- /dev/null +++ b/protocol/mbed/HIDKeyboard.h @@ -0,0 +1,27 @@ +#ifndef HIDKEYBOARD_H + +#include "stdint.h" +#include "stdbool.h" +#include "USBHID.h" +#include "report.h" + + +class HIDKeyboard : public USBDevice { +public: + HIDKeyboard(uint16_t vendor_id = 0xFEED, uint16_t product_id = 0xabed, uint16_t product_release = 0x0001); + + bool sendReport(report_keyboard_t report); +protected: + uint16_t reportLength; + virtual bool USBCallback_setConfiguration(uint8_t configuration); + virtual uint8_t * stringImanufacturerDesc(); + virtual uint8_t * stringIproductDesc(); + virtual uint8_t * stringIserialDesc(); + virtual uint16_t reportDescLength(); + virtual uint8_t * reportDesc(); + virtual uint8_t * configurationDesc(); + //virtual uint8_t * deviceDesc(); + virtual bool USBCallback_request(); +}; + +#endif diff --git a/protocol/mbed/mbed_driver.cpp b/protocol/mbed/mbed_driver.cpp new file mode 100644 index 000000000..333f8e378 --- /dev/null +++ b/protocol/mbed/mbed_driver.cpp @@ -0,0 +1,41 @@ +#include "HIDKeyboard.h" +#include "host.h" +#include "host_driver.h" +#include "mbed_driver.h" + +HIDKeyboard keyboard; + + +/* Host driver */ +static uint8_t keyboard_leds(void); +static void send_keyboard(report_keyboard_t *report); +static void send_mouse(report_mouse_t *report); +static void send_system(uint16_t data); +static void send_consumer(uint16_t data); + +host_driver_t mbed_driver = { + keyboard_leds, + send_keyboard, + send_mouse, + send_system, + send_consumer +}; + + +static uint8_t keyboard_leds(void) +{ + return 0; +} +static void send_keyboard(report_keyboard_t *report) +{ + keyboard.sendReport(*report); +} +static void send_mouse(report_mouse_t *report) +{ +} +static void send_system(uint16_t data) +{ +} +static void send_consumer(uint16_t data) +{ +} diff --git a/protocol/mbed/mbed_driver.h b/protocol/mbed/mbed_driver.h new file mode 100644 index 000000000..dd1153b43 --- /dev/null +++ b/protocol/mbed/mbed_driver.h @@ -0,0 +1,3 @@ +#include "host_driver.h" + +extern host_driver_t mbed_driver; -- cgit v1.2.3 From 4c8e0fd0bd1712421f957ec5e0ca16fc6bbb3856 Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 19 Jun 2014 16:13:35 +0900 Subject: Port ps2_usb to mbed --- common/progmem.h | 1 + converter/ps2_usb/Makefile.mbed | 44 +++++++++++++++++++++++ converter/ps2_usb/config_mbed.h | 60 +++++++++++++++++++++++++++++++ converter/ps2_usb/keymap_common.c | 3 +- converter/ps2_usb/keymap_common.h | 1 - converter/ps2_usb/main.cpp | 46 ++++++++++++++++++++++++ converter/ps2_usb/matrix.c | 3 +- keyboard/mbed_onekey/Makefile | 2 +- protocol.mk | 1 + protocol/ps2.h | 65 ++++------------------------------ protocol/ps2_busywait.c | 16 +++++---- protocol/ps2_io.h | 15 ++++++++ protocol/ps2_io_avr.c | 74 +++++++++++++++++++++++++++++++++++++++ protocol/ps2_io_mbed.c | 60 +++++++++++++++++++++++++++++++ tool/mbed/common.mk | 42 +++++++++++----------- 15 files changed, 343 insertions(+), 90 deletions(-) create mode 100644 converter/ps2_usb/Makefile.mbed create mode 100644 converter/ps2_usb/config_mbed.h create mode 100644 converter/ps2_usb/main.cpp create mode 100644 protocol/ps2_io.h create mode 100644 protocol/ps2_io_avr.c create mode 100644 protocol/ps2_io_mbed.c (limited to 'protocol') diff --git a/common/progmem.h b/common/progmem.h index 09aeb8b7c..199b1bedf 100644 --- a/common/progmem.h +++ b/common/progmem.h @@ -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 index 000000000..631f270f7 --- /dev/null +++ b/converter/ps2_usb/Makefile.mbed @@ -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 index 000000000..5819763e6 --- /dev/null +++ b/converter/ps2_usb/config_mbed.h @@ -0,0 +1,60 @@ +/* +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 . +*/ + +#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 diff --git a/converter/ps2_usb/keymap_common.c b/converter/ps2_usb/keymap_common.c index 241d2e33b..e344fb416 100644 --- a/converter/ps2_usb/keymap_common.c +++ b/converter/ps2_usb/keymap_common.c @@ -15,10 +15,11 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ #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)]); } diff --git a/converter/ps2_usb/keymap_common.h b/converter/ps2_usb/keymap_common.h index 216a8dc02..d783e01de 100644 --- a/converter/ps2_usb/keymap_common.h +++ b/converter/ps2_usb/keymap_common.h @@ -19,7 +19,6 @@ along with this program. If not, see . #include #include -#include #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 index 000000000..860af149a --- /dev/null +++ b/converter/ps2_usb/main.cpp @@ -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); + } + } +} diff --git a/converter/ps2_usb/matrix.c b/converter/ps2_usb/matrix.c index 45344c0f7..45cf2a4a9 100644 --- a/converter/ps2_usb/matrix.c +++ b/converter/ps2_usb/matrix.c @@ -17,8 +17,6 @@ along with this program. If not, see . #include #include -#include -#include #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: diff --git a/keyboard/mbed_onekey/Makefile b/keyboard/mbed_onekey/Makefile index d0d1148ce..2f7399ba0 100644 --- a/keyboard/mbed_onekey/Makefile +++ b/keyboard/mbed_onekey/Makefile @@ -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) diff --git a/protocol.mk b/protocol.mk index 7f561e62d..ca435ba43 100644 --- a/protocol.mk +++ b/protocol.mk @@ -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 diff --git a/protocol/ps2.h b/protocol/ps2.h index 483eea720..acde679cf 100644 --- a/protocol/ps2.h +++ b/protocol/ps2.h @@ -39,8 +39,9 @@ POSSIBILITY OF SUCH DAMAGE. #define PS2_H #include -#include -#include +#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< -#include +#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< +#include + +/* 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< +#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); +} diff --git a/tool/mbed/common.mk b/tool/mbed/common.mk index 1bd7d6edb..93a927a3e 100644 --- a/tool/mbed/common.mk +++ b/tool/mbed/common.mk @@ -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 -- cgit v1.2.3 From b4a91ecf4e46ee533563fd759e26dd249bf6b4de Mon Sep 17 00:00:00 2001 From: tmk Date: Fri, 20 Jun 2014 14:39:49 +0900 Subject: Add LED indicator support in mbed --- protocol/mbed/HIDKeyboard.cpp | 45 ++++++++++++++++++++++++++++--------------- protocol/mbed/HIDKeyboard.h | 4 ++++ protocol/mbed/mbed_driver.cpp | 2 +- 3 files changed, 35 insertions(+), 16 deletions(-) (limited to 'protocol') diff --git a/protocol/mbed/HIDKeyboard.cpp b/protocol/mbed/HIDKeyboard.cpp index f40c2d2f2..947077cd2 100644 --- a/protocol/mbed/HIDKeyboard.cpp +++ b/protocol/mbed/HIDKeyboard.cpp @@ -17,6 +17,10 @@ bool HIDKeyboard::sendReport(report_keyboard_t report) { return true; } +uint8_t HIDKeyboard::leds() { + return led_state; +} + bool HIDKeyboard::USBCallback_setConfiguration(uint8_t configuration) { if (configuration != DEFAULT_CONFIGURATION) { return false; @@ -228,29 +232,40 @@ bool HIDKeyboard::USBCallback_request() { } // Process class-specific requests -/* if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { - switch (transfer->setup.bRequest) - { - case SET_REPORT: - // First byte will be used for report ID - //outputReport.data[0] = transfer->setup.wValue & 0xff; - //outputReport.length = transfer->setup.wLength + 1; - outputReport.length = transfer->setup.wLength; - - //transfer->remaining = sizeof(outputReport.data) - 1; - //transfer->ptr = &outputReport.data[1]; - transfer->remaining = sizeof(outputReport.data); - transfer->ptr = &outputReport.data[0]; + switch (transfer->setup.bRequest) { + case SET_REPORT: + // LED indicator + // TODO: check Interface and Report length? + // if (transfer->setup.wIndex == INTERFACE_KEYBOAD) { } + // if (transfer->setup.wLength == 1) + + transfer->remaining = 1; + //transfer->ptr = ?? what ptr should be set when OUT(not used?) transfer->direction = HOST_TO_DEVICE; - transfer->notify = true; + transfer->notify = true; /* notify with USBCallback_requestCompleted */ success = true; default: break; } } -*/ return success; } + +void HIDKeyboard::USBCallback_requestCompleted(uint8_t * buf, uint32_t length) +{ + if (length > 0) { + CONTROL_TRANSFER *transfer = getTransferPtr(); + if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { + switch (transfer->setup.bRequest) { + case SET_REPORT: + led_state = buf[0]; + break; + default: + break; + } + } + } +} diff --git a/protocol/mbed/HIDKeyboard.h b/protocol/mbed/HIDKeyboard.h index 4ebe610a6..c537e5ece 100644 --- a/protocol/mbed/HIDKeyboard.h +++ b/protocol/mbed/HIDKeyboard.h @@ -11,6 +11,7 @@ public: HIDKeyboard(uint16_t vendor_id = 0xFEED, uint16_t product_id = 0xabed, uint16_t product_release = 0x0001); bool sendReport(report_keyboard_t report); + uint8_t leds(void); protected: uint16_t reportLength; virtual bool USBCallback_setConfiguration(uint8_t configuration); @@ -22,6 +23,9 @@ protected: virtual uint8_t * configurationDesc(); //virtual uint8_t * deviceDesc(); virtual bool USBCallback_request(); + virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length); +private: + uint8_t led_state; }; #endif diff --git a/protocol/mbed/mbed_driver.cpp b/protocol/mbed/mbed_driver.cpp index 333f8e378..6c7b16e23 100644 --- a/protocol/mbed/mbed_driver.cpp +++ b/protocol/mbed/mbed_driver.cpp @@ -24,7 +24,7 @@ host_driver_t mbed_driver = { static uint8_t keyboard_leds(void) { - return 0; + return keyboard.leds(); } static void send_keyboard(report_keyboard_t *report) { -- cgit v1.2.3 From be19b2bf324cbbca53cbd4a7588311041a7353a6 Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 1 Jul 2014 04:29:07 +0900 Subject: Change buffer size: 256 bytes --- protocol/serial_uart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'protocol') diff --git a/protocol/serial_uart.c b/protocol/serial_uart.c index 6c0af8817..600340cd8 100644 --- a/protocol/serial_uart.c +++ b/protocol/serial_uart.c @@ -47,7 +47,7 @@ void serial_init(void) } // RX ring buffer -#define RBUF_SIZE 8 +#define RBUF_SIZE 256 static uint8_t rbuf[RBUF_SIZE]; static uint8_t rbuf_head = 0; static uint8_t rbuf_tail = 0; -- cgit v1.2.3 From 3e7875455ed392ce5238efa0734e82995e1f8040 Mon Sep 17 00:00:00 2001 From: tmk Date: Sun, 20 Jul 2014 14:42:44 +0900 Subject: Add flow control by firmware to serial_uart --- keyboard/hhkb_rn42/MEMO.txt | 1 + keyboard/hhkb_rn42/config.h | 17 ++++++++++------- keyboard/hhkb_rn42/rn42/rn42_task.c | 2 -- protocol/serial_uart.c | 19 +++++++++++++++++++ 4 files changed, 30 insertions(+), 9 deletions(-) (limited to 'protocol') diff --git a/keyboard/hhkb_rn42/MEMO.txt b/keyboard/hhkb_rn42/MEMO.txt index 100b4442d..86f5faef2 100644 --- a/keyboard/hhkb_rn42/MEMO.txt +++ b/keyboard/hhkb_rn42/MEMO.txt @@ -8,6 +8,7 @@ Roving RN-42 TODO ---- +- LUFA sendchar should be buffered and serial_uart.c buffur size is too large(256). - Factroy reset doesn't work; need to test again. 10K pull-up is too high? - Lipo voltage ADC sensing - Lipo charger configuration: fast charge time: USB charger spec? diff --git a/keyboard/hhkb_rn42/config.h b/keyboard/hhkb_rn42/config.h index cd8fc176d..e641a5a2b 100644 --- a/keyboard/hhkb_rn42/config.h +++ b/keyboard/hhkb_rn42/config.h @@ -57,20 +57,23 @@ along with this program. If not, see . #define RTSEN 0 #define CTSEN 1 - #define SERIAL_UART_BAUD 115200 - #define SERIAL_UART_DATA UDR1 - #define SERIAL_UART_UBRR ((F_CPU/(16.0*SERIAL_UART_BAUD)-1+0.5)) - #define SERIAL_UART_RXD_VECT USART1_RX_vect - #define SERIAL_UART_TXD_READY (UCSR1A&(1<>8); /* baud rate */ \ UCSR1B |= (1< 2) SERIAL_UART_RTS_LO(); } while (0) + // prohibit to send + #define rbuf_check_rts_hi() do { if (RBUF_SPACE() <= 2) SERIAL_UART_RTS_HI(); } while (0) +#else + #define rbuf_check_rts_lo() + #define rbuf_check_rts_hi() +#endif + + void serial_init(void) { SERIAL_UART_INIT(); @@ -61,6 +77,7 @@ uint8_t serial_recv(void) data = rbuf[rbuf_tail]; rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; + rbuf_check_rts_lo(); return data; } @@ -73,6 +90,7 @@ int16_t serial_recv2(void) data = rbuf[rbuf_tail]; rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; + rbuf_check_rts_lo(); return data; } @@ -90,4 +108,5 @@ ISR(SERIAL_UART_RXD_VECT) rbuf[rbuf_head] = SERIAL_UART_DATA; rbuf_head = next; } + rbuf_check_rts_hi(); } -- cgit v1.2.3 From c2d830c07c823c48a17c214b093ec1bab989fb6a Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 20 Nov 2014 17:06:46 +0900 Subject: USB initialize when plug-in during battery powered --- common/avr/suspend.c | 49 ++++++++++++++++++++++++++++++++++--------------- common/suspend.h | 3 ++- protocol/lufa/lufa.c | 15 ++++++++++++++- 3 files changed, 50 insertions(+), 17 deletions(-) (limited to 'protocol') diff --git a/common/avr/suspend.c b/common/avr/suspend.c index f44a036be..66a579fd7 100644 --- a/common/avr/suspend.c +++ b/common/avr/suspend.c @@ -7,6 +7,9 @@ #include "backlight.h" #include "suspend_avr.h" #include "suspend.h" +#ifdef PROTOCOL_LUFA +#include "lufa.h" +#endif #define wdt_intr_enable(value) \ @@ -26,30 +29,45 @@ __asm__ __volatile__ ( \ ) -void suspend_power_down(void) +void suspend_idle(uint8_t time) { -#ifdef BACKLIGHT_ENABLE - backlight_set(0); -#endif -#ifndef NO_SUSPEND_POWER_DOWN - // Enable watchdog to wake from MCU sleep cli(); - wdt_reset(); + set_sleep_mode(SLEEP_MODE_IDLE); + sleep_enable(); + sei(); + sleep_cpu(); + sleep_disable(); +} + +/* Power down MCU with watchdog timer + * wdto: watchdog timer timeout defined in + * WDTO_15MS + * WDTO_30MS + * WDTO_60MS + * WDTO_120MS + * WDTO_250MS + * WDTO_500MS + * WDTO_1S + * WDTO_2S + * WDTO_4S + * WDTO_8S + */ +void suspend_power_down(uint8_t wdto) +{ +#ifdef PROTOCOL_LUFA + if (USB_DeviceState == DEVICE_STATE_Configured) return; +#endif - // Watchdog Interrupt and System Reset Mode - //wdt_enable(WDTO_1S); - //WDTCSR |= _BV(WDIE); - // Watchdog Interrupt Mode - wdt_intr_enable(WDTO_120MS); - + wdt_intr_enable(wdto); + // TODO: more power saving // See PicoPower application note // - I/O port input with pullup // - prescale clock // - BOD disable // - Power Reduction Register PRR - // sleep in power down mode + set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sei(); @@ -58,12 +76,13 @@ void suspend_power_down(void) // Disable watchdog after sleep wdt_disable(); -#endif } bool suspend_wakeup_condition(void) { + matrix_power_up(); matrix_scan(); + matrix_power_down(); for (uint8_t r = 0; r < MATRIX_ROWS; r++) { if (matrix_get_row(r)) return true; } diff --git a/common/suspend.h b/common/suspend.h index 9b76f280d..f339c670a 100644 --- a/common/suspend.h +++ b/common/suspend.h @@ -5,7 +5,8 @@ #include -void suspend_power_down(void); +void suspend_idle(uint8_t timeout); +void suspend_power_down(uint8_t timeout); bool suspend_wakeup_condition(void); void suspend_wakeup_init(void); diff --git a/protocol/lufa/lufa.c b/protocol/lufa/lufa.c index 16a602df1..6802f3b63 100644 --- a/protocol/lufa/lufa.c +++ b/protocol/lufa/lufa.c @@ -148,10 +148,23 @@ static void Console_Task(void) */ void EVENT_USB_Device_Connect(void) { + /* For battery powered device */ + if (!USB_IsInitialized) { + USB_Init(); + USB_Device_EnableSOFEvents(); + } } void EVENT_USB_Device_Disconnect(void) { + /* For battery powered device */ +/* TODO: This doesn't work. After several plug in/outs can not be enumerated. + if (USB_IsInitialized) { + USB_Disable(); // Disable all interrupts + USB_Controller_Enable(); + USB_INT_Enable(USB_INT_VBUSTI); + } +*/ } void EVENT_USB_Device_Reset(void) @@ -574,7 +587,7 @@ int main(void) print("Keyboard start.\n"); while (1) { while (USB_DeviceState == DEVICE_STATE_Suspended) { - suspend_power_down(); + suspend_power_down(WDTO_120MS); if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) { USB_Device_SendRemoteWakeup(); } -- cgit v1.2.3