Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

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