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

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