]> git.tdb.fi Git - model-railway-devices.git/commitdiff
Add command to change arducontrol baud rate
authorMikko Rasa <tdb@tdb.fi>
Tue, 12 Nov 2013 14:03:03 +0000 (16:03 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 12 Nov 2013 14:03:03 +0000 (16:03 +0200)
arducontrol/Makefile
arducontrol/arducontrol.c
arducontrol/commands.h
arducontrol/interface.c

index 73dfb0f35227fac0aa9ffc885c33252d347d8bf3..218af27d0aaf414f1734ab05488397fe12be281d 100644 (file)
@@ -1,4 +1,4 @@
-FEATURES := SERIAL_ASYNC
+FEATURES := SERIAL_ASYNC CLOCK_TIMER=1 CLOCK_RATE=100
 include ../common/build.mk
 
-arducontrol.elf: adc.o interface.o mfx.o monitor.o motorola.o output.o serial.o s88.o timer.o
+arducontrol.elf: adc.o clock.o interface.o mfx.o monitor.o motorola.o output.o serial.o s88.o timer.o
index 8ec27eaf7f291e298f8aa76128883c720c6de504..025088f67483c617331651907c804bae9a692cf1 100644 (file)
@@ -41,6 +41,7 @@ its data to the track can start.
 */
 #include <avr/io.h>
 #include <avr/interrupt.h>
+#include "clock.h"
 #include "interface.h"
 #include "monitor.h"
 #include "output.h"
@@ -48,6 +49,7 @@ its data to the track can start.
 
 int main(void)
 {
+       clock_start();
        output_init();
        interface_init();
        monitor_init();
index 3b7597c0a43f5c3f9591edb37bc17924ab02dc61..c58ad7c0e3bf9099f56573db2b9699d83b8383ab 100644 (file)
@@ -23,6 +23,7 @@ enum Command
        MFX_SPEED_FUNCS8 = 0x29,
        MFX_SPEED_FUNCS16 = 0x2A,
        S88_READ = 0x30,
+       SET_BAUD_RATE = 0x70,
        COMMAND_OK = 0x80,
        RECEIVE_OVERRUN = 0x81,
        FRAMING_ERROR = 0x82,
@@ -30,6 +31,7 @@ enum Command
        LENGTH_ERROR = 0x84,
        INVALID_VALUE = 0x85,
        OVERCURRENT = 0xA0,
+       BAUD_CHANGE_FAILED = 0xA1,
        TRACK_CURRENT = 0xC0,
        INPUT_VOLTAGE = 0xC1,
        POWER_STATE = 0xC2,
index 63b1ebd1700b0c11d1e193ed1b0c1c3ef4eb0a91..a73189648669345eda9a8589bc7d56ab9e94f12d 100644 (file)
@@ -1,4 +1,5 @@
 #include <avr/io.h>
+#include "clock.h"
 #include "interface.h"
 #include "mfx.h"
 #include "monitor.h"
 static uint8_t cmd_buffer[15];
 static uint8_t cmd_length = 0;
 static uint8_t cmd_read_pos = 0;
+static uint16_t baud_rate = 9600;
+static uint16_t baud_change = 0;
+static uint32_t baud_changed_at = 0;
 
 static uint8_t dispatch_command(const uint8_t *, uint8_t);
+uint8_t interface_command(const uint8_t *, uint8_t);
 
 void interface_init(void)
 {
        DDRB |= 0x01;
        DDRD = (DDRD&0xFC)|0x02;
 
-       serial_init(9600);
+       serial_init(baud_rate);
 }
 
 void interface_check(void)
@@ -61,6 +66,21 @@ void interface_check(void)
                }
                PORTB &= ~0x01;
        }
+
+       if(baud_change)
+       {
+               if(!baud_changed_at)
+               {
+                       baud_changed_at = clock_get();
+                       serial_set_baud(baud_change);
+               }
+               else if(clock_get()-baud_changed_at>3*CLOCK_RATE)
+               {
+                       baud_change = 0;
+                       serial_set_baud(baud_rate);
+                       interface_send1(BAUD_CHANGE_FAILED);
+               }
+       }
 }
 
 static uint8_t dispatch_command(const uint8_t *cmd, uint8_t length)
@@ -80,6 +100,8 @@ static uint8_t dispatch_command(const uint8_t *cmd, uint8_t length)
                return mfx_command(cmd, length);
        else if(type==3)
                return s88_command(cmd, length);
+       else if(type==7)
+               return interface_command(cmd, length);
        else
                return INVALID_COMMAND;
 }
@@ -96,3 +118,31 @@ void interface_send1(uint8_t cmd)
        serial_write(0xFE);
        serial_write(cmd);
 }
+
+uint8_t interface_command(const uint8_t *cmd, uint8_t length)
+{
+       if(cmd[0]==SET_BAUD_RATE)
+       {
+               if(length!=3)
+                       return LENGTH_ERROR;
+
+               uint16_t baud = (cmd[1]<<8)|cmd[2];
+               if(baud!=baud_rate)
+               {
+                       if(baud!=baud_change)
+                       {
+                               baud_change = baud;
+                               baud_changed_at = 0;
+                       }
+                       else
+                       {
+                               baud_rate = baud_change;
+                               baud_change = 0;
+                       }
+               }
+       }
+       else
+               return INVALID_COMMAND;
+
+       return COMMAND_OK;
+}