Use _check instead of register read for probe()

This commit is contained in:
Alex Voinea 2022-02-25 19:23:05 +01:00 committed by D.R.racer
parent 54b98c4e63
commit 28c527fece
4 changed files with 31 additions and 10 deletions

View File

@ -112,16 +112,10 @@ uint8_t pat9125_probe()
#error not implemented
#elif defined(PAT9125_SWI2C)
swi2c_init();
return swi2c_readByte_A8(PAT9125_I2C_ADDR,0x00,NULL);
return swi2c_check(PAT9125_I2C_ADDR) == 0;
#elif defined(PAT9125_I2C)
twi_init();
#ifdef IR_SENSOR
// NOTE: this is called from the MK3S variant, so it should be kept minimal
uint8_t data;
return (twi_r8(PAT9125_I2C_ADDR,PAT9125_PID1,&data) == 0);
#else
return (pat9125_rd_reg(PAT9125_PID1) != 0);
#endif
return twi_check(PAT9125_I2C_ADDR) == 0;
#endif
}

View File

@ -142,9 +142,9 @@ uint8_t swi2c_check(uint8_t dev_addr)
{
swi2c_start();
swi2c_write((dev_addr & SWI2C_DMSK) << SWI2C_ASHF);
if (!swi2c_wait_ack()) { swi2c_stop(); return 0; }
if (!swi2c_wait_ack()) { swi2c_stop(); return 1; }
swi2c_stop();
return 1;
return 0;
}
#ifdef SWI2C_A8 //8bit address

View File

@ -101,6 +101,25 @@ static uint8_t twi_start(uint8_t address, uint8_t reg)
}
uint8_t twi_check(uint8_t address)
{
// send start condition
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
if(twi_wait(TW_START))
return 1;
// send address
TWDR = TW_WRITE | (address << 1);
TWCR = _BV(TWEN) | _BV(TWINT);
if(twi_wait(TW_MT_SLA_ACK))
return 2;
// send stop
twi_stop();
return 0;
}
uint8_t twi_r8(uint8_t address, uint8_t reg, uint8_t* data)
{
if(twi_start(address, reg))

View File

@ -44,6 +44,14 @@ void twi_init(void);
*/
void twi_disable(void);
/*
* Function twi_check
* Desc checks if a device exists on the bus
* Input address: 7bit i2c device address
* Output 0 on device found at address
*/
uint8_t twi_check(uint8_t address);
/*
* Function twi_r8
* Desc read a single byte from a device