SAILAPIGuide  1_20_00_02
Macros | Functions | Variables
opt3001.c File Reference
#include <ti/drivers/GPIO.h>
#include <ti/drivers/I2C.h>
#include <ti/sail/opt3001/opt3001.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>

Macros

#define LUX_BASE_RES   00.01F
 
#define RANGE_BASE   00040.95F
 
#define RANGE_MAX   83865.60F
 
#define RANGE_BITS   0xF000
 
#define READ_BUF_SIZE   2
 
#define WRITE_BUF_SIZE   3
 
#define CONVERSION_MODE   0xC000
 

Functions

bool OPT3001_close (OPT3001_Handle handle)
 Function to close a OPT3001 sensor specified by the OPT3001 handle. More...
 
bool OPT3001_disableInterrupt (OPT3001_Handle handle)
 Function to disable the GPIO interrupt. More...
 
bool OPT3001_enableInterrupt (OPT3001_Handle handle)
 Enable interrupts from the specified OPT3001. More...
 
bool OPT3001_getLux (OPT3001_Handle handle, float *data)
 Function to get the sensor's lux reading. More...
 
bool OPT3001_getLuxLimits (OPT3001_Handle handle, float *high, float *low)
 Function to get the sensor's lux limits. More...
 
void OPT3001_init ()
 Function to initialize OPT3001 driver. More...
 
OPT3001_Handle OPT3001_open (unsigned int index, I2C_Handle i2cHandle, OPT3001_Params *params)
 Function to open a given OPT3001 sensor. More...
 
void OPT3001_Params_init (OPT3001_Params *params)
 Function to initialize a OPT3001_Params struct to its defaults. More...
 
bool OPT3001_readRegister (OPT3001_Handle handle, uint16_t *data, uint8_t registerAddress)
 Read the specified register from a OPT3001 sensor. More...
 
bool OPT3001_setLuxLimits (OPT3001_Handle handle, float high, float low)
 Function to get the sensor's lux limits. More...
 
bool OPT3001_setRange (OPT3001_Handle handle, OPT3001_FullRange range)
 Set the lux measurement result range. More...
 
bool OPT3001_setConversionMode (OPT3001_Handle handle, OPT3001_ConversionMode mode)
 Function to change the conversion mode. More...
 
bool OPT3001_writeRegister (OPT3001_Handle handle, uint16_t data, uint8_t registerAddress)
 Write the specified data to a OPT3001 sensor. More...
 

Variables

const OPT3001_Params OPT3001_defaultParams
 
OPT3001_Config OPT3001_config []
 

Macro Definition Documentation

§ LUX_BASE_RES

#define LUX_BASE_RES   00.01F

Referenced by OPT3001_setLuxLimits().

§ RANGE_BASE

#define RANGE_BASE   00040.95F

Referenced by OPT3001_setLuxLimits().

§ RANGE_MAX

#define RANGE_MAX   83865.60F

§ RANGE_BITS

#define RANGE_BITS   0xF000

Referenced by OPT3001_setRange().

§ READ_BUF_SIZE

#define READ_BUF_SIZE   2

Referenced by OPT3001_readRegister().

§ WRITE_BUF_SIZE

#define WRITE_BUF_SIZE   3

Referenced by OPT3001_writeRegister().

§ CONVERSION_MODE

#define CONVERSION_MODE   0xC000

Function Documentation

§ OPT3001_close()

bool OPT3001_close ( OPT3001_Handle  handle)

Function to close a OPT3001 sensor specified by the OPT3001 handle.

The OPT3001 hardware will be placed in a low power state in which ADC conversions are disabled. If the pin is configured to interrupt, the GPIO pin interrupts will be disabled. The I2C handle is not affected.

Precondition
OPT3001_open() had to be called first.
Parameters
handleA OPT3001_Handle returned from OPT3001_open()
Returns
true on success or false upon failure.

References OPT3001_Object::callback, OPT3001_Object::i2cHandle, OPT3001_Config::object, OPT3001_disableInterrupt(), OPT3001_setConversionMode(), OPT3001_setLuxLimits(), OPT3001_SHUTDOWN, and RANGE_MAX.

§ OPT3001_disableInterrupt()

bool OPT3001_disableInterrupt ( OPT3001_Handle  handle)

Function to disable the GPIO interrupt.

Interrupts on the OPT3001 specific GPIO index will be disabled. The lux limits set are unaffected.

See also
OPT3001_enableInterrupt()
Precondition
Had to call OPT3001_enableInterrupt() first for this function to have an effect.
Parameters
handleA OPT3001_Handle
Returns
true on success or false upon failure.

References OPT3001_Config::hwAttrs, and OPT3001_Config::object.

Referenced by OPT3001_close().

§ OPT3001_enableInterrupt()

bool OPT3001_enableInterrupt ( OPT3001_Handle  handle)

Enable interrupts from the specified OPT3001.

Interrupts on the OPT3001 specific GPIO index will be enabled.

See also
OPT3001_disableInterrupt()
Precondition
OPT3001_setLuxLimits() must have been called first to recieve interrupts from the OPT3001 INT pin.
Parameters
handleA OPT3001_Handle
Returns
true on success or false upon failure.

References OPT3001_Config::hwAttrs, OPT3001_Config::object, OPT3001_CONFIG, and OPT3001_readRegister().

§ OPT3001_getLux()

bool OPT3001_getLux ( OPT3001_Handle  handle,
float *  data 
)

Function to get the sensor's lux reading.

Parameters
handleA OPT3001_Handle
dataA pointer to a float to store the lux value.
Returns
true on success, false on failure

References OPT3001_readRegister(), and OPT3001_RESULT.

§ OPT3001_getLuxLimits()

bool OPT3001_getLuxLimits ( OPT3001_Handle  handle,
float *  high,
float *  low 
)

Function to get the sensor's lux limits.

Parameters
handleA OPT3001_Handle
highA pointer to a float to store the high limit
lowA pointer to a float to store the low limit
Returns
true on success, false on failure

References OPT3001_HILIMIT, and OPT3001_readRegister().

§ OPT3001_init()

void OPT3001_init ( )

Function to initialize OPT3001 driver.

This function will initialize the OPT3001 driver.

References OPT3001_Config::object.

§ OPT3001_open()

OPT3001_Handle OPT3001_open ( unsigned int  index,
I2C_Handle  i2cHandle,
OPT3001_Params params 
)

Function to open a given OPT3001 sensor.

Function to initialize a given OPT3001 sensor specified by the particular index value. This function must be called from a task context. If one intends to use the INT pin, a callback function must be specified in the OPT3001_Params structure. Additionally, a gpioIndex must be setup and specified in the OPT3001_HWAttrs structure.

The I2C controller must be operating in BLOCKING mode. Failure to ensure the I2C_Handle is in BLOCKING mode will result in undefined behavior.

The user should ensure that each sensor has its own slaveAddress, gpioIndex (if alarming) and OPT3001Index.

Precondition
OPT3001_init() has to be called first
Parameters
OPT3001IndexLogical sensor number for the OPT3001 indexed into the OPT3001_config table
i2cHandleAn I2C_Handle opened in BLOCKING mode
paramsA pointer to OPT3001_Params structure. If NULL, it will use default values.
Returns
A OPT3001_Handle on success, or a NULL on failure.
See also
OPT3001_init()
OPT3001_Params_init()
OPT3001_close()

References OPT3001_Object::callback, OPT3001_Params::callback, OPT3001_Params::conversionMode, OPT3001_Params::conversionReady, OPT3001_Params::conversionTime, OPT3001_Params::faultCount, OPT3001_HWAttrs::gpioIndex, OPT3001_Object::i2cHandle, OPT3001_Params::interruptMode, OPT3001_CONFIG, OPT3001_LOLIMIT, OPT3001_writeRegister(), and OPT3001_Params::range.

§ OPT3001_Params_init()

void OPT3001_Params_init ( OPT3001_Params params)

Function to initialize a OPT3001_Params struct to its defaults.

Parameters
paramsA pointer to OPT3001_Params structure for initialization.

Default values are:

 conversionMode    = OPT3001_CONTINUOUS
 conversionReady   = OPT3001_CONVRD_DIS
 conversionTime    = OPT3001_800MS
 faultCount        = OPT3001_FAULT1
 range             = OPT3001_AUTO
 interruptMode     = OPT3001_LATCH
 callback          = NULL

References OPT3001_defaultParams.

§ OPT3001_readRegister()

bool OPT3001_readRegister ( OPT3001_Handle  handle,
uint16_t *  data,
uint8_t  registerAddress 
)

Read the specified register from a OPT3001 sensor.

Parameters
handleA OPT3001_Handle
registerAddressRegister address
dataA pointer to a data register in which received data will be written to. Must be 16 bits.
Returns
true on success or false upon failure.

References OPT3001_Config::hwAttrs, and READ_BUF_SIZE.

Referenced by OPT3001_enableInterrupt(), OPT3001_getLux(), OPT3001_getLuxLimits(), OPT3001_setConversionMode(), and OPT3001_setRange().

§ OPT3001_setLuxLimits()

bool OPT3001_setLuxLimits ( OPT3001_Handle  handle,
float  high,
float  low 
)

Function to get the sensor's lux limits.

Parameters
handleA OPT3001_Handle
highA float specifying the high limit. Use OPT3001_IGNORE if high limit is not desired.
lowA float specifying the low limit. Use OPT3001_IGNORE if a low limit is not desired.
Returns
true on success, false on failure

References LUX_BASE_RES, OPT3001_HILIMIT, OPT3001_IGNORE, OPT3001_LOLIMIT, OPT3001_writeRegister(), RANGE_BASE, and RANGE_MAX.

Referenced by OPT3001_close().

§ OPT3001_setRange()

bool OPT3001_setRange ( OPT3001_Handle  handle,
OPT3001_FullRange  range 
)

Set the lux measurement result range.

Parameters
handleA OPT3001_Handle
OPT3001_FullRangeA range value specifying max possible result
Returns
true on success or false upon failure.

References OPT3001_CONFIG, OPT3001_readRegister(), OPT3001_writeRegister(), and RANGE_BITS.

§ OPT3001_setConversionMode()

bool OPT3001_setConversionMode ( OPT3001_Handle  handle,
OPT3001_ConversionMode  mode 
)

Function to change the conversion mode.

This function will set the OPT3001 hardware to operate in the conversion mode specified by OPT3001_ConversionMode.

Precondition
OPT3001_open has to be called first.
Parameters
handleA OPT3001_Handle
modeA OPT3001_ConversionMode specifying the mode to operate in.
Returns
true on success or false upon failure.

References OPT3001_CONFIG, OPT3001_readRegister(), OPT3001_SINGLESHOT, and OPT3001_writeRegister().

Referenced by OPT3001_close().

§ OPT3001_writeRegister()

bool OPT3001_writeRegister ( OPT3001_Handle  handle,
uint16_t  data,
uint8_t  registerAddress 
)

Write the specified data to a OPT3001 sensor.

This function is not thread safe. When writing to a handle, it is possible to overwrite data written by another task.

For example: Task A and B are writing to the configuration register. Task A has a higher priority than Task B. Task B is running and reads the configuration register. Task A then preempts Task B and reads the configuration register, performs a logical OR and writes to the configuration register. Task B then resumes execution and performs its logical OR and writes to the configuration register–overwriting the data written by Task A.

Such instances can be prevented through the use of Semaphores. Below is an example which utilizes an initialized Semaphore_handle, OPT3001Lock.

if (0 == sem_wait(&OPT3001Lock))
{
//Perform read/write operations
sem_post(OPT3001Lock);
}
else
{
//handle error scenario
}
Parameters
handleA OPT3001_Handle
dataA uint16_t to be written to the OPT3001 sensor
registerAddressOPT3001 register address
Returns
true on success or false upon failure.

References OPT3001_Config::hwAttrs, and WRITE_BUF_SIZE.

Referenced by OPT3001_open(), OPT3001_setConversionMode(), OPT3001_setLuxLimits(), and OPT3001_setRange().

Variable Documentation

§ OPT3001_defaultParams

const OPT3001_Params OPT3001_defaultParams
Initial value:
= {
NULL
}
Definition: opt3001.h:341
Definition: opt3001.h:241
Definition: opt3001.h:322
Definition: opt3001.h:284
Definition: opt3001.h:271
Definition: opt3001.h:255

Referenced by OPT3001_Params_init().

§ OPT3001_config

OPT3001_Config OPT3001_config[]
Copyright 2018, Texas Instruments Incorporated