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.

m0110.c 12KB

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