aboutsummaryrefslogtreecommitdiff
path: root/protocol
diff options
context:
space:
mode:
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 483eea720..acde679cf 100644
--- a/protocol/ps2.h
+++ b/protocol/ps2.h
@@ -39,8 +39,9 @@ POSSIBILITY OF SUCH DAMAGE.
39#define PS2_H 39#define PS2_H
40 40
41#include <stdbool.h> 41#include <stdbool.h>
42#include <util/delay.h> 42#include "wait.h"
43#include <avr/io.h> 43#include "ps2_io.h"
44#include "print.h"
44 45
45/* 46/*
46 * Primitive PS/2 Library for AVR 47 * Primitive PS/2 Library for AVR
@@ -92,79 +93,27 @@ uint8_t ps2_host_recv(void);
92void ps2_host_set_led(uint8_t usb_led); 93void ps2_host_set_led(uint8_t usb_led);
93 94
94 95
95/* Check port settings for clock and data line */
96#if !(defined(PS2_CLOCK_PORT) && \
97 defined(PS2_CLOCK_PIN) && \
98 defined(PS2_CLOCK_DDR) && \
99 defined(PS2_CLOCK_BIT))
100# error "PS/2 clock port setting is required in config.h"
101#endif
102
103#if !(defined(PS2_DATA_PORT) && \
104 defined(PS2_DATA_PIN) && \
105 defined(PS2_DATA_DDR) && \
106 defined(PS2_DATA_BIT))
107# error "PS/2 data port setting is required in config.h"
108#endif
109
110/*-------------------------------------------------------------------- 96/*--------------------------------------------------------------------
111 * static functions 97 * static functions
112 *------------------------------------------------------------------*/ 98 *------------------------------------------------------------------*/
113static inline void clock_lo(void)
114{
115 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
116 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
117}
118static inline void clock_hi(void)
119{
120 /* input with pull up */
121 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
122 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
123}
124static inline bool clock_in(void)
125{
126 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
127 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
128 _delay_us(1);
129 return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
130}
131static inline void data_lo(void)
132{
133 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
134 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
135}
136static inline void data_hi(void)
137{
138 /* input with pull up */
139 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
140 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
141}
142static inline bool data_in(void)
143{
144 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
145 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
146 _delay_us(1);
147 return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
148}
149
150static inline uint16_t wait_clock_lo(uint16_t us) 99static inline uint16_t wait_clock_lo(uint16_t us)
151{ 100{
152 while (clock_in() && us) { asm(""); _delay_us(1); us--; } 101 while (clock_in() && us) { asm(""); wait_us(1); us--; }
153 return us; 102 return us;
154} 103}
155static inline uint16_t wait_clock_hi(uint16_t us) 104static inline uint16_t wait_clock_hi(uint16_t us)
156{ 105{
157 while (!clock_in() && us) { asm(""); _delay_us(1); us--; } 106 while (!clock_in() && us) { asm(""); wait_us(1); us--; }
158 return us; 107 return us;
159} 108}
160static inline uint16_t wait_data_lo(uint16_t us) 109static inline uint16_t wait_data_lo(uint16_t us)
161{ 110{
162 while (data_in() && us) { asm(""); _delay_us(1); us--; } 111 while (data_in() && us) { asm(""); wait_us(1); us--; }
163 return us; 112 return us;
164} 113}
165static inline uint16_t wait_data_hi(uint16_t us) 114static inline uint16_t wait_data_hi(uint16_t us)
166{ 115{
167 while (!data_in() && us) { asm(""); _delay_us(1); us--; } 116 while (!data_in() && us) { asm(""); wait_us(1); us--; }
168 return us; 117 return us;
169} 118}
170 119
diff --git a/protocol/ps2_busywait.c b/protocol/ps2_busywait.c
index 05dd7b27e..a64933219 100644
--- a/protocol/ps2_busywait.c
+++ b/protocol/ps2_busywait.c
@@ -40,8 +40,9 @@ POSSIBILITY OF SUCH DAMAGE.
40 */ 40 */
41 41
42#include <stdbool.h> 42#include <stdbool.h>
43#include <util/delay.h> 43#include "wait.h"
44#include "ps2.h" 44#include "ps2.h"
45#include "ps2_io.h"
45#include "debug.h" 46#include "debug.h"
46 47
47 48
@@ -58,8 +59,11 @@ uint8_t ps2_error = PS2_ERR_NONE;
58 59
59void ps2_host_init(void) 60void ps2_host_init(void)
60{ 61{
62 clock_init();
63 data_init();
64
61 // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20) 65 // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
62 _delay_ms(2500); 66 wait_ms(2500);
63 67
64 inhibit(); 68 inhibit();
65} 69}
@@ -71,7 +75,7 @@ uint8_t ps2_host_send(uint8_t data)
71 75
72 /* terminate a transmission if we have */ 76 /* terminate a transmission if we have */
73 inhibit(); 77 inhibit();
74 _delay_us(100); // 100us [4]p.13, [5]p.50 78 wait_us(100); // 100us [4]p.13, [5]p.50
75 79
76 /* 'Request to Send' and Start bit */ 80 /* 'Request to Send' and Start bit */
77 data_lo(); 81 data_lo();
@@ -80,7 +84,7 @@ uint8_t ps2_host_send(uint8_t data)
80 84
81 /* Data bit */ 85 /* Data bit */
82 for (uint8_t i = 0; i < 8; i++) { 86 for (uint8_t i = 0; i < 8; i++) {
83 _delay_us(15); 87 wait_us(15);
84 if (data&(1<<i)) { 88 if (data&(1<<i)) {
85 parity = !parity; 89 parity = !parity;
86 data_hi(); 90 data_hi();
@@ -92,13 +96,13 @@ uint8_t ps2_host_send(uint8_t data)
92 } 96 }
93 97
94 /* Parity bit */ 98 /* Parity bit */
95 _delay_us(15); 99 wait_us(15);
96 if (parity) { data_hi(); } else { data_lo(); } 100 if (parity) { data_hi(); } else { data_lo(); }
97 WAIT(clock_hi, 50, 4); 101 WAIT(clock_hi, 50, 4);
98 WAIT(clock_lo, 50, 5); 102 WAIT(clock_lo, 50, 5);
99 103
100 /* Stop bit */ 104 /* Stop bit */
101 _delay_us(15); 105 wait_us(15);
102 data_hi(); 106 data_hi();
103 107
104 /* Ack */ 108 /* Ack */
diff --git a/protocol/ps2_io.h b/protocol/ps2_io.h
new file mode 100644
index 000000000..a46a358e7
--- /dev/null
+++ b/protocol/ps2_io.h
@@ -0,0 +1,15 @@
1#ifndef PS2_IO_H
2#define PS2_IO_H
3
4
5void clock_init(void);
6void clock_lo(void);
7void clock_hi(void);
8bool clock_in(void);
9
10void data_init(void);
11void data_lo(void);
12void data_hi(void);
13bool data_in(void);
14
15#endif
diff --git a/protocol/ps2_io_avr.c b/protocol/ps2_io_avr.c
new file mode 100644
index 000000000..be13d6696
--- /dev/null
+++ b/protocol/ps2_io_avr.c
@@ -0,0 +1,74 @@
1#include <stdbool.h>
2#include <util/delay.h>
3
4/* Check port settings for clock and data line */
5#if !(defined(PS2_CLOCK_PORT) && \
6 defined(PS2_CLOCK_PIN) && \
7 defined(PS2_CLOCK_DDR) && \
8 defined(PS2_CLOCK_BIT))
9# error "PS/2 clock port setting is required in config.h"
10#endif
11
12#if !(defined(PS2_DATA_PORT) && \
13 defined(PS2_DATA_PIN) && \
14 defined(PS2_DATA_DDR) && \
15 defined(PS2_DATA_BIT))
16# error "PS/2 data port setting is required in config.h"
17#endif
18
19
20/*
21 * Clock
22 */
23void clock_init(void)
24{
25}
26
27void clock_lo(void)
28{
29 PS2_CLOCK_PORT &= ~(1<<PS2_CLOCK_BIT);
30 PS2_CLOCK_DDR |= (1<<PS2_CLOCK_BIT);
31}
32
33void clock_hi(void)
34{
35 /* input with pull up */
36 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
37 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
38}
39
40bool clock_in(void)
41{
42 PS2_CLOCK_DDR &= ~(1<<PS2_CLOCK_BIT);
43 PS2_CLOCK_PORT |= (1<<PS2_CLOCK_BIT);
44 _delay_us(1);
45 return PS2_CLOCK_PIN&(1<<PS2_CLOCK_BIT);
46}
47
48/*
49 * Data
50 */
51void data_init(void)
52{
53}
54
55void data_lo(void)
56{
57 PS2_DATA_PORT &= ~(1<<PS2_DATA_BIT);
58 PS2_DATA_DDR |= (1<<PS2_DATA_BIT);
59}
60
61void data_hi(void)
62{
63 /* input with pull up */
64 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
65 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
66}
67
68bool data_in(void)
69{
70 PS2_DATA_DDR &= ~(1<<PS2_DATA_BIT);
71 PS2_DATA_PORT |= (1<<PS2_DATA_BIT);
72 _delay_us(1);
73 return PS2_DATA_PIN&(1<<PS2_DATA_BIT);
74}
diff --git a/protocol/ps2_io_mbed.c b/protocol/ps2_io_mbed.c
new file mode 100644
index 000000000..83bdcef7f
--- /dev/null
+++ b/protocol/ps2_io_mbed.c
@@ -0,0 +1,60 @@
1#include <stdbool.h>
2#include "ps2_io.h"
3#include "gpio_api.h"
4
5
6static gpio_t clock;
7static gpio_t data;
8
9/*
10 * Clock
11 */
12void clock_init(void)
13{
14 gpio_init(&clock, P0_9);
15 gpio_mode(&clock, OpenDrain|PullNone);
16}
17
18void clock_lo(void)
19{
20 gpio_dir(&clock, PIN_OUTPUT);
21 gpio_write(&clock, 0);
22}
23void clock_hi(void)
24{
25 gpio_dir(&clock, PIN_OUTPUT);
26 gpio_write(&clock, 1);
27}
28
29bool clock_in(void)
30{
31 gpio_dir(&clock, PIN_INPUT);
32 return gpio_read(&clock);
33}
34
35/*
36 * Data
37 */
38void data_init(void)
39{
40 gpio_init(&data, P0_8);
41 gpio_mode(&data, OpenDrain|PullNone);
42}
43
44void data_lo(void)
45{
46 gpio_dir(&data, PIN_OUTPUT);
47 gpio_write(&data, 0);
48}
49
50void data_hi(void)
51{
52 gpio_dir(&data, PIN_OUTPUT);
53 gpio_write(&data, 1);
54}
55
56bool data_in(void)
57{
58 gpio_dir(&data, PIN_INPUT);
59 return gpio_read(&data);
60}