Program test for Coragem
Dependencies: SX1272 SPI_MX25R
Si1133.h
00001 /***************************************************************************//** 00002 * @file Si1133.h 00003 ******************************************************************************* 00004 * @section License 00005 * <b>(C) Copyright 2017 Silicon Labs, http://www.silabs.com</b> 00006 ******************************************************************************* 00007 * 00008 * SPDX-License-Identifier: Apache-2.0 00009 * 00010 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00011 * not use this file except in compliance with the License. 00012 * You may obtain a copy of the License at 00013 * 00014 * http://www.apache.org/licenses/LICENSE-2.0 00015 * 00016 * Unless required by applicable law or agreed to in writing, software 00017 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00018 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 * See the License for the specific language governing permissions and 00020 * limitations under the License. 00021 * 00022 ******************************************************************************/ 00023 00024 #ifndef SI1133_H 00025 #define SI1133_H 00026 00027 #include "mbed.h" 00028 00029 /** Si1133 class. 00030 * Used for taking Light level and UV index measurements. 00031 * 00032 * Example: 00033 * @code 00034 * #include "mbed.h" 00035 * #include "Si1133.h" 00036 * 00037 * //Create an Si1133 object 00038 * Si1133 sensor(PC4, PC5); 00039 * 00040 * int main() 00041 * { 00042 * //Try to open the Si1133 00043 * if (sensor.open()) { 00044 * printf("Device detected!\n"); 00045 * 00046 * while (1) { 00047 * //Print the current light level 00048 * printf("Lux = %.3f\n", (float)sensor.get_light_level()); 00049 * //Print the current UV index 00050 * printf("UV index = %.3f\n", (float)sensor.get_uv_index()); 00051 * 00052 * //Sleep for 0.5 seconds 00053 * wait(0.5); 00054 * } 00055 * } else { 00056 * error("Device not detected!\n"); 00057 * } 00058 * } 00059 * @endcode 00060 */ 00061 class Si1133 00062 { 00063 public: 00064 00065 /** Create an Si1133 object connected to the specified I2C pins with the specified I2C slave address 00066 * 00067 * @param sda The I2C data pin. 00068 * @param scl The I2C clock pin. 00069 * @param hz The I2C bus frequency (defaults to 400kHz). 00070 */ 00071 Si1133(PinName sda, PinName scl, int hz = 400000); 00072 00073 /** 00074 * Si1133 destructor 00075 */ 00076 ~Si1133(void); 00077 00078 /** Probe for the Si1133 and try to initialize the sensor 00079 * 00080 * @returns 00081 * 'true' if the device exists on the bus, 00082 * 'false' if the device doesn't exist on the bus. 00083 */ 00084 bool open(); 00085 00086 /** Measure the current light level (in lux) on the Si1133 00087 * 00088 * @returns The current temperature measurement in Lux. 00089 */ 00090 float get_light_level(); 00091 00092 /** Measure the current UV Index on the Si1133 00093 * 00094 * @returns The current UV index measurement. 00095 */ 00096 float get_uv_index(); 00097 00098 /** Do a combined measurement and return both the light level and UV index 00099 * 00100 * @param[out] light_level Measured light level in Lux 00101 * @param[out] uv_index Measured UV index 00102 * 00103 * @returns true if measurement was successful 00104 */ 00105 bool get_light_and_uv(float *light_level, float *uv_index); 00106 00107 uint32_t wait_until_sleep(void); 00108 uint32_t wake(void); 00109 00110 #ifdef MBED_OPERATORS 00111 /** A shorthand for get_light_level() 00112 * 00113 * @returns The current temperature measurement in Lux. 00114 */ 00115 operator float(); 00116 #endif 00117 00118 private: 00119 /** 00120 * @name I2C Registers 00121 * @{ 00122 */ 00123 enum Register { 00124 REG_PART_ID = 0x00, /**< Part ID */ 00125 REG_HW_ID = 0x01, /**< Hardware ID */ 00126 REG_REV_ID = 0x02, /**< Hardware revision */ 00127 REG_HOSTIN0 = 0x0A, /**< Data for parameter table on PARAM_SET write to COMMAND register */ 00128 REG_COMMAND = 0x0B, /**< Initiated action in Sensor when specific codes written here */ 00129 REG_IRQ_ENABLE = 0x0F, /**< Interrupt enable */ 00130 REG_RESPONSE1 = 0x10, /**< Contains the readback value from a query or a set command */ 00131 REG_RESPONSE0 = 0x11, /**< Chip state and error status */ 00132 REG_IRQ_STATUS = 0x12, /**< Interrupt status */ 00133 REG_HOSTOUT0 = 0x13, /**< Captured Sensor Data */ 00134 REG_HOSTOUT1 = 0x14, /**< Captured Sensor Data */ 00135 REG_HOSTOUT2 = 0x15, /**< Captured Sensor Data */ 00136 REG_HOSTOUT3 = 0x16, /**< Captured Sensor Data */ 00137 REG_HOSTOUT4 = 0x17, /**< Captured Sensor Data */ 00138 REG_HOSTOUT5 = 0x18, /**< Captured Sensor Data */ 00139 REG_HOSTOUT6 = 0x19, /**< Captured Sensor Data */ 00140 REG_HOSTOUT7 = 0x1A, /**< Captured Sensor Data */ 00141 REG_HOSTOUT8 = 0x1B, /**< Captured Sensor Data */ 00142 REG_HOSTOUT9 = 0x1C, /**< Captured Sensor Data */ 00143 REG_HOSTOUT10 = 0x1D, /**< Captured Sensor Data */ 00144 REG_HOSTOUT11 = 0x1E, /**< Captured Sensor Data */ 00145 REG_HOSTOUT12 = 0x1F, /**< Captured Sensor Data */ 00146 REG_HOSTOUT13 = 0x20, /**< Captured Sensor Data */ 00147 REG_HOSTOUT14 = 0x21, /**< Captured Sensor Data */ 00148 REG_HOSTOUT15 = 0x22, /**< Captured Sensor Data */ 00149 REG_HOSTOUT16 = 0x23, /**< Captured Sensor Data */ 00150 REG_HOSTOUT17 = 0x24, /**< Captured Sensor Data */ 00151 REG_HOSTOUT18 = 0x25, /**< Captured Sensor Data */ 00152 REG_HOSTOUT19 = 0x26, /**< Captured Sensor Data */ 00153 REG_HOSTOUT20 = 0x27, /**< Captured Sensor Data */ 00154 REG_HOSTOUT21 = 0x28, /**< Captured Sensor Data */ 00155 REG_HOSTOUT22 = 0x29, /**< Captured Sensor Data */ 00156 REG_HOSTOUT23 = 0x2A, /**< Captured Sensor Data */ 00157 REG_HOSTOUT24 = 0x2B, /**< Captured Sensor Data */ 00158 REG_HOSTOUT25 = 0x2C, /**< Captured Sensor Data */ 00159 }; 00160 /**@}*/ 00161 00162 /** 00163 * @name Parameters 00164 * @{ 00165 */ 00166 enum Parameter { 00167 PARAM_I2C_ADDR = 0x00, /**< I2C address */ 00168 PARAM_CH_LIST = 0x01, /**< Channel list */ 00169 PARAM_ADCCONFIG0 = 0x02, /**< ADC config for Channel 0 */ 00170 PARAM_ADCSENS0 = 0x03, /**< ADC sensitivity setting for Channel 0 */ 00171 PARAM_ADCPOST0 = 0x04, /**< ADC resolution, shift and threshold settings for Channel 0 */ 00172 PARAM_MEASCONFIG0 = 0x05, /**< ADC measurement counter selection for Channel 0 */ 00173 PARAM_ADCCONFIG1 = 0x06, /**< ADC config for Channel 1 */ 00174 PARAM_ADCSENS1 = 0x07, /**< ADC sensitivity setting for Channel 1 */ 00175 PARAM_ADCPOST1 = 0x08, /**< ADC resolution, shift and threshold settings for Channel 1 */ 00176 PARAM_MEASCONFIG1 = 0x09, /**< ADC measurement counter selection for Channel 1 */ 00177 PARAM_ADCCONFIG2 = 0x0A, /**< ADC config for Channel 2 */ 00178 PARAM_ADCSENS2 = 0x0B, /**< ADC sensitivity setting for Channel 2 */ 00179 PARAM_ADCPOST2 = 0x0C, /**< ADC resolution, shift and threshold settings for Channel 2 */ 00180 PARAM_MEASCONFIG2 = 0x0D, /**< ADC measurement counter selection for Channel 2 */ 00181 PARAM_ADCCONFIG3 = 0x0E, /**< ADC config for Channel 3 */ 00182 PARAM_ADCSENS3 = 0x0F, /**< ADC sensitivity setting for Channel 3 */ 00183 PARAM_ADCPOST3 = 0x10, /**< ADC resolution, shift and threshold settings for Channel 3 */ 00184 PARAM_MEASCONFIG3 = 0x11, /**< ADC measurement counter selection for Channel 3 */ 00185 PARAM_ADCCONFIG4 = 0x12, /**< ADC config for Channel 4 */ 00186 PARAM_ADCSENS4 = 0x13, /**< ADC sensitivity setting for Channel 4 */ 00187 PARAM_ADCPOST4 = 0x14, /**< ADC resolution, shift and threshold settings for Channel 4 */ 00188 PARAM_MEASCONFIG4 = 0x15, /**< ADC measurement counter selection for Channel 4 */ 00189 PARAM_ADCCONFIG5 = 0x16, /**< ADC config for Channel 5 */ 00190 PARAM_ADCSENS5 = 0x17, /**< ADC sensitivity setting for Channel 5 */ 00191 PARAM_ADCPOST5 = 0x18, /**< ADC resolution, shift and threshold settings for Channel 5 */ 00192 PARAM_MEASCONFIG5 = 0x19, /**< ADC measurement counter selection for Channel 5 */ 00193 PARAM_MEASRATE_H = 0x1A, /**< Main measurement rate counter MSB */ 00194 PARAM_MEASRATE_L = 0x1B, /**< Main measurement rate counter LSB */ 00195 PARAM_MEASCOUNT0 = 0x1C, /**< Measurement rate extension counter 0 */ 00196 PARAM_MEASCOUNT1 = 0x1D, /**< Measurement rate extension counter 1 */ 00197 PARAM_MEASCOUNT2 = 0x1E, /**< Measurement rate extension counter 2 */ 00198 PARAM_THRESHOLD0_H = 0x25, /**< Threshold level 0 MSB */ 00199 PARAM_THRESHOLD0_L = 0x26, /**< Threshold level 0 LSB */ 00200 PARAM_THRESHOLD1_H = 0x27, /**< Threshold level 1 MSB */ 00201 PARAM_THRESHOLD1_L = 0x28, /**< Threshold level 1 LSB */ 00202 PARAM_THRESHOLD2_H = 0x29, /**< Threshold level 2 MSB */ 00203 PARAM_THRESHOLD2_L = 0x2A, /**< Threshold level 2 LSB */ 00204 PARAM_BURST = 0x2B, /**< Burst enable and burst count */ 00205 }; 00206 /**@}*/ 00207 00208 /** 00209 * @name Commands 00210 * @{ 00211 */ 00212 enum Command { 00213 CMD_RESET_CMD_CTR = 0x00, /**< Resets the command counter */ 00214 CMD_RESET = 0x01, /**< Forces a Reset */ 00215 CMD_NEW_ADDR = 0x02, /**< Stores the new I2C address */ 00216 CMD_FORCE_CH = 0x11, /**< Initiates a set of measurements specified in CHAN_LIST parameter */ 00217 CMD_PAUSE_CH = 0x12, /**< Pauses autonomous measurements */ 00218 CMD_START = 0x13, /**< Starts autonomous measurements */ 00219 CMD_PARAM_SET = 0x80, /**< Sets a parameter */ 00220 CMD_PARAM_QUERY = 0x40, /**< Reads a parameter */ 00221 }; 00222 /**@}*/ 00223 00224 /** 00225 * @name Responses 00226 * @{ 00227 */ 00228 enum Response { 00229 RSP0_CHIPSTAT_MASK = 0xE0, /**< Chip state mask in Response0 register */ 00230 RSP0_COUNTER_MASK = 0x1F, /**< Command counter and error indicator mask in Response0 register */ 00231 RSP0_SLEEP = 0x20, /**< Sleep state indicator bit mask in Response0 register */ 00232 }; 00233 /**@}*/ 00234 00235 /** 00236 * @brief 00237 * Structure to store the data measured by the Si1133 00238 */ 00239 typedef struct { 00240 uint8_t irq_status; /**< Interrupt status of the device */ 00241 int32_t ch0; /**< Channel 0 measurement data */ 00242 int32_t ch1; /**< Channel 1 measurement data */ 00243 int32_t ch2; /**< Channel 2 measurement data */ 00244 int32_t ch3; /**< Channel 3 measurement data */ 00245 } Samples_t; 00246 00247 /** 00248 * @brief 00249 * Structure to store the calculation coefficients 00250 */ 00251 typedef struct { 00252 int16_t info; /**< Info */ 00253 uint16_t mag; /**< Magnitude */ 00254 } Coeff_t; 00255 00256 /** 00257 * @brief 00258 * Structure to store the coefficients used for Lux calculation 00259 */ 00260 typedef struct { 00261 Coeff_t coeff_high[4]; /**< High amplitude coeffs */ 00262 Coeff_t coeff_low[9]; /**< Low amplitude coeffs */ 00263 } LuxCoeff_t; 00264 00265 /* Forward-declare constant coefficient table */ 00266 static const LuxCoeff_t lk; 00267 static const Coeff_t uk[]; 00268 00269 /* Private functions */ 00270 uint32_t read_register(enum Register reg, uint8_t *data); 00271 uint32_t write_register(enum Register reg, uint8_t data); 00272 uint32_t write_register_block(enum Register reg, uint8_t length, uint8_t *data); 00273 uint32_t read_register_block(enum Register reg, uint8_t length, uint8_t *data); 00274 uint32_t get_irq_status(uint8_t *irq_status); 00275 00276 uint32_t reset(void); 00277 uint32_t reset_cmd_counter (void); 00278 uint32_t send_cmd(enum Command command); 00279 uint32_t force_measurement (void); 00280 uint32_t pause_measurement (void); 00281 uint32_t start_measurement (void); 00282 uint32_t set_parameter (enum Parameter address, uint8_t value); 00283 uint32_t read_parameter (enum Parameter address); 00284 uint32_t init (void); 00285 uint32_t deinit (void); 00286 uint32_t measure (Samples_t *samples); 00287 int32_t get_uv (int32_t uv); 00288 int32_t get_lux (int32_t vis_high, int32_t vis_low, int32_t ir); 00289 uint32_t measure_lux_uv (float *lux, float *uvi); 00290 uint32_t get_measurement (float *lux, float *uvi); 00291 uint32_t get_hardware_id (uint8_t *hardware_id); 00292 00293 int32_t calculate_polynomial_helper (int32_t input, int8_t fraction, uint16_t mag, int8_t shift); 00294 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); 00295 00296 /* Member variables */ 00297 I2C m_I2C; 00298 }; 00299 00300 #endif
Generated on Sat Jul 30 2022 05:15:02 by 1.7.2