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_usart.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. Copyright 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. /*
  31. Primitive PS/2 Library for AVR
  32. ==============================
  33. Host side is only supported now.
  34. Synchronous USART is used to receive data by hardware process
  35. rather than interrupt. During V-USB interrupt runs, CLOCK interrupt
  36. cannot interpose. In the result it is prone to lost CLOCK edge.
  37. I/O control
  38. -----------
  39. High state is asserted by internal pull-up.
  40. If you have a signaling problem, you may need to have
  41. external pull-up resisters on CLOCK and DATA line.
  42. PS/2 References
  43. ---------------
  44. http://www.computer-engineering.org/ps2protocol/
  45. http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
  46. */
  47. #include <stdbool.h>
  48. #include <avr/io.h>
  49. #include <avr/interrupt.h>
  50. #include <util/delay.h>
  51. #include "ps2.h"
  52. #include "debug.h"
  53. #if 0
  54. #define DEBUGP_INIT() do { DDRC = 0xFF; } while (0)
  55. #define DEBUGP(x) do { PORTC = x; } while (0)
  56. #else
  57. #define DEBUGP_INIT()
  58. #define DEBUGP(x)
  59. #endif
  60. #define WAIT(stat, us, err) do { \
  61. if (!wait_##stat(us)) { \
  62. ps2_error = err; \
  63. goto ERROR; \
  64. } \
  65. } while (0)
  66. uint8_t ps2_error = PS2_ERR_NONE;
  67. static inline void clock_lo(void);
  68. static inline void clock_hi(void);
  69. static inline bool clock_in(void);
  70. static inline void data_lo(void);
  71. static inline void data_hi(void);
  72. static inline bool data_in(void);
  73. static inline uint16_t wait_clock_lo(uint16_t us);
  74. static inline uint16_t wait_clock_hi(uint16_t us);
  75. static inline uint16_t wait_data_lo(uint16_t us);
  76. static inline uint16_t wait_data_hi(uint16_t us);
  77. static inline void idle(void);
  78. static inline void inhibit(void);
  79. #if defined PS2_USE_INT || defined PS2_USE_USART
  80. static inline uint8_t pbuf_dequeue(void);
  81. static inline void pbuf_enqueue(uint8_t data);
  82. #endif
  83. void ps2_host_init(void)
  84. {
  85. DEBUGP_INIT();
  86. DEBUGP(0x1);
  87. idle();
  88. PS2_USART_INIT();
  89. PS2_USART_RX_INT_ON();
  90. }
  91. uint8_t ps2_host_send(uint8_t data)
  92. {
  93. uint8_t res = 0;
  94. bool parity = true;
  95. ps2_error = PS2_ERR_NONE;
  96. DEBUGP(0x6);
  97. PS2_USART_OFF();
  98. /* terminate a transmission if we have */
  99. inhibit();
  100. _delay_us(100);
  101. /* start bit [1] */
  102. data_lo();
  103. clock_hi();
  104. WAIT(clock_lo, 15000, 1);
  105. /* data [2-9] */
  106. for (uint8_t i = 0; i < 8; i++) {
  107. _delay_us(15);
  108. if (data&(1<<i)) {
  109. parity = !parity;
  110. data_hi();
  111. } else {
  112. data_lo();
  113. }
  114. WAIT(clock_hi, 50, 2);
  115. WAIT(clock_lo, 50, 3);
  116. }
  117. /* parity [10] */
  118. _delay_us(15);
  119. if (parity) { data_hi(); } else { data_lo(); }
  120. WAIT(clock_hi, 50, 4);
  121. WAIT(clock_lo, 50, 5);
  122. /* stop bit [11] */
  123. _delay_us(15);
  124. data_hi();
  125. /* ack [12] */
  126. WAIT(data_lo, 50, 6);
  127. WAIT(clock_lo, 50, 7);
  128. /* wait for idle state */
  129. WAIT(clock_hi, 50, 8);
  130. WAIT(data_hi, 50, 9);
  131. res = ps2_host_recv_response();
  132. ERROR:
  133. idle();
  134. PS2_USART_INIT();
  135. PS2_USART_RX_INT_ON();
  136. return res;
  137. }
  138. // Do polling data from keyboard to get response to last command.
  139. uint8_t ps2_host_recv_response(void)
  140. {
  141. uint8_t data = 0;
  142. PS2_USART_INIT();
  143. PS2_USART_RX_POLL_ON();
  144. while (!PS2_USART_RX_READY)
  145. ;
  146. data = PS2_USART_RX_DATA;
  147. PS2_USART_OFF();
  148. DEBUGP(0x9);
  149. return data;
  150. }
  151. uint8_t ps2_host_recv(void)
  152. {
  153. return pbuf_dequeue();
  154. }
  155. ISR(PS2_USART_RX_VECT)
  156. {
  157. DEBUGP(0x7);
  158. uint8_t error = PS2_USART_ERROR;
  159. uint8_t data = PS2_USART_RX_DATA;
  160. if (error) {
  161. DEBUGP(error>>2);
  162. } else {
  163. pbuf_enqueue(data);
  164. }
  165. DEBUGP(0x8);
  166. }
  167. /* send LED state to keyboard */
  168. void ps2_host_set_led(uint8_t led)
  169. {
  170. // send 0xED then keyboard keeps waiting for next LED data
  171. // and keyboard does not send any scan codes during waiting.
  172. // If fail to send LED data keyboard looks like being freezed.
  173. uint8_t retry = 3;
  174. while (retry-- && ps2_host_send(PS2_SET_LED) != PS2_ACK)
  175. ;
  176. retry = 3;
  177. while (retry-- && ps2_host_send(led) != PS2_ACK)
  178. ;
  179. }
  180. /*--------------------------------------------------------------------
  181. * static functions
  182. *------------------------------------------------------------------*/
  183. static inline void clock_lo()
  184. {
  185. PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
  186. PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
  187. }
  188. static inline void clock_hi()
  189. {
  190. /* input with pull up */
  191. PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
  192. PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
  193. }
  194. static inline bool clock_in()
  195. {
  196. PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
  197. PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
  198. _delay_us(1);
  199. return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
  200. }
  201. static inline void data_lo()
  202. {
  203. PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
  204. PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
  205. }
  206. static inline void data_hi()
  207. {
  208. /* input with pull up */
  209. PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
  210. PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
  211. }
  212. static inline bool data_in()
  213. {
  214. PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
  215. PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
  216. _delay_us(1);
  217. return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
  218. }
  219. static inline uint16_t wait_clock_lo(uint16_t us)
  220. {
  221. while (clock_in() && us) { asm(""); _delay_us(1); us--; }
  222. return us;
  223. }
  224. static inline uint16_t wait_clock_hi(uint16_t us)
  225. {
  226. while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
  227. return us;
  228. }
  229. static inline uint16_t wait_data_lo(uint16_t us)
  230. {
  231. while (data_in() && us) { asm(""); _delay_us(1); us--; }
  232. return us;
  233. }
  234. static inline uint16_t wait_data_hi(uint16_t us)
  235. {
  236. while (!data_in() && us) { asm(""); _delay_us(1); us--; }
  237. return us;
  238. }
  239. /* idle state that device can send */
  240. static inline void idle(void)
  241. {
  242. clock_hi();
  243. data_hi();
  244. }
  245. /* inhibit device to send */
  246. static inline void inhibit(void)
  247. {
  248. clock_lo();
  249. data_hi();
  250. }
  251. /*--------------------------------------------------------------------
  252. * Ring buffer to store scan codes from keyboard
  253. *------------------------------------------------------------------*/
  254. #define PBUF_SIZE 8
  255. static uint8_t pbuf[PBUF_SIZE];
  256. static uint8_t pbuf_head = 0;
  257. static uint8_t pbuf_tail = 0;
  258. static inline void pbuf_enqueue(uint8_t data)
  259. {
  260. if (!data)
  261. return;
  262. uint8_t sreg = SREG;
  263. cli();
  264. uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
  265. if (next != pbuf_tail) {
  266. pbuf[pbuf_head] = data;
  267. pbuf_head = next;
  268. } else {
  269. debug("pbuf: full\n");
  270. }
  271. SREG = sreg;
  272. }
  273. static inline uint8_t pbuf_dequeue(void)
  274. {
  275. uint8_t val = 0;
  276. uint8_t sreg = SREG;
  277. cli();
  278. if (pbuf_head != pbuf_tail) {
  279. val = pbuf[pbuf_tail];
  280. pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
  281. }
  282. SREG = sreg;
  283. return val;
  284. }