aboutsummaryrefslogtreecommitdiff
path: root/protocol/ps2_busywait.c
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2013-11-28 14:20:00 +0900
committertmk <nobody@nowhere>2013-11-28 14:20:00 +0900
commit4eb27ee89038e934dcb498df7508199efd9c93f1 (patch)
tree48da6b3a1e09d4ecbcab7c7b32977ae561a3bd6f /protocol/ps2_busywait.c
parent532e100450615ef1e63e2eed548c38d0c4fa688b (diff)
downloadqmk_firmware-4eb27ee89038e934dcb498df7508199efd9c93f1.tar.gz
qmk_firmware-4eb27ee89038e934dcb498df7508199efd9c93f1.zip
Add ps2_interrupt.c
Diffstat (limited to 'protocol/ps2_busywait.c')
-rw-r--r--protocol/ps2_busywait.c51
1 files changed, 27 insertions, 24 deletions
diff --git a/protocol/ps2_busywait.c b/protocol/ps2_busywait.c
index 5ab377877..05dd7b27e 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
35POSSIBILITY OF SUCH DAMAGE. 35POSSIBILITY OF SUCH DAMAGE.
36*/ 36*/
37 37
38/*
39 * PS/2 protocol busywait version
40 */
41
38#include <stdbool.h> 42#include <stdbool.h>
39#include <util/delay.h> 43#include <util/delay.h>
40#include "ps2.h" 44#include "ps2.h"
41#include "debug.h" 45#include "debug.h"
42 46
43 47
44/*
45 * PS/2 protocol busywait version
46 */
47
48#define WAIT(stat, us, err) do { \ 48#define WAIT(stat, us, err) do { \
49 if (!wait_##stat(us)) { \ 49 if (!wait_##stat(us)) { \
50 ps2_error = err; \ 50 ps2_error = err; \
@@ -52,6 +52,7 @@ POSSIBILITY OF SUCH DAMAGE.
52 } \ 52 } \
53} while (0) 53} while (0)
54 54
55
55uint8_t ps2_error = PS2_ERR_NONE; 56uint8_t ps2_error = PS2_ERR_NONE;
56 57
57 58
@@ -65,18 +66,19 @@ void ps2_host_init(void)
65 66
66uint8_t ps2_host_send(uint8_t data) 67uint8_t ps2_host_send(uint8_t data)
67{ 68{
68 uint8_t res = 0;
69 bool parity = true; 69 bool parity = true;
70 ps2_error = PS2_ERR_NONE; 70 ps2_error = PS2_ERR_NONE;
71
71 /* terminate a transmission if we have */ 72 /* terminate a transmission if we have */
72 inhibit(); 73 inhibit();
73 _delay_us(200); // at least 100us 74 _delay_us(100); // 100us [4]p.13, [5]p.50
74 75
75 /* start bit [1] */ 76 /* 'Request to Send' and Start bit */
76 data_lo(); 77 data_lo();
77 clock_hi(); 78 clock_hi();
78 WAIT(clock_lo, 20000, 10); // may take 15ms at most until device starts clocking 79 WAIT(clock_lo, 10000, 10); // 10ms [5]p.50
79 /* data [2-9] */ 80
81 /* Data bit */
80 for (uint8_t i = 0; i < 8; i++) { 82 for (uint8_t i = 0; i < 8; i++) {
81 _delay_us(15); 83 _delay_us(15);
82 if (data&(1<<i)) { 84 if (data&(1<<i)) {
@@ -88,15 +90,18 @@ uint8_t ps2_host_send(uint8_t data)
88 WAIT(clock_hi, 50, 2); 90 WAIT(clock_hi, 50, 2);
89 WAIT(clock_lo, 50, 3); 91 WAIT(clock_lo, 50, 3);
90 } 92 }
91 /* parity [10] */ 93
94 /* Parity bit */
92 _delay_us(15); 95 _delay_us(15);
93 if (parity) { data_hi(); } else { data_lo(); } 96 if (parity) { data_hi(); } else { data_lo(); }
94 WAIT(clock_hi, 50, 4); 97 WAIT(clock_hi, 50, 4);
95 WAIT(clock_lo, 50, 5); 98 WAIT(clock_lo, 50, 5);
96 /* stop bit [11] */ 99
100 /* Stop bit */
97 _delay_us(15); 101 _delay_us(15);
98 data_hi(); 102 data_hi();
99 /* ack [12] */ 103
104 /* Ack */
100 WAIT(data_lo, 50, 6); 105 WAIT(data_lo, 50, 6);
101 WAIT(clock_lo, 50, 7); 106 WAIT(clock_lo, 50, 7);
102 107
@@ -105,17 +110,16 @@ uint8_t ps2_host_send(uint8_t data)
105 WAIT(data_hi, 50, 9); 110 WAIT(data_hi, 50, 9);
106 111
107 inhibit(); 112 inhibit();
108 res = ps2_host_recv_response(); 113 return ps2_host_recv_response();
109ERROR: 114ERROR:
110 inhibit(); 115 inhibit();
111 return res; 116 return 0;
112} 117}
113 118
114/* receive data when host want else inhibit communication */ 119/* receive data when host want else inhibit communication */
115uint8_t ps2_host_recv_response(void) 120uint8_t ps2_host_recv_response(void)
116{ 121{
117 // Command might take 20ms to response([3]p.21) 122 // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
118 // TrackPoint might take 25ms ([5]2.7)
119 // 250 * 100us(wait for start bit in ps2_host_recv) 123 // 250 * 100us(wait for start bit in ps2_host_recv)
120 uint8_t data = 0; 124 uint8_t data = 0;
121 uint8_t try = 250; 125 uint8_t try = 250;
@@ -125,14 +129,6 @@ uint8_t ps2_host_recv_response(void)
125 return data; 129 return data;
126} 130}
127 131
128/* send LED state to keyboard */
129void ps2_host_set_led(uint8_t led)
130{
131 ps2_host_send(0xED);
132 ps2_host_send(led);
133}
134
135
136/* called after start bit comes */ 132/* called after start bit comes */
137uint8_t ps2_host_recv(void) 133uint8_t ps2_host_recv(void)
138{ 134{
@@ -180,3 +176,10 @@ ERROR:
180 inhibit(); 176 inhibit();
181 return 0; 177 return 0;
182} 178}
179
180/* send LED state to keyboard */
181void ps2_host_set_led(uint8_t led)
182{
183 ps2_host_send(0xED);
184 ps2_host_send(led);
185}