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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. #include <stdbool.h>
  31. #include <avr/io.h>
  32. #include <avr/interrupt.h>
  33. #include <util/delay.h>
  34. #include "ps2.h"
  35. #include "debug.h"
  36. static uint8_t recv_data(void);
  37. static inline void clock_lo(void);
  38. static inline void clock_hi(void);
  39. static inline bool clock_in(void);
  40. static inline void data_lo(void);
  41. static inline void data_hi(void);
  42. static inline bool data_in(void);
  43. static inline uint16_t wait_clock_lo(uint16_t us);
  44. static inline uint16_t wait_clock_hi(uint16_t us);
  45. static inline uint16_t wait_data_lo(uint16_t us);
  46. static inline uint16_t wait_data_hi(uint16_t us);
  47. static inline void idle(void);
  48. static inline void inhibit(void);
  49. /*
  50. Primitive PS/2 Library for AVR
  51. ==============================
  52. Host side is only supported now.
  53. I/O control
  54. -----------
  55. High state is asserted by input with pull up.
  56. PS/2 References
  57. ---------------
  58. http://www.computer-engineering.org/ps2protocol/
  59. http://www.mcamafia.de/pdf/ibm_hitrc07.pdf
  60. */
  61. #define WAIT(stat, us, err) do { \
  62. if (!wait_##stat(us)) { \
  63. ps2_error = err; \
  64. goto ERROR; \
  65. } \
  66. } while (0)
  67. uint8_t ps2_error = PS2_ERR_NONE;
  68. void ps2_host_init(void)
  69. {
  70. #ifdef PS2_USE_INT
  71. PS2_INT_INIT();
  72. PS2_INT_ON();
  73. idle();
  74. #else
  75. inhibit();
  76. #endif
  77. }
  78. // TODO: send using interrupt if available
  79. uint8_t ps2_host_send(uint8_t data)
  80. {
  81. uint8_t res = 0;
  82. bool parity = true;
  83. ps2_error = PS2_ERR_NONE;
  84. #ifdef PS2_USE_INT
  85. PS2_INT_OFF();
  86. #endif
  87. /* terminate a transmission if we have */
  88. inhibit();
  89. _delay_us(100);
  90. /* start bit [1] */
  91. data_lo();
  92. clock_hi();
  93. WAIT(clock_lo, 15000, 1);
  94. /* data [2-9] */
  95. for (uint8_t i = 0; i < 8; i++) {
  96. _delay_us(15);
  97. if (data&(1<<i)) {
  98. parity = !parity;
  99. data_hi();
  100. } else {
  101. data_lo();
  102. }
  103. WAIT(clock_hi, 50, 2);
  104. WAIT(clock_lo, 50, 3);
  105. }
  106. /* parity [10] */
  107. _delay_us(15);
  108. if (parity) { data_hi(); } else { data_lo(); }
  109. WAIT(clock_hi, 50, 4);
  110. WAIT(clock_lo, 50, 5);
  111. /* stop bit [11] */
  112. _delay_us(15);
  113. data_hi();
  114. /* ack [12] */
  115. WAIT(data_lo, 50, 6);
  116. WAIT(clock_lo, 50, 7);
  117. /* wait for idle state */
  118. WAIT(clock_hi, 50, 8);
  119. WAIT(data_hi, 50, 9);
  120. res = ps2_host_recv_response();
  121. ERROR:
  122. #ifdef PS2_USE_INT
  123. PS2_INT_ON();
  124. idle();
  125. #else
  126. inhibit();
  127. #endif
  128. return res;
  129. }
  130. /* receive data when host want else inhibit communication */
  131. uint8_t ps2_host_recv_response(void)
  132. {
  133. uint8_t data = 0;
  134. /* terminate a transmission if we have */
  135. inhibit();
  136. _delay_us(100);
  137. /* release lines(idle state) */
  138. idle();
  139. /* wait start bit */
  140. wait_clock_lo(2000);
  141. data = recv_data();
  142. inhibit();
  143. return data;
  144. }
  145. #ifndef PS2_USE_INT
  146. uint8_t ps2_host_recv(void)
  147. {
  148. return ps2_host_recv_response();
  149. }
  150. #else
  151. /* ring buffer to store ps/2 key data */
  152. #define PBUF_SIZE 8
  153. static uint8_t pbuf[PBUF_SIZE];
  154. static uint8_t pbuf_head = 0;
  155. static uint8_t pbuf_tail = 0;
  156. static inline void pbuf_enqueue(uint8_t data)
  157. {
  158. if (!data)
  159. return;
  160. uint8_t sreg = SREG;
  161. cli();
  162. uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
  163. if (next != pbuf_tail) {
  164. pbuf[pbuf_head] = data;
  165. pbuf_head = next;
  166. } else {
  167. debug("pbuf: full\n");
  168. }
  169. SREG = sreg;
  170. }
  171. static inline uint8_t pbuf_dequeue(void)
  172. {
  173. uint8_t val = 0;
  174. uint8_t sreg = SREG;
  175. cli();
  176. if (pbuf_head != pbuf_tail) {
  177. val = pbuf[pbuf_tail];
  178. pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
  179. }
  180. SREG = sreg;
  181. return val;
  182. }
  183. /* get data received by interrupt */
  184. uint8_t ps2_host_recv(void)
  185. {
  186. if (ps2_error) {
  187. print("x");
  188. phex(ps2_error);
  189. ps2_host_send(0xFE); // request to resend
  190. ps2_error = PS2_ERR_NONE;
  191. }
  192. idle();
  193. return pbuf_dequeue();
  194. }
  195. #if 0
  196. #define DEBUGP_INIT() do { DDRC = 0xFF; } while (0)
  197. #define DEBUGP(x) do { PORTC = x; } while (0)
  198. #else
  199. #define DEBUGP_INIT()
  200. #define DEBUGP(x)
  201. #endif
  202. ISR(PS2_INT_VECT)
  203. {
  204. static enum {
  205. INIT,
  206. START,
  207. BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7,
  208. PARITY,
  209. STOP,
  210. } state = INIT;
  211. static uint8_t data = 0;
  212. static uint8_t parity = 1;
  213. // TODO: abort if elapse 100us from previous interrupt
  214. // return unless falling edge
  215. if (clock_in()) {
  216. goto RETURN;
  217. }
  218. state++;
  219. DEBUGP(state);
  220. switch (state) {
  221. case START:
  222. if (data_in())
  223. goto ERROR;
  224. break;
  225. case BIT0:
  226. case BIT1:
  227. case BIT2:
  228. case BIT3:
  229. case BIT4:
  230. case BIT5:
  231. case BIT6:
  232. case BIT7:
  233. data >>= 1;
  234. if (data_in()) {
  235. data |= 0x80;
  236. parity++;
  237. }
  238. break;
  239. case PARITY:
  240. if (data_in()) {
  241. if (!(parity & 0x01))
  242. goto ERROR;
  243. } else {
  244. if (parity & 0x01)
  245. goto ERROR;
  246. }
  247. break;
  248. case STOP:
  249. if (!data_in())
  250. goto ERROR;
  251. pbuf_enqueue(data);
  252. goto DONE;
  253. break;
  254. default:
  255. goto ERROR;
  256. }
  257. goto RETURN;
  258. ERROR:
  259. DEBUGP(0x0F);
  260. inhibit();
  261. ps2_error = state;
  262. DONE:
  263. state = INIT;
  264. data = 0;
  265. parity = 1;
  266. RETURN:
  267. return;
  268. }
  269. #endif
  270. static void ps2_reset(void)
  271. {
  272. ps2_host_send(0xFF);
  273. }
  274. /* send LED state to keyboard */
  275. void ps2_host_set_led(uint8_t led)
  276. {
  277. ps2_host_send(0xED);
  278. ps2_host_send(led);
  279. }
  280. /* called after start bit comes */
  281. static uint8_t recv_data(void)
  282. {
  283. uint8_t data = 0;
  284. bool parity = true;
  285. ps2_error = PS2_ERR_NONE;
  286. /* start bit [1] */
  287. WAIT(clock_lo, 1, 1);
  288. WAIT(data_lo, 1, 2);
  289. WAIT(clock_hi, 50, 3);
  290. /* data [2-9] */
  291. for (uint8_t i = 0; i < 8; i++) {
  292. WAIT(clock_lo, 50, 4);
  293. if (data_in()) {
  294. parity = !parity;
  295. data |= (1<<i);
  296. }
  297. WAIT(clock_hi, 50, 5);
  298. }
  299. /* parity [10] */
  300. WAIT(clock_lo, 50, 6);
  301. if (data_in() != parity) {
  302. ps2_error = PS2_ERR_PARITY;
  303. goto ERROR;
  304. }
  305. WAIT(clock_hi, 50, 7);
  306. /* stop bit [11] */
  307. WAIT(clock_lo, 50, 8);
  308. WAIT(data_hi, 1, 9);
  309. WAIT(clock_hi, 50, 10);
  310. return data;
  311. ERROR:
  312. return 0;
  313. }
  314. static inline void clock_lo()
  315. {
  316. PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
  317. PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
  318. }
  319. static inline void clock_hi()
  320. {
  321. /* input with pull up */
  322. PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
  323. PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
  324. }
  325. static inline bool clock_in()
  326. {
  327. PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
  328. PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
  329. _delay_us(1);
  330. return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
  331. }
  332. static inline void data_lo()
  333. {
  334. PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
  335. PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
  336. }
  337. static inline void data_hi()
  338. {
  339. /* input with pull up */
  340. PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
  341. PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
  342. }
  343. static inline bool data_in()
  344. {
  345. PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
  346. PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
  347. _delay_us(1);
  348. return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
  349. }
  350. static inline uint16_t wait_clock_lo(uint16_t us)
  351. {
  352. while (clock_in() && us) { asm(""); _delay_us(1); us--; }
  353. return us;
  354. }
  355. static inline uint16_t wait_clock_hi(uint16_t us)
  356. {
  357. while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
  358. return us;
  359. }
  360. static inline uint16_t wait_data_lo(uint16_t us)
  361. {
  362. while (data_in() && us) { asm(""); _delay_us(1); us--; }
  363. return us;
  364. }
  365. static inline uint16_t wait_data_hi(uint16_t us)
  366. {
  367. while (!data_in() && us) { asm(""); _delay_us(1); us--; }
  368. return us;
  369. }
  370. /* idle state that device can send */
  371. static inline void idle(void)
  372. {
  373. clock_hi();
  374. data_hi();
  375. }
  376. /* inhibit device to send */
  377. static inline void inhibit(void)
  378. {
  379. clock_lo();
  380. data_hi();
  381. }