serialprg.ino 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #ifdef __AVR_ATtiny84__
  2. #define BAUDRATE 9600
  3. #endif
  4. #ifdef __AVR_ATmega328P__
  5. #define BAUDRATE 9600
  6. #endif
  7. void serialPrg() {
  8. //int value1, value2, value3, value4;
  9. byte value;
  10. bool endOfPrg = false;
  11. bool endOfFile = false;
  12. byte data[32];
  13. char tmp[16];
  14. word readAddress;
  15. int crc, readcrc;
  16. byte count;
  17. byte type;
  18. addr = 0;
  19. Serial.end();
  20. Serial.begin(BAUDRATE);
  21. Serial.println("serPrgStart");
  22. Serial.println("waiting for command:");
  23. Serial.println("w: write HEX file, r: read EPPROM, e: end");
  24. while (!endOfPrg) {
  25. while (Serial.available() > 0) {
  26. // look for the next valid integer in the incoming serial stream:
  27. char myChar = Serial.read();
  28. if (myChar == 'w') {
  29. // hexfile is comming to programm
  30. Serial.println("waitin");
  31. addr = 0;
  32. do {
  33. for (byte i = 0; i < 8; i++) {
  34. data[i] = 0xFF;
  35. }
  36. do {
  37. c = getNextChar();
  38. } while (!(c == ':'));
  39. Serial.print(".");
  40. // read counter
  41. c = getNextChar();
  42. count = hexToByte(c) << 4;
  43. c = getNextChar();
  44. count += hexToByte(c);
  45. printHex8(count);
  46. crc = count;
  47. Serial.print(".");
  48. // address
  49. c = getNextChar();
  50. readAddress = hexToByte(c) << 12;
  51. c = getNextChar();
  52. readAddress += hexToByte(c) << 8;
  53. c = getNextChar();
  54. readAddress += hexToByte(c) << 4;
  55. c = getNextChar();
  56. readAddress += hexToByte(c);
  57. printHex16(readAddress);
  58. crc += readAddress >> 8;
  59. crc += readAddress & 0x00FF;
  60. Serial.print(".");
  61. // reading data type
  62. c = getNextChar();
  63. type = hexToByte(c) << 4;
  64. c = getNextChar();
  65. type += hexToByte(c);
  66. printHex8(type);
  67. crc += type;
  68. Serial.print(".");
  69. if (type == 0x01) {
  70. endOfFile = true;
  71. }
  72. // read data bytes
  73. for (byte x = 0; x < count; x++) {
  74. c = getNextChar();
  75. value = hexToByte(c) << 4;
  76. c = getNextChar();
  77. value += hexToByte(c);
  78. printHex8(value);
  79. Serial.print(".");
  80. data[x] = value;
  81. crc += value;
  82. }
  83. // read CRC
  84. c = getNextChar();
  85. readcrc = hexToByte(c) << 4;
  86. c = getNextChar();
  87. readcrc += hexToByte(c);
  88. printHex8(readcrc);
  89. crc += readcrc;
  90. // check CRC
  91. value = crc & 0x00FF;
  92. printHex8(value);
  93. if (value == 0) {
  94. Serial.print("ok");
  95. // adding value to EEPROM
  96. for (byte x = 0; x < count; x++) {
  97. EEPROM.write(readAddress + x, data[x]);
  98. }
  99. } else {
  100. Serial.println("CRC Error");
  101. endOfFile = true;
  102. }
  103. Serial.println();
  104. } while (!(endOfFile));
  105. Serial.println("endOfFile");
  106. }
  107. if (myChar == 'r') {
  108. // write eeprom as hexfile to receiver
  109. Serial.println("EEPROM data:");
  110. byte checksum = 0;
  111. for (int addr = 0; addr <= E2END; addr++) {
  112. value = EEPROM.read(addr);
  113. if ((addr % 16) == 0) {
  114. printCheckSum(checksum);
  115. checksum = 0;
  116. Serial.print(":10");
  117. checksum += 0x10;
  118. printHex16(addr);
  119. checksum += (addr >> 8);
  120. checksum += (addr & 0x00FF);
  121. Serial.print("00");
  122. }
  123. printHex8(value);
  124. checksum += value;
  125. }
  126. printCheckSum(checksum);
  127. // ending
  128. Serial.println(":00000001FF");
  129. }
  130. if (myChar == 'e') {
  131. // end of programm
  132. endOfPrg = true;
  133. }
  134. }
  135. }
  136. Serial.println("end");
  137. Serial.end();
  138. doReset();
  139. }
  140. char getNextChar() {
  141. while (!Serial.available()) {
  142. }
  143. return Serial.read();
  144. }
  145. void printCheckSum(byte checksum) {
  146. printHex8(checksum);
  147. Serial.println();
  148. }
  149. void printHex8(int num) {
  150. char tmp[16];
  151. sprintf(tmp, "%.2X", num);
  152. Serial.print(tmp);
  153. }
  154. void printHex16(int num) {
  155. char tmp[16];
  156. sprintf(tmp, "%.4X", num);
  157. Serial.print(tmp);
  158. }
  159. byte hexToByte (char c) {
  160. if ( (c >= '0') && (c <= '9') ) {
  161. return c - '0';
  162. }
  163. if ( (c >= 'A') && (c <= 'F') ) {
  164. return (c - 'A') + 10;
  165. }
  166. }