From 78d6a233688058936d344c3e846d11dac8132949 Mon Sep 17 00:00:00 2001 From: Jan Michel Date: Mon, 26 May 2014 17:01:03 +0200 Subject: [PATCH] firmware update with trailing line break --- Power/firmware/PowerSwitch.c | 124 ++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 59 deletions(-) diff --git a/Power/firmware/PowerSwitch.c b/Power/firmware/PowerSwitch.c index 74158b6..6a67b21 100755 --- a/Power/firmware/PowerSwitch.c +++ b/Power/firmware/PowerSwitch.c @@ -1,7 +1,7 @@ #define F_CPU 6000000UL #include -// #include +#include // #include // #include #include @@ -22,9 +22,10 @@ //B2 Switch2 In //B3 Reset -- +//all commands end in \n // cc -- channel address in hex //Scc(0|1|/) -- switch channel off/on/toggle -//Scc? -- read channel status +//Scc? -- read channel status - 2nd char: 'e' in case of overcurrent, 3rd char: enable 1/0 //Ccc? -- read current //Lccxxx -- set current limit (hex) //Lcc? -- read current limit @@ -39,8 +40,8 @@ uint8_t output_error = 0; uint16_t adc[2]; uint8_t rxcnt = 0, txcnt = 0, txpoint = 0; -uint8_t rxbuf[6]; -uint8_t txbuf[6]; +uint8_t rxbuf[7]; +uint8_t txbuf[7]; uint16_t limit[2] = {0x0800,0x0800}; uint8_t nib_to_hex(uint16_t in, uint8_t nib) { @@ -73,7 +74,8 @@ void send_answer_hex(uint16_t v) { txbuf[1]=nib_to_hex(v,2); txbuf[2]=nib_to_hex(v,1); txbuf[3]=nib_to_hex(v,0); - STARTTX(4); + txbuf[4]='\n'; + STARTTX(5); } void send_answer_chars(uint8_t a, uint8_t b, uint8_t c) { @@ -81,7 +83,8 @@ void send_answer_chars(uint8_t a, uint8_t b, uint8_t c) { txbuf[1]=a; txbuf[2]=b; txbuf[3]=c; - STARTTX(4); + txbuf[4]='\n'; + STARTTX(5); } @@ -137,7 +140,7 @@ ISR(ADC_vect) { ISR(USART1_UDRE_vect) { UDR1 = txbuf[txpoint++]; - if(--txcnt == 0) { + if(--txcnt == 0 || txpoint > 7) { txpoint = 0; UCSR1B &= ~(1<< UDRIE1); } @@ -148,67 +151,68 @@ ISR(USART1_RX_vect) { if (rxcnt != 0 || (buf == 'S' || buf == 'A' || buf == 'L' || buf == 'C')) { rxbuf[rxcnt++] = buf; } - //Forward any incoming 4 letter answer - if (rxcnt == 4) { - if (rxbuf[0] == 'A') { - rxcnt = 0; - memcpy ((uint8_t*)txbuf,(uint8_t*)rxbuf,4); - STARTTX(4); - } - //Four letter 'switch' command - if (rxbuf[0] == 'S') { - rxcnt = 0; - if (ISMYADDR()) { - rxbuf[2] -= '0'; - if (rxbuf[3] != '?') { - switchoutput(rxbuf[2],rxbuf[3]-'0'); - } - send_answer_chars('0', (output_error & (1<= 6) { rxcnt = 0; } + if (rxcnt >= 7 || buf == '\n' || buf == '\r') { rxcnt = 0; } } @@ -238,10 +242,12 @@ __attribute__((naked)) int main(void) { DIDR0 = (1<