2 Copyright 2010,2011,2012,2013 Jun WAKO <wakojun@gmail.com>
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.
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
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
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.
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.
39 * PS/2 protocol USART version
43 #include <avr/interrupt.h>
44 #include <util/delay.h>
49 #define WAIT(stat, us, err) \
51 if (!wait_##stat(us)) { \
57 uint8_t ps2_error
= PS2_ERR_NONE
;
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);
64 void ps2_host_init(void) {
65 idle(); // without this many USART errors occur when cable is disconnected
67 PS2_USART_RX_INT_ON();
68 // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20)
72 uint8_t ps2_host_send(uint8_t data
) {
74 ps2_error
= PS2_ERR_NONE
;
78 /* terminate a transmission if we have */
80 _delay_us(100); // [4]p.13
82 /* 'Request to Send' and Start bit */
85 WAIT(clock_lo
, 10000, 10); // 10ms [5]p.50
88 for (uint8_t i
= 0; i
< 8; i
++) {
90 if (data
& (1 << i
)) {
96 WAIT(clock_hi
, 50, 2);
97 WAIT(clock_lo
, 50, 3);
107 WAIT(clock_hi
, 50, 4);
108 WAIT(clock_lo
, 50, 5);
115 WAIT(data_lo
, 50, 6);
116 WAIT(clock_lo
, 50, 7);
118 /* wait for idle state */
119 WAIT(clock_hi
, 50, 8);
120 WAIT(data_hi
, 50, 9);
124 PS2_USART_RX_INT_ON();
125 return ps2_host_recv_response();
129 PS2_USART_RX_INT_ON();
133 uint8_t ps2_host_recv_response(void) {
134 // Command may take 25ms/20ms at most([5]p.46, [3]p.21)
136 while (retry
-- && !pbuf_has_data()) {
139 return pbuf_dequeue();
142 uint8_t ps2_host_recv(void) {
143 if (pbuf_has_data()) {
144 ps2_error
= PS2_ERR_NONE
;
145 return pbuf_dequeue();
147 ps2_error
= PS2_ERR_NODATA
;
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
;
159 xprintf("PS2 USART error: %02X data: %02X\n", error
, data
);
163 /* send LED state to keyboard */
164 void ps2_host_set_led(uint8_t led
) {
169 /*--------------------------------------------------------------------
170 * Ring buffer to store scan codes from keyboard
171 *------------------------------------------------------------------*/
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
) {
179 uint8_t next
= (pbuf_head
+ 1) % PBUF_SIZE
;
180 if (next
!= pbuf_tail
) {
181 pbuf
[pbuf_head
] = data
;
184 print("pbuf: full\n");
188 static inline uint8_t pbuf_dequeue(void) {
193 if (pbuf_head
!= pbuf_tail
) {
194 val
= pbuf
[pbuf_tail
];
195 pbuf_tail
= (pbuf_tail
+ 1) % PBUF_SIZE
;
201 static inline bool pbuf_has_data(void) {
204 bool has_data
= (pbuf_head
!= pbuf_tail
);
208 static inline void pbuf_clear(void) {
211 pbuf_head
= pbuf_tail
= 0;