uv4  2.0.0.0
uv4.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef UV4_H
36 #define UV4_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_i2c_master.h"
51 
52 // -------------------------------------------------------------- PUBLIC MACROS
62 #define UV4_MAP_MIKROBUS( cfg, mikrobus ) \
63  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
64  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
65  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
66 
72 #define UV4_RETVAL uint8_t
73 
74 #define UV4_OK 0x00
75 #define UV4_INIT_ERROR 0xFF
76 
82 #define UV4_PART_ID_REG 0x00
83 #define UV4_HW_ID_REG 0x01
84 #define UV4_REV_ID_REG 0x02
85 #define UV4_INFO_0_REG 0x03
86 #define UV4_INFO_1_REG 0x04
87 #define UV4_INPUT_3_REG 0x07
88 #define UV4_INPUT_2_REG 0x08
89 #define UV4_INPUT_1_REG 0x09
90 #define UV4_INPUT_0_REG 0x0A
91 #define UV4_COMMAND_REG 0x0B
92 #define UV4_IRQ_ENABLE_REG 0x0F
93 #define UV4_RESPONSE_1_REG 0x10
94 #define UV4_RESPONSE_0_REG 0x11
95 #define UV4_IRQ_STATUS_REG 0x12
96 #define UV4_OUTPUT_0_REG 0x13
97 #define UV4_OUTPUT_1_REG 0x14
98 #define UV4_OUTPUT_2_REG 0x15
99 #define UV4_OUTPUT_3_REG 0x16
100 #define UV4_OUTPUT_4_REG 0x17
101 #define UV4_OUTPUT_5_REG 0x18
102 #define UV4_OUTPUT_6_REG 0x19
103 #define UV4_OUTPUT_7_REG 0x1A
104 #define UV4_OUTPUT_8_REG 0x1B
105 #define UV4_OUTPUT_9_REG 0x1C
106 #define UV4_OUTPUT_10_REG 0x1D
107 #define UV4_OUTPUT_11_REG 0x1E
108 #define UV4_OUTPUT_12_REG 0x1F
109 #define UV4_OUTPUT_13_REG 0x20
110 #define UV4_OUTPUT_14_REG 0x21
111 #define UV4_OUTPUT_15_REG 0x22
112 #define UV4_OUTPUT_16_REG 0x23
113 #define UV4_OUTPUT_17_REG 0x24
114 #define UV4_OUTPUT_18_REG 0x25
115 #define UV4_OUTPUT_19_REG 0x26
116 #define UV4_OUTPUT_20_REG 0x27
117 #define UV4_OUTPUT_21_REG 0x28
118 #define UV4_OUTPUT_22_REG 0x29
119 #define UV4_OUTPUT_23_REG 0x2A
120 #define UV4_OUTPUT_24_REG 0x2B
121 #define UV4_OUTPUT_25_REG 0x2C
122 
128 #define UV4_I2C_ADDR_PARAM 0x00
129 #define UV4_CHAN_LIST_PARAM 0x01
130 #define UV4_ADCCONFIG_0_PARAM 0x02
131 #define UV4_ADCSENS_0_PARAM 0x03
132 #define UV4_ADCPOST_0_PARAM 0x04
133 #define UV4_MEASCONFIG_0_PARAM 0x05
134 #define UV4_ADCCONFIG_1_PARAM 0x06
135 #define UV4_ADCSENS_1_PARAM 0x07
136 #define UV4_ADCPOST_1_PARAM 0x08
137 #define UV4_MEASCONFIG_1_PARAM 0x09
138 #define UV4_ADCCONFIG_2_PARAM 0x0A
139 #define UV4_ADCSENS_2_PARAM 0x0B
140 #define UV4_ADCPOST_2_PARAM 0x0C
141 #define UV4_MEASCONFIG_2_PARAM 0x0D
142 #define UV4_ADCCONFIG_3_PARAM 0x0E
143 #define UV4_ADCSENS_3_PARAM 0x0F
144 #define UV4_ADCPOST_3_PARAM 0x10
145 #define UV4_MEASCONFIG_3_PARAM 0x11
146 #define UV4_ADCCONFIG_4_PARAM 0x12
147 #define UV4_ADCSENS_4_PARAM 0x13
148 #define UV4_ADCPOST_4_PARAM 0x14
149 #define UV4_MEASCONFIG_4_PARAM 0x15
150 #define UV4_ADCCONFIG_5_PARAM 0x16
151 #define UV4_ADCSENS_5_PARAM 0x17
152 #define UV4_ADCPOST_5_PARAM 0x18
153 #define UV4_MEASCONFIG_5_PARAM 0x19
154 #define UV4_MEASRATE_H_PARAM 0x1A
155 #define UV4_MEASRATE_L_PARAM 0x1B
156 #define UV4_MEASCOUNT_0_PARAM 0x1C
157 #define UV4_MEASCOUNT_1_PARAM 0x1D
158 #define UV4_MEASCOUNT_2_PARAM 0x1E
159 #define UV4_THRESHOLD0_H_PARAM 0x25
160 #define UV4_THRESHOLD0_L_PARAM 0x26
161 #define UV4_THRESHOLD1_H_PARAM 0x27
162 #define UV4_THRESHOLD1_L_PARAM 0x28
163 #define UV4_THRESHOLD2_H_PARAM 0x29
164 #define UV4_THRESHOLD2_L_PARAM 0x2A
165 #define UV4_BURST_PARAM 0x2B
166 
172 #define UV4_RESET_CTR_CMD 0x00
173 #define UV4_RESET_SW_CMD 0x01
174 #define UV4_FORCE_CMD 0x11
175 #define UV4_PAUSE_CMD 0x12
176 #define UV4_START_CMD 0x13
177 
183 #define UV4_ALL_INTERR_DISABLE 0x00
184 #define UV4_CHAN0_INTERR_ENABLE 0x01
185 #define UV4_CHAN1_INTERR_ENABLE 0x02
186 #define UV4_CHAN2_INTERR_ENABLE 0x04
187 #define UV4_CHAN3_INTERR_ENABLE 0x08
188 #define UV4_CHAN4_INTERR_ENABLE 0x10
189 #define UV4_CHAN5_INTERR_ENABLE 0x20
190 
196 #define UV4_ALL_CHANNELS_DISABLE 0x00
197 #define UV4_CHAN0_ENABLE 0x01
198 #define UV4_CHAN1_ENABLE 0x02
199 #define UV4_CHAN2_ENABLE 0x04
200 #define UV4_CHAN3_ENABLE 0x08
201 #define UV4_CHAN4_ENABLE 0x10
202 #define UV4_CHAN5_ENABLE 0x20
203 
209 #define UV4_DATARATE_1024 0x00
210 #define UV4_DATARATE_2048 0x20
211 #define UV4_DATARATE_4096 0x40
212 #define UV4_DATARATE_512 0x60
213 
219 #define UV4_OPT_FUNC_SMALL_IR 0x00
220 #define UV4_OPT_FUNC_MEDIUM_IR 0x01
221 #define UV4_OPT_FUNC_LARGE_IR 0x02
222 #define UV4_OPT_FUNC_WHITE 0x0B
223 #define UV4_OPT_FUNC_LARGE_WHITE 0x0D
224 #define UV4_OPT_FUNC_UV 0x18
225 #define UV4_OPT_FUNC_UV_DEEP 0x19
226 
232 #define UV4_NORMAL_GAIN 0x00
233 #define UV4_HIGH_RANGE 0x80
234 #define UV4_1_MEASUREMENT 0x00
235 #define UV4_2_MEASUREMENTS 0x10
236 #define UV4_4_MEASUREMENTS 0x20
237 #define UV4_8_MEASUREMENTS 0x30
238 #define UV4_16_MEASUREMENTS 0x40
239 #define UV4_32_MEASUREMENTS 0x50
240 #define UV4_64_MEASUREMENTS 0x60
241 #define UV4_128_MEASUREMENTS 0x70
242 #define UV4_MEASTIME_24MICROSEC 0x00
243 #define UV4_MEASTIME_48MICROSEC 0x01
244 #define UV4_MEASTIME_96MICROSEC 0x02
245 #define UV4_MEASTIME_25MILISEC 0x0A
246 #define UV4_MEASTIME_50MILISEC 0x0B
247 
253 #define UV4_16BIT_OUT 0x00
254 #define UV4_24BIT_OUT 0x40
255 #define UV4_NO_SHIFT_RIGHT 0x00
256 #define UV4_1BIT_SHIFT_RIGHT 0x08
257 #define UV4_2BITS_SHIFT_RIGHT 0x10
258 #define UV4_3BITS_SHIFT_RIGHT 0x18
259 #define UV4_4BITS_SHIFT_RIGHT 0x20
260 
266 #define UV4_THRESHOLDS_DISABLE 0x00
267 #define UV4_THRESHOLD0_INTERR_ENABLE 0x01
268 #define UV4_THRESHOLD1_INTERR_ENABLE 0x02
269 #define UV4_THRESHOLD2_INTERR_ENABLE 0x03
270 
276 #define UV4_NO_COUNTER_SELECTED 0x00
277 #define UV4_MEASCOUNT1_SELECT 0x40
278 #define UV4_MEASCOUNT2_SELECT 0x80
279 #define UV4_MEASCOUNT3_SELECT 0xC0
280 
286 #define UV4_BURST_DISABLE 0x00
287 #define UV4_BURST_ENABLE 0x80
288 
294 #define UV4_AUTOINC_ENABLE 0x00
295 #define UV4_AUTOINC_DISABLE 0x40
296 
302 #define UV4_OK 0x00
303 #define UV4_ADDR_ERR 0x01
304 #define UV4_PARAM_ERR 0x02
305 #define UV4_CMMND_ERR 0x03
306 #define UV4_LOC_ERR 0x04
307 #define UV4_ACCM_OVERFLOW 0x05
308 #define UV4_BUFF_OVERFLOW 0x06
309 #define UV4_RST_OCCURED 0x07
310 #define UV4_INTERR_OCCURED 0xFF
311 #define UV4_NO_INTERR 0x00
312  // End group macro
315 // --------------------------------------------------------------- PUBLIC TYPES
324 typedef struct
325 {
326 
327  digital_in_t int_pin;
328 
329  // Modules
330 
331  i2c_master_t i2c;
332 
333  // ctx variable
334 
335  uint8_t slave_address;
336 
337  uint8_t config_byte;
338 
339  uint8_t optical_data[ 6 ];
340 
341  uint8_t tmp;
342  uint8_t bit_size[ 6 ];
343  uint8_t inc_en;
344 
345 } uv4_t;
346 
350 typedef struct
351 {
352  // Communication gpio pins
353 
354  pin_name_t scl;
355  pin_name_t sda;
356 
357  // Additional gpio pins
358 
359  pin_name_t int_pin;
360 
361  // static variable
362 
363  uint32_t i2c_speed;
364  uint8_t i2c_address;
365 
366  uint8_t config_byte;
367  uint8_t optical_data[ 6 ];
368 
369  uint8_t dev_tmp;
370 
371 } uv4_cfg_t;
372  // End types group
374 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
375 
381 #ifdef __cplusplus
382 extern "C"{
383 #endif
384 
393 void uv4_cfg_setup ( uv4_cfg_t *cfg );
394 
404 
412 void uv4_default_cfg ( uv4_t* ctx );
413 
424 UV4_RETVAL uv4_write_reg ( uv4_t* ctx, const uint8_t reg_address, const uint8_t transfer_data );
425 
439 UV4_RETVAL uv4_read_reg ( uv4_t* ctx, uint8_t reg_address, uint8_t* data_out,
440  uint8_t n_bytes );
441 
455 UV4_RETVAL uv4_write_param_data ( uv4_t* ctx, uint8_t param_address, uint8_t transfer_data );
456 
470 UV4_RETVAL uv4_read_param_data(uv4_t* ctx, uint8_t param_address, uint8_t* param_out);
471 
482 void uv4_read_measurements(uv4_t* ctx, uint32_t* measure_out);
483 
497 UV4_RETVAL uv4_check_err_and_ctr(uv4_t* ctx, uint8_t* cnt_data);
498 
509 
510 #ifdef __cplusplus
511 }
512 #endif
513 #endif // _UV4_H_
514  // End public_function group
517 
518 // ------------------------------------------------------------------------- END
uv4_t::int_pin
digital_in_t int_pin
Definition: uv4.h:327
uv4_cfg_t::config_byte
uint8_t config_byte
Definition: uv4.h:366
uv4_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: uv4.h:363
uv4_cfg_setup
void uv4_cfg_setup(uv4_cfg_t *cfg)
Config Object Initialization function.
uv4_t::slave_address
uint8_t slave_address
Definition: uv4.h:335
uv4_cfg_t::scl
pin_name_t scl
Definition: uv4.h:354
uv4_t
Click ctx object definition.
Definition: uv4.h:325
uv4_t::inc_en
uint8_t inc_en
Definition: uv4.h:343
uv4_cfg_t::int_pin
pin_name_t int_pin
Definition: uv4.h:359
uv4_write_reg
UV4_RETVAL uv4_write_reg(uv4_t *ctx, const uint8_t reg_address, const uint8_t transfer_data)
Generic write function.
uv4_read_reg
UV4_RETVAL uv4_read_reg(uv4_t *ctx, uint8_t reg_address, uint8_t *data_out, uint8_t n_bytes)
Generic read function.
uv4_t::tmp
uint8_t tmp
Definition: uv4.h:341
uv4_cfg_t
Click configuration structure definition.
Definition: uv4.h:351
uv4_read_param_data
UV4_RETVAL uv4_read_param_data(uv4_t *ctx, uint8_t param_address, uint8_t *param_out)
Parameter Data Read Function.
uv4_cfg_t::sda
pin_name_t sda
Definition: uv4.h:355
UV4_RETVAL
#define UV4_RETVAL
Definition: uv4.h:72
uv4_check_err_and_ctr
UV4_RETVAL uv4_check_err_and_ctr(uv4_t *ctx, uint8_t *cnt_data)
Error And Counter Check function.
uv4_cfg_t::dev_tmp
uint8_t dev_tmp
Definition: uv4.h:369
uv4_check_interr
UV4_RETVAL uv4_check_interr(uv4_t *ctx)
Error And Counter Check function.
uv4_cfg_t::i2c_address
uint8_t i2c_address
Definition: uv4.h:364
uv4_write_param_data
UV4_RETVAL uv4_write_param_data(uv4_t *ctx, uint8_t param_address, uint8_t transfer_data)
Parameter Data Write Function.
uv4_read_measurements
void uv4_read_measurements(uv4_t *ctx, uint32_t *measure_out)
Measurements Read Function.
uv4_t::config_byte
uint8_t config_byte
Definition: uv4.h:337
uv4_default_cfg
void uv4_default_cfg(uv4_t *ctx)
Click Default Configuration function.
uv4_init
UV4_RETVAL uv4_init(uv4_t *ctx, uv4_cfg_t *cfg)
Initialization function.
uv4_t::i2c
i2c_master_t i2c
Definition: uv4.h:331