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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. uint16_t adb_host_kbd_recv(void)
  71. {
  72. uint16_t data = 0;
  73. attention();
  74. send_byte(0x2C); // Addr:Keyboard(0010), Cmd:Talk(11), Register0(00)
  75. place_bit0(); // Stopbit(0)
  76. if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us)
  77. return 0; // No data to send
  78. }
  79. if (!read_bit()) { // Startbit(1)
  80. // Service Request
  81. dprintf("Startbit ERROR\n");
  82. return -2;
  83. }
  84. // ad hoc fix: without block inerrupt read wrong bit occasionally and get keys stuck
  85. cli();
  86. data = read_byte();
  87. data = (data<<8) | read_byte();
  88. uint8_t stop = read_bit(); // Stopbit(0)
  89. sei();
  90. if (stop) {
  91. dprintf("Stopbit ERROR\n");
  92. return -3;
  93. }
  94. return data;
  95. }
  96. void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l)
  97. {
  98. attention();
  99. send_byte(cmd);
  100. place_bit0(); // Stopbit(0)
  101. _delay_us(200); // Tlt/Stop to Start
  102. place_bit1(); // Startbit(1)
  103. send_byte(data_h);
  104. send_byte(data_l);
  105. place_bit0(); // Stopbit(0);
  106. }
  107. // send state of LEDs
  108. void adb_host_kbd_led(uint8_t led)
  109. {
  110. // Addr:Keyboard(0010), Cmd:Listen(10), Register2(10)
  111. // send upper byte (not used)
  112. // send lower byte (bit2: ScrollLock, bit1: CapsLock, bit0:
  113. adb_host_listen(0x2A,0,led&0x07);
  114. }
  115. static inline void data_lo()
  116. {
  117. ADB_DDR |= (1<<ADB_DATA_BIT);
  118. ADB_PORT &= ~(1<<ADB_DATA_BIT);
  119. }
  120. static inline void data_hi()
  121. {
  122. ADB_PORT |= (1<<ADB_DATA_BIT);
  123. ADB_DDR &= ~(1<<ADB_DATA_BIT);
  124. }
  125. static inline bool data_in()
  126. {
  127. ADB_PORT |= (1<<ADB_DATA_BIT);
  128. ADB_DDR &= ~(1<<ADB_DATA_BIT);
  129. return ADB_PIN&(1<<ADB_DATA_BIT);
  130. }
  131. #ifdef ADB_PSW_BIT
  132. static inline void psw_lo()
  133. {
  134. ADB_DDR |= (1<<ADB_PSW_BIT);
  135. ADB_PORT &= ~(1<<ADB_PSW_BIT);
  136. }
  137. static inline void psw_hi()
  138. {
  139. ADB_PORT |= (1<<ADB_PSW_BIT);
  140. ADB_DDR &= ~(1<<ADB_PSW_BIT);
  141. }
  142. static inline bool psw_in()
  143. {
  144. ADB_PORT |= (1<<ADB_PSW_BIT);
  145. ADB_DDR &= ~(1<<ADB_PSW_BIT);
  146. return ADB_PIN&(1<<ADB_PSW_BIT);
  147. }
  148. #endif
  149. static inline void attention(void)
  150. {
  151. data_lo();
  152. _delay_us(700);
  153. place_bit1();
  154. }
  155. static inline void place_bit0(void)
  156. {
  157. data_lo();
  158. _delay_us(65);
  159. data_hi();
  160. _delay_us(35);
  161. }
  162. static inline void place_bit1(void)
  163. {
  164. data_lo();
  165. _delay_us(35);
  166. data_hi();
  167. _delay_us(65);
  168. }
  169. static inline void send_byte(uint8_t data)
  170. {
  171. for (int i = 0; i < 8; i++) {
  172. if (data&(0x80>>i))
  173. place_bit1();
  174. else
  175. place_bit0();
  176. }
  177. }
  178. static inline bool read_bit(void)
  179. {
  180. // ADB Bit Cells
  181. //
  182. // bit cell time: 70-130us
  183. // low part of bit0: 60-70% of bit cell
  184. // low part of bit1: 30-40% of bit cell
  185. //
  186. // bit cell time 70us 130us
  187. // --------------------------------------------
  188. // low part of bit0 42-49 78-91
  189. // high part of bit0 21-28 39-52
  190. // low part of bit1 21-28 39-52
  191. // high part of bit1 42-49 78-91
  192. //
  193. //
  194. // bit0:
  195. // 70us bit cell:
  196. // ____________~~~~~~
  197. // 42-49 21-28
  198. //
  199. // 130us bit cell:
  200. // ____________~~~~~~
  201. // 78-91 39-52
  202. //
  203. // bit1:
  204. // 70us bit cell:
  205. // ______~~~~~~~~~~~~
  206. // 21-28 42-49
  207. //
  208. // 130us bit cell:
  209. // ______~~~~~~~~~~~~
  210. // 39-52 78-91
  211. //
  212. // read:
  213. // ________|~~~~~~~~~
  214. // 55us
  215. // Read data line after 55us. If data line is low/high then bit is 0/1.
  216. // This method might not work at <90us bit cell time.
  217. //
  218. // [from Apple IIgs Hardware Reference Second Edition]
  219. bool bit;
  220. wait_data_lo(75); // wait the start of bit cell at least 130ms(55+0+75)
  221. _delay_us(55);
  222. bit = data_in();
  223. wait_data_hi(36); // wait high part of bit cell at least 91ms(55+36)
  224. return bit;
  225. }
  226. static inline uint8_t read_byte(void)
  227. {
  228. uint8_t data = 0;
  229. for (int i = 0; i < 8; i++) {
  230. data <<= 1;
  231. if (read_bit())
  232. data = data | 1;
  233. }
  234. return data;
  235. }
  236. static inline uint8_t wait_data_lo(uint16_t us)
  237. {
  238. while (data_in() && us) {
  239. _delay_us(1);
  240. us--;
  241. }
  242. return us;
  243. }
  244. static inline uint8_t wait_data_hi(uint8_t us)
  245. {
  246. while (!data_in() && us) {
  247. _delay_us(1);
  248. us--;
  249. }
  250. return us;
  251. }
  252. /*
  253. ADB Protocol
  254. ============
  255. Resources
  256. ---------
  257. ADB - The Untold Story: Space Aliens Ate My Mouse
  258. http://developer.apple.com/legacy/mac/library/#technotes/hw/hw_01.html
  259. ADB Manager
  260. http://developer.apple.com/legacy/mac/library/documentation/mac/pdf/Devices/ADB_Manager.pdf
  261. Service request(5-17)
  262. Apple IIgs Hardware Reference Second Edition [Chapter6 p121]
  263. ftp://ftp.apple.asimov.net/pub/apple_II/documentation/Apple%20IIgs%20Hardware%20Reference.pdf
  264. ADB Keycode
  265. http://72.0.193.250/Documentation/macppc/adbkeycodes/
  266. http://m0115.web.fc2.com/m0115.jpg
  267. [Inside Macintosh volume V, pages 191-192]
  268. http://www.opensource.apple.com/source/IOHIDFamily/IOHIDFamily-421.18.3/IOHIDFamily/Cosmo_USB2ADB.c
  269. ADB Signaling
  270. http://kbdbabel.sourceforge.net/doc/kbd_signaling_pcxt_ps2_adb.pdf
  271. ADB Overview & History
  272. http://en.wikipedia.org/wiki/Apple_Desktop_Bus
  273. Microchip Application Note: ADB device(with code for PIC16C)
  274. http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011062
  275. AVR ATtiny2131 ADB to PS/2 converter(Japanese)
  276. http://hp.vector.co.jp/authors/VA000177/html/KeyBoardA5DEA5CBA5A2II.html
  277. Pinouts
  278. -------
  279. ADB female socket from the front:
  280. __________
  281. | | <--- top
  282. | 4o o3 |
  283. |2o o1|
  284. | == |
  285. |________| <--- bottom
  286. | | <--- 4pins
  287. ADB female socket from bottom:
  288. ========== <--- front
  289. | |
  290. | |
  291. |2o o1|
  292. |4o o3|
  293. ---------- <--- back
  294. 1: Data
  295. 2: Power SW(low when press Power key)
  296. 3: Vcc(5V)
  297. 4: GND
  298. Commands
  299. --------
  300. ADB command is 1byte and consists of 4bit-address, 2bit-command
  301. type and 2bit-register. The commands are always sent by Host.
  302. Command format:
  303. 7 6 5 4 3 2 1 0
  304. | | | |------------ address
  305. | |-------- command type
  306. | |---- register
  307. bits commands
  308. ------------------------------------------------------
  309. - - - - 0 0 0 0 Send Request(reset all devices)
  310. A A A A 0 0 0 1 Flush(reset a device)
  311. - - - - 0 0 1 0 Reserved
  312. - - - - 0 0 1 1 Reserved
  313. - - - - 0 1 - - Reserved
  314. A A A A 1 0 R R Listen(write to a device)
  315. A A A A 1 1 R R Talk(read from a device)
  316. The command to read keycodes from keyboard is 0x2C which
  317. consist of keyboard address 2 and Talk against register 0.
  318. Address:
  319. 2: keyboard
  320. 3: mice
  321. Registers:
  322. 0: application(keyobard uses this to store its data.)
  323. 1: application
  324. 2: application(keyboard uses this for LEDs and state of modifiers)
  325. 3: status and command
  326. Communication
  327. -------------
  328. This is a minimum information for keyboard communication.
  329. See "Resources" for detail.
  330. Signaling:
  331. ~~~~____________~~||||||||||||__~~~~~_~~|||||||||||||||__~~~~
  332. |800us | |7 Command 0| | | |15-64 Data 0|Stopbit(0)
  333. +Attention | | | +Startbit(1)
  334. +Startbit(1) | +Tlt(140-260us)
  335. +stopbit(0)
  336. Bit cells:
  337. bit0: ______~~~
  338. 65 :35us
  339. bit1: ___~~~~~~
  340. 35 :65us
  341. bit0 low time: 60-70% of bit cell(42-91us)
  342. bit1 low time: 30-40% of bit cell(21-52us)
  343. bit cell time: 70-130us
  344. [from Apple IIgs Hardware Reference Second Edition]
  345. Criterion for bit0/1:
  346. After 55us if line is low/high then bit is 0/1.
  347. Attention & start bit:
  348. Host asserts low in 560-1040us then places start bit(1).
  349. Tlt(Stop to Start):
  350. Bus stays high in 140-260us then device places start bit(1).
  351. Global reset:
  352. Host asserts low in 2.8-5.2ms. All devices are forced to reset.
  353. Service request from device(Srq):
  354. Device can request to send at commad(Global only?) stop bit.
  355. Requesting device keeps low for 140-260us at stop bit of command.
  356. Keyboard Data(Register0)
  357. This 16bit data can contains two keycodes and two released flags.
  358. First keycode is palced in upper byte. When one keyocode is sent,
  359. lower byte is 0xFF.
  360. Release flag is 1 when key is released.
  361. 1514 . . . . . 8 7 6 . . . . . 0
  362. | | | | | | | | | +-+-+-+-+-+-+- Keycode2
  363. | | | | | | | | +--------------- Released2(1 when the key is released)
  364. | +-+-+-+-+-+-+----------------- Keycode1
  365. +------------------------------- Released1(1 when the key is released)
  366. Keycodes:
  367. Scancode consists of 7bit keycode and 1bit release flag.
  368. Device can send two keycodes at once. If just one keycode is sent
  369. keycode1 contains it and keyocode2 is 0xFF.
  370. Power switch:
  371. You can read the state from PSW line(active low) however
  372. the switch has a special scancode 0x7F7F, so you can
  373. also read from Data line. It uses 0xFFFF for release scancode.
  374. Keyboard LEDs & state of keys(Register2)
  375. This register hold current state of three LEDs and nine keys.
  376. The state of LEDs can be changed by sending Listen command.
  377. 1514 . . . . . . 7 6 5 . 3 2 1 0
  378. | | | | | | | | | | | | | | | +- LED1(NumLock)
  379. | | | | | | | | | | | | | | +--- LED2(CapsLock)
  380. | | | | | | | | | | | | | +----- LED3(ScrollLock)
  381. | | | | | | | | | | +-+-+------- Reserved
  382. | | | | | | | | | +------------- ScrollLock
  383. | | | | | | | | +--------------- NumLock
  384. | | | | | | | +----------------- Apple/Command
  385. | | | | | | +------------------- Option
  386. | | | | | +--------------------- Shift
  387. | | | | +----------------------- Control
  388. | | | +------------------------- Reset/Power
  389. | | +--------------------------- CapsLock
  390. | +----------------------------- Delete
  391. +------------------------------- Reserved
  392. END_OF_ADB
  393. */