1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers/srf08: Acquire exclusive access to I2C bus

This commit is contained in:
PeterKietzmann 2015-01-20 08:54:40 +01:00
parent c9deca5610
commit a1e8cedf6b

View File

@ -21,8 +21,6 @@
* @}
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "hwtimer.h"
@ -35,17 +33,27 @@
int srf08_init(srf08_t *dev, i2c_t i2c, uint8_t addr, i2c_speed_t speed)
{
int status;
dev->i2c = i2c;
dev->addr = addr;
/* Acquire exclusive access to the bus. */
i2c_acquire(dev->i2c);
/* initialize i2c interface */
if(i2c_init_master(i2c, speed) < 0) {
status = i2c_init_master(dev->i2c, speed);
/* Release the bus for other threads. */
i2c_release(dev->i2c);
if(status < 0) {
return -1;
}
/* set the maximum range */
if (srf08_set_max_range(dev, SRF08_MAX_RANGE_6M) < 0) {
return -3;
}
/* set the maximum gain */
if (srf08_set_max_gain(dev, SRF08_MAX_GAIN) < 0) {
return -4;
@ -57,13 +65,29 @@ int srf08_init(srf08_t *dev, i2c_t i2c, uint8_t addr, i2c_speed_t speed)
int srf08_set_max_range(srf08_t *dev, uint8_t max_range)
{
return i2c_write_reg(dev->i2c, dev->addr, SRF08_RANGE_REG, max_range);
int status;
/* Acquire exclusive access to the bus. */
i2c_acquire(dev->i2c);
status = i2c_write_reg(dev->i2c, dev->addr, SRF08_RANGE_REG, max_range);
/* Release the bus for other threads. */
i2c_release(dev->i2c);
return status;
}
int srf08_set_max_gain(srf08_t *dev, uint8_t gain)
{
return i2c_write_reg(dev->i2c, dev->addr, SRF08_GAIN_REG, gain);
int status;
/* Acquire exclusive access to the bus. */
i2c_acquire(dev->i2c);
status = i2c_write_reg(dev->i2c, dev->addr, SRF08_GAIN_REG, gain);
/* Release the bus for other threads. */
i2c_release(dev->i2c);
return status;
}
@ -75,8 +99,12 @@ int srf08_get_distances(srf08_t *dev, uint16_t *range_array, int num_echos, srf0
char register_location;
char max_reg_no_read = (num_echos * sizeof(range_bytes)) +1;
/* Acquire exclusive access to the bus. */
i2c_acquire(dev->i2c);
/* set ranging mode */
status = i2c_write_reg(dev->i2c, dev->addr, SRF08_COMMAND_REG, ranging_mode);
/* Release the bus for other threads. */
i2c_release(dev->i2c);
if (!status) {
DEBUG("Write the ranging command to the i2c-interface is failed");
@ -94,8 +122,12 @@ int srf08_get_distances(srf08_t *dev, uint16_t *range_array, int num_echos, srf0
for (register_location = 2; register_location < max_reg_no_read;
register_location += sizeof(range_bytes)) {
/* Acquire exclusive access to the bus. */
i2c_acquire(dev->i2c);
/* read the echo bytes */
status = i2c_read_regs(dev->i2c, dev->addr, register_location, range_bytes, sizeof(range_bytes));
/* Release the bus for other threads. */
i2c_release(dev->i2c);
if (!status) {
DEBUG("Read the echo bytes from the i2c-interface is failed");