Advertisement
Guest User

davinci reset

a guest
Feb 4th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.35 KB | None | 0 0
  1. /*
  2.  
  3. Da Vinci EEPROM update Copyright (C) 2014 by Oliver Fueckert <[email protected]>
  4. UNI/O Library Copyright (C) 2011 by Stephen Early <[email protected]>
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. "Software"), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merg`e, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be
  15. included in all copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  24.  
  25. #ifndef _NANODEUNIO_LIB_H
  26. #define _NANODEUNIO_LIB_H
  27.  
  28. #if ARDUINO >= 100
  29. #include <Arduino.h> // Arduino 1.0
  30. #else
  31. #include <WProgram.h> // Arduino 0022
  32. #endif
  33.  
  34. #define NANODE_MAC_DEVICE 0xa0
  35. #define NANODE_MAC_ADDRESS 0xfa
  36.  
  37. class NanodeUNIO {
  38. private:
  39. byte addr;
  40. public:
  41. NanodeUNIO(byte address);
  42.  
  43. boolean read(byte *buffer,word address,word length);
  44. boolean start_write(const byte *buffer,word address,word length);
  45. boolean enable_write(void);
  46. boolean disable_write(void);
  47. boolean read_status(byte *status);
  48. boolean write_status(byte status);
  49. boolean await_write_complete(void);
  50. boolean simple_write(const byte *buffer,word address,word length);
  51. };
  52.  
  53. #endif /* _NANODEUNIO_LIB_H */
  54.  
  55. #define UNIO_STARTHEADER 0x55
  56. #define UNIO_READ 0x03
  57. #define UNIO_CRRD 0x06
  58. #define UNIO_WRITE 0x6c
  59. #define UNIO_WREN 0x96
  60. #define UNIO_WRDI 0x91
  61. #define UNIO_RDSR 0x05
  62. #define UNIO_WRSR 0x6e
  63. #define UNIO_ERAL 0x6d
  64. #define UNIO_SETAL 0x67
  65.  
  66. #define UNIO_TSTBY 600
  67. #define UNIO_TSS 10
  68. #define UNIO_THDR 5
  69. #define UNIO_QUARTER_BIT 10
  70. #define UNIO_FUDGE_FACTOR 5
  71. #define UNIO_OUTPUT() do { DDRD |= 0x80; } while (0)
  72. #define UNIO_INPUT() do { DDRD &= 0x7f; } while (0)
  73.  
  74. static void set_bus(boolean state) {
  75. PORTD=(PORTD&0x7f)|(!!state)<<7;
  76. }
  77.  
  78. static boolean read_bus(void) {
  79. return !!(PIND&0x80);
  80. }
  81. static void unio_inter_command_gap(void) {
  82. set_bus(1);
  83. delayMicroseconds(UNIO_TSS+UNIO_FUDGE_FACTOR);
  84. }
  85.  
  86. static void unio_standby_pulse(void) {
  87. set_bus(0);
  88. UNIO_OUTPUT();
  89. delayMicroseconds(UNIO_TSS+UNIO_FUDGE_FACTOR);
  90. set_bus(1);
  91. delayMicroseconds(UNIO_TSTBY+UNIO_FUDGE_FACTOR);
  92. }
  93.  
  94. static volatile boolean rwbit(boolean w) {
  95. boolean a,b;
  96. set_bus(!w);
  97. delayMicroseconds(UNIO_QUARTER_BIT);
  98. a=read_bus();
  99. delayMicroseconds(UNIO_QUARTER_BIT);
  100. set_bus(w);
  101. delayMicroseconds(UNIO_QUARTER_BIT);
  102. b=read_bus();
  103. delayMicroseconds(UNIO_QUARTER_BIT);
  104. return b&&!a;
  105. }
  106.  
  107. static boolean read_bit(void) {
  108. boolean b;
  109. UNIO_INPUT();
  110. b=rwbit(1);
  111. UNIO_OUTPUT();
  112. return b;
  113. }
  114.  
  115. static boolean send_byte(byte b, boolean mak) {
  116. for (int i=0; i<8; i++) {
  117. rwbit(b&0x80);
  118. b<<=1;
  119. }
  120. rwbit(mak);
  121. return read_bit();
  122. }
  123.  
  124. static boolean read_byte(byte *b, boolean mak) {
  125. byte data=0;
  126. UNIO_INPUT();
  127. for (int i=0; i<8; i++) {
  128. data = (data << 1) | rwbit(1);
  129. }
  130. UNIO_OUTPUT();
  131. *b=data;
  132. rwbit(mak);
  133. return read_bit();
  134. }
  135.  
  136. static boolean unio_send(const byte *data,word length,boolean end) {
  137. for (word i=0; i<length; i++) {
  138. if (!send_byte(data[i],!(((i+1)==length) && end))) return false;
  139. }
  140. return true;
  141. }
  142.  
  143. static boolean unio_read(byte *data,word length) {
  144. for (word i=0; i<length; i++) {
  145. if (!read_byte(data+i,!((i+1)==length))) return false;
  146. }
  147. return true;
  148. }
  149.  
  150. static void unio_start_header(void) {
  151. set_bus(0);
  152. delayMicroseconds(UNIO_THDR+UNIO_FUDGE_FACTOR);
  153. send_byte(UNIO_STARTHEADER,true);
  154. }
  155.  
  156. NanodeUNIO::NanodeUNIO(byte address) {
  157. addr=address;
  158. }
  159.  
  160. #define fail() do { sei(); return false; } while (0)
  161.  
  162. boolean NanodeUNIO::read(byte *buffer,word address,word length) {
  163. byte cmd[4];
  164. cmd[0]=addr;
  165. cmd[1]=UNIO_READ;
  166. cmd[2]=(byte)(address>>8);
  167. cmd[3]=(byte)(address&0xff);
  168. unio_standby_pulse();
  169. cli();
  170. unio_start_header();
  171. if (!unio_send(cmd,4,false)) fail();
  172. if (!unio_read(buffer,length)) fail();
  173. sei();
  174. return true;
  175. }
  176.  
  177. boolean NanodeUNIO::start_write(const byte *buffer,word address,word length) {
  178. byte cmd[4];
  179. if (((address&0x0f)+length)>16) return false; // would cross page boundary
  180. cmd[0]=addr;
  181. cmd[1]=UNIO_WRITE;
  182. cmd[2]=(byte)(address>>8);
  183. cmd[3]=(byte)(address&0xff);
  184. unio_standby_pulse();
  185. cli();
  186. unio_start_header();
  187. if (!unio_send(cmd,4,false)) fail();
  188. if (!unio_send(buffer,length,true)) fail();
  189. sei();
  190. return true;
  191. }
  192.  
  193. boolean NanodeUNIO::enable_write(void) {
  194. byte cmd[2];
  195. cmd[0]=addr;
  196. cmd[1]=UNIO_WREN;
  197. unio_standby_pulse();
  198. cli();
  199. unio_start_header();
  200. if (!unio_send(cmd,2,true)) fail();
  201. sei();
  202. return true;
  203. }
  204.  
  205. boolean NanodeUNIO::disable_write(void) {
  206. byte cmd[2];
  207. cmd[0]=addr;
  208. cmd[1]=UNIO_WRDI;
  209. unio_standby_pulse();
  210. cli();
  211. unio_start_header();
  212. if (!unio_send(cmd,2,true)) fail();
  213. sei();
  214. return true;
  215. }
  216.  
  217. boolean NanodeUNIO::read_status(byte *status) {
  218. byte cmd[2];
  219. cmd[0]=addr;
  220. cmd[1]=UNIO_RDSR;
  221. unio_standby_pulse();
  222. cli();
  223. unio_start_header();
  224. if (!unio_send(cmd,2,false)) fail();
  225. if (!unio_read(status,1)) fail();
  226. sei();
  227. return true;
  228. }
  229.  
  230. boolean NanodeUNIO::write_status(byte status) {
  231. byte cmd[3];
  232. cmd[0]=addr;
  233. cmd[1]=UNIO_WRSR;
  234. cmd[2]=status;
  235. unio_standby_pulse();
  236. cli();
  237. unio_start_header();
  238. if (!unio_send(cmd,3,true)) fail();
  239. sei();
  240. return true;
  241. }
  242.  
  243. boolean NanodeUNIO::await_write_complete(void) {
  244. byte cmd[2];
  245. byte status;
  246. cmd[0]=addr;
  247. cmd[1]=UNIO_RDSR;
  248. unio_standby_pulse();
  249. do {
  250. unio_inter_command_gap();
  251. cli();
  252. unio_start_header();
  253. if (!unio_send(cmd,2,false)) fail();
  254. if (!unio_read(&status,1)) fail();
  255. sei();
  256. } while (status&0x01);
  257. return true;
  258. }
  259.  
  260. boolean NanodeUNIO::simple_write(const byte *buffer,word address,word length) {
  261. word wlen;
  262. while (length>0) {
  263. wlen=length;
  264. if (((address&0x0f)+wlen)>16) {
  265. wlen=16-(address&0x0f);
  266. }
  267. if (!enable_write()) return false;
  268. if (!start_write(buffer,address,wlen)) return false;
  269. if (!await_write_complete()) return false;
  270. buffer+=wlen;
  271. address+=wlen;
  272. length-=wlen;
  273. }
  274. return true;
  275. }
  276.  
  277. static void status(boolean r)
  278. {
  279. if (r) Serial.println("(success)");
  280. else Serial.println("(failure)");
  281. }
  282.  
  283. static void dump_eeprom(word address,word length)
  284. {
  285. byte buf[128];
  286. char lbuf[80];
  287. char *x;
  288. int i,j;
  289.  
  290. NanodeUNIO unio(NANODE_MAC_DEVICE);
  291.  
  292. memset(buf,0,128);
  293. status(unio.read(buf,address,length));
  294.  
  295. for (i=0; i<128; i+=16) {
  296. x=lbuf;
  297. sprintf(x,"%02X: ",i);
  298. x+=4;
  299. for (j=0; j<16; j++) {
  300. sprintf(x,"%02X",buf[i+j]);
  301. x+=2;
  302. }
  303. *x=32;
  304. x+=1;
  305. for (j=0; j<16; j++) {
  306. if (buf[i+j]>=32 && buf[i+j]<127) *x=buf[i+j];
  307. else *x=46;
  308. x++;
  309. }
  310. *x=0;
  311. Serial.println(lbuf);
  312. }
  313. }
  314.  
  315. int led = 13;
  316. /*
  317. These are the values to be written to the EEPROM
  318. Make sure only one is uncommented.
  319. By default its set for the starter ABS cartdridge with 120m of filament
  320.  
  321. Verified with firmware 1.1.I
  322. */
  323.  
  324. // Value to write to the EEPROM for remaining filament lenght
  325. // Default Starter Cartdridge is 120m
  326. //120m
  327. //char x[] = {0xc0,0xd4,0x01,0x00};
  328. //240m
  329. char x[] = {0x80,0xa9,0x03,0x00};
  330. //400m
  331. //char x[] = {0x80,0x1a,0x06,0x00};
  332.  
  333. // extruder temp, default is 210 C for ABS
  334. char et[] = {0xe6,0x00}; // 240 C
  335. //char et[] = {0xe6,0x00}; // 230 C
  336.  
  337. // bed temp 90 degrees, default ABS
  338. char bt[] = {0x5a,0x00};
  339.  
  340. byte sr;
  341. NanodeUNIO unio(NANODE_MAC_DEVICE);
  342.  
  343. void setup() {
  344. Serial.begin(115200);
  345. }
  346.  
  347. void loop() {
  348.  
  349. do {
  350. digitalWrite(led, LOW);
  351. Serial.println("Testing connection to Da Vinci EEPROM CHIP\n");
  352. delay(100);
  353. digitalWrite(led, HIGH);
  354. } while(!unio.read_status(&sr));
  355.  
  356. Serial.println("Da Vinci EEPROM found...");
  357. Serial.println("Reading the Davinci EEPROM Contents...");
  358. dump_eeprom(0,128);
  359. //dump_eeprom(116,4);
  360.  
  361. Serial.println("Updating EEPROM...");
  362. status(unio.simple_write((const byte *)x,8,4));
  363. status(unio.simple_write((const byte *)x,12,4));
  364. status(unio.simple_write((const byte *)et,16,2)); // extruder temp
  365. status(unio.simple_write((const byte *)bt,18,2)); // bed temp
  366. status(unio.simple_write((const byte *)x,52,4));
  367. // same block from offset 0 is offset 64 bytes
  368. status(unio.simple_write((const byte *)x,64 + 8,4));
  369. status(unio.simple_write((const byte *)x,64 + 12,4));
  370. status(unio.simple_write((const byte *)et,64 + 16,2)); // extruder temp
  371. status(unio.simple_write((const byte *)bt,64 + 18,2)); // bed temp
  372. status(unio.simple_write((const byte *)x,64 + 52,4));
  373.  
  374. Serial.println("Dumping Content after modification...");
  375. dump_eeprom(0,128);
  376.  
  377. digitalWrite(led, HIGH); // turn the LED on
  378. delay(10000); // wait for two seconds
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement