drivers/1wire: Adds some improvents

The following changes where added.
- Removes dependency to slave structure. Selecting a device on the bus only
  requires the unique romcode.

- Make search operation mutual exclusive on the bus. This is necessary to
  prevent another driver instance from making a transition on the bus at
  the same time.

- Skip also rom when only 1 slave is expected on the bus. Physical
  connected devuce should be indicated by the maxslave paramter during
  initializing, not the reigstered slaves.

- Fixes some compiler warnings in existing ds28e17 driver.

Signed-off-by: Marco Krahl <ocram.lhark@gmail.com>
This commit is contained in:
Marco Krahl
2021-04-08 19:12:10 +02:00
committed by Xiang Xiao
parent 8456f3615e
commit 624a60f456
3 changed files with 29 additions and 26 deletions
+19 -9
View File
@@ -330,9 +330,9 @@ int onewire_reset_resume(FAR struct onewire_master_s *master)
*
****************************************************************************/
int onewire_reset_select(FAR struct onewire_slave_s *slave)
int onewire_reset_select(FAR struct onewire_master_s *master,
uint64_t romcode)
{
FAR struct onewire_master_s *master = slave->master;
uint64_t tmp;
uint8_t skip_rom[1] =
{
@@ -354,13 +354,13 @@ int onewire_reset_select(FAR struct onewire_slave_s *slave)
/* Issue skip-ROM if single slave, match-ROM otherwise. */
if (master->nslaves == 1)
if (master->nslaves == 1 || master->maxslaves == 1)
{
ret = ONEWIRE_WRITE(master->dev, skip_rom, sizeof(skip_rom));
}
else
{
tmp = onewire_leuint64(slave->romcode);
tmp = onewire_leuint64(romcode);
memcpy(&match_rom[1], &tmp, 8);
ret = ONEWIRE_WRITE(master->dev, match_rom, sizeof(match_rom));
}
@@ -541,6 +541,14 @@ int onewire_search(FAR struct onewire_master_s *master,
DEBUGASSERT(master->insearch == false);
/* Make complete search on the bus mutal exlusive */
ret = onewire_sem_wait(master);
if (ret < 0)
{
return ret;
}
/* Skip costly search if bus supports only a single slave. */
if (master->maxslaves == 1)
@@ -554,7 +562,7 @@ int onewire_search(FAR struct onewire_master_s *master,
nslaves_match++;
}
return (ret < 0) ? ret : nslaves_match;
goto unlock;
}
/* Select search type. */
@@ -570,7 +578,7 @@ int onewire_search(FAR struct onewire_master_s *master,
ret = ONEWIRE_RESET(dev);
if (ret < 0)
{
return ret;
goto unlock;
}
/* Send the Search-ROM command. */
@@ -578,7 +586,7 @@ int onewire_search(FAR struct onewire_master_s *master,
ret = ONEWIRE_WRITE(dev, cmd, sizeof(cmd));
if (ret < 0)
{
return ret;
goto unlock;
}
last_rom = rom;
@@ -611,7 +619,7 @@ int onewire_search(FAR struct onewire_master_s *master,
{
/* Error or zero valid directions. */
return ret;
goto unlock;
}
if (ret == 2 && taken_bit == 0)
@@ -668,7 +676,9 @@ int onewire_search(FAR struct onewire_master_s *master,
*/
}
return nslaves_match;
unlock:
onewire_sem_post(master);
return (ret < 0) ? ret : nslaves_match;
}
/****************************************************************************
+2 -1
View File
@@ -111,7 +111,8 @@ int onewire_reset_resume(FAR struct onewire_master_s *master);
*
****************************************************************************/
int onewire_reset_select(FAR struct onewire_slave_s *slave);
int onewire_reset_select(FAR struct onewire_master_s *master,
uint64_t romcode);
/****************************************************************************
* Name: onewire_triplet
+8 -16
View File
@@ -132,7 +132,9 @@ struct ds_i2c_inst_s /* Must be cast-compatible with i2c_maste
* Private Function Prototypes
****************************************************************************/
#ifdef CONFIG_I2C_RESET
static int ds_i2c_reset(FAR struct i2c_master_s *i2cdev);
#endif
static int ds_i2c_transfer(FAR struct i2c_master_s *i2cdev,
FAR struct i2c_msg_s *msgs, int count);
@@ -581,15 +583,15 @@ static int ds_i2c_setfrequency(FAR struct ds_i2c_inst_s *inst,
break;
default:
i2cerr("ERROR: bad I2C freq %u\n", frequency);
i2cerr("ERROR: bad I2C freq %lu\n", frequency);
return -EINVAL;
}
i2cinfo("Changing I2C freq %u -> %u\n", inst->frequency, frequency);
i2cinfo("Changing I2C freq %lu -> %lu\n", inst->frequency, frequency);
/* Select DS28E17 */
ret = onewire_reset_select(&inst->slave);
ret = onewire_reset_select(master, inst->slave.romcode);
if (ret < 0)
{
i2cerr("ERROR: cannot change I2C freq\n");
@@ -640,7 +642,7 @@ static int ds_i2c_process(FAR struct i2c_master_s *i2cdev,
/* Select DS28E17 */
i = onewire_reset_select(&inst->slave);
i = onewire_reset_select(master, inst->slave.romcode);
if (i < 0)
{
goto errout;
@@ -714,7 +716,7 @@ static int ds_i2c_process(FAR struct i2c_master_s *i2cdev,
}
else
{
ret = onewire_reset_select(&inst->slave);
ret = onewire_reset_select(master, inst->slave.romcode);
}
if (ret < 0)
@@ -877,20 +879,10 @@ int ds28e17_search(FAR struct ds28e17_dev_s *priv,
void *arg)
{
FAR struct onewire_master_s *master = (FAR struct onewire_master_s *)priv;
int ret;
DEBUGASSERT(master != NULL && cb_search != NULL);
ret = onewire_sem_wait(master);
if (ret < 0)
{
return ret;
}
ret = onewire_search(master, DS_FAMILY, false, cb_search, arg);
onewire_sem_post(master);
return ret;
return onewire_search(master, DS_FAMILY, false, cb_search, arg);
}
/****************************************************************************