aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.rules36
-rw-r--r--ps2.h4
-rw-r--r--ps2_vusb/Makefile37
-rw-r--r--ps2_vusb/config.h88
-rw-r--r--ps2_vusb/main.c54
-rw-r--r--ps2_vusb/ps2_usart.c325
-rw-r--r--ps2_vusb/sendchar_dummy.c8
-rw-r--r--ps2_vusb/sendchar_usart.c58
-rw-r--r--ps2_vusb/usart_print.c53
-rw-r--r--ps2_vusb/usart_print.h121
-rw-r--r--ps2_vusb/usbconfig.h12
11 files changed, 527 insertions, 269 deletions
diff --git a/Makefile.rules b/Makefile.rules
index 77bdf8846..96d1d7e1e 100644
--- a/Makefile.rules
+++ b/Makefile.rules
@@ -233,42 +233,6 @@ LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
233 233
234 234
235 235
236#---------------- Programming Options (avrdude) ----------------
237
238# Programming hardware
239# Type: avrdude -c ?
240# to get a full listing.
241#
242AVRDUDE_PROGRAMMER = stk500v2
243
244# com1 = serial port. Use lpt1 to connect to parallel port.
245AVRDUDE_PORT = com1 # programmer connected to serial device
246
247AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
248#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
249
250
251# Uncomment the following if you want avrdude's erase cycle counter.
252# Note that this counter needs to be initialized first using -Yn,
253# see avrdude manual.
254#AVRDUDE_ERASE_COUNTER = -y
255
256# Uncomment the following if you do /not/ wish a verification to be
257# performed after programming the device.
258#AVRDUDE_NO_VERIFY = -V
259
260# Increase verbosity level. Please use this when submitting bug
261# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
262# to submit bug reports.
263#AVRDUDE_VERBOSE = -v -v
264
265AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
266AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
267AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
268AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
269
270
271
272#---------------- Debugging Options ---------------- 236#---------------- Debugging Options ----------------
273 237
274# For simulavr only - target MCU frequency. 238# For simulavr only - target MCU frequency.
diff --git a/ps2.h b/ps2.h
index 7a8bbd266..a821acde4 100644
--- a/ps2.h
+++ b/ps2.h
@@ -56,6 +56,10 @@ POSSIBILITY OF SUCH DAMAGE.
56# error "PS/2 data port setting is required in config.h" 56# error "PS/2 data port setting is required in config.h"
57#endif 57#endif
58 58
59#define PS2_ACK 0xFA
60#define PS2_RESEND 0xFE
61#define PS2_SET_LED 0xED
62
59#define PS2_ERR_NONE 0 63#define PS2_ERR_NONE 0
60#define PS2_ERR_PARITY 0x10 64#define PS2_ERR_PARITY 0x10
61 65
diff --git a/ps2_vusb/Makefile b/ps2_vusb/Makefile
index 406008d84..6901d1f5c 100644
--- a/ps2_vusb/Makefile
+++ b/ps2_vusb/Makefile
@@ -12,8 +12,9 @@ TARGET_SRC = main.c \
12 keymap.c \ 12 keymap.c \
13 matrix.c \ 13 matrix.c \
14 led.c \ 14 led.c \
15 ps2.c \ 15 ps2_usart.c \
16 usart_print.c 16 sendchar_dummy.c
17# sendchar_usart.c
17 18
18OPT_DEFS = -DDEBUG_LEVEL=0 19OPT_DEFS = -DDEBUG_LEVEL=0
19 20
@@ -32,7 +33,7 @@ MCU = atmega168
32# so your program will run at the correct speed. You should also set this 33# so your program will run at the correct speed. You should also set this
33# variable to same clock speed. The _delay_ms() macro uses this, and many 34# variable to same clock speed. The _delay_ms() macro uses this, and many
34# examples use this variable to calculate timings. Do not add a "UL" here. 35# examples use this variable to calculate timings. Do not add a "UL" here.
35F_CPU = 16000000 36F_CPU = 20000000
36 37
37 38
38# Build Options 39# Build Options
@@ -43,5 +44,35 @@ MOUSEKEY_ENABLE = yes # Mouse keys
43#USB_NKRO_ENABLE = yes # USB Nkey Rollover 44#USB_NKRO_ENABLE = yes # USB Nkey Rollover
44 45
45 46
47
48#---------------- Programming Options (avrdude) ----------------
49# Type: avrdude -c ? to get a full listing.
50AVRDUDE_PROGRAMMER = usbasp
51AVRDUDE_PORT =
52AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
53#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
54
55# Uncomment the following if you want avrdude's erase cycle counter.
56# Note that this counter needs to be initialized first using -Yn,
57# see avrdude manual.
58#AVRDUDE_ERASE_COUNTER = -y
59
60# Uncomment the following if you do /not/ wish a verification to be
61# performed after programming the device.
62#AVRDUDE_NO_VERIFY = -V
63
64# Increase verbosity level. Please use this when submitting bug
65# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
66# to submit bug reports.
67#AVRDUDE_VERBOSE = -v -v
68
69#AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
70AVRDUDE_FLAGS = -p $(MCU) -c $(AVRDUDE_PROGRAMMER)
71AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
72AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
73AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
74
75
76
46include $(COMMON_DIR)/Makefile.vusb 77include $(COMMON_DIR)/Makefile.vusb
47include $(COMMON_DIR)/Makefile.common 78include $(COMMON_DIR)/Makefile.common
diff --git a/ps2_vusb/config.h b/ps2_vusb/config.h
index 858fe883f..9447dd936 100644
--- a/ps2_vusb/config.h
+++ b/ps2_vusb/config.h
@@ -3,7 +3,9 @@
3 3
4 4
5#define VENDOR_ID 0xFEED 5#define VENDOR_ID 0xFEED
6#define PRODUCT_ID 0x6512 6#define PRODUCT_ID 0x2233
7// TODO: share these strings with usbconfig.h
8// Edit usbconfig.h to change these.
7#define MANUFACTURER t.m.k. 9#define MANUFACTURER t.m.k.
8#define PRODUCT PS/2 keyboard converter 10#define PRODUCT PS/2 keyboard converter
9#define DESCRIPTION convert PS/2 keyboard to USB 11#define DESCRIPTION convert PS/2 keyboard to USB
@@ -33,36 +35,84 @@
33#define PS2_CLOCK_PORT PORTD 35#define PS2_CLOCK_PORT PORTD
34#define PS2_CLOCK_PIN PIND 36#define PS2_CLOCK_PIN PIND
35#define PS2_CLOCK_DDR DDRD 37#define PS2_CLOCK_DDR DDRD
36#define PS2_CLOCK_BIT 3 38#define PS2_CLOCK_BIT 4
37#define PS2_DATA_PORT PORTD 39#define PS2_DATA_PORT PORTD
38#define PS2_DATA_PIN PIND 40#define PS2_DATA_PIN PIND
39#define PS2_DATA_DDR DDRD 41#define PS2_DATA_DDR DDRD
40#define PS2_DATA_BIT 7 42#define PS2_DATA_BIT 0
41 43
42/* External interrupt for PS/2 clock line (optional) */ 44
43#define PS2_INT_ENABLE() do { \ 45// Synchronous USART is used to receive data from keyboard.
44 EIMSK |= (1<<INT1); \ 46// Use RXD pin for PS/2 DATA line and XCK for PS/2 CLOCK.
45 EICRA |= ((1<<ISC11) | (0<<ISC10)); \ 47// NOTE: This is recomended strongly if you use V-USB library.
46 EIFR |= (1<<INTF1); \ 48#define PS2_USE_USART
49
50// External or Pin Change Interrupt is used to receive data from keyboard.
51// Use INT1 or PCINTxx for PS/2 CLOCK line. see below.
52//#define PS2_USE_INT
53
54
55#ifdef PS2_USE_USART
56// synchronous, odd parity, 1-bit stop, 8-bit data, sample at falling edge
57// set DDR of CLOCK as input to be slave
58#define PS2_USART_INIT() do { \
59 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT); \
60 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT); \
61 UCSR0C = ((1 << UMSEL00) | \
62 (3 << UPM00) | \
63 (0 << USBS0) | \
64 (3 << UCSZ00) | \
65 (0 << UCPOL0)); \
66 UCSR0A = 0; \
67 UBRR0H = 0; \
68 UBRR0L = 0; \
47} while (0) 69} while (0)
70#define PS2_USART_RX_INT_ON() do { \
71 UCSR0B = ((1 << RXCIE0) | \
72 (1 << RXEN0)); \
73} while (0)
74#define PS2_USART_RX_POLL_ON() do { \
75 UCSR0B = (1 << RXEN0); \
76} while (0)
77#define PS2_USART_OFF() do { \
78 UCSR0C = 0; \
79 UCSR0B &= ~((1 << RXEN0) | \
80 (1 << TXEN0)); \
81} while (0)
82#define PS2_USART_RX_READY (UCSR0A & (1<<RXC0))
83#define PS2_USART_RX_DATA UDR0
84#define PS2_USART_ERROR (UCSR0A & ((1<<FE0) | (1<<DOR0) | (1<<UPE0)))
85#define PS2_USART_RX_VECT USART_RX_vect
86#endif
48 87
49#define PS2_INT_DISABLE() do { \ 88
89#ifdef PS2_USE_INT
90/* INT1
91#define PS2_INT_INIT() do { \
92 EICRA |= ((1<<ISC11) | \
93 (0<<ISC10)); \
94} while (0)
95#define PS2_INT_ON() do { \
96 EIMSK |= (1<<INT1); \
97} while (0)
98#define PS2_INT_OFF() do { \
50 EIMSK &= ~(1<<INT1); \ 99 EIMSK &= ~(1<<INT1); \
51} while (0) 100} while (0)
52#define PS2_INT_VECT INT1_vect 101#define PS2_INT_VECT INT1_vect
102*/
53 103
54/* Pin Change interrupt for PS/2 clock line (optional) 104/* PCINT20 */
55#define PS2_INT_ENABLE() do { \ 105#define PS2_INT_INIT() do { \
56 PCMSK2 |= (1<<PCINT22); \ 106 PCICR |= (1<<PCIE2); \
57 PCICR |= (1<<PCIE2); \
58 PCIFR |= (1<<PCIF2); \
59} while (0) 107} while (0)
60 108#define PS2_INT_ON() do { \
61#define PS2_INT_DISABLE() do { \ 109 PCMSK2 |= (1<<PCINT20); \
62 PCMSK2 &= ~(1<<PCINT22); \ 110} while (0)
63 PCICR &= ~(1<<PCIE); \ 111#define PS2_INT_OFF() do { \
112 PCMSK2 &= ~(1<<PCINT20); \
113 PCICR &= ~(1<<PCIE2); \
64} while (0) 114} while (0)
65#define PS2_INT_VECT PCINT2_vect 115#define PS2_INT_VECT PCINT2_vect
66*/ 116#endif
67 117
68#endif 118#endif
diff --git a/ps2_vusb/main.c b/ps2_vusb/main.c
index 5afbde01b..1ffe5e6ec 100644
--- a/ps2_vusb/main.c
+++ b/ps2_vusb/main.c
@@ -1,3 +1,7 @@
1/* PS/2 to USB keyboard converter
2 * 2011/02/20
3 * Copyright (c) 2011 tmk
4 */
1/* Name: main.c 5/* Name: main.c
2 * Project: hid-mouse, a very simple HID example 6 * Project: hid-mouse, a very simple HID example
3 * Author: Christian Starkjohann 7 * Author: Christian Starkjohann
@@ -8,65 +12,51 @@
8 * This Revision: $Id: main.c 790 2010-05-30 21:00:26Z cs $ 12 * This Revision: $Id: main.c 790 2010-05-30 21:00:26Z cs $
9 */ 13 */
10#include <stdint.h> 14#include <stdint.h>
11#include <avr/io.h>
12#include <avr/wdt.h> 15#include <avr/wdt.h>
13#include <avr/interrupt.h> /* for sei() */ 16#include <avr/interrupt.h>
14#include <avr/pgmspace.h> /* required by usbdrv.h */ 17#include <util/delay.h>
15#include <util/delay.h> /* for _delay_ms() */
16#include "usbdrv.h" 18#include "usbdrv.h"
17#include "usart_print.h" /* This is also an example for using debug macros */ 19#include "oddebug.h"
18#include "usb_keycodes.h"
19#include "matrix_skel.h"
20#include "keymap_skel.h"
21#include "mousekey.h"
22#include "layer.h"
23#include "print.h"
24#include "debug.h"
25#include "sendchar.h"
26#include "host.h"
27#include "host_vusb.h" 20#include "host_vusb.h"
28#include "timer.h"
29#include "led.h"
30#include "keyboard.h" 21#include "keyboard.h"
31 22
23
24#if 0
32#define DEBUGP_INIT() do { DDRC = 0xFF; } while (0) 25#define DEBUGP_INIT() do { DDRC = 0xFF; } while (0)
33#define DEBUGP(x) do { PORTC = x; } while (0) 26#define DEBUGP(x) do { PORTC = x; } while (0)
27#else
28#define DEBUGP_INIT()
29#define DEBUGP(x)
30#endif
31
34 32
35//static uint8_t last_led = 0;
36int main(void) 33int main(void)
37{ 34{
38 DEBUGP_INIT(); 35 DEBUGP_INIT();
39 wdt_enable(WDTO_1S); 36 wdt_enable(WDTO_1S);
40 /* Even if you don't use the watchdog, turn it off here. On newer devices,
41 * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
42 */
43
44 /* RESET status: all port bits are inputs without pull-up.
45 * That's the way we need D+ and D-. Therefore we don't need any
46 * additional hardware initialization.
47 */
48 odDebugInit(); 37 odDebugInit();
49 usbInit(); 38 usbInit();
50 39
51 print_enable = true;
52 debug_enable = true;
53 keyboard_init();
54
55 /* enforce re-enumeration, do this while interrupts are disabled! */ 40 /* enforce re-enumeration, do this while interrupts are disabled! */
56 usbDeviceDisconnect(); 41 usbDeviceDisconnect();
57 uint8_t i = 0; 42 uint8_t i = 0;
58 while(--i){ /* fake USB disconnect for > 250 ms */ 43 /* fake USB disconnect for > 250 ms */
44 while(--i){
59 wdt_reset(); 45 wdt_reset();
60 _delay_ms(1); 46 _delay_ms(1);
61 } 47 }
62 usbDeviceConnect(); 48 usbDeviceConnect();
63 sei(); 49 sei();
64 50
65 //uint8_t fn_bits = 0; 51 keyboard_init();
66 while (1) { /* main event loop */ 52
53 while (1) {
54 DEBUGP(0x1);
67 wdt_reset(); 55 wdt_reset();
68 usbPoll(); 56 usbPoll();
57 DEBUGP(0x2);
69 keyboard_proc(); 58 keyboard_proc();
59 DEBUGP(0x3);
70 host_vusb_keyboard_send(); 60 host_vusb_keyboard_send();
71 } 61 }
72} 62}
diff --git a/ps2_vusb/ps2_usart.c b/ps2_vusb/ps2_usart.c
new file mode 100644
index 000000000..e45b94c93
--- /dev/null
+++ b/ps2_vusb/ps2_usart.c
@@ -0,0 +1,325 @@
1/*
2Copyright (c) 2010,2011 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/*
38Primitive PS/2 Library for AVR
39==============================
40Host side is only supported now.
41Synchronous USART is used to receive data by hardware process
42rather than interrupt. During V-USB interrupt runs, CLOCK interrupt
43cannot interpose. In the result it is prone to lost CLOCK edge.
44
45
46I/O control
47-----------
48High state is asserted by internal pull-up.
49If you have a signaling problem, you may need to have
50external pull-up resisters on CLOCK and DATA line.
51
52
53PS/2 References
54---------------
55http://www.computer-engineering.org/ps2protocol/
56http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
57*/
58#include <stdbool.h>
59#include <avr/io.h>
60#include <avr/interrupt.h>
61#include <util/delay.h>
62#include "ps2.h"
63#include "debug.h"
64
65
66#if 0
67#define DEBUGP_INIT() do { DDRC = 0xFF; } while (0)
68#define DEBUGP(x) do { PORTC = x; } while (0)
69#else
70#define DEBUGP_INIT()
71#define DEBUGP(x)
72#endif
73
74#define WAIT(stat, us, err) do { \
75 if (!wait_##stat(us)) { \
76 ps2_error = err; \
77 goto ERROR; \
78 } \
79} while (0)
80
81
82uint8_t ps2_error = PS2_ERR_NONE;
83
84
85static inline void clock_lo(void);
86static inline void clock_hi(void);
87static inline bool clock_in(void);
88static inline void data_lo(void);
89static inline void data_hi(void);
90static inline bool data_in(void);
91static inline uint16_t wait_clock_lo(uint16_t us);
92static inline uint16_t wait_clock_hi(uint16_t us);
93static inline uint16_t wait_data_lo(uint16_t us);
94static inline uint16_t wait_data_hi(uint16_t us);
95static inline void idle(void);
96static inline void inhibit(void);
97#if defined PS2_USE_INT || defined PS2_USE_USART
98static inline uint8_t pbuf_dequeue(void);
99static inline void pbuf_enqueue(uint8_t data);
100#endif
101
102
103void ps2_host_init(void)
104{
105 DEBUGP_INIT();
106 DEBUGP(0x1);
107 idle();
108 PS2_USART_INIT();
109 PS2_USART_RX_INT_ON();
110}
111
112uint8_t ps2_host_send(uint8_t data)
113{
114 uint8_t res = 0;
115 bool parity = true;
116 ps2_error = PS2_ERR_NONE;
117
118 DEBUGP(0x6);
119 PS2_USART_OFF();
120
121 /* terminate a transmission if we have */
122 inhibit();
123 _delay_us(100);
124
125 /* start bit [1] */
126 data_lo();
127 clock_hi();
128 WAIT(clock_lo, 15000, 1);
129 /* data [2-9] */
130 for (uint8_t i = 0; i < 8; i++) {
131 _delay_us(15);
132 if (data&(1<<i)) {
133 parity = !parity;
134 data_hi();
135 } else {
136 data_lo();
137 }
138 WAIT(clock_hi, 50, 2);
139 WAIT(clock_lo, 50, 3);
140 }
141 /* parity [10] */
142 _delay_us(15);
143 if (parity) { data_hi(); } else { data_lo(); }
144 WAIT(clock_hi, 50, 4);
145 WAIT(clock_lo, 50, 5);
146 /* stop bit [11] */
147 _delay_us(15);
148 data_hi();
149 /* ack [12] */
150 WAIT(data_lo, 50, 6);
151 WAIT(clock_lo, 50, 7);
152
153 /* wait for idle state */
154 WAIT(clock_hi, 50, 8);
155 WAIT(data_hi, 50, 9);
156
157 res = ps2_host_recv_response();
158ERROR:
159 idle();
160 PS2_USART_INIT();
161 PS2_USART_RX_INT_ON();
162 return res;
163}
164
165// Do polling data from keyboard to get response to last command.
166uint8_t ps2_host_recv_response(void)
167{
168 uint8_t data = 0;
169 PS2_USART_INIT();
170 PS2_USART_RX_POLL_ON();
171 while (!PS2_USART_RX_READY)
172 ;
173 data = PS2_USART_RX_DATA;
174 PS2_USART_OFF();
175 DEBUGP(0x9);
176 return data;
177}
178
179uint8_t ps2_host_recv(void)
180{
181 return pbuf_dequeue();
182}
183
184ISR(PS2_USART_RX_VECT)
185{
186 DEBUGP(0x7);
187 uint8_t error = PS2_USART_ERROR;
188 uint8_t data = PS2_USART_RX_DATA;
189 if (error) {
190 DEBUGP(error>>2);
191 } else {
192 pbuf_enqueue(data);
193 }
194 DEBUGP(0x8);
195}
196
197/* send LED state to keyboard */
198void ps2_host_set_led(uint8_t led)
199{
200 // send 0xED then keyboard keeps waiting for next LED data
201 // and keyboard does not send any scan codes during waiting.
202 // If fail to send LED data keyboard looks like being freezed.
203 uint8_t retry = 3;
204 while (retry-- && ps2_host_send(PS2_SET_LED) != PS2_ACK)
205 ;
206 retry = 3;
207 while (retry-- && ps2_host_send(led) != PS2_ACK)
208 ;
209}
210
211
212/*--------------------------------------------------------------------
213 * static functions
214 *------------------------------------------------------------------*/
215static inline void clock_lo()
216{
217 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
218 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
219}
220static inline void clock_hi()
221{
222 /* input with pull up */
223 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
224 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
225}
226static inline bool clock_in()
227{
228 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
229 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
230 _delay_us(1);
231 return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
232}
233static inline void data_lo()
234{
235 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
236 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
237}
238static inline void data_hi()
239{
240 /* input with pull up */
241 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
242 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
243}
244static inline bool data_in()
245{
246 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
247 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
248 _delay_us(1);
249 return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
250}
251
252static inline uint16_t wait_clock_lo(uint16_t us)
253{
254 while (clock_in() && us) { asm(""); _delay_us(1); us--; }
255 return us;
256}
257static inline uint16_t wait_clock_hi(uint16_t us)
258{
259 while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
260 return us;
261}
262static inline uint16_t wait_data_lo(uint16_t us)
263{
264 while (data_in() && us) { asm(""); _delay_us(1); us--; }
265 return us;
266}
267static inline uint16_t wait_data_hi(uint16_t us)
268{
269 while (!data_in() && us) { asm(""); _delay_us(1); us--; }
270 return us;
271}
272
273/* idle state that device can send */
274static inline void idle(void)
275{
276 clock_hi();
277 data_hi();
278}
279
280/* inhibit device to send */
281static inline void inhibit(void)
282{
283 clock_lo();
284 data_hi();
285}
286
287
288/*--------------------------------------------------------------------
289 * Ring buffer to store scan codes from keyboard
290 *------------------------------------------------------------------*/
291#define PBUF_SIZE 8
292static uint8_t pbuf[PBUF_SIZE];
293static uint8_t pbuf_head = 0;
294static uint8_t pbuf_tail = 0;
295static inline void pbuf_enqueue(uint8_t data)
296{
297 if (!data)
298 return;
299
300 uint8_t sreg = SREG;
301 cli();
302 uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
303 if (next != pbuf_tail) {
304 pbuf[pbuf_head] = data;
305 pbuf_head = next;
306 } else {
307 debug("pbuf: full\n");
308 }
309 SREG = sreg;
310}
311
312static inline uint8_t pbuf_dequeue(void)
313{
314 uint8_t val = 0;
315
316 uint8_t sreg = SREG;
317 cli();
318 if (pbuf_head != pbuf_tail) {
319 val = pbuf[pbuf_tail];
320 pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
321 }
322 SREG = sreg;
323
324 return val;
325}
diff --git a/ps2_vusb/sendchar_dummy.c b/ps2_vusb/sendchar_dummy.c
new file mode 100644
index 000000000..8c9e583f1
--- /dev/null
+++ b/ps2_vusb/sendchar_dummy.c
@@ -0,0 +1,8 @@
1#include <stdint.h>
2#include "sendchar.h"
3
4
5int8_t sendchar(uint8_t c)
6{
7 return 1;
8}
diff --git a/ps2_vusb/sendchar_usart.c b/ps2_vusb/sendchar_usart.c
new file mode 100644
index 000000000..fe18177a3
--- /dev/null
+++ b/ps2_vusb/sendchar_usart.c
@@ -0,0 +1,58 @@
1#include <stdint.h>
2#include "oddebug.h"
3#include "sendchar.h"
4
5
6/* from oddebug.h */
7#if defined UBRR
8# define ODDBG_UBRR UBRR
9#elif defined UBRRL
10# define ODDBG_UBRR UBRRL
11#elif defined UBRR0
12# define ODDBG_UBRR UBRR0
13#elif defined UBRR0L
14# define ODDBG_UBRR UBRR0L
15#endif
16
17#if defined UCR
18# define ODDBG_UCR UCR
19#elif defined UCSRB
20# define ODDBG_UCR UCSRB
21#elif defined UCSR0B
22# define ODDBG_UCR UCSR0B
23#endif
24
25#if defined TXEN
26# define ODDBG_TXEN TXEN
27#else
28# define ODDBG_TXEN TXEN0
29#endif
30
31#if defined USR
32# define ODDBG_USR USR
33#elif defined UCSRA
34# define ODDBG_USR UCSRA
35#elif defined UCSR0A
36# define ODDBG_USR UCSR0A
37#endif
38
39#if defined UDRE
40# define ODDBG_UDRE UDRE
41#else
42# define ODDBG_UDRE UDRE0
43#endif
44
45#if defined UDR
46# define ODDBG_UDR UDR
47#elif defined UDR0
48# define ODDBG_UDR UDR0
49#endif
50
51
52/* from oddebug.c */
53int8_t sendchar(uint8_t c)
54{
55 while(!(ODDBG_USR & (1 << ODDBG_UDRE))); /* wait for data register empty */
56 ODDBG_UDR = c;
57 return 1;
58}
diff --git a/ps2_vusb/usart_print.c b/ps2_vusb/usart_print.c
deleted file mode 100644
index 2dfc949ba..000000000
--- a/ps2_vusb/usart_print.c
+++ /dev/null
@@ -1,53 +0,0 @@
1/* Name: oddebug.c
2 * Project: AVR library
3 * Author: Christian Starkjohann
4 * Creation Date: 2005-01-16
5 * Tabsize: 4
6 * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH
7 * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
8 * This Revision: $Id: oddebug.c 692 2008-11-07 15:07:40Z cs $
9 */
10
11#include "usart_print.h"
12#include "sendchar.h"
13
14
15int8_t sendchar(uint8_t c)
16{
17 while(!(ODDBG_USR & (1 << ODDBG_UDRE))); /* wait for data register empty */
18 ODDBG_UDR = c;
19 return 0;
20}
21
22void uartPutc(char c)
23{
24 while(!(ODDBG_USR & (1 << ODDBG_UDRE))); /* wait for data register empty */
25 ODDBG_UDR = c;
26}
27
28static uchar hexAscii(uchar h)
29{
30 h &= 0xf;
31 if(h >= 10)
32 h += 'a' - (uchar)10 - '0';
33 h += '0';
34 return h;
35}
36
37void printHex(uchar c)
38{
39 uartPutc(hexAscii(c >> 4));
40 uartPutc(hexAscii(c));
41}
42
43void odDebug(uchar prefix, uchar *data, uchar len)
44{
45 printHex(prefix);
46 uartPutc(':');
47 while(len--){
48 uartPutc(' ');
49 printHex(*data++);
50 }
51 uartPutc('\r');
52 uartPutc('\n');
53}
diff --git a/ps2_vusb/usart_print.h b/ps2_vusb/usart_print.h
deleted file mode 100644
index e6e83a081..000000000
--- a/ps2_vusb/usart_print.h
+++ /dev/null
@@ -1,121 +0,0 @@
1/* Name: oddebug.h
2 * Project: AVR library
3 * Author: Christian Starkjohann
4 * Creation Date: 2005-01-16
5 * Tabsize: 4
6 * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH
7 * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
8 * This Revision: $Id: oddebug.h 692 2008-11-07 15:07:40Z cs $
9 */
10
11#ifndef __oddebug_h_included__
12#define __oddebug_h_included__
13
14/*
15General Description:
16This module implements a function for debug logs on the serial line of the
17AVR microcontroller. Debugging can be configured with the define
18'DEBUG_LEVEL'. If this macro is not defined or defined to 0, all debugging
19calls are no-ops. If it is 1, DBG1 logs will appear, but not DBG2. If it is
202, DBG1 and DBG2 logs will be printed.
21
22A debug log consists of a label ('prefix') to indicate which debug log created
23the output and a memory block to dump in hex ('data' and 'len').
24*/
25
26
27#ifndef F_CPU
28# define F_CPU 12000000 /* 12 MHz */
29#endif
30
31/* make sure we have the UART defines: */
32#include "usbportability.h"
33
34#ifndef uchar
35# define uchar unsigned char
36#endif
37
38#if DEBUG_LEVEL > 0 && !(defined TXEN || defined TXEN0) /* no UART in device */
39# warning "Debugging disabled because device has no UART"
40# undef DEBUG_LEVEL
41#endif
42
43#ifndef DEBUG_LEVEL
44# define DEBUG_LEVEL 0
45#endif
46
47/* ------------------------------------------------------------------------- */
48
49#if DEBUG_LEVEL > 0
50# define DBG1(prefix, data, len) odDebug(prefix, data, len)
51#else
52# define DBG1(prefix, data, len)
53#endif
54
55#if DEBUG_LEVEL > 1
56# define DBG2(prefix, data, len) odDebug(prefix, data, len)
57#else
58# define DBG2(prefix, data, len)
59#endif
60
61/* ------------------------------------------------------------------------- */
62
63extern void odDebug(uchar prefix, uchar *data, uchar len);
64void uartPutc(char c);
65void printHex(uchar c);
66
67/* Try to find our control registers; ATMEL likes to rename these */
68
69#if defined UBRR
70# define ODDBG_UBRR UBRR
71#elif defined UBRRL
72# define ODDBG_UBRR UBRRL
73#elif defined UBRR0
74# define ODDBG_UBRR UBRR0
75#elif defined UBRR0L
76# define ODDBG_UBRR UBRR0L
77#endif
78
79#if defined UCR
80# define ODDBG_UCR UCR
81#elif defined UCSRB
82# define ODDBG_UCR UCSRB
83#elif defined UCSR0B
84# define ODDBG_UCR UCSR0B
85#endif
86
87#if defined TXEN
88# define ODDBG_TXEN TXEN
89#else
90# define ODDBG_TXEN TXEN0
91#endif
92
93#if defined USR
94# define ODDBG_USR USR
95#elif defined UCSRA
96# define ODDBG_USR UCSRA
97#elif defined UCSR0A
98# define ODDBG_USR UCSR0A
99#endif
100
101#if defined UDRE
102# define ODDBG_UDRE UDRE
103#else
104# define ODDBG_UDRE UDRE0
105#endif
106
107#if defined UDR
108# define ODDBG_UDR UDR
109#elif defined UDR0
110# define ODDBG_UDR UDR0
111#endif
112
113static inline void odDebugInit(void)
114{
115 ODDBG_UCR |= (1<<ODDBG_TXEN);
116 ODDBG_UBRR = F_CPU / (57600 * 16L) - 1;
117}
118
119/* ------------------------------------------------------------------------- */
120
121#endif /* __oddebug_h_included__ */
diff --git a/ps2_vusb/usbconfig.h b/ps2_vusb/usbconfig.h
index abc31ced5..34807066f 100644
--- a/ps2_vusb/usbconfig.h
+++ b/ps2_vusb/usbconfig.h
@@ -11,6 +11,8 @@
11#ifndef __usbconfig_h_included__ 11#ifndef __usbconfig_h_included__
12#define __usbconfig_h_included__ 12#define __usbconfig_h_included__
13 13
14#include "config.h"
15
14/* 16/*
15General Description: 17General Description:
16This file is an example configuration (with inline documentation) for the USB 18This file is an example configuration (with inline documentation) for the USB
@@ -27,7 +29,7 @@ section at the end of this file).
27/* This is the port where the USB bus is connected. When you configure it to 29/* This is the port where the USB bus is connected. When you configure it to
28 * "B", the registers PORTB, PINB and DDRB will be used. 30 * "B", the registers PORTB, PINB and DDRB will be used.
29 */ 31 */
30#define USB_CFG_DMINUS_BIT 4 32#define USB_CFG_DMINUS_BIT 3
31/* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected. 33/* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected.
32 * This may be any bit in the port. 34 * This may be any bit in the port.
33 */ 35 */
@@ -216,7 +218,7 @@ section at the end of this file).
216 218
217/* -------------------------- Device Description --------------------------- */ 219/* -------------------------- Device Description --------------------------- */
218 220
219#define USB_CFG_VENDOR_ID 0xc0, 0x16 /* = 0x16c0 = 5824 = voti.nl */ 221#define USB_CFG_VENDOR_ID (VENDOR_ID & 0xFF), ((VENDOR_ID >> 8) & 0xFF)
220/* USB vendor ID for the device, low byte first. If you have registered your 222/* USB vendor ID for the device, low byte first. If you have registered your
221 * own Vendor ID, define it here. Otherwise you may use one of obdev's free 223 * own Vendor ID, define it here. Otherwise you may use one of obdev's free
222 * shared VID/PID pairs. Be sure to read USB-IDs-for-free.txt for rules! 224 * shared VID/PID pairs. Be sure to read USB-IDs-for-free.txt for rules!
@@ -225,7 +227,7 @@ section at the end of this file).
225 * with libusb: 0x16c0/0x5dc. Use this VID/PID pair ONLY if you understand 227 * with libusb: 0x16c0/0x5dc. Use this VID/PID pair ONLY if you understand
226 * the implications! 228 * the implications!
227 */ 229 */
228#define USB_CFG_DEVICE_ID 0xdd, 0x05 /* = 0x05dc = 1500 */ 230#define USB_CFG_DEVICE_ID (PRODUCT_ID & 0xFF), ((PRODUCT_ID >> 8) & 0xFF)
229/* This is the ID of the product, low byte first. It is interpreted in the 231/* This is the ID of the product, low byte first. It is interpreted in the
230 * scope of the vendor ID. If you have registered your own VID with usb.org 232 * scope of the vendor ID. If you have registered your own VID with usb.org
231 * or if you have licensed a PID from somebody else, define it here. Otherwise 233 * or if you have licensed a PID from somebody else, define it here. Otherwise
@@ -249,8 +251,8 @@ section at the end of this file).
249 * obdev's free shared VID/PID pair. See the file USB-IDs-for-free.txt for 251 * obdev's free shared VID/PID pair. See the file USB-IDs-for-free.txt for
250 * details. 252 * details.
251 */ 253 */
252#define USB_CFG_DEVICE_NAME 'k', 'e', 'y', 'b', 'o', 'a', 'r', 'd' 254#define USB_CFG_DEVICE_NAME 'P', 'S', '/', '2', ' ', 'k', 'e', 'y', 'b', 'o', 'a', 'r', 'd', ' ', 'c', 'o', 'n', 'v', 'e', 'r', 't', 'e', 'r'
253#define USB_CFG_DEVICE_NAME_LEN 8 255#define USB_CFG_DEVICE_NAME_LEN 23
254/* Same as above for the device name. If you don't want a device name, undefine 256/* Same as above for the device name. If you don't want a device name, undefine
255 * the macros. See the file USB-IDs-for-free.txt before you assign a name if 257 * the macros. See the file USB-IDs-for-free.txt before you assign a name if
256 * you use a shared VID/PID. 258 * you use a shared VID/PID.