Browse Source

Fix PS/2 USART version

keymap_in_eeprom
tmk 10 years ago
parent
commit
ccbc1dd8e7
1 changed files with 25 additions and 29 deletions
  1. 25
    29
      protocol/ps2_usart.c

+ 25
- 29
protocol/ps2_usart.c View File

/* /*
Copyright 2010,2011 Jun WAKO <[email protected]>
Copyright 2010,2011,2012,2013 Jun WAKO <[email protected]>


This software is licensed with a Modified BSD License. This software is licensed with a Modified BSD License.
All of this is supposed to be Free Software, Open Source, DFSG-free, All of this is supposed to be Free Software, Open Source, DFSG-free,
#include "debug.h" #include "debug.h"




#if 0
#define DEBUGP_INIT() do { DDRC = 0xFF; } while (0)
#define DEBUGP(x) do { PORTC = x; } while (0)
#else
#define DEBUGP_INIT()
#define DEBUGP(x)
#endif

#define WAIT(stat, us, err) do { \ #define WAIT(stat, us, err) do { \
if (!wait_##stat(us)) { \ if (!wait_##stat(us)) { \
ps2_error = err; \ ps2_error = err; \
static inline void inhibit(void); static inline void inhibit(void);
static inline uint8_t pbuf_dequeue(void); static inline uint8_t pbuf_dequeue(void);
static inline void pbuf_enqueue(uint8_t data); 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) void ps2_host_init(void)
{ {
DEBUGP_INIT();
DEBUGP(0x1);
idle(); idle();
PS2_USART_INIT(); PS2_USART_INIT();
PS2_USART_RX_INT_ON(); PS2_USART_RX_INT_ON();
bool parity = true; bool parity = true;
ps2_error = PS2_ERR_NONE; ps2_error = PS2_ERR_NONE;


DEBUGP(0x6);
PS2_USART_OFF(); PS2_USART_OFF();


/* terminate a transmission if we have */ /* terminate a transmission if we have */
WAIT(clock_hi, 50, 8); WAIT(clock_hi, 50, 8);
WAIT(data_hi, 50, 9); WAIT(data_hi, 50, 9);


PS2_USART_INIT();
PS2_USART_RX_INT_ON();
res = ps2_host_recv_response(); res = ps2_host_recv_response();
ERROR: ERROR:
idle(); idle();
// Do polling data from keyboard to get response to last command. // Do polling data from keyboard to get response to last command.
uint8_t ps2_host_recv_response(void) uint8_t ps2_host_recv_response(void)
{ {
uint8_t data = 0;
PS2_USART_INIT();
PS2_USART_RX_POLL_ON();
while (!PS2_USART_RX_READY)
;
data = PS2_USART_RX_DATA;
PS2_USART_OFF();
DEBUGP(0x9);
return data;
while (!pbuf_has_data()) {
_delay_ms(1); // without this delay it seems to fall into deadlock
}
return pbuf_dequeue();
} }


uint8_t ps2_host_recv(void) uint8_t ps2_host_recv(void)


ISR(PS2_USART_RX_VECT) ISR(PS2_USART_RX_VECT)
{ {
DEBUGP(0x7);
uint8_t error = PS2_USART_ERROR; uint8_t error = PS2_USART_ERROR;
uint8_t data = PS2_USART_RX_DATA; uint8_t data = PS2_USART_RX_DATA;
if (error) {
DEBUGP(error>>2);
} else {
if (!error) {
pbuf_enqueue(data); pbuf_enqueue(data);
} }
DEBUGP(0x8);
} }


/* send LED state to keyboard */ /* send LED state to keyboard */
static uint8_t pbuf_tail = 0; static uint8_t pbuf_tail = 0;
static inline void pbuf_enqueue(uint8_t data) static inline void pbuf_enqueue(uint8_t data)
{ {
if (!data)
return;

uint8_t sreg = SREG; uint8_t sreg = SREG;
cli(); cli();
uint8_t next = (pbuf_head + 1) % PBUF_SIZE; uint8_t next = (pbuf_head + 1) % PBUF_SIZE;


return val; return val;
} }
static inline bool pbuf_has_data(void)
{
uint8_t sreg = SREG;
cli();
bool has_data = (pbuf_head != pbuf_tail);
SREG = sreg;
return has_data;
}
static inline void pbuf_clear(void)
{
uint8_t sreg = SREG;
cli();
pbuf_head = pbuf_tail = 0;
SREG = sreg;
}