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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 line is 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
  82. bit 0 always 1
  83. To get scan code, use ((bits&(1<<7)) | ((bits&7F))>>1).
  84. SCAN CODE:
  85. M0110A
  86. ,---------------------------------------------------------.
  87. | `| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Backs|
  88. |---------------------------------------------------------|
  89. |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]| \|
  90. |---------------------------------------------------------|
  91. |CapsLo| A| S| D| F| G| H| J| K| L| ;| '|Return|
  92. |---------------------------------------------------------|
  93. |Shift | Z| X| C| V| B| N| M| ,| ,| /| |
  94. `---------------------------------------------------------'
  95. |Opt|Mac | Space |Enter|Opt|
  96. `------------------------------------------------'
  97. ,---------------------------------------------------------.
  98. | 32| 12| 13| 14| 15| 17| 16| 1A| 1C| 19| 1D| 1B| 18| 33|
  99. |---------------------------------------------------------|
  100. | 30| 0C| 0D| 0E| 0F| 10| 11| 20| 22| 1F| 23| 21| 1E| 2A|
  101. |---------------------------------------------------------|
  102. | 39| 00| 01| 02| 03| 05| 04| 26| 28| 25| 29| 27| 24|
  103. |---------------------------------------------------------|
  104. | 38| 06| 07| 08| 09| 0B| 2D| 2E| 2B| 2F| 2C| 38|
  105. `---------------------------------------------------------'
  106. | 3A| 37| 31 | 34| 3A|
  107. `------------------------------------------------'
  108. References
  109. ----------
  110. Protocol:
  111. http://www.mac.linux-m68k.org/devel/plushw.php
  112. Connector:
  113. http://www.kbdbabel.org/conn/kbd_connector_macplus.png
  114. Signaling:
  115. http://www.kbdbabel.org/signaling/kbd_signaling_mac.png
  116. http://typematic.blog.shinobi.jp/Entry/14/
  117. Scan Codes:
  118. http://m0115.web.fc2.com/m0110.jpg
  119. http://m0115.web.fc2.com/m0110a.jpg
  120. */
  121. #define WAIT(stat, us, err) do { \
  122. if (!wait_##stat(us)) { \
  123. m0110_error = err; \
  124. goto ERROR; \
  125. } \
  126. } while (0)
  127. uint8_t m0110_error = 0;
  128. void m0110_init(void)
  129. {
  130. uint8_t data;
  131. idle();
  132. _delay_ms(255);
  133. m0110_send(M0110_MODLE);
  134. data = m0110_recv();
  135. print("m0110_init model: "); phex(data); print("\n");
  136. m0110_send(M0110_TEST);
  137. data = m0110_recv();
  138. print("m0110_init test: "); phex(data); print("\n");
  139. }
  140. uint8_t m0110_send(uint8_t data)
  141. {
  142. m0110_error = 0;
  143. request();
  144. WAIT(clock_lo, 1000, 0);
  145. for (uint8_t bit = 0x80; bit; bit >>= 1) {
  146. WAIT(clock_lo, 250, 3);
  147. _delay_us(15);
  148. if (data&bit) {
  149. data_hi();
  150. } else {
  151. data_lo();
  152. }
  153. WAIT(clock_hi, 200, 4);
  154. }
  155. _delay_us(100); // hold last bit for 80us
  156. idle();
  157. return 1;
  158. ERROR:
  159. if (m0110_error) {
  160. print("m0110_send err: "); phex(m0110_error); print("\n");
  161. }
  162. idle();
  163. return 0;
  164. }
  165. uint8_t m0110_recv(void)
  166. {
  167. uint8_t data = 0;
  168. m0110_error = 0;
  169. WAIT(clock_lo, -1, 0); // need 250ms? insted 0xffff(16bit max)us
  170. for (uint8_t i = 0; i < 8; i++) {
  171. data <<= 1;
  172. WAIT(clock_lo, 200, 2);
  173. WAIT(clock_hi, 200, 3);
  174. if (data_in()) {
  175. data |= 1;
  176. }
  177. }
  178. idle();
  179. print("m0110_send recv data: "); phex(data); print("\n");
  180. return data;
  181. ERROR:
  182. if (m0110_error) {
  183. print("m0110_recv err: "); phex(m0110_error); print("\n");
  184. }
  185. idle();
  186. return 0xFF;
  187. }
  188. uint8_t m0110_recv_key(void)
  189. {
  190. uint8_t key;
  191. m0110_send(M0110_INQUIRY);
  192. key = m0110_recv();
  193. if (key == 0xFF || key == M0110_NULL)
  194. return M0110_NULL;
  195. else
  196. return (key&(1<<7) | (key&0x7F)>>1);
  197. }
  198. static inline void clock_lo()
  199. {
  200. M0110_CLOCK_PORT &= ~(1<<M0110_CLOCK_BIT);
  201. M0110_CLOCK_DDR |= (1<<M0110_CLOCK_BIT);
  202. }
  203. static inline void clock_hi()
  204. {
  205. /* input with pull up */
  206. M0110_CLOCK_DDR &= ~(1<<M0110_CLOCK_BIT);
  207. M0110_CLOCK_PORT |= (1<<M0110_CLOCK_BIT);
  208. }
  209. static inline bool clock_in()
  210. {
  211. M0110_CLOCK_DDR &= ~(1<<M0110_CLOCK_BIT);
  212. M0110_CLOCK_PORT |= (1<<M0110_CLOCK_BIT);
  213. _delay_us(1);
  214. return M0110_CLOCK_PIN&(1<<M0110_CLOCK_BIT);
  215. }
  216. static inline void data_lo()
  217. {
  218. M0110_DATA_PORT &= ~(1<<M0110_DATA_BIT);
  219. M0110_DATA_DDR |= (1<<M0110_DATA_BIT);
  220. }
  221. static inline void data_hi()
  222. {
  223. /* input with pull up */
  224. M0110_DATA_DDR &= ~(1<<M0110_DATA_BIT);
  225. M0110_DATA_PORT |= (1<<M0110_DATA_BIT);
  226. }
  227. static inline bool data_in()
  228. {
  229. M0110_DATA_DDR &= ~(1<<M0110_DATA_BIT);
  230. M0110_DATA_PORT |= (1<<M0110_DATA_BIT);
  231. _delay_us(1);
  232. return M0110_DATA_PIN&(1<<M0110_DATA_BIT);
  233. }
  234. static inline uint16_t wait_clock_lo(uint16_t us)
  235. {
  236. while (clock_in() && us) { asm(""); _delay_us(1); us--; }
  237. return us;
  238. }
  239. static inline uint16_t wait_clock_hi(uint16_t us)
  240. {
  241. while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
  242. return us;
  243. }
  244. static inline uint16_t wait_data_lo(uint16_t us)
  245. {
  246. while (data_in() && us) { asm(""); _delay_us(1); us--; }
  247. return us;
  248. }
  249. static inline uint16_t wait_data_hi(uint16_t us)
  250. {
  251. while (!data_in() && us) { asm(""); _delay_us(1); us--; }
  252. return us;
  253. }
  254. static inline void idle(void)
  255. {
  256. clock_hi();
  257. data_hi();
  258. }
  259. static inline void request(void)
  260. {
  261. clock_hi();
  262. data_lo();
  263. }