]> git.tdb.fi Git - model-railway-devices.git/commitdiff
Rewrite arducontrol interface module to use the new serial API
authorMikko Rasa <tdb@tdb.fi>
Mon, 28 Oct 2013 19:59:48 +0000 (21:59 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 28 Oct 2013 20:08:54 +0000 (22:08 +0200)
arducontrol/Makefile
arducontrol/interface.c

index a7cb562725e34561feddab560414c594ec50ec10..be2ca328bc6e75a8eadbffbab839d1ffa8219aed 100644 (file)
@@ -1,3 +1,4 @@
+FEATURES := SERIAL_ASYNC
 include ../common/build.mk
 
 arducontrol.elf: adc.o interface.o monitor.o motorola.o output.o serial.o s88.o timer.o
index 4064120992bd16e4a38e20446f3d98085ff785ba..675770f771ab09881256ac19ee43ba0b1c165119 100644 (file)
@@ -1,3 +1,4 @@
+#include <avr/io.h>
 #include "interface.h"
 #include "monitor.h"
 #include "motorola.h"
@@ -5,13 +6,6 @@
 #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;
-
-void process_commands(void);
 uint8_t dispatch_command(const uint8_t *, uint8_t);
 
 void interface_init(void)
@@ -23,48 +17,23 @@ void interface_init(void)
 
 void interface_check(void)
 {
-       if(recv_overrun)
-       {
+       if(serial_read_overrun())
                interface_send1(RECEIVE_OVERRUN);
-               recv_overrun = 0;
-       }
-       if(recv_fill>0)
-               process_commands();
-}
 
-void process_commands(void)
-{
-       while(recv_fill>0)
+       while(serial_read_available())
        {
-               uint8_t cmd[15];
-               uint8_t length = ~recv_buf[recv_tail];
+               uint8_t length = ~serial_read();
 
-               if(length<0x10)
-               {
-                       if(recv_fill<=length)
-                               break;
-
-                       uint8_t i, j;
-                       for(i=0, j=recv_tail+1; i<length; ++i, ++j)
-                       {
-                               if(j>=sizeof(recv_buf))
-                                       j = 0;
-                               cmd[i] = recv_buf[j];
-                       }
-               }
-               else
-               {
-                       length = 0;
+               if(length>=0x10)
                        interface_send1(FRAMING_ERROR);
-               }
+               else if(length>0)
+               {
+                       uint8_t cmd[15];
+                       uint8_t i;
 
-               recv_tail += length+1;
-               if(recv_tail>=sizeof(recv_buf))
-                       recv_tail -= sizeof(recv_buf);
-               recv_fill -= length+1;
+                       for(i=0; i<length; ++i)
+                               cmd[i] = serial_read();
 
-               if(length>0)
-               {
                        uint8_t result = dispatch_command(cmd, length);
                        interface_send1(result);
                }
@@ -104,19 +73,3 @@ void interface_send1(uint8_t cmd)
        serial_write(0xFE);
        serial_write(cmd);
 }
-
-static inline void receive(uint8_t c)
-{
-       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_SET_CALLBACK(receive)