Keyboard firmwares for Atmel AVR and Cortex-M
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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