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_busywait.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 busywait version
  32. */
  33. #include <stdbool.h>
  34. #include "wait.h"
  35. #include "ps2.h"
  36. #include "ps2_io.h"
  37. #include "debug.h"
  38. #define WAIT(stat, us, err) do { \
  39. if (!wait_##stat(us)) { \
  40. ps2_error = err; \
  41. goto ERROR; \
  42. } \
  43. } while (0)
  44. uint8_t ps2_error = PS2_ERR_NONE;
  45. void ps2_host_init(void)
  46. {
  47. clock_init();
  48. data_init();
  49. // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
  50. wait_ms(2500);
  51. inhibit();
  52. }
  53. uint8_t ps2_host_send(uint8_t data)
  54. {
  55. bool parity = true;
  56. ps2_error = PS2_ERR_NONE;
  57. /* terminate a transmission if we have */
  58. inhibit();
  59. wait_us(100); // 100us [4]p.13, [5]p.50
  60. /* 'Request to Send' and Start bit */
  61. data_lo();
  62. clock_hi();
  63. WAIT(clock_lo, 10000, 10); // 10ms [5]p.50
  64. /* Data bit */
  65. for (uint8_t i = 0; i < 8; i++) {
  66. wait_us(15);
  67. if (data&(1<<i)) {
  68. parity = !parity;
  69. data_hi();
  70. } else {
  71. data_lo();
  72. }
  73. WAIT(clock_hi, 50, 2);
  74. WAIT(clock_lo, 50, 3);
  75. }
  76. /* Parity bit */
  77. wait_us(15);
  78. if (parity) { data_hi(); } else { data_lo(); }
  79. WAIT(clock_hi, 50, 4);
  80. WAIT(clock_lo, 50, 5);
  81. /* Stop bit */
  82. wait_us(15);
  83. data_hi();
  84. /* Ack */
  85. WAIT(data_lo, 50, 6);
  86. WAIT(clock_lo, 50, 7);
  87. /* wait for idle state */
  88. WAIT(clock_hi, 50, 8);
  89. WAIT(data_hi, 50, 9);
  90. inhibit();
  91. return ps2_host_recv_response();
  92. ERROR:
  93. inhibit();
  94. return 0;
  95. }
  96. /* receive data when host want else inhibit communication */
  97. uint8_t ps2_host_recv_response(void)
  98. {
  99. // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
  100. // 250 * 100us(wait for start bit in ps2_host_recv)
  101. uint8_t data = 0;
  102. uint8_t try = 250;
  103. do {
  104. data = ps2_host_recv();
  105. } while (try-- && ps2_error);
  106. return data;
  107. }
  108. /* called after start bit comes */
  109. uint8_t ps2_host_recv(void)
  110. {
  111. uint8_t data = 0;
  112. bool parity = true;
  113. ps2_error = PS2_ERR_NONE;
  114. /* release lines(idle state) */
  115. idle();
  116. /* start bit [1] */
  117. WAIT(clock_lo, 100, 1); // TODO: this is enough?
  118. WAIT(data_lo, 1, 2);
  119. WAIT(clock_hi, 50, 3);
  120. /* data [2-9] */
  121. for (uint8_t i = 0; i < 8; i++) {
  122. WAIT(clock_lo, 50, 4);
  123. if (data_in()) {
  124. parity = !parity;
  125. data |= (1<<i);
  126. }
  127. WAIT(clock_hi, 50, 5);
  128. }
  129. /* parity [10] */
  130. WAIT(clock_lo, 50, 6);
  131. if (data_in() != parity) {
  132. ps2_error = PS2_ERR_PARITY;
  133. goto ERROR;
  134. }
  135. WAIT(clock_hi, 50, 7);
  136. /* stop bit [11] */
  137. WAIT(clock_lo, 50, 8);
  138. WAIT(data_hi, 1, 9);
  139. WAIT(clock_hi, 50, 10);
  140. inhibit();
  141. return data;
  142. ERROR:
  143. if (ps2_error > PS2_ERR_STARTBIT3) {
  144. xprintf("x%02X\n", ps2_error);
  145. }
  146. inhibit();
  147. return 0;
  148. }
  149. /* send LED state to keyboard */
  150. void ps2_host_set_led(uint8_t led)
  151. {
  152. ps2_host_send(0xED);
  153. ps2_host_send(led);
  154. }