You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

adb_blargg.c 5.3KB

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