Remove unnecessary import of rgblight.h in tmk_core/protocol/*/*.c (#8432)
[jackhill/qmk/firmware.git] / tmk_core / protocol / ps2_usart.c
1 /*
2 Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com>
3
4 This software is licensed with a Modified BSD License.
5 All of this is supposed to be Free Software, Open Source, DFSG-free,
6 GPL-compatible, and OK to use in both free and proprietary applications.
7 Additions and corrections to this file are welcome.
8
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in
18 the documentation and/or other materials provided with the
19 distribution.
20
21 * Neither the name of the copyright holders nor the names of
22 contributors may be used to endorse or promote products derived
23 from this software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * PS/2 protocol USART version
40 */
41
42 #include <stdbool.h>
43 #include <avr/interrupt.h>
44 #include <util/delay.h>
45 #include "ps2.h"
46 #include "ps2_io.h"
47 #include "print.h"
48
49 #define WAIT(stat, us, err) \
50 do { \
51 if (!wait_##stat(us)) { \
52 ps2_error = err; \
53 goto ERROR; \
54 } \
55 } while (0)
56
57 uint8_t ps2_error = PS2_ERR_NONE;
58
59 static inline uint8_t pbuf_dequeue(void);
60 static inline void pbuf_enqueue(uint8_t data);
61 static inline bool pbuf_has_data(void);
62 static inline void pbuf_clear(void);
63
64 void ps2_host_init(void) {
65 idle(); // without this many USART errors occur when cable is disconnected
66 PS2_USART_INIT();
67 PS2_USART_RX_INT_ON();
68 // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
69 //_delay_ms(2500);
70 }
71
72 uint8_t ps2_host_send(uint8_t data) {
73 bool parity = true;
74 ps2_error = PS2_ERR_NONE;
75
76 PS2_USART_OFF();
77
78 /* terminate a transmission if we have */
79 inhibit();
80 _delay_us(100); // [4]p.13
81
82 /* 'Request to Send' and Start bit */
83 data_lo();
84 clock_hi();
85 WAIT(clock_lo, 10000, 10); // 10ms [5]p.50
86
87 /* Data bit[2-9] */
88 for (uint8_t i = 0; i < 8; i++) {
89 _delay_us(15);
90 if (data & (1 << i)) {
91 parity = !parity;
92 data_hi();
93 } else {
94 data_lo();
95 }
96 WAIT(clock_hi, 50, 2);
97 WAIT(clock_lo, 50, 3);
98 }
99
100 /* Parity bit */
101 _delay_us(15);
102 if (parity) {
103 data_hi();
104 } else {
105 data_lo();
106 }
107 WAIT(clock_hi, 50, 4);
108 WAIT(clock_lo, 50, 5);
109
110 /* Stop bit */
111 _delay_us(15);
112 data_hi();
113
114 /* Ack */
115 WAIT(data_lo, 50, 6);
116 WAIT(clock_lo, 50, 7);
117
118 /* wait for idle state */
119 WAIT(clock_hi, 50, 8);
120 WAIT(data_hi, 50, 9);
121
122 idle();
123 PS2_USART_INIT();
124 PS2_USART_RX_INT_ON();
125 return ps2_host_recv_response();
126 ERROR:
127 idle();
128 PS2_USART_INIT();
129 PS2_USART_RX_INT_ON();
130 return 0;
131 }
132
133 uint8_t ps2_host_recv_response(void) {
134 // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
135 uint8_t retry = 25;
136 while (retry-- && !pbuf_has_data()) {
137 _delay_ms(1);
138 }
139 return pbuf_dequeue();
140 }
141
142 uint8_t ps2_host_recv(void) {
143 if (pbuf_has_data()) {
144 ps2_error = PS2_ERR_NONE;
145 return pbuf_dequeue();
146 } else {
147 ps2_error = PS2_ERR_NODATA;
148 return 0;
149 }
150 }
151
152 ISR(PS2_USART_RX_VECT) {
153 // TODO: request RESEND when error occurs?
154 uint8_t error = PS2_USART_ERROR; // USART error should be read before data
155 uint8_t data = PS2_USART_RX_DATA;
156 if (!error) {
157 pbuf_enqueue(data);
158 } else {
159 xprintf("PS2 USART error: %02X data: %02X\n", error, data);
160 }
161 }
162
163 /* send LED state to keyboard */
164 void ps2_host_set_led(uint8_t led) {
165 ps2_host_send(0xED);
166 ps2_host_send(led);
167 }
168
169 /*--------------------------------------------------------------------
170 * Ring buffer to store scan codes from keyboard
171 *------------------------------------------------------------------*/
172 #define PBUF_SIZE 32
173 static uint8_t pbuf[PBUF_SIZE];
174 static uint8_t pbuf_head = 0;
175 static uint8_t pbuf_tail = 0;
176 static inline void pbuf_enqueue(uint8_t data) {
177 uint8_t sreg = SREG;
178 cli();
179 uint8_t next = (pbuf_head + 1) % PBUF_SIZE;
180 if (next != pbuf_tail) {
181 pbuf[pbuf_head] = data;
182 pbuf_head = next;
183 } else {
184 print("pbuf: full\n");
185 }
186 SREG = sreg;
187 }
188 static inline uint8_t pbuf_dequeue(void) {
189 uint8_t val = 0;
190
191 uint8_t sreg = SREG;
192 cli();
193 if (pbuf_head != pbuf_tail) {
194 val = pbuf[pbuf_tail];
195 pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
196 }
197 SREG = sreg;
198
199 return val;
200 }
201 static inline bool pbuf_has_data(void) {
202 uint8_t sreg = SREG;
203 cli();
204 bool has_data = (pbuf_head != pbuf_tail);
205 SREG = sreg;
206 return has_data;
207 }
208 static inline void pbuf_clear(void) {
209 uint8_t sreg = SREG;
210 cli();
211 pbuf_head = pbuf_tail = 0;
212 SREG = sreg;
213 }