lightmixsens  2.0.0.0
lightmixsens.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 LIGHTMIXSENS_H
29 #define LIGHTMIXSENS_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_i2c_master.h"
48 
69 #define LIGHTMIXSENS_REG_ENABLE 0x80
70 #define LIGHTMIXSENS_REG_ATIME 0x81
71 #define LIGHTMIXSENS_REG_PTIME 0x82
72 #define LIGHTMIXSENS_REG_WTIME 0x83
73 #define LIGHTMIXSENS_REG_AILT 0x84
74 #define LIGHTMIXSENS_REG_AIHT 0x86
75 #define LIGHTMIXSENS_REG_PILT 0x88
76 #define LIGHTMIXSENS_REG_PIHT 0x8A
77 #define LIGHTMIXSENS_REG_PERS 0x8C
78 #define LIGHTMIXSENS_REG_CFG0 0x8D
79 #define LIGHTMIXSENS_REG_PCFG0 0x8E
80 #define LIGHTMIXSENS_REG_PCFG1 0x8F
81 #define LIGHTMIXSENS_REG_CFG1 0x90
82 #define LIGHTMIXSENS_REG_REVID 0x91
83 #define LIGHTMIXSENS_REG_ID 0x92
84 #define LIGHTMIXSENS_REG_STATUS 0x93
85 #define LIGHTMIXSENS_REG_CDATA 0x94
86 #define LIGHTMIXSENS_REG_RDATA 0x96
87 #define LIGHTMIXSENS_REG_GDATA_IRDATA 0x98
88 #define LIGHTMIXSENS_REG_BDATA 0x9A
89 #define LIGHTMIXSENS_REG_PDATA 0x9C
90 #define LIGHTMIXSENS_REG_REVID2 0x9E
91 #define LIGHTMIXSENS_REG_CFG2 0x9F
92 #define LIGHTMIXSENS_REG_CFG3 0xAB
93 #define LIGHTMIXSENS_REG_POFFSET 0xC0
94 #define LIGHTMIXSENS_REG_AZCFG 0xD6
95 #define LIGHTMIXSENS_REG_CALIB 0xD7
96 #define LIGHTMIXSENS_REG_CALIBCFG 0xD9
97 #define LIGHTMIXSENS_REG_CALIBSTAT 0xDC
98 #define LIGHTMIXSENS_REG_INTENAB 0xDD
99  // lightmixsens_reg
101 
116 #define LIGHTMIXSENS_ATIME_WTIME_CONST 2.8
117 #define LIGHTMIXSENS_PTIME_CONST 88
118 #define LIGHTMIXSENS_WTIME_INCR_FACTOR 12
119 
120 #define LIGHTMIXSENS_ENABLE_WAIT 0x08
121 #define LIGHTMIXSENS_ENABLE_PROX 0x04
122 #define LIGHTMIXSENS_ENABLE_ALS 0x02
123 #define LIGHTMIXSENS_ENABLE_OSC 0x01
124 #define LIGHTMIXSENS_DISABLE_ALL 0x00
125 
126 #define LIGHTMIXSENS_PCFG1_GAIN_1x 0x00
127 #define LIGHTMIXSENS_PCFG1_GAIN_2x 0x40
128 #define LIGHTMIXSENS_PCFG1_GAIN_4x 0x80
129 #define LIGHTMIXSENS_PCFG1_GAIN_8x 0xC0
130 #define LIGHTMIXSENS_PCFG1_DRIVE_6mA 0x00
131 #define LIGHTMIXSENS_PCFG1_DRIVE_12mA 0x01
132 #define LIGHTMIXSENS_PCFG1_DRIVE_18mA 0x02
133 #define LIGHTMIXSENS_PCFG1_DRIVE_192mA 0x1F
134 
135 #define LIGHTMIXSENS_CFG0_WLONG 0x04
136 
137 #define LIGHTMIXSENS_CFG1_IR_TO_GREEN 0x08
138 #define LIGHTMIXSENS_CFG1_AGAIN_1x 0x00
139 #define LIGHTMIXSENS_CFG1_AGAIN_4x 0x01
140 #define LIGHTMIXSENS_CFG1_AGAIN_16x 0x02
141 #define LIGHTMIXSENS_CFG1_AGAIN_64x 0x03
142 
143 #define LIGHTMIXSENS_CFG2_AGAINMAX 0x10
144 #define LIGHTMIXSENS_CFG2_AGAINL 0x04
145 
146 #define LIGHTMIXSENS_CFG3_INT_READ_CLEAR 0x80
147 #define LIGHTMIXSENS_CFG3_SAI 0x10
148 
153 #define LIGHTMIXSENS_FLAG_ASAT 0x80
154 #define LIGHTMIXSENS_FLAG_PSAT 0x40
155 #define LIGHTMIXSENS_FLAG_PINT 0x20
156 #define LIGHTMIXSENS_FLAG_AINT 0x10
157 #define LIGHTMIXSENS_FLAG_CINT 0x08
158 #define LIGHTMIXSENS_FLAG_PSAT_REFL 0x02
159 #define LIGHTMIXSENS_FLAG_PSAT_AMB 0x01
160 
165 #define LIGHTMIXSENS_CALIB_ELECTRICAL 0x20
166 #define LIGHTMIXSENS_CALIB_OFFSET_START 0x01
167 
168 #define LIGHTMIXSENS_CALIBCFG_PROX_TARGET_0 0x00
169 #define LIGHTMIXSENS_CALIBCFG_PROX_TARGET_1 0x20
170 #define LIGHTMIXSENS_CALIBCFG_PROX_TARGET_3 0x40
171 #define LIGHTMIXSENS_CALIBCFG_PROX_TARGET_7 0x60
172 #define LIGHTMIXSENS_CALIBCFG_PROX_TARGET_15 0x80
173 #define LIGHTMIXSENS_CALIBCFG_PROX_TARGET_31 0xA0
174 #define LIGHTMIXSENS_CALIBCFG_PROX_TARGET_63 0xC0
175 #define LIGHTMIXSENS_CALIBCFG_PROX_TARGET_127 0xE0
176 #define LIGHTMIXSENS_CALIBCFG_AUTO_OFFSET_ADJ 0x08
177 #define LIGHTMIXSENS_CALIBCFG_PROX_AVG_DIS 0x00
178 #define LIGHTMIXSENS_CALIBCFG_PROX_AVG_2 0x01
179 #define LIGHTMIXSENS_CALIBCFG_PROX_AVG_4 0x02
180 #define LIGHTMIXSENS_CALIBCFG_PROX_AVG_8 0x03
181 #define LIGHTMIXSENS_CALIBCFG_PROX_AVG_16 0x04
182 #define LIGHTMIXSENS_CALIBCFG_PROX_AVG_32 0x05
183 #define LIGHTMIXSENS_CALIBCFG_PROX_AVG_64 0x06
184 #define LIGHTMIXSENS_CALIBCFG_PROX_AVG_128 0x07
185 
186 #define LIGHTMIXSENS_CALIBSTAT_FINISHED 0x01
187 #define LIGHTMIXSENS_DUMMY_DATA 0x00
188 
194 #define LIGHTMIXSENS_SET_DEV_ADDR 0x39
195  // lightmixsens_set
197 
212 #define LIGHTMIXSENS_MAP_MIKROBUS( cfg, mikrobus ) \
213  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
214  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
215  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
216  // lightmixsens_map // lightmixsens
219 
224 typedef struct
225 {
226  // Input pins
227  digital_in_t int_pin;
229  // Modules
230  i2c_master_t i2c;
232  // I2C slave address
233  uint8_t slave_address;
236 
241 typedef struct
242 {
243  pin_name_t scl;
244  pin_name_t sda;
246  pin_name_t int_pin;
248  uint32_t i2c_speed;
249  uint8_t i2c_address;
252 
257 typedef struct
258 {
265 
270 typedef enum
271 {
273  LIGHTMIXSENS_ERROR = -1
274 
276 
293 
308 
323 
338 err_t lightmixsens_generic_write ( lightmixsens_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len );
339 
354 err_t lightmixsens_generic_read ( lightmixsens_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len );
355 
356 
369 err_t lightmixsens_write_byte ( lightmixsens_t *ctx, uint8_t reg_addr, uint8_t data_in );
370 
384 err_t lightmixsens_write_word ( lightmixsens_t *ctx, uint8_t reg_addr, uint16_t data_in );
385 
398 err_t lightmixsens_read_byte ( lightmixsens_t *ctx, uint8_t reg_addr, uint8_t *data_out );
399 
413 err_t lightmixsens_read_word ( lightmixsens_t *ctx, uint8_t reg_addr, uint16_t *data_out );
414 
425 void lightmixsens_get_status ( lightmixsens_t *ctx, uint8_t flag_mask, uint8_t *status );
426 
436 
446 
456 
466 
476 
477 #ifdef __cplusplus
478 }
479 #endif
480 #endif // LIGHTMIXSENS_H
481  // lightmixsens
483 
484 // ------------------------------------------------------------------------ END
lightmixsens_data_obj::lightmixsens_gdata
uint16_t lightmixsens_gdata
Definition: lightmixsens.h:261
lightmixsens_cfg_setup
void lightmixsens_cfg_setup(lightmixsens_cfg_t *cfg)
Light mix sens configuration object setup function.
lightmixsens_t::i2c
i2c_master_t i2c
Definition: lightmixsens.h:230
lightmixsens_data_obj::lightmixsens_rdata
uint16_t lightmixsens_rdata
Definition: lightmixsens.h:260
lightmixsens_cfg_t::sda
pin_name_t sda
Definition: lightmixsens.h:244
lightmixsens_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: lightmixsens.h:248
lightmixsens_get_status
void lightmixsens_get_status(lightmixsens_t *ctx, uint8_t flag_mask, uint8_t *status)
Get Status function.
lightmixsens_cfg_t::i2c_address
uint8_t i2c_address
Definition: lightmixsens.h:249
lightmixsens_switch_ir_to_green
void lightmixsens_switch_ir_to_green(lightmixsens_t *ctx)
Switch IR To Green Channel function.
lightmixsens_wait_atime
void lightmixsens_wait_atime(lightmixsens_t *ctx)
Wait ALS/Color Intregration Time Done function.
lightmixsens_data_obj::lightmixsens_pdata
uint8_t lightmixsens_pdata
Definition: lightmixsens.h:263
LIGHTMIXSENS_ERROR
@ LIGHTMIXSENS_ERROR
Definition: lightmixsens.h:273
lightmixsens_generic_write
err_t lightmixsens_generic_write(lightmixsens_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len)
Light mix sens I2C writing function.
lightmixsens_cfg_t::int_pin
pin_name_t int_pin
Definition: lightmixsens.h:246
lightmixsens_t::int_pin
digital_in_t int_pin
Definition: lightmixsens.h:227
lightmixsens_data_obj::lightmixsens_bdata
uint16_t lightmixsens_bdata
Definition: lightmixsens.h:262
lightmixsens_init
err_t lightmixsens_init(lightmixsens_t *ctx, lightmixsens_cfg_t *cfg)
Light mix sens initialization function.
lightmixsens_wait_wtime
void lightmixsens_wait_wtime(lightmixsens_t *ctx)
Wait WTIME + WLONG Done function.
lightmixsens_write_word
err_t lightmixsens_write_word(lightmixsens_t *ctx, uint8_t reg_addr, uint16_t data_in)
Generic Write Word function.
lightmixsens_t::slave_address
uint8_t slave_address
Definition: lightmixsens.h:233
lightmixsens_switch_ir_to_prox
void lightmixsens_switch_ir_to_prox(lightmixsens_t *ctx)
Switch IR To Proximity function.
lightmixsens_read_byte
err_t lightmixsens_read_byte(lightmixsens_t *ctx, uint8_t reg_addr, uint8_t *data_out)
Generic Read Byte function.
lightmixsens_t
Light mix sens Click context object.
Definition: lightmixsens.h:225
lightmixsens_write_byte
err_t lightmixsens_write_byte(lightmixsens_t *ctx, uint8_t reg_addr, uint8_t data_in)
Generic Write Byte function.
lightmixsens_read_word
err_t lightmixsens_read_word(lightmixsens_t *ctx, uint8_t reg_addr, uint16_t *data_out)
Generic Read Word function.
lightmixsens_return_value_t
lightmixsens_return_value_t
Light mix sens Click return value data.
Definition: lightmixsens.h:271
LIGHTMIXSENS_OK
@ LIGHTMIXSENS_OK
Definition: lightmixsens.h:272
lightmixsens_cfg_t::scl
pin_name_t scl
Definition: lightmixsens.h:243
lightmixsens_generic_read
err_t lightmixsens_generic_read(lightmixsens_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len)
Light mix sens I2C reading function.
lightmixsens_cfg_t
Light mix sens Click configuration object.
Definition: lightmixsens.h:242
lightmixsens_wait_ptime
void lightmixsens_wait_ptime(lightmixsens_t *ctx)
Wait Proximity Sample Time Done function.
lightmixsens_data_obj::lightmixsens_cdata
uint16_t lightmixsens_cdata
Definition: lightmixsens.h:259
lightmixsens_default_cfg
void lightmixsens_default_cfg(lightmixsens_t *ctx)
Light mix sens default configuration function.
lightmixsens_data_obj
Light mix sens Click data object.
Definition: lightmixsens.h:258