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 "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 AMBIENT21_REG_ENABLE 0x00
70 #define AMBIENT21_REG_CONFIG 0x01
71 #define AMBIENT21_REG_AILTL 0x04
72 #define AMBIENT21_REG_AILTH 0x05
73 #define AMBIENT21_REG_AIHTL 0x06
74 #define AMBIENT21_REG_AIHTH 0x07
75 #define AMBIENT21_REG_NPAILTL 0x08
76 #define AMBIENT21_REG_NPAILTH 0x09
77 #define AMBIENT21_REG_NPAIHTL 0x0A
78 #define AMBIENT21_REG_NPAIHTH 0x0B
79 #define AMBIENT21_REG_PERSIST 0x0C
80 #define AMBIENT21_REG_PID 0x11
81 #define AMBIENT21_REG_ID 0x12
82 #define AMBIENT21_REG_STATUS 0x13
83 #define AMBIENT21_REG_C0DATAL 0x14
84 #define AMBIENT21_REG_C0DATAH 0x15
85 #define AMBIENT21_REG_C1DATAL 0x16
86 #define AMBIENT21_REG_C1DATAH 0x17
87  // ambient21_reg
89 
104 #define AMBIENT21_CMD_NORMAL_OP 0xA0
105 #define AMBIENT21_CMD_SPEC_FUNC 0xE0
106 #define AMBIENT21_SF_INT_SET 0x04
107 #define AMBIENT21_SF_CLEAR_ALS_INT 0x06
108 #define AMBIENT21_SF_CLEAR_ALS_NOPERS_INT 0x07
109 #define AMBIENT21_SF_CLEAR_NOPERS_INT 0x0A
110 #define AMBIENT21_ADDR_SF_MASK 0x1F
111 
116 #define AMBIENT21_ENABLE_NPIEN 0x80
117 #define AMBIENT21_ENABLE_SAI 0x40
118 #define AMBIENT21_ENABLE_AIEN 0x10
119 #define AMBIENT21_ENABLE_AEN 0x02
120 #define AMBIENT21_ENABLE_PON 0x01
121 
126 #define AMBIENT21_CONFIG_SRESET 0x80
127 #define AMBIENT21_CONFIG_AGAIN_LOW 0x00
128 #define AMBIENT21_CONFIG_AGAIN_MID 0x10
129 #define AMBIENT21_CONFIG_AGAIN_HIGH 0x20
130 #define AMBIENT21_CONFIG_AGAIN_MAX 0x30
131 #define AMBIENT21_CONFIG_AGAIN_MASK 0x30
132 #define AMBIENT21_CONFIG_ATIME_100MS 0x00
133 #define AMBIENT21_CONFIG_ATIME_200MS 0x01
134 #define AMBIENT21_CONFIG_ATIME_300MS 0x02
135 #define AMBIENT21_CONFIG_ATIME_400MS 0x03
136 #define AMBIENT21_CONFIG_ATIME_500MS 0x04
137 #define AMBIENT21_CONFIG_ATIME_600MS 0x05
138 #define AMBIENT21_CONFIG_ATIME_MASK 0x07
139 
144 #define AMBIENT21_PERSIST_EVERY 0x00
145 #define AMBIENT21_PERSIST_ANY 0x01
146 #define AMBIENT21_PERSIST_2 0x02
147 #define AMBIENT21_PERSIST_3 0x03
148 #define AMBIENT21_PERSIST_5 0x04
149 #define AMBIENT21_PERSIST_10 0x05
150 #define AMBIENT21_PERSIST_15 0x06
151 #define AMBIENT21_PERSIST_20 0x07
152 #define AMBIENT21_PERSIST_25 0x08
153 #define AMBIENT21_PERSIST_30 0x09
154 #define AMBIENT21_PERSIST_35 0x0A
155 #define AMBIENT21_PERSIST_40 0x0B
156 #define AMBIENT21_PERSIST_45 0x0C
157 #define AMBIENT21_PERSIST_50 0x0D
158 #define AMBIENT21_PERSIST_55 0x0E
159 #define AMBIENT21_PERSIST_60 0x0F
160 #define AMBIENT21_PERSIST_MASK 0x0F
161 
166 #define AMBIENT21_STATUS_NPINTR 0x20
167 #define AMBIENT21_STATUS_AINT 0x10
168 #define AMBIENT21_STATUS_AVALID 0x01
169 
174 #define AMBIENT21_DEVICE_ID 0x50
175 
180 #define AMBIENT21_AGAIN_LOW 0x00
181 #define AMBIENT21_AGAIN_MID 0x01
182 #define AMBIENT21_AGAIN_HIGH 0x02
183 #define AMBIENT21_AGAIN_MAX 0x03
184 #define AMBIENT21_ATIME_100MS 0x00
185 #define AMBIENT21_ATIME_200MS 0x01
186 #define AMBIENT21_ATIME_300MS 0x02
187 #define AMBIENT21_ATIME_400MS 0x03
188 #define AMBIENT21_ATIME_500MS 0x04
189 #define AMBIENT21_ATIME_600MS 0x05
190 
195 #define AMBIENT21_GAIN_0 1.0f
196 #define AMBIENT21_GAIN_1 25.0f
197 #define AMBIENT21_GAIN_2 428.0f
198 #define AMBIENT21_GAIN_3 9876.0f
199 #define AMBIENT21_TIME_RES 100.0f
200 #define AMBIENT21_LUX_GDF 900.0f
201 #define AMBIENT21_LUX_COEF 1.0f
202 
208 #define AMBIENT21_DEVICE_ADDRESS 0x29
209  // ambient21_set
211 
226 #define AMBIENT21_MAP_MIKROBUS( cfg, mikrobus ) \
227  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
228  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
229  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
230  // ambient21_map // ambient21
233 
238 typedef struct
239 {
240  // Input pins
241  digital_in_t int_pin;
243  // Modules
244  i2c_master_t i2c;
246  // I2C slave address
247  uint8_t slave_address;
249  float atime_ms;
250  float again;
251  float cpl;
253 } ambient21_t;
254 
259 typedef struct
260 {
261  pin_name_t scl;
262  pin_name_t sda;
264  pin_name_t int_pin;
266  uint32_t i2c_speed;
267  uint8_t i2c_address;
270 
275 typedef enum
276 {
278  AMBIENT21_ERROR = -1
279 
281 
298 
313 
327 
342 err_t ambient21_generic_write ( ambient21_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
343 
358 err_t ambient21_generic_read ( ambient21_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
359 
372 err_t ambient21_write_register ( ambient21_t *ctx, uint8_t reg, uint8_t data_in );
373 
386 err_t ambient21_read_register ( ambient21_t *ctx, uint8_t reg, uint8_t *data_out );
387 
399 err_t ambient21_write_spec_func ( ambient21_t *ctx, uint8_t spec_func );
400 
410 
422 
434 
457 err_t ambient21_set_config ( ambient21_t *ctx, uint8_t again, uint8_t atime );
458 
471 err_t ambient21_read_raw_data ( ambient21_t *ctx, uint16_t *ch_0, uint16_t *ch_1 );
472 
486 err_t ambient21_measure_light_level ( ambient21_t *ctx, uint16_t *lux );
487 
488 #ifdef __cplusplus
489 }
490 #endif
491 #endif // AMBIENT21_H
492  // ambient21
494 
495 // ------------------------------------------------------------------------ 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:267
AMBIENT21_OK
@ AMBIENT21_OK
Definition: ambient21.h:277
ambient21_cfg_t
Ambient 21 Click configuration object.
Definition: ambient21.h:260
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:266
ambient21_t::int_pin
digital_in_t int_pin
Definition: ambient21.h:241
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:250
ambient21_cfg_t::int_pin
pin_name_t int_pin
Definition: ambient21.h:264
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:249
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:251
ambient21_cfg_t::scl
pin_name_t scl
Definition: ambient21.h:261
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:244
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:247
ambient21_return_value_t
ambient21_return_value_t
Ambient 21 Click return value data.
Definition: ambient21.h:276
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:262
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:278
ambient21_t
Ambient 21 Click context object.
Definition: ambient21.h:239