41 #include "lib/sensors.h" 49 #include <ti/drivers/I2C.h> 58 #define PRINTF(...) printf(__VA_ARGS__) 68 #if BOARD_SENSORS_ENABLE 70 #ifndef Board_OPT3001_ADDR 71 #error "Board file doesn't define I2C address Board_OPT3001_ADDR" 74 #define OPT_3001_I2C_ADDRESS Board_OPT3001_ADDR 77 #define REG_RESULT 0x00 78 #define REG_CONFIGURATION 0x01 79 #define REG_LOW_LIMIT 0x02 80 #define REG_HIGH_LIMIT 0x03 82 #define REG_MANUFACTURER_ID 0x7E 83 #define REG_DEVICE_ID 0x7F 93 #define CFG_OVF 0x0001 94 #define CFG_CRF 0x8000 98 #define CFG_POL 0x0800 100 #define CFG_FC 0x0300 103 #define CFG_CT_100 0x0000 104 #define CFG_CT_800 CFG_CT 107 #define CFG_M_CONTI 0x0004 108 #define CFG_M_SINGLE 0x0002 109 #define CFG_M_SHUTDOWN 0x0000 112 #define CFG_RN_RESET 0x00C0 113 #define CFG_CT_RESET CFG_CT_800 114 #define CFG_L_RESET 0x1000 115 #define CFG_DEFAULTS (CFG_RN_RESET | CFG_CT_100 | CFG_L_RESET) 118 #define CFG_ENABLE_CONTINUOUS (CFG_M_CONTI | CFG_DEFAULTS) 119 #define CFG_ENABLE_SINGLE_SHOT (CFG_M_SINGLE | CFG_DEFAULTS) 120 #define CFG_DISABLE CFG_DEFAULTS 123 #define REGISTER_LENGTH 2 126 #define DATA_LENGTH 2 129 #define HI_UINT16(a) (((a) >> 8) & 0xFF) 130 #define LO_UINT16(a) (((a) >> 0) & 0xFF) 132 #define SWAP16(v) ((LO_UINT16(v) << 8) | (HI_UINT16(v) << 0)) 134 #define LSB16(v) (LO_UINT16(v)), (HI_UINT16(v)) 135 #define MSB16(v) (HI_UINT16(v)), (LO_UINT16(v)) 138 volatile OPT_3001_STATUS
status;
141 static OPT_3001_Object opt_3001;
144 #define SENSOR_STARTUP_DELAY (CLOCK_SECOND >> 3) 146 static struct ctimer startup_timer;
148 static I2C_Handle i2c_handle;
162 I2C_Transaction i2c_transaction = {
164 .writeCount = wcount,
167 .slaveAddress = OPT_3001_I2C_ADDRESS,
170 return I2C_transfer(i2c_handle, &i2c_transaction);
208 I2C_Params i2c_params;
209 I2C_Params_init(&i2c_params);
211 i2c_params.transferMode = I2C_MODE_BLOCKING;
212 i2c_params.bitRate = I2C_400kHz;
214 i2c_handle = I2C_open(Board_I2C0, &i2c_params);
215 if(i2c_handle == NULL) {
219 opt_3001.status = OPT_3001_STATUS_DISABLED;
231 uint16_t data = (enable)
232 ? CFG_ENABLE_SINGLE_SHOT
235 uint8_t cfg_data[] = { REG_CONFIGURATION, LSB16(data) };
236 return i2c_write(cfg_data,
sizeof(cfg_data));
254 uint8_t cfg_data[] = { REG_CONFIGURATION };
255 uint16_t cfg_value = 0;
257 bool spi_ok =
i2c_write_read(cfg_data,
sizeof(cfg_data), &cfg_value,
sizeof(cfg_value));
259 opt_3001.status = OPT_3001_STATUS_I2C_ERROR;
264 opt_3001.status = OPT_3001_STATUS_DATA_READY;
265 sensors_changed(&opt_3001_sensor);
282 if(opt_3001.status != OPT_3001_STATUS_DATA_READY) {
283 return OPT_3001_READING_ERROR;
286 uint8_t cfg_data[] = { REG_CONFIGURATION };
287 uint16_t cfg_value = 0;
289 bool spi_ok =
i2c_write_read(cfg_data,
sizeof(cfg_data), &cfg_value,
sizeof(cfg_value));
291 opt_3001.status = OPT_3001_STATUS_I2C_ERROR;
292 return OPT_3001_READING_ERROR;
295 uint8_t result_data[] = { REG_RESULT };
296 uint16_t result_value = 0;
298 spi_ok =
i2c_write_read(result_data,
sizeof(result_data), &result_value,
sizeof(result_value));
300 opt_3001.status = OPT_3001_STATUS_I2C_ERROR;
301 return OPT_3001_READING_ERROR;
304 result_value = SWAP16(result_value);
306 uint32_t e = (result_value & 0x0FFF) >> 0;
307 uint32_t m = (result_value & 0xF000) >> 12;
308 uint32_t converted = m * 100 * (1 << e);
310 PRINTF(
"OPT: %04X r=%d (centilux)\n", result_value,
313 return (
int)converted;
330 case SENSORS_HW_INIT:
332 opt_3001.status = OPT_3001_STATUS_STANDBY;
334 opt_3001.status = OPT_3001_STATUS_DISABLED;
335 rv = OPT_3001_READING_ERROR;
344 opt_3001.status = OPT_3001_STATUS_BOOTING;
369 return opt_3001.status;
static bool i2c_write_read(void *wbuf, size_t wcount, void *rbuf, size_t rcount)
Setup and peform an I2C transaction.
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
static int value(int type)
Returns a reading from the sensor.
static bool i2c_read(void *rbuf, size_t rcount)
Peform a read only I2C transaction.
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.
Header file for the callback timer
static bool i2c_write(void *wbuf, size_t wcount)
Peform a write only I2C transaction.
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.
static bool sensor_init(void)
Initialize the OPT-3001 sensor driver.
Header file with definitions related to the sensors on the Sensortags.
#define CFG_CRF
[7] Conversion Ready Field