Forráskód Böngészése

Merge pull request #609 from XPila/MK3-V3_2

FSensor PAT9125 - i2c ACK check + two attempts in init function (PFW-208).
PavelSindler 6 éve
szülő
commit
0503aba23a

+ 20 - 6
Firmware/fsensor.cpp

@@ -63,7 +63,7 @@ bool fsensor_enable()
 {
 //	puts_P(PSTR("fsensor_enable\n"));
 	int pat9125 = pat9125_init();
-//    printf_P(PSTR("PAT9125_init:%d\n"), pat9125);
+    printf_P(PSTR("PAT9125_init:%d\n"), pat9125);
 	if (pat9125)
 		fsensor_not_responding = false;
 	else
@@ -74,6 +74,7 @@ bool fsensor_enable()
 	fsensor_err_cnt = 0;
 	eeprom_update_byte((uint8_t*)EEPROM_FSENSOR, fsensor_enabled?0x01:0x00); 
 	FSensorStateMenu = fsensor_enabled?1:0;
+//	printf_P(PSTR("fsensor_enable - end %d\n"), fsensor_enabled?1:0);
 	return fsensor_enabled;
 }
 
@@ -108,7 +109,14 @@ void fsensor_setup_interrupt()
 void fsensor_autoload_check_start(void)
 {
 //	puts_P(PSTR("fsensor_autoload_check_start\n"));
-	pat9125_update_y(); //update sensor
+	if (!pat9125_update_y()) //update sensor
+	{
+		puts_P(PSTR("pat9125 not responding (3).\n"));
+		fsensor_disable();
+		fsensor_not_responding = true;
+		fsensor_autoload_enabled = false;
+		return;
+	}
 	fsensor_autoload_y = pat9125_y; //save current y value
 	fsensor_autoload_c = 0; //reset number of changes counter
 	fsensor_autoload_sum = 0;
@@ -130,7 +138,13 @@ bool fsensor_check_autoload(void)
 	uint8_t fsensor_autoload_c_old = fsensor_autoload_c;
 	if ((millis() - fsensor_autoload_last_millis) < 25) return false;
 	fsensor_autoload_last_millis = millis();
-	pat9125_update_y(); //update sensor
+	if (!pat9125_update_y())
+	{
+		puts_P(PSTR("pat9125 not responding (2).\n"));
+		fsensor_disable();
+		fsensor_not_responding = true;
+		return false; //update sensor
+	}
 	int16_t dy = fsensor_autoload_y - pat9125_y;
 	if (dy) //? y value is different
 	{
@@ -170,9 +184,9 @@ ISR(PCINT2_vect)
 	*digitalPinToPCMSK(fsensor_int_pin) |= bit(digitalPinToPCMSKbit(fsensor_int_pin));*/
 	if (!pat9125_update_y())
 	{
-#ifdef DEBUG_FSENSOR_LOG
-		puts_P(PSTR("pat9125 not responding.\n"));
-#endif //DEBUG_FSENSOR_LOG
+//#ifdef DEBUG_FSENSOR_LOG
+		puts_P(PSTR("pat9125 not responding (1).\n"));
+//#endif //DEBUG_FSENSOR_LOG
 		fsensor_disable();
 		fsensor_not_responding = true;
 	}

+ 30 - 1
Firmware/pat9125.cpp

@@ -92,7 +92,10 @@ int pat9125_init()
 //	pat9125_PID2 = 0x91;
 	if ((pat9125_PID1 != 0x31) || (pat9125_PID2 != 0x91))
 	{
-		return 0;
+		pat9125_PID1 = pat9125_rd_reg(PAT9125_PID1);
+		pat9125_PID2 = pat9125_rd_reg(PAT9125_PID2);
+		if ((pat9125_PID1 != 0x31) || (pat9125_PID2 != 0x91))
+			return 0;
 	}
 	// Switch to bank0, not allowed to perform OTS_RegWriteRead.
 	pat9125_wr_reg(PAT9125_BANK_SELECTION, 0);
@@ -132,6 +135,9 @@ int pat9125_init()
 	pat9125_wr_reg(PAT9125_BANK_SELECTION, 0x00);
 	// Enable write protect.
 	pat9125_wr_reg(PAT9125_WP, 0x00);
+
+	pat9125_PID1 = pat9125_rd_reg(PAT9125_PID1);
+	pat9125_PID2 = pat9125_rd_reg(PAT9125_PID2);
 	return 1;
 }
 
@@ -142,11 +148,13 @@ int pat9125_update()
 		unsigned char ucMotion = pat9125_rd_reg(PAT9125_MOTION);
 		pat9125_b = pat9125_rd_reg(PAT9125_FRAME);
 		pat9125_s = pat9125_rd_reg(PAT9125_SHUTTER);
+		if (pat9125_PID1 == 0xff) return 0;
 		if (ucMotion & 0x80)
 		{
 			unsigned char ucXL = pat9125_rd_reg(PAT9125_DELTA_XL);
 			unsigned char ucYL = pat9125_rd_reg(PAT9125_DELTA_YL);
 			unsigned char ucXYH = pat9125_rd_reg(PAT9125_DELTA_XYH);
+			if (pat9125_PID1 == 0xff) return 0;
 			int iDX = ucXL | ((ucXYH << 4) & 0xf00);
 			int iDY = ucYL | ((ucXYH << 8) & 0xf00);
 			if (iDX & 0x800) iDX -= 4096;
@@ -164,10 +172,12 @@ int pat9125_update_y()
 	if ((pat9125_PID1 == 0x31) && (pat9125_PID2 == 0x91))
 	{
 		unsigned char ucMotion = pat9125_rd_reg(PAT9125_MOTION);
+		if (pat9125_PID1 == 0xff) return 0;
 		if (ucMotion & 0x80)
 		{
 			unsigned char ucYL = pat9125_rd_reg(PAT9125_DELTA_YL);
 			unsigned char ucXYH = pat9125_rd_reg(PAT9125_DELTA_XYH);
+			if (pat9125_PID1 == 0xff) return 0;
 			int iDY = ucYL | ((ucXYH << 8) & 0xf00);
 			if (iDY & 0x800) iDY -= 4096;
 			pat9125_y -= iDY; //negative number, because direction switching does not work
@@ -179,6 +189,7 @@ int pat9125_update_y()
 
 unsigned char pat9125_rd_reg(unsigned char addr)
 {
+//    printf_P(PSTR("pat9125_rd_reg 0x%hhx "), addr);
 	unsigned char data = 0;
 #ifdef PAT9125_SWSPI
 	swspi_start();
@@ -188,6 +199,14 @@ unsigned char pat9125_rd_reg(unsigned char addr)
 #endif //PAT9125_SWSPI
 #ifdef PAT9125_SWI2C
 	int iret = swi2c_readByte_A8(PAT9125_I2C_ADDR, addr, &data);
+	if (!iret) //NO ACK error
+	{
+		pat9125_PID1 = 0xff;
+		pat9125_PID2 = 0xff;
+//	    printf_P(PSTR("ERR\n"));
+		return 0;
+	}
+//    printf_P(PSTR("0x%hhx OK\n"), data);
 #endif //PAT9125_SWI2C
 #ifdef PAT9125_HWI2C
 	Wire.beginTransmission(PAT9125_I2C_ADDR);
@@ -202,6 +221,7 @@ unsigned char pat9125_rd_reg(unsigned char addr)
 
 void pat9125_wr_reg(unsigned char addr, unsigned char data)
 {
+//    printf_P(PSTR("pat9125_wr_reg 0x%hhx 0x%hhx  "), addr, data);
 #ifdef PAT9125_SWSPI
 	swspi_start();
 	swspi_tx(addr | 0x80);
@@ -210,6 +230,15 @@ void pat9125_wr_reg(unsigned char addr, unsigned char data)
 #endif //PAT9125_SWSPI
 #ifdef PAT9125_SWI2C
 	int iret = swi2c_writeByte_A8(PAT9125_I2C_ADDR, addr, &data);
+	if (!iret) //NO ACK error
+	{
+		pat9125_PID1 = 0xff;
+		pat9125_PID2 = 0xff;
+//	    printf_P(PSTR("ERR\n"));
+		return;
+	}
+//    printf_P(PSTR("OK\n"));
+
 #endif //PAT9125_SWI2C
 #ifdef PAT9125_HWI2C
 	Wire.beginTransmission(PAT9125_I2C_ADDR);

+ 1 - 1
Firmware/ultralcd_implementation_hitachi_HD44780.h

@@ -194,7 +194,7 @@ extern volatile uint16_t buttons;  //an extended version of the last checked but
     LCD_CLASS lcd(LCD_I2C_ADDRESS, LCD_WIDTH, LCD_HEIGHT);
     
 // 2 wire Non-latching LCD SR from:
-// https://bitbucket.org/fmalpartida/new-LiquidCrystal_Prusa/wiki/schematics#!shiftregister-connection 
+// https://bitbucket.org/fmalpartida/new-LiquidCrystal/wiki/schematics#!shiftregister-connection 
 #elif defined(SR_LCD_2W_NL)
 
   extern "C" void __cxa_pure_virtual() { while (1); }