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.h 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #ifndef PS2_H
  31. #define PS2_H
  32. #include <stdbool.h>
  33. #include "wait.h"
  34. #include "ps2_io.h"
  35. #include "print.h"
  36. /*
  37. * Primitive PS/2 Library for AVR
  38. *
  39. * PS/2 Resources
  40. * --------------
  41. * [1] The PS/2 Mouse/Keyboard Protocol
  42. * http://www.computer-engineering.org/ps2protocol/
  43. * Concise and thorough primer of PS/2 protocol.
  44. *
  45. * [2] Keyboard and Auxiliary Device Controller
  46. * http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
  47. * Signal Timing and Format
  48. *
  49. * [3] Keyboards(101- and 102-key)
  50. * http://www.mcamafia.de/pdf/ibm_hitrc11.pdf
  51. * Keyboard Layout, Scan Code Set, POR, and Commands.
  52. *
  53. * [4] PS/2 Reference Manuals
  54. * http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
  55. * Collection of IBM Personal System/2 documents.
  56. *
  57. * [5] TrackPoint Engineering Specifications for version 3E
  58. * https://web.archive.org/web/20100526161812/http://wwwcssrv.almaden.ibm.com/trackpoint/download.html
  59. */
  60. #define PS2_ACK 0xFA
  61. #define PS2_RESEND 0xFE
  62. #define PS2_SET_LED 0xED
  63. // TODO: error numbers
  64. #define PS2_ERR_NONE 0
  65. #define PS2_ERR_STARTBIT1 1
  66. #define PS2_ERR_STARTBIT2 2
  67. #define PS2_ERR_STARTBIT3 3
  68. #define PS2_ERR_PARITY 0x10
  69. #define PS2_ERR_NODATA 0x20
  70. #define PS2_LED_SCROLL_LOCK 0
  71. #define PS2_LED_NUM_LOCK 1
  72. #define PS2_LED_CAPS_LOCK 2
  73. extern uint8_t ps2_error;
  74. void ps2_host_init(void);
  75. uint8_t ps2_host_send(uint8_t data);
  76. uint8_t ps2_host_recv_response(void);
  77. uint8_t ps2_host_recv(void);
  78. void ps2_host_set_led(uint8_t usb_led);
  79. /*--------------------------------------------------------------------
  80. * static functions
  81. *------------------------------------------------------------------*/
  82. static inline uint16_t wait_clock_lo(uint16_t us)
  83. {
  84. while (clock_in() && us) { asm(""); wait_us(1); us--; }
  85. return us;
  86. }
  87. static inline uint16_t wait_clock_hi(uint16_t us)
  88. {
  89. while (!clock_in() && us) { asm(""); wait_us(1); us--; }
  90. return us;
  91. }
  92. static inline uint16_t wait_data_lo(uint16_t us)
  93. {
  94. while (data_in() && us) { asm(""); wait_us(1); us--; }
  95. return us;
  96. }
  97. static inline uint16_t wait_data_hi(uint16_t us)
  98. {
  99. while (!data_in() && us) { asm(""); wait_us(1); us--; }
  100. return us;
  101. }
  102. /* idle state that device can send */
  103. static inline void idle(void)
  104. {
  105. clock_hi();
  106. data_hi();
  107. }
  108. /* inhibit device to send */
  109. static inline void inhibit(void)
  110. {
  111. clock_lo();
  112. data_hi();
  113. }
  114. #endif