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.

ps2.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. Copyright (c) 2010,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 "ps2.h"
  35. #include "debug.h"
  36. static uint8_t recv_data(void);
  37. static inline void clock_lo(void);
  38. static inline void clock_hi(void);
  39. static inline bool clock_in(void);
  40. static inline void data_lo(void);
  41. static inline void data_hi(void);
  42. static inline bool data_in(void);
  43. static inline uint16_t wait_clock_lo(uint16_t us);
  44. static inline uint16_t wait_clock_hi(uint16_t us);
  45. static inline uint16_t wait_data_lo(uint16_t us);
  46. static inline uint16_t wait_data_hi(uint16_t us);
  47. static inline void idle(void);
  48. static inline void inhibit(void);
  49. /*
  50. Primitive PS/2 Library for AVR
  51. ==============================
  52. Host side is only supported now.
  53. I/O control
  54. -----------
  55. High state is asserted by input with pull up.
  56. PS/2 References
  57. ---------------
  58. http://www.computer-engineering.org/ps2protocol/
  59. http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
  60. */
  61. #define WAIT(stat, us, err) do { \
  62. if (!wait_##stat(us)) { \
  63. ps2_error = err; \
  64. goto ERROR; \
  65. } \
  66. } while (0)
  67. uint8_t ps2_error = PS2_ERR_NONE;
  68. void ps2_host_init(void)
  69. {
  70. #ifdef PS2_INT_ENABLE
  71. PS2_INT_ENABLE();
  72. idle();
  73. #else
  74. inhibit();
  75. #endif
  76. }
  77. uint8_t ps2_host_send(uint8_t data)
  78. {
  79. bool parity;
  80. RETRY:
  81. parity = true;
  82. ps2_error = 0;
  83. /* terminate a transmission if we have */
  84. inhibit();
  85. _delay_us(100);
  86. /* start bit [1] */
  87. data_lo();
  88. clock_hi();
  89. WAIT(clock_lo, 15000, 1);
  90. /* data [2-9] */
  91. for (uint8_t i = 0; i < 8; i++) {
  92. _delay_us(15);
  93. if (data&(1<<i)) {
  94. parity = !parity;
  95. data_hi();
  96. } else {
  97. data_lo();
  98. }
  99. WAIT(clock_hi, 50, 2);
  100. WAIT(clock_lo, 50, 3);
  101. }
  102. /* parity [10] */
  103. _delay_us(15);
  104. if (parity) { data_hi(); } else { data_lo(); }
  105. WAIT(clock_hi, 50, 4);
  106. WAIT(clock_lo, 50, 5);
  107. /* stop bit [11] */
  108. _delay_us(15);
  109. data_hi();
  110. /* ack [12] */
  111. WAIT(data_lo, 50, 6);
  112. WAIT(clock_lo, 50, 7);
  113. /* wait for idle state */
  114. WAIT(clock_hi, 50, 8);
  115. WAIT(data_hi, 50, 9);
  116. uint8_t res = ps2_host_recv_response();
  117. if (res == 0xFE && data != 0xFE)
  118. goto RETRY;
  119. inhibit();
  120. return res;
  121. ERROR:
  122. inhibit();
  123. return 0;
  124. }
  125. /* receive data when host want else inhibit communication */
  126. uint8_t ps2_host_recv_response(void)
  127. {
  128. uint8_t data = 0;
  129. /* terminate a transmission if we have */
  130. inhibit();
  131. _delay_us(100);
  132. /* release lines(idle state) */
  133. idle();
  134. /* wait start bit */
  135. wait_clock_lo(2000);
  136. data = recv_data();
  137. inhibit();
  138. return data;
  139. }
  140. #ifndef PS2_INT_VECT
  141. uint8_t ps2_host_recv(void)
  142. {
  143. return ps2_host_recv_response();
  144. }
  145. #else
  146. /* ring buffer to store ps/2 key data */
  147. #define PBUF_SIZE 8
  148. static uint8_t pbuf[PBUF_SIZE];
  149. static uint8_t pbuf_head = 0;
  150. static uint8_t pbuf_tail = 0;
  151. static inline void pbuf_enqueue(uint8_t data)
  152. {
  153. if (!data)
  154. return;
  155. uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
  156. if (next != pbuf_tail) {
  157. pbuf[pbuf_head] = data;
  158. pbuf_head = next;
  159. } else {
  160. print("pbuf: full\n");
  161. }
  162. }
  163. static inline uint8_t pbuf_dequeue(void)
  164. {
  165. uint8_t val = 0;
  166. uint8_t sreg = SREG;
  167. cli();
  168. if (pbuf_head != pbuf_tail) {
  169. val = pbuf[pbuf_tail];
  170. pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
  171. }
  172. SREG = sreg;
  173. return val;
  174. }
  175. /* get data received by interrupt */
  176. uint8_t ps2_host_recv(void)
  177. {
  178. return pbuf_dequeue();
  179. }
  180. ISR(PS2_INT_VECT)
  181. {
  182. PORTC = 0xFF;
  183. /* interrupt means start bit comes */
  184. pbuf_enqueue(recv_data());
  185. /* release lines(idle state) */
  186. idle();
  187. _delay_us(5);
  188. PORTC = 0x00;
  189. }
  190. #endif
  191. /*
  192. static void ps2_reset(void)
  193. {
  194. ps2_host_send(0xFF);
  195. if (ps2_host_recv_response() == 0xFA) {
  196. _delay_ms(1000);
  197. ps2_host_recv_response();
  198. }
  199. }
  200. */
  201. /* send LED state to keyboard */
  202. void ps2_host_set_led(uint8_t led)
  203. {
  204. #ifdef PS2_INT_DISABLE
  205. PS2_INT_DISABLE();
  206. #endif
  207. ps2_host_send(0xED);
  208. ps2_host_recv_response();
  209. ps2_host_send(led);
  210. ps2_host_recv_response();
  211. #ifdef PS2_INT_ENABLE
  212. PS2_INT_ENABLE();
  213. idle();
  214. #endif
  215. }
  216. /* called after start bit comes */
  217. static uint8_t recv_data(void)
  218. {
  219. uint8_t data = 0;
  220. bool parity = true;
  221. ps2_error = 0;
  222. /* start bit [1] */
  223. WAIT(clock_lo, 1, 1);
  224. WAIT(data_lo, 1, 2);
  225. WAIT(clock_hi, 50, 3);
  226. /* data [2-9] */
  227. for (uint8_t i = 0; i < 8; i++) {
  228. WAIT(clock_lo, 50, 4);
  229. if (data_in()) {
  230. parity = !parity;
  231. data |= (1<<i);
  232. }
  233. WAIT(clock_hi, 50, 5);
  234. }
  235. /* parity [10] */
  236. WAIT(clock_lo, 50, 6);
  237. if (data_in() != parity) {
  238. ps2_error = PS2_ERR_PARITY;
  239. goto ERROR;
  240. }
  241. WAIT(clock_hi, 50, 7);
  242. /* stop bit [11] */
  243. WAIT(clock_lo, 50, 8);
  244. WAIT(data_hi, 1, 9);
  245. WAIT(clock_hi, 50, 10);
  246. return data;
  247. ERROR:
  248. return 0;
  249. }
  250. static inline void clock_lo()
  251. {
  252. PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
  253. PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
  254. }
  255. static inline void clock_hi()
  256. {
  257. /* input with pull up */
  258. PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
  259. PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
  260. }
  261. static inline bool clock_in()
  262. {
  263. PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
  264. PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
  265. return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
  266. }
  267. static inline void data_lo()
  268. {
  269. PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
  270. PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
  271. }
  272. static inline void data_hi()
  273. {
  274. /* input with pull up */
  275. PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
  276. PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
  277. }
  278. static inline bool data_in()
  279. {
  280. PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
  281. PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
  282. return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
  283. }
  284. static inline uint16_t wait_clock_lo(uint16_t us)
  285. {
  286. while (clock_in() && us) { asm(""); _delay_us(1); us--; }
  287. return us;
  288. }
  289. static inline uint16_t wait_clock_hi(uint16_t us)
  290. {
  291. while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
  292. return us;
  293. }
  294. static inline uint16_t wait_data_lo(uint16_t us)
  295. {
  296. while (data_in() && us) { asm(""); _delay_us(1); us--; }
  297. return us;
  298. }
  299. static inline uint16_t wait_data_hi(uint16_t us)
  300. {
  301. while (!data_in() && us) { asm(""); _delay_us(1); us--; }
  302. return us;
  303. }
  304. /* idle state that device can send */
  305. static inline void idle(void)
  306. {
  307. clock_hi();
  308. data_hi();
  309. }
  310. /* inhibit device to send */
  311. static inline void inhibit(void)
  312. {
  313. clock_lo();
  314. data_hi();
  315. }