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.
5 #include "adb_blargg.h"
12 #include <avr/interrupt.h>
13 #include <util/delay.h>
15 // Copyright 2011 Jun WAKO <wakojun@gmail.com>
16 // Copyright 2013 Shay Green <gblargg@gmail.com>
17 // See bottom of file for license
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) )
25 #if !defined(ADB_PORT) || \
26 !defined(ADB_PIN) || \
27 !defined(ADB_DDR) || \
28 !defined(ADB_DATA_BIT)
32 enum { data_mask = 1<<ADB_DATA_BIT };
34 enum { adb_cmd_read = 0x2C };
35 enum { adb_cmd_write = 0x28 };
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)
42 static void place_bit( byte bit )
44 // 100 us bit cell time
48 // Difference between a 0 and 1 bit is just this 30us portion in the middle
57 static void place_bit0( void ) { place_bit( 0 ); }
58 static void place_bit1( void ) { place_bit( 1 ); }
60 static void send_byte( byte data )
62 for ( byte n = 8; n; n-- )
64 place_bit( data & 0x80 );
69 static void command( byte cmd )
78 void adb_host_init( void )
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;
86 ADB_PORT |= (1<<ADB_PSW_BIT);
87 ADB_DDR &= ~(1<<ADB_PSW_BIT);
91 bool adb_host_psw( void )
94 return (ADB_PIN & (1<<ADB_PSW_BIT)) != 0;
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 )
104 while ( data_in() == data )
106 delay_loop_usec( 1 /* us period */, 7 /* cycles loop overhead */ );
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 ); }
116 static uint16_t adb_host_talk( byte cmd )
120 if ( !while_hi( 260 - 5 ) ) // avg 160
121 return adb_host_nothing;
123 // Receive start bit and 16 data bits.
124 // Doing them all in loop allows consistent error checking
130 enum { timeout = 130 }; // maximum bit cell time
132 byte lo = while_lo( timeout );
134 goto error; // timeout
136 byte hi = while_hi( lo );
138 goto error; // timeout
140 if ( timeout-lo < lo-hi )
143 goto error; // start bit is wrong
147 // duration must be split in two due to 255 limit
148 if ( !while_lo( 255 ) && !while_lo( 351 - 255 ) )
151 if ( while_hi( 91 ) )
157 return adb_host_error;
160 uint16_t adb_host_kbd_recv( void )
162 return adb_host_talk( adb_cmd_read + 0 );
165 uint16_t adb_host_kbd_modifiers( void )
167 return adb_host_talk( adb_cmd_read + 2 );
170 void adb_host_listen( byte cmd, byte data_h, byte data_l )
181 void adb_host_kbd_led( byte led )
183 adb_host_listen( adb_cmd_write + 2, 0, led & 0x07 );
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.
191 Redistribution and use in source and binary forms, with or without
192 modification, are permitted provided that the following conditions are met:
194 * Redistributions of source code must retain the above copyright
195 notice, this list of conditions and the following disclaimer.
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
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.
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. */