42 #include "lib/sensors.h" 49 #include <ti/drivers/I2C.h> 58 #define PRINTF(...) printf(__VA_ARGS__) 68 #if BOARD_SENSORS_ENABLE 70 #ifndef Board_HDC1000_ADDR 71 #error "Board file doesn't define the I2C address Board_HDC1000_ADDR" 74 #define HDC1000_I2C_ADDRESS Board_HDC1000_ADDR 77 #define HDC1000_REG_TEMP 0x00 78 #define HDC1000_REG_HUM 0x01 79 #define HDC1000_REG_CONFIG 0x02 80 #define HDC1000_REG_SERID_H 0xFB 81 #define HDC1000_REG_SERID_M 0xFC 82 #define HDC1000_REG_SERID_L 0xFD 83 #define HDC1000_REG_MANF_ID 0xFE 84 #define HDC1000_REG_DEV_ID 0xFF 87 #define HDC1000_VAL_MANF_ID 0x5449 88 #define HDC1000_VAL_DEV_ID 0x1000 89 #define HDC1000_VAL_CONFIG 0x1000 92 #define HI_UINT16(a) (((a) >> 8) & 0xFF) 93 #define LO_UINT16(a) ((a) & 0xFF) 95 #define SWAP16(v) ((LO_UINT16(v) << 8) | HI_UINT16(v)) 97 #define LSB16(v) (LO_UINT16(v)), (HI_UINT16(v)) 99 static I2C_Handle i2c_handle;
105 } HDC_1000_SensorData;
107 static HDC_1000_SensorData sensor_data;
116 #define MEASUREMENT_DURATION 2 122 #define SENSOR_STARTUP_DELAY 3 124 static struct ctimer startup_timer;
138 I2C_Transaction i2c_transaction = {
140 .writeCount = wcount,
143 .slaveAddress = HDC1000_I2C_ADDRESS,
146 return I2C_transfer(i2c_handle, &i2c_transaction);
184 I2C_Params i2c_params;
185 I2C_Params_init(&i2c_params);
187 i2c_params.transferMode = I2C_MODE_BLOCKING;
188 i2c_params.bitRate = I2C_400kHz;
190 i2c_handle = I2C_open(Board_I2C0, &i2c_params);
191 if(i2c_handle == NULL) {
196 uint8_t config_data[] = { HDC1000_REG_CONFIG, LSB16(HDC1000_VAL_CONFIG) };
198 return i2c_write(config_data,
sizeof(config_data));
208 uint8_t temp_reg[] = { HDC1000_REG_TEMP };
210 return i2c_write(temp_reg,
sizeof(temp_reg));
221 int32_t raw_temp = SWAP16(sensor_data.temp);
222 int32_t raw_hum = SWAP16(sensor_data.hum);
225 *temp = raw_temp * 100 * 165 / 65536 - 40000;
227 *hum = raw_hum * 100 * 100 / 65536;
240 if(
i2c_read(&sensor_data,
sizeof(sensor_data))) {
246 sensors_changed(&hdc_1000_sensor);
261 PRINTF(
"Sensor disabled or starting up (%d)\n", sensor_status);
262 return HDC_1000_READING_ERROR;
266 case HDC_1000_SENSOR_TYPE_TEMP:
267 case HDC_1000_SENSOR_TYPE_HUMID:
269 PRINTF(
"HDC: t=%d h=%d\n", (
int)temp, (
int)hum);
271 if(type == HDC_1000_SENSOR_TYPE_TEMP) {
273 }
else if(type == HDC_1000_SENSOR_TYPE_HUMID) {
276 return HDC_1000_READING_ERROR;
280 PRINTF(
"Invalid type\n");
281 return HDC_1000_READING_ERROR;
297 case SENSORS_HW_INIT:
298 memset(&sensor_data, 0,
sizeof(sensor_data));
329 return sensor_status;
343 return sensor_status;
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
static bool start(void)
Start measurement.
static int configure(int type, int enable)
Configuration function for the HDC1000 sensor.
Header file for the Sensortag HDC1000 sensor.
static int status(int type)
Returns the status of the sensor.
static bool i2c_read(void *rbuf, size_t rcount)
Peform a read only I2C transaction.
Header file for the callback timer
static bool i2c_write_read(void *wbuf, size_t wcount, void *rbuf, size_t rcount)
Setup and peform an I2C transaction.
static void convert(int32_t *temp, int32_t *hum)
Convert raw data to temperature and humidity.
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
static int value(int type)
Returns a reading from the sensor.
static bool i2c_write(void *wbuf, size_t wcount)
Peform a write only I2C transaction.
static bool sensor_init(void)
Initialize the HDC-1000 sensor driver.
static void notify_ready(void *unused)
Callback when sensor is ready to read data from.
Header file with definitions related to the sensors on the Sensortags.