upload
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

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