-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathps2adb.ino
476 lines (406 loc) · 11.1 KB
/
ps2adb.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
* Copyright (c) 2013 Michel Gallant. All rights reserved.
*
* adb_bus.h - Architecture-indpendent ADB bus defintions.
*
* HISTORY
* 5-Jan-2013 Michel Gallant at Foulab
* Created.
*/
#include <PS2Mouse.h>
#define PS2DATA 4
#define PS2CLOCK 3
#define ADB 1
//State machine states
#define INIT 1
#define ATTENTION 2
#define SYNC 3
#define GOTSTOPBIT 4
// Input compare interrupt on
#define ENABLE_CAP_INT TIMSK1 |= 0x20;
#define DISABLE_CAP_INT TIMSK1 &= ~0x20;
//ADB commands
#define TALK 3
#define LISTEN 2
// PS2 Mouse button butmasks
#define LBUTTON 1
#define RBUTTON 2
//Raw logic level of input.
volatile byte level;
//Time since last level change
volatile unsigned int time;
// By default, a mouse has address 3.
// This can be changed by the computer.
// It is stored in register 3.
#define INIT_DEVICE_ADDRESS 3
#define INIT_HANDLER_ID 1
// Data registers. Register 0 is mouse movement data.
// Register 3 is device address and status bits.
// Registers 1 and 2 are unused on most devices.
volatile byte reg0[2];
//byte reg3[2];
volatile byte state = INIT;
//TODO: Why does this seem backwards from the cmdBits struct below?
typedef struct {
byte reserved0:1, // bit 15, always 0
exceptionalEvent:1, // bit 14
serviceRequestEnable:1, // bit 13
reserved1:1, // bit 12, always 0
deviceAddress:4; // bits 11..8
byte handlerId:8; // bits 7..0
} adbRegister3bits;
typedef struct {
byte b[2];
} adbRegister3byte;
typedef union {
adbRegister3bits bits;
adbRegister3byte bytes;
} adbRegister3;
typedef struct{
byte reg:2, // Bits 0..1
cmd:2, // Bits 2..3
address:4; // Bits 4..7
}cmdBits;
typedef struct{
byte cmdByte;
}cmdByte;
typedef union{
cmdBits bits;
cmdByte bytes;
}cmd;
//typedef struct{
adbRegister3 reg3;
#define TLEN 256
//volatile unsigned int t[TLEN];
//volatile unsigned int l[TLEN];
//volatile unsigned int a[TLEN];
//volatile unsigned int* volatile times;
//volatile unsigned int* endtimes;
//volatile unsigned int* volatile levels;
//volatile unsigned int* endlevels;
volatile int working;
volatile cmd command;
volatile unsigned int syncs = 0;
volatile unsigned int attens = 0;
volatile unsigned long int acalls = 0;
volatile unsigned long int aloops = 0;
volatile byte pending = 0;
PS2Mouse mouse( PS2CLOCK, PS2DATA );
void setup(){
/*times = t;
endtimes = times + TLEN;
levels = l;
endlevels = levels + TLEN;*/
Serial.begin(115200);
mouse.initialize();
reg3.bytes.b[0] = 0;
reg3.bytes.b[1] = 0;
reg3.bits.deviceAddress = INIT_DEVICE_ADDRESS;
reg3.bits.handlerId = INIT_HANDLER_ID;
Serial.print("Starting ADB. Device address: ");
Serial.print( reg3.bits.deviceAddress );
Serial.print(" Handler ID: " );
Serial.println( reg3.bits.handlerId );
// Set data direction in
DDRB &= ~ADB;
// Enable weak pullup
PORTB |= ADB;
// Set up timer-counter #1
TCCR1A = 0x00;
// Input compare noise canceller on,
// Falling edge input compare interrupt,
// clock prescaler = 8
TCCR1B = 0x82;
TCCR1C = 0x00;
TCNT1= 0x00;
// Input compare interrupt on
ENABLE_CAP_INT
// If ADB input is high, set falling-edge interrupt.
// Otherwise, set rising-edge input
//(level = PORTB & ADB);//? TCCR1B &= 0xBF: TCCR1B |= 0x40;
TCCR1B |= 0x40;
working = 1;
}
void loop(){
//int data[3];
//printMouseReport(data);
//delay(200);
//Serial.println( "Waiting for HIGH" );
if( !working ){
Serial.println( "DONE WORKING");
/*for( int i =0; i < TLEN; i++){
Serial.print( " Level : " );
Serial.print( l[i] ? "H" : "L" );
Serial.print( " Time: " );
Serial.println( t[i] );
}*/
Serial.print( "Syncs, attentions, acalls, aloops:: " );
Serial.print( syncs );
Serial.print(", ");
Serial.print( attens );
Serial.print(", ");
Serial.print( acalls );
Serial.print(", ");
Serial.println( aloops );
while(1);
}
getHigh();
getAttentionSignal();
//Serial.println(time);
if( state != ATTENTION ) return;
getSyncSignal();
if( state != SYNC ) return;
//Serial.println( "Got sync!" );
// Get command byte
for( int i = 0; i < 8; i++ ){
byte b = getBit();
if ( b > 1 ) return;
command.bytes.cmdByte = ( command.bytes.cmdByte << 1 ) + b;
}
/*command.bits.reg = 2;
Serial.println( command.bits.address );
Serial.println( command.bits.cmd );
Serial.println( command.bits.reg );
Serial.println( command.bytes.cmdByte, HEX );
Serial.println( reg3.bits.deviceAddress );
Serial.print( reg3.bytes.b[0], HEX );
Serial.println( reg3.bytes.b[1], HEX );*/
if( command.bits.address == reg3.bits.deviceAddress ){
//Serial.println("ME!");
//pending = 1;
if( command.bits.cmd == TALK && command.bits.reg == 0 && pending ){
getStopAndTlt();
//Serial.print( "Got Tlt. time = " );
//Serial.println( time );
sendMouse();
}
//TODO: Handle LISTEN and FLUSH here
}
//TODO: Handle global commands here
// Check for new movement/ buttons from the mouse.
// If there is data pending, don't check, because that would
// overwrite the currently pending data. The mouse
// should accumulate any extra movement that happens
// during this time.
if( !pending ) checkMouse();
/*Serial.print( "cb " );
Serial.println( command.bytes.cmdByte, HEX );*/
}
void printMouseReport(int data[]){
mouse.report(data);
Serial.print( "RAW: ");
Serial.print( data[0], HEX );
Serial.print(" Status: X - ");
Serial.print( data[0]& 1<<4 ? data[1] : -data[1] );
Serial.print(" Y - ");
Serial.print( data[0] & 1<<5 ? data[2] : -data[2] );
Serial.print(" L - ");
Serial.print( (data[0] & 1<<0) ? "D" : "U" );
Serial.print(" R - ");
Serial.println( data[0] & 1<<1 ? "D" : "U" );
}
void getHigh(){
while( !level );
return;
//asm("cli");
}
void getAttentionSignal(){
while( !level ){
aloops++;
};
acalls++;
//TODO: Attention time on the IIgs seems longer.
//Increase to 827 or more... maybe 850?
if( time > 776 && time < 850 ){
state = ATTENTION;
attens++;
}
}
void getSyncSignal(){
while( level );
if( time > 67 && time < 80 ){
state = SYNC;
syncs++;
}
}
byte getBit(){
// b is the candidate - what we suspect the bit will be.
// If both the high and low times match the protocol spec,
// return b, otherwise return error.
byte b;
while( !level );
if( time < 50 ){
b = 1;
}else if( time >= 50 && time < 72 ){
b = 0;
}else{
return 2;
}
while( level );
if( b == 1 && time >= 50 && time < 72 ){
return 1;
}else if( b == 0 && time < 50 ){
return 0;
}else{
return 2;
}
}
ISR(TIMER1_CAPT_vect){
// Disable interrupts
asm("cli");
// Clear counter register
TCNT1 = 0;
// Save line level to level value
//level = PORTB & ADB;
// Save match register to time value
// The timer ticks twice per microsecond,
// so dividing by two gives us actual microseconds
time = ICR1 >> 1;
// Set next interrupt to happen on appropriate
// rising or falling edge
//level ? TCCR1B &= 0xBF: TCCR1B |= 0x40;
level = (TCCR1B & 0x40);
TCCR1B ^= 0x40;
//*(times++) = time;
//*(levels++) = !level;
/*if( levels == endlevels ){
working = 0;
TIMSK1&= ~0x20;
//times = t;
//levels = l;
}*/
// Enable interrupts
asm("sei");
//Serial.println( time );
}
void getStopAndTlt(){
while( !level );
//If we have a 0, this is the stop bit
if( time > 50 && time < 65 ){
state = GOTSTOPBIT;
// If time is around 300 uS low, then some other device
// has requested service and we can't transmit
}else if( time > 280 && time < 330 ){
state = INIT;
}else{
//TODO: probably should be COLLISION not INIT
state = INIT;
}
// Delay until Tlt is reached
// 200mS, which is 400 counter counts
while( level && TCNT1 < 400 );
}
void sendMouse(){
// Disable timer interrupts
DISABLE_CAP_INT
//start bit
sendBit( 1 );
sendByte( reg0[0] );
sendByte( reg0[1] );
//sendByte( /*reg0[0] */ 0x00 );
//sendByte( /*reg0[1]*/ 0x80 );
//stop bit
sendBit( 0 );
pending = 0;
//Serial.println("M");
//re-enable timer interrupt
ENABLE_CAP_INT
}
//TODO: ADD COLLISION DETECTION HERE!
inline void sendBit( byte b ){
if(b){
goLow();
delayMicroseconds(33);
goHigh();
delayMicroseconds(61);
}else{
goLow();
delayMicroseconds(61);
goHigh();
delayMicroseconds(33);
}
}
void sendByte( byte b ){
// Send a byte MSB first.
for( int i = 0; i < 8; i ++ ){
sendBit( b & 0x80 );
b = b << 1;
}
}
inline void goLow(){
//Turn off pullup
PORTB &= ~ADB;
// Set direction output
DDRB |= ADB;
}
inline void goHigh(){
// Set direction input
DDRB &= ~ADB;
//Turn on pullup
PORTB |= ADB;
}
// Check for new movement/ buttons from the mouse.
// Transfer to to register 0 and set the
// pending flag if there is new data.
void checkMouse(){
static byte oldbuttons = 0;
static byte buttons = 0;
int data[3];
// Ignore the ADB bus while we read the PS2 mouse.
// We don't care what's happening there.
DISABLE_CAP_INT
//2 mS
mouse.report(data);
// Looks like the mouse library already sign-extends x and y values
// Now we just downconvert them to 7 bit values,
// capping them if they would otherwise overflow 7 bits
if( data [1] < -64 ) data[1]=-64;
else if( data[1] > 63 ) data[1] = 63;
// Vertical axis seems reversed from PS2 mouse
data[2] = -data[2];
if( data [2] < -64 ) data[2]=-64;
else if( data[2] > 63 ) data[2] = 63;
// Move Y value into reg 0 byte 1
reg0[0] = data[2] & 0x7F;
// Move x value into reg 0 byte 2
reg0[1] = data[1] & 0x7F;
// TODO: check for button changes here
buttons = data[0] & 0x03;
if( reg0[0] || reg0[1] || buttons != oldbuttons ){
pending = 1;
Serial.print( "Got mouse event. x: ");
Serial.print( data[1] );
Serial.print( ", y: ");
Serial.print( data[2] );
Serial.print( ", btns: " );
Serial.println( buttons );
}
if ( !(buttons & LBUTTON) ) reg0[0] |= 0x80;
if ( !(buttons & RBUTTON) ) reg0[1] |= 0x80;
oldbuttons = buttons;
ENABLE_CAP_INT
}