From 4aeccf771463da1b4fdc2b9239570de33539a1bc Mon Sep 17 00:00:00 2001 From: Henry Bruce Date: Wed, 6 Jan 2016 15:29:12 -0800 Subject: [PATCH] si1132: Initial Implementation Silicon Labs SI1132 light sensor. Only visible light readings are currently supported. Signed-off-by: Henry Bruce Signed-off-by: Abhishek Malik --- src/si1132/CMakeLists.txt | 6 + src/si1132/javaupm_si1132.i | 17 +++ src/si1132/jsupm_si1132.i | 8 ++ src/si1132/pyupm_si1132.i | 11 ++ src/si1132/si1132.cxx | 231 ++++++++++++++++++++++++++++++++++++ src/si1132/si1132.h | 100 ++++++++++++++++ 6 files changed, 373 insertions(+) create mode 100644 src/si1132/CMakeLists.txt create mode 100644 src/si1132/javaupm_si1132.i create mode 100644 src/si1132/jsupm_si1132.i create mode 100644 src/si1132/pyupm_si1132.i create mode 100644 src/si1132/si1132.cxx create mode 100644 src/si1132/si1132.h diff --git a/src/si1132/CMakeLists.txt b/src/si1132/CMakeLists.txt new file mode 100644 index 00000000..0b13ba54 --- /dev/null +++ b/src/si1132/CMakeLists.txt @@ -0,0 +1,6 @@ +include_directories(..) +set (libname "si1132") +set (libdescription "upm Si1132") +set (module_src ${libname}.cxx) +set (module_h ${libname}.h) +upm_module_init() diff --git a/src/si1132/javaupm_si1132.i b/src/si1132/javaupm_si1132.i new file mode 100644 index 00000000..6b1c9bdd --- /dev/null +++ b/src/si1132/javaupm_si1132.i @@ -0,0 +1,17 @@ +%module(directors="1") javaupm_si1132 +%include "../upm.i" +%include "../upm/javaupm_interfaces.i" + +%{ + #include "si1132.h" +%} + +/* +%include "../upm/iModuleStatus.h" +%include "../upm/iLightSensor.h" + +%feature("director") IModuleStatus; +%feature("director") ILightSensor; +*/ + +%include "si1132.h" diff --git a/src/si1132/jsupm_si1132.i b/src/si1132/jsupm_si1132.i new file mode 100644 index 00000000..48f62a78 --- /dev/null +++ b/src/si1132/jsupm_si1132.i @@ -0,0 +1,8 @@ +%module jsupm_si1132 +%include "../upm.i" + +%{ + #include "si1132.h" +%} + +%include "si1132.h" diff --git a/src/si1132/pyupm_si1132.i b/src/si1132/pyupm_si1132.i new file mode 100644 index 00000000..495232fd --- /dev/null +++ b/src/si1132/pyupm_si1132.i @@ -0,0 +1,11 @@ +%module pyupm_si1132 +%include "../upm.i" + +%include "stdint.i" + +%feature("autodoc", "3"); + +%include "si1132.h" +%{ + #include "si1132.h" +%} diff --git a/src/si1132/si1132.cxx b/src/si1132/si1132.cxx new file mode 100644 index 00000000..d970c943 --- /dev/null +++ b/src/si1132/si1132.cxx @@ -0,0 +1,231 @@ +/* + * Author: Scott Ware + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include "si1132.h" + + +#define SI1132_ADDRESS 0x60 + +/* REGISTER ADDRESSES */ +#define SI1132_REG_PART_ID 0x00 +#define SI1132_REG_INT_CFG 0x03 +#define SI1132_REG_IRQ_ENABLE 0x04 +#define SI1132_REG_HW_KEY 0x07 +#define SI1132_REG_MEAS_RATE0 0x08 +#define SI1132_REG_MEAS_RATE1 0x09 +#define SI1132_REG_PARAM_WR 0x17 +#define SI1132_REG_COMMAND 0x18 +#define SI1132_REG_RESPONSE 0x20 +#define SI1132_REG_ALS_VIS_DATA0 0x22 +#define SI1132_REG_ALS_VIS_DATA1 0x23 +#define SI1132_REG_PARAM_RD 0x2E +#define SI1132_REG_CHIP_STAT 0x30 + +/* REGISTER VALUES */ +#define SI1132_PART_ID 0x32 +#define SI1132_HW_KEY_INIT 0x17 + + /* COMMAND VALUES */ +#define SI1132_COMMAND_RESET 0x01 +#define SI1132_COMMAND_ALS_FORCE 0x06 +#define SI1132_COMMAND_PARAM_QUERY 0x80 +#define SI1132_COMMAND_PARAM_SET 0xA0 +#define SI1132_COMMAND_ALS_AUTO 0x0E + +/* PARAMETER RAM ADDRESSES */ +#define SI1132_PARAM_CHLIST 0x01 +#define SI1132_PARAM_ALS_VIS_ADC_COUNT 0x10 +#define SI1132_PARAM_ALS_VIS_ADC_GAIN 0x11 +#define SI1132_PARAM_ALS_VIS_ADC_MISC 0x12 + +/* PARAMETER RAM VALUES */ +#define SI1132_PARAM_CHLIST_ENALSVIS 0x10 +#define SI1132_PARAM_ALS_VIS_RANGE_STD 0x00 +#define SI1132_PARAM_ALS_VIS_RANGE_HI 0x20 + + + +using namespace upm; + +SI1132::SI1132 (int bus) { + i2c = new mraa::I2c(bus); + i2c->address(SI1132_ADDRESS); + + // Reset chip to defaults + status = reset(); + if (!isConfigured()) + UPM_THROW("config failure"); +} + +SI1132::~SI1132() { + delete i2c; +} + +mraa::Result SI1132::reset() { + i2c->address(SI1132_ADDRESS); + + // Check version + uint8_t regValue = i2c->readReg(SI1132_REG_PART_ID); + if (regValue != SI1132_PART_ID) { + fprintf(stderr, "SI1132: Read ID failed. Data = %02x\n", regValue); + status = mraa::ERROR_UNSPECIFIED; + return status; + } + + // disable automatic updates + uint16_t rate = 0; + status = i2c->writeWordReg(SI1132_REG_MEAS_RATE0, rate); + if (status != mraa::SUCCESS) { + fprintf(stderr, "SI1132_REG_MEAS_RATE0 failed\n"); + return status; + } + + // reset device + status = i2c->writeReg(SI1132_REG_COMMAND, SI1132_COMMAND_RESET); + if (status != mraa::SUCCESS) { + fprintf(stderr, "SI1132: Reset failed.\n"); + status = mraa::ERROR_UNSPECIFIED; + return status; + } + sleepMs(30); + + // start state machine + i2c->writeReg(SI1132_REG_HW_KEY, SI1132_HW_KEY_INIT); + regValue = i2c->readReg(SI1132_REG_HW_KEY); + if (regValue != SI1132_HW_KEY_INIT) { + fprintf(stderr, "Si1132: Did not start\n"); + status = mraa::ERROR_UNSPECIFIED; + return status; + } + + status = writeParam(SI1132_PARAM_CHLIST, SI1132_PARAM_CHLIST_ENALSVIS); + + // set visible light range for indoor lighting + status = writeParam(SI1132_PARAM_ALS_VIS_ADC_MISC, SI1132_PARAM_ALS_VIS_RANGE_STD); + + // set visible light gain to 8 + status = writeParam(SI1132_PARAM_ALS_VIS_ADC_GAIN, 3); + status = writeParam(SI1132_PARAM_ALS_VIS_ADC_COUNT, 3 << 4); + return status; +} + + +uint16_t SI1132::getVisibleRaw() { + status = runCommand(SI1132_COMMAND_ALS_FORCE); + if (status != mraa::SUCCESS) + UPM_THROW("command failed"); + return i2c->readWordReg(SI1132_REG_ALS_VIS_DATA0); +} + + +double SI1132::getVisibleLux() { + uint16_t rawValue = getVisibleRaw(); + if (rawValue < 256) + rawValue = 0; + else + rawValue -= 256; + return static_cast(rawValue); +} + +bool SI1132::isConfigured() { + return status == mraa::SUCCESS; +} + + +mraa::Result SI1132::clearResponseRegister() +{ + uint8_t regValue = 0xFF; + status = i2c->writeReg(SI1132_REG_COMMAND, 0); + if (status != mraa::SUCCESS) + return status; + int bytesRead = i2c->readBytesReg(SI1132_REG_RESPONSE, ®Value, 1); + if (bytesRead == 1 && regValue == 0) + status = mraa::SUCCESS; + else + status = mraa::ERROR_UNSPECIFIED; + return status; +} + +mraa::Result SI1132::runCommand(uint8_t command) +{ + uint8_t response = 0; + int sleepTimeMs = 5; + int timeoutMs = 50; + int waitTimeMs = 0; + i2c->address(SI1132_ADDRESS); + status = clearResponseRegister(); + if (status != mraa::SUCCESS) + return status; + status = i2c->writeReg(SI1132_REG_COMMAND, command); + if (status != mraa::SUCCESS) + return status; + while (response == 0 && waitTimeMs < timeoutMs) { + response = i2c->readReg(SI1132_REG_RESPONSE); + sleepMs(sleepTimeMs); + waitTimeMs += sleepTimeMs; + } + if (response == 0) { + status = mraa::ERROR_UNSPECIFIED; + fprintf(stderr, "Comand %d failed\n", command); + } + return status; +} + +mraa::Result SI1132::writeParam(uint8_t param, uint8_t value) +{ + i2c->address(SI1132_ADDRESS); + status = i2c->writeReg(SI1132_REG_PARAM_WR, value); + if (status != mraa::SUCCESS) + return status; + return runCommand(SI1132_COMMAND_PARAM_SET | param); +} + +mraa::Result SI1132::readParam(uint8_t param, uint8_t* value) +{ + status = runCommand(SI1132_COMMAND_PARAM_QUERY | param); + if (status != mraa::SUCCESS) + return status; + if (i2c->readBytesReg(SI1132_REG_PARAM_RD, value, 1) != 1) + status = mraa::ERROR_UNSPECIFIED; + return status; +} + + +void SI1132::sleepMs(int mseconds) +{ + struct timespec sleepTime; + + sleepTime.tv_sec = mseconds / 1000; // Number of seconds + sleepTime.tv_nsec = ( mseconds % 1000 ) * 1000000; // Convert fractional seconds to nanoseconds + + // Iterate nanosleep in a loop until the total sleep time is the original + // value of the seconds parameter + while ( ( nanosleep( &sleepTime, &sleepTime ) != 0 ) && ( errno == EINTR ) ); +} diff --git a/src/si1132/si1132.h b/src/si1132/si1132.h new file mode 100644 index 00000000..ee26955d --- /dev/null +++ b/src/si1132/si1132.h @@ -0,0 +1,100 @@ +/* + * Author: Henry Bruce + * Copyright (c) 2015 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#pragma once + +#include +#include "mraa/i2c.hpp" +#include "upm/iLightSensor.h" + + +namespace upm { + +/** + * @brief Si1132 UV and visible light sensor library + * @defgroup Si1132 libupm-Si1132 + * @ingroup silabs i2c light ilightsensor + */ + +/** + * @brief API for Si1132 UV and Visible Light Sensor + * + * The Silicon Labs + * [Si1132](https://www.silabs.com/Support%20Documents/TechnicalDocs/Si1132.pdf) + * is a low-power, ultraviolet (UV) index, and ambient light sensor with I2C + * digital interface and programmable-event interrupt output. + * + * @library si1132 + * @sensor si1132 + * @comname Si1132 Light Sensor + * @altname Si1132 + * @type light + * @man silabs + * @con i2c + * @if ilightsensor + + */ +class SI1132 : public ILightSensor { + public: + /** + * Instanciates a Si1132 object + * + * @param bus number of used bus + * @param devAddr address of used i2c device + */ + SI1132 (int bus); + + /** + * Si1132 object destructor, basicaly it close i2c connection. + */ + ~SI1132 (); + + /** + * Read the raw visible light value + */ + uint16_t getVisibleRaw(); + + /** + * Read the lux value + */ + double getVisibleLux(); + + /** + * Returns whether the sensor is configured. + */ + bool isConfigured(); + const char* getModuleName() { return "si1132"; } + + private: + mraa::Result reset(); + mraa::Result clearResponseRegister(); + mraa::Result runCommand(uint8_t command); + mraa::Result readParam(uint8_t param, uint8_t* value); + mraa::Result writeParam(uint8_t param, uint8_t value); + void sleepMs(int mseconds); + + mraa::I2c* i2c; + mraa::Result status; +}; + +}