Dcodes.cpp 5.8 KB

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