2014-10-15 11:29:41 +02:00
|
|
|
/*
|
2015-04-13 13:07:56 +02:00
|
|
|
* Copyright (C) 2013 Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
2015-09-04 18:45:32 +02:00
|
|
|
* 2015 Freie Universität Berlin
|
2014-10-15 11:29:41 +02:00
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
|
|
|
* Public License v2.1. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup driver_srf02
|
|
|
|
* @{
|
|
|
|
*
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2015-09-04 18:45:32 +02:00
|
|
|
* @brief Driver for the SRF02 ultrasonic range sensor
|
2014-10-15 11:29:41 +02:00
|
|
|
*
|
|
|
|
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
|
|
|
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
|
2015-09-04 18:45:32 +02:00
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2014-10-15 11:29:41 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2015-09-04 18:45:32 +02:00
|
|
|
|
|
|
|
#include "xtimer.h"
|
2014-10-15 11:29:41 +02:00
|
|
|
#include "srf02.h"
|
|
|
|
#include "periph/i2c.h"
|
|
|
|
|
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
2015-09-04 18:45:32 +02:00
|
|
|
/**
|
|
|
|
* @brief The datasheet tells us, that ranging takes 70ms
|
|
|
|
*/
|
|
|
|
#define RANGE_DELAY (70000U)
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2015-09-04 18:45:32 +02:00
|
|
|
/**
|
|
|
|
* @brief Per default use normal speed on the I2C bus
|
|
|
|
*/
|
|
|
|
#define BUS_SPEED (I2C_SPEED_NORMAL)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief SRF02 register addresses
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
#define REG_CMD (0x00)
|
|
|
|
#define REG_HIGH (0x02)
|
|
|
|
#define REG_LOW (0x03)
|
|
|
|
#define REG_AUTO_HIGH (0x04)
|
|
|
|
#define REG_AUTO_LOW (0x05)
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
int srf02_init(srf02_t *dev, i2c_t i2c, uint8_t addr)
|
2014-10-15 11:29:41 +02:00
|
|
|
{
|
|
|
|
dev->i2c = i2c;
|
|
|
|
dev->addr = addr;
|
2015-09-04 18:45:32 +02:00
|
|
|
char rev;
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2015-01-20 08:25:26 +01:00
|
|
|
/* Acquire exclusive access to the bus. */
|
|
|
|
i2c_acquire(dev->i2c);
|
2014-10-15 11:29:41 +02:00
|
|
|
/* initialize i2c interface */
|
2015-09-04 18:45:32 +02:00
|
|
|
if (i2c_init_master(dev->i2c, BUS_SPEED) < 0) {
|
|
|
|
DEBUG("[srf02] error initializing I2C bus\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* try to read the software revision (read the CMD reg) from the device */
|
|
|
|
i2c_read_reg(i2c, addr, REG_CMD, &rev);
|
|
|
|
if (rev == 0 || rev == 255) {
|
|
|
|
DEBUG("[srf02] error reading the devices software revision\n");
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
DEBUG("[srf02] software revision: 0x%02x\n", rev);
|
|
|
|
}
|
2015-01-20 08:25:26 +01:00
|
|
|
/* Release the bus for other threads. */
|
|
|
|
i2c_release(dev->i2c);
|
|
|
|
|
2015-09-04 18:45:32 +02:00
|
|
|
DEBUG("[srf02] initialization successful\n");
|
|
|
|
return 0;
|
2014-10-15 11:29:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t srf02_get_distance(srf02_t *dev, srf02_mode_t mode)
|
|
|
|
{
|
2015-09-04 18:45:32 +02:00
|
|
|
char res[2];
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2015-09-04 18:45:32 +02:00
|
|
|
/* trigger a new measurement by writing the mode to the CMD register */
|
|
|
|
DEBUG("[srf02] trigger new reading\n");
|
2015-01-20 08:25:26 +01:00
|
|
|
i2c_acquire(dev->i2c);
|
2015-09-04 18:45:32 +02:00
|
|
|
i2c_write_reg(dev->i2c, dev->addr, REG_CMD, mode);
|
2015-01-20 08:25:26 +01:00
|
|
|
i2c_release(dev->i2c);
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2015-09-04 18:45:32 +02:00
|
|
|
/* give the sensor the required time for sampling */
|
|
|
|
xtimer_usleep(RANGE_DELAY);
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2015-09-04 18:45:32 +02:00
|
|
|
/* read the results */
|
2015-01-20 08:25:26 +01:00
|
|
|
i2c_acquire(dev->i2c);
|
2015-09-04 18:45:32 +02:00
|
|
|
i2c_read_regs(dev->i2c, dev->addr, REG_HIGH, res, 2);
|
2015-01-20 08:25:26 +01:00
|
|
|
i2c_release(dev->i2c);
|
2015-09-04 18:45:32 +02:00
|
|
|
DEBUG("[srf02] result - high: 0x%02x low: 0x%02x\n", res[0], res[1]);
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2015-09-04 18:45:32 +02:00
|
|
|
/* compile result - TODO: fix for different host byte order other than LE */
|
|
|
|
return ((((uint16_t)res[0]) << 8) | (res[1] & 0xff));
|
2014-10-15 11:29:41 +02:00
|
|
|
}
|