X-Git-Url: https://git.friedersdorff.com/?a=blobdiff_plain;f=ps2.c;h=8a05916210d77b4741c060c543a45fad75a102e2;hb=e65575d4a5f03a90adfa9850cb71a5d0448098cd;hp=adb425ae3345e0d548a80a5cdcb8c0e0c5d9474b;hpb=2a562a4191a5b9e88731efb5eabd921821f25f7e;p=max%2Ftmk_keyboard.git diff --git a/ps2.c b/ps2.c index adb425ae..8a059162 100644 --- a/ps2.c +++ b/ps2.c @@ -1,5 +1,5 @@ /* -Copyright (c) 2010 Jun WAKO +Copyright 2010,2011 Jun WAKO This software is licensed with a Modified BSD License. All of this is supposed to be Free Software, Open Source, DFSG-free, @@ -34,14 +34,16 @@ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + #include #include +#include #include #include "ps2.h" -#include "print.h" #include "debug.h" +static uint8_t recv_data(void); static inline void clock_lo(void); static inline void clock_hi(void); static inline bool clock_in(void); @@ -52,6 +54,8 @@ static inline uint16_t wait_clock_lo(uint16_t us); static inline uint16_t wait_clock_hi(uint16_t us); static inline uint16_t wait_data_lo(uint16_t us); static inline uint16_t wait_data_hi(uint16_t us); +static inline void idle(void); +static inline void inhibit(void); /* @@ -79,38 +83,40 @@ http://www.mcamafia.de/pdf/ibm_hitrc07.pdf } \ } while (0) -#define WAIT_NORETRY(stat, us, err) do { \ - if (!wait_##stat(us)) { \ - ps2_error = err; \ - return 0; \ - } \ -} while (0) - uint8_t ps2_error = PS2_ERR_NONE; void ps2_host_init(void) { - /* inhibit */ - clock_lo(); - data_hi(); +#ifdef PS2_INT_ENABLE + PS2_INT_ENABLE(); + idle(); +#else + inhibit(); +#endif } +// TODO: send using interrupt if available uint8_t ps2_host_send(uint8_t data) { + uint8_t res = 0; bool parity = true; - ps2_error = 0; - - /* request to send */ - clock_lo(); - data_lo(); + ps2_error = PS2_ERR_NONE; +#ifdef PS2_INT_DISABLE + PS2_INT_DISABLE(); +#endif + /* terminate a transmission if we have */ + inhibit(); _delay_us(100); + /* start bit [1] */ + data_lo(); clock_hi(); WAIT(clock_lo, 15000, 1); /* data [2-9] */ for (uint8_t i = 0; i < 8; i++) { + _delay_us(15); if (data&(1<>= 1; + if (data_in()) { + data |= 0x80; + parity++; + } + break; + case PARITY: + if (data_in()) { + if (!(parity & 0x01)) + goto ERROR; + } else { + if (parity & 0x01) + goto ERROR; + } + break; + case STOP: + if (!data_in()) + goto ERROR; + pbuf_enqueue(data); + goto DONE; + break; + default: + goto ERROR; + } + goto RETURN; +ERROR: + DEBUGP(0x0F); + inhibit(); + ps2_error = state; +DONE: + state = INIT; + data = 0; + parity = 1; +RETURN: + return; +} +#endif + + +static void ps2_reset(void) +{ + ps2_host_send(0xFF); +} + +/* send LED state to keyboard */ +void ps2_host_set_led(uint8_t led) +{ + ps2_host_send(0xED); + ps2_host_send(led); +} + + +/* called after start bit comes */ +static uint8_t recv_data(void) +{ + uint8_t data = 0; + bool parity = true; + ps2_error = PS2_ERR_NONE; /* start bit [1] */ - WAIT(clock_lo, 20000, 1); + WAIT(clock_lo, 1, 1); WAIT(data_lo, 1, 2); WAIT(clock_hi, 50, 3); @@ -182,15 +356,11 @@ uint8_t ps2_host_recv(void) WAIT(data_hi, 1, 9); WAIT(clock_hi, 50, 10); - /* inhibit device to send */ - clock_lo(); - return data; ERROR: return 0; } - static inline void clock_lo() { PS2_CLOCK_PORT &= ~(1<