summaryrefslogtreecommitdiffstats
path: root/app/kvm.c
blob: a4c3024ed9fe107681809bc969398085faea47f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "project.h"


static enum
{
  STATE_SYNC = 0,
  STATE_ADDR = 1,
  STATE_ENDPOINT = 2,
  STATE_LEN = 3,
  STATE_DATA = 4,
} state = STATE_SYNC;


static uint8_t buf[16];
static int addr;
static int endpoint;
static unsigned int len;
static unsigned int ptr;

void
kvm_dispatch (void)
{
#if 0
  printf
    ("Addr %d, Write to ep %x, %d bytes %02x %02x %02x %02x ...\r\n",
     addr, endpoint, len, buf[0], buf[1], buf[2], buf[3]);
#endif

  if (len)
    {
      if (endpoint == 0x81)
        {
          usb_wakeup_host ();
          led_set (GPIO8);
        }
      else
        {
          led_set (GPIO9);
        }

      if (!usb_is_suspended)
        usbd_ep_write_packet (usbd_dev, endpoint, buf, len);
    }
}

void
kvm_recv (uint8_t d)
{
//  printf ("S%d V0x%02x\r\n", state, d);

  if (state == STATE_ADDR)
    {
      addr = d;
      usart2_queue (d - 1);
      usart3_queue (d - 3);
      state = STATE_ENDPOINT;
      return;
    }
  else
    {
      usart2_queue (d);
      usart3_queue (d);
    }


  switch (state)
    {
    case STATE_SYNC:
      if (d != 0x5a)
        break;
      state = STATE_ADDR;
      break;
    case STATE_ENDPOINT:
      endpoint = d;
      if ((endpoint < 0x81) || (endpoint > 0x83))
        {
          state = STATE_SYNC;
        }
      else
        {
          state = STATE_LEN;
        }
      break;
    case STATE_LEN:
      len = d;
      ptr = 0;
      if (len > sizeof (buf))
        {
          state = STATE_SYNC;
        }
      else
        {
          state = STATE_DATA;
        }
      break;
    case STATE_DATA:
      buf[ptr++] = d;
      if (ptr >= len)
        {
          if (addr == BUS_ID)
            kvm_dispatch ();
          state = STATE_SYNC;
        }
      break;
    default:
      break;
    }
}