Subversion Repositories pentevo

Rev

Blame | Last modification | View Log | Download | RSS feed | ?url?

  1. #include "_global.h"
  2. #include "_ps2k.h"
  3. #include <avr/interrupt.h>
  4. #include <util/delay_basic.h>
  5.  
  6. //-----------------------------------------------------------------------------
  7.  
  8. volatile u8 ps2k_bit_count=0, ps2k_data, ps2k_raw_ready, ps2k_raw_code;
  9. volatile u8 ps2k_skip=0, ps2k_flags=0;
  10. volatile u16 ps2k_key_flags_and_code=0;
  11.  
  12. #define ps2k_dataline_up()    { DDRD&=~(1<<PD6); PORTD|=(1<<PD6); }
  13. #define ps2k_dataline_down()  { PORTD&=~(1<<PD6); DDRD|=(1<<PD6); }
  14. #define ps2k_clockline_up()   { DDRE&=~(1<<PE4); PORTE|=(1<<PE4); }
  15. #define ps2k_clockline_down() { PORTE&=~(1<<PE4); DDRE|=(1<<PE4); }
  16.  
  17. //-----------------------------------------------------------------------------
  18.  
  19. void ps2k_init(void)
  20. {
  21.  EICRB|=(1<<ISC41)|(0<<ISC40);
  22.  EIMSK|=(1<<INT4);
  23.  TCCR0=(0<<CS02)|(1<<CS01)|(0<<CS00); // clk/8
  24. }
  25.  
  26. //-----------------------------------------------------------------------------
  27.  
  28. void ps2k_setsysled(void)
  29. {
  30.  if (ps2k_send_byte(0xed))
  31.  {
  32.   u8 answ;
  33.   if (ps2k_receive_byte(&answ))
  34.   {
  35.    if (answ==0xfa)
  36.    {
  37.     u8 leds;
  38.     if (mode1&0x80) leds=0x00; else leds=0x01;
  39.     ps2k_send_byte(leds);
  40.    }
  41.   }
  42.  }
  43. }
  44.  
  45. //-----------------------------------------------------------------------------
  46. // out: hi8 - key code, lo8 - flags
  47. u16 waitkey(void)
  48. {
  49.  u16 tmp;
  50.  do
  51.  {
  52.   do
  53.   {
  54.    random8();
  55.    tmp=ps2k_key_flags_and_code;
  56.   }while (!((u8)tmp&(1<<PS2K_BIT_READY)));
  57.   *(u8*)&ps2k_key_flags_and_code=0;
  58.  }while ((u8)tmp&(1<<PS2K_BIT_RELEASE));
  59.  return tmp;
  60. }
  61.  
  62. //-----------------------------------------------------------------------------
  63.  
  64. u8 inkey(u16 *key)
  65. {
  66.  random8();
  67.  u16 tmp;
  68.  tmp=ps2k_key_flags_and_code;
  69.  if (!((u8)tmp&(1<<PS2K_BIT_READY))) return 0;
  70.  *(u8*)&ps2k_key_flags_and_code=0;
  71.  if ((u8)tmp&(1<<PS2K_BIT_RELEASE)) return 0;
  72.  *key=tmp;
  73.  return 1;
  74. }
  75.  
  76. //-----------------------------------------------------------------------------
  77.  
  78. u8 ps2k_send_byte(u8 data)
  79. {
  80.  u8 tmp, pin;
  81.  u16 ps2k_timeout;
  82.  do
  83.  {
  84.   ps2k_dataline_up();
  85.   TIMSK&=~(1<<TOIE0);
  86.   ps2k_clockline_up();
  87.   tmp=0;
  88.   do
  89.    pin=PINE&(1<<PE4);
  90.   while ( (pin) && (--tmp) );
  91.  }while (pin==0);
  92.  
  93.  cli();
  94.  ps2k_clockline_down();
  95.  ps2k_dataline_down();
  96.  ps2k_data=data;
  97.  ps2k_flags=1<<PS2K_BIT_TX;
  98.  ps2k_bit_count=0;
  99.  ps2k_skip=0;
  100.  *(u8*)&ps2k_key_flags_and_code=0;
  101.  ps2k_raw_ready=0;
  102.  sei();
  103. // _delay_loop_2(276);  // 100 us
  104. // ps2k_dataline_down();
  105.  _delay_loop_2(1382); // 500 us
  106.  set_timeout_ms(&ps2k_timeout,15);
  107.  ps2k_clockline_up();
  108.  do
  109.  {
  110.   if (ps2k_flags&(1<<PS2K_BIT_ACKBIT)) return 1;
  111.  }while (!(check_timeout_ms(&ps2k_timeout)));
  112.  ps2k_dataline_up();
  113.  return 0;
  114. }
  115.  
  116. //-----------------------------------------------------------------------------
  117.  
  118. u8 ps2k_receive_byte(u8 *data)
  119. {
  120.  u16 ps2k_timeout;
  121.  ps2k_raw_ready=0;
  122.  set_timeout_ms(&ps2k_timeout,20);
  123.  do
  124.  {
  125.   if (ps2k_raw_ready)
  126.   {
  127.    *data=ps2k_raw_code;
  128.    return 1;
  129.   }
  130.  }while (!(check_timeout_ms(&ps2k_timeout)));
  131.  // *data=0;
  132.  return 0;
  133. }
  134.  
  135. //-----------------------------------------------------------------------------
  136.  
  137. u8 ps2k_detect_send(u8 data)
  138. {
  139.  print_hexbyte_for_dump(data);
  140.  if (!(ps2k_send_byte(data)))
  141.  {
  142.   print_mlmsg(mlmsg_txfail);
  143.   return 0;
  144.  }
  145.  u8 rxdata;
  146.  if (!(ps2k_receive_byte(&rxdata)))
  147.  {
  148.   print_mlmsg(mlmsg_noresponse);
  149.   return 0;
  150.  }
  151.  print_hexbyte_for_dump(rxdata);
  152.  if (rxdata!=0xfa)
  153.  {
  154.   print_mlmsg(mlmsg_unwanted);
  155.   return 0;
  156.  }
  157.  return 1;
  158. }
  159.  
  160. //-----------------------------------------------------------------------------
  161.  
  162. u8 ps2k_detect_receive(u8 data)
  163. {
  164.  u8 rxdata;
  165.  if (!(ps2k_receive_byte(&rxdata)))
  166.  {
  167.   print_mlmsg(mlmsg_noresponse);
  168.   return 0;
  169.  }
  170.  print_hexbyte_for_dump(rxdata);
  171.  if (rxdata!=data)
  172.  {
  173.   print_mlmsg(mlmsg_unwanted);
  174.   return 0;
  175.  }
  176.  return 1;
  177. }
  178.  
  179. //-----------------------------------------------------------------------------
  180.  
  181. void ps2k_detect_kbd(void)
  182. {
  183.  ps2k_clockline_up();
  184.  print_mlmsg(mlmsg_kbd_detect);
  185.  
  186.  u16 ps2k_timeout;
  187.  ps2k_raw_ready=0;
  188.  set_timeout_ms(&ps2k_timeout,500);
  189.  do
  190.  {
  191.   if (ps2k_raw_ready)
  192.   {
  193.    print_hexbyte_for_dump(ps2k_raw_code);
  194.    break;
  195.   }
  196.  }while (!(check_timeout_ms(&ps2k_timeout)));
  197.  
  198.  ps2k_detect_send(0xff);
  199.  ps2k_raw_ready=0;
  200.  set_timeout_ms(&ps2k_timeout,1000);
  201.  do
  202.  {
  203.   if (ps2k_raw_ready)
  204.   {
  205.    u8 tmp;
  206.    tmp=ps2k_raw_code;
  207.    print_hexbyte_for_dump(tmp);
  208.    if (tmp!=0xaa) print_mlmsg(mlmsg_unwanted);
  209.    break;
  210.   }
  211.  }while (!(check_timeout_ms(&ps2k_timeout)));
  212.  if (!(ps2k_raw_ready)) print_mlmsg(mlmsg_noresponse);
  213.  
  214.  if (ps2k_detect_send(0xf2))
  215.  {
  216.   if (ps2k_detect_receive(0xab))
  217.    ps2k_detect_receive(0x83);
  218.  }
  219.  if (ps2k_detect_send(0xf0))
  220.   ps2k_detect_send(0x02);
  221.  if (ps2k_detect_send(0xf3))
  222.  {
  223.   ps2k_detect_send(0x00);
  224.   put_char('\r');
  225.   put_char('\n');
  226.  }
  227. }
  228.  
  229. //-----------------------------------------------------------------------------
  230.