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 12KB

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