serialprg.ino 4.9 KB

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