ambient14  2.1.0.0
ambient14.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 AMBIENT14_H
29 #define AMBIENT14_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 AMBIENT14_REG_ENABLE 0x80
74 #define AMBIENT14_REG_ATIME 0x81
75 #define AMBIENT14_REG_PRATE 0x82
76 #define AMBIENT14_REG_AWTIME 0x83
77 #define AMBIENT14_REG_AILTL 0x84
78 #define AMBIENT14_REG_AILTH 0x85
79 #define AMBIENT14_REG_AIHTL 0x86
80 #define AMBIENT14_REG_AIHTH 0x87
81 #define AMBIENT14_REG_PILTL 0x88
82 #define AMBIENT14_REG_PILTH 0x89
83 #define AMBIENT14_REG_PIHTL 0x8A
84 #define AMBIENT14_REG_PIHTH 0x8B
85 #define AMBIENT14_REG_PERS 0x8C
86 #define AMBIENT14_REG_CFG0 0x8D
87 #define AMBIENT14_REG_PCFG0 0x8E
88 #define AMBIENT14_REG_PCFG1 0x8F
89 #define AMBIENT14_REG_PCFG2 0x90
90 #define AMBIENT14_REG_CFG1 0x91
91 #define AMBIENT14_REG_REVID 0x92
92 #define AMBIENT14_REG_ID 0x93
93 #define AMBIENT14_REG_STATUS 0x94
94 #define AMBIENT14_REG_ALSL 0x95
95 #define AMBIENT14_REG_ALSH 0x96
96 #define AMBIENT14_REG_IRL 0x97
97 #define AMBIENT14_REG_IRH 0x98
98 #define AMBIENT14_REG_PDATAL 0x99
99 #define AMBIENT14_REG_PDATAH 0x9A
100 #define AMBIENT14_REG_REVID2 0xA6
101 #define AMBIENT14_REG_SOFTRST 0xA8
102 #define AMBIENT14_REG_PWTIME 0xA9
103 #define AMBIENT14_REG_CFG8 0xAA
104 #define AMBIENT14_REG_CFG3 0xAB
105 #define AMBIENT14_REG_CFG6 0xAE
106 #define AMBIENT14_REG_POFFSETL 0xC0
107 #define AMBIENT14_REG_POFFSETH 0xC1
108 #define AMBIENT14_REG_CALIB 0xD7
109 #define AMBIENT14_REG_CALIB_OFFSET 0xD8
110 #define AMBIENT14_REG_CALIBCFG 0xD9
111 #define AMBIENT14_REG_CALIBSTAT 0xDC
112 #define AMBIENT14_REG_INTENAB 0xDD
113 #define AMBIENT14_REG_FAC_L 0xE6
114 #define AMBIENT14_REG_FAC_H 0xE7
115 #define AMBIENT14_REG_TEST9 0xF9
116  // ambient14_reg
118 
133 #define AMBIENT14_ENABLE_PWEN 0x10
134 #define AMBIENT14_ENABLE_AWEN 0x08
135 #define AMBIENT14_ENABLE_PEN 0x04
136 #define AMBIENT14_ENABLE_AEN 0x02
137 #define AMBIENT14_ENABLE_PON 0x01
138 
143 #define AMBIENT14_SOFT_RESET 0x01
144 
149 #define AMBIENT14_PCFG0_PGAIN1_1X 0x00
150 #define AMBIENT14_PCFG0_PGAIN1_2X 0x40
151 #define AMBIENT14_PCFG0_PGAIN1_4X 0x80
152 #define AMBIENT14_PCFG0_PGAIN1_8X 0xC0
153 #define AMBIENT14_PCFG0_PGAIN1_MASK 0xC0
154 #define AMBIENT14_PCFG0_PPULSE_MIN 0x00
155 #define AMBIENT14_PCFG0_PPULSE_DEFAULT 0x01
156 #define AMBIENT14_PCFG0_PPULSE_MAX 0x3F
157 #define AMBIENT14_PCFG0_PPULSE_MASK 0x3F
158 
163 #define AMBIENT14_PCFG1_PPULSE_LENH_DEFAULT 0x00
164 #define AMBIENT14_PCFG1_PPULSE_LENH_MASK 0xC0
165 #define AMBIENT14_PCFG1_RESERVED 0x30
166 #define AMBIENT14_PCFG1_PLDRIVE_7_MA 0x05
167 #define AMBIENT14_PCFG1_PLDRIVE_8_MA 0x06
168 #define AMBIENT14_PCFG1_PLDRIVE_9_MA 0x07
169 #define AMBIENT14_PCFG1_PLDRIVE_10_MA 0x08
170 #define AMBIENT14_PCFG1_PLDRIVE_MASK 0x0F
171 
176 #define AMBIENT14_PCFG2_PPULSE_LENL_DEFAULT 0x20
177 #define AMBIENT14_PCFG2_PPULSE_LENL_MASK 0xFF
178 
183 #define AMBIENT14_CFG1_PGAIN2_2_5X 0x00
184 #define AMBIENT14_CFG1_PGAIN2_5X 0x20
185 #define AMBIENT14_CFG1_PGAIN2_10X 0x60
186 #define AMBIENT14_CFG1_PGAIN2_MASK 0x60
187 #define AMBIENT14_CFG1_AGAIN_16X 0x05
188 #define AMBIENT14_CFG1_AGAIN_128X 0x08
189 #define AMBIENT14_CFG1_AGAIN_1024X 0x0B
190 #define AMBIENT14_CFG1_AGAIN_MASK 0x1F
191 
196 #define AMBIENT14_CFG8_RESERVED 0x29
197 
202 #define AMBIENT14_CFG6_APC_ENABLE 0x00
203 #define AMBIENT14_CFG6_APC_DISABLE 0x40
204 #define AMBIENT14_CFG6_RESERVED 0x3F
205 
210 #define AMBIENT14_STATUS_ASAT 0x80
211 #define AMBIENT14_STATUS_PSAT 0x40
212 #define AMBIENT14_STATUS_PINT 0x20
213 #define AMBIENT14_STATUS_AINT 0x10
214 #define AMBIENT14_STATUS_CINT 0x08
215 #define AMBIENT14_STATUS_ZINT 0x04
216 #define AMBIENT14_STATUS_PSAT_REFLECTIVE 0x02
217 #define AMBIENT14_STATUS_PSAT_AMBIENT 0x01
218 #define AMBIENT14_STATUS_CLEAR_ALL 0xFF
219 
224 #define AMBIENT14_INTENAB_ASIEN 0x80
225 #define AMBIENT14_INTENAB_PSIEN 0x40
226 #define AMBIENT14_INTENAB_PIEN 0x20
227 #define AMBIENT14_INTENAB_AIEN 0x10
228 #define AMBIENT14_INTENAB_CIEN 0x08
229 #define AMBIENT14_INTENAB_ZIEN 0x04
230 #define AMBIENT14_INTENAB_DISABLE 0x00
231 
236 #define AMBIENT14_CALIB_CALAVG 0x80
237 #define AMBIENT14_CALIB_ELECTRICAL_CALIB 0x20
238 #define AMBIENT14_CALIB_CALPRATE 0x10
239 #define AMBIENT14_CALIB_START_OFFSET_CAL 0x01
240 
245 #define AMBIENT14_CALIBCFG_BINSRCH_TARGET_3 0x00
246 #define AMBIENT14_CALIBCFG_BINSRCH_TARGET_7 0x20
247 #define AMBIENT14_CALIBCFG_BINSRCH_TARGET_15 0x40
248 #define AMBIENT14_CALIBCFG_BINSRCH_TARGET_31 0x60
249 #define AMBIENT14_CALIBCFG_BINSRCH_TARGET_63 0x80
250 #define AMBIENT14_CALIBCFG_BINSRCH_TARGET_127 0xA0
251 #define AMBIENT14_CALIBCFG_BINSRCH_TARGET_255 0xC0
252 #define AMBIENT14_CALIBCFG_BINSRCH_TARGET_511 0xE0
253 #define AMBIENT14_CALIBCFG_BINSRCH_TARGET_MASK 0xE0
254 #define AMBIENT14_CALIBCFG_RESERVED 0x10
255 #define AMBIENT14_CALIBCFG_AUTO_OFFSET_ADJ 0x08
256 #define AMBIENT14_CALIBCFG_PROX_AVG_DISABLE 0x00
257 #define AMBIENT14_CALIBCFG_PROX_AVG_2 0x01
258 #define AMBIENT14_CALIBCFG_PROX_AVG_4 0x02
259 #define AMBIENT14_CALIBCFG_PROX_AVG_8 0x03
260 #define AMBIENT14_CALIBCFG_PROX_AVG_16 0x04
261 #define AMBIENT14_CALIBCFG_PROX_AVG_MASK 0x07
262 
267 #define AMBIENT14_CALIBSTAT_OFFSET_ADJUSTED 0x04
268 #define AMBIENT14_CALIBSTAT_CALIB_FINISHED 0x01
269 
274 #define AMBIENT14_SINGLE_STEP_MS 2.78f
275 #define AMBIENT14_ATIME_MAX ( 256 * AMBIENT14_SINGLE_STEP_MS )
276 #define AMBIENT14_ATIME_MIN AMBIENT14_SINGLE_STEP_MS
277 #define AMBIENT14_ATIME_DEFAULT 100.0f
278 #define AMBIENT14_AWTIME_MAX ( 256 * AMBIENT14_SINGLE_STEP_MS )
279 #define AMBIENT14_AWTIME_MIN AMBIENT14_SINGLE_STEP_MS
280 #define AMBIENT14_AWTIME_DEFAULT 50.0f
281 #define AMBIENT14_PWTIME_MAX ( 256 * AMBIENT14_SINGLE_STEP_MS )
282 #define AMBIENT14_PWTIME_MIN AMBIENT14_SINGLE_STEP_MS
283 #define AMBIENT14_PWTIME_DEFAULT 500.0f
284 #define AMBIENT14_ROUND_TO_INT 0.5f
285 
290 #define AMBIENT14_ILLUMINANCE_GAIN_16_TO_128 0.125f
291 #define AMBIENT14_ILLUMINANCE_ATIME_100_GAIN_128 0.0095f
292 #define AMBIENT14_ILLUMINANCE_GAIN_1024_TO_128 8.2f
293 
298 #define AMBIENT14_DEVICE_ID 0x50
299 
305 #define AMBIENT14_DEVICE_ADDRESS 0x39
306  // ambient14_set
308 
323 #define AMBIENT14_MAP_MIKROBUS( cfg, mikrobus ) \
324  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
325  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
326  cfg.sby = MIKROBUS( mikrobus, MIKROBUS_RST ); \
327  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
328  // ambient14_map // ambient14
331 
336 typedef struct
337 {
338  // Output pins
339  digital_out_t sby;
341  // Input pins
342  digital_in_t int_pin;
344  // Modules
345  i2c_master_t i2c;
347  // I2C slave address
348  uint8_t slave_address;
350 } ambient14_t;
351 
356 typedef struct
357 {
358  pin_name_t scl;
359  pin_name_t sda;
361  pin_name_t sby;
362  pin_name_t int_pin;
364  uint32_t i2c_speed;
365  uint8_t i2c_address;
368 
373 typedef enum
374 {
376  AMBIENT14_ERROR = -1
377 
379 
396 
411 
425 
440 err_t ambient14_write_regs ( ambient14_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
441 
456 err_t ambient14_read_regs ( ambient14_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
457 
470 err_t ambient14_write_reg ( ambient14_t *ctx, uint8_t reg, uint8_t data_in );
471 
484 err_t ambient14_read_reg ( ambient14_t *ctx, uint8_t reg, uint8_t *data_out );
485 
495 
505 
515 
528 
540 
552 
566 err_t ambient14_set_atime ( ambient14_t *ctx, float atime_ms );
567 
580 err_t ambient14_set_awtime ( ambient14_t *ctx, float awtime_ms );
581 
594 err_t ambient14_set_pwtime ( ambient14_t *ctx, float pwtime_ms );
595 
607 err_t ambient14_read_proximity ( ambient14_t *ctx, uint16_t *prox_data );
608 
621 err_t ambient14_read_als_ir ( ambient14_t *ctx, uint16_t *als_data, uint16_t *ir_data );
622 
635 err_t ambient14_get_illuminance ( ambient14_t *ctx, uint16_t als_data, float *illuminance );
636 
637 #ifdef __cplusplus
638 }
639 #endif
640 #endif // AMBIENT14_H
641  // ambient14
643 
644 // ------------------------------------------------------------------------ END
ambient14_write_reg
err_t ambient14_write_reg(ambient14_t *ctx, uint8_t reg, uint8_t data_in)
Ambient 14 write reg function.
ambient14_cfg_t::scl
pin_name_t scl
Definition: ambient14.h:358
ambient14_default_cfg
err_t ambient14_default_cfg(ambient14_t *ctx)
Ambient 14 default configuration function.
AMBIENT14_OK
@ AMBIENT14_OK
Definition: ambient14.h:375
ambient14_t::slave_address
uint8_t slave_address
Definition: ambient14.h:348
ambient14_write_regs
err_t ambient14_write_regs(ambient14_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Ambient 14 write regs function.
ambient14_get_int_pin
uint8_t ambient14_get_int_pin(ambient14_t *ctx)
Ambient 14 get int pin function.
ambient14_get_illuminance
err_t ambient14_get_illuminance(ambient14_t *ctx, uint16_t als_data, float *illuminance)
Ambient 14 get illuminance function.
ambient14_return_value_t
ambient14_return_value_t
Ambient 14 Click return value data.
Definition: ambient14.h:374
ambient14_set_pwtime
err_t ambient14_set_pwtime(ambient14_t *ctx, float pwtime_ms)
Ambient 14 set pwtime function.
AMBIENT14_ERROR
@ AMBIENT14_ERROR
Definition: ambient14.h:376
ambient14_t::sby
digital_out_t sby
Definition: ambient14.h:339
ambient14_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: ambient14.h:364
ambient14_read_proximity
err_t ambient14_read_proximity(ambient14_t *ctx, uint16_t *prox_data)
Ambient 14 read proximity function.
ambient14_cfg_t
Ambient 14 Click configuration object.
Definition: ambient14.h:357
ambient14_set_awtime
err_t ambient14_set_awtime(ambient14_t *ctx, float awtime_ms)
Ambient 14 set awtime function.
ambient14_read_reg
err_t ambient14_read_reg(ambient14_t *ctx, uint8_t reg, uint8_t *data_out)
Ambient 14 read reg function.
ambient14_t
Ambient 14 Click context object.
Definition: ambient14.h:337
ambient14_t::i2c
i2c_master_t i2c
Definition: ambient14.h:345
ambient14_set_atime
err_t ambient14_set_atime(ambient14_t *ctx, float atime_ms)
Ambient 14 set atime function.
ambient14_disable_device
void ambient14_disable_device(ambient14_t *ctx)
Ambient 14 disable device function.
ambient14_cfg_t::i2c_address
uint8_t i2c_address
Definition: ambient14.h:365
ambient14_read_regs
err_t ambient14_read_regs(ambient14_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Ambient 14 read regs function.
ambient14_cfg_t::sby
pin_name_t sby
Definition: ambient14.h:361
ambient14_check_communication
err_t ambient14_check_communication(ambient14_t *ctx)
Ambient 14 check communication function.
ambient14_init
err_t ambient14_init(ambient14_t *ctx, ambient14_cfg_t *cfg)
Ambient 14 initialization function.
ambient14_clear_interrupts
err_t ambient14_clear_interrupts(ambient14_t *ctx)
Ambient 14 clear interrupts function.
ambient14_cfg_t::sda
pin_name_t sda
Definition: ambient14.h:359
ambient14_soft_reset
err_t ambient14_soft_reset(ambient14_t *ctx)
Ambient 14 soft reset function.
ambient14_enable_device
void ambient14_enable_device(ambient14_t *ctx)
Ambient 14 enable device function.
ambient14_cfg_t::int_pin
pin_name_t int_pin
Definition: ambient14.h:362
ambient14_t::int_pin
digital_in_t int_pin
Definition: ambient14.h:342
ambient14_read_als_ir
err_t ambient14_read_als_ir(ambient14_t *ctx, uint16_t *als_data, uint16_t *ir_data)
Ambient 14 read als ir function.
ambient14_cfg_setup
void ambient14_cfg_setup(ambient14_cfg_t *cfg)
Ambient 14 configuration object setup function.