light3  2.1.0.0
light3.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef LIGHT3_H
29 #define LIGHT3_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_i2c_master.h"
52 
73 #define LIGHT3_REG_RESULT_MSB_CH0 0x00
74 #define LIGHT3_REG_RESULT_LSB_CH0 0x01
75 #define LIGHT3_REG_RESULT_MSB_CH1 0x02
76 #define LIGHT3_REG_RESULT_LSB_CH1 0x03
77 #define LIGHT3_REG_RESULT_FIFO_MSB_CH0 0x04
78 #define LIGHT3_REG_RESULT_FIFO_LSB_CH0 0x05
79 #define LIGHT3_REG_RESULT_FIFO_MSB_CH1 0x06
80 #define LIGHT3_REG_RESULT_FIFO_LSB_CH1 0x07
81 #define LIGHT3_REG_TRESHOLD_LOW 0x08
82 #define LIGHT3_REG_TRESHOLD_HIGH 0x09
83 #define LIGHT3_REG_CONFIG 0x0A
84 #define LIGHT3_REG_INT_CONFIG 0x0B
85 #define LIGHT3_REG_ERROR_FLAGS 0x0C
86 #define LIGHT3_REG_DEVICE_ID 0x11
87  // light3_reg
89 
104 #define LIGHT3_QWAKE_EN 0x8000u
105 #define LIGHT3_RANGE_561_LUX 0x0000u
106 #define LIGHT3_RANGE_1_1_KLUX 0x0400u
107 #define LIGHT3_RANGE_2_2_KLUX 0x0800u
108 #define LIGHT3_RANGE_4_4_KLUX 0x0C00u
109 #define LIGHT3_RANGE_8_9_KLUX 0x1000u
110 #define LIGHT3_RANGE_17_9_KLUX 0x1400u
111 #define LIGHT3_RANGE_35_9_KLUX 0x1800u
112 #define LIGHT3_RANGE_71_8_KLUX 0x1C00u
113 #define LIGHT3_RANGE_143_KLUX 0x2000u
114 #define LIGHT3_RANGE_AUTO_RANGE 0x3000u
115 #define LIGHT3_CONV_TIME_600_US 0x0000u
116 #define LIGHT3_CONV_TIME_1_MS 0x0040u
117 #define LIGHT3_CONV_TIME_1_8_MS 0x0080u
118 #define LIGHT3_CONV_TIME_3_4_MS 0x00C0u
119 #define LIGHT3_CONV_TIME_6_5_MS 0x0100u
120 #define LIGHT3_CONV_TIME_12_7_MS 0x0140u
121 #define LIGHT3_CONV_TIME_25_MS 0x0180u
122 #define LIGHT3_CONV_TIME_50_MS 0x01C0u
123 #define LIGHT3_CONV_TIME_100_MS 0x0200u
124 #define LIGHT3_CONV_TIME_200_MS 0x0240u
125 #define LIGHT3_CONV_TIME_400_MS 0x0280u
126 #define LIGHT3_CONV_TIME_800_MS 0x02C0u
127 #define LIGHT3_MODE_POWER_DOWN 0x0000u
128 #define LIGHT3_MODE_AUTO_RANGE_ONE_SHOT 0x0010u
129 #define LIGHT3_MODE_ONE_SHOT 0x0020u
130 #define LIGHT3_MODE_CONTINUOUS 0x0030u
131 #define LIGHT3_LATCH 0x0008u
132 #define LIGHT3_INT_POL_ACTIVE_LOW 0x0000u
133 #define LIGHT3_INT_POL_ACTIVE_HIGH 0x0004u
134 #define LIGHT3_FAULT_COUNT_1 0x0000u
135 #define LIGHT3_FAULT_COUNT_2 0x0001u
136 #define LIGHT3_FAULT_COUNT_4 0x0002u
137 #define LIGHT3_FAULT_COUNT_8 0x0003u
138 
143 #define LIGHT3_INT_CONFIG_REG_DEF_VALUE 0x8000u
144 #define LIGHT3_TRESHOLD_CH0_SEL 0x0000u
145 #define LIGHT3_TRESHOLD_CH1_SEL 0x0020u
146 #define LIGHT3_INT_DIR_INPUT 0x0000u
147 #define LIGHT3_INT_DIR_OUTPUT 0x0010u
148 #define LIGHT3_INT_CFG_SMBUS_ALERT 0x0000u
149 #define LIGHT3_INT_EVERY_CONV 0x0004u
150 #define LIGHT3_INT_EVERY_2_CONV 0x0008u
151 #define LIGHT3_INT_EVERY_4_CONV 0x000Cu
152 #define LIGHT3_I2C_BURST_ENABLED 0x0001u
153 
158 #define LIGHT3_LUX_CONV_FACTOR 0.000535f
159 
164 #define LIGHT3_PIN_STATE_LOW 0x00
165 #define LIGHT3_PIN_STATE_HIGH 0x01
166 
171 #define LIGHT3_SW_RESET_ADDRESS 0x00
172 #define LIGHT3_SW_RESET_COMMAND 0x06
173 
179 #define LIGHT3_DEVICE_ADDRESS_0 0x44
180 #define LIGHT3_DEVICE_ADDRESS_1 0x45
181  // light3_set
183 
198 #define LIGHT3_MAP_MIKROBUS( cfg, mikrobus ) \
199  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
200  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
201  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
202  // light3_map // light3
205 
210 typedef struct
211 {
212  // Input pins
213  digital_in_t int_pin;
215  // Modules
216  i2c_master_t i2c;
218  // I2C slave address
219  uint8_t slave_address;
221 } light3_t;
222 
227 typedef struct
228 {
229  pin_name_t scl;
230  pin_name_t sda;
232  pin_name_t int_pin;
234  uint32_t i2c_speed;
235  uint8_t i2c_address;
237 } light3_cfg_t;
238 
243 typedef enum
244 {
246  LIGHT3_ERROR = -1
247 
249 
266 
280 err_t light3_init ( light3_t *ctx, light3_cfg_t *cfg );
281 
295 
310 err_t light3_generic_write ( light3_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
311 
326 err_t light3_generic_read ( light3_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
327 
336 uint8_t light3_get_int_pin ( light3_t *ctx );
337 
348 err_t light3_sw_reset ( light3_t *ctx );
349 
363 err_t light3_write_reg ( light3_t *ctx, uint8_t reg, uint16_t data_in );
364 
378 err_t light3_read_reg ( light3_t *ctx, uint8_t reg, uint16_t *data_out );
379 
392 err_t light3_get_ch0_data ( light3_t *ctx, float *lux );
393 
406 err_t light3_get_ch1_data ( light3_t *ctx, float *lux );
407 
420 err_t light3_get_ch0_fifo_data ( light3_t *ctx, float *lux );
421 
434 err_t light3_get_ch1_fifo_data ( light3_t *ctx, float *lux );
435 
436 #ifdef __cplusplus
437 }
438 #endif
439 #endif // LIGHT3_H
440  // light3
442 
443 // ------------------------------------------------------------------------ END
light3_cfg_setup
void light3_cfg_setup(light3_cfg_t *cfg)
Light 3 configuration object setup function.
light3_get_ch0_data
err_t light3_get_ch0_data(light3_t *ctx, float *lux)
Light 3 read CH0 data function.
light3_default_cfg
err_t light3_default_cfg(light3_t *ctx)
Light 3 default configuration function.
LIGHT3_OK
@ LIGHT3_OK
Definition: light3.h:245
light3_cfg_t::int_pin
pin_name_t int_pin
Definition: light3.h:232
light3_cfg_t::scl
pin_name_t scl
Definition: light3.h:229
light3_get_ch1_data
err_t light3_get_ch1_data(light3_t *ctx, float *lux)
Light 3 read CH1 data function.
light3_cfg_t::i2c_address
uint8_t i2c_address
Definition: light3.h:235
light3_init
err_t light3_init(light3_t *ctx, light3_cfg_t *cfg)
Light 3 initialization function.
light3_cfg_t
Light 3 Click configuration object.
Definition: light3.h:228
light3_get_ch1_fifo_data
err_t light3_get_ch1_fifo_data(light3_t *ctx, float *lux)
Light 3 read CH1 FIFO data function.
light3_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: light3.h:234
light3_t
Light 3 Click context object.
Definition: light3.h:211
light3_read_reg
err_t light3_read_reg(light3_t *ctx, uint8_t reg, uint16_t *data_out)
Light 3 register reading function.
light3_sw_reset
err_t light3_sw_reset(light3_t *ctx)
Light 3 software reset function.
light3_return_value_t
light3_return_value_t
Light 3 Click return value data.
Definition: light3.h:244
light3_generic_read
err_t light3_generic_read(light3_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Light 3 I2C reading function.
light3_t::int_pin
digital_in_t int_pin
Definition: light3.h:213
LIGHT3_ERROR
@ LIGHT3_ERROR
Definition: light3.h:246
light3_t::slave_address
uint8_t slave_address
Definition: light3.h:219
light3_t::i2c
i2c_master_t i2c
Definition: light3.h:216
light3_write_reg
err_t light3_write_reg(light3_t *ctx, uint8_t reg, uint16_t data_in)
Light 3 register writing function.
light3_cfg_t::sda
pin_name_t sda
Definition: light3.h:230
light3_generic_write
err_t light3_generic_write(light3_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Light 3 I2C writing function.
light3_get_int_pin
uint8_t light3_get_int_pin(light3_t *ctx)
Light 3 get INT pin state function.
light3_get_ch0_fifo_data
err_t light3_get_ch0_fifo_data(light3_t *ctx, float *lux)
Light 3 read CH0 FIFO data function.