Guest User

Untitled

a guest
Oct 13th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.30 KB | None | 0 0
  1. /*
  2. Copyright 2011 Jun WAKO <[email protected]>
  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. #include <stdbool.h>
  39. #include <util/delay.h>
  40. #include <avr/io.h>
  41. #include <avr/interrupt.h>
  42. #include "adb.h"
  43. #include "debug.h"
  44.  
  45.  
  46. // GCC doesn't inline functions normally
  47. #define data_lo() (ADB_DDR |=  (1<<ADB_DATA_BIT))
  48. #define data_hi() (ADB_DDR &= ~(1<<ADB_DATA_BIT))
  49. #define data_in() (ADB_PIN &   (1<<ADB_DATA_BIT))
  50.  
  51. #ifdef ADB_PSW_BIT
  52. static inline void psw_lo(void);
  53. static inline void psw_hi(void);
  54. static inline bool psw_in(void);
  55. #endif
  56.  
  57. static inline void attention(void);
  58. static inline void place_bit0(void);
  59. static inline void place_bit1(void);
  60. static inline void send_byte(uint8_t data);
  61. static inline uint16_t wait_data_lo(uint16_t us);
  62. static inline uint16_t wait_data_hi(uint16_t us);
  63.  
  64.  
  65. void adb_host_init(void)
  66. {
  67.     ADB_PORT &= ~(1<<ADB_DATA_BIT);
  68.     data_hi();
  69. #ifdef ADB_PSW_BIT
  70.     psw_hi();
  71. #endif
  72.  
  73.     // Enable keyboard left/right modifier distinction
  74.     // Addr:Keyboard(0010), Cmd:Listen(10), Register3(11)
  75.     // upper byte: reserved bits 0000, device address 0010
  76.     // lower byte: device handler 00000011
  77.     adb_host_listen(0x2B,0x02,0x03);
  78. }
  79.  
  80. #ifdef ADB_PSW_BIT
  81. bool adb_host_psw(void)
  82. {
  83.     return psw_in();
  84. }
  85. #endif
  86.  
  87. /*
  88.  * Don't call this in a row without the delay, otherwise it makes some of poor controllers
  89.  * overloaded and misses strokes. Recommended interval is 12ms.
  90.  *
  91.  * Thanks a lot, blargg!
  92.  * <http://geekhack.org/index.php?topic=14290.msg1068919#msg1068919>
  93.  * <http://geekhack.org/index.php?topic=14290.msg1070139#msg1070139>
  94.  */
  95.  
  96. // ADB Bit Cells
  97. //
  98. // bit cell time: 70-130us
  99. // low part of bit0: 60-70% of bit cell
  100. // low part of bit1: 30-40% of bit cell
  101. //
  102. //    bit cell time         70us        130us
  103. //    --------------------------------------------
  104. //    low  part of bit0     42-49       78-91
  105. //    high part of bit0     21-28       39-52
  106. //    low  part of bit1     21-28       39-52
  107. //    high part of bit1     42-49       78-91
  108. //
  109. //
  110. // bit0:
  111. //    70us bit cell:
  112. //      ____________~~~~~~
  113. //      42-49        21-28  
  114. //
  115. //    130us bit cell:
  116. //      ____________~~~~~~
  117. //      78-91        39-52  
  118. //
  119. // bit1:
  120. //    70us bit cell:
  121. //      ______~~~~~~~~~~~~
  122. //      21-28        42-49
  123. //
  124. //    130us bit cell:
  125. //      ______~~~~~~~~~~~~
  126. //      39-52        78-91
  127. //
  128. // [from Apple IIgs Hardware Reference Second Edition]
  129.  
  130. uint16_t adb_host_kbd_recv(void)
  131. {
  132.     uint16_t data = 0;
  133.     attention();
  134.     send_byte(0x2C);            // Addr:Keyboard(0010), Cmd:Talk(11), Register0(00)
  135.     place_bit0();               // Stopbit(0)
  136.     if (!wait_data_lo(500)) {   // Tlt/Stop to Start(140-260us)
  137.         return 0;               // No data to send
  138.     }
  139.    
  140.     // ad hoc fix: without block inerrupt read wrong bit occasionally and get keys stuck
  141.     // TODO: is this needed anymore with improved timing?
  142.     //cli();
  143.     uint8_t n = 17; // start bit + 16 data bits
  144.     do {
  145.         uint8_t lo = (uint8_t) wait_data_hi(130);
  146.         if (!lo)
  147.             goto error;
  148.        
  149.         uint8_t hi = (uint8_t) wait_data_lo(lo);
  150.         if (!hi)
  151.             goto error;
  152.        
  153.         hi = lo - hi;
  154.         lo = 130 - lo;
  155.        
  156.         data <<= 1;
  157.         if (lo < hi) {
  158.             data |= 1;
  159.         }
  160.         else if (n == 17) {
  161.             // Service Request
  162.             dprintf("Startbit ERROR\n");
  163.             sei();
  164.             return -2;
  165.         }
  166.     }
  167.     while ( --n );
  168.  
  169.     // Stop bit can't be checked normally since it could have service request lenghtening
  170.     // and its high state never goes low.
  171.     if (!wait_data_hi(351) || wait_data_lo(91)) {
  172.         dprintf("Stopbit ERROR\n");
  173.         sei();
  174.         return -3;
  175.     }
  176.     sei();
  177.     return data;
  178.  
  179. error:
  180.     dprintf("Bit ERROR\n");
  181.     sei();
  182.     return -4;
  183. }
  184.  
  185. void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l)
  186. {
  187.     attention();
  188.     send_byte(cmd);
  189.     place_bit0();               // Stopbit(0)
  190.     _delay_us(200);             // Tlt/Stop to Start
  191.     place_bit1();               // Startbit(1)
  192.     send_byte(data_h);
  193.     send_byte(data_l);
  194.     place_bit0();               // Stopbit(0);
  195. }
  196.  
  197. // send state of LEDs
  198. void adb_host_kbd_led(uint8_t led)
  199. {
  200.     // Addr:Keyboard(0010), Cmd:Listen(10), Register2(10)
  201.     // send upper byte (not used)
  202.     // send lower byte (bit2: ScrollLock, bit1: CapsLock, bit0:
  203.     adb_host_listen(0x2A,0,led&0x07);
  204. }
  205.  
  206.  
  207. #ifdef ADB_PSW_BIT
  208. static inline void psw_lo()
  209. {
  210.     ADB_DDR  |=  (1<<ADB_PSW_BIT);
  211.     ADB_PORT &= ~(1<<ADB_PSW_BIT);
  212. }
  213. static inline void psw_hi()
  214. {
  215.     ADB_PORT |=  (1<<ADB_PSW_BIT);
  216.     ADB_DDR  &= ~(1<<ADB_PSW_BIT);
  217. }
  218. static inline bool psw_in()
  219. {
  220.     ADB_PORT |=  (1<<ADB_PSW_BIT);
  221.     ADB_DDR  &= ~(1<<ADB_PSW_BIT);
  222.     return ADB_PIN&(1<<ADB_PSW_BIT);
  223. }
  224. #endif
  225.  
  226. static inline void attention(void)
  227. {
  228.     data_lo();
  229.     _delay_us(800-35); // bit1 holds lo for 35 more
  230.     place_bit1();
  231. }
  232.  
  233. static inline void place_bit0(void)
  234. {
  235.     data_lo();
  236.     _delay_us(65);
  237.     data_hi();
  238.     _delay_us(35);
  239. }
  240.  
  241. static inline void place_bit1(void)
  242. {
  243.     data_lo();
  244.     _delay_us(35);
  245.     data_hi();
  246.     _delay_us(65);
  247. }
  248.  
  249. static inline void send_byte(uint8_t data)
  250. {
  251.     for (int i = 0; i < 8; i++) {
  252.         if (data&(0x80>>i))
  253.             place_bit1();
  254.         else
  255.             place_bit0();
  256.     }
  257. }
  258.  
  259. // These are carefully coded to take 6 cycles of overhead.
  260. // inline asm approach became too convoluted
  261. static inline uint16_t wait_data_lo(uint16_t us)
  262. {
  263.     do {
  264.         if ( !data_in() )
  265.             break;
  266.         _delay_us(1 - (6 * 1000000.0 / F_CPU));
  267.     }
  268.     while ( --us );
  269.     return us;
  270. }
  271.  
  272. static inline uint16_t wait_data_hi(uint16_t us)
  273. {
  274.     do {
  275.         if ( data_in() )
  276.             break;
  277.         _delay_us(1 - (6 * 1000000.0 / F_CPU));
  278.     }
  279.     while ( --us );
  280.     return us;
  281. }
  282.  
  283.  
  284. /*
  285. ADB Protocol
  286. ============
  287.  
  288. Resources
  289. ---------
  290. ADB - The Untold Story: Space Aliens Ate My Mouse
  291.     http://developer.apple.com/legacy/mac/library/#technotes/hw/hw_01.html
  292. ADB Manager
  293.     http://developer.apple.com/legacy/mac/library/documentation/mac/pdf/Devices/ADB_Manager.pdf
  294.     Service request(5-17)
  295. Apple IIgs Hardware Reference Second Edition [Chapter6 p121]
  296.     ftp://ftp.apple.asimov.net/pub/apple_II/documentation/Apple%20IIgs%20Hardware%20Reference.pdf
  297. ADB Keycode
  298.     http://72.0.193.250/Documentation/macppc/adbkeycodes/
  299.     http://m0115.web.fc2.com/m0115.jpg
  300.     [Inside Macintosh volume V, pages 191-192]
  301.     http://www.opensource.apple.com/source/IOHIDFamily/IOHIDFamily-421.18.3/IOHIDFamily/Cosmo_USB2ADB.c
  302. ADB Signaling
  303.     http://kbdbabel.sourceforge.net/doc/kbd_signaling_pcxt_ps2_adb.pdf
  304. ADB Overview & History
  305.     http://en.wikipedia.org/wiki/Apple_Desktop_Bus
  306. Microchip Application Note: ADB device(with code for PIC16C)
  307.     http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011062
  308. AVR ATtiny2131 ADB to PS/2 converter(Japanese)
  309.     http://hp.vector.co.jp/authors/VA000177/html/KeyBoardA5DEA5CBA5A2II.html
  310.  
  311.  
  312. Pinouts
  313. -------
  314.     ADB female socket from the front:
  315.     __________
  316.     |        | <--- top
  317.     | 4o  o3 |
  318.     |2o    o1|
  319.     |   ==   |
  320.     |________| <--- bottom
  321.       |    |   <--- 4pins
  322.  
  323.  
  324.     ADB female socket from bottom:
  325.  
  326.     ========== <--- front
  327.     |        |
  328.     |        |
  329.     |2o    o1|
  330.     |4o    o3|
  331.     ---------- <--- back
  332.  
  333.     1: Data
  334.     2: Power SW(low when press Power key)
  335.     3: Vcc(5V)
  336.     4: GND
  337.  
  338.  
  339. Commands
  340. --------
  341.     ADB command is 1byte and consists of 4bit-address, 2bit-command
  342.     type and 2bit-register. The commands are always sent by Host.
  343.  
  344.     Command format:
  345.     7 6 5 4 3 2 1 0
  346.     | | | |------------ address
  347.             | |-------- command type
  348.                 | |---- register
  349.  
  350.     bits                commands
  351.     ------------------------------------------------------
  352.     - - - - 0 0 0 0     Send Request(reset all devices)
  353.     A A A A 0 0 0 1     Flush(reset a device)
  354.     - - - - 0 0 1 0     Reserved
  355.     - - - - 0 0 1 1     Reserved
  356.     - - - - 0 1 - -     Reserved
  357.     A A A A 1 0 R R     Listen(write to a device)
  358.     A A A A 1 1 R R     Talk(read from a device)
  359.  
  360.     The command to read keycodes from keyboard is 0x2C which
  361.     consist of keyboard address 2 and Talk against register 0.
  362.  
  363.     Address:
  364.     2:  keyboard
  365.     3:  mice
  366.  
  367.     Registers:
  368.     0: application(keyobard uses this to store its data.)
  369.     1: application
  370.     2: application(keyboard uses this for LEDs and state of modifiers)
  371.     3: status and command
  372.  
  373.  
  374. Communication
  375. -------------
  376.     This is a minimum information for keyboard communication.
  377.     See "Resources" for detail.
  378.  
  379.     Signaling:
  380.  
  381.     ~~~~____________~~||||||||||||__~~~~~_~~|||||||||||||||__~~~~
  382.  
  383.         |800us     |  |7 Command 0|  |   |  |15-64  Data  0|Stopbit(0)
  384.         +Attention |              |  |   +Startbit(1)
  385.                    +Startbit(1)   |  +Tlt(140-260us)
  386.                                   +stopbit(0)
  387.  
  388.     Bit cells:
  389.  
  390.     bit0: ______~~~
  391.           65    :35us
  392.  
  393.     bit1: ___~~~~~~
  394.           35 :65us
  395.  
  396.     bit0 low time: 60-70% of bit cell(42-91us)
  397.     bit1 low time: 30-40% of bit cell(21-52us)
  398.     bit cell time: 70-130us
  399.     [from Apple IIgs Hardware Reference Second Edition]
  400.  
  401.     Criterion for bit0/1:
  402.     After 55us if line is low/high then bit is 0/1.
  403.  
  404.     Attention & start bit:
  405.     Host asserts low in 560-1040us then places start bit(1).
  406.  
  407.     Tlt(Stop to Start):
  408.     Bus stays high in 140-260us then device places start bit(1).
  409.  
  410.     Global reset:
  411.     Host asserts low in 2.8-5.2ms. All devices are forced to reset.
  412.  
  413.     Service request from device(Srq):
  414.     Device can request to send at commad(Global only?) stop bit.
  415.     Requesting device keeps low for 140-260us at stop bit of command.
  416.  
  417.  
  418. Keyboard Data(Register0)
  419.     This 16bit data can contains two keycodes and two released flags.
  420.     First keycode is palced in upper byte. When one keyocode is sent,
  421.     lower byte is 0xFF.
  422.     Release flag is 1 when key is released.
  423.  
  424.     1514 . . . . . 8 7 6 . . . . . 0
  425.      | | | | | | | | | +-+-+-+-+-+-+-   Keycode2
  426.      | | | | | | | | +---------------   Released2(1 when the key is released)
  427.      | +-+-+-+-+-+-+-----------------   Keycode1
  428.      +-------------------------------   Released1(1 when the key is released)
  429.  
  430.     Keycodes:
  431.     Scancode consists of 7bit keycode and 1bit release flag.
  432.     Device can send two keycodes at once. If just one keycode is sent
  433.     keycode1 contains it and keyocode2 is 0xFF.
  434.  
  435.     Power switch:
  436.     You can read the state from PSW line(active low) however
  437.     the switch has a special scancode 0x7F7F, so you can
  438.     also read from Data line. It uses 0xFFFF for release scancode.
  439.  
  440. Keyboard LEDs & state of keys(Register2)
  441.     This register hold current state of three LEDs and nine keys.
  442.     The state of LEDs can be changed by sending Listen command.
  443.    
  444.     1514 . . . . . . 7 6 5 . 3 2 1 0
  445.      | | | | | | | | | | | | | | | +-   LED1(NumLock)
  446.      | | | | | | | | | | | | | | +---   LED2(CapsLock)
  447.      | | | | | | | | | | | | | +-----   LED3(ScrollLock)
  448.      | | | | | | | | | | +-+-+-------   Reserved
  449.      | | | | | | | | | +-------------   ScrollLock
  450.      | | | | | | | | +---------------   NumLock
  451.      | | | | | | | +-----------------   Apple/Command
  452.      | | | | | | +-------------------   Option
  453.      | | | | | +---------------------   Shift
  454.      | | | | +-----------------------   Control
  455.      | | | +-------------------------   Reset/Power
  456.      | | +---------------------------   CapsLock
  457.      | +-----------------------------   Delete
  458.      +-------------------------------   Reserved
  459.  
  460. END_OF_ADB
  461. */
Advertisement
Add Comment
Please, Sign In to add comment