From dea3f23a69f700032b97f765a77d03b71a2d1e27 Mon Sep 17 00:00:00 2001 From: Voinea Dragos Date: Thu, 14 Jan 2021 11:52:22 +0200 Subject: [PATCH 1/3] PRUSA SN in eeprom --- Firmware/Marlin_main.cpp | 38 ++++++++++++++++++++++++++++++-------- Firmware/eeprom.h | 4 +++- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 3be465912..86b14402c 100755 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -407,6 +407,7 @@ static void gcode_M105(uint8_t extruder); static void temp_compensation_start(); static void temp_compensation_apply(); +static bool get_PRUSA_SN(char* SN); uint16_t gcode_in_progress = 0; uint16_t mcode_in_progress = 0; @@ -1075,6 +1076,21 @@ void setup() if(!(eeprom_read_byte((uint8_t*)EEPROM_FAN_CHECK_ENABLED))) eeprom_update_byte((unsigned char *)EEPROM_FAN_CHECK_ENABLED,true); } + + + if (eeprom_read_byte((uint8_t*)EEPROM_PRUSA_SN + 19)) //saved EEPROM SN is not valid. Try to retrieve it. + { + char SN[20]; + if (get_PRUSA_SN(SN)) + { + eeprom_update_block(SN, (uint8_t*)EEPROM_PRUSA_SN, 20); + puts_P(PSTR("SN updated")); + } + else + puts_P(PSTR("SN update failed")); + } + + #ifndef W25X20CL SERIAL_PROTOCOLLNPGM("start"); #else @@ -3386,25 +3402,26 @@ void gcode_M701() * * Typical format of S/N is:CZPX0917X003XC13518 * - * Command operates only in farm mode, if not in farm mode, "Not in farm mode." is written to MYSERIAL. - * * Send command ;S to serial port 0 to retrieve serial number stored in 32U2 processor, - * reply is transmitted to serial port 1 character by character. + * reply is transmitted to the selected serial port. * Operation takes typically 23 ms. If the retransmit is not finished until 100 ms, * it is interrupted, so less, or no characters are retransmitted, only newline character is send * in any case. + * The command will fail if the 32U2 processor is unpowered via USB since it is isolated from the rest of the electronics. + * In that case the value that is stored in the EEPROM should be used instead. + * + * @return 1 on success */ -static void gcode_PRUSA_SN() +static bool get_PRUSA_SN(char* SN) { uint8_t selectedSerialPort_bak = selectedSerialPort; - char SN[20]; selectedSerialPort = 0; SERIAL_ECHOLNRPGM(PSTR(";S")); uint8_t numbersRead = 0; ShortTimer timeout; timeout.start(); - while (numbersRead < (sizeof(SN) - 1)) { + while (numbersRead < 19) { if (MSerial.available() > 0) { SN[numbersRead] = MSerial.read(); numbersRead++; @@ -3413,7 +3430,7 @@ static void gcode_PRUSA_SN() } SN[numbersRead] = 0; selectedSerialPort = selectedSerialPort_bak; - SERIAL_ECHOLN(SN); + return (numbersRead == 19); } //! Detection of faulty RAMBo 1.1b boards equipped with bigger capacitors //! at the TACH_1 pin, which causes bad detection of print fan speed. @@ -3950,7 +3967,12 @@ void process_commands() card.openFile(strchr_pointer+4,false); } else if (code_seen("SN")) { // PRUSA SN - gcode_PRUSA_SN(); + char SN[20]; + eeprom_read_block(SN, (uint8_t*)EEPROM_PRUSA_SN, 20); + if (SN[19]) + puts_P(PSTR("SN invalid")); + else + puts(SN); } else if(code_seen("Fir")){ // PRUSA Fir diff --git a/Firmware/eeprom.h b/Firmware/eeprom.h index 731db1da1..f54bb8ed7 100644 --- a/Firmware/eeprom.h +++ b/Firmware/eeprom.h @@ -316,6 +316,7 @@ static_assert(sizeof(Sheets) == EEPROM_SHEETS_SIZEOF, "Sizeof(Sheets) is not EEP | 0x0D29 3369 | uint8 | EEPROM_PINDA_TEMP_COMPENSATION | ffh 255 | ffh 255 | PINDA temp compensation unknown state | LCD menu | D3 Ax0d29 C1 | ^ | ^ | ^ | 00h 0 | ^ | PINDA has no temp compensation PINDA v1/2 | ^ | ^ | ^ | ^ | ^ | 01h 1 | ^ | PINDA has temp compensation aka SuperPINDA | ^ | ^ +| 0x0D15 3349 | char[20] | EEPROM_PRUSA_SN | SN[19] == 0 | ffffffffffffffff... | PRUSA Serial number string | PRUSA SN | D3 Ax0d15 C20 | Address begin | Bit/Type | Name | Valid values | Default/FactoryReset | Description | Gcode/Function| Debug code @@ -521,8 +522,9 @@ static Sheets * const EEPROM_Sheets_base = (Sheets*)(EEPROM_SHEETS_BASE); #define EEPROM_ALTFAN_OVERRIDE (EEPROM_UVLO_LA_K-1) //uint8 #define EEPROM_EXPERIMENTAL_VISIBILITY (EEPROM_ALTFAN_OVERRIDE-1) //uint8 #define EEPROM_PINDA_TEMP_COMPENSATION (EEPROM_EXPERIMENTAL_VISIBILITY-1) //uint8 +#define EEPROM_PRUSA_SN (EEPROM_PINDA_TEMP_COMPENSATION-20) //char[20] //This is supposed to point to last item to allow EEPROM overrun check. Please update when adding new items. -#define EEPROM_LAST_ITEM EEPROM_PINDA_TEMP_COMPENSATION +#define EEPROM_LAST_ITEM EEPROM_PRUSA_SN // !!!!! // !!!!! this is end of EEPROM section ... all updates MUST BE inserted before this mark !!!!! // !!!!! From 20c3f4cb7709692824d7751e150d9df0606b75b3 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Thu, 14 Jan 2021 12:53:12 +0200 Subject: [PATCH 2/3] Update comments --- Firmware/Marlin_main.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 86b14402c..d43e05e58 100755 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -1077,8 +1077,9 @@ void setup() eeprom_update_byte((unsigned char *)EEPROM_FAN_CHECK_ENABLED,true); } - - if (eeprom_read_byte((uint8_t*)EEPROM_PRUSA_SN + 19)) //saved EEPROM SN is not valid. Try to retrieve it. + //saved EEPROM SN is not valid. Try to retrieve it. + //SN is valid only if it is NULL terminated. Any other character means either uninitialized or corrupted + if (eeprom_read_byte((uint8_t*)EEPROM_PRUSA_SN + 19)) { char SN[20]; if (get_PRUSA_SN(SN)) @@ -3403,14 +3404,14 @@ void gcode_M701() * Typical format of S/N is:CZPX0917X003XC13518 * * Send command ;S to serial port 0 to retrieve serial number stored in 32U2 processor, - * reply is transmitted to the selected serial port. + * reply is stored in *SN. * Operation takes typically 23 ms. If the retransmit is not finished until 100 ms, - * it is interrupted, so less, or no characters are retransmitted, only newline character is send - * in any case. + * it is interrupted, so less, or no characters are retransmitted, the function returns false * The command will fail if the 32U2 processor is unpowered via USB since it is isolated from the rest of the electronics. * In that case the value that is stored in the EEPROM should be used instead. * * @return 1 on success + * @return 0 on general failure */ static bool get_PRUSA_SN(char* SN) { From 698499f00df6bc7797cdf8bca212661cfd79f8da Mon Sep 17 00:00:00 2001 From: Voinea Dragos Date: Sun, 31 Jan 2021 17:18:32 +0200 Subject: [PATCH 3/3] split timer0 and timer2 initialization. Move timer2 init early --- Firmware/Marlin_main.cpp | 2 ++ Firmware/temperature.cpp | 5 ++++- Firmware/timer02.c | 20 ++++++++++++-------- Firmware/timer02.h | 3 +++ 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index d43e05e58..68ecb1c19 100755 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -1013,6 +1013,8 @@ static void w25x20cl_err_msg() // are initialized by the main() routine provided by the Arduino framework. void setup() { + timer2_init(); // enables functional millis + mmu_init(); ultralcd_init(); diff --git a/Firmware/temperature.cpp b/Firmware/temperature.cpp index 3b38c25cc..a1281b649 100755 --- a/Firmware/temperature.cpp +++ b/Firmware/temperature.cpp @@ -1124,7 +1124,10 @@ void tp_init() adc_init(); - timer0_init(); + timer0_init(); //enables the heatbed timer. + + // timer2 already enabled earlier in the code + // now enable the COMPB temperature interrupt OCR2B = 128; TIMSK2 |= (1< #include +#include "macros.h" void timer0_init(void) { - //save sreg - uint8_t _sreg = SREG; - //disable interrupts for sure - cli(); + CRITICAL_SECTION_START; TCNT0 = 0; // Fast PWM duty (0-255). @@ -25,7 +23,14 @@ void timer0_init(void) TCCR0A = (1 << WGM01) | (1 << WGM00) | (1 << COM0B1) | (1 << COM0B0); TCCR0B = (1 << CS01); // CLK/8 prescaling TIMSK0 |= (1 << TOIE0); // enable timer overflow interrupt - + + CRITICAL_SECTION_END; +} + +void timer2_init(void) +{ + CRITICAL_SECTION_START; + // Everything, that used to be on timer0 was moved to timer2 (delay, beeping, millis etc.) //setup timer2 TCCR2A = 0x00; //COM_A-B=00, WGM_0-1=00 @@ -36,9 +41,8 @@ void timer0_init(void) TIMSK2 &= ~(1<