1
0

serialprg.ino 5.0 KB

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