1
0

serialprg.ino 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. endOfFile = false;
  33. Serial.println("ready");
  34. addr = 0;
  35. do {
  36. for (byte i = 0; i < 8; i++) {
  37. data[i] = 0xFF;
  38. }
  39. do {
  40. c = getNextChar();
  41. } while (!(c == ':'));
  42. #ifdef debug
  43. Serial.print(".");
  44. #endif
  45. // read counter
  46. c = getNextChar();
  47. count = hexToByte(c) << 4;
  48. c = getNextChar();
  49. count += hexToByte(c);
  50. #ifdef debug
  51. printHex8(count);
  52. #endif
  53. crc = count;
  54. #ifdef debug
  55. Serial.print(".");
  56. #endif
  57. // address
  58. c = getNextChar();
  59. readAddress = hexToByte(c) << 12;
  60. c = getNextChar();
  61. readAddress += hexToByte(c) << 8;
  62. c = getNextChar();
  63. readAddress += hexToByte(c) << 4;
  64. c = getNextChar();
  65. readAddress += hexToByte(c);
  66. #ifdef debug
  67. printHex16(readAddress);
  68. #endif
  69. crc += readAddress >> 8;
  70. crc += readAddress & 0x00FF;
  71. #ifdef debug
  72. Serial.print(".");
  73. #endif
  74. // reading data type
  75. c = getNextChar();
  76. type = hexToByte(c) << 4;
  77. c = getNextChar();
  78. type += hexToByte(c);
  79. #ifdef debug
  80. printHex8(type);
  81. #endif
  82. crc += type;
  83. #ifdef debug
  84. Serial.print(".");
  85. #endif
  86. if (type == 0x01) {
  87. endOfFile = true;
  88. }
  89. // read data bytes
  90. for (byte x = 0; x < count; x++) {
  91. c = getNextChar();
  92. value = hexToByte(c) << 4;
  93. c = getNextChar();
  94. value += hexToByte(c);
  95. #ifdef debug
  96. printHex8(value);
  97. Serial.print(".");
  98. #endif
  99. data[x] = value;
  100. crc += value;
  101. }
  102. // read CRC
  103. c = getNextChar();
  104. readcrc = hexToByte(c) << 4;
  105. c = getNextChar();
  106. readcrc += hexToByte(c);
  107. #ifdef debug
  108. printHex8(readcrc);
  109. Serial.print(".");
  110. #endif
  111. crc += readcrc;
  112. // check CRC
  113. value = crc & 0x00FF;
  114. #ifdef debug
  115. printHex8(value);
  116. #endif
  117. if (value == 0) {
  118. Serial.print("ok");
  119. // adding value to EEPROM
  120. for (byte x = 0; x < count; x++) {
  121. EEPROM.write(readAddress + x, data[x]);
  122. }
  123. } else {
  124. Serial.println(", CRC Error");
  125. endOfFile = true;
  126. }
  127. Serial.println();
  128. } while (!(endOfFile));
  129. Serial.println("endOfFile");
  130. }
  131. if (myChar == 'r') {
  132. // write eeprom as hexfile to receiver
  133. Serial.println("EEPROM data:");
  134. int checksum = 0;
  135. for (int addr = 0; addr <= E2END; addr++) {
  136. value = EEPROM.read(addr);
  137. if ((addr % 8) == 0) {
  138. printCheckSum(checksum);
  139. checksum = 0;
  140. Serial.print(":08");
  141. checksum += 0x08;
  142. printHex16(addr);
  143. checksum += (addr >> 8);
  144. checksum += (addr & 0x00FF);
  145. Serial.print("00");
  146. }
  147. printHex8(value);
  148. checksum += value;
  149. }
  150. printCheckSum(checksum);
  151. // ending
  152. Serial.println(":00000001FF");
  153. }
  154. if (myChar == 'e') {
  155. // end of programm
  156. endOfPrg = true;
  157. }
  158. }
  159. }
  160. Serial.println("end");
  161. Serial.end();
  162. doReset();
  163. }
  164. char getNextChar() {
  165. while (!Serial.available()) {
  166. }
  167. return Serial.read();
  168. }
  169. void printCheckSum(int value) {
  170. int checksum = value & 0xFF;
  171. checksum = (checksum ^ 0xFF) + 1;
  172. printHex8(checksum);
  173. Serial.println();
  174. }
  175. void printHex8(int num) {
  176. char tmp[3];
  177. tmp[0] = nibbleToHex(num >> 4);
  178. tmp[1] = nibbleToHex(num);
  179. tmp[2] = 0x00;
  180. Serial.print(tmp);
  181. }
  182. void printHex16(int num) {
  183. char tmp[5];
  184. tmp[0] = nibbleToHex(num >> 12);
  185. tmp[1] = nibbleToHex(num >> 8);
  186. tmp[2] = nibbleToHex(num >> 4);
  187. tmp[3] = nibbleToHex(num);
  188. tmp[4] = 0x00;
  189. Serial.print(tmp);
  190. }
  191. byte hexToByte (char c) {
  192. if ( (c >= '0') && (c <= '9') ) {
  193. return c - '0';
  194. }
  195. if ( (c >= 'A') && (c <= 'F') ) {
  196. return (c - 'A') + 10;
  197. }
  198. }
  199. byte nibbleToHex (byte value) {
  200. byte c = value & 0x0F;
  201. if ( (c >= 0) && (c <= 9) ) {
  202. return c + '0';
  203. }
  204. if ( (c >= 10) && (c <= 15) ) {
  205. return (c + 'A') - 10;
  206. }
  207. }
  208. #endif