Advertisement
Guest User

Untitled

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