serialprg.ino 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #ifdef SPS_SERIAL_PRG
  2. #if defined(__AVR_ATtiny861__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATmega328P__) || defined(ESP32)
  3. #define BAUDRATE 9600
  4. #endif
  5. void initSerialPrg() {
  6. Serial.end();
  7. Serial.begin(BAUDRATE);
  8. Serial.println();
  9. }
  10. void sendHeader() {
  11. #ifdef __AVR_ATtiny84__
  12. Serial.println("TinySPS");
  13. #endif
  14. #ifdef __AVR_ATmega328P__
  15. Serial.println("ArduinoSPS");
  16. #endif
  17. Serial.print("max prg size:");
  18. Serial.print(STORESIZE, HEX);
  19. Serial.println();
  20. Serial.println("waiting for command:");
  21. Serial.println("w: write HEX file, r: read EPPROM, e: end");
  22. }
  23. void serialPrg() {
  24. byte value;
  25. bool endOfPrg = false;
  26. bool endOfFile = false;
  27. byte data[32];
  28. word readAddress;
  29. int crc, readcrc;
  30. byte count;
  31. byte type;
  32. addr = 0;
  33. sendHeader();
  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. writebyte(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. store();
  139. }
  140. if (myChar == 'r') {
  141. // write eeprom as hexfile to receiver
  142. Serial.println("EEPROM data:");
  143. int checksum = 0;
  144. for (int addr = 0; addr <= STORESIZE; addr++) {
  145. value = readbyte(addr);
  146. if ((addr % 8) == 0) {
  147. printCheckSum(checksum);
  148. checksum = 0;
  149. Serial.print(":08");
  150. checksum += 0x08;
  151. printHex16(addr);
  152. checksum += (addr >> 8);
  153. checksum += (addr & 0x00FF);
  154. Serial.print("00");
  155. }
  156. printHex8(value);
  157. checksum += value;
  158. }
  159. printCheckSum(checksum);
  160. // ending
  161. Serial.println(":00000001FF");
  162. }
  163. if (myChar == 'e') {
  164. // end of programm
  165. endOfPrg = true;
  166. }
  167. if (myChar == 'h') {
  168. sendHeader();
  169. }
  170. }
  171. }
  172. Serial.println("end");
  173. }
  174. char getNextChar() {
  175. while (!Serial.available()) {
  176. }
  177. return Serial.read();
  178. }
  179. void printCheckSum(int value) {
  180. int checksum = value & 0xFF;
  181. checksum = (checksum ^ 0xFF) + 1;
  182. printHex8(checksum);
  183. Serial.println();
  184. }
  185. void printHex8(int num) {
  186. char tmp[3];
  187. tmp[0] = nibbleToHex(num >> 4);
  188. tmp[1] = nibbleToHex(num);
  189. tmp[2] = 0x00;
  190. Serial.print(tmp);
  191. }
  192. void printHex16(int num) {
  193. char tmp[5];
  194. tmp[0] = nibbleToHex(num >> 12);
  195. tmp[1] = nibbleToHex(num >> 8);
  196. tmp[2] = nibbleToHex(num >> 4);
  197. tmp[3] = nibbleToHex(num);
  198. tmp[4] = 0x00;
  199. Serial.print(tmp);
  200. }
  201. byte hexToByte (char c) {
  202. if ( (c >= '0') && (c <= '9') ) {
  203. return c - '0';
  204. }
  205. if ( (c >= 'A') && (c <= 'F') ) {
  206. return (c - 'A') + 10;
  207. }
  208. }
  209. byte nibbleToHex (byte value) {
  210. byte c = value & 0x0F;
  211. if ( (c >= 0) && (c <= 9) ) {
  212. return c + '0';
  213. }
  214. if ( (c >= 10) && (c <= 15) ) {
  215. return (c + 'A') - 10;
  216. }
  217. }
  218. #endif