ambient21  2.1.0.0
ambient21.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 AMBIENT21_H
29 #define AMBIENT21_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "drv_digital_out.h"
36 #include "drv_digital_in.h"
37 #include "drv_i2c_master.h"
38 
59 #define AMBIENT21_REG_ENABLE 0x00
60 #define AMBIENT21_REG_CONFIG 0x01
61 #define AMBIENT21_REG_AILTL 0x04
62 #define AMBIENT21_REG_AILTH 0x05
63 #define AMBIENT21_REG_AIHTL 0x06
64 #define AMBIENT21_REG_AIHTH 0x07
65 #define AMBIENT21_REG_NPAILTL 0x08
66 #define AMBIENT21_REG_NPAILTH 0x09
67 #define AMBIENT21_REG_NPAIHTL 0x0A
68 #define AMBIENT21_REG_NPAIHTH 0x0B
69 #define AMBIENT21_REG_PERSIST 0x0C
70 #define AMBIENT21_REG_PID 0x11
71 #define AMBIENT21_REG_ID 0x12
72 #define AMBIENT21_REG_STATUS 0x13
73 #define AMBIENT21_REG_C0DATAL 0x14
74 #define AMBIENT21_REG_C0DATAH 0x15
75 #define AMBIENT21_REG_C1DATAL 0x16
76 #define AMBIENT21_REG_C1DATAH 0x17
77  // ambient21_reg
79 
94 #define AMBIENT21_CMD_NORMAL_OP 0xA0
95 #define AMBIENT21_CMD_SPEC_FUNC 0xE0
96 #define AMBIENT21_SF_INT_SET 0x04
97 #define AMBIENT21_SF_CLEAR_ALS_INT 0x06
98 #define AMBIENT21_SF_CLEAR_ALS_NOPERS_INT 0x07
99 #define AMBIENT21_SF_CLEAR_NOPERS_INT 0x0A
100 #define AMBIENT21_ADDR_SF_MASK 0x1F
101 
106 #define AMBIENT21_ENABLE_NPIEN 0x80
107 #define AMBIENT21_ENABLE_SAI 0x40
108 #define AMBIENT21_ENABLE_AIEN 0x10
109 #define AMBIENT21_ENABLE_AEN 0x02
110 #define AMBIENT21_ENABLE_PON 0x01
111 
116 #define AMBIENT21_CONFIG_SRESET 0x80
117 #define AMBIENT21_CONFIG_AGAIN_LOW 0x00
118 #define AMBIENT21_CONFIG_AGAIN_MID 0x10
119 #define AMBIENT21_CONFIG_AGAIN_HIGH 0x20
120 #define AMBIENT21_CONFIG_AGAIN_MAX 0x30
121 #define AMBIENT21_CONFIG_AGAIN_MASK 0x30
122 #define AMBIENT21_CONFIG_ATIME_100MS 0x00
123 #define AMBIENT21_CONFIG_ATIME_200MS 0x01
124 #define AMBIENT21_CONFIG_ATIME_300MS 0x02
125 #define AMBIENT21_CONFIG_ATIME_400MS 0x03
126 #define AMBIENT21_CONFIG_ATIME_500MS 0x04
127 #define AMBIENT21_CONFIG_ATIME_600MS 0x05
128 #define AMBIENT21_CONFIG_ATIME_MASK 0x07
129 
134 #define AMBIENT21_PERSIST_EVERY 0x00
135 #define AMBIENT21_PERSIST_ANY 0x01
136 #define AMBIENT21_PERSIST_2 0x02
137 #define AMBIENT21_PERSIST_3 0x03
138 #define AMBIENT21_PERSIST_5 0x04
139 #define AMBIENT21_PERSIST_10 0x05
140 #define AMBIENT21_PERSIST_15 0x06
141 #define AMBIENT21_PERSIST_20 0x07
142 #define AMBIENT21_PERSIST_25 0x08
143 #define AMBIENT21_PERSIST_30 0x09
144 #define AMBIENT21_PERSIST_35 0x0A
145 #define AMBIENT21_PERSIST_40 0x0B
146 #define AMBIENT21_PERSIST_45 0x0C
147 #define AMBIENT21_PERSIST_50 0x0D
148 #define AMBIENT21_PERSIST_55 0x0E
149 #define AMBIENT21_PERSIST_60 0x0F
150 #define AMBIENT21_PERSIST_MASK 0x0F
151 
156 #define AMBIENT21_STATUS_NPINTR 0x20
157 #define AMBIENT21_STATUS_AINT 0x10
158 #define AMBIENT21_STATUS_AVALID 0x01
159 
164 #define AMBIENT21_DEVICE_ID 0x50
165 
170 #define AMBIENT21_AGAIN_LOW 0x00
171 #define AMBIENT21_AGAIN_MID 0x01
172 #define AMBIENT21_AGAIN_HIGH 0x02
173 #define AMBIENT21_AGAIN_MAX 0x03
174 #define AMBIENT21_ATIME_100MS 0x00
175 #define AMBIENT21_ATIME_200MS 0x01
176 #define AMBIENT21_ATIME_300MS 0x02
177 #define AMBIENT21_ATIME_400MS 0x03
178 #define AMBIENT21_ATIME_500MS 0x04
179 #define AMBIENT21_ATIME_600MS 0x05
180 
185 #define AMBIENT21_GAIN_0 1.0f
186 #define AMBIENT21_GAIN_1 25.0f
187 #define AMBIENT21_GAIN_2 428.0f
188 #define AMBIENT21_GAIN_3 9876.0f
189 #define AMBIENT21_TIME_RES 100.0f
190 #define AMBIENT21_LUX_GDF 900.0f
191 #define AMBIENT21_LUX_COEF 1.0f
192 
198 #define AMBIENT21_DEVICE_ADDRESS 0x29
199  // ambient21_set
201 
216 #define AMBIENT21_MAP_MIKROBUS( cfg, mikrobus ) \
217  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
218  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
219  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
220  // ambient21_map // ambient21
223 
228 typedef struct
229 {
230  // Input pins
231  digital_in_t int_pin;
233  // Modules
234  i2c_master_t i2c;
236  // I2C slave address
237  uint8_t slave_address;
239  float atime_ms;
240  float again;
241  float cpl;
243 } ambient21_t;
244 
249 typedef struct
250 {
251  pin_name_t scl;
252  pin_name_t sda;
254  pin_name_t int_pin;
256  uint32_t i2c_speed;
257  uint8_t i2c_address;
260 
265 typedef enum
266 {
268  AMBIENT21_ERROR = -1
269 
271 
288 
303 
317 
332 err_t ambient21_generic_write ( ambient21_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
333 
348 err_t ambient21_generic_read ( ambient21_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
349 
362 err_t ambient21_write_register ( ambient21_t *ctx, uint8_t reg, uint8_t data_in );
363 
376 err_t ambient21_read_register ( ambient21_t *ctx, uint8_t reg, uint8_t *data_out );
377 
389 err_t ambient21_write_spec_func ( ambient21_t *ctx, uint8_t spec_func );
390 
400 
412 
424 
447 err_t ambient21_set_config ( ambient21_t *ctx, uint8_t again, uint8_t atime );
448 
461 err_t ambient21_read_raw_data ( ambient21_t *ctx, uint16_t *ch_0, uint16_t *ch_1 );
462 
476 err_t ambient21_measure_light_level ( ambient21_t *ctx, uint16_t *lux );
477 
478 #ifdef __cplusplus
479 }
480 #endif
481 #endif // AMBIENT21_H
482  // ambient21
484 
485 // ------------------------------------------------------------------------ END
ambient21_measure_light_level
err_t ambient21_measure_light_level(ambient21_t *ctx, uint16_t *lux)
Ambient 21 measure light level function.
ambient21_cfg_t::i2c_address
uint8_t i2c_address
Definition: ambient21.h:257
AMBIENT21_OK
@ AMBIENT21_OK
Definition: ambient21.h:267
ambient21_cfg_t
Ambient 21 Click configuration object.
Definition: ambient21.h:250
ambient21_write_spec_func
err_t ambient21_write_spec_func(ambient21_t *ctx, uint8_t spec_func)
Ambient 21 write spec func function.
ambient21_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: ambient21.h:256
ambient21_t::int_pin
digital_in_t int_pin
Definition: ambient21.h:231
ambient21_generic_write
err_t ambient21_generic_write(ambient21_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Ambient 21 I2C writing function.
ambient21_t::again
float again
Definition: ambient21.h:240
ambient21_cfg_t::int_pin
pin_name_t int_pin
Definition: ambient21.h:254
ambient21_set_config
err_t ambient21_set_config(ambient21_t *ctx, uint8_t again, uint8_t atime)
Ambient 21 set config function.
ambient21_cfg_setup
void ambient21_cfg_setup(ambient21_cfg_t *cfg)
Ambient 21 configuration object setup function.
ambient21_get_int_pin
uint8_t ambient21_get_int_pin(ambient21_t *ctx)
Ambient 21 get int pin function.
ambient21_read_register
err_t ambient21_read_register(ambient21_t *ctx, uint8_t reg, uint8_t *data_out)
Ambient 21 read register function.
ambient21_t::atime_ms
float atime_ms
Definition: ambient21.h:239
ambient21_write_register
err_t ambient21_write_register(ambient21_t *ctx, uint8_t reg, uint8_t data_in)
Ambient 21 write register function.
ambient21_t::cpl
float cpl
Definition: ambient21.h:241
ambient21_cfg_t::scl
pin_name_t scl
Definition: ambient21.h:251
ambient21_init
err_t ambient21_init(ambient21_t *ctx, ambient21_cfg_t *cfg)
Ambient 21 initialization function.
ambient21_clear_interrupts
err_t ambient21_clear_interrupts(ambient21_t *ctx)
Ambient 21 clear interrupts function.
ambient21_t::i2c
i2c_master_t i2c
Definition: ambient21.h:234
ambient21_read_raw_data
err_t ambient21_read_raw_data(ambient21_t *ctx, uint16_t *ch_0, uint16_t *ch_1)
Ambient 21 read raw data function.
ambient21_t::slave_address
uint8_t slave_address
Definition: ambient21.h:237
ambient21_return_value_t
ambient21_return_value_t
Ambient 21 Click return value data.
Definition: ambient21.h:266
ambient21_default_cfg
err_t ambient21_default_cfg(ambient21_t *ctx)
Ambient 21 default configuration function.
ambient21_check_communication
err_t ambient21_check_communication(ambient21_t *ctx)
Ambient 21 check communication function.
ambient21_cfg_t::sda
pin_name_t sda
Definition: ambient21.h:252
ambient21_generic_read
err_t ambient21_generic_read(ambient21_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Ambient 21 I2C reading function.
AMBIENT21_ERROR
@ AMBIENT21_ERROR
Definition: ambient21.h:268
ambient21_t
Ambient 21 Click context object.
Definition: ambient21.h:229