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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

ps2_interrupt.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. Copyright 2010,2011,2012,2013 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. /*
  31. * PS/2 protocol Pin interrupt version
  32. */
  33. #include <stdbool.h>
  34. #include <avr/interrupt.h>
  35. #include <util/delay.h>
  36. #include "pbuff.h"
  37. #include "ps2.h"
  38. #include "ps2_io.h"
  39. #include "print.h"
  40. #define WAIT(stat, us, err) do { \
  41. if (!wait_##stat(us)) { \
  42. ps2_error = err; \
  43. goto ERROR; \
  44. } \
  45. } while (0)
  46. uint8_t ps2_error = PS2_ERR_NONE;
  47. void ps2_host_init(void)
  48. {
  49. idle();
  50. PS2_INT_INIT();
  51. PS2_INT_ON();
  52. // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
  53. //_delay_ms(2500);
  54. }
  55. uint8_t ps2_host_send(uint8_t data)
  56. {
  57. bool parity = true;
  58. ps2_error = PS2_ERR_NONE;
  59. PS2_INT_OFF();
  60. /* terminate a transmission if we have */
  61. inhibit();
  62. _delay_us(100); // 100us [4]p.13, [5]p.50
  63. /* 'Request to Send' and Start bit */
  64. data_lo();
  65. clock_hi();
  66. WAIT(clock_lo, 10000, 10); // 10ms [5]p.50
  67. /* Data bit[2-9] */
  68. for (uint8_t i = 0; i < 8; i++) {
  69. _delay_us(15);
  70. if (data&(1<<i)) {
  71. parity = !parity;
  72. data_hi();
  73. } else {
  74. data_lo();
  75. }
  76. WAIT(clock_hi, 50, 2);
  77. WAIT(clock_lo, 50, 3);
  78. }
  79. /* Parity bit */
  80. _delay_us(15);
  81. if (parity) { data_hi(); } else { data_lo(); }
  82. WAIT(clock_hi, 50, 4);
  83. WAIT(clock_lo, 50, 5);
  84. /* Stop bit */
  85. _delay_us(15);
  86. data_hi();
  87. /* Ack */
  88. WAIT(data_lo, 50, 6);
  89. WAIT(clock_lo, 50, 7);
  90. /* wait for idle state */
  91. WAIT(clock_hi, 50, 8);
  92. WAIT(data_hi, 50, 9);
  93. idle();
  94. PS2_INT_ON();
  95. return ps2_host_recv_response();
  96. ERROR:
  97. idle();
  98. PS2_INT_ON();
  99. return 0;
  100. }
  101. uint8_t ps2_host_recv_response(void)
  102. {
  103. // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
  104. uint8_t retry = 25;
  105. while (retry-- && !pbuf_has_data()) {
  106. _delay_ms(1);
  107. }
  108. return pbuf_dequeue();
  109. }
  110. /* get data received by interrupt */
  111. uint8_t ps2_host_recv(void)
  112. {
  113. if (pbuf_has_data()) {
  114. ps2_error = PS2_ERR_NONE;
  115. return pbuf_dequeue();
  116. } else {
  117. ps2_error = PS2_ERR_NODATA;
  118. return 0;
  119. }
  120. }
  121. ISR(PS2_INT_VECT)
  122. {
  123. static enum {
  124. INIT,
  125. START,
  126. BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7,
  127. PARITY,
  128. STOP,
  129. } state = INIT;
  130. static uint8_t data = 0;
  131. static uint8_t parity = 1;
  132. // TODO: abort if elapse 100us from previous interrupt
  133. // return unless falling edge
  134. if (clock_in()) {
  135. goto RETURN;
  136. }
  137. state++;
  138. switch (state) {
  139. case START:
  140. if (data_in())
  141. goto ERROR;
  142. break;
  143. case BIT0:
  144. case BIT1:
  145. case BIT2:
  146. case BIT3:
  147. case BIT4:
  148. case BIT5:
  149. case BIT6:
  150. case BIT7:
  151. data >>= 1;
  152. if (data_in()) {
  153. data |= 0x80;
  154. parity++;
  155. }
  156. break;
  157. case PARITY:
  158. if (data_in()) {
  159. if (!(parity & 0x01))
  160. goto ERROR;
  161. } else {
  162. if (parity & 0x01)
  163. goto ERROR;
  164. }
  165. break;
  166. case STOP:
  167. if (!data_in())
  168. goto ERROR;
  169. pbuf_enqueue(data);
  170. goto DONE;
  171. break;
  172. default:
  173. goto ERROR;
  174. }
  175. goto RETURN;
  176. ERROR:
  177. ps2_error = state;
  178. DONE:
  179. state = INIT;
  180. data = 0;
  181. parity = 1;
  182. RETURN:
  183. return;
  184. }
  185. /* send LED state to keyboard */
  186. void ps2_host_set_led(uint8_t led)
  187. {
  188. ps2_host_send(0xED);
  189. ps2_host_send(led);
  190. }