summaryrefslogtreecommitdiff
path: root/protocol
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2014-06-19 16:13:35 +0900
committertmk <nobody@nowhere>2014-07-30 14:07:43 +0900
commit4c8e0fd0bd1712421f957ec5e0ca16fc6bbb3856 (patch)
tree2141d2e168ad043ea18bcb3aee3f3a03dd2a2f1b /protocol
parent80c3ff5fa03429f1e4ea15032f665ceb88c9b8c3 (diff)
Port ps2_usb to mbed
Diffstat (limited to 'protocol')
-rw-r--r--protocol/ps2.h65
-rw-r--r--protocol/ps2_busywait.c16
-rw-r--r--protocol/ps2_io.h15
-rw-r--r--protocol/ps2_io_avr.c74
-rw-r--r--protocol/ps2_io_mbed.c60
5 files changed, 166 insertions, 64 deletions
diff --git a/protocol/ps2.h b/protocol/ps2.h
index 483eea7209..acde679cf4 100644
--- a/protocol/ps2.h
+++ b/protocol/ps2.h
@@ -39,8 +39,9 @@ POSSIBILITY OF SUCH DAMAGE.
#define PS2_H
#include <stdbool.h>
-#include <util/delay.h>
-#include <avr/io.h>
+#include "wait.h"
+#include "ps2_io.h"
+#include "print.h"
/*
* Primitive PS/2 Library for AVR
@@ -92,79 +93,27 @@ uint8_t ps2_host_recv(void);
void ps2_host_set_led(uint8_t usb_led);
-/* Check port settings for clock and data line */
-#if !(defined(PS2_CLOCK_PORT) && \
- defined(PS2_CLOCK_PIN) && \
- defined(PS2_CLOCK_DDR) && \
- defined(PS2_CLOCK_BIT))
-# error "PS/2 clock port setting is required in config.h"
-#endif
-
-#if !(defined(PS2_DATA_PORT) && \
- defined(PS2_DATA_PIN) && \
- defined(PS2_DATA_DDR) && \
- defined(PS2_DATA_BIT))
-# error "PS/2 data port setting is required in config.h"
-#endif
-
/*--------------------------------------------------------------------
* static functions
*------------------------------------------------------------------*/
-static inline void clock_lo(void)
-{
- PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
- PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
-}
-static inline void clock_hi(void)
-{
- /* input with pull up */
- PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
- PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
-}
-static inline bool clock_in(void)
-{
- PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
- PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
- _delay_us(1);
- return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
-}
-static inline void data_lo(void)
-{
- PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
- PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
-}
-static inline void data_hi(void)
-{
- /* input with pull up */
- PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
- PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
-}
-static inline bool data_in(void)
-{
- PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
- PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
- _delay_us(1);
- return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
-}
-
static inline uint16_t wait_clock_lo(uint16_t us)
{
- while (clock_in() && us) { asm(""); _delay_us(1); us--; }
+ while (clock_in() && us) { asm(""); wait_us(1); us--; }
return us;
}
static inline uint16_t wait_clock_hi(uint16_t us)
{
- while (!clock_in() && us) { asm(""); _delay_us(1); us--; }
+ while (!clock_in() && us) { asm(""); wait_us(1); us--; }
return us;
}
static inline uint16_t wait_data_lo(uint16_t us)
{
- while (data_in() && us) { asm(""); _delay_us(1); us--; }
+ while (data_in() && us) { asm(""); wait_us(1); us--; }
return us;
}
static inline uint16_t wait_data_hi(uint16_t us)
{
- while (!data_in() && us) { asm(""); _delay_us(1); us--; }
+ while (!data_in() && us) { asm(""); wait_us(1); us--; }
return us;
}
diff --git a/protocol/ps2_busywait.c b/protocol/ps2_busywait.c
index 05dd7b27e6..a64933219e 100644
--- a/protocol/ps2_busywait.c
+++ b/protocol/ps2_busywait.c
@@ -40,8 +40,9 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdbool.h>
-#include <util/delay.h>
+#include "wait.h"
#include "ps2.h"
+#include "ps2_io.h"
#include "debug.h"
@@ -58,8 +59,11 @@ uint8_t ps2_error = PS2_ERR_NONE;
void ps2_host_init(void)
{
+ clock_init();
+ data_init();
+
// POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
- _delay_ms(2500);
+ wait_ms(2500);
inhibit();
}
@@ -71,7 +75,7 @@ uint8_t ps2_host_send(uint8_t data)
/* terminate a transmission if we have */
inhibit();
- _delay_us(100); // 100us [4]p.13, [5]p.50
+ wait_us(100); // 100us [4]p.13, [5]p.50
/* 'Request to Send' and Start bit */
data_lo();
@@ -80,7 +84,7 @@ uint8_t ps2_host_send(uint8_t data)
/* Data bit */
for (uint8_t i = 0; i < 8; i++) {
- _delay_us(15);
+ wait_us(15);
if (data&(1<<i)) {
parity = !parity;
data_hi();
@@ -92,13 +96,13 @@ uint8_t ps2_host_send(uint8_t data)
}
/* Parity bit */
- _delay_us(15);
+ wait_us(15);
if (parity) { data_hi(); } else { data_lo(); }
WAIT(clock_hi, 50, 4);
WAIT(clock_lo, 50, 5);
/* Stop bit */
- _delay_us(15);
+ wait_us(15);
data_hi();
/* Ack */
diff --git a/protocol/ps2_io.h b/protocol/ps2_io.h
new file mode 100644
index 0000000000..a46a358e7c
--- /dev/null
+++ b/protocol/ps2_io.h
@@ -0,0 +1,15 @@
+#ifndef PS2_IO_H
+#define PS2_IO_H
+
+
+void clock_init(void);
+void clock_lo(void);
+void clock_hi(void);
+bool clock_in(void);
+
+void data_init(void);
+void data_lo(void);
+void data_hi(void);
+bool data_in(void);
+
+#endif
diff --git a/protocol/ps2_io_avr.c b/protocol/ps2_io_avr.c
new file mode 100644
index 0000000000..be13d6696a
--- /dev/null
+++ b/protocol/ps2_io_avr.c
@@ -0,0 +1,74 @@
+#include <stdbool.h>
+#include <util/delay.h>
+
+/* Check port settings for clock and data line */
+#if !(defined(PS2_CLOCK_PORT) && \
+ defined(PS2_CLOCK_PIN) && \
+ defined(PS2_CLOCK_DDR) && \
+ defined(PS2_CLOCK_BIT))
+# error "PS/2 clock port setting is required in config.h"
+#endif
+
+#if !(defined(PS2_DATA_PORT) && \
+ defined(PS2_DATA_PIN) && \
+ defined(PS2_DATA_DDR) && \
+ defined(PS2_DATA_BIT))
+# error "PS/2 data port setting is required in config.h"
+#endif
+
+
+/*
+ * Clock
+ */
+void clock_init(void)
+{
+}
+
+void clock_lo(void)
+{
+ PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
+ PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
+}
+
+void clock_hi(void)
+{
+ /* input with pull up */
+ PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
+ PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
+}
+
+bool clock_in(void)
+{
+ PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
+ PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
+ _delay_us(1);
+ return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
+}
+
+/*
+ * Data
+ */
+void data_init(void)
+{
+}
+
+void data_lo(void)
+{
+ PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
+ PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
+}
+
+void data_hi(void)
+{
+ /* input with pull up */
+ PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
+ PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
+}
+
+bool data_in(void)
+{
+ PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
+ PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
+ _delay_us(1);
+ return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
+}
diff --git a/protocol/ps2_io_mbed.c b/protocol/ps2_io_mbed.c
new file mode 100644
index 0000000000..83bdcef7f5
--- /dev/null
+++ b/protocol/ps2_io_mbed.c
@@ -0,0 +1,60 @@
+#include <stdbool.h>
+#include "ps2_io.h"
+#include "gpio_api.h"
+
+
+static gpio_t clock;
+static gpio_t data;
+
+/*
+ * Clock
+ */
+void clock_init(void)
+{
+ gpio_init(&clock, P0_9);
+ gpio_mode(&clock, OpenDrain|PullNone);
+}
+
+void clock_lo(void)
+{
+ gpio_dir(&clock, PIN_OUTPUT);
+ gpio_write(&clock, 0);
+}
+void clock_hi(void)
+{
+ gpio_dir(&clock, PIN_OUTPUT);
+ gpio_write(&clock, 1);
+}
+
+bool clock_in(void)
+{
+ gpio_dir(&clock, PIN_INPUT);
+ return gpio_read(&clock);
+}
+
+/*
+ * Data
+ */
+void data_init(void)
+{
+ gpio_init(&data, P0_8);
+ gpio_mode(&data, OpenDrain|PullNone);
+}
+
+void data_lo(void)
+{
+ gpio_dir(&data, PIN_OUTPUT);
+ gpio_write(&data, 0);
+}
+
+void data_hi(void)
+{
+ gpio_dir(&data, PIN_OUTPUT);
+ gpio_write(&data, 1);
+}
+
+bool data_in(void)
+{
+ gpio_dir(&data, PIN_INPUT);
+ return gpio_read(&data);
+}