41 #include "lib/sensors.h" 50 #include <ti/drivers/I2C.h> 59 #define PRINTF(...) printf(__VA_ARGS__) 69 #if BOARD_SENSORS_ENABLE 71 #ifndef Board_OPT3001_ADDR 72 #error "Board file doesn't define I2C address Board_OPT3001_ADDR" 75 #define OPT_3001_I2C_ADDRESS Board_OPT3001_ADDR 78 #define REG_RESULT 0x00 79 #define REG_CONFIGURATION 0x01 80 #define REG_LOW_LIMIT 0x02 81 #define REG_HIGH_LIMIT 0x03 83 #define REG_MANUFACTURER_ID 0x7E 84 #define REG_DEVICE_ID 0x7F 94 #define CFG_OVF 0x0001 95 #define CFG_CRF 0x8000 99 #define CFG_POL 0x0800 100 #define CFG_ME 0x0400 101 #define CFG_FC 0x0300 104 #define CFG_CT_100 0x0000 105 #define CFG_CT_800 CFG_CT 108 #define CFG_M_CONTI 0x0004 109 #define CFG_M_SINGLE 0x0002 110 #define CFG_M_SHUTDOWN 0x0000 113 #define CFG_RN_RESET 0x00C0 114 #define CFG_CT_RESET CFG_CT_800 115 #define CFG_L_RESET 0x1000 116 #define CFG_DEFAULTS (CFG_RN_RESET | CFG_CT_100 | CFG_L_RESET) 119 #define CFG_ENABLE_CONTINUOUS (CFG_M_CONTI | CFG_DEFAULTS) 120 #define CFG_ENABLE_SINGLE_SHOT (CFG_M_SINGLE | CFG_DEFAULTS) 121 #define CFG_DISABLE CFG_DEFAULTS 124 #define REGISTER_LENGTH 2 127 #define DATA_LENGTH 2 130 #define HI_UINT16(a) (((a) >> 8) & 0xFF) 131 #define LO_UINT16(a) (((a) >> 0) & 0xFF) 133 #define SWAP16(v) ((LO_UINT16(v) << 8) | (HI_UINT16(v) << 0)) 135 #define LSB16(v) (LO_UINT16(v)), (HI_UINT16(v)) 136 #define MSB16(v) (HI_UINT16(v)), (LO_UINT16(v)) 139 volatile OPT_3001_STATUS
status;
142 static OPT_3001_Object opt_3001;
145 #define SENSOR_STARTUP_DELAY (CLOCK_SECOND >> 3) 147 static struct ctimer startup_timer;
149 static I2C_Handle i2c_handle;
159 uint16_t data = (enable)
160 ? CFG_ENABLE_SINGLE_SHOT
166 opt_3001.status = OPT_3001_STATUS_I2C_ERROR;
171 uint8_t cfg_data[] = { REG_CONFIGURATION, LSB16(data) };
194 opt_3001.status = OPT_3001_STATUS_I2C_ERROR;
204 uint8_t cfg_data[] = { REG_CONFIGURATION };
205 uint16_t cfg_value = 0;
208 sizeof(cfg_data), &cfg_value,
211 opt_3001.status = OPT_3001_STATUS_I2C_ERROR;
217 opt_3001.status = OPT_3001_STATUS_DATA_READY;
218 sensors_changed(&opt_3001_sensor);
238 if(opt_3001.status != OPT_3001_STATUS_DATA_READY) {
239 return OPT_3001_READING_ERROR;
245 opt_3001.status = OPT_3001_STATUS_I2C_ERROR;
246 return OPT_3001_READING_ERROR;
249 uint8_t cfg_data[] = { REG_CONFIGURATION };
250 uint16_t cfg_value = 0;
253 sizeof(cfg_data), &cfg_value,
sizeof(cfg_value));
255 opt_3001.status = OPT_3001_STATUS_I2C_ERROR;
257 return OPT_3001_READING_ERROR;
260 uint8_t result_data[] = { REG_RESULT };
261 uint16_t result_value = 0;
264 sizeof(result_data), &result_value,
265 sizeof(result_value));
268 opt_3001.status = OPT_3001_STATUS_I2C_ERROR;
269 return OPT_3001_READING_ERROR;
272 result_value = SWAP16(result_value);
278 uint32_t m = (result_value & 0x0FFF) >> 0;
279 uint32_t e = (result_value & 0xF000) >> 12;
280 uint32_t converted = m * (1 << e);
282 PRINTF(
"OPT: %04X r=%d (centilux)\n", result_value,
285 return (
int)converted;
302 case SENSORS_HW_INIT:
303 opt_3001.status = OPT_3001_STATUS_STANDBY;
311 opt_3001.status = OPT_3001_STATUS_BOOTING;
336 return opt_3001.status;
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
static int value(int type)
Returns a reading from the sensor.
static void notify_ready_cb(void *unused)
Callback when sensor is ready to read data from.
static int status(int type)
Returns the status of the sensor.
static int configure(int type, int enable)
Configuration function for the OPT3001 sensor.
Header file for the Sensortag OPT-3001 light sensor.
Implementation of the I2C HAL driver for CC13xx/CC26xx.
bool i2c_arch_write_read(I2C_Handle i2c_handle, uint_least8_t slave_addr, void *wbuf, size_t wcount, void *rbuf, size_t rcount)
Setup and peform an I2C transaction.
static bool i2c_arch_write(I2C_Handle i2c_handle, uint_least8_t slave_addr, void *wbuf, size_t wcount)
Perform a write-only I2C transaction.
Header file for the callback timer
void i2c_arch_release(I2C_Handle i2c_handle)
Release the I2C Peripheral for other modules to use.
I2C_Handle i2c_arch_acquire(uint_least8_t index)
Open and lock the I2C Peripheral for use.
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
static bool sensor_enable(bool enable)
Turn the sensor on/off.
Header file with definitions related to the sensors on the Sensortags.
#define CFG_CRF
[7] Conversion Ready Field