Keyboard firmwares for Atmel AVR and Cortex-M
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

m0110.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. /* M0110A Support was contributed by skagon@github */
  31. #include <stdbool.h>
  32. #include <avr/io.h>
  33. #include <avr/interrupt.h>
  34. #include <util/delay.h>
  35. #include "m0110.h"
  36. #include "debug.h"
  37. static inline uint8_t inquiry(void);
  38. static inline uint8_t instant(void);
  39. static inline void clock_lo(void);
  40. static inline void clock_hi(void);
  41. static inline bool clock_in(void);
  42. static inline void data_lo(void);
  43. static inline void data_hi(void);
  44. static inline bool data_in(void);
  45. static inline uint16_t wait_clock_lo(uint16_t us);
  46. static inline uint16_t wait_clock_hi(uint16_t us);
  47. static inline uint16_t wait_data_lo(uint16_t us);
  48. static inline uint16_t wait_data_hi(uint16_t us);
  49. static inline void idle(void);
  50. static inline void request(void);
  51. /*
  52. Primitive M0110 Library for AVR
  53. ==============================
  54. Signaling
  55. ---------
  56. CLOCK is always from KEYBOARD. DATA are sent with MSB first.
  57. 1) IDLE: both lines are high.
  58. CLOCK ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  59. DATA ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  60. 2) KEYBOARD->HOST: HOST reads bit on rising edge.
  61. CLOCK ~~~~~~~~~~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~~~~~~~~~
  62. DATA ~~~~~~~~~~~~X777777X666666X555555X444444X333333X222222X111111X000000X~~~~~~~
  63. <--> 160us(clock low)
  64. <---> 180us(clock high)
  65. 3) HOST->KEYBOARD: HOST asserts bit on falling edge.
  66. CLOCK ~~~~~~~~~~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~|__|~~~~~~~~~~~
  67. DATA ~~~~~~|_____X777777X666666X555555X444444X333333X222222X111111X000000X~~~~~~~
  68. <----> 840us(request to send by host) <---> 80us(hold DATA)
  69. <--> 180us(clock low)
  70. <---> 220us(clock high)
  71. Protocol
  72. --------
  73. COMMAND:
  74. Inquiry 0x10 get key event
  75. Instant 0x12 get key event
  76. Model 0x14 get model number(M0110 responds with 0x09)
  77. bit 7 1 if another device connected(used when keypad exists?)
  78. bit4-6 next device model number
  79. bit1-3 keyboard model number
  80. bit 0 always 1
  81. Test 0x16 test(ACK:0x7D/NAK:0x77)
  82. KEY EVENT:
  83. bit 7 key state(0:press 1:release)
  84. bit 6-1 scan code(see below)
  85. bit 0 always 1
  86. To get scan code use this: ((bits&(1<<7)) | ((bits&0x7F))>>1).
  87. Note: On the M0110A, the numpad keys and the arrow keys are preceded by 0x79.
  88. Moreover, the numpad keys =, /, * and + are preceded by shift-down 0x71 on press and shift-up 0xF1 on release.
  89. So, the data transferred by nupmad 5 is "79 2F" whereas for numpad + it's "71 79 0D".
  90. ARROW KEYS:
  91. Arrow keys and Pad+,*,/,=(Calc keys) share same byte sequence and its preceding byte
  92. 0x71 and 0xF1 means press and release event of SHIFT. These cause very confusing situation.
  93. It is difficult or impossible to tell Calc key from Arrow key with SHIFT in some cases.
  94. Raw key events:
  95. press release
  96. ---------------- ----------------
  97. Left: 0x79, 0x0D 0x79, 0x8D
  98. Right: 0x79, 0x05 0x79, 0x85
  99. Up: 0x79, 0x1B 0x79, 0x9B
  100. Down: 0x79, 0x11 0x79, 0x91
  101. Pad+: 0x71, 0x79, 0x0D 0xF1, 0x79, 0x8D
  102. Pad*: 0x71, 0x79, 0x05 0xF1, 0x79, 0x85
  103. Pad/: 0x71, 0x79, 0x1B 0xF1, 0x79, 0x9B
  104. Pad=: 0x71, 0x79, 0x11 0xF1, 0x79, 0x91
  105. SCAN CODE:
  106. m0111_recv_key() function returns follwing scan codes instead of raw key events.
  107. Scan codes are 1 byte long and bit7 is set when key is released.
  108. M0110
  109. ,---------------------------------------------------------.
  110. | `| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Backs|
  111. |---------------------------------------------------------|
  112. |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]| \|
  113. |---------------------------------------------------------|
  114. |CapsLo| A| S| D| F| G| H| J| K| L| ;| '|Return|
  115. |---------------------------------------------------------|
  116. |Shift | Z| X| C| V| B| N| M| ,| ,| /| |
  117. `---------------------------------------------------------'
  118. |Opt|Mac | Space |Enter|Opt|
  119. `------------------------------------------------'
  120. ,---------------------------------------------------------.
  121. | 32| 12| 13| 14| 15| 17| 16| 1A| 1C| 19| 1D| 1B| 18| 33|
  122. |---------------------------------------------------------|
  123. | 30| 0C| 0D| 0E| 0F| 10| 11| 20| 22| 1F| 23| 21| 1E| 2A|
  124. |---------------------------------------------------------|
  125. | 39| 00| 01| 02| 03| 05| 04| 26| 28| 25| 29| 27| 24|
  126. |---------------------------------------------------------|
  127. | 38| 06| 07| 08| 09| 0B| 2D| 2E| 2B| 2F| 2C| 38|
  128. `---------------------------------------------------------'
  129. | 3A| 37| 31 | 34| 3A|
  130. `------------------------------------------------'
  131. M0110A
  132. ,---------------------------------------------------------. ,---------------.
  133. | `| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Bcksp| |Clr| =| /| *|
  134. |---------------------------------------------------------| |---------------|
  135. |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]| | | 7| 8| 9| -|
  136. |-----------------------------------------------------' | |---------------|
  137. |CapsLo| A| S| D| F| G| H| J| K| L| ;| '|Return| | 4| 5| 6| +|
  138. |---------------------------------------------------------| |---------------|
  139. |Shift | Z| X| C| V| B| N| M| ,| ,| /|Shft|Up | | 1| 2| 3| |
  140. |---------------------------------------------------------' |-----------|Ent|
  141. |Optio|Mac | Space | \|Lft|Rgt|Dn | | 0| .| |
  142. `---------------------------------------------------------' `---------------'
  143. ,---------------------------------------------------------. ,---------------.
  144. | 32| 12| 13| 14| 15| 17| 16| 1A| 1C| 19| 1D| 1B| 18| 33| | 47| 68| 6D| 62|
  145. |---------------------------------------------------------| |---------------|
  146. | 30| 0C| 0D| 0E| 0F| 10| 11| 20| 22| 1F| 23| 21| 1E| | | 59| 5B| 5C| 4E|
  147. |-----------------------------------------------------' | |---------------|
  148. | 39| 00| 01| 02| 03| 05| 04| 26| 28| 25| 29| 27| 24| | 56| 57| 58| 66|
  149. |---------------------------------------------------------| |---------------|
  150. | 38| 06| 07| 08| 09| 0B| 2D| 2E| 2B| 2F| 2C| 38| 4D| | 53| 54| 55| |
  151. |---------------------------------------------------------' |-----------| 4C|
  152. | 3A| 37| 31 | 2A| 46| 42| 48| | 52| 41| |
  153. `---------------------------------------------------------' `---------------'
  154. References
  155. ----------
  156. Technical Info for 128K/512K and Plus
  157. ftp://ftp.apple.asimov.net/pub/apple_II/documentation/macintosh/Mac%20Hardware%20Info%20-%20Mac%20128K.pdf
  158. ftp://ftp.apple.asimov.net/pub/apple_II/documentation/macintosh/Mac%20Hardware%20Info%20-%20Mac%20Plus.pdf
  159. Protocol:
  160. Page 20 of Tech Info for 128K/512K
  161. http://www.mac.linux-m68k.org/devel/plushw.php
  162. Connector:
  163. Page 20 of Tech Info for 128K/512K
  164. http://www.kbdbabel.org/conn/kbd_connector_macplus.png
  165. Signaling:
  166. http://www.kbdbabel.org/signaling/kbd_signaling_mac.png
  167. http://typematic.blog.shinobi.jp/Entry/14/
  168. Scan Codes:
  169. Page 22 of Tech Info for 128K/512K
  170. Page 07 of Tech Info for Plus
  171. http://m0115.web.fc2.com/m0110.jpg
  172. http://m0115.web.fc2.com/m0110a.jpg
  173. */
  174. #define WAIT_US(stat, us, err) do { \
  175. if (!wait_##stat(us)) { \
  176. m0110_error = err; \
  177. goto ERROR; \
  178. } \
  179. } while (0)
  180. #define WAIT_MS(stat, ms, err) do { \
  181. uint16_t _ms = ms; \
  182. while (_ms) { \
  183. if (wait_##stat(1000)) { \
  184. break; \
  185. } \
  186. _ms--; \
  187. } \
  188. if (_ms == 0) { \
  189. m0110_error = err; \
  190. goto ERROR; \
  191. } \
  192. } while (0)
  193. uint8_t m0110_error = 0;
  194. void m0110_init(void)
  195. {
  196. uint8_t data;
  197. idle();
  198. _delay_ms(1000);
  199. // Model Number
  200. // M0110 : 0x09 00001001 : model number 4 (100)
  201. // M0110A: 0x0B 00001011 : model number 5 (101)
  202. // M0110 & M0120: ???
  203. m0110_send(M0110_MODEL);
  204. data = m0110_recv();
  205. print("m0110_init model: "); phex(data); print("\n");
  206. m0110_send(M0110_TEST);
  207. data = m0110_recv();
  208. print("m0110_init test: "); phex(data); print("\n");
  209. }
  210. uint8_t m0110_send(uint8_t data)
  211. {
  212. m0110_error = 0;
  213. request();
  214. WAIT_MS(clock_lo, 250, 1); // keyboard may block long time
  215. for (uint8_t bit = 0x80; bit; bit >>= 1) {
  216. WAIT_US(clock_lo, 250, 3);
  217. if (data&bit) {
  218. data_hi();
  219. } else {
  220. data_lo();
  221. }
  222. WAIT_US(clock_hi, 200, 4);
  223. }
  224. _delay_us(100); // hold last bit for 80us
  225. idle();
  226. return 1;
  227. ERROR:
  228. print("m0110_send err: "); phex(m0110_error); print("\n");
  229. _delay_ms(500);
  230. idle();
  231. return 0;
  232. }
  233. uint8_t m0110_recv(void)
  234. {
  235. uint8_t data = 0;
  236. m0110_error = 0;
  237. WAIT_MS(clock_lo, 250, 1); // keyboard may block long time
  238. for (uint8_t i = 0; i < 8; i++) {
  239. data <<= 1;
  240. WAIT_US(clock_lo, 200, 2);
  241. WAIT_US(clock_hi, 200, 3);
  242. if (data_in()) {
  243. data |= 1;
  244. }
  245. }
  246. idle();
  247. return data;
  248. ERROR:
  249. print("m0110_recv err: "); phex(m0110_error); print("\n");
  250. _delay_ms(500);
  251. idle();
  252. return 0xFF;
  253. }
  254. uint8_t m0110_recv_key(void)
  255. {
  256. static uint8_t keybuf = 0x00;
  257. uint8_t key, key2, key3;
  258. if (keybuf) {
  259. key = keybuf;
  260. keybuf = 0x00;
  261. return key;
  262. }
  263. key = instant(); // Use INSTANT for better response. Should be INQUIRY ?
  264. switch (key) {
  265. case M0110_KEYPAD:
  266. // Pad/Arrow keys
  267. return (M0110_RAW2SCAN(instant()) | M0110_KEYPAD_OFFSET);
  268. break;
  269. case M0110_SHIFT_MAKE:
  270. case M0110_SHIFT_BREAK:
  271. key2 = instant();
  272. if (key2 == M0110_KEYPAD) {
  273. key3 = instant();
  274. switch (key3) {
  275. case M0110_ARROW_UP:
  276. case M0110_ARROW_DOWN:
  277. case M0110_ARROW_LEFT:
  278. case M0110_ARROW_RIGHT:
  279. // Calc keys
  280. return (M0110_RAW2SCAN(key3) | M0110_CALC_OFFSET);
  281. default:
  282. // Shift + Pad/Arrow keys
  283. keybuf = M0110_RAW2SCAN(key3);
  284. return (M0110_RAW2SCAN(key) | M0110_KEYPAD_OFFSET);
  285. }
  286. } else {
  287. // Shift + other keys
  288. keybuf = M0110_RAW2SCAN(key2);
  289. return M0110_RAW2SCAN(key);
  290. }
  291. break;
  292. default:
  293. // other keys
  294. return M0110_RAW2SCAN(key);
  295. break;
  296. }
  297. }
  298. static inline uint8_t inquiry(void)
  299. {
  300. m0110_send(M0110_INQUIRY);
  301. return m0110_recv();
  302. }
  303. static inline uint8_t instant(void)
  304. {
  305. m0110_send(M0110_INSTANT);
  306. return m0110_recv();
  307. }
  308. static inline void clock_lo()
  309. {
  310. M0110_CLOCK_PORT &= ~(1<<M0110_CLOCK_BIT);
  311. M0110_CLOCK_DDR |= (1<<M0110_CLOCK_BIT);
  312. }
  313. static inline void clock_hi()
  314. {
  315. /* input with pull up */
  316. M0110_CLOCK_DDR &= ~(1<<M0110_CLOCK_BIT);
  317. M0110_CLOCK_PORT |= (1<<M0110_CLOCK_BIT);
  318. }
  319. static inline bool clock_in()
  320. {
  321. M0110_CLOCK_DDR &= ~(1<<M0110_CLOCK_BIT);
  322. M0110_CLOCK_PORT |= (1<<M0110_CLOCK_BIT);
  323. _delay_us(1);
  324. return M0110_CLOCK_PIN&(1<<M0110_CLOCK_BIT);
  325. }
  326. static inline void data_lo()
  327. {
  328. M0110_DATA_PORT &= ~(1<<M0110_DATA_BIT);
  329. M0110_DATA_DDR |= (1<<M0110_DATA_BIT);
  330. }
  331. static inline void data_hi()
  332. {
  333. /* input with pull up */
  334. M0110_DATA_DDR &= ~(1<<M0110_DATA_BIT);
  335. M0110_DATA_PORT |= (1<<M0110_DATA_BIT);
  336. }
  337. static inline bool data_in()
  338. {
  339. M0110_DATA_DDR &= ~(1<<M0110_DATA_BIT);
  340. M0110_DATA_PORT |= (1<<M0110_DATA_BIT);
  341. _delay_us(1);
  342. return M0110_DATA_PIN&(1<<M0110_DATA_BIT);
  343. }
  344. static inline uint16_t wait_clock_lo(uint16_t us)
  345. {
  346. while (clock_in() && us) { asm(""); _delay_us(1); us--; }
  347. return us;
  348. }
  349. static inline uint16_t wait_clock_hi(uint16_t us)
  350. {
  351. while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
  352. return us;
  353. }
  354. static inline uint16_t wait_data_lo(uint16_t us)
  355. {
  356. while (data_in() && us) { asm(""); _delay_us(1); us--; }
  357. return us;
  358. }
  359. static inline uint16_t wait_data_hi(uint16_t us)
  360. {
  361. while (!data_in() && us) { asm(""); _delay_us(1); us--; }
  362. return us;
  363. }
  364. static inline void idle(void)
  365. {
  366. clock_hi();
  367. data_hi();
  368. }
  369. static inline void request(void)
  370. {
  371. clock_hi();
  372. data_lo();
  373. }