From 4eb27ee89038e934dcb498df7508199efd9c93f1 Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 28 Nov 2013 14:20:00 +0900 Subject: [PATCH] Add ps2_interrupt.c --- protocol.mk | 2 +- protocol/ps2_busywait.c | 51 ++-- protocol/{ps2.c => ps2_interrupt.c} | 354 +++++++--------------------- protocol/ps2_usart.c | 23 +- 4 files changed, 122 insertions(+), 308 deletions(-) rename protocol/{ps2.c => ps2_interrupt.c} (51%) diff --git a/protocol.mk b/protocol.mk index e3320087..7f561e62 100644 --- a/protocol.mk +++ b/protocol.mk @@ -13,7 +13,7 @@ ifdef PS2_USE_BUSYWAIT endif ifdef PS2_USE_INT - SRC += protocol/ps2.c + SRC += protocol/ps2_interrupt.c OPT_DEFS += -DPS2_USE_INT endif diff --git a/protocol/ps2_busywait.c b/protocol/ps2_busywait.c index 5ab37787..05dd7b27 100644 --- a/protocol/ps2_busywait.c +++ b/protocol/ps2_busywait.c @@ -35,16 +35,16 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/* + * PS/2 protocol busywait version + */ + #include #include #include "ps2.h" #include "debug.h" -/* - * PS/2 protocol busywait version - */ - #define WAIT(stat, us, err) do { \ if (!wait_##stat(us)) { \ ps2_error = err; \ @@ -52,6 +52,7 @@ POSSIBILITY OF SUCH DAMAGE. } \ } while (0) + uint8_t ps2_error = PS2_ERR_NONE; @@ -65,18 +66,19 @@ void ps2_host_init(void) uint8_t ps2_host_send(uint8_t data) { - uint8_t res = 0; bool parity = true; ps2_error = PS2_ERR_NONE; + /* terminate a transmission if we have */ inhibit(); - _delay_us(200); // at least 100us + _delay_us(100); // 100us [4]p.13, [5]p.50 - /* start bit [1] */ + /* 'Request to Send' and Start bit */ data_lo(); clock_hi(); - WAIT(clock_lo, 20000, 10); // may take 15ms at most until device starts clocking - /* data [2-9] */ + WAIT(clock_lo, 10000, 10); // 10ms [5]p.50 + + /* Data bit */ for (uint8_t i = 0; i < 8; i++) { _delay_us(15); if (data&(1< -#include -#include #include #include "ps2.h" #include "debug.h" -#ifndef PS2_USE_INT -static uint8_t recv_data(void); -#endif -static inline void clock_lo(void); -static inline void clock_hi(void); -static inline bool clock_in(void); -static inline void data_lo(void); -static inline void data_hi(void); -static inline bool data_in(void); -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); - - -/* -Primitive PS/2 Library for AVR -============================== -Host side is only supported now. - - -I/O control ------------ -High state is asserted by input with pull up. - - -PS/2 References ---------------- -http://www.computer-engineering.org/ps2protocol/ -http://www.mcamafia.de/pdf/ibm_hitrc07.pdf -*/ - - #define WAIT(stat, us, err) do { \ if (!wait_##stat(us)) { \ ps2_error = err; \ @@ -89,38 +56,38 @@ http://www.mcamafia.de/pdf/ibm_hitrc07.pdf uint8_t ps2_error = PS2_ERR_NONE; +static inline uint8_t pbuf_dequeue(void); +static inline void pbuf_enqueue(uint8_t data); +static inline bool pbuf_has_data(void); +static inline void pbuf_clear(void); + + void ps2_host_init(void) { - // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20) - _delay_ms(2500); - -#ifdef PS2_USE_INT + idle(); PS2_INT_INIT(); PS2_INT_ON(); - idle(); -#else - inhibit(); -#endif + // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20) + //_delay_ms(2500); } -// TODO: send using interrupt if available uint8_t ps2_host_send(uint8_t data) { - uint8_t res = 0; bool parity = true; ps2_error = PS2_ERR_NONE; -#ifdef PS2_USE_INT + PS2_INT_OFF(); -#endif + /* terminate a transmission if we have */ inhibit(); - _delay_us(200); // at least 100us + _delay_us(100); // 100us [4]p.13, [5]p.50 - /* start bit [1] */ + /* 'Request to Send' and Start bit */ data_lo(); clock_hi(); - WAIT(clock_lo, 20000, 10); // may take 15ms at most until device starts clocking - /* data [2-9] */ + WAIT(clock_lo, 10000, 10); // 10ms [5]p.50 + + /* Data bit[2-9] */ for (uint8_t i = 0; i < 8; i++) { _delay_us(15); if (data&(1<