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

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