1
0

serialprg.ino 5.4 KB

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