1
0
Quellcode durchsuchen

implementing CRC on eeprom readout command.

Wilfried Klaas vor 6 Jahren
Ursprung
Commit
d3335311f6
2 geänderte Dateien mit 32 neuen und 13 gelöschten Zeilen
  1. 3 1
      SPS/SPS.ino
  2. 29 12
      SPS/serialprg.ino

+ 3 - 1
SPS/SPS.ino

@@ -72,6 +72,7 @@
 #ifdef __AVR_ATtiny84__
 #define SPS_ENHANCEMENT
 #define SPS_SERVO
+//#define SPS_TONE
 #endif
 
 #include <debug.h>
@@ -281,7 +282,6 @@ void setup() {
     programMode();
   }
 #ifdef SPS_ENHANCEMENT
-  serialPrg();
   if (digitalRead(SW_SEL) == 0) {
     serialPrg();
   }
@@ -1024,12 +1024,14 @@ void doByte(byte data) {
       }
       break;
 #endif
+#ifdef __AVR_ATmega328P__
     case 13:
       digitalWrite(LED_BUILTIN, 0);
       break;
     case 14:
       digitalWrite(LED_BUILTIN, 1);
       break;
+#endif
   }
 #endif
 }

+ 29 - 12
SPS/serialprg.ino

@@ -7,12 +7,10 @@
 #endif
 
 void serialPrg() {
-  //int value1, value2, value3, value4;
   byte value;
   bool endOfPrg = false;
   bool endOfFile = false;
   byte data[32];
-  char tmp[16];
   word readAddress;
   int crc, readcrc;
   byte count;
@@ -21,7 +19,7 @@ void serialPrg() {
   addr = 0;
   Serial.end();
   Serial.begin(BAUDRATE);
-  Serial.println("serPrgStart");
+  Serial.println();
   Serial.println("waiting for command:");
   Serial.println("w: write HEX file, r: read EPPROM, e: end");
   while (!endOfPrg) {
@@ -125,14 +123,14 @@ void serialPrg() {
       if (myChar == 'r') {
         // write eeprom as hexfile to receiver
         Serial.println("EEPROM data:");
-        byte checksum = 0;
+        int checksum = 0;
         for (int addr = 0; addr <= E2END; addr++) {
           value = EEPROM.read(addr);
-          if ((addr % 16) == 0) {
+          if ((addr % 8) == 0) {
             printCheckSum(checksum);
             checksum = 0;
-            Serial.print(":10");
-            checksum += 0x10;
+            Serial.print(":08");
+            checksum += 0x08;
             printHex16(addr);
             checksum += (addr >> 8);
             checksum += (addr & 0x00FF);
@@ -161,20 +159,29 @@ char getNextChar() {
   }
   return  Serial.read();
 }
-void printCheckSum(byte checksum) {
+
+void printCheckSum(int value) {
+  int checksum = value & 0xFF;
+  checksum = (checksum ^ 0xFF) + 1;
   printHex8(checksum);
   Serial.println();
 }
 
 void printHex8(int num) {
-  char tmp[16];
-  sprintf(tmp, "%.2X", num);
+  char tmp[3];
+  tmp[0] = nibbleToHex(num >> 4);
+  tmp[1] = nibbleToHex(num);
+  tmp[2] = 0x00;
   Serial.print(tmp);
 }
 
 void printHex16(int num) {
-  char tmp[16];
-  sprintf(tmp, "%.4X", num);
+  char tmp[5];
+  tmp[0] = nibbleToHex(num >> 12);
+  tmp[1] = nibbleToHex(num >> 8);
+  tmp[2] = nibbleToHex(num >> 4);
+  tmp[3] = nibbleToHex(num);
+  tmp[4] = 0x00;
   Serial.print(tmp);
 }
 
@@ -186,3 +193,13 @@ byte hexToByte (char c) {
     return (c - 'A') + 10;
   }
 }
+
+byte nibbleToHex (byte value) {
+  byte c = value & 0x0F;
+  if ( (c >= 0) && (c <= 9) ) {
+    return c + '0';
+  }
+  if ( (c >= 10) && (c <= 15) ) {
+    return (c + 'A') - 10;
+  }
+}