/* Da Vinci EEPROM update (C) Samuel Dupre - V1.3 Pour re-programmation de cartouche Da Vinci: Choix: -copier les données de 40h-7Fh à 00h-3Fh -incrémentation du numéro de série de 11 à 12 -passer à 240m -V1.1 ajout de la température lit fils par défaut 210°C -V1.2 permet avec menu serie de changer les valeurs -type de fils ABS ou PLA -température du fil -température du lit -sauvegarde dans l'EEPROM arduino V1.3 permet de rentrer des valeurs >99 er Da Vinci EEPROM update Copyright (C) 2014 by Oliver Fueckert UNI/O Library Copyright (C) 2011 by Stephen Early Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef _NANODEUNIO_LIB_H #define _NANODEUNIO_LIB_H #if ARDUINO >= 100 #include // Arduino 1.0 #else #include // Arduino 0022 #endif #include //Sauvegarde des paramètres #define NANODE_MAC_DEVICE 0xa0 #define NANODE_MAC_ADDRESS 0xfa byte menu; //Lecture un octet byte menu1; //Lecture un octet byte menu2; //Lecture un octet int tfil = 210; //temperature du fil int tlit = 90; //temperature du lit long longueur = 240000; //longueur du fil char mat = 65; //A ABS P PLA byte prg[4];//Pour programmer la longueur; class NanodeUNIO { private: byte addr; public: NanodeUNIO(byte address); boolean read(byte *buffer, word address, word length); boolean start_write(const byte *buffer, word address, word length); boolean enable_write(void); boolean disable_write(void); boolean read_status(byte *status); boolean write_status(byte status); boolean await_write_complete(void); boolean simple_write(const byte *buffer, word address, word length); }; #endif /* _NANODEUNIO_LIB_H */ #define UNIO_STARTHEADER 0x55 #define UNIO_READ 0x03 #define UNIO_CRRD 0x06 #define UNIO_WRITE 0x6c #define UNIO_WREN 0x96 #define UNIO_WRDI 0x91 #define UNIO_RDSR 0x05 #define UNIO_WRSR 0x6e #define UNIO_ERAL 0x6d #define UNIO_SETAL 0x67 #define UNIO_TSTBY 600 #define UNIO_TSS 10 #define UNIO_THDR 5 #define UNIO_QUARTER_BIT 10 #define UNIO_FUDGE_FACTOR 5 #define UNIO_OUTPUT() do { DDRD |= 0x80; } while (0) #define UNIO_INPUT() do { DDRD &= 0x7f; } while (0) static void set_bus(boolean state) { PORTD = (PORTD & 0x7f) | (!!state) << 7; } static boolean read_bus(void) { return !!(PIND & 0x80); } static void unio_inter_command_gap(void) { set_bus(1); delayMicroseconds(UNIO_TSS + UNIO_FUDGE_FACTOR); } static void unio_standby_pulse(void) { set_bus(0); UNIO_OUTPUT(); delayMicroseconds(UNIO_TSS + UNIO_FUDGE_FACTOR); set_bus(1); delayMicroseconds(UNIO_TSTBY + UNIO_FUDGE_FACTOR); } static volatile boolean rwbit(boolean w) { boolean a, b; set_bus(!w); delayMicroseconds(UNIO_QUARTER_BIT); a = read_bus(); delayMicroseconds(UNIO_QUARTER_BIT); set_bus(w); delayMicroseconds(UNIO_QUARTER_BIT); b = read_bus(); delayMicroseconds(UNIO_QUARTER_BIT); return b && !a; } static boolean read_bit(void) { boolean b; UNIO_INPUT(); b = rwbit(1); UNIO_OUTPUT(); return b; } static boolean send_byte(byte b, boolean mak) { for (int i = 0; i < 8; i++) { rwbit(b & 0x80); b <<= 1; } rwbit(mak); return read_bit(); } static boolean read_byte(byte *b, boolean mak) { byte data = 0; UNIO_INPUT(); for (int i = 0; i < 8; i++) { data = (data << 1) | rwbit(1); } UNIO_OUTPUT(); *b = data; rwbit(mak); return read_bit(); } static boolean unio_send(const byte *data, word length, boolean end) { for (word i = 0; i < length; i++) { if (!send_byte(data[i], !(((i + 1) == length) && end))) return false; } return true; } static boolean unio_read(byte *data, word length) { for (word i = 0; i < length; i++) { if (!read_byte(data + i, !((i + 1) == length))) return false; } return true; } static void unio_start_header(void) { set_bus(0); delayMicroseconds(UNIO_THDR + UNIO_FUDGE_FACTOR); send_byte(UNIO_STARTHEADER, true); } NanodeUNIO::NanodeUNIO(byte address) { addr = address; } #define fail() do { sei(); return false; } while (0) boolean NanodeUNIO::read(byte *buffer, word address, word length) { byte cmd[4]; cmd[0] = addr; cmd[1] = UNIO_READ; cmd[2] = (byte)(address >> 8); cmd[3] = (byte)(address & 0xff); unio_standby_pulse(); cli(); unio_start_header(); if (!unio_send(cmd, 4, false)) fail(); if (!unio_read(buffer, length)) fail(); sei(); return true; } boolean NanodeUNIO::start_write(const byte *buffer, word address, word length) { byte cmd[4]; if (((address & 0x0f) + length) > 16) return false; // would cross page boundary cmd[0] = addr; cmd[1] = UNIO_WRITE; cmd[2] = (byte)(address >> 8); cmd[3] = (byte)(address & 0xff); unio_standby_pulse(); cli(); unio_start_header(); if (!unio_send(cmd, 4, false)) fail(); if (!unio_send(buffer, length, true)) fail(); sei(); return true; } boolean NanodeUNIO::enable_write(void) { byte cmd[2]; cmd[0] = addr; cmd[1] = UNIO_WREN; unio_standby_pulse(); cli(); unio_start_header(); if (!unio_send(cmd, 2, true)) fail(); sei(); return true; } boolean NanodeUNIO::disable_write(void) { byte cmd[2]; cmd[0] = addr; cmd[1] = UNIO_WRDI; unio_standby_pulse(); cli(); unio_start_header(); if (!unio_send(cmd, 2, true)) fail(); sei(); return true; } boolean NanodeUNIO::read_status(byte *status) { byte cmd[2]; cmd[0] = addr; cmd[1] = UNIO_RDSR; unio_standby_pulse(); cli(); unio_start_header(); if (!unio_send(cmd, 2, false)) fail(); if (!unio_read(status, 1)) fail(); sei(); return true; } boolean NanodeUNIO::write_status(byte status) { byte cmd[3]; cmd[0] = addr; cmd[1] = UNIO_WRSR; cmd[2] = status; unio_standby_pulse(); cli(); unio_start_header(); if (!unio_send(cmd, 3, true)) fail(); sei(); return true; } boolean NanodeUNIO::await_write_complete(void) { byte cmd[2]; byte status; cmd[0] = addr; cmd[1] = UNIO_RDSR; unio_standby_pulse(); do { unio_inter_command_gap(); cli(); unio_start_header(); if (!unio_send(cmd, 2, false)) fail(); if (!unio_read(&status, 1)) fail(); sei(); } while (status & 0x01); return true; } boolean NanodeUNIO::simple_write(const byte *buffer, word address, word length) { word wlen; while (length > 0) { wlen = length; if (((address & 0x0f) + wlen) > 16) { wlen = 16 - (address & 0x0f); } if (!enable_write()) return false; if (!start_write(buffer, address, wlen)) return false; if (!await_write_complete()) return false; buffer += wlen; address += wlen; length -= wlen; } return true; } static void status(boolean r) { if (r) Serial.println("(success)"); else Serial.println("(failure)"); } static void IncrementSerial(unsigned char * cArray, long lAddress, long lSize) { unsigned char szTempBuffer[20] = {0}; memcpy(szTempBuffer, &cArray[lAddress], lSize); long lSerial = atol((char *)szTempBuffer); lSerial++; sprintf((char *)szTempBuffer, "%04d", lSerial); memcpy(&cArray[lAddress], szTempBuffer, lSize); } static void change_eeprom(word address, word length) { byte buf[128]; int i, j; //Pour affichage char lbuf[80]; char *x; NanodeUNIO unio(NANODE_MAC_DEVICE); memset(buf, 0, 128); status(unio.read(buf, address, length)); //Change la numero de serie dans la partie 40h-7Fh IncrementSerial(&buf[0], 0x58, 12); //char prg[] = {0x80, 0xa9, 0x03, 0x00}; //240m for (i = 0; i < 4; i++) { buf[i + 0x48] = prg[i]; buf[i + 0x4C] = prg[i]; buf[i + 0x74] = prg[i]; } /* température - Fil ABS 210°C(D2) par defaut ou 230°C(E6) - lit 90°C (5A) par defaut */ buf[0x1] = mat; buf[0x50] = tfil; //230°C fils buf[0x52] = tlit; //90°C lit ne chane pas pour l'instant. for (i = 0; i < 0x40; i++) { //copier les donnees de 40h-7Fh à 00h-3Fh buf[i] = buf[i + 0x40]; } status(unio.simple_write((const byte *)buf, 0, 128)); } static void dump_eeprom(word address, word length) { byte buf[128]; char lbuf[80]; char *x; int i, j; NanodeUNIO unio(NANODE_MAC_DEVICE); memset(buf, 0, 128); status(unio.read(buf, address, length)); for (i = 0; i < 128; i += 16) { x = lbuf; sprintf(x, "%02X: ", i); x += 4; for (j = 0; j < 16; j++) { sprintf(x, "%02X", buf[i + j]); x += 2; } *x = 32; x += 1; //Liste des caractères for (j = 0; j < 16; j++) { if (buf[i + j] >= 32 && buf[i + j] < 127) *x = buf[i + j]; else *x = 46; x++; } *x = 0; Serial.println(lbuf); } } static void videbuffer_att() { while (Serial.available() > 0) { char t = Serial.read(); } do {} while (!Serial.available()); } static int readnumber() { //lit une chaine de caractère et la convertit en entier videbuffer_att(); while (Serial.available() == 0); int blinkRate = Serial.parseInt(); //read int or parseFloat for ..float... // // while (Serial.available() > 0) { // String recup = Serial.readString(); // int myStringLength = recup.length(); // char myChar[myStringLength]; // recup.toCharArray(myChar, myStringLength); // blinkRate = atoi(myChar); // } return blinkRate; } static void converttobyte(unsigned long y) { char buffer[33]; char *strptr; strptr = ultoa(y, buffer, 16); long number = (long) strtol( &strptr[0], NULL, 16); // Split them up into r, g, b values long r = number >> 16; long g = number >> 8 & 0xFF; long b = number & 0xFF; prg[0] = b; prg[1] = g; prg[2] = r; } static void writesetup() { //char prg[] = {0x80,0xa9,0x03,0x00};//240m converttobyte(longueur); EEPROM.write(1, mat); for (int i = 0; i < 4; i++) { EEPROM.write(i + 0x48, prg[i]); } EEPROM.write(0x50, tfil); //230°C fils EEPROM.write(0x51, 0x00); EEPROM.write(0x52, tlit); //90°C lit ne chane pas pour l'instant. EEPROM.write(0x53, 0x00); } static void readsetup() { mat = EEPROM.read(1); for (int i = 0; i < 4; i++) { prg[i] = EEPROM.read(i + 0x48); } tfil = EEPROM.read(0x50); //230°C fils tlit = EEPROM.read(0x52); //90°C lit ne chane pas pour l'instant. unsigned long octet2 = prg[2] * 256; unsigned long octet1 = (prg[1] + octet2); longueur = (octet1 * 256 + prg[0]); } static void choix() { Serial.print("\nConfiguration:\nMatiere: "); if (mat == 65) { Serial.println("ABS"); } else { Serial.println("PLA"); } Serial.print("Longueur: "); Serial.print(longueur / 1000); Serial.println("m"); Serial.print("Temperature du fils: "); Serial.print(tfil); Serial.write(byte(0xB0)); Serial.println("C"); Serial.print("Temperature du lit: "); Serial.print(tlit); Serial.write(byte(0xB0)); Serial.println("C"); } static void mymenu() { //Menu pour y acceder tapper 'menu' sur le monitor série boolean retour_M1 = false; boolean retour_M11 = false; digitalWrite(13, LOW); //Eteind la led de l'arduino while (retour_M1 == false) { choix(); //Menu Serial.println("Menu\n1. configurer\n2. Sortie avec Sauvegarde\n3. Sortie sans sauvegarde"); videbuffer_att(); menu = Serial.read(); if (menu == 49) { retour_M11 = false; while (retour_M11 == false) { //Menu 1. Serial.println("Menu1. Configurer\n1. Matiere\n2. Longueur de fil\n3. Temperature du fil\n4. Temperature du lit\n5. Back"); videbuffer_att(); menu1 = Serial.read(); //Menu 1.1. if (menu1 == 49) { Serial.println("Menu\n1. ABS\n2. PLA"); videbuffer_att(); menu2 = Serial.read(); if (menu2 == 49) { //ABS Serial.println("ABS"); mat = 65; //true ABS } if (menu2 == 50) { //PLA Serial.println("PLA"); mat = 80; //false PLA } } //Menu 1.2. if (menu1 == 50) { Serial.println("Longueur de fil \n1. 120m\n2. 240m");//Longueur de fils videbuffer_att(); menu2 = Serial.read(); if (menu2 == 49) { //120m Serial.println("120m"); longueur = 120000; } if (menu2 == 50) { //240m Serial.println("240m"); longueur = 240000; } } if (menu1 == 51) { Serial.print("Temperature du fil : actuelle="); Serial.print(tfil); Serial.write(byte(0xB0)); Serial.println("C"); Serial.println("\n-ABS:180 <=Temperature<=250 defaut 210\n-PLA 180<=Temperature<=225"); tfil = readnumber(); Serial.print("Temperature du fil : "); Serial.print(tfil); Serial.write(byte(0xB0)); Serial.println("C"); } if (menu1 == 52) { Serial.print("Temperature du lit : actuelle="); Serial.print(tlit); Serial.write(byte(0xB0)); Serial.println("C"); Serial.println("\n-ABS 90<=Temperature<=110 défaut 90\n-PLA 55<=Temperature<=65"); tlit = readnumber(); Serial.print("Temperature du lit : "); Serial.print(tlit); Serial.write(byte(0xB0)); Serial.println("C"); } if (menu1 == 53) { //Exit retour_M11 = true; } } } if (menu == 50) { //Menu 2 Avec sauvegarde writesetup(); retour_M1 = true; } if (menu == 51) { //Menu 3 Sans sauvegarde readsetup(); retour_M1 = true; } } } int led = 13; /* These are the values to be written to the EEPROM Make sure only one is uncommented. By default its set for the starter ABS cartdridge with 120m of filament Verified with firmware 1.1.I */ // Value to write to the EEPROM for remaining filament lenght // Default Starter Cartdridge is 120m //120m //char x[] = {0xc0,0xd4,0x01,0x00}; //240m //char x[] = {0x80,0xa9,0x03,0x00}; //400m //char x[] = {0x80,0x1a,0x06,0x00}; // extruder temp, default is 210 C for ABS //char et[] = {0xd2,0x00}; // 210 C //char et[] = {0xe6,0x00}; // 230 C // bed temp 90 degrees, default ABS //char bt[] = {0x5a,0x00}; byte sr; NanodeUNIO unio(NANODE_MAC_DEVICE); void setup() { Serial.begin(9600); Serial.println("(c)V1.2-2016 sammy76.free.fr"); readsetup(); choix(); Serial.println("\nTaper 'menu' pour configurer"); delay(3000); //3secondes } void loop() { do { digitalWrite(led, LOW); Serial.println("Verifie la connection avec l'EEPROM Da Vinci..."); delay(100); digitalWrite(led, HIGH); if (Serial.available()) { // read the most recent byte String Str = Serial.readString(); if (Str.substring(0, 4) == "menu") { //'1' menu mymenu(); } } } while (!unio.read_status(&sr)); Serial.println("Da Vinci EEPROM trouvee..."); Serial.println("Lit le contenu de l'EEPROM Da Vinci..."); dump_eeprom(0, 128); Serial.println("Mise a jour de l'EEPROM Da Vinci..."); change_eeprom(0, 128); // Serial.println("Lit le contenu de l'EEPROM Da Vinci apres modification..."); dump_eeprom(0, 128); digitalWrite(led, HIGH); // turn the LED on delay(10000); // wait for two seconds }