X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=arducontrol%2Finterface.c;h=63b1ebd1700b0c11d1e193ed1b0c1c3ef4eb0a91;hb=55f60a37bbeeaa62796e1be8bef062eeb1522a71;hp=041dd67bafc05f07fb9ef096b9f9b8a0668f9c8b;hpb=5ee5a22903f8059bf38bc8b2041eb18dfc58bcf0;p=model-railway-devices.git diff --git a/arducontrol/interface.c b/arducontrol/interface.c index 041dd67..63b1ebd 100644 --- a/arducontrol/interface.c +++ b/arducontrol/interface.c @@ -1,22 +1,21 @@ +#include #include "interface.h" +#include "mfx.h" #include "monitor.h" #include "motorola.h" #include "output.h" #include "serial.h" +#include "s88.h" -volatile uint8_t recv_buf[32]; -uint8_t recv_head = 0; -uint8_t recv_tail = 0; -volatile uint8_t recv_fill = 0; -volatile uint8_t recv_overrun = 0; -uint8_t cmd_buf[15]; -uint8_t cmd_length; +static uint8_t cmd_buffer[15]; +static uint8_t cmd_length = 0; +static uint8_t cmd_read_pos = 0; -void process_commands(void); -uint8_t process_command(void); +static uint8_t dispatch_command(const uint8_t *, uint8_t); void interface_init(void) { + DDRB |= 0x01; DDRD = (DDRD&0xFC)|0x02; serial_init(9600); @@ -24,91 +23,76 @@ void interface_init(void) void interface_check(void) { - if(recv_overrun) - { - serial_write(0xFE); - serial_write(RECEIVE_OVERRUN); - recv_overrun = 0; - } - if(recv_fill>0) - process_commands(); -} + if(serial_read_overrun()) + interface_send1(RECEIVE_OVERRUN); -void process_commands(void) -{ - while(recv_fill>0) + uint8_t count = serial_read_available(); + if(count>0) { - uint8_t consumed; - uint8_t c = recv_buf[recv_tail]; - - cmd_length = 0; - - if(c>=0xF0) + PORTB |= 0x01; + if(cmd_length==0) { - cmd_length = ~c; - if(recv_fill<=cmd_length) - break; - - uint8_t i, j; - for(i=0, j=recv_tail+1; i=0x10) + interface_send1(FRAMING_ERROR); + else { - if(j>=sizeof(recv_buf)) - j = 0; - cmd_buf[i] = recv_buf[j]; + cmd_length = l; + --count; + cmd_read_pos = 0; } - - consumed = 1+cmd_length; - } - else - { - serial_write(0xFE); - serial_write(FRAMING_ERROR); - consumed = 1; } - recv_tail += consumed; - if(recv_tail>=sizeof(recv_buf)) - recv_tail -= sizeof(recv_buf); - recv_fill -= consumed; - - if(cmd_length>0) + if(cmd_read_poscmd_length) + count = cmd_length-cmd_read_pos; + for(uint8_t i=0; i=cmd_length) + { + uint8_t result = dispatch_command(cmd_buffer, cmd_length); + interface_send1(result); + cmd_length = 0; + } } + PORTB &= ~0x01; } } -uint8_t process_command(void) +static uint8_t dispatch_command(const uint8_t *cmd, uint8_t length) { - uint8_t type = cmd_buf[0]>>4; + uint8_t type = cmd[0]>>4; if(type==0) { - uint8_t subtype = (cmd_buf[0]>>3)&1; + uint8_t subtype = (cmd[0]>>3)&1; if(subtype==0) - return output_command(); + return output_command(cmd, length); else - return monitor_command(); + return monitor_command(cmd, length); } else if(type==1) - return motorola_command(); + return motorola_command(cmd, length); + else if(type==2) + return mfx_command(cmd, length); + else if(type==3) + return s88_command(cmd, length); else return INVALID_COMMAND; } -static inline void receive(uint8_t c) +void interface_send(const uint8_t *cmd, uint8_t length) { - if(recv_fill>=sizeof(recv_buf)) - { - recv_overrun = 1; - return; - } - - recv_buf[recv_head++] = c; - if(recv_head>=sizeof(recv_buf)) - recv_head = 0; - ++recv_fill; + serial_write(~length); + for(uint8_t i=0; i