diff options
| author | tmk <nobody@nowhere> | 2013-11-25 17:43:26 +0900 |
|---|---|---|
| committer | tmk <nobody@nowhere> | 2013-11-25 17:43:26 +0900 |
| commit | 9ae9742ac59b18cf989370f53d669daeb75bd7a3 (patch) | |
| tree | 2a661eaef74bb7817ff5d03ff115ec00249e188a /protocol/ps2.h | |
| parent | d5ecbb83daa13b806ee4879692aaf807b6b1b69f (diff) | |
| download | qmk_firmware-9ae9742ac59b18cf989370f53d669daeb75bd7a3.tar.gz qmk_firmware-9ae9742ac59b18cf989370f53d669daeb75bd7a3.zip | |
Add ps2_busywait.c and recfactor PS/2 protocol
Diffstat (limited to 'protocol/ps2.h')
| -rw-r--r-- | protocol/ps2.h | 144 |
1 files changed, 122 insertions, 22 deletions
diff --git a/protocol/ps2.h b/protocol/ps2.h index 834165356..b780d01a2 100644 --- a/protocol/ps2.h +++ b/protocol/ps2.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | Copyright 2010,2011 Jun WAKO <wakojun@gmail.com> | 2 | Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com> |
| 3 | 3 | ||
| 4 | This software is licensed with a Modified BSD License. | 4 | This software is licensed with a Modified BSD License. |
| 5 | All of this is supposed to be Free Software, Open Source, DFSG-free, | 5 | All of this is supposed to be Free Software, Open Source, DFSG-free, |
| @@ -37,32 +37,44 @@ POSSIBILITY OF SUCH DAMAGE. | |||
| 37 | 37 | ||
| 38 | #ifndef PS2_H | 38 | #ifndef PS2_H |
| 39 | #define PS2_H | 39 | #define PS2_H |
| 40 | |||
| 41 | #include <stdbool.h> | ||
| 42 | #include <util/delay.h> | ||
| 43 | #include <avr/io.h> | ||
| 44 | |||
| 40 | /* | 45 | /* |
| 41 | * Primitive PS/2 Library for AVR | 46 | * Primitive PS/2 Library for AVR |
| 47 | * | ||
| 48 | * PS/2 Resources | ||
| 49 | * -------------- | ||
| 50 | * [1] The PS/2 Mouse/Keyboard Protocol | ||
| 51 | * http://www.computer-engineering.org/ps2protocol/ | ||
| 52 | * Concise and thorough primer of PS/2 protocol. | ||
| 53 | * | ||
| 54 | * [2] Keyboard and Auxiliary Device Controller | ||
| 55 | * http://www.mcamafia.de/pdf/ibm_hitrc07.pdf | ||
| 56 | * Signal Timing and Format | ||
| 57 | * | ||
| 58 | * [3] Keyboards(101- and 102-key) | ||
| 59 | * http://www.mcamafia.de/pdf/ibm_hitrc11.pdf | ||
| 60 | * Keyboard Layout, Scan Code Set, POR, and Commands. | ||
| 61 | * | ||
| 62 | * [4] PS/2 Reference Manuals | ||
| 63 | * http://www.mcamafia.de/pdf/ibm_hitrc07.pdf | ||
| 64 | * Collection of IBM Personal System/2 documents. | ||
| 65 | * | ||
| 66 | * [5] TrackPoint Engineering Specifications for version 3E | ||
| 67 | * https://web.archive.org/web/20100526161812/http://wwwcssrv.almaden.ibm.com/trackpoint/download.html | ||
| 42 | */ | 68 | */ |
| 43 | |||
| 44 | |||
| 45 | /* port settings for clock and data line */ | ||
| 46 | #if !(defined(PS2_CLOCK_PORT) && \ | ||
| 47 | defined(PS2_CLOCK_PIN) && \ | ||
| 48 | defined(PS2_CLOCK_DDR) && \ | ||
| 49 | defined(PS2_CLOCK_BIT)) | ||
| 50 | # error "PS/2 clock port setting is required in config.h" | ||
| 51 | #endif | ||
| 52 | |||
| 53 | #if !(defined(PS2_DATA_PORT) && \ | ||
| 54 | defined(PS2_DATA_PIN) && \ | ||
| 55 | defined(PS2_DATA_DDR) && \ | ||
| 56 | defined(PS2_DATA_BIT)) | ||
| 57 | # error "PS/2 data port setting is required in config.h" | ||
| 58 | #endif | ||
| 59 | |||
| 60 | #define PS2_ACK 0xFA | 69 | #define PS2_ACK 0xFA |
| 61 | #define PS2_RESEND 0xFE | 70 | #define PS2_RESEND 0xFE |
| 62 | #define PS2_SET_LED 0xED | 71 | #define PS2_SET_LED 0xED |
| 63 | 72 | ||
| 64 | #define PS2_ERR_NONE 0 | 73 | #define PS2_ERR_NONE 0 |
| 65 | #define PS2_ERR_PARITY 0x10 | 74 | #define PS2_ERR_STARTBIT1 1 |
| 75 | #define PS2_ERR_STARTBIT2 2 | ||
| 76 | #define PS2_ERR_STARTBIT3 3 | ||
| 77 | #define PS2_ERR_PARITY 0x10 | ||
| 66 | 78 | ||
| 67 | #define PS2_LED_SCROLL_LOCK 0 | 79 | #define PS2_LED_SCROLL_LOCK 0 |
| 68 | #define PS2_LED_NUM_LOCK 1 | 80 | #define PS2_LED_NUM_LOCK 1 |
| @@ -71,13 +83,101 @@ POSSIBILITY OF SUCH DAMAGE. | |||
| 71 | 83 | ||
| 72 | extern uint8_t ps2_error; | 84 | extern uint8_t ps2_error; |
| 73 | 85 | ||
| 74 | /* host role */ | ||
| 75 | void ps2_host_init(void); | 86 | void ps2_host_init(void); |
| 76 | uint8_t ps2_host_send(uint8_t data); | 87 | uint8_t ps2_host_send(uint8_t data); |
| 77 | uint8_t ps2_host_recv_response(void); | 88 | uint8_t ps2_host_recv_response(void); |
| 78 | uint8_t ps2_host_recv(void); | 89 | uint8_t ps2_host_recv(void); |
| 79 | void ps2_host_set_led(uint8_t usb_led); | 90 | void ps2_host_set_led(uint8_t usb_led); |
| 80 | 91 | ||
| 81 | /* device role */ | 92 | |
| 93 | /* Check port settings for clock and data line */ | ||
| 94 | #if !(defined(PS2_CLOCK_PORT) && \ | ||
| 95 | defined(PS2_CLOCK_PIN) && \ | ||
| 96 | defined(PS2_CLOCK_DDR) && \ | ||
| 97 | defined(PS2_CLOCK_BIT)) | ||
| 98 | # error "PS/2 clock port setting is required in config.h" | ||
| 99 | #endif | ||
| 100 | |||
| 101 | #if !(defined(PS2_DATA_PORT) && \ | ||
| 102 | defined(PS2_DATA_PIN) && \ | ||
| 103 | defined(PS2_DATA_DDR) && \ | ||
| 104 | defined(PS2_DATA_BIT)) | ||
| 105 | # error "PS/2 data port setting is required in config.h" | ||
| 106 | #endif | ||
| 107 | |||
| 108 | /*-------------------------------------------------------------------- | ||
| 109 | * static functions | ||
| 110 | *------------------------------------------------------------------*/ | ||
| 111 | static inline void clock_lo(void) | ||
| 112 | { | ||
| 113 | PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT); | ||
| 114 | PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT); | ||
| 115 | } | ||
| 116 | static inline void clock_hi(void) | ||
| 117 | { | ||
| 118 | /* input with pull up */ | ||
| 119 | PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT); | ||
| 120 | PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT); | ||
| 121 | } | ||
| 122 | static inline bool clock_in(void) | ||
| 123 | { | ||
| 124 | PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT); | ||
| 125 | PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT); | ||
| 126 | _delay_us(1); | ||
| 127 | return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT); | ||
| 128 | } | ||
| 129 | static inline void data_lo(void) | ||
| 130 | { | ||
| 131 | PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT); | ||
| 132 | PS2_DATA_DDR |= (1<<PS2_DATA_BIT); | ||
| 133 | } | ||
| 134 | static inline void data_hi(void) | ||
| 135 | { | ||
| 136 | /* input with pull up */ | ||
| 137 | PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT); | ||
| 138 | PS2_DATA_PORT |= (1<<PS2_DATA_BIT); | ||
| 139 | } | ||
| 140 | static inline bool data_in(void) | ||
| 141 | { | ||
| 142 | PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT); | ||
| 143 | PS2_DATA_PORT |= (1<<PS2_DATA_BIT); | ||
| 144 | _delay_us(1); | ||
| 145 | return PS2_DATA_PIN&(1<<PS2_DATA_BIT); | ||
| 146 | } | ||
| 147 | |||
| 148 | static inline uint16_t wait_clock_lo(uint16_t us) | ||
| 149 | { | ||
| 150 | while (clock_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 151 | return us; | ||
| 152 | } | ||
| 153 | static inline uint16_t wait_clock_hi(uint16_t us) | ||
| 154 | { | ||
| 155 | while (!clock_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 156 | return us; | ||
| 157 | } | ||
| 158 | static inline uint16_t wait_data_lo(uint16_t us) | ||
| 159 | { | ||
| 160 | while (data_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 161 | return us; | ||
| 162 | } | ||
| 163 | static inline uint16_t wait_data_hi(uint16_t us) | ||
| 164 | { | ||
| 165 | while (!data_in() && us) { asm(""); _delay_us(1); us--; } | ||
| 166 | return us; | ||
| 167 | } | ||
| 168 | |||
| 169 | /* idle state that device can send */ | ||
| 170 | static inline void idle(void) | ||
| 171 | { | ||
| 172 | clock_hi(); | ||
| 173 | data_hi(); | ||
| 174 | } | ||
| 175 | |||
| 176 | /* inhibit device to send */ | ||
| 177 | static inline void inhibit(void) | ||
| 178 | { | ||
| 179 | clock_lo(); | ||
| 180 | data_hi(); | ||
| 181 | } | ||
| 82 | 182 | ||
| 83 | #endif | 183 | #endif |
