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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. // bit0: ______~~~
  175. // 65 :35us
  176. //
  177. // bit1: ___~~~~~~
  178. // 35 :65us
  179. //
  180. // bit0 low time: 60-70% of bit cell(42-91us)
  181. // bit1 low time: 30-40% of bit cell(21-52us)
  182. // bit cell time: 70-130us
  183. // [from Apple IIgs Hardware Reference Second Edition]
  184. //
  185. // After 55us if data line is low/high then bit is 0/1.
  186. // Too simple to rely on?
  187. bool bit;
  188. wait_data_lo(75); // wait the beginning of bit cell
  189. _delay_us(55);
  190. bit = data_in();
  191. wait_data_hi(36); // wait high part of bit cell
  192. return bit;
  193. }
  194. static inline uint8_t read_byte(void)
  195. {
  196. uint8_t data = 0;
  197. for (int i = 0; i < 8; i++) {
  198. data <<= 1;
  199. if (read_bit())
  200. data = data | 1;
  201. }
  202. return data;
  203. }
  204. static inline uint8_t wait_data_lo(uint8_t us)
  205. {
  206. while (data_in() && us) {
  207. _delay_us(1);
  208. us--;
  209. }
  210. return us;
  211. }
  212. static inline uint8_t wait_data_hi(uint8_t us)
  213. {
  214. while (!data_in() && us) {
  215. _delay_us(1);
  216. us--;
  217. }
  218. return us;
  219. }
  220. /*
  221. ADB Protocol
  222. ============
  223. Resources
  224. ---------
  225. ADB - The Untold Story: Space Aliens Ate My Mouse
  226. http://developer.apple.com/legacy/mac/library/#technotes/hw/hw_01.html
  227. Apple IIgs Hardware Reference Second Edition [p80(Chapter6 p121)]
  228. ftp://ftp.apple.asimov.net/pub/apple_II/documentation/Apple%20IIgs%20Hardware%20Reference.pdf
  229. ADB Keycode
  230. http://72.0.193.250/Documentation/macppc/adbkeycodes/
  231. http://m0115.web.fc2.com/m0115.jpg
  232. [Inside Macintosh volume V, pages 191-192]
  233. ADB Signaling
  234. http://kbdbabel.sourceforge.net/doc/kbd_signaling_pcxt_ps2_adb.pdf
  235. ADB Overview & History
  236. http://en.wikipedia.org/wiki/Apple_Desktop_Bus
  237. Microchip Application Note: ADB device(with code for PIC16C)
  238. http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011062
  239. AVR ATtiny2131 ADB to PS/2 converter(Japanese)
  240. http://hp.vector.co.jp/authors/VA000177/html/KeyBoardA5DEA5CBA5A2II.html
  241. Pinouts
  242. -------
  243. ADB female socket from the front:
  244. __________
  245. | | <--- top
  246. | 4o o3 |
  247. |2o o1|
  248. | == |
  249. |________| <--- bottom
  250. | | <--- 4pins
  251. ADB female socket from bottom:
  252. ========== <--- front
  253. | |
  254. | |
  255. |2o o1|
  256. |4o o3|
  257. ---------- <--- back
  258. 1: Data
  259. 2: Power SW(low when press Power key)
  260. 3: Vcc(5V)
  261. 4: GND
  262. Commands
  263. --------
  264. ADB command is 1byte and consists of 4bit-address, 2bit-command
  265. type and 2bit-register. The commands are always sent by Host.
  266. Command format:
  267. 7 6 5 4 3 2 1 0
  268. | | | |------------ address
  269. | |-------- command type
  270. | |---- register
  271. bits commands
  272. ------------------------------------------------------
  273. - - - - 0 0 0 0 Send Request(reset all devices)
  274. A A A A 0 0 0 1 Flush(reset a device)
  275. - - - - 0 0 1 0 Reserved
  276. - - - - 0 0 1 1 Reserved
  277. - - - - 0 1 - - Reserved
  278. A A A A 1 0 R R Listen(write to a device)
  279. A A A A 1 1 R R Talk(read from a device)
  280. The command to read keycodes from keyboard is 0x2C which
  281. consist of keyboard address 2 and Talk against register 0.
  282. Address:
  283. 2: keyboard
  284. 3: mice
  285. Registers:
  286. 0: application(keyobard uses this to store its data.)
  287. 1: application
  288. 2: application(keyboard uses this for LEDs and state of modifiers)
  289. 3: status and command
  290. Communication
  291. -------------
  292. This is a minimum information for keyboard communication.
  293. See "Resources" for detail.
  294. Signaling:
  295. ~~~~____________~~||||||||||||__~~~~~_~~|||||||||||||||__~~~~
  296. |800us | |7 Command 0| | | |15-64 Data 0|Stopbit(0)
  297. +Attention | | | +Startbit(1)
  298. +Startbit(1) | +Tlt(140-260us)
  299. +stopbit(0)
  300. Bit cells:
  301. bit0: ______~~~
  302. 65 :35us
  303. bit1: ___~~~~~~
  304. 35 :65us
  305. bit0 low time: 60-70% of bit cell(42-91us)
  306. bit1 low time: 30-40% of bit cell(21-52us)
  307. bit cell time: 70-130us
  308. [from Apple IIgs Hardware Reference Second Edition]
  309. Criterion for bit0/1:
  310. After 55us if line is low/high then bit is 0/1.
  311. Attention & start bit:
  312. Host asserts low in 560-1040us then places start bit(1).
  313. Tlt(Stop to Start):
  314. Bus stays high in 140-260us then device places start bit(1).
  315. Global reset:
  316. Host asserts low in 2.8-5.2ms. All devices are forced to reset.
  317. Send request from device(Srq):
  318. Device can request to send at commad(Global only?) stop bit.
  319. keep low for 300us to request.
  320. Keyboard Data(Register0)
  321. This 16bit data can contains two keycodes and two released flags.
  322. First keycode is palced in upper byte. When one keyocode is sent,
  323. lower byte is 0xFF.
  324. Release flag is 1 when key is released.
  325. 1514 . . . . . 8 7 6 . . . . . 0
  326. | | | | | | | | | +-+-+-+-+-+-+- Keycode2
  327. | | | | | | | | +--------------- Released2(1 when the key is released)
  328. | +-+-+-+-+-+-+----------------- Keycode1
  329. +------------------------------- Released1(1 when the key is released)
  330. Keycodes:
  331. Scancode consists of 7bit keycode and 1bit release flag.
  332. Device can send two keycodes at once. If just one keycode is sent
  333. keycode1 contains it and keyocode2 is 0xFF.
  334. Power switch:
  335. You can read the state from PSW line(active low) however
  336. the switch has a special scancode 0x7F7F, so you can
  337. also read from Data line. It uses 0xFFFF for release scancode.
  338. Keyboard LEDs & state of keys(Register2)
  339. This register hold current state of three LEDs and nine keys.
  340. The state of LEDs can be changed by sending Listen command.
  341. 1514 . . . . . . 7 6 5 . 3 2 1 0
  342. | | | | | | | | | | | | | | | +- LED1(NumLock)
  343. | | | | | | | | | | | | | | +--- LED2(CapsLock)
  344. | | | | | | | | | | | | | +----- LED3(ScrollLock)
  345. | | | | | | | | | | +-+-+------- Reserved
  346. | | | | | | | | | +------------- ScrollLock
  347. | | | | | | | | +--------------- NumLock
  348. | | | | | | | +----------------- Apple/Command
  349. | | | | | | +------------------- Option
  350. | | | | | +--------------------- Shift
  351. | | | | +----------------------- Control
  352. | | | +------------------------- Reset/Power
  353. | | +--------------------------- CapsLock
  354. | +----------------------------- Delete
  355. +------------------------------- Reserved
  356. END_OF_ADB
  357. */