1
0

serialprg.ino 5.1 KB

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