Просмотр исходного кода

adding new programming feature, intel hex file.

Klaas, Wilfried 6 лет назад
Родитель
Сommit
c6905c879d
2 измененных файлов с 152 добавлено и 28 удалено
  1. 8 2
      SPS/SPS.ino
  2. 144 26
      SPS/serialprg.ino

+ 8 - 2
SPS/SPS.ino

@@ -62,7 +62,7 @@
 
 #ifdef __AVR_ATmega328P__
 #define debug
-#define SPS_USE_DISPLAY
+//#define SPS_USE_DISPLAY
 #define SPS_RECEIVER
 #define SPS_ENHANCEMENT
 #define SPS_SERVO
@@ -275,13 +275,13 @@ void setup() {
   initDebug();
 #endif
 
-  //  writeProgram();
   doReset();
 
   if (digitalRead(SW_PRG) == 0) {
     programMode();
   }
 #ifdef SPS_ENHANCEMENT
+  serialPrg();
   if (digitalRead(SW_SEL) == 0) {
     serialPrg();
   }
@@ -1024,6 +1024,12 @@ void doByte(byte data) {
       }
       break;
 #endif
+    case 13:
+      digitalWrite(LED_BUILTIN, 0);
+      break;
+    case 14:
+      digitalWrite(LED_BUILTIN, 1);
+      break;
   }
 #endif
 }

+ 144 - 26
SPS/serialprg.ino

@@ -3,54 +3,150 @@
 #endif
 
 #ifdef __AVR_ATmega328P__
-#define BAUDRATE 57600
+#define BAUDRATE 9600
 #endif
 
 void serialPrg() {
-  int value1, value2, value3, value4;
+  //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;
+  byte type;
 
   addr = 0;
   Serial.end();
   Serial.begin(BAUDRATE);
   Serial.println("serPrgStart");
+  Serial.println("waiting for command:");
+  Serial.println("w: write HEX file, r: read EPPROM, e: end");
   while (!endOfPrg) {
     while (Serial.available() > 0) {
-
       // look for the next valid integer in the incoming serial stream:
       char myChar = Serial.read();
-      if (myChar == 'd') {
-        for (byte i = 0; i < 8; i++) {
-          char c;
-          while (!Serial.available()) {
+      if (myChar == 'w') {
+        // hexfile is comming to programm
+        Serial.println("waitin");
+        addr = 0;
+        do {
+          for (byte i = 0; i < 8; i++) {
+            data[i] = 0xFF;
           }
           do {
-            c = Serial.read();
-          } while (!(((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F'))));
-          value = hexToByte(c);
+            c = getNextChar();
+          } while (!(c == ':'));
+
+          Serial.print(".");
+
+          // read counter
+          c = getNextChar();
+          count = hexToByte(c) << 4;
+          c = getNextChar();
+          count += hexToByte(c);
+          printHex8(count);
+
+          crc = count;
+          Serial.print(".");
+
+          // address
+          c = getNextChar();
+          readAddress = hexToByte(c) << 12;
+          c = getNextChar();
+          readAddress += hexToByte(c) << 8;
+          c = getNextChar();
+          readAddress += hexToByte(c) << 4;
+          c = getNextChar();
+          readAddress += hexToByte(c);
+
+          printHex16(readAddress);
 
-          while (!Serial.available()) {
+          crc += readAddress >> 8;
+          crc += readAddress & 0x00FF;
+          Serial.print(".");
+
+          // reading data type
+          c = getNextChar();
+          type = hexToByte(c) << 4;
+          c = getNextChar();
+          type += hexToByte(c);
+          printHex8(type);
+
+          crc += type;
+
+          Serial.print(".");
+
+          if (type == 0x01) {
+            endOfFile = true;
           }
-          do {
-            c = Serial.read();
-          } while (!(((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F'))));
 
-          value = value * 16 + hexToByte(c);
-          EEPROM.write(addr + i, value);
-        }
-        Serial.print("w:");
-        for (byte i = 0; i < 8; i++) {
-          value = EEPROM.read(addr + i);;
-          Serial.print(value, HEX);
-          if (i < 7) {
-            Serial.print(",");
+          // read data bytes
+          for (byte x = 0; x < count; x++) {
+            c = getNextChar();
+            value = hexToByte(c) << 4;
+            c = getNextChar();
+            value += hexToByte(c);
+            printHex8(value);
+
+            Serial.print(".");
+            data[x] = value;
+            crc += value;
           }
+          // read CRC
+          c = getNextChar();
+          readcrc = hexToByte(c) << 4;
+          c = getNextChar();
+          readcrc += hexToByte(c);
+          printHex8(readcrc);
+
+          crc += readcrc;
+          // check CRC
+          value = crc & 0x00FF;
+          printHex8(value);
+
+          if (value == 0) {
+            Serial.print("ok");
+            // adding value to EEPROM
+            for (byte x = 0; x < count; x++) {
+              EEPROM.write(readAddress + x, data[x]);
+            }
+          } else {
+            Serial.println("CRC Error");
+            endOfFile = true;
+          }
+
+          Serial.println();
+        } while (!(endOfFile));
+        Serial.println("endOfFile");
+      }
+      if (myChar == 'r') {
+        // write eeprom as hexfile to receiver
+        Serial.println("EEPROM data:");
+        byte checksum = 0;
+        for (int addr = 0; addr <= E2END; addr++) {
+          value = EEPROM.read(addr);
+          if ((addr % 16) == 0) {
+            printCheckSum(checksum);
+            checksum = 0;
+            Serial.print(":10");
+            checksum += 0x10;
+            printHex16(addr);
+            checksum += (addr >> 8);
+            checksum += (addr & 0x00FF);
+            Serial.print("00");
+          }
+          printHex8(value);
+          checksum += value;
         }
-        Serial.println();
-        addr = addr + 8;
+        printCheckSum(checksum);
+        // ending
+        Serial.println(":00000001FF");
       }
-      if (myChar == '*') {
+      if (myChar == 'e') {
+        // end of programm
         endOfPrg = true;
       }
     }
@@ -60,6 +156,28 @@ void serialPrg() {
   doReset();
 }
 
+char getNextChar() {
+  while (!Serial.available()) {
+  }
+  return  Serial.read();
+}
+void printCheckSum(byte checksum) {
+  printHex8(checksum);
+  Serial.println();
+}
+
+void printHex8(int num) {
+  char tmp[16];
+  sprintf(tmp, "%.2X", num);
+  Serial.print(tmp);
+}
+
+void printHex16(int num) {
+  char tmp[16];
+  sprintf(tmp, "%.4X", num);
+  Serial.print(tmp);
+}
+
 byte hexToByte (char c) {
   if ( (c >= '0') && (c <= '9') ) {
     return c - '0';