added protocol stack: pjrc, vusb

This commit is contained in:
tmk 2011-02-09 00:03:58 +09:00
parent 5552b5afea
commit acc974c64b
55 changed files with 836 additions and 1832 deletions

View file

@ -9,16 +9,11 @@ TARGET_DIR = .
# keyboard dependent files
TARGET_SRC = main.c \
keyboard.c \
mousekey.c \
layer.c \
keymap.c \
matrix.c \
led.c \
ps2.c \
host_vusb.c \
print.c \
util.c \
timer.c \
usart_print.c
OPT_DEFS = -DDEBUG_LEVEL=0
@ -44,9 +39,10 @@ F_CPU = 16000000
# Build Options
# comment out to disable the options.
#
#MOUSEKEY_ENABLE = yes # Mouse keys
MOUSEKEY_ENABLE = yes # Mouse keys
#USB_EXTRA_ENABLE = yes # Enhanced feature for Windows(Audio control and System control)
#USB_NKRO_ENABLE = yes # USB Nkey Rollover
include $(COMMON_DIR)/Makefile.vusb
include $(COMMON_DIR)/Makefile.common

View file

@ -1,165 +0,0 @@
# Name: Makefile
# Project: hid-mouse example
# Author: Christian Starkjohann
# Creation Date: 2008-04-07
# Tabsize: 4
# Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
# License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
# This Revision: $Id: Makefile 692 2008-11-07 15:07:40Z cs $
DEVICE = atmega168
F_CPU = 16000000 # in Hz
FUSE_L = # see below for fuse values for particular devices
FUSE_H =
#AVRDUDE = avrdude -c usbasp -p $(DEVICE) # edit this line for your programmer
AVRDUDE = avrdude -P COM1 -b 19200 -c arduino -p $(DEVICE)
CFLAGS = -Iusbdrv -I. -DDEBUG_LEVEL=1
OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE)
##############################################################################
# Fuse values for particular devices
##############################################################################
# If your device is not listed here, go to
# http://palmavr.sourceforge.net/cgi-bin/fc.cgi
# and choose options for external crystal clock and no clock divider
#
################################## ATMega8 ##################################
# ATMega8 FUSE_L (Fuse low byte):
# 0x9f = 1 0 0 1 1 1 1 1
# ^ ^ \ / \--+--/
# | | | +------- CKSEL 3..0 (external >8M crystal)
# | | +--------------- SUT 1..0 (crystal osc, BOD enabled)
# | +------------------ BODEN (BrownOut Detector enabled)
# +-------------------- BODLEVEL (2.7V)
# ATMega8 FUSE_H (Fuse high byte):
# 0xc9 = 1 1 0 0 1 0 0 1 <-- BOOTRST (boot reset vector at 0x0000)
# ^ ^ ^ ^ ^ ^ ^------ BOOTSZ0
# | | | | | +-------- BOOTSZ1
# | | | | + --------- EESAVE (don't preserve EEPROM over chip erase)
# | | | +-------------- CKOPT (full output swing)
# | | +---------------- SPIEN (allow serial programming)
# | +------------------ WDTON (WDT not always on)
# +-------------------- RSTDISBL (reset pin is enabled)
#
############################## ATMega48/88/168 ##############################
# ATMega*8 FUSE_L (Fuse low byte):
# 0xdf = 1 1 0 1 1 1 1 1
# ^ ^ \ / \--+--/
# | | | +------- CKSEL 3..0 (external >8M crystal)
# | | +--------------- SUT 1..0 (crystal osc, BOD enabled)
# | +------------------ CKOUT (if 0: Clock output enabled)
# +-------------------- CKDIV8 (if 0: divide by 8)
# ATMega*8 FUSE_H (Fuse high byte):
# 0xde = 1 1 0 1 1 1 1 0
# ^ ^ ^ ^ ^ \-+-/
# | | | | | +------ BODLEVEL 0..2 (110 = 1.8 V)
# | | | | + --------- EESAVE (preserve EEPROM over chip erase)
# | | | +-------------- WDTON (if 0: watchdog always on)
# | | +---------------- SPIEN (allow serial programming)
# | +------------------ DWEN (debug wire enable)
# +-------------------- RSTDISBL (reset pin is enabled)
#
############################## ATTiny25/45/85 ###############################
# ATMega*5 FUSE_L (Fuse low byte):
# 0xef = 1 1 1 0 1 1 1 1
# ^ ^ \+/ \--+--/
# | | | +------- CKSEL 3..0 (clock selection -> crystal @ 12 MHz)
# | | +--------------- SUT 1..0 (BOD enabled, fast rising power)
# | +------------------ CKOUT (clock output on CKOUT pin -> disabled)
# +-------------------- CKDIV8 (divide clock by 8 -> don't divide)
# ATMega*5 FUSE_H (Fuse high byte):
# 0xdd = 1 1 0 1 1 1 0 1
# ^ ^ ^ ^ ^ \-+-/
# | | | | | +------ BODLEVEL 2..0 (brownout trigger level -> 2.7V)
# | | | | +---------- EESAVE (preserve EEPROM on Chip Erase -> not preserved)
# | | | +-------------- WDTON (watchdog timer always on -> disable)
# | | +---------------- SPIEN (enable serial programming -> enabled)
# | +------------------ DWEN (debug wire enable)
# +-------------------- RSTDISBL (disable external reset -> enabled)
#
################################ ATTiny2313 #################################
# ATTiny2313 FUSE_L (Fuse low byte):
# 0xef = 1 1 1 0 1 1 1 1
# ^ ^ \+/ \--+--/
# | | | +------- CKSEL 3..0 (clock selection -> crystal @ 12 MHz)
# | | +--------------- SUT 1..0 (BOD enabled, fast rising power)
# | +------------------ CKOUT (clock output on CKOUT pin -> disabled)
# +-------------------- CKDIV8 (divide clock by 8 -> don't divide)
# ATTiny2313 FUSE_H (Fuse high byte):
# 0xdb = 1 1 0 1 1 0 1 1
# ^ ^ ^ ^ \-+-/ ^
# | | | | | +---- RSTDISBL (disable external reset -> enabled)
# | | | | +-------- BODLEVEL 2..0 (brownout trigger level -> 2.7V)
# | | | +-------------- WDTON (watchdog timer always on -> disable)
# | | +---------------- SPIEN (enable serial programming -> enabled)
# | +------------------ EESAVE (preserve EEPROM on Chip Erase -> not preserved)
# +-------------------- DWEN (debug wire enable)
# symbolic targets:
help:
@echo "This Makefile has no default rule. Use one of the following:"
@echo "make hex ....... to build main.hex"
@echo "make program ... to flash fuses and firmware"
@echo "make fuse ...... to flash the fuses"
@echo "make flash ..... to flash the firmware (use this on metaboard)"
@echo "make clean ..... to delete objects and hex file"
hex: main.hex
program: flash fuse
# rule for programming fuse bits:
fuse:
@[ "$(FUSE_H)" != "" -a "$(FUSE_L)" != "" ] || \
{ echo "*** Edit Makefile and choose values for FUSE_L and FUSE_H!"; exit 1; }
$(AVRDUDE) -U hfuse:w:$(FUSE_H):m -U lfuse:w:$(FUSE_L):m
# rule for uploading firmware:
flash: main.hex
$(AVRDUDE) -U flash:w:main.hex:i
# rule for deleting dependent files (those which can be built by Make):
clean:
rm -f main.hex main.lst main.obj main.cof main.list main.map main.eep.hex main.elf *.o usbdrv/*.o main.s usbdrv/oddebug.s usbdrv/usbdrv.s
# Generic rule for compiling C files:
.c.o:
$(COMPILE) -c $< -o $@
# Generic rule for assembling Assembler source files:
.S.o:
$(COMPILE) -x assembler-with-cpp -c $< -o $@
# "-x assembler-with-cpp" should not be necessary since this is the default
# file type for the .S (with capital S) extension. However, upper case
# characters are not always preserved on Windows. To ensure WinAVR
# compatibility define the file type manually.
# Generic rule for compiling C to assembler, used for debugging only.
.c.s:
$(COMPILE) -S $< -o $@
# file targets:
# Since we don't want to ship the driver multipe times, we copy it into this project:
usbdrv:
cp -r ../usbdrv .
main.elf: usbdrv $(OBJECTS) # usbdrv dependency only needed because we copy it
$(COMPILE) -o main.elf $(OBJECTS)
main.hex: main.elf
rm -f main.hex main.eep.hex
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
avr-size main.hex
# debugging targets:
disasm: main.elf
avr-objdump -d main.elf
cpp:
$(COMPILE) -E main.c

View file

@ -1,36 +0,0 @@
#ifndef HOST_H
#define HOST_H
#include <stdint.h>
#define REPORT_KEYS 6
#define MOUSE_BTN1 (1<<0)
#define MOUSE_BTN2 (1<<1)
#define MOUSE_BTN3 (1<<2)
#define MOUSE_BTN4 (1<<3)
#define MOUSE_BTN5 (1<<4)
typedef struct {
uint8_t mods;
uint8_t rserved; // not used
uint8_t keys[REPORT_KEYS];
} report_keyboard_t;
typedef struct {
uint8_t buttons;
int8_t x;
int8_t y;
/*
int8_t v;
int8_t h;
*/
} report_mouse_t;
extern uint8_t host_keyboard_led;
void host_keyboard_send(report_keyboard_t *report);
void host_mouse_send(report_mouse_t *report);
#endif

View file

@ -18,14 +18,6 @@ void host_vusb_keyboard_send()
usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
}
/*
if (kbuf_head != kbuf_tail) {
if (usbInterruptIsReady()) {
usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
}
}
*/
}
void host_keyboard_send(report_keyboard_t *report)
@ -37,12 +29,6 @@ void host_keyboard_send(report_keyboard_t *report)
print("kbuf: "); phex(kbuf_head); phex(kbuf_tail); print("\n");
} else {
print("kbuf: full\n");
// hmm...
/*
matrix_init();
kbuf_head = 0;
kbuf_tail = 0;
*/
}
}

View file

@ -1,97 +0,0 @@
#include "usb_keycodes.h"
#include "host.h"
#include "ps2.h"
#include "usb.h"
#include "keyboard.h"
#include "print.h"
static report_keyboard_t report0;
static report_keyboard_t report1;
static report_keyboard_t *report = &report0;
static report_keyboard_t *report_prev = &report1;
void keyboard_set_led(uint8_t usb_led)
{
uint8_t ps2_led = 0;
if (usb_led & (1<<USB_LED_SCROLL_LOCK))
ps2_led |= (1<<PS2_LED_SCROLL_LOCK);
if (usb_led & (1<<USB_LED_NUM_LOCK))
ps2_led |= (1<<PS2_LED_NUM_LOCK);
if (usb_led & (1<<USB_LED_CAPS_LOCK))
ps2_led |= (1<<PS2_LED_CAPS_LOCK);
print("ps2_led: "); phex(ps2_led); print("\n");
ps2_host_set_led(ps2_led);
}
void keyboard_send(void)
{
host_keyboard_send(report);
}
bool keyboard_has_key(void)
{
for (int i = 0; i < REPORT_KEYS; i++) {
if (report->keys[i])
return true;
}
return false;
}
void keyboard_add_mod(uint8_t mod)
{
report->mods |= mod;
}
void keyboard_add_key(uint8_t code)
{
int8_t i = 0;
int8_t empty = -1;
for (; i < REPORT_KEYS; i++) {
if (report_prev->keys[i] == code) {
report->keys[i] = code;
break;
}
if (empty == -1 && report_prev->keys[i] == KB_NO && report->keys[i] == KB_NO) {
empty = i;
}
}
if (i == REPORT_KEYS && empty != -1) {
report->keys[empty] = code;
}
}
void keyboard_add_code(uint8_t code)
{
if (IS_MOD(code)) {
keyboard_add_mod(code);
} else {
keyboard_add_key(code);
}
}
void keyboard_swap_report(void)
{
report_keyboard_t *tmp = report_prev;
report_prev = report;
report = tmp;
}
void keyboard_clear_report(void)
{
report->mods = 0;
for (int8_t i = 0; i < REPORT_KEYS; i++) {
report->keys[i] = 0;
}
}
report_keyboard_t *keyboard_report(void)
{
return report;
}
report_keyboard_t *keyboard_report_prev(void)
{
return report_prev;
}

View file

@ -1,22 +0,0 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include <stdint.h>
#include <stdbool.h>
#include "host.h"
void keyboard_set_led(uint8_t led);
void keyboard_send(void);
bool keyboard_has_key(void);
void keyboard_add_mod(uint8_t mod);
void keyboard_add_key(uint8_t key);
void keyboard_add_code(uint8_t code);
void keyboard_swap_report(void);
void keyboard_clear_report(void);
report_keyboard_t *keyboard_report(void);
report_keyboard_t *keyboard_report_prev(void);
#endif

View file

@ -4,8 +4,8 @@
#include <stdint.h>
#include <stdbool.h>
#include <avr/pgmspace.h>
#include "usb_keyboard.h"
#include "usb_keycodes.h"
#include "keyboard.h"
#include "print.h"
#include "debug.h"
#include "util.h"
@ -185,5 +185,5 @@ uint8_t keymap_fn_keycode(uint8_t fn_bits)
// define a condition to enter special function mode
bool keymap_is_special_mode(uint8_t fn_bits)
{
return usb_keyboard_mods == (BIT_LSHIFT | BIT_RSHIFT) || usb_keyboard_mods == (BIT_LCTRL | BIT_RSHIFT);
return keyboard_get_mods() == (BIT_LSHIFT | BIT_RSHIFT) || keyboard_get_mods() == (BIT_LCTRL | BIT_RSHIFT);
}

View file

@ -1,183 +0,0 @@
#include "keymap_skel.h"
#include "keyboard.h"
#include "debug.h"
#include "timer.h"
#include "layer.h"
/*
* Parameters:
* ENTER_DELAY |=======|
* SEND_FN_TERM |================|
*
* Fn key processing cases:
* 1. release Fn after SEND_FN_TERM.
* Layer sw ___________|~~~~~~~~~~~|___
* Fn press ___|~~~~~~~~~~~~~~~~~~~|___
* Fn send ___________________________
*
* 2. release Fn during SEND_FN_TERM.(not layer used)
* Layer sw ___________|~~~~~~|________
* Fn press ___|~~~~~~~~~~~~~~|________
* Fn key send __________________|~|______
* other key press ___________________________
* other key send ___________________________
*
* 3. release Fn during SEND_FN_TERM.(layer used)
* Layer sw ___________|~~~~~~|________
* Fn press ___|~~~~~~~~~~~~~~|________
* Fn key send ___________________________
* Fn send ___________________________
* other key press _____________|~~|__________
* other key send _____________|~~|__________
*
* 4. press other key during ENTER_DELAY.
* Layer sw ___________________________
* Fn key press ___|~~~~~~~~~|_____________
* Fn key send ______|~~~~~~|_____________
* other key press ______|~~~|________________
* other key send _______|~~|________________
*
* 5. press Fn while press other key.
* Layer sw ___________________________
* Fn key press ___|~~~~~~~~~|_____________
* Fn key send ___|~~~~~~~~~|_____________
* other key press ~~~~~~~|___________________
* other key send ~~~~~~~|___________________
*
* 6. press Fn twice quickly and keep holding down.(repeat)
* Layer sw ___________________________
* Fn key press ___|~|____|~~~~~~~~~~~~~~~~
* Fn key send _____|~|__|~~~~~~~~~~~~~~~~
*/
// LAYER_ENTER_DELAY: prevent from moving new layer
#define LAYER_ENTER_DELAY 10
// LAYER_SEND_FN_TERM: send keycode if release key in this term
#define LAYER_SEND_FN_TERM 40
uint8_t default_layer = 0;
uint8_t current_layer = 0;
static bool layer_used = false;
static uint8_t new_layer(uint8_t fn_bits);
uint8_t layer_get_keycode(uint8_t row, uint8_t col)
{
uint8_t code = keymap_get_keycode(current_layer, row, col);
// normal key or mouse key
if ((IS_KEY(code) || IS_MOUSEKEY(code))) {
layer_used = true;
}
return code;
}
// bit substract b from a
#define BIT_SUBT(a, b) (a&(a^b))
void layer_switching(uint8_t fn_bits)
{
// layer switching
static uint8_t last_fn = 0;
static uint8_t last_mods = 0;
static uint16_t last_timer = 0;
static uint8_t sent_fn = 0;
if (fn_bits == last_fn) { // Fn state is not changed
if (fn_bits == 0) {
// do nothing
} else {
if (timer_elapsed(last_timer) > LAYER_ENTER_DELAY) {
uint8_t _layer_to_switch = new_layer(BIT_SUBT(fn_bits, sent_fn));
if (current_layer != _layer_to_switch) { // not switch layer yet
debug("Fn case: 1,2,3(LAYER_ENTER_DELAY passed)\n");
debug("Switch Layer: "); debug_hex(current_layer);
current_layer = _layer_to_switch;
layer_used = false;
debug(" -> "); debug_hex(current_layer); debug("\n");
}
} else {
if (keyboard_has_key()) { // other keys is pressed
uint8_t _fn_to_send = BIT_SUBT(fn_bits, sent_fn);
if (_fn_to_send) {
debug("Fn case: 4(send Fn before other key pressed)\n");
// send only Fn key first
keyboard_swap_report();
keyboard_clear_report();
keyboard_add_code(keymap_fn_keycode(_fn_to_send)); // TODO: do all Fn keys
keyboard_add_mod(last_mods);
keyboard_send();
keyboard_swap_report();
sent_fn |= _fn_to_send;
}
}
}
// add Fn keys to send
//keyboard_add_code(keymap_fn_keycode(fn_bits&sent_fn)); // TODO: do all Fn keys
}
} else { // Fn state is changed(edge)
uint8_t fn_changed = 0;
debug("fn_bits: "); debug_bin(fn_bits); debug("\n");
debug("sent_fn: "); debug_bin(sent_fn); debug("\n");
debug("last_fn: "); debug_bin(last_fn); debug("\n");
debug("last_mods: "); debug_hex(last_mods); debug("\n");
debug("last_timer: "); debug_hex16(last_timer); debug("\n");
// pressed Fn
if ((fn_changed = BIT_SUBT(fn_bits, last_fn))) {
debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
if (keyboard_has_key()) {
debug("Fn case: 5(pressed Fn with other key)\n");
sent_fn |= fn_changed;
} else if (fn_changed & sent_fn) { // pressed same Fn in a row
if (timer_elapsed(last_timer) > LAYER_ENTER_DELAY) {
debug("Fn case: 6(repate2)\n");
// time passed: not repeate
sent_fn &= ~fn_changed;
} else {
debug("Fn case: 6(repate)\n");
}
}
}
// released Fn
if ((fn_changed = BIT_SUBT(last_fn, fn_bits))) {
debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
if (timer_elapsed(last_timer) < LAYER_SEND_FN_TERM) {
//if (!layer_used && BIT_SUBT(fn_changed, sent_fn)) { // layer not used && Fn not sent
if (BIT_SUBT(fn_changed, sent_fn)) { // layer not used && Fn not sent
debug("Fn case: 2(send Fn one shot: released Fn during LAYER_SEND_FN_TERM)\n");
// send only Fn key first
keyboard_swap_report();
keyboard_clear_report();
keyboard_add_code(keymap_fn_keycode(fn_changed)); // TODO: do all Fn keys
keyboard_add_mod(last_mods);
keyboard_send();
keyboard_swap_report();
sent_fn |= fn_changed;
}
}
debug("Switch Layer(released Fn): "); debug_hex(current_layer);
current_layer = new_layer(BIT_SUBT(fn_bits, sent_fn));
layer_used = false;
debug(" -> "); debug_hex(current_layer); debug("\n");
}
last_fn = fn_bits;
last_mods = keyboard_report()->mods;
last_timer = timer_read();
}
// send Fn keys
for (uint8_t i = 0; i < 8; i++) {
if ((sent_fn & fn_bits) & (1<<i)) {
keyboard_add_code(keymap_fn_keycode(1<<i));
}
}
}
inline
static uint8_t new_layer(uint8_t fn_bits)
{
return (fn_bits ? keymap_fn_layer(fn_bits) : default_layer);
}

16
ps2_vusb/led.c Normal file
View file

@ -0,0 +1,16 @@
#include "stdint.h"
#include "ps2.h"
#include "led.h"
void led_set(uint8_t usb_led)
{
uint8_t ps2_led = 0;
if (usb_led & (1<<USB_LED_SCROLL_LOCK))
ps2_led |= (1<<PS2_LED_SCROLL_LOCK);
if (usb_led & (1<<USB_LED_NUM_LOCK))
ps2_led |= (1<<PS2_LED_NUM_LOCK);
if (usb_led & (1<<USB_LED_CAPS_LOCK))
ps2_led |= (1<<PS2_LED_CAPS_LOCK);
ps2_host_set_led(ps2_led);
}

View file

@ -84,7 +84,7 @@ int main(void)
// do nothing
}
else if (IS_MOD(code)) {
keyboard_add_mod(MOD_BIT(code));
keyboard_add_mod_bit(MOD_BIT(code));
}
else if (IS_KEY(code)) {
keyboard_add_key(code);

View file

@ -9,7 +9,7 @@
#include "util.h"
#include "debug.h"
#include "ps2.h"
#include "usb_keyboard.h"
#include "keyboard.h"
#include "matrix_skel.h"