Преглед изворни кода

Fix interrupt version of ps2.c

tags/v1.9
tmk пре 10 година
родитељ
комит
416a5b849e
2 измењених фајлова са 37 додато и 25 уклоњено
  1. 35
    18
      protocol/ps2.c
  2. 2
    7
      protocol/ps2_mouse.c

+ 35
- 18
protocol/ps2.c Прегледај датотеку

/* /*
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"




#ifndef PS2_USE_INT
static uint8_t recv_data(void); static uint8_t recv_data(void);
#endif
static inline void clock_lo(void); static inline void clock_lo(void);
static inline void clock_hi(void); static inline void clock_hi(void);
static inline bool clock_in(void); static inline bool clock_in(void);
WAIT(clock_hi, 50, 8); WAIT(clock_hi, 50, 8);
WAIT(data_hi, 50, 9); WAIT(data_hi, 50, 9);


#ifdef PS2_USE_INT
PS2_INT_ON();
#endif
res = ps2_host_recv_response(); res = ps2_host_recv_response();
ERROR: ERROR:
#ifdef PS2_USE_INT #ifdef PS2_USE_INT
return res; return res;
} }


#ifndef PS2_USE_INT
/* receive data when host want else inhibit communication */ /* receive data when host want else inhibit communication */
uint8_t ps2_host_recv_response(void) uint8_t ps2_host_recv_response(void)
{ {
uint8_t data = 0; uint8_t data = 0;


#ifdef PS2_USE_INT
PS2_INT_OFF();
#endif
/* terminate a transmission if we have */ /* terminate a transmission if we have */
inhibit(); inhibit();
_delay_us(100); _delay_us(100);
inhibit(); inhibit();
return data; return data;
} }
#endif


#ifndef PS2_USE_INT #ifndef PS2_USE_INT
uint8_t ps2_host_recv(void) uint8_t ps2_host_recv(void)
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;
}


/* get data received by interrupt */ /* get data received by interrupt */
uint8_t ps2_host_recv(void) uint8_t ps2_host_recv(void)
return pbuf_dequeue(); return pbuf_dequeue();
} }


#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
uint8_t ps2_host_recv_response(void)
{
while (!pbuf_has_data()) ;
return pbuf_dequeue();
}

ISR(PS2_INT_VECT) ISR(PS2_INT_VECT)
{ {
static enum { static enum {
} }


state++; state++;
DEBUGP(state);
switch (state) { switch (state) {
case START: case START:
if (data_in()) if (data_in())
if (!data_in()) if (!data_in())
goto ERROR; goto ERROR;
pbuf_enqueue(data); pbuf_enqueue(data);
//phex(data);
goto DONE; goto DONE;
break; break;
default: default:
} }
goto RETURN; goto RETURN;
ERROR: ERROR:
DEBUGP(0x0F);
inhibit(); inhibit();
ps2_error = state; ps2_error = state;
DONE: DONE:
#endif #endif




static void ps2_reset(void)
{
ps2_host_send(0xFF);
}

/* send LED state to keyboard */ /* send LED state to keyboard */
void ps2_host_set_led(uint8_t led) void ps2_host_set_led(uint8_t led)
{ {
} }




#ifndef PS2_USE_INT
/* called after start bit comes */ /* called after start bit comes */
static uint8_t recv_data(void) static uint8_t recv_data(void)
{ {
ERROR: ERROR:
return 0; return 0;
} }
#endif


static inline void clock_lo() static inline void clock_lo()
{ {

+ 2
- 7
protocol/ps2_mouse.c Прегледај датотеку

phex(rcv); phex(ps2_error); print("\n"); phex(rcv); phex(ps2_error); print("\n");


// read completion code of BAT // read completion code of BAT
rcv = ps2_host_recv();
rcv = ps2_host_recv_response();
print("ps2_mouse_init: read BAT: "); print("ps2_mouse_init: read BAT: ");
phex(rcv); phex(ps2_error); print("\n"); phex(rcv); phex(ps2_error); print("\n");


// read Device ID // read Device ID
rcv = ps2_host_recv();
rcv = ps2_host_recv_response();
print("ps2_mouse_init: read DevID: "); print("ps2_mouse_init: read DevID: ");
phex(rcv); phex(ps2_error); print("\n"); phex(rcv); phex(ps2_error); print("\n");


// send Enable Data Reporting
rcv = ps2_host_send(0xF4);
print("ps2_mouse_init: send 0xF4: ");
phex(rcv); phex(ps2_error); print("\n");

// send Set Remote mode // send Set Remote mode
rcv = ps2_host_send(0xF0); rcv = ps2_host_send(0xF0);
print("ps2_mouse_init: send 0xF0: "); print("ps2_mouse_init: send 0xF0: ");

Loading…
Откажи
Сачувај