serialprg.ino 5.6 KB

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