mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
SRF08 driver, new handling of failed sensor initialization.
This commit is contained in:
parent
c7d35903e1
commit
e49318e856
@ -1,23 +1,16 @@
|
||||
/*
|
||||
<<<<<<< HEAD
|
||||
|
||||
* lm75a-temp-sensor.h - Definitions of the LM75A temperature sensor driver.
|
||||
*
|
||||
* Copyright (C) 2013 Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
||||
*
|
||||
* This source code is licensed under the LGPLv2 license,
|
||||
=======
|
||||
* lm75a-temp-sensor.h - Definitions of the LM75A temperature sensor driver for the LPC2387 chip.
|
||||
* Copyright (C) 2013 Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
||||
*
|
||||
* This source code is licensed under the LGPLv2 license,
|
||||
>>>>>>> 5953946... Driver for the LM75A Digital temperature sensor and thermal watchdog
|
||||
* See the file LICENSE for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @internal
|
||||
<<<<<<< HEAD
|
||||
* @brief Definitions of the LM75A temperature sensor driver.
|
||||
*
|
||||
* The connection between the LM75A and the MCU is based
|
||||
@ -28,16 +21,6 @@
|
||||
* @version $Revision: 3859 $
|
||||
*
|
||||
* @note $Id: lm75a-temp-sensor.h 3854 2013-09-2 15:35:21 kasmi $
|
||||
=======
|
||||
* @brief Definitions of the LM75A temperature sensor driver for the LPC2387 chip.
|
||||
* The connection between the LM75A and the LPC2387 chip is based on the I2C-interface.
|
||||
*
|
||||
* @author Freie Universität Berlin, Computer Systems & Telematics
|
||||
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
||||
* @version $Revision: 3854 $
|
||||
*
|
||||
* @note $Id: lm75a-temp-sensor.h 3854 2013-06-19 12:27:01Z zkasmi $
|
||||
>>>>>>> 5953946... Driver for the LM75A Digital temperature sensor and thermal watchdog
|
||||
*/
|
||||
|
||||
#ifndef LM75A_H_
|
||||
@ -48,7 +31,6 @@
|
||||
#include "i2c.h"
|
||||
|
||||
/* LM75A register addresses */
|
||||
<<<<<<< HEAD
|
||||
#define LM75A_ADDR 0x48
|
||||
#define LM75A_TEMPERATURE_REG 0x0
|
||||
#define LM75A_CONFIG_REG 0x1
|
||||
@ -99,63 +81,10 @@ enum FAULT_QUEUE_VALUES {
|
||||
LM75A_TWO_FAULT = 2,
|
||||
LM75A_FOUR_FAULT = 4,
|
||||
LM75A_SIX_FAULT = 6
|
||||
=======
|
||||
#define LM75A_ADDR 0x48
|
||||
#define LM75A_TEMPERATURE_REG 0x0
|
||||
#define LM75A_CONFIG_REG 0x1
|
||||
#define LM75A_THYST_REG 0x2
|
||||
#define LM75A_OVER_TEMP_REG 0x3
|
||||
|
||||
/* Define the used I2C Interface */
|
||||
//#define LM75A_I2C_INTERFACE I2C0 // P0.27 SDA0, P0.28 SCL0
|
||||
#define LM75A_I2C_INTERFACE I2C1_0 // P0.0 SDA1, P0.1 SCL1
|
||||
//#define LM75A_I2C_INTERFACE I2C1_1 // P0.19 SDA1, P0.20 SCL1
|
||||
//#define LM75A_I2C_INTERFACE I2C2 // P0.10 SDA2, P0.11 SCL2
|
||||
|
||||
/* LM75A operation modes */
|
||||
enum OPERATION_MODES {
|
||||
LM75A_NORMAL_OPERATION_MODE,
|
||||
LM75A_SHUTDOWN_MODE,
|
||||
LM75A_COMPARATOR_MODE,
|
||||
LM75A_INTERRUPT_MODE
|
||||
};
|
||||
|
||||
/*Common definitions for LMA75A */
|
||||
#define LM75A_BIT0 0x0
|
||||
#define LM75A_BIT1 0x1
|
||||
#define LM75A_BIT2 0x2
|
||||
#define LM75A_BIT3 0x3
|
||||
#define LM75A_BIT4 0x4
|
||||
#define LM75A_BIT5 0x5
|
||||
#define LM75A_BIT6 0x6
|
||||
#define LM75A_BIT7 0x7
|
||||
#define LM75A_BIT8 0x8
|
||||
#define LM75A_BIT9 0x9
|
||||
#define LM75A_BIT10 0xA
|
||||
#define LM75A_BIT15 0xF
|
||||
#define LM75A_MOST_SIG_BYTE_MASK 0xFF00
|
||||
#define LM75A_LEAST_SIG_BYTE_MASK 0x00FF
|
||||
#define LM75A_DATA_BITS_MASK 0x07FF
|
||||
#define LM75A_SIGN_BIT_MASK (1<<LM75A_BIT10)
|
||||
#define LM75A_LSB_MASK 0x1
|
||||
#define LM75A_EXTINT_MODE 0x1
|
||||
|
||||
/* LM75A configuration register */
|
||||
#define LM75A_ACTIVE_LOW 0
|
||||
#define LM75A_ACTIVE_HIGH 1
|
||||
#define LM75A_DEFAULT_CONFIG_VALUE 0
|
||||
|
||||
enum FAULT_QUEUE_VALUES {
|
||||
LM75A_ONE_FAULT = 1,
|
||||
LM75A_TWO_FAULT = 2,
|
||||
LM75A_FOUR_FAULT = 4,
|
||||
LM75A_SIX_FAULT = 6
|
||||
>>>>>>> 5953946... Driver for the LM75A Digital temperature sensor and thermal watchdog
|
||||
};
|
||||
|
||||
/* LM75A default values */
|
||||
enum DEFAULT_VALUES {
|
||||
<<<<<<< HEAD
|
||||
LM75A_DEFAULT_TOS = 80,
|
||||
LM75A_DEFAULT_THYST = 75,
|
||||
LM75A_DEFAULT_OPERATION = LM75A_NORMAL_OPERATION_MODE,
|
||||
@ -174,48 +103,21 @@ enum DEFAULT_VALUES {
|
||||
* @brief Set the over-temperature shutdown threshold (TOS).
|
||||
*
|
||||
* @param[in] tos the TOS value.
|
||||
=======
|
||||
LM75A_DEFAULT_TOS = 80,
|
||||
LM75A_DEFAULT_THYST = 75,
|
||||
LM75A_DEFAULT_OPERATION = LM75A_NORMAL_OPERATION_MODE,
|
||||
LM75A_DEFAULT_MODE = LM75A_COMPARATOR_MODE,
|
||||
LM75A_DEFAULT_POLARITY = LM75A_ACTIVE_LOW,
|
||||
LM75A_DEFAULT_FAULT_NUM = LM75A_ONE_FAULT
|
||||
};
|
||||
|
||||
/*define inter-threads messages */
|
||||
#define LM75A_EXIT_MSG 0
|
||||
#define LM75A_SAMPLING_MSG 1
|
||||
#define LM75A_SLEEP_MSG 2
|
||||
#define LM75A_WEAKUP_MSG 3
|
||||
|
||||
/**
|
||||
* @brief Set the over-temperature shutdown threshold (TOS).
|
||||
*
|
||||
* @param[in] tos the TOS value.
|
||||
>>>>>>> 5953946... Driver for the LM75A Digital temperature sensor and thermal watchdog
|
||||
*
|
||||
*/
|
||||
void lm75A_set_over_temperature(float_t tos);
|
||||
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* @brief Set the hysteresis temperature.
|
||||
*
|
||||
* @param[in] thsyt the hysteresis value.
|
||||
=======
|
||||
* @brief Set the hysteresis temperature.
|
||||
*
|
||||
* @param[in] thsyt the hysteresis value.
|
||||
>>>>>>> 5953946... Driver for the LM75A Digital temperature sensor and thermal watchdog
|
||||
*
|
||||
*/
|
||||
void lm75A_set_hysteresis_temperature(float_t thsyt);
|
||||
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* @brief Set various operation modes of the temperature sensor.
|
||||
* The LM75A provide four modes: normal, comparator, interrupt,
|
||||
* and the shutdown mode.
|
||||
@ -223,24 +125,13 @@ void lm75A_set_hysteresis_temperature(float_t thsyt);
|
||||
*
|
||||
* @param[in] op_mode the operation mode value: the normal, shutdown,
|
||||
* comparator, or interrupt mode.
|
||||
=======
|
||||
* @brief Set various operation modes of the temperature sensor.
|
||||
* The LM75A provide four modes: normal, comparator, interrupt, and the shutdown mode.
|
||||
* All these modes are defined in the lm75a-temp-sensor.h
|
||||
*
|
||||
* @param[in] op_mode the operation mode value: the normal, shutdown, comparator, or interrupt mode.
|
||||
>>>>>>> 5953946... Driver for the LM75A Digital temperature sensor and thermal watchdog
|
||||
*
|
||||
*/
|
||||
void lm75A_set_operation_mode(uint8_t op_mode);
|
||||
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* @brief Get the content of the configuration register.
|
||||
=======
|
||||
* @brief Get the content of the configuration register.
|
||||
>>>>>>> 5953946... Driver for the LM75A Digital temperature sensor and thermal watchdog
|
||||
*
|
||||
* @return the configuration register value.
|
||||
*
|
||||
@ -249,11 +140,7 @@ uint8_t lm75A_get_config_reg(void);
|
||||
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* @brief Get the adjusted hysteresis temperature.
|
||||
=======
|
||||
* @brief Get the adjusted hysteresis temperature.
|
||||
>>>>>>> 5953946... Driver for the LM75A Digital temperature sensor and thermal watchdog
|
||||
*
|
||||
* @return the content of the hysteresis register.
|
||||
*
|
||||
@ -262,11 +149,7 @@ float_t lm75A_get_hysteresis_temperature(void);
|
||||
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* @brief Get the adjusted over-temperature shutdown threshold (TOS).
|
||||
=======
|
||||
* @brief Get the adjusted over-temperature shutdown threshold (TOS).
|
||||
>>>>>>> 5953946... Driver for the LM75A Digital temperature sensor and thermal watchdog
|
||||
*
|
||||
* @return the content of the TOS-register.
|
||||
*
|
||||
@ -275,12 +158,8 @@ float_t lm75A_get_over_temperature(void);
|
||||
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* @brief Get the ambient temperature which is measured from the
|
||||
* LM75A sensor.
|
||||
=======
|
||||
* @brief Get the ambient temperature which is measured from the LM75A sensor.
|
||||
>>>>>>> 5953946... Driver for the LM75A Digital temperature sensor and thermal watchdog
|
||||
*
|
||||
* @return the content of the temperature register.
|
||||
*
|
||||
@ -289,22 +168,15 @@ float_t lm75A_get_ambient_temperature(void);
|
||||
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* @brief Set the LM75A sensor in the initial state.
|
||||
* The temperature sensor has the following values in this state:
|
||||
* config_register = 0; hyst_register = 75; the tos_reg = 80.
|
||||
=======
|
||||
* @brief Set the LM75A sensor in the initial state.
|
||||
* The temperature sensor has the following values in this state:
|
||||
* config_register = 0; hyst_register = 75; the tos_reg = 80.
|
||||
>>>>>>> 5953946... Driver for the LM75A Digital temperature sensor and thermal watchdog
|
||||
*
|
||||
*/
|
||||
void lm75A_reset(void);
|
||||
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* @brief Start a continuous sampling of the temperature values.
|
||||
* This function prints the values of all registers over
|
||||
* the rs232 interface.
|
||||
@ -315,20 +187,11 @@ void lm75A_reset(void);
|
||||
* subroutine is leaved. This parameter
|
||||
* is optional, the NULL-value can be
|
||||
* entered.
|
||||
=======
|
||||
* @brief Start a continuous sampling of the temperature values.
|
||||
* This function prints the values of all registers over the rs232 interface.
|
||||
*
|
||||
*@param[in] external_interr_handler pointer to an external task handler which is performed, if an external
|
||||
* interrupt is occurred and the external subroutine is leaved. This parameter
|
||||
* is optional, the NULL-value can be entered.
|
||||
>>>>>>> 5953946... Driver for the LM75A Digital temperature sensor and thermal watchdog
|
||||
*/
|
||||
void lm75A_start_sensor_sampling(void (*extern_interrupt_task)(void));
|
||||
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* @brief Register an interrupt handler for the external interrupt.
|
||||
* Only the port0 and port2 are supported.
|
||||
*
|
||||
@ -385,54 +248,6 @@ bool lm75A_external_interrupt_register(void *handler);
|
||||
* @brief Alarm the sensor sampling task about an external interrupt.
|
||||
*
|
||||
* @param[in] b is true if an external interrupt is occurred, otherwise false.
|
||||
=======
|
||||
* @brief Register an interrupt handler for the external interrupt.
|
||||
* Only the port0 and port2 are supported.
|
||||
*
|
||||
* @param[in] port port number.
|
||||
* @param[in] pin_bit_mask pin number in form of a bit mask: Pin0 --> BIT0, Pin1 --> BIT1, Pin2 --> BIT2 = 2^2 = 4
|
||||
* @param[in] flags define if the interrupt is generated on rising or falling edge (#GPIOINT_RISING_EDGE,
|
||||
* #GPIOINT_FALLING_EDGE).
|
||||
* @param[in] handler pointer to an interrupt handler.
|
||||
*
|
||||
* @return true if the the external interrupt handler is successfully registered, otherwise false.
|
||||
*/
|
||||
bool lm75A_ext_irq_handler_register(int32_t port, uint32_t pin_bit_mask,
|
||||
int32_t flags, void* handler);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Initialize the LM75A temperature sensor.
|
||||
* The baud rate and the handler for the external interrupt can be initialized. The external interrupt
|
||||
* handler is optional, if no handler is available, the NULL-value can be entered. The hysteresis and
|
||||
* the over-temperature are displayed before and after a rest action is performed. After this the LM7A
|
||||
* sensor is set in the interrupt or the comparator mode.
|
||||
*
|
||||
* @param[in] i2c_interface the i2c interface, several interfaces can be selected: i2c0, i2c1 and i2c2.
|
||||
* @param[in] baud_rate the baud rate.
|
||||
* @param[in] external_interr_handler pointer to a handler for the external interrupt.
|
||||
*
|
||||
* @return true if the I2C interface and the external interrupt handler are successfully initialized, otherwise false.
|
||||
*/
|
||||
bool lm75A_init(uint8_t i2c_interface, uint32_t baud_rate,
|
||||
void* external_interr_handler);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Register the external interrupt handler for the over-temperature shutdown output.
|
||||
*
|
||||
* @param[in] handler pointer to a handler for the external interrupts.
|
||||
*
|
||||
* @return true if the the external interrupt handler is successfully registered, otherwise false.
|
||||
*/
|
||||
bool lm75A_external_interrupt_register(void* handler);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Alarm the sensor sampling task about an external interrupt.
|
||||
*
|
||||
* @param[in] b is true if an external interrupt is occurred, otherwise false.
|
||||
>>>>>>> 5953946... Driver for the LM75A Digital temperature sensor and thermal watchdog
|
||||
*
|
||||
*/
|
||||
void lm75A_set_in_alarm(bool b);
|
||||
|
@ -92,6 +92,6 @@ uint32_t srf02_get_distance(uint8_t ranging_mode);
|
||||
* but without transmitting the burst.
|
||||
*
|
||||
*/
|
||||
void srf02_start_ranging(void);
|
||||
void srf02_start_ranging(uint16_t ranging_mode);
|
||||
|
||||
#endif /* SRF02_ULTRASONIC_SENSOR_I2C_H_ */
|
||||
|
@ -20,7 +20,7 @@
|
||||
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
||||
* @version $Revision: 3855 $
|
||||
*
|
||||
* @note $Id: lm75a-temp-sensor.c 3854 2013-09-04 13:56:37 kasmi $
|
||||
* @note $Id: lm75a-temp-sensor.c 3855 2013-09-05 13:53:49 kasmi $
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
@ -69,7 +69,6 @@ static uint16_t to_uint16(uint8_t *buff)
|
||||
//Read: Sensor --> MC
|
||||
volatile static float_t to_float(uint8_t reg_addr, uint16_t reg_value)
|
||||
{
|
||||
|
||||
uint16_t sign = reg_value & LM75A_SIGN_BIT_MASK;
|
||||
float_t f_temp = 0.0;
|
||||
float_t factor = 0.0;
|
||||
@ -90,9 +89,7 @@ volatile static float_t to_float(uint8_t reg_addr, uint16_t reg_value)
|
||||
}
|
||||
else { //the number is positive
|
||||
f_temp = reg_value * factor;
|
||||
|
||||
}
|
||||
|
||||
return f_temp;
|
||||
}
|
||||
|
||||
@ -137,12 +134,9 @@ static uint16_t lm75A_get_register_value(uint8_t i2c_interface,
|
||||
uint8_t rx_buff[reg_size];
|
||||
|
||||
i2c_clear_buffer(rx_buff, reg_size);
|
||||
|
||||
if ((reg_size > 0) && (reg_size < 3)) {
|
||||
|
||||
status = i2c_read(i2c_interface, LM75A_ADDR, reg_addr, rx_buff,
|
||||
reg_size);
|
||||
|
||||
if (!status) { //Slave is not ready
|
||||
puts(
|
||||
"[lm75a_tempSensorI2C/lm75A_getConfigReg]: Slave is not\
|
||||
@ -209,7 +203,6 @@ void lm75A_reset(void)
|
||||
lm75A_set_hysteresis_temperature(LM75A_DEFAULT_THYST);
|
||||
set_register(LM75A_I2C_INTERFACE, LM75A_CONFIG_REG,
|
||||
LM75A_DEFAULT_CONFIG_VALUE);
|
||||
|
||||
}
|
||||
|
||||
void lm75A_set_operation_mode(uint8_t op_mode)
|
||||
@ -236,7 +229,6 @@ void lm75A_set_operation_mode(uint8_t op_mode)
|
||||
default:
|
||||
config_reg &= ~(1 << LM75A_BIT0);
|
||||
}
|
||||
|
||||
set_register(LM75A_I2C_INTERFACE, LM75A_CONFIG_REG, config_reg);
|
||||
}
|
||||
|
||||
@ -254,7 +246,6 @@ bool lm75A_init(uint8_t i2c_interface, uint32_t baud_rate,
|
||||
puts("fatal error happened in i2c_initialize()\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
//i2c_enable_pull_up_resistor(i2c_interface);
|
||||
i2c_disable_pull_up_resistor(i2c_interface);
|
||||
|
||||
@ -321,11 +312,9 @@ void lm75A_start_sensor_sampling(void (*handler)(void))
|
||||
handler();
|
||||
my_alarm = false;
|
||||
}
|
||||
|
||||
hwtimer_wait(HWTIMER_TICKS(100000));
|
||||
LED_RED_TOGGLE;
|
||||
hwtimer_wait(HWTIMER_TICKS(100000));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
||||
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
||||
* @version $Revision: 3855 $
|
||||
*
|
||||
* @note $Id: srf02-ultrasonic-sensor.c 3857 2013-09-04 14:05:41 kasmi $
|
||||
* @note $Id: srf02-ultrasonic-sensor.c 3857 2013-09-04 17:25:41 kasmi $
|
||||
*
|
||||
*/
|
||||
|
||||
@ -59,39 +59,39 @@ uint32_t srf02_get_distance(uint8_t ranging_mode)
|
||||
|
||||
tx_buff[0] = ranging_mode;
|
||||
status = i2c_write(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
|
||||
SRF02_COMMAND_REG, tx_buff, reg_size);
|
||||
SRF02_COMMAND_REG, tx_buff, reg_size);
|
||||
|
||||
if (!status) {
|
||||
puts("Write the ranging command to the i2c-interface is failed");
|
||||
distance = UINT32_MAX;
|
||||
return distance;
|
||||
}
|
||||
if (!status) {
|
||||
puts("Write the ranging command to the i2c-interface is failed");
|
||||
distance = UINT32_MAX;
|
||||
return distance;
|
||||
}
|
||||
|
||||
hwtimer_wait(HWTIMER_TICKS(65000));
|
||||
status = i2c_read(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
|
||||
SRF02_RANGE_HIGH_BYTE, rx_buff, reg_size);
|
||||
if (!status) {
|
||||
puts("Read the distance from the i2c-interface is failed");
|
||||
distance = UINT32_MAX;
|
||||
return distance;
|
||||
}
|
||||
range_high_byte = rx_buff[0];
|
||||
hwtimer_wait(HWTIMER_TICKS(65000));
|
||||
status = i2c_read(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
|
||||
SRF02_RANGE_HIGH_BYTE, rx_buff, reg_size);
|
||||
|
||||
status = i2c_read(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
|
||||
SRF02_RANGE_LOW_BYTE, rx_buff, reg_size);
|
||||
range_low_byte = rx_buff[0];
|
||||
if (!status) {
|
||||
puts("Read the distance from the i2c-interface is failed");
|
||||
distance = UINT32_MAX;
|
||||
return distance;
|
||||
}
|
||||
range_high_byte = rx_buff[0];
|
||||
|
||||
distance = (range_high_byte << 8) | range_low_byte;
|
||||
//printf("%u | %u\n", range_high_byte, range_low_byte);
|
||||
return distance;
|
||||
status = i2c_read(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
|
||||
SRF02_RANGE_LOW_BYTE, rx_buff, reg_size);
|
||||
range_low_byte = rx_buff[0];
|
||||
|
||||
distance = (range_high_byte << 8) | range_low_byte;
|
||||
//printf("%u | %u\n", range_high_byte, range_low_byte);
|
||||
return distance;
|
||||
}
|
||||
|
||||
void srf02_start_ranging(uint16_t ranging_mode)
|
||||
{
|
||||
uint32_t distance = 0;
|
||||
uint32_t distance = 0;
|
||||
|
||||
while (1) {
|
||||
|
||||
distance = srf02_get_distance(ranging_mode);
|
||||
|
||||
if (distance != UINT32_MAX) {
|
||||
@ -118,7 +118,6 @@ void srf02_start_ranging(uint16_t ranging_mode)
|
||||
default:
|
||||
printf("distance = %lu cm\n", distance);
|
||||
}
|
||||
|
||||
hwtimer_wait(HWTIMER_TICKS(50000));
|
||||
}
|
||||
else {
|
||||
|
@ -15,9 +15,9 @@
|
||||
*
|
||||
* @author Freie Universität Berlin, Computer Systems & Telematics
|
||||
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
||||
* @version $Revision: 3854 $
|
||||
* @version $Revision: 3857 $
|
||||
*
|
||||
* @note $Id: srf08-ultrasonic-sensor.c 3857 2013-09-03 17:55:49 kasmi $
|
||||
* @note $Id: srf08-ultrasonic-sensor.c 3857 2013-09-05 14:45:13 kasmi $
|
||||
*
|
||||
*/
|
||||
|
||||
@ -32,10 +32,8 @@ bool srf08_init(uint8_t i2c_interface, uint32_t baud_rate)
|
||||
{
|
||||
if (i2c_initialize(i2c_interface, (uint32_t) I2CMASTER, 0, baud_rate, NULL)
|
||||
== false) { /* initialize I2C */
|
||||
puts("fatal error!happened in i2cInitialize() \n");
|
||||
|
||||
while (1)
|
||||
; /* Fatal error */
|
||||
puts("fatal error happened in i2c_initialize()\n");
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
i2c_enable_pull_up_resistor(i2c_interface);
|
||||
@ -47,6 +45,7 @@ bool srf08_init(uint8_t i2c_interface, uint32_t baud_rate)
|
||||
void srf08_set_range(uint8_t max_range)
|
||||
{
|
||||
uint8_t tx_buff[1];
|
||||
|
||||
tx_buff[0] = max_range;
|
||||
i2c_write(SRF08_I2C_INTERFACE, SRF08_DEFAULT_ADDR, SRF08_RANGE_REG, tx_buff,
|
||||
1);
|
||||
@ -55,6 +54,7 @@ void srf08_set_range(uint8_t max_range)
|
||||
void srf08_set_gain(uint8_t gain)
|
||||
{
|
||||
uint8_t tx_buff[1];
|
||||
|
||||
tx_buff[0] = gain;
|
||||
i2c_write(SRF08_I2C_INTERFACE, SRF08_DEFAULT_ADDR, SRF08_GAIN_REG, tx_buff,
|
||||
1);
|
||||
@ -63,18 +63,18 @@ void srf08_set_gain(uint8_t gain)
|
||||
uint8_t srf08_get_range(void)
|
||||
{
|
||||
uint8_t rx_buff[1];
|
||||
|
||||
i2c_read(SRF08_I2C_INTERFACE, SRF08_DEFAULT_ADDR, SRF08_RANGE_REG, rx_buff,
|
||||
1);
|
||||
|
||||
return rx_buff[0];
|
||||
}
|
||||
|
||||
uint8_t srf08_get_gain(void)
|
||||
{
|
||||
uint8_t rx_buff[1];
|
||||
|
||||
i2c_read(SRF08_I2C_INTERFACE, SRF08_DEFAULT_ADDR, SRF08_GAIN_REG, rx_buff,
|
||||
1);
|
||||
|
||||
return rx_buff[0];
|
||||
}
|
||||
|
||||
@ -90,48 +90,37 @@ int32_t srf08_get_distances(uint32_t *range_array, uint8_t ranging_mode)
|
||||
uint8_t tx_buff[reg_size];
|
||||
uint8_t register_location;
|
||||
uint8_t echo_number = 0;
|
||||
uint8_t echo_number = 0;
|
||||
|
||||
tx_buff[0] = ranging_mode;
|
||||
status = i2c_write(SRF08_I2C_INTERFACE, SRF08_DEFAULT_ADDR,
|
||||
SRF08_COMMAND_REG, tx_buff, reg_size);
|
||||
|
||||
if (!status) {
|
||||
puts("Write the ranging command to the i2c-interface is failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
hwtimer_wait(HWTIMER_TICKS(70000));
|
||||
|
||||
// Read all echo buffers
|
||||
for (register_location = 2; register_location < SRF08_MAX_REGISTER_NUMBER;
|
||||
register_location += 2) {
|
||||
|
||||
//read the high echo byte
|
||||
status = i2c_read(SRF08_I2C_INTERFACE, SRF08_DEFAULT_ADDR,
|
||||
register_location, rx_buff, reg_size);
|
||||
echo_number++;
|
||||
|
||||
if (!status) {
|
||||
puts("Read the the high echo byte from the i2c-interface is \
|
||||
failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
range_high_byte = rx_buff[0];
|
||||
|
||||
//read the low echo byte
|
||||
status = i2c_read(SRF08_I2C_INTERFACE, SRF08_DEFAULT_ADDR,
|
||||
register_location + 1, rx_buff, reg_size);
|
||||
|
||||
if (!status) {
|
||||
puts("Read the the low echo byte from the i2c-interface \
|
||||
is failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
range_low_byte = rx_buff[0];
|
||||
|
||||
if ((range_high_byte == 0) && (range_low_byte == 0)) {
|
||||
break;
|
||||
}
|
||||
@ -139,14 +128,11 @@ int32_t srf08_get_distances(uint32_t *range_array, uint8_t ranging_mode)
|
||||
distance = (range_high_byte << 8) | range_low_byte;
|
||||
range_array[(register_location - 2) / 2] = distance;
|
||||
echo_number++;
|
||||
|
||||
printf("distance = %4lu cm , echo%d\n",
|
||||
distance, register_location / 2);
|
||||
}
|
||||
|
||||
hwtimer_wait(HWTIMER_TICKS(500000));
|
||||
}
|
||||
|
||||
puts("--------------------------------------------");
|
||||
return echo_number;
|
||||
}
|
||||
@ -156,8 +142,8 @@ void srf08_start_ranging(uint8_t ranging_mode)
|
||||
uint32_t range_array[SRF08_MAX_ECHO_NUMBER];
|
||||
uint8_t i;
|
||||
uint8_t echo_number;
|
||||
puts("Ultrasonic SRF08 engine is started");
|
||||
|
||||
puts("Ultrasonic SRF08 engine is started");
|
||||
//wait due to calibration
|
||||
hwtimer_wait(HWTIMER_TICKS(700000));
|
||||
printf("Actual range = %d\n", srf08_get_range());
|
||||
@ -166,7 +152,6 @@ void srf08_start_ranging(uint8_t ranging_mode)
|
||||
while (1) {
|
||||
puts("--------------------------------------------");
|
||||
echo_number = srf08_get_distances(range_array, ranging_mode);
|
||||
|
||||
if (echo_number > 0) {
|
||||
for (i = 0; i < echo_number; i++) {
|
||||
printf("stored distance = %4lu cm , echo%d\n", range_array[i], i + 1);
|
||||
|
Loading…
Reference in New Issue
Block a user