Skip to content

Commit ef6478a

Browse files
committed
core: Add adb_host_talk()
1 parent 5c665b4 commit ef6478a

File tree

2 files changed

+70
-52
lines changed

2 files changed

+70
-52
lines changed

protocol/adb.c

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ static inline void place_bit1(void);
6060
static inline void send_byte(uint8_t data);
6161
static inline uint16_t wait_data_lo(uint16_t us);
6262
static inline uint16_t wait_data_hi(uint16_t us);
63-
static inline uint16_t adb_host_dev_recv(uint8_t device);
6463

6564

6665
void adb_host_init(void)
@@ -87,49 +86,9 @@ bool adb_host_psw(void)
8786
* <http://geekhack.org/index.php?topic=14290.msg1068919#msg1068919>
8887
* <http://geekhack.org/index.php?topic=14290.msg1070139#msg1070139>
8988
*/
90-
91-
// ADB Bit Cells
92-
//
93-
// bit cell time: 70-130us
94-
// low part of bit0: 60-70% of bit cell
95-
// low part of bit1: 30-40% of bit cell
96-
//
97-
// bit cell time 70us 130us
98-
// --------------------------------------------
99-
// low part of bit0 42-49 78-91
100-
// high part of bit0 21-28 39-52
101-
// low part of bit1 21-28 39-52
102-
// high part of bit1 42-49 78-91
103-
//
104-
//
105-
// bit0:
106-
// 70us bit cell:
107-
// ____________~~~~~~
108-
// 42-49 21-28
109-
//
110-
// 130us bit cell:
111-
// ____________~~~~~~
112-
// 78-91 39-52
113-
//
114-
// bit1:
115-
// 70us bit cell:
116-
// ______~~~~~~~~~~~~
117-
// 21-28 42-49
118-
//
119-
// 130us bit cell:
120-
// ______~~~~~~~~~~~~
121-
// 39-52 78-91
122-
//
123-
// [from Apple IIgs Hardware Reference Second Edition]
124-
125-
enum {
126-
ADDR_KEYB = 0x20,
127-
ADDR_MOUSE = 0x30
128-
};
129-
13089
uint16_t adb_host_kbd_recv(void)
13190
{
132-
return adb_host_dev_recv(ADDR_KEYB);
91+
return adb_host_talk(ADB_ADDR_KEYBOARD, ADB_REG_0);
13392
}
13493

13594
#ifdef ADB_MOUSE_ENABLE
@@ -139,16 +98,16 @@ void adb_mouse_init(void) {
13998

14099
uint16_t adb_host_mouse_recv(void)
141100
{
142-
return adb_host_dev_recv(ADDR_MOUSE);
101+
return adb_host_talk(ADB_ADDR_MOUSE, ADB_REG_0);
143102
}
144103
#endif
145104

146-
static inline uint16_t adb_host_dev_recv(uint8_t device)
105+
uint16_t adb_host_talk(uint8_t addr, uint8_t reg)
147106
{
148107
uint16_t data = 0;
149108
cli();
150109
attention();
151-
send_byte(device|0x0C); // Addr:Keyboard(0010)/Mouse(0011), Cmd:Talk(11), Register0(00)
110+
send_byte((addr<<4) | (ADB_CMD_TALK<<2) | reg);
152111
place_bit0(); // Stopbit(0)
153112
if (!wait_data_hi(500)) { // Service Request(310us Adjustable Keyboard): just ignored
154113
sei();
@@ -158,20 +117,20 @@ static inline uint16_t adb_host_dev_recv(uint8_t device)
158117
sei();
159118
return 0; // No data to send
160119
}
161-
120+
162121
uint8_t n = 17; // start bit + 16 data bits
163122
do {
164123
uint8_t lo = (uint8_t) wait_data_hi(130);
165124
if (!lo)
166125
goto error;
167-
126+
168127
uint8_t hi = (uint8_t) wait_data_lo(lo);
169128
if (!hi)
170129
goto error;
171-
130+
172131
hi = lo - hi;
173132
lo = 130 - lo;
174-
133+
175134
data <<= 1;
176135
if (lo < hi) {
177136
data |= 1;
@@ -205,7 +164,7 @@ void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l)
205164
place_bit0(); // Stopbit(0)
206165
_delay_us(200); // Tlt/Stop to Start
207166
place_bit1(); // Startbit(1)
208-
send_byte(data_h);
167+
send_byte(data_h);
209168
send_byte(data_l);
210169
place_bit0(); // Stopbit(0);
211170
sei();
@@ -375,7 +334,7 @@ Commands
375334
A A A A 1 1 R R Talk(read from a device)
376335
377336
The command to read keycodes from keyboard is 0x2C which
378-
consist of keyboard address 2 and Talk against register 0.
337+
consist of keyboard address 2 and Talk against register 0.
379338
380339
Address:
381340
2: keyboard
@@ -457,7 +416,7 @@ Keyboard Data(Register0)
457416
Keyboard LEDs & state of keys(Register2)
458417
This register hold current state of three LEDs and nine keys.
459418
The state of LEDs can be changed by sending Listen command.
460-
419+
461420
1514 . . . . . . 7 6 5 . 3 2 1 0
462421
| | | | | | | | | | | | | | | +- LED1(NumLock)
463422
| | | | | | | | | | | | | | +--- LED2(CapsLock)
@@ -474,5 +433,46 @@ Keyboard LEDs & state of keys(Register2)
474433
| +----------------------------- Delete
475434
+------------------------------- Reserved
476435
436+
ADB Bit Cells
437+
bit cell time: 70-130us
438+
low part of bit0: 60-70% of bit cell
439+
low part of bit1: 30-40% of bit cell
440+
441+
bit cell time 70us 130us
442+
--------------------------------------------
443+
low part of bit0 42-49 78-91
444+
high part of bit0 21-28 39-52
445+
low part of bit1 21-28 39-52
446+
high part of bit1 42-49 78-91
447+
448+
449+
bit0:
450+
70us bit cell:
451+
____________~~~~~~
452+
42-49 21-28
453+
454+
130us bit cell:
455+
____________~~~~~~
456+
78-91 39-52
457+
458+
bit1:
459+
70us bit cell:
460+
______~~~~~~~~~~~~
461+
21-28 42-49
462+
463+
130us bit cell:
464+
______~~~~~~~~~~~~
465+
39-52 78-91
466+
467+
[from Apple IIgs Hardware Reference Second Edition]
468+
469+
Keyboard Handle ID
470+
Apple Standard Keyboard M0116: 0x01
471+
Apple Extended Keyboard M0115: 0x02
472+
Apple Extended Keyboard II M3501: 0x02
473+
Apple Adjustable Keybaord: 0x10
474+
475+
http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L802
476+
477477
END_OF_ADB
478478
*/

protocol/adb.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,29 @@ POSSIBILITY OF SUCH DAMAGE.
5252
#define ADB_CAPS 0x39
5353

5454

55+
/* ADB commands */
56+
#define ADB_ADDR_KEYBOARD 2
57+
#define ADB_ADDR_MOUSE 3
58+
#define ADB_CMD_LISTEN 2
59+
#define ADB_CMD_TALK 3
60+
#define ADB_REG_0 0
61+
#define ADB_REG_1 1
62+
#define ADB_REG_2 2
63+
#define ADB_REG_3 3
64+
65+
/* ADB keyboard handle id */
66+
#define ADB_HANDLE_M0116 0x01
67+
#define ADB_HANDLE_M0115 0x02
68+
#define ADB_HANDLE_M3501 0x02
69+
#define ADB_HANDLE_M1242 0x10
70+
71+
5572
// ADB host
5673
void adb_host_init(void);
5774
bool adb_host_psw(void);
5875
uint16_t adb_host_kbd_recv(void);
5976
uint16_t adb_host_mouse_recv(void);
77+
uint16_t adb_host_talk(uint8_t addr, uint8_t reg);
6078
void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l);
6179
void adb_host_kbd_led(uint8_t led);
6280
void adb_mouse_task(void);

0 commit comments

Comments
 (0)