11 static uint8_t cmd_buffer[15];
12 static uint8_t cmd_length = 0;
13 static uint8_t cmd_read_pos = 0;
14 static uint16_t baud_rate = 9600;
15 static uint16_t baud_change = 0;
16 static uint32_t baud_changed_at = 0;
18 static uint8_t dispatch_command(const uint8_t *, uint8_t);
19 uint8_t interface_command(const uint8_t *, uint8_t);
21 void interface_init(void)
24 DDRD = (DDRD&0xFC)|0x02;
26 serial_init(baud_rate);
29 void interface_check(void)
31 if(serial_read_overrun())
32 interface_send1(RECEIVE_OVERRUN);
34 uint8_t count = serial_read_available();
40 uint8_t l = ~serial_read();
44 interface_send1(FRAMING_ERROR);
53 if(cmd_read_pos<cmd_length)
55 if(cmd_read_pos+count>cmd_length)
56 count = cmd_length-cmd_read_pos;
57 for(uint8_t i=0; i<count; ++i)
58 cmd_buffer[cmd_read_pos++] = serial_read();
60 if(cmd_read_pos>=cmd_length)
62 uint8_t result = dispatch_command(cmd_buffer, cmd_length);
63 interface_send1(result);
74 baud_changed_at = clock_get();
75 serial_set_baud(baud_change);
77 else if(clock_get()-baud_changed_at>3*CLOCK_RATE)
80 serial_set_baud(baud_rate);
81 interface_send1(BAUD_CHANGE_FAILED);
86 static uint8_t dispatch_command(const uint8_t *cmd, uint8_t length)
88 uint8_t type = cmd[0]>>4;
91 uint8_t subtype = (cmd[0]>>3)&1;
93 return output_command(cmd, length);
95 return monitor_command(cmd, length);
98 return motorola_command(cmd, length);
100 return mfx_command(cmd, length);
102 return s88_command(cmd, length);
104 return interface_command(cmd, length);
106 return INVALID_COMMAND;
109 void interface_send(const uint8_t *cmd, uint8_t length)
111 serial_write(~length);
112 for(uint8_t i=0; i<length; ++i)
113 serial_write(cmd[i]);
116 void interface_send1(uint8_t cmd)
122 uint8_t interface_command(const uint8_t *cmd, uint8_t length)
124 if(cmd[0]==SET_BAUD_RATE)
129 uint16_t baud = (cmd[1]<<8)|cmd[2];
132 if(baud!=baud_change)
139 baud_rate = baud_change;
145 return INVALID_COMMAND;