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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. Copyright (c) 2010 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 <util/delay.h>
  33. #include "ps2.h"
  34. #include "print.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. /*
  47. Primitive PS/2 Library for AVR
  48. ==============================
  49. Host side is only supported now.
  50. I/O control
  51. -----------
  52. High state is asserted by input with pull up.
  53. PS/2 References
  54. ---------------
  55. http://www.computer-engineering.org/ps2protocol/
  56. http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
  57. */
  58. #define WAIT(stat, us, err) do { \
  59. if (!wait_##stat(us)) { \
  60. ps2_error = err; \
  61. goto ERROR; \
  62. } \
  63. } while (0)
  64. #define WAIT_NORETRY(stat, us, err) do { \
  65. if (!wait_##stat(us)) { \
  66. ps2_error = err; \
  67. return 0; \
  68. } \
  69. } while (0)
  70. uint8_t ps2_error = PS2_ERR_NONE;
  71. void ps2_host_init(void)
  72. {
  73. /* inhibit */
  74. clock_lo();
  75. data_hi();
  76. }
  77. uint8_t ps2_host_send(uint8_t data)
  78. {
  79. bool parity = true;
  80. ps2_error = 0;
  81. /* request to send */
  82. clock_lo();
  83. _delay_us(100);
  84. /* start bit [1] */
  85. data_lo();
  86. clock_hi();
  87. WAIT(clock_lo, 15000, 1);
  88. /* data [2-9] */
  89. for (uint8_t i = 0; i < 8; i++) {
  90. if (data&(1<<i)) {
  91. parity = !parity;
  92. data_hi();
  93. } else {
  94. data_lo();
  95. }
  96. WAIT(clock_hi, 50, 2);
  97. WAIT(clock_lo, 50, 3);
  98. }
  99. /* parity [10] */
  100. if (parity) { data_hi(); } else { data_lo(); }
  101. WAIT(clock_hi, 50, 4);
  102. WAIT(clock_lo, 50, 5);
  103. /* stop bit [11] */
  104. data_hi();
  105. /* ack [12] */
  106. WAIT(data_lo, 50, 6);
  107. WAIT(clock_lo, 50, 7);
  108. WAIT(clock_hi, 50, 8);
  109. WAIT(data_hi, 50, 9);
  110. /* inhibit device to send */
  111. clock_lo();
  112. return 1;
  113. ERROR:
  114. /* inhibit device to send */
  115. data_hi();
  116. clock_lo();
  117. return 0;
  118. }
  119. uint8_t ps2_host_recv(void)
  120. {
  121. uint8_t data = 0;
  122. bool parity = true;
  123. ps2_error = 0;
  124. /* terminate a transmission if we have */
  125. clock_lo();
  126. _delay_us(100);
  127. /* release lines(idle state) */
  128. clock_hi();
  129. data_hi();
  130. /* start bit [1] */
  131. WAIT(clock_lo, 2000, 1); // How long should we wait?
  132. WAIT(data_lo, 1, 2);
  133. WAIT(clock_hi, 50, 3);
  134. /* data [2-9] */
  135. for (uint8_t i = 0; i < 8; i++) {
  136. WAIT(clock_lo, 50, 4);
  137. if (data_in()) {
  138. parity = !parity;
  139. data |= (1<<i);
  140. }
  141. WAIT(clock_hi, 50, 5);
  142. }
  143. /* parity [10] */
  144. WAIT(clock_lo, 50, 6);
  145. if (data_in() != parity) {
  146. ps2_error = PS2_ERR_PARITY;
  147. goto ERROR;
  148. }
  149. WAIT(clock_hi, 50, 7);
  150. /* stop bit [11] */
  151. WAIT(clock_lo, 50, 8);
  152. WAIT(data_hi, 1, 9);
  153. WAIT(clock_hi, 50, 10);
  154. /* inhibit device to send */
  155. clock_lo();
  156. return data;
  157. ERROR:
  158. /* inhibit device to send */
  159. data_hi();
  160. clock_lo();
  161. return 0;
  162. }
  163. static inline void clock_lo()
  164. {
  165. PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
  166. PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
  167. }
  168. static inline void clock_hi()
  169. {
  170. /* input with pull up */
  171. PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
  172. PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
  173. }
  174. static inline bool clock_in()
  175. {
  176. PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
  177. PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
  178. return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
  179. }
  180. static inline void data_lo()
  181. {
  182. PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
  183. PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
  184. }
  185. static inline void data_hi()
  186. {
  187. /* input with pull up */
  188. PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
  189. PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
  190. }
  191. static inline bool data_in()
  192. {
  193. PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
  194. PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
  195. return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
  196. }
  197. static inline uint16_t wait_clock_lo(uint16_t us)
  198. {
  199. while (clock_in() && us) { asm(""); _delay_us(1); us--; }
  200. return us;
  201. }
  202. static inline uint16_t wait_clock_hi(uint16_t us)
  203. {
  204. while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
  205. return us;
  206. }
  207. static inline uint16_t wait_data_lo(uint16_t us)
  208. {
  209. while (data_in() && us) { asm(""); _delay_us(1); us--; }
  210. return us;
  211. }
  212. static inline uint16_t wait_data_hi(uint16_t us)
  213. {
  214. while (!data_in() && us) { asm(""); _delay_us(1); us--; }
  215. return us;
  216. }