]> jspc29.x-matter.uni-frankfurt.de Git - electronics.git/commitdiff
firmware update with trailing line break
authorJan Michel <j.michel@gsi.de>
Mon, 26 May 2014 15:01:03 +0000 (17:01 +0200)
committerJan Michel <j.michel@gsi.de>
Mon, 26 May 2014 15:01:03 +0000 (17:01 +0200)
Power/firmware/PowerSwitch.c

index 74158b6f0010c080250f0579958aa94441708f79..6a67b2122dff0e525b1945371e89c28412f72f73 100755 (executable)
@@ -1,7 +1,7 @@
 #define F_CPU 6000000UL\r
 \r
 #include <avr/interrupt.h>\r
-// #include <util/delay.h>\r
+#include <util/delay.h>\r
 // #include <avr/sleep.h>\r
 // #include <stdint.h>\r
 #include <avr/io.h>\r
 //B2 Switch2 In \r
 //B3 Reset   --\r
 \r
+//all commands end in \n\r
 // cc          -- channel address in hex \r
 //Scc(0|1|/)   -- switch channel off/on/toggle\r
-//Scc?         -- read channel status\r
+//Scc?         -- read channel status - 2nd char: 'e' in case of overcurrent, 3rd char: enable 1/0\r
 //Ccc?         -- read current\r
 //Lccxxx       -- set current limit (hex)\r
 //Lcc?         -- read current limit\r
@@ -39,8 +40,8 @@ uint8_t  output_error = 0;
 uint16_t adc[2];\r
 \r
 uint8_t rxcnt = 0, txcnt = 0, txpoint = 0;\r
-uint8_t rxbuf[6];\r
-uint8_t txbuf[6];\r
+uint8_t rxbuf[7];\r
+uint8_t txbuf[7];\r
 uint16_t limit[2] = {0x0800,0x0800};\r
 \r
 uint8_t nib_to_hex(uint16_t in, uint8_t nib) {\r
@@ -73,7 +74,8 @@ void send_answer_hex(uint16_t v) {
   txbuf[1]=nib_to_hex(v,2);\r
   txbuf[2]=nib_to_hex(v,1);\r
   txbuf[3]=nib_to_hex(v,0);\r
-  STARTTX(4);\r
+  txbuf[4]='\n';\r
+  STARTTX(5);\r
   }\r
   \r
 void send_answer_chars(uint8_t a, uint8_t b, uint8_t c) {\r
@@ -81,7 +83,8 @@ void send_answer_chars(uint8_t a, uint8_t b, uint8_t c) {
   txbuf[1]=a;\r
   txbuf[2]=b;\r
   txbuf[3]=c;\r
-  STARTTX(4);\r
+  txbuf[4]='\n';\r
+  STARTTX(5);\r
   }\r
 \r
   \r
@@ -137,7 +140,7 @@ ISR(ADC_vect) {
 \r
 ISR(USART1_UDRE_vect) {\r
   UDR1 = txbuf[txpoint++];\r
-  if(--txcnt == 0) {  \r
+  if(--txcnt == 0 || txpoint > 7) {  \r
     txpoint = 0;\r
     UCSR1B &= ~(1<< UDRIE1);\r
     }\r
@@ -148,67 +151,68 @@ ISR(USART1_RX_vect) {
   if (rxcnt != 0 || (buf == 'S' || buf == 'A' || buf == 'L' || buf == 'C')) {\r
     rxbuf[rxcnt++] = buf;\r
     }\r
-  //Forward any incoming 4 letter answer  \r
-  if (rxcnt == 4) {\r
-    if (rxbuf[0] == 'A') {\r
-      rxcnt = 0;\r
-      memcpy ((uint8_t*)txbuf,(uint8_t*)rxbuf,4);\r
-      STARTTX(4);\r
-      }\r
-  //Four letter 'switch' command  \r
-    if (rxbuf[0] == 'S') {  \r
-      rxcnt = 0;\r
-      if (ISMYADDR()) {\r
-        rxbuf[2] -= '0';\r
-        if (rxbuf[3] != '?') {\r
-          switchoutput(rxbuf[2],rxbuf[3]-'0');\r
-          }\r
-        send_answer_chars('0', (output_error  & (1<<rxbuf[2]))?'e':'0', \r
-                               (output_enable & (1<<rxbuf[2]))?'1':'0');      \r
-        }\r
-      else {\r
-        forward_msg(4);\r
+  //Forward any incoming 4 letter answer\r
+  if (buf == '\n' || buf == '\r') {\r
+    // if(rxcnt == 1 && rxbuf[0] == '\n') {\r
+      // txbuf[0] = '\n';\r
+      // STARTTX(1);\r
+      // }\r
+    if(rxcnt == 5) {\r
+      if (rxbuf[0] == 'A') {\r
+        memcpy ((uint8_t*)txbuf,(uint8_t*)rxbuf,4);\r
+        STARTTX(4);\r
         }\r
-      }\r
-  //Read current command\r
-    if (rxbuf[0] == 'C') { \r
-      rxcnt = 0;\r
-      if (ISMYADDR()) {\r
-        if (rxbuf[3] == '?')\r
-          send_answer_hex(adc[rxbuf[2]-'0']);\r
+    //Four letter 'switch' command  \r
+      if (rxbuf[0] == 'S') {  \r
+        if (ISMYADDR()) {\r
+          rxbuf[2] -= '0';\r
+          if (rxbuf[3] != '?') {\r
+            switchoutput(rxbuf[2],rxbuf[3]-'0');\r
+            }\r
+          send_answer_chars('0', (output_error  & (1<<rxbuf[2]))?'e':'0', \r
+                                 (output_enable & (1<<rxbuf[2]))?'1':'0');      \r
+          }\r
+        else {\r
+          forward_msg(5);\r
+          }\r
         }\r
-      else {\r
-        forward_msg(4);\r
+    //Read current command\r
+      if (rxbuf[0] == 'C') { \r
+        if (ISMYADDR()) {\r
+          if (rxbuf[3] == '?')\r
+            send_answer_hex(adc[rxbuf[2]-'0']);\r
+          }\r
+        else {\r
+          forward_msg(5);\r
+          }\r
         }\r
+    //Read current limit    \r
+      if (rxbuf[0] == 'L' && rxbuf[3] == '?') {\r
+        if (ISMYADDR()) {\r
+          send_answer_hex(limit[rxbuf[2]-'0']);      \r
+          }\r
+        else {\r
+          forward_msg(5);\r
+          }\r
+        } \r
       }\r
-  //Read current limit    \r
-    if (rxbuf[0] == 'L' && rxbuf[3] == '?') {\r
-      rxcnt = 0;\r
+  //Set current limit\r
+    if (rxcnt == 7 && rxbuf[0] == 'L') {\r
       if (ISMYADDR()) {\r
-        send_answer_hex(limit[rxbuf[2]-'0']);      \r
+        rxbuf[2] -= '0';\r
+        if(rxbuf[3] != '?') {\r
+          uint16_t lim = hex_to_int(rxbuf[3])*256 + hex_to_int(rxbuf[4])*16 + hex_to_int(rxbuf[5]);\r
+          limit[rxbuf[2]] = lim;\r
+          }\r
+        send_answer_hex(limit[rxbuf[2]]);   \r
         }\r
       else {\r
-        forward_msg(4);\r
+        forward_msg(7);\r
         }\r
       } \r
-    }\r
-  //Set current limit\r
-  if (rxcnt == 6 && rxbuf[0] == 'L') {\r
-    rxcnt = 0;\r
-    if (ISMYADDR()) {\r
-      rxbuf[2] -= '0';\r
-      if(rxbuf[3] != '?') {\r
-        uint16_t lim = hex_to_int(rxbuf[3])*256 + hex_to_int(rxbuf[4])*16 + hex_to_int(rxbuf[5]);\r
-        limit[rxbuf[2]] = lim;\r
-        }\r
-      send_answer_hex(limit[rxbuf[2]]);   \r
-      }\r
-    else {\r
-      forward_msg(6);\r
-      }\r
-    }   \r
+    }      \r
     \r
-  if (rxcnt >= 6) { rxcnt = 0; }  \r
+  if (rxcnt >= 7 || buf == '\n' || buf == '\r') { rxcnt = 0; }  \r
     \r
 } \r
 \r
@@ -238,10 +242,12 @@ __attribute__((naked)) int main(void) {
   DIDR0  = (1<<ADC1D) | (1<< ADC7D); //disable digital inputs\r
   \r
   UCSR1A = (1 << U2X1);\r
-  UCSR1B = (1 << RXCIE1) | (0 << TXCIE1) | (1 << RXEN1) | (1 << TXEN1);\r
+  UCSR1B = (1 << RXCIE1) | (0 << TXCIE1) | (0 << RXEN1) | (1 << TXEN1);\r
   UCSR1C = (3 << UCSZ10); //8 Bit\r
   UBRR1  = 0x26; //19200\r
   \r
+  _delay_ms(100);\r
+  UCSR1B |= (1 << RXEN1);\r
   sei();  \r
   while(1) {\r
     \r