Dcodes.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "Dcodes.h"
  2. #include "Marlin.h"
  3. #include "cmdqueue.h"
  4. inline void serial_print_hex_nibble(uint8_t val)
  5. {
  6. MYSERIAL.write((val > 9)?(val - 10 + 'a'):(val + '0'));
  7. }
  8. void serial_print_hex_byte(uint8_t val)
  9. {
  10. serial_print_hex_nibble(val >> 4);
  11. serial_print_hex_nibble(val & 15);
  12. }
  13. void serial_print_hex_word(uint16_t val)
  14. {
  15. serial_print_hex_byte(val >> 8);
  16. serial_print_hex_byte(val & 255);
  17. }
  18. int parse_hex(char* hex, uint8_t* data, int count)
  19. {
  20. int parsed = 0;
  21. while (*hex)
  22. {
  23. if (count && (parsed >= count)) break;
  24. char c = *(hex++);
  25. if (c == ' ') continue;
  26. if (c == '\n') break;
  27. uint8_t val = 0x00;
  28. if ((c >= '0') && (c <= '9')) val |= ((c - '0') << 4);
  29. else if ((c >= 'a') && (c <= 'f')) val |= ((c - 'a' + 10) << 4);
  30. else return -parsed;
  31. c = *(hex++);
  32. if ((c >= '0') && (c <= '9')) val |= (c - '0');
  33. else if ((c >= 'a') && (c <= 'f')) val |= (c - 'a' + 10);
  34. else return -parsed;
  35. data[parsed] = val;
  36. parsed++;
  37. }
  38. return parsed;
  39. }
  40. void dcode_0()
  41. {
  42. if (*(strchr_pointer + 1) == 0) return;
  43. MYSERIAL.println("D0 - Reset");
  44. if (code_seen('B')) //bootloader
  45. asm volatile("jmp 0x1e000");
  46. else //reset
  47. asm volatile("jmp 0x00000");
  48. /*
  49. cli(); //disable interrupts
  50. wdt_reset(); //reset watchdog
  51. WDTCSR = (1<<WDCE) | (1<<WDE); //enable watchdog
  52. WDTCSR = (1<<WDE) | (1<<WDP0); //30ms prescaler
  53. while(1); //wait for reset
  54. */
  55. }
  56. void dcode_1()
  57. {
  58. MYSERIAL.println("D1 - Clear EEPROM");
  59. cli();
  60. for (int i = 0; i < 4096; i++)
  61. eeprom_write_byte((unsigned char*)i, (unsigned char)0);
  62. sei();
  63. }
  64. void dcode_2()
  65. {
  66. MYSERIAL.println("D2 - Read/Write RAM");
  67. uint16_t address = 0x0000; //default 0x0000
  68. uint16_t count = 0x2000; //default 0x2000 (entire ram)
  69. if (code_seen('A')) // Address (0x0000-0x1fff)
  70. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  71. if (code_seen('C')) // Count (0x0001-0x2000)
  72. count = (int)code_value();
  73. address &= 0x1fff;
  74. if (count > 0x2000) count = 0x2000;
  75. if ((address + count) > 0x2000) count = 0x2000 - address;
  76. if (code_seen('X')) // Data
  77. {
  78. uint8_t data[16];
  79. count = parse_hex(strchr_pointer + 1, data, 16);
  80. if (count > 0)
  81. {
  82. for (int i = 0; i < count; i++)
  83. *((uint8_t*)(address + i)) = data[i];
  84. MYSERIAL.print(count, DEC);
  85. MYSERIAL.println(" bytes written to RAM at addres ");
  86. serial_print_hex_word(address);
  87. MYSERIAL.write('\n');
  88. }
  89. else
  90. count = 0;
  91. }
  92. while (count)
  93. {
  94. serial_print_hex_word(address);
  95. MYSERIAL.write(' ');
  96. uint8_t countperline = 16;
  97. while (count && countperline)
  98. {
  99. uint8_t data = *((uint8_t*)address++);
  100. MYSERIAL.write(' ');
  101. serial_print_hex_byte(data);
  102. countperline--;
  103. count--;
  104. }
  105. MYSERIAL.write('\n');
  106. }
  107. }
  108. void dcode_3()
  109. {
  110. MYSERIAL.println("D3 - Read/Write EEPROM");
  111. uint16_t address = 0x0000; //default 0x0000
  112. uint16_t count = 0x2000; //default 0x2000 (entire eeprom)
  113. if (code_seen('A')) // Address (0x0000-0x1fff)
  114. address = (strchr_pointer[1] == 'x')?strtol(strchr_pointer + 2, 0, 16):(int)code_value();
  115. if (code_seen('C')) // Count (0x0001-0x2000)
  116. count = (int)code_value();
  117. address &= 0x1fff;
  118. if (count > 0x2000) count = 0x2000;
  119. if ((address + count) > 0x2000) count = 0x2000 - address;
  120. if (code_seen('X')) // Data
  121. {
  122. uint8_t data[16];
  123. count = parse_hex(strchr_pointer + 1, data, 16);
  124. if (count > 0)
  125. {
  126. for (int i = 0; i < count; i++)
  127. eeprom_write_byte((uint8_t*)(address + i), data[i]);
  128. MYSERIAL.print(count, DEC);
  129. MYSERIAL.println(" bytes written to EEPROM at addres ");
  130. serial_print_hex_word(address);
  131. MYSERIAL.write('\n');
  132. }
  133. else
  134. count = 0;
  135. }
  136. while (count)
  137. {
  138. serial_print_hex_word(address);
  139. MYSERIAL.write(' ');
  140. uint8_t countperline = 16;
  141. while (count && countperline)
  142. {
  143. uint8_t data = eeprom_read_byte((uint8_t*)address++);
  144. MYSERIAL.write(' ');
  145. serial_print_hex_byte(data);
  146. countperline--;
  147. count--;
  148. }
  149. MYSERIAL.write('\n');
  150. }
  151. }
  152. void dcode_4()
  153. {
  154. MYSERIAL.println("D4 - Read/Write PIN");
  155. if (code_seen('P')) // Pin (0-255)
  156. {
  157. int pin = (int)code_value();
  158. if ((pin >= 0) && (pin <= 255))
  159. {
  160. if (code_seen('F')) // Function in/out (0/1)
  161. {
  162. int fnc = (int)code_value();
  163. if (fnc == 0) pinMode(pin, INPUT);
  164. else if (fnc == 1) pinMode(pin, OUTPUT);
  165. }
  166. if (code_seen('V')) // Value (0/1)
  167. {
  168. int val = (int)code_value();
  169. if (val == 0) digitalWrite(pin, LOW);
  170. else if (val == 1) digitalWrite(pin, HIGH);
  171. }
  172. else
  173. {
  174. int val = (digitalRead(pin) != LOW)?1:0;
  175. MYSERIAL.print("PIN");
  176. MYSERIAL.print(pin);
  177. MYSERIAL.print("=");
  178. MYSERIAL.println(val);
  179. }
  180. }
  181. }
  182. }