]> git.donarmstrong.com Git - tmk_firmware.git/blob - converter/adb_usb/adb_blargg.c
Disable BOOTMAGIC and fix debug prints of adb_usb
[tmk_firmware.git] / converter / adb_usb / adb_blargg.c
1 // Bit-banged implementation without any use of interrupts.
2 // Data pin must have external 1K pull-up resistor.
3 // Operates data pin as open-collector output.
4
5 #include "adb_blargg.h"
6
7 #ifdef HAVE_CONFIG_H
8         #include "config.h"
9 #endif
10
11 #include <avr/io.h>
12 #include <avr/interrupt.h>
13 #include <util/delay.h>
14
15 // Copyright 2011 Jun WAKO <wakojun@gmail.com>
16 // Copyright 2013 Shay Green <gblargg@gmail.com>
17 // See bottom of file for license
18
19 typedef uint8_t byte;
20
21 // Make loop iteration take us total, including cyc overhead of loop logic
22 #define delay_loop_usec( us, cyc ) \
23         __builtin_avr_delay_cycles( (unsigned long) (F_CPU / 1e6 * (us) + 0.5) - (cyc) )
24
25 #if !defined(ADB_PORT) || \
26         !defined(ADB_PIN)  || \
27         !defined(ADB_DDR)  || \
28         !defined(ADB_DATA_BIT)
29         #error
30 #endif
31
32 enum { data_mask = 1<<ADB_DATA_BIT };
33
34 enum { adb_cmd_read  = 0x2C };
35 enum { adb_cmd_write = 0x28 };
36
37 // gcc is very unreliable for inlining, so use macros
38 #define data_lo() (ADB_DDR |= data_mask)
39 #define data_hi() (ADB_DDR &= ~data_mask)
40 #define data_in() (ADB_PIN & data_mask)
41
42 static void place_bit( byte bit )
43 {
44         // 100 us bit cell time
45         data_lo();
46         _delay_us( 35 );
47         
48         // Difference between a 0 and 1 bit is just this 30us portion in the middle
49         if ( bit )
50                 data_hi();
51         _delay_us( 30 );
52         
53         data_hi();
54         _delay_us( 35 );
55 }
56
57 static void place_bit0( void ) { place_bit( 0 ); }
58 static void place_bit1( void ) { place_bit( 1 ); }
59
60 static void send_byte( byte data )
61 {
62         for ( byte n = 8; n; n-- )
63         {
64                 place_bit( data & 0x80 );
65                 data <<= 1;
66         }
67 }
68
69 static void command( byte cmd )
70 {
71         data_lo();
72         _delay_us( 800 );
73         place_bit1();
74         send_byte( cmd );
75         place_bit0();
76 }
77
78 void adb_host_init( void )
79 {
80         // Always keep port output 0, then just toggle DDR to be GND or leave it floating (high).
81         ADB_DDR  &= ~data_mask;
82         ADB_PORT &= ~data_mask;
83         
84         #ifdef ADB_PSW_BIT
85                 // Weak pull-up
86                 ADB_PORT |=  (1<<ADB_PSW_BIT);
87                 ADB_DDR  &= ~(1<<ADB_PSW_BIT);
88         #endif
89 }
90
91 bool adb_host_psw( void )
92 {
93         #ifdef ADB_PSW_BIT
94                 return (ADB_PIN & (1<<ADB_PSW_BIT)) != 0;
95         #else
96                 return true;
97         #endif
98 }
99
100 // Waits while data == val, or until us timeout expires. Returns remaining time,
101 // zero if timed out.
102 static byte while_data( byte us, byte data )
103 {
104         while ( data_in() == data )
105         {
106                 delay_loop_usec( 1 /* us period */, 7 /* cycles loop overhead */ );
107                 if ( !--us )
108                         break;
109         }
110         return us;
111 }
112
113 static byte while_lo( byte us ) { return while_data( us, 0 ); }
114 static byte while_hi( byte us ) { return while_data( us, data_mask ); }
115
116 static uint16_t adb_host_talk( byte cmd )
117 {
118         command( cmd );
119         _delay_us( 5 );
120         if ( !while_hi( 260 - 5 ) ) // avg 160
121                 return adb_host_nothing;
122         
123         // Receive start bit and 16 data bits.
124         // Doing them all in loop allows consistent error checking
125         uint16_t data = 0;
126         byte n = 17;
127         do
128         {
129                 data <<= 1;
130                 enum { timeout = 130 }; // maximum bit cell time
131                 
132                 byte lo = while_lo( timeout );
133                 if ( !lo )
134                         goto error; // timeout
135                 
136                 byte hi = while_hi( lo );
137                 if ( !hi )
138                         goto error; // timeout
139                 
140                 if ( timeout-lo < lo-hi )
141                         data |= 1;
142                 else if ( n == 17 )
143                         goto error; // start bit is wrong
144         }
145         while ( --n );
146         
147         // duration must be split in two due to 255 limit
148         if ( !while_lo( 255 ) && !while_lo( 351 - 255 ) )
149                 goto error;
150         
151         if ( while_hi( 91 ) )
152                 goto error;
153         
154         return data;
155         
156 error:
157         return adb_host_error;
158 }
159
160 uint16_t adb_host_kbd_recv( void )
161 {
162         return adb_host_talk( adb_cmd_read + 0 );
163 }
164
165 uint16_t adb_host_kbd_modifiers( void )
166 {
167         return adb_host_talk( adb_cmd_read + 2 );
168 }
169
170 void adb_host_listen( byte cmd, byte data_h, byte data_l )
171 {
172         command( cmd );
173         _delay_us( 200 );
174         
175         place_bit1();
176         send_byte( data_h ); 
177         send_byte( data_l );
178         place_bit0();
179 }
180
181 void adb_host_kbd_led( byte led )
182 {
183         adb_host_listen( adb_cmd_write + 2, 0, led & 0x07 );
184 }
185
186 /* This software is licensed with a Modified BSD License.
187 All of this is supposed to be Free Software, Open Source, DFSG-free,
188 GPL-compatible, and OK to use in both free and proprietary applications.
189 Additions and corrections to this file are welcome.
190
191 Redistribution and use in source and binary forms, with or without
192 modification, are permitted provided that the following conditions are met:
193
194 * Redistributions of source code must retain the above copyright
195   notice, this list of conditions and the following disclaimer.
196
197 * Redistributions in binary form must reproduce the above copyright
198   notice, this list of conditions and the following disclaimer in
199   the documentation and/or other materials provided with the
200   distribution.
201
202 * Neither the name of the copyright holders nor the names of
203   contributors may be used to endorse or promote products derived
204   from this software without specific prior written permission.
205
206 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
207 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
208 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
210 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
212 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
213 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
214 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
215 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
216 POSSIBILITY OF SUCH DAMAGE. */