Keyboard firmwares for Atmel AVR and Cortex-M
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.

adb.c 13KB

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