Forked so that I can make it take I2C as a parameter; on a bus with other I2C things.

Fork of Si1133 by Silicon Labs

Committer:
stevew817
Date:
Sun Nov 12 20:18:04 2017 +0100
Revision:
2:1e2dd643afa8
Parent:
0:667132a19341
Cosmetic changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stevew817 0:667132a19341 1 /***************************************************************************//**
stevew817 0:667132a19341 2 * @file Si1133.h
stevew817 0:667132a19341 3 *******************************************************************************
stevew817 0:667132a19341 4 * @section License
stevew817 0:667132a19341 5 * <b>(C) Copyright 2017 Silicon Labs, http://www.silabs.com</b>
stevew817 0:667132a19341 6 *******************************************************************************
stevew817 0:667132a19341 7 *
stevew817 0:667132a19341 8 * SPDX-License-Identifier: Apache-2.0
stevew817 0:667132a19341 9 *
stevew817 0:667132a19341 10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
stevew817 0:667132a19341 11 * not use this file except in compliance with the License.
stevew817 0:667132a19341 12 * You may obtain a copy of the License at
stevew817 0:667132a19341 13 *
stevew817 0:667132a19341 14 * http://www.apache.org/licenses/LICENSE-2.0
stevew817 0:667132a19341 15 *
stevew817 0:667132a19341 16 * Unless required by applicable law or agreed to in writing, software
stevew817 0:667132a19341 17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
stevew817 0:667132a19341 18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
stevew817 0:667132a19341 19 * See the License for the specific language governing permissions and
stevew817 0:667132a19341 20 * limitations under the License.
stevew817 0:667132a19341 21 *
stevew817 0:667132a19341 22 ******************************************************************************/
stevew817 0:667132a19341 23
stevew817 0:667132a19341 24 #ifndef SI1133_H
stevew817 0:667132a19341 25 #define SI1133_H
stevew817 0:667132a19341 26
stevew817 0:667132a19341 27 #include "mbed.h"
stevew817 0:667132a19341 28
stevew817 0:667132a19341 29 /** Si1133 class.
stevew817 0:667132a19341 30 * Used for taking Light level and UV index measurements.
stevew817 0:667132a19341 31 *
stevew817 0:667132a19341 32 * Example:
stevew817 0:667132a19341 33 * @code
stevew817 0:667132a19341 34 * #include "mbed.h"
stevew817 0:667132a19341 35 * #include "Si1133.h"
stevew817 0:667132a19341 36 *
stevew817 0:667132a19341 37 * //Create an Si1133 object
stevew817 0:667132a19341 38 * Si1133 sensor(PC4, PC5);
stevew817 0:667132a19341 39 *
stevew817 0:667132a19341 40 * int main()
stevew817 0:667132a19341 41 * {
stevew817 0:667132a19341 42 * //Try to open the Si1133
stevew817 0:667132a19341 43 * if (sensor.open()) {
stevew817 0:667132a19341 44 * printf("Device detected!\n");
stevew817 0:667132a19341 45 *
stevew817 0:667132a19341 46 * while (1) {
stevew817 0:667132a19341 47 * //Print the current light level
stevew817 0:667132a19341 48 * printf("Lux = %.3f\n", (float)sensor.get_light_level());
stevew817 0:667132a19341 49 * //Print the current UV index
stevew817 0:667132a19341 50 * printf("UV index = %.3f\n", (float)sensor.get_uv_index());
stevew817 0:667132a19341 51 *
stevew817 0:667132a19341 52 * //Sleep for 0.5 seconds
stevew817 0:667132a19341 53 * wait(0.5);
stevew817 0:667132a19341 54 * }
stevew817 0:667132a19341 55 * } else {
stevew817 0:667132a19341 56 * error("Device not detected!\n");
stevew817 0:667132a19341 57 * }
stevew817 0:667132a19341 58 * }
stevew817 0:667132a19341 59 * @endcode
stevew817 0:667132a19341 60 */
stevew817 0:667132a19341 61 class Si1133
stevew817 0:667132a19341 62 {
stevew817 0:667132a19341 63 public:
stevew817 0:667132a19341 64
stevew817 0:667132a19341 65 /** Create an Si1133 object connected to the specified I2C pins with the specified I2C slave address
stevew817 0:667132a19341 66 *
stevew817 0:667132a19341 67 * @param sda The I2C data pin.
stevew817 0:667132a19341 68 * @param scl The I2C clock pin.
stevew817 0:667132a19341 69 * @param hz The I2C bus frequency (defaults to 400kHz).
stevew817 0:667132a19341 70 */
stevew817 0:667132a19341 71 Si1133(PinName sda, PinName scl, int hz = 400000);
stevew817 0:667132a19341 72
stevew817 0:667132a19341 73 /**
stevew817 0:667132a19341 74 * Si1133 destructor
stevew817 0:667132a19341 75 */
stevew817 0:667132a19341 76 ~Si1133(void);
stevew817 0:667132a19341 77
stevew817 0:667132a19341 78 /** Probe for the Si1133 and try to initialize the sensor
stevew817 0:667132a19341 79 *
stevew817 0:667132a19341 80 * @returns
stevew817 0:667132a19341 81 * 'true' if the device exists on the bus,
stevew817 0:667132a19341 82 * 'false' if the device doesn't exist on the bus.
stevew817 0:667132a19341 83 */
stevew817 0:667132a19341 84 bool open();
stevew817 0:667132a19341 85
stevew817 0:667132a19341 86 /** Measure the current light level (in lux) on the Si1133
stevew817 0:667132a19341 87 *
stevew817 0:667132a19341 88 * @returns The current temperature measurement in Lux.
stevew817 0:667132a19341 89 */
stevew817 0:667132a19341 90 float get_light_level();
stevew817 0:667132a19341 91
stevew817 0:667132a19341 92 /** Measure the current UV Index on the Si1133
stevew817 0:667132a19341 93 *
stevew817 0:667132a19341 94 * @returns The current UV index measurement.
stevew817 0:667132a19341 95 */
stevew817 0:667132a19341 96 float get_uv_index();
stevew817 0:667132a19341 97
stevew817 0:667132a19341 98 /** Do a combined measurement and return both the light level and UV index
stevew817 0:667132a19341 99 *
stevew817 0:667132a19341 100 * @param[out] light_level Measured light level in Lux
stevew817 0:667132a19341 101 * @param[out] uv_index Measured UV index
stevew817 0:667132a19341 102 *
stevew817 0:667132a19341 103 * @returns true if measurement was successful
stevew817 0:667132a19341 104 */
stevew817 0:667132a19341 105 bool get_light_and_uv(float *light_level, float *uv_index);
stevew817 0:667132a19341 106
stevew817 0:667132a19341 107 #ifdef MBED_OPERATORS
stevew817 0:667132a19341 108 /** A shorthand for get_light_level()
stevew817 0:667132a19341 109 *
stevew817 0:667132a19341 110 * @returns The current temperature measurement in Lux.
stevew817 0:667132a19341 111 */
stevew817 0:667132a19341 112 operator float();
stevew817 0:667132a19341 113 #endif
stevew817 0:667132a19341 114
stevew817 0:667132a19341 115 private:
stevew817 0:667132a19341 116 /**
stevew817 0:667132a19341 117 * @name I2C Registers
stevew817 0:667132a19341 118 * @{
stevew817 0:667132a19341 119 */
stevew817 0:667132a19341 120 enum Register {
stevew817 0:667132a19341 121 REG_PART_ID = 0x00, /**< Part ID */
stevew817 0:667132a19341 122 REG_HW_ID = 0x01, /**< Hardware ID */
stevew817 0:667132a19341 123 REG_REV_ID = 0x02, /**< Hardware revision */
stevew817 0:667132a19341 124 REG_HOSTIN0 = 0x0A, /**< Data for parameter table on PARAM_SET write to COMMAND register */
stevew817 0:667132a19341 125 REG_COMMAND = 0x0B, /**< Initiated action in Sensor when specific codes written here */
stevew817 0:667132a19341 126 REG_IRQ_ENABLE = 0x0F, /**< Interrupt enable */
stevew817 0:667132a19341 127 REG_RESPONSE1 = 0x10, /**< Contains the readback value from a query or a set command */
stevew817 0:667132a19341 128 REG_RESPONSE0 = 0x11, /**< Chip state and error status */
stevew817 0:667132a19341 129 REG_IRQ_STATUS = 0x12, /**< Interrupt status */
stevew817 0:667132a19341 130 REG_HOSTOUT0 = 0x13, /**< Captured Sensor Data */
stevew817 0:667132a19341 131 REG_HOSTOUT1 = 0x14, /**< Captured Sensor Data */
stevew817 0:667132a19341 132 REG_HOSTOUT2 = 0x15, /**< Captured Sensor Data */
stevew817 0:667132a19341 133 REG_HOSTOUT3 = 0x16, /**< Captured Sensor Data */
stevew817 0:667132a19341 134 REG_HOSTOUT4 = 0x17, /**< Captured Sensor Data */
stevew817 0:667132a19341 135 REG_HOSTOUT5 = 0x18, /**< Captured Sensor Data */
stevew817 0:667132a19341 136 REG_HOSTOUT6 = 0x19, /**< Captured Sensor Data */
stevew817 0:667132a19341 137 REG_HOSTOUT7 = 0x1A, /**< Captured Sensor Data */
stevew817 0:667132a19341 138 REG_HOSTOUT8 = 0x1B, /**< Captured Sensor Data */
stevew817 0:667132a19341 139 REG_HOSTOUT9 = 0x1C, /**< Captured Sensor Data */
stevew817 0:667132a19341 140 REG_HOSTOUT10 = 0x1D, /**< Captured Sensor Data */
stevew817 0:667132a19341 141 REG_HOSTOUT11 = 0x1E, /**< Captured Sensor Data */
stevew817 0:667132a19341 142 REG_HOSTOUT12 = 0x1F, /**< Captured Sensor Data */
stevew817 0:667132a19341 143 REG_HOSTOUT13 = 0x20, /**< Captured Sensor Data */
stevew817 0:667132a19341 144 REG_HOSTOUT14 = 0x21, /**< Captured Sensor Data */
stevew817 0:667132a19341 145 REG_HOSTOUT15 = 0x22, /**< Captured Sensor Data */
stevew817 0:667132a19341 146 REG_HOSTOUT16 = 0x23, /**< Captured Sensor Data */
stevew817 0:667132a19341 147 REG_HOSTOUT17 = 0x24, /**< Captured Sensor Data */
stevew817 0:667132a19341 148 REG_HOSTOUT18 = 0x25, /**< Captured Sensor Data */
stevew817 0:667132a19341 149 REG_HOSTOUT19 = 0x26, /**< Captured Sensor Data */
stevew817 0:667132a19341 150 REG_HOSTOUT20 = 0x27, /**< Captured Sensor Data */
stevew817 0:667132a19341 151 REG_HOSTOUT21 = 0x28, /**< Captured Sensor Data */
stevew817 0:667132a19341 152 REG_HOSTOUT22 = 0x29, /**< Captured Sensor Data */
stevew817 0:667132a19341 153 REG_HOSTOUT23 = 0x2A, /**< Captured Sensor Data */
stevew817 0:667132a19341 154 REG_HOSTOUT24 = 0x2B, /**< Captured Sensor Data */
stevew817 0:667132a19341 155 REG_HOSTOUT25 = 0x2C, /**< Captured Sensor Data */
stevew817 0:667132a19341 156 };
stevew817 0:667132a19341 157 /**@}*/
stevew817 0:667132a19341 158
stevew817 0:667132a19341 159 /**
stevew817 0:667132a19341 160 * @name Parameters
stevew817 0:667132a19341 161 * @{
stevew817 0:667132a19341 162 */
stevew817 0:667132a19341 163 enum Parameter {
stevew817 0:667132a19341 164 PARAM_I2C_ADDR = 0x00, /**< I2C address */
stevew817 0:667132a19341 165 PARAM_CH_LIST = 0x01, /**< Channel list */
stevew817 0:667132a19341 166 PARAM_ADCCONFIG0 = 0x02, /**< ADC config for Channel 0 */
stevew817 0:667132a19341 167 PARAM_ADCSENS0 = 0x03, /**< ADC sensitivity setting for Channel 0 */
stevew817 0:667132a19341 168 PARAM_ADCPOST0 = 0x04, /**< ADC resolution, shift and threshold settings for Channel 0 */
stevew817 0:667132a19341 169 PARAM_MEASCONFIG0 = 0x05, /**< ADC measurement counter selection for Channel 0 */
stevew817 0:667132a19341 170 PARAM_ADCCONFIG1 = 0x06, /**< ADC config for Channel 1 */
stevew817 0:667132a19341 171 PARAM_ADCSENS1 = 0x07, /**< ADC sensitivity setting for Channel 1 */
stevew817 0:667132a19341 172 PARAM_ADCPOST1 = 0x08, /**< ADC resolution, shift and threshold settings for Channel 1 */
stevew817 0:667132a19341 173 PARAM_MEASCONFIG1 = 0x09, /**< ADC measurement counter selection for Channel 1 */
stevew817 0:667132a19341 174 PARAM_ADCCONFIG2 = 0x0A, /**< ADC config for Channel 2 */
stevew817 0:667132a19341 175 PARAM_ADCSENS2 = 0x0B, /**< ADC sensitivity setting for Channel 2 */
stevew817 0:667132a19341 176 PARAM_ADCPOST2 = 0x0C, /**< ADC resolution, shift and threshold settings for Channel 2 */
stevew817 0:667132a19341 177 PARAM_MEASCONFIG2 = 0x0D, /**< ADC measurement counter selection for Channel 2 */
stevew817 0:667132a19341 178 PARAM_ADCCONFIG3 = 0x0E, /**< ADC config for Channel 3 */
stevew817 0:667132a19341 179 PARAM_ADCSENS3 = 0x0F, /**< ADC sensitivity setting for Channel 3 */
stevew817 0:667132a19341 180 PARAM_ADCPOST3 = 0x10, /**< ADC resolution, shift and threshold settings for Channel 3 */
stevew817 0:667132a19341 181 PARAM_MEASCONFIG3 = 0x11, /**< ADC measurement counter selection for Channel 3 */
stevew817 0:667132a19341 182 PARAM_ADCCONFIG4 = 0x12, /**< ADC config for Channel 4 */
stevew817 0:667132a19341 183 PARAM_ADCSENS4 = 0x13, /**< ADC sensitivity setting for Channel 4 */
stevew817 0:667132a19341 184 PARAM_ADCPOST4 = 0x14, /**< ADC resolution, shift and threshold settings for Channel 4 */
stevew817 0:667132a19341 185 PARAM_MEASCONFIG4 = 0x15, /**< ADC measurement counter selection for Channel 4 */
stevew817 0:667132a19341 186 PARAM_ADCCONFIG5 = 0x16, /**< ADC config for Channel 5 */
stevew817 0:667132a19341 187 PARAM_ADCSENS5 = 0x17, /**< ADC sensitivity setting for Channel 5 */
stevew817 0:667132a19341 188 PARAM_ADCPOST5 = 0x18, /**< ADC resolution, shift and threshold settings for Channel 5 */
stevew817 0:667132a19341 189 PARAM_MEASCONFIG5 = 0x19, /**< ADC measurement counter selection for Channel 5 */
stevew817 0:667132a19341 190 PARAM_MEASRATE_H = 0x1A, /**< Main measurement rate counter MSB */
stevew817 0:667132a19341 191 PARAM_MEASRATE_L = 0x1B, /**< Main measurement rate counter LSB */
stevew817 0:667132a19341 192 PARAM_MEASCOUNT0 = 0x1C, /**< Measurement rate extension counter 0 */
stevew817 0:667132a19341 193 PARAM_MEASCOUNT1 = 0x1D, /**< Measurement rate extension counter 1 */
stevew817 0:667132a19341 194 PARAM_MEASCOUNT2 = 0x1E, /**< Measurement rate extension counter 2 */
stevew817 0:667132a19341 195 PARAM_THRESHOLD0_H = 0x25, /**< Threshold level 0 MSB */
stevew817 0:667132a19341 196 PARAM_THRESHOLD0_L = 0x26, /**< Threshold level 0 LSB */
stevew817 0:667132a19341 197 PARAM_THRESHOLD1_H = 0x27, /**< Threshold level 1 MSB */
stevew817 0:667132a19341 198 PARAM_THRESHOLD1_L = 0x28, /**< Threshold level 1 LSB */
stevew817 0:667132a19341 199 PARAM_THRESHOLD2_H = 0x29, /**< Threshold level 2 MSB */
stevew817 0:667132a19341 200 PARAM_THRESHOLD2_L = 0x2A, /**< Threshold level 2 LSB */
stevew817 0:667132a19341 201 PARAM_BURST = 0x2B, /**< Burst enable and burst count */
stevew817 0:667132a19341 202 };
stevew817 0:667132a19341 203 /**@}*/
stevew817 0:667132a19341 204
stevew817 0:667132a19341 205 /**
stevew817 0:667132a19341 206 * @name Commands
stevew817 0:667132a19341 207 * @{
stevew817 0:667132a19341 208 */
stevew817 0:667132a19341 209 enum Command {
stevew817 0:667132a19341 210 CMD_RESET_CMD_CTR = 0x00, /**< Resets the command counter */
stevew817 0:667132a19341 211 CMD_RESET = 0x01, /**< Forces a Reset */
stevew817 0:667132a19341 212 CMD_NEW_ADDR = 0x02, /**< Stores the new I2C address */
stevew817 0:667132a19341 213 CMD_FORCE_CH = 0x11, /**< Initiates a set of measurements specified in CHAN_LIST parameter */
stevew817 0:667132a19341 214 CMD_PAUSE_CH = 0x12, /**< Pauses autonomous measurements */
stevew817 0:667132a19341 215 CMD_START = 0x13, /**< Starts autonomous measurements */
stevew817 0:667132a19341 216 CMD_PARAM_SET = 0x80, /**< Sets a parameter */
stevew817 0:667132a19341 217 CMD_PARAM_QUERY = 0x40, /**< Reads a parameter */
stevew817 0:667132a19341 218 };
stevew817 0:667132a19341 219 /**@}*/
stevew817 0:667132a19341 220
stevew817 0:667132a19341 221 /**
stevew817 0:667132a19341 222 * @name Responses
stevew817 0:667132a19341 223 * @{
stevew817 0:667132a19341 224 */
stevew817 0:667132a19341 225 enum Response {
stevew817 0:667132a19341 226 RSP0_CHIPSTAT_MASK = 0xE0, /**< Chip state mask in Response0 register */
stevew817 0:667132a19341 227 RSP0_COUNTER_MASK = 0x1F, /**< Command counter and error indicator mask in Response0 register */
stevew817 0:667132a19341 228 RSP0_SLEEP = 0x20, /**< Sleep state indicator bit mask in Response0 register */
stevew817 0:667132a19341 229 };
stevew817 0:667132a19341 230 /**@}*/
stevew817 0:667132a19341 231
stevew817 0:667132a19341 232 /**
stevew817 0:667132a19341 233 * @brief
stevew817 0:667132a19341 234 * Structure to store the data measured by the Si1133
stevew817 0:667132a19341 235 */
stevew817 0:667132a19341 236 typedef struct {
stevew817 0:667132a19341 237 uint8_t irq_status; /**< Interrupt status of the device */
stevew817 0:667132a19341 238 int32_t ch0; /**< Channel 0 measurement data */
stevew817 0:667132a19341 239 int32_t ch1; /**< Channel 1 measurement data */
stevew817 0:667132a19341 240 int32_t ch2; /**< Channel 2 measurement data */
stevew817 0:667132a19341 241 int32_t ch3; /**< Channel 3 measurement data */
stevew817 0:667132a19341 242 } Samples_t;
stevew817 0:667132a19341 243
stevew817 0:667132a19341 244 /**
stevew817 0:667132a19341 245 * @brief
stevew817 0:667132a19341 246 * Structure to store the calculation coefficients
stevew817 0:667132a19341 247 */
stevew817 0:667132a19341 248 typedef struct {
stevew817 0:667132a19341 249 int16_t info; /**< Info */
stevew817 0:667132a19341 250 uint16_t mag; /**< Magnitude */
stevew817 0:667132a19341 251 } Coeff_t;
stevew817 0:667132a19341 252
stevew817 0:667132a19341 253 /**
stevew817 0:667132a19341 254 * @brief
stevew817 0:667132a19341 255 * Structure to store the coefficients used for Lux calculation
stevew817 0:667132a19341 256 */
stevew817 0:667132a19341 257 typedef struct {
stevew817 0:667132a19341 258 Coeff_t coeff_high[4]; /**< High amplitude coeffs */
stevew817 0:667132a19341 259 Coeff_t coeff_low[9]; /**< Low amplitude coeffs */
stevew817 0:667132a19341 260 } LuxCoeff_t;
stevew817 0:667132a19341 261
stevew817 0:667132a19341 262 /* Forward-declare constant coefficient table */
stevew817 0:667132a19341 263 static const LuxCoeff_t lk;
stevew817 0:667132a19341 264 static const Coeff_t uk[];
stevew817 0:667132a19341 265
stevew817 0:667132a19341 266 /* Private functions */
stevew817 0:667132a19341 267 uint32_t read_register(enum Register reg, uint8_t *data);
stevew817 0:667132a19341 268 uint32_t write_register(enum Register reg, uint8_t data);
stevew817 0:667132a19341 269 uint32_t write_register_block(enum Register reg, uint8_t length, uint8_t *data);
stevew817 0:667132a19341 270 uint32_t read_register_block(enum Register reg, uint8_t length, uint8_t *data);
stevew817 0:667132a19341 271 uint32_t get_irq_status(uint8_t *irq_status);
stevew817 0:667132a19341 272 uint32_t wait_until_sleep(void);
stevew817 0:667132a19341 273 uint32_t reset(void);
stevew817 0:667132a19341 274 uint32_t reset_cmd_counter (void);
stevew817 0:667132a19341 275 uint32_t send_cmd(enum Command command);
stevew817 0:667132a19341 276 uint32_t force_measurement (void);
stevew817 0:667132a19341 277 uint32_t pause_measurement (void);
stevew817 0:667132a19341 278 uint32_t start_measurement (void);
stevew817 0:667132a19341 279 uint32_t set_parameter (enum Parameter address, uint8_t value);
stevew817 0:667132a19341 280 uint32_t read_parameter (enum Parameter address);
stevew817 0:667132a19341 281 uint32_t init (void);
stevew817 0:667132a19341 282 uint32_t deinit (void);
stevew817 2:1e2dd643afa8 283 uint32_t measure (Samples_t *samples);
stevew817 2:1e2dd643afa8 284 int32_t get_uv (int32_t uv);
stevew817 2:1e2dd643afa8 285 int32_t get_lux (int32_t vis_high, int32_t vis_low, int32_t ir);
stevew817 2:1e2dd643afa8 286 uint32_t measure_lux_uv (float *lux, float *uvi);
stevew817 2:1e2dd643afa8 287 uint32_t get_measurement (float *lux, float *uvi);
stevew817 2:1e2dd643afa8 288 uint32_t get_hardware_id (uint8_t *hardware_id);
stevew817 0:667132a19341 289
stevew817 0:667132a19341 290 int32_t calculate_polynomial_helper (int32_t input, int8_t fraction, uint16_t mag, int8_t shift);
stevew817 0:667132a19341 291 int32_t calculate_polynomial (int32_t x, int32_t y, uint8_t input_fraction, uint8_t output_fraction, uint8_t num_coeff, const Coeff_t *kp);
stevew817 0:667132a19341 292
stevew817 0:667132a19341 293 /* Member variables */
stevew817 0:667132a19341 294 I2C m_I2C;
stevew817 0:667132a19341 295 };
stevew817 0:667132a19341 296
stevew817 0:667132a19341 297 #endif