Skip to content

Conversation

MrKevinWeiss
Copy link
Contributor

Contribution description

Update the srf02 sensor driver to the new I2C API.

Issues/PRs references

#6577

@MrKevinWeiss MrKevinWeiss added Type: cleanup The issue proposes a clean-up / The PR cleans-up parts of the codebase / documentation Area: drivers Area: Device drivers TF: I2C Marks issues and PRs related to the work of the I²C rework task force labels Jun 27, 2018
@MrKevinWeiss MrKevinWeiss self-assigned this Jun 27, 2018
@MrKevinWeiss
Copy link
Contributor Author

My srf02 may have some issues, it seems to work... just not reliably. This behavior is in the master as well. On the scope everything looks fine. (It basically will intermittently respond to a correct address). If someone can test it on different hardware that isn't possibly broken that would be great!

@MrKevinWeiss
Copy link
Contributor Author

It may be that or it may be this from the datasheet (although it shouldn't keep doing this)

Checking for Completion of Ranging
You do not have to use a timer on your own controller to wait for ranging to finish. You can take advantage of the fact that the SRF02 will not respond to any I2C activity whilst ranging. Therefore, if you try to read from the SRF02 (we use the software revision number a location 0) then you will get 255 (0xFF) whilst ranging. This is because the I2C data line (SDA) is pulled high if nothing is driving it. As soon as the ranging is complete the SRF02 will again respond to the I2C bus, so just keep reading the register until its not 255 (0xFF) anymore. You can then read the sonar data. Your controller can take advantage of this to perform other tasks while the SRF02 is ranging. The SRF02 will always be ready 70mS after initiating the ranging.


/* read the results */
i2c_acquire(dev->i2c);
i2c_read_regs(dev->i2c, dev->addr, REG_HIGH, res, 2);
i2c_read_regs(dev->i2c, dev->addr, REG_HIGH, res, 2, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not your fault and I know this is (more) about adapting to the new I2c iface ...

but what if read_regs fails? This function will return garbage then, right? To me it would make more sense to change the function to int srf02_read(const srf02_t *dev, uint16_t *val) write result to val and return 0 in success and errors accordingly.

However, that would at least be a separate commit, if not a PR -

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @smlng, we will have to change later the behaviour of many drivers.
For now, we can stay like this.

}

uint16_t srf02_read(const srf02_t *dev)
{
uint8_t res[2];
uint8_t res[2] = {0xFF, 0xFF};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 0xff instead of 0x0, just curious 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, you use that below to get invalid/failed reads, but then see my other comment on how this (IMHO) should be fixed instead.

Copy link
Member

@smlng smlng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adaption to new I2C interface looks good. Other changes might be done later on.

@dylad
Copy link
Member

dylad commented Jun 27, 2018

Can someone test this device on a second CPU arch before we merge it ?

@smlng
Copy link
Member

smlng commented Jun 27, 2018

I can try, if @MrKevinWeiss provides the sensor 😄

@smlng
Copy link
Member

smlng commented Jun 27, 2018

Tested on remote-revb with tests/driver_srf02/ app, works like charm - but didn't check in depth with scope or logic analyser. RE-ACK, would merge.

@dylad
Copy link
Member

dylad commented Jun 27, 2018

Here we go !

@dylad dylad merged commit f65649f into RIOT-OS:new_i2c_if Jun 27, 2018
basilfx pushed a commit to basilfx/RIOT that referenced this pull request Jul 10, 2018
dylad added a commit to dylad/RIOT that referenced this pull request Jul 10, 2018
@MrKevinWeiss MrKevinWeiss deleted the pr/drivers/srf02/i2c branch July 11, 2018 08:05
dylad added a commit that referenced this pull request Jul 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: drivers Area: Device drivers TF: I2C Marks issues and PRs related to the work of the I²C rework task force Type: cleanup The issue proposes a clean-up / The PR cleans-up parts of the codebase / documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants