You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

adb.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. Copyright 2011 Jun WAKO <[email protected]>
  3. This software is licensed with a Modified BSD License.
  4. All of this is supposed to be Free Software, Open Source, DFSG-free,
  5. GPL-compatible, and OK to use in both free and proprietary applications.
  6. Additions and corrections to this file are welcome.
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the
  14. distribution.
  15. * Neither the name of the copyright holders nor the names of
  16. contributors may be used to endorse or promote products derived
  17. from this software without specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdbool.h>
  31. #include <util/delay.h>
  32. #include <avr/io.h>
  33. #include <avr/interrupt.h>
  34. #include "adb.h"
  35. #include "debug.h"
  36. static inline void data_lo(void);
  37. static inline void data_hi(void);
  38. static inline bool data_in(void);
  39. #ifdef ADB_PSW_BIT
  40. static inline void psw_lo(void);
  41. static inline void psw_hi(void);
  42. static inline bool psw_in(void);
  43. #endif
  44. static inline void attention(void);
  45. static inline void place_bit0(void);
  46. static inline void place_bit1(void);
  47. static inline void send_byte(uint8_t data);
  48. static inline bool read_bit(void);
  49. static inline uint8_t read_byte(void);
  50. static inline uint8_t wait_data_lo(uint16_t us);
  51. static inline uint8_t wait_data_hi(uint8_t us);
  52. void adb_host_init(void)
  53. {
  54. data_hi();
  55. #ifdef ADB_PSW_BIT
  56. psw_hi();
  57. #endif
  58. // Enable keyboard left/right modifier distinction
  59. // Addr:Keyboard(0010), Cmd:Listen(10), Register3(11)
  60. // upper byte: reserved bits 0000, device address 0010
  61. // lower byte: device handler 00000011
  62. adb_host_listen(0x2B,0x02,0x03);
  63. }
  64. #ifdef ADB_PSW_BIT
  65. bool adb_host_psw(void)
  66. {
  67. return psw_in();
  68. }
  69. #endif
  70. /*
  71. * Don't call this in a row without the delay, otherwise it makes some of poor controllers
  72. * overloaded and misses strokes. Recommended interval is 12ms.
  73. *
  74. * Thanks a lot, blargg!
  75. * <http://geekhack.org/index.php?topic=14290.msg1068919#msg1068919>
  76. * <http://geekhack.org/index.php?topic=14290.msg1070139#msg1070139>
  77. */
  78. uint16_t adb_host_kbd_recv(void)
  79. {
  80. uint16_t data = 0;
  81. attention();
  82. send_byte(0x2C); // Addr:Keyboard(0010), Cmd:Talk(11), Register0(00)
  83. place_bit0(); // Stopbit(0)
  84. if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us)
  85. return 0; // No data to send
  86. }
  87. if (!read_bit()) { // Startbit(1)
  88. // Service Request
  89. dprintf("Startbit ERROR\n");
  90. return -2;
  91. }
  92. // ad hoc fix: without block inerrupt read wrong bit occasionally and get keys stuck
  93. cli();
  94. data = read_byte();
  95. data = (data<<8) | read_byte();
  96. uint8_t stop = read_bit(); // Stopbit(0)
  97. sei();
  98. if (stop) {
  99. dprintf("Stopbit ERROR\n");
  100. return -3;
  101. }
  102. return data;
  103. }
  104. void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l)
  105. {
  106. attention();
  107. send_byte(cmd);
  108. place_bit0(); // Stopbit(0)
  109. _delay_us(200); // Tlt/Stop to Start
  110. place_bit1(); // Startbit(1)
  111. send_byte(data_h);
  112. send_byte(data_l);
  113. place_bit0(); // Stopbit(0);
  114. }
  115. // send state of LEDs
  116. void adb_host_kbd_led(uint8_t led)
  117. {
  118. // Addr:Keyboard(0010), Cmd:Listen(10), Register2(10)
  119. // send upper byte (not used)
  120. // send lower byte (bit2: ScrollLock, bit1: CapsLock, bit0:
  121. adb_host_listen(0x2A,0,led&0x07);
  122. }
  123. static inline void data_lo()
  124. {
  125. ADB_DDR |= (1<<ADB_DATA_BIT);
  126. ADB_PORT &= ~(1<<ADB_DATA_BIT);
  127. }
  128. static inline void data_hi()
  129. {
  130. ADB_PORT |= (1<<ADB_DATA_BIT);
  131. ADB_DDR &= ~(1<<ADB_DATA_BIT);
  132. }
  133. static inline bool data_in()
  134. {
  135. ADB_PORT |= (1<<ADB_DATA_BIT);
  136. ADB_DDR &= ~(1<<ADB_DATA_BIT);
  137. return ADB_PIN&(1<<ADB_DATA_BIT);
  138. }
  139. #ifdef ADB_PSW_BIT
  140. static inline void psw_lo()
  141. {
  142. ADB_DDR |= (1<<ADB_PSW_BIT);
  143. ADB_PORT &= ~(1<<ADB_PSW_BIT);
  144. }
  145. static inline void psw_hi()
  146. {
  147. ADB_PORT |= (1<<ADB_PSW_BIT);
  148. ADB_DDR &= ~(1<<ADB_PSW_BIT);
  149. }
  150. static inline bool psw_in()
  151. {
  152. ADB_PORT |= (1<<ADB_PSW_BIT);
  153. ADB_DDR &= ~(1<<ADB_PSW_BIT);
  154. return ADB_PIN&(1<<ADB_PSW_BIT);
  155. }
  156. #endif
  157. static inline void attention(void)
  158. {
  159. data_lo();
  160. _delay_us(700);
  161. place_bit1();
  162. }
  163. static inline void place_bit0(void)
  164. {
  165. data_lo();
  166. _delay_us(65);
  167. data_hi();
  168. _delay_us(35);
  169. }
  170. static inline void place_bit1(void)
  171. {
  172. data_lo();
  173. _delay_us(35);
  174. data_hi();
  175. _delay_us(65);
  176. }
  177. static inline void send_byte(uint8_t data)
  178. {
  179. for (int i = 0; i < 8; i++) {
  180. if (data&(0x80>>i))
  181. place_bit1();
  182. else
  183. place_bit0();
  184. }
  185. }
  186. static inline bool read_bit(void)
  187. {
  188. // ADB Bit Cells
  189. //
  190. // bit cell time: 70-130us
  191. // low part of bit0: 60-70% of bit cell
  192. // low part of bit1: 30-40% of bit cell
  193. //
  194. // bit cell time 70us 130us
  195. // --------------------------------------------
  196. // low part of bit0 42-49 78-91
  197. // high part of bit0 21-28 39-52
  198. // low part of bit1 21-28 39-52
  199. // high part of bit1 42-49 78-91
  200. //
  201. //
  202. // bit0:
  203. // 70us bit cell:
  204. // ____________~~~~~~
  205. // 42-49 21-28
  206. //
  207. // 130us bit cell:
  208. // ____________~~~~~~
  209. // 78-91 39-52
  210. //
  211. // bit1:
  212. // 70us bit cell:
  213. // ______~~~~~~~~~~~~
  214. // 21-28 42-49
  215. //
  216. // 130us bit cell:
  217. // ______~~~~~~~~~~~~
  218. // 39-52 78-91
  219. //
  220. // read:
  221. // ________|~~~~~~~~~
  222. // 55us
  223. // Read data line after 55us. If data line is low/high then bit is 0/1.
  224. // This method might not work at <90us bit cell time.
  225. //
  226. // [from Apple IIgs Hardware Reference Second Edition]
  227. bool bit;
  228. wait_data_lo(75); // wait the start of bit cell at least 130ms(55+0+75)
  229. _delay_us(55);
  230. bit = data_in();
  231. wait_data_hi(36); // wait high part of bit cell at least 91ms(55+36)
  232. return bit;
  233. }
  234. static inline uint8_t read_byte(void)
  235. {
  236. uint8_t data = 0;
  237. for (int i = 0; i < 8; i++) {
  238. data <<= 1;
  239. if (read_bit())
  240. data = data | 1;
  241. }
  242. return data;
  243. }
  244. static inline uint8_t wait_data_lo(uint16_t us)
  245. {
  246. while (data_in() && us) {
  247. _delay_us(1);
  248. us--;
  249. }
  250. return us;
  251. }
  252. static inline uint8_t wait_data_hi(uint8_t us)
  253. {
  254. while (!data_in() && us) {
  255. _delay_us(1);
  256. us--;
  257. }
  258. return us;
  259. }
  260. /*
  261. ADB Protocol
  262. ============
  263. Resources
  264. ---------
  265. ADB - The Untold Story: Space Aliens Ate My Mouse
  266. http://developer.apple.com/legacy/mac/library/#technotes/hw/hw_01.html
  267. ADB Manager
  268. http://developer.apple.com/legacy/mac/library/documentation/mac/pdf/Devices/ADB_Manager.pdf
  269. Service request(5-17)
  270. Apple IIgs Hardware Reference Second Edition [Chapter6 p121]
  271. ftp://ftp.apple.asimov.net/pub/apple_II/documentation/Apple%20IIgs%20Hardware%20Reference.pdf
  272. ADB Keycode
  273. http://72.0.193.250/Documentation/macppc/adbkeycodes/
  274. http://m0115.web.fc2.com/m0115.jpg
  275. [Inside Macintosh volume V, pages 191-192]
  276. http://www.opensource.apple.com/source/IOHIDFamily/IOHIDFamily-421.18.3/IOHIDFamily/Cosmo_USB2ADB.c
  277. ADB Signaling
  278. http://kbdbabel.sourceforge.net/doc/kbd_signaling_pcxt_ps2_adb.pdf
  279. ADB Overview & History
  280. http://en.wikipedia.org/wiki/Apple_Desktop_Bus
  281. Microchip Application Note: ADB device(with code for PIC16C)
  282. http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011062
  283. AVR ATtiny2131 ADB to PS/2 converter(Japanese)
  284. http://hp.vector.co.jp/authors/VA000177/html/KeyBoardA5DEA5CBA5A2II.html
  285. Pinouts
  286. -------
  287. ADB female socket from the front:
  288. __________
  289. | | <--- top
  290. | 4o o3 |
  291. |2o o1|
  292. | == |
  293. |________| <--- bottom
  294. | | <--- 4pins
  295. ADB female socket from bottom:
  296. ========== <--- front
  297. | |
  298. | |
  299. |2o o1|
  300. |4o o3|
  301. ---------- <--- back
  302. 1: Data
  303. 2: Power SW(low when press Power key)
  304. 3: Vcc(5V)
  305. 4: GND
  306. Commands
  307. --------
  308. ADB command is 1byte and consists of 4bit-address, 2bit-command
  309. type and 2bit-register. The commands are always sent by Host.
  310. Command format:
  311. 7 6 5 4 3 2 1 0
  312. | | | |------------ address
  313. | |-------- command type
  314. | |---- register
  315. bits commands
  316. ------------------------------------------------------
  317. - - - - 0 0 0 0 Send Request(reset all devices)
  318. A A A A 0 0 0 1 Flush(reset a device)
  319. - - - - 0 0 1 0 Reserved
  320. - - - - 0 0 1 1 Reserved
  321. - - - - 0 1 - - Reserved
  322. A A A A 1 0 R R Listen(write to a device)
  323. A A A A 1 1 R R Talk(read from a device)
  324. The command to read keycodes from keyboard is 0x2C which
  325. consist of keyboard address 2 and Talk against register 0.
  326. Address:
  327. 2: keyboard
  328. 3: mice
  329. Registers:
  330. 0: application(keyobard uses this to store its data.)
  331. 1: application
  332. 2: application(keyboard uses this for LEDs and state of modifiers)
  333. 3: status and command
  334. Communication
  335. -------------
  336. This is a minimum information for keyboard communication.
  337. See "Resources" for detail.
  338. Signaling:
  339. ~~~~____________~~||||||||||||__~~~~~_~~|||||||||||||||__~~~~
  340. |800us | |7 Command 0| | | |15-64 Data 0|Stopbit(0)
  341. +Attention | | | +Startbit(1)
  342. +Startbit(1) | +Tlt(140-260us)
  343. +stopbit(0)
  344. Bit cells:
  345. bit0: ______~~~
  346. 65 :35us
  347. bit1: ___~~~~~~
  348. 35 :65us
  349. bit0 low time: 60-70% of bit cell(42-91us)
  350. bit1 low time: 30-40% of bit cell(21-52us)
  351. bit cell time: 70-130us
  352. [from Apple IIgs Hardware Reference Second Edition]
  353. Criterion for bit0/1:
  354. After 55us if line is low/high then bit is 0/1.
  355. Attention & start bit:
  356. Host asserts low in 560-1040us then places start bit(1).
  357. Tlt(Stop to Start):
  358. Bus stays high in 140-260us then device places start bit(1).
  359. Global reset:
  360. Host asserts low in 2.8-5.2ms. All devices are forced to reset.
  361. Service request from device(Srq):
  362. Device can request to send at commad(Global only?) stop bit.
  363. Requesting device keeps low for 140-260us at stop bit of command.
  364. Keyboard Data(Register0)
  365. This 16bit data can contains two keycodes and two released flags.
  366. First keycode is palced in upper byte. When one keyocode is sent,
  367. lower byte is 0xFF.
  368. Release flag is 1 when key is released.
  369. 1514 . . . . . 8 7 6 . . . . . 0
  370. | | | | | | | | | +-+-+-+-+-+-+- Keycode2
  371. | | | | | | | | +--------------- Released2(1 when the key is released)
  372. | +-+-+-+-+-+-+----------------- Keycode1
  373. +------------------------------- Released1(1 when the key is released)
  374. Keycodes:
  375. Scancode consists of 7bit keycode and 1bit release flag.
  376. Device can send two keycodes at once. If just one keycode is sent
  377. keycode1 contains it and keyocode2 is 0xFF.
  378. Power switch:
  379. You can read the state from PSW line(active low) however
  380. the switch has a special scancode 0x7F7F, so you can
  381. also read from Data line. It uses 0xFFFF for release scancode.
  382. Keyboard LEDs & state of keys(Register2)
  383. This register hold current state of three LEDs and nine keys.
  384. The state of LEDs can be changed by sending Listen command.
  385. 1514 . . . . . . 7 6 5 . 3 2 1 0
  386. | | | | | | | | | | | | | | | +- LED1(NumLock)
  387. | | | | | | | | | | | | | | +--- LED2(CapsLock)
  388. | | | | | | | | | | | | | +----- LED3(ScrollLock)
  389. | | | | | | | | | | +-+-+------- Reserved
  390. | | | | | | | | | +------------- ScrollLock
  391. | | | | | | | | +--------------- NumLock
  392. | | | | | | | +----------------- Apple/Command
  393. | | | | | | +------------------- Option
  394. | | | | | +--------------------- Shift
  395. | | | | +----------------------- Control
  396. | | | +------------------------- Reset/Power
  397. | | +--------------------------- CapsLock
  398. | +----------------------------- Delete
  399. +------------------------------- Reserved
  400. END_OF_ADB
  401. */