ps2_usart.c (6154B)
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 "gpio.h" 46 #include "ps2.h" 47 #include "ps2_io.h" 48 #include "print.h" 49 50 #ifndef PS2_CLOCK_DDR 51 # define PS2_CLOCK_DDR PORTx_ADDRESS(PS2_CLOCK_PIN) 52 #endif 53 #ifndef PS2_CLOCK_BIT 54 # define PS2_CLOCK_BIT (PS2_CLOCK_PIN & 0xF) 55 #endif 56 #ifndef PS2_DATA_DDR 57 # define PS2_DATA_DDR PORTx_ADDRESS(PS2_DATA_PIN) 58 #endif 59 #ifndef PS2_DATA_BIT 60 # define PS2_DATA_BIT (PS2_DATA_PIN & 0xF) 61 #endif 62 63 #define WAIT(stat, us, err) \ 64 do { \ 65 if (!wait_##stat(us)) { \ 66 ps2_error = err; \ 67 goto ERROR; \ 68 } \ 69 } while (0) 70 71 uint8_t ps2_error = PS2_ERR_NONE; 72 73 static inline uint8_t pbuf_dequeue(void); 74 static inline void pbuf_enqueue(uint8_t data); 75 static inline void pbuf_clear(void); 76 bool pbuf_has_data(void); 77 78 void ps2_host_init(void) { 79 idle(); // without this many USART errors occur when cable is disconnected 80 PS2_USART_INIT(); 81 PS2_USART_RX_INT_ON(); 82 // POR(150-2000ms) plus BAT(300-500ms) may take 2.5sec([3]p.20) 83 //_delay_ms(2500); 84 } 85 86 uint8_t ps2_host_send(uint8_t data) { 87 bool parity = true; 88 ps2_error = PS2_ERR_NONE; 89 90 PS2_USART_OFF(); 91 92 /* terminate a transmission if we have */ 93 inhibit(); 94 _delay_us(100); // [4]p.13 95 96 /* 'Request to Send' and Start bit */ 97 data_lo(); 98 clock_hi(); 99 WAIT(clock_lo, 10000, 10); // 10ms [5]p.50 100 101 /* Data bit[2-9] */ 102 for (uint8_t i = 0; i < 8; i++) { 103 _delay_us(15); 104 if (data & (1 << i)) { 105 parity = !parity; 106 data_hi(); 107 } else { 108 data_lo(); 109 } 110 WAIT(clock_hi, 50, 2); 111 WAIT(clock_lo, 50, 3); 112 } 113 114 /* Parity bit */ 115 _delay_us(15); 116 if (parity) { 117 data_hi(); 118 } else { 119 data_lo(); 120 } 121 WAIT(clock_hi, 50, 4); 122 WAIT(clock_lo, 50, 5); 123 124 /* Stop bit */ 125 _delay_us(15); 126 data_hi(); 127 128 /* Ack */ 129 WAIT(data_lo, 50, 6); 130 WAIT(clock_lo, 50, 7); 131 132 /* wait for idle state */ 133 WAIT(clock_hi, 50, 8); 134 WAIT(data_hi, 50, 9); 135 136 idle(); 137 PS2_USART_INIT(); 138 PS2_USART_RX_INT_ON(); 139 return ps2_host_recv_response(); 140 ERROR: 141 idle(); 142 PS2_USART_INIT(); 143 PS2_USART_RX_INT_ON(); 144 return 0; 145 } 146 147 uint8_t ps2_host_recv_response(void) { 148 // Command may take 25ms/20ms at most([5]p.46, [3]p.21) 149 uint8_t retry = 25; 150 while (retry-- && !pbuf_has_data()) { 151 _delay_ms(1); 152 } 153 return pbuf_dequeue(); 154 } 155 156 uint8_t ps2_host_recv(void) { 157 if (pbuf_has_data()) { 158 ps2_error = PS2_ERR_NONE; 159 return pbuf_dequeue(); 160 } else { 161 ps2_error = PS2_ERR_NODATA; 162 return 0; 163 } 164 } 165 166 ISR(PS2_USART_RX_VECT) { 167 // TODO: request RESEND when error occurs? 168 uint8_t error = PS2_USART_ERROR; // USART error should be read before data 169 uint8_t data = PS2_USART_RX_DATA; 170 if (!error) { 171 pbuf_enqueue(data); 172 } else { 173 xprintf("PS2 USART error: %02X data: %02X\n", error, data); 174 } 175 } 176 177 /* send LED state to keyboard */ 178 void ps2_host_set_led(uint8_t led) { 179 ps2_host_send(0xED); 180 ps2_host_send(led); 181 } 182 183 /*-------------------------------------------------------------------- 184 * Ring buffer to store scan codes from keyboard 185 *------------------------------------------------------------------*/ 186 #define PBUF_SIZE 32 187 static uint8_t pbuf[PBUF_SIZE]; 188 static uint8_t pbuf_head = 0; 189 static uint8_t pbuf_tail = 0; 190 static inline void pbuf_enqueue(uint8_t data) { 191 uint8_t sreg = SREG; 192 cli(); 193 uint8_t next = (pbuf_head + 1) % PBUF_SIZE; 194 if (next != pbuf_tail) { 195 pbuf[pbuf_head] = data; 196 pbuf_head = next; 197 } else { 198 print("pbuf: full\n"); 199 } 200 SREG = sreg; 201 } 202 static inline uint8_t pbuf_dequeue(void) { 203 uint8_t val = 0; 204 205 uint8_t sreg = SREG; 206 cli(); 207 if (pbuf_head != pbuf_tail) { 208 val = pbuf[pbuf_tail]; 209 pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE; 210 } 211 SREG = sreg; 212 213 return val; 214 } 215 bool pbuf_has_data(void) { 216 uint8_t sreg = SREG; 217 cli(); 218 bool has_data = (pbuf_head != pbuf_tail); 219 SREG = sreg; 220 return has_data; 221 } 222 static inline void pbuf_clear(void) { 223 uint8_t sreg = SREG; 224 cli(); 225 pbuf_head = pbuf_tail = 0; 226 SREG = sreg; 227 }