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

drivers/srf02: Acquire exclusive access to I2C bus

This commit is contained in:
PeterKietzmann 2015-01-20 08:25:26 +01:00
parent 83cc1acd61
commit c9deca5610

View File

@ -21,13 +21,9 @@
* @}
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "msg.h"
#include "vtimer.h"
#include "timex.h"
#include "thread.h"
#include "mutex.h"
#include "hwtimer.h"
#include "srf02.h"
#include "periph/i2c.h"
@ -38,11 +34,18 @@
int srf02_init(srf02_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 */
return i2c_init_master(i2c, speed);
status = i2c_init_master(dev->i2c, speed);
/* Release the bus for other threads. */
i2c_release(dev->i2c);
return status;
}
uint16_t srf02_get_distance(srf02_t *dev, srf02_mode_t mode)
@ -52,8 +55,12 @@ uint16_t srf02_get_distance(srf02_t *dev, srf02_mode_t mode)
char range_low_byte = 0;
uint16_t distance = 0;
/* Acquire exclusive access to the bus. */
i2c_acquire(dev->i2c);
/* initialize measure mode*/
status = i2c_write_reg(dev->i2c, dev->addr, SRF02_COMMAND_REG, mode);
/* Release the bus for other threads. */
i2c_release(dev->i2c);
if (status < 0) {
DEBUG("Write the ranging command to the i2c-interface is failed");
@ -63,8 +70,12 @@ uint16_t srf02_get_distance(srf02_t *dev, srf02_mode_t mode)
hwtimer_wait(70000);
/* Acquire exclusive access to the bus. */
i2c_acquire(dev->i2c);
status = i2c_read_reg(dev->i2c, dev->addr,
SRF02_RANGE_HIGH_BYTE, &range_high_byte);
/* Release the bus for other threads. */
i2c_release(dev->i2c);
if (status < 0) {
DEBUG("Read the high echo byte from the i2c-interface is failed");
@ -72,8 +83,12 @@ uint16_t srf02_get_distance(srf02_t *dev, srf02_mode_t mode)
return distance;
}
/* Acquire exclusive access to the bus. */
i2c_acquire(dev->i2c);
status = i2c_read_reg(dev->i2c, dev->addr,
SRF02_RANGE_LOW_BYTE, &range_low_byte);
/* Release the bus for other threads. */
i2c_release(dev->i2c);
if (status < 0) {
DEBUG("Read the low echo byte from the i2c-interface is failed");