light4  2.1.0.0
light4.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 LIGHT4_H
29 #define LIGHT4_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 LIGHT4_REG_UV_CALIB 0x08
74 #define LIGHT4_REG_MOD_CHANNEL_CTRL 0x40
75 #define LIGHT4_REG_ENABLE 0x80
76 #define LIGHT4_REG_MEAS_MODE0 0x81
77 #define LIGHT4_REG_MEAS_MODE1 0x82
78 #define LIGHT4_REG_SAMPLE_TIME0 0x83
79 #define LIGHT4_REG_SAMPLE_TIME1 0x84
80 #define LIGHT4_REG_ALS_NR_SAMPLES0 0x85
81 #define LIGHT4_REG_ALS_NR_SAMPLES1 0x86
82 #define LIGHT4_REG_FD_NR_SAMPLES0 0x87
83 #define LIGHT4_REG_FD_NR_SAMPLES1 0x88
84 #define LIGHT4_REG_WTIME 0x89
85 #define LIGHT4_REG_AILT0 0x8A
86 #define LIGHT4_REG_AILT1 0x8B
87 #define LIGHT4_REG_AILT2 0x8C
88 #define LIGHT4_REG_AIHT0 0x8D
89 #define LIGHT4_REG_AIHT1 0x8B
90 #define LIGHT4_REG_AIHT2 0x8F
91 #define LIGHT4_REG_AUX_ID 0x90
92 #define LIGHT4_REG_REV_ID 0x91
93 #define LIGHT4_REG_ID 0x92
94 #define LIGHT4_REG_STATUS 0x93
95 #define LIGHT4_REG_ALS_STATUS 0x94
96 #define LIGHT4_REG_ALS_DATA0_LSB 0x95
97 #define LIGHT4_REG_ALS_DATA0_MSB 0x96
98 #define LIGHT4_REG_ALS_DATA1_LSB 0x97
99 #define LIGHT4_REG_ALS_DATA1_MSB 0x98
100 #define LIGHT4_REG_ALS_DATA2_LSB 0x99
101 #define LIGHT4_REG_ALS_DATA2_MSB 0x9A
102 #define LIGHT4_REG_ALS_STATUS2 0x9B
103 #define LIGHT4_REG_ALS_STATUS3 0x9C
104 #define LIGHT4_REG_STATUS2 0x9D
105 #define LIGHT4_REG_STATUS3 0x9E
106 #define LIGHT4_REG_STATUS4 0x9F
107 #define LIGHT4_REG_STATUS5 0xA0
108 #define LIGHT4_REG_CFG0 0xA1
109 #define LIGHT4_REG_CFG1 0xA2
110 #define LIGHT4_REG_CFG2 0xA3
111 #define LIGHT4_REG_CFG3 0xA4
112 #define LIGHT4_REG_CFG4 0xA5
113 #define LIGHT4_REG_CFG5 0xA6
114 #define LIGHT4_REG_CFG6 0xA7
115 #define LIGHT4_REG_CFG7 0xA8
116 #define LIGHT4_REG_CFG8 0xA9
117 #define LIGHT4_REG_CFG9 0xAA
118 #define LIGHT4_REG_AGC_NR_SAMPLES_LSB 0xAC
119 #define LIGHT4_REG_AGC_NR_SAMPLES_MSB 0xAD
120 #define LIGHT4_REG_TRIGGER_MODE 0xAE
121 #define LIGHT4_REG_CONTROL 0xB1
122 #define LIGHT4_REG_INTENAB 0xBA
123 #define LIGHT4_REG_SIEN 0xBB
124 #define LIGHT4_REG_MOD_COMP_CFG1 0xCE
125 #define LIGHT4_REG_MEAS_SEQR_FD_0 0xCF
126 #define LIGHT4_REG_MEAS_SEQR_ALS_FD_1 0xD0
127 #define LIGHT4_REG_MEAS_SEQR_APERS_AND_VSYNC_WAIT 0xD1
128 #define LIGHT4_REG_MEAS_SEQR_RESIDUAL_0 0xD2
129 #define LIGHT4_REG_MEAS_SEQR_RESIDUAL_1_AND_WAIT 0xD3
130 #define LIGHT4_REG_MEAS_SEQR_STEP0_MOD_GAINX_L 0xD4
131 #define LIGHT4_REG_MEAS_SEQR_STEP0_MOD_GAINX_H 0xD5
132 #define LIGHT4_REG_MEAS_SEQR_STEP1_MOD_GAINX_L 0xD6
133 #define LIGHT4_REG_MEAS_SEQR_STEP1_MOD_GAINX_H 0xD7
134 #define LIGHT4_REG_MEAS_SEQR_STEP2_MOD_GAINX_L 0xD8
135 #define LIGHT4_REG_MEAS_SEQR_STEP2_MOD_GAINX_H 0xD9
136 #define LIGHT4_REG_MEAS_SEQR_STEP3_MOD_GAINX_L 0xDA
137 #define LIGHT4_REG_MEAS_SEQR_STEP3_MOD_GAINX_H 0xDB
138 #define LIGHT4_REG_MEAS_SEQR_STEP0_MOD_PHDX_L 0xDC
139 #define LIGHT4_REG_MEAS_SEQR_STEP0_MOD_PHDX_H 0xDD
140 #define LIGHT4_REG_MEAS_SEQR_STEP1_MOD_PHDX_L 0xDE
141 #define LIGHT4_REG_MEAS_SEQR_STEP1_MOD_PHDX_H 0xDF
142 #define LIGHT4_REG_MEAS_SEQR_STEP2_MOD_PHDX_L 0xE0
143 #define LIGHT4_REG_MEAS_SEQR_STEP2_MOD_PHDX_H 0xE1
144 #define LIGHT4_REG_MEAS_SEQR_STEP3_MOD_PHDX_L 0xE2
145 #define LIGHT4_REG_MEAS_SEQR_STEP3_MOD_PHDX_H 0xE3
146 #define LIGHT4_REG_MOD_CALIB_CFG0 0xE4
147 #define LIGHT4_REG_MOD_CALIB_CFG2 0xE6
148 #define LIGHT4_REG_VSYNC_PERIOD_LSB 0xF2
149 #define LIGHT4_REG_VSYNC_PERIOD_MSB 0xF3
150 #define LIGHT4_REG_VSYNC_PERIOD_TARGET_LSB 0xF4
151 #define LIGHT4_REG_VSYNC_PERIOD_TARGET_MSB 0xF5
152 #define LIGHT4_REG_VSYNC_CONTROL 0xF6
153 #define LIGHT4_REG_VSYNC_CFG 0xF7
154 #define LIGHT4_REG_VSYNC_GPIO_INT 0xF8
155 #define LIGHT4_REG_MOD_FIFO_DATA_CFG0 0xF9
156 #define LIGHT4_REG_MOD_FIFO_DATA_CFG1 0xFA
157 #define LIGHT4_REG_MOD_FIFO_DATA_CFG2 0xFB
158 #define LIGHT4_REG_FIFO_THR 0xDC
159 #define LIGHT4_REG_FIFO_STATUS0 0xFD
160 #define LIGHT4_REG_FIFO_STATUS1 0xFE
161 #define LIGHT4_REG_FIFO_DATA 0xFF
162  // light4_reg
164 
179 #define LIGHT4_FDEN 0x40
180 #define LIGHT4_AEN 0x02
181 #define LIGHT4_PON 0x01
182 
187 #define LIGHT4_MOD2_DISABLE 0x04
188 #define LIGHT4_MOD1_DISABLE 0x02
189 #define LIGHT4_MOD0_DISABLE 0x01
190 #define LIGHT4_MOD_ENABLE_ALL 0x00
191 
196 #define LIGHT4_MIEN 0x80
197 #define LIGHT4_AIEN 0x04
198 #define LIGHT4_FIEN 0x02
199 #define LIGHT4_SIEN 0x01
200 
205 #define LIGHT4_SOFT_RESET 0x08
206 #define LIGHT4_FIFO_CLR 0x02
207 #define LIGHT4_CLEAR_SAI_ACTIVE 0x01
208 
213 #define LIGHT4_MEAS_SEQR_STEP_MASK 0xC0
214 #define LIGHT4_ALS_DATA0_ANALOG_SATURATION_STATUS 0x20
215 #define LIGHT4_ALS_DATA1_ANALOG_SATURATION_STATUS 0x10
216 #define LIGHT4_ALS_DATA2_ANALOG_SATURATION_STATUS 0x08
217 #define LIGHT4_ALS_DATA0_SCALED_STATUS 0x04
218 #define LIGHT4_ALS_DATA1_SCALED_STATUS 0x02
219 #define LIGHT4_ALS_DATA2_SCALED_STATUS 0x01
220 
225 #define LIGHT4_PIN_STATE_HIGH 0x01
226 #define LIGHT4_PIN_STATE_LOW 0x00
227 
232 #define LIGHT4_AUX_ID 0x06
233 #define LIGHT4_REV_ID 0x11
234 #define LIGHT4_DEVICE_ID 0x5C
235 
240 #define LIGHT4_CHANNEL0_SEL 0x00
241 #define LIGHT4_CHANNEL1_SEL 0x01
242 #define LIGHT4_CHANNEL2_SEL 0x02
243 
249 #define LIGHT4_DEVICE_ADDRESS 0x39
250  // light4_set
252 
267 #define LIGHT4_MAP_MIKROBUS( cfg, mikrobus ) \
268  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
269  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
270  cfg.en = MIKROBUS( mikrobus, MIKROBUS_RST ); \
271  cfg.snc = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
272  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
273  // light4_map // light4
276 
281 typedef struct
282 {
283  // Output pins
284  digital_out_t en;
285  digital_out_t snc;
287  // Input pins
288  digital_in_t int_pin;
290  // Modules
291  i2c_master_t i2c;
293  // I2C slave address
294  uint8_t slave_address;
296 } light4_t;
297 
302 typedef struct
303 {
304  pin_name_t scl;
305  pin_name_t sda;
307  pin_name_t en;
308  pin_name_t snc;
309  pin_name_t int_pin;
311  uint32_t i2c_speed;
312  uint8_t i2c_address;
314 } light4_cfg_t;
315 
320 typedef enum
321 {
324  LIGHT4_ANALOG_SAT = -2
325 
327 
344 
358 err_t light4_init ( light4_t *ctx, light4_cfg_t *cfg );
359 
373 
388 err_t light4_generic_write ( light4_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
389 
404 err_t light4_generic_read ( light4_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
405 
415 void light4_set_en_pin ( light4_t *ctx, uint8_t pin_state );
416 
426 void light4_set_snc_pin ( light4_t *ctx, uint8_t pin_state );
427 
436 uint8_t light4_get_int_pin ( light4_t *ctx );
437 
451 err_t light4_write_reg ( light4_t *ctx, uint8_t reg, uint8_t data_in );
452 
466 err_t light4_read_reg ( light4_t *ctx, uint8_t reg, uint8_t *data_out );
467 
479 err_t light4_sw_reset ( light4_t *ctx );
480 
494 err_t light4_read_channel_data ( light4_t *ctx, uint8_t channel_sel, float *channel_data );
495 
496 #ifdef __cplusplus
497 }
498 #endif
499 #endif // LIGHT4_H
500  // light4
502 
503 // ------------------------------------------------------------------------ END
LIGHT4_ANALOG_SAT
@ LIGHT4_ANALOG_SAT
Definition: light4.h:324
light4_t::int_pin
digital_in_t int_pin
Definition: light4.h:288
light4_cfg_t::int_pin
pin_name_t int_pin
Definition: light4.h:309
light4_t::i2c
i2c_master_t i2c
Definition: light4.h:291
light4_set_snc_pin
void light4_set_snc_pin(light4_t *ctx, uint8_t pin_state)
Light 4 set SNC pin function.
light4_cfg_t::en
pin_name_t en
Definition: light4.h:307
light4_set_en_pin
void light4_set_en_pin(light4_t *ctx, uint8_t pin_state)
Light 4 set EN pin function.
light4_cfg_t::i2c_address
uint8_t i2c_address
Definition: light4.h:312
light4_t
Light 4 Click context object.
Definition: light4.h:282
LIGHT4_ERROR
@ LIGHT4_ERROR
Definition: light4.h:323
light4_t::snc
digital_out_t snc
Definition: light4.h:285
light4_write_reg
err_t light4_write_reg(light4_t *ctx, uint8_t reg, uint8_t data_in)
Light 4 Register writing function.
light4_get_int_pin
uint8_t light4_get_int_pin(light4_t *ctx)
Light 4 get INT pin function.
light4_cfg_t::snc
pin_name_t snc
Definition: light4.h:308
light4_t::slave_address
uint8_t slave_address
Definition: light4.h:294
light4_cfg_t
Light 4 Click configuration object.
Definition: light4.h:303
light4_return_value_t
light4_return_value_t
Light 4 Click return value data.
Definition: light4.h:321
light4_cfg_t::scl
pin_name_t scl
Definition: light4.h:304
light4_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: light4.h:311
LIGHT4_OK
@ LIGHT4_OK
Definition: light4.h:322
light4_default_cfg
err_t light4_default_cfg(light4_t *ctx)
Light 4 default configuration function.
light4_generic_read
err_t light4_generic_read(light4_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Light 4 I2C reading function.
light4_cfg_setup
void light4_cfg_setup(light4_cfg_t *cfg)
Light 4 configuration object setup function.
light4_generic_write
err_t light4_generic_write(light4_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Light 4 I2C writing function.
light4_t::en
digital_out_t en
Definition: light4.h:284
light4_read_reg
err_t light4_read_reg(light4_t *ctx, uint8_t reg, uint8_t *data_out)
Light 4 Register reading function.
light4_read_channel_data
err_t light4_read_channel_data(light4_t *ctx, uint8_t channel_sel, float *channel_data)
Light 4 get channel data function.
light4_sw_reset
err_t light4_sw_reset(light4_t *ctx)
Light 4 Software reset function.
light4_cfg_t::sda
pin_name_t sda
Definition: light4.h:305
light4_init
err_t light4_init(light4_t *ctx, light4_cfg_t *cfg)
Light 4 initialization function.