color9  2.0.0.0
color9.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 COLOR9_H
36 #define COLOR9_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 
53 // -------------------------------------------------------------- PUBLIC MACROS
59 #define COLOR9_SLAVE_ADDR 0x52
60 #define COLOR9_MAIN_CTRL 0x00
61 #define COLOR9_LS_MEAS_RATE 0x04
62 #define COLOR9_LS_GAIN 0x05
63 #define COLOR9_PART_ID 0x06
64 #define COLOR9_MAIN_STATUS 0x07
65 #define COLOR9_LS_DATA_IR_0 0x0A
66 #define COLOR9_LS_DATA_IR_1 0x0B
67 #define COLOR9_LS_DATA_IR_2 0x0C
68 #define COLOR9_LS_DATA_GREEN_0 0x0D
69 #define COLOR9_LS_DATA_GREEN_1 0x0E
70 #define COLOR9_LS_DATA_GREEN_2 0x0F
71 #define COLOR9_LS_DATA_BLUE_0 0x10
72 #define COLOR9_LS_DATA_BLUE_1 0x11
73 #define COLOR9_LS_DATA_BLUE_2 0x12
74 #define COLOR9_LS_DATA_RED_0 0x13
75 #define COLOR9_LS_DATA_RED_1 0x14
76 #define COLOR9_LS_DATA_RED_2 0x15
77 #define COLOR9_INT_CFG 0x19
78 #define COLOR9_INT_PERSISTENCE 0x1A
79 #define COLOR9_LS_THRES_UP_0 0x21
80 #define COLOR9_LS_THRES_UP_1 0x22
81 #define COLOR9_LS_THRES_UP_2 0x23
82 #define COLOR9_LS_THRES_LOW_0 0x24
83 #define COLOR9_LS_THRES_LOW_1 0x25
84 #define COLOR9_LS_THRES_LOW_2 0x26
85 #define COLOR9_LS_THRES_VAR 0x27
86 
87 
92 #define COLOR9_MAIN_CTRL_SW_RESET 0x10
93 #define COLOR9_MAIN_CTRL_CS_MODE 0x04
94 #define COLOR9_MAIN_CTRL_LS_EN 0x02
95  // End group main_ctrl
96 
97 
102 #define COLOR9_LS_MEAS_BITWIDTH_20 0x00
103 #define COLOR9_LS_MEAS_BITWIDTH_19 0x01
104 #define COLOR9_LS_MEAS_BITWIDTH_18 0x02
105 #define COLOR9_LS_MEAS_BITWIDTH_17 0x03
106 #define COLOR9_LS_MEAS_BITWIDTH_16 0x04
107 #define COLOR9_LS_MEAS_BITWIDTH_13 0x05
108 
109 #define COLOR9_LS_MEAS_RATE_25ms 0x00
110 #define COLOR9_LS_MEAS_RATE_50ms 0x01
111 #define COLOR9_LS_MEAS_RATE_100ms 0x02
112 #define COLOR9_LS_MEAS_RATE_200ms 0x03
113 #define COLOR9_LS_MEAS_RATE_500ms 0x04
114 #define COLOR9_LS_MEAS_RATE_1000ms 0x05
115 #define COLOR9_LS_MEAS_RATE_2000ms 0x06
116  // End group ls_meas_rate
117 
122 #define COLOR9_LS_GAIN_1 0x00
123 #define COLOR9_LS_GAIN_3 0x01
124 #define COLOR9_LS_GAIN_6 0x02
125 #define COLOR9_LS_GAIN_9 0x03
126 #define COLOR9_LS_GAIN_18 0x04
127  // End group ls_gain
128 
133 #define COLOR9_INT_CFG_IR_CH 0x00
134 #define COLOR9_INT_CFG_ALS_GREEN_CH 0x01
135 #define COLOR9_INT_CFG_RED_CH 0x02
136 #define COLOR9_INT_CFG_BLUE_CH 0x03
137 #define COLOR9_INT_CFG_LS_THRESHOLD_INT_MODE 0x00
138 #define COLOR9_INT_CFG_LS_VARIATION_INT_MODE 0x01
139 #define COLOR9_INT_CFG_LS_INT_ENABLE 0x01
140 #define COLOR9_INT_CFG_LS_INT_DISABLE 0x00
141  // End group int_cfg
142 
147 #define COLOR9_INT_PERSISTENCE_ANYVAL_TRIGGER 0x00
148 #define COLOR9_INT_PERSISTENCE_SECOND_COUNT_TRIGGER 0x01
149 #define COLOR9_INT_PERSISTENCE_SIXTEEN_COUNT_TRIGGER 0x0F
150  // End group int_persistence
151 
156 #define COLOR9_LS_THRES_VAR_COUNT_8 0x00
157 #define COLOR9_LS_THRES_VAR_COUNT_16 0x01
158 #define COLOR9_LS_THRES_VAR_COUNT_32 0x02
159 #define COLOR9_LS_THRES_VAR_COUNT_64 0x03
160 #define COLOR9_LS_THRES_VAR_COUNT_1024 0x07
161  // End group ls_thres_var
162 
167 #define COLOR9_MAP_MIKROBUS( cfg, mikrobus ) \
168  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
169  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
170  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
171 
177 #define COLOR9_RETVAL uint8_t
178 
179 #define COLOR9_OK 0x00
180 #define COLOR9_INIT_ERROR 0xFF
181  // End group macro
184 // --------------------------------------------------------------- PUBLIC TYPES
193 typedef struct
194 {
195  digital_in_t int_pin;
196 
197  // Modules
198 
199  i2c_master_t i2c;
200 
201  // ctx variable
202 
203  uint8_t slave_address;
204 
205 } color9_t;
206 
210 typedef struct
211 {
212  // Communication gpio pins
213 
214  pin_name_t scl;
215  pin_name_t sda;
216 
217  // Additional gpio pins
218 
219  pin_name_t int_pin;
220 
221  // static variable
222 
223  uint32_t i2c_speed;
224  uint8_t i2c_address;
225 
226 } color9_cfg_t;
227  // End types group
229 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
230 
236 #ifdef __cplusplus
237 extern "C"{
238 #endif
239 
249 
259 
260 
261 
271 void color9_write ( color9_t* ctx, uint8_t reg_addr, uint8_t r_data );
272 
281 uint8_t color9_read ( color9_t* ctx, uint8_t reg_addr );
282 
289 void color9_sw_reset ( color9_t* ctx );
290 
300 uint32_t color9_get_Ir ( color9_t* ctx );
301 
311 uint32_t color9_get_green ( color9_t* ctx );
312 
322 uint32_t color9_get_blue ( color9_t* ctx );
323 
333 uint32_t color9_get_red ( color9_t* ctx );
334 
345 
355 void color9_meas_rate ( color9_t* ctx, uint8_t resolution, uint8_t meas_rate );
356 
365 void color9_reg_ctrl ( color9_t* ctx, uint8_t cfg );
366 
375 void color9_set_gain ( color9_t* ctx, uint8_t g_setting );
376 
387 void color9_int_config ( color9_t* ctx, uint8_t int_select, uint8_t int_mode,
388  uint8_t cfg_setting );
389 
398 void color9_sample_compare ( color9_t* ctx, uint8_t num_of_compares );
399 
408 uint32_t color9_read_32 ( color9_t* ctx, uint8_t reg_addr );
409 
419 void color9_write_32 ( color9_t* ctx, uint8_t reg_addr, uint32_t treas_val );
420 
429 void color9_similarity_count ( color9_t* ctx, uint8_t sim_set );
430 
439 void color9_upper_tresset ( color9_t* ctx, uint32_t set_val );
440 
449 void color9_lower_tresset ( color9_t* ctx, uint32_t set_val );
450 
451 #ifdef __cplusplus
452 }
453 #endif
454 #endif // _COLOR9_H_
455  // End public_function group
458 
459 // ------------------------------------------------------------------------- END
color9_read
uint8_t color9_read(color9_t *ctx, uint8_t reg_addr)
Generic read function.
color9_cfg_t
Click configuration structure definition.
Definition: color9.h:211
color9_get_blue
uint32_t color9_get_blue(color9_t *ctx)
Gets value of Blue measurement reading.
color9_t::slave_address
uint8_t slave_address
Definition: color9.h:203
color9_int_config
void color9_int_config(color9_t *ctx, uint8_t int_select, uint8_t int_mode, uint8_t cfg_setting)
Interrupt configuration.
color9_get_green
uint32_t color9_get_green(color9_t *ctx)
Gets value of Green measurement reading.
color9_write
void color9_write(color9_t *ctx, uint8_t reg_addr, uint8_t r_data)
Generic write function.
color9_meas_rate
void color9_meas_rate(color9_t *ctx, uint8_t resolution, uint8_t meas_rate)
Configure measurement rates.
color9_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: color9.h:223
color9_similarity_count
void color9_similarity_count(color9_t *ctx, uint8_t sim_set)
Similarity count.
COLOR9_RETVAL
#define COLOR9_RETVAL
Definition: color9.h:177
color9_lower_tresset
void color9_lower_tresset(color9_t *ctx, uint32_t set_val)
Lower Tresset.
color9_reg_ctrl
void color9_reg_ctrl(color9_t *ctx, uint8_t cfg)
Function for setting the main configuration register.
color9_get_status_reg
uint8_t color9_get_status_reg(color9_t *ctx)
Status register value.
color9_sample_compare
void color9_sample_compare(color9_t *ctx, uint8_t num_of_compares)
Configre number of compares between samples.
color9_read_32
uint32_t color9_read_32(color9_t *ctx, uint8_t reg_addr)
Generic read function for 32 bits.
color9_cfg_t::i2c_address
uint8_t i2c_address
Definition: color9.h:224
color9_t::i2c
i2c_master_t i2c
Definition: color9.h:199
color9_cfg_t::scl
pin_name_t scl
Definition: color9.h:214
color9_init
COLOR9_RETVAL color9_init(color9_t *ctx, color9_cfg_t *cfg)
Initialization function.
color9_cfg_t::int_pin
pin_name_t int_pin
Definition: color9.h:219
color9_get_red
uint32_t color9_get_red(color9_t *ctx)
Gets value of Red measurement reading.
color9_sw_reset
void color9_sw_reset(color9_t *ctx)
Software reset function.
color9_upper_tresset
void color9_upper_tresset(color9_t *ctx, uint32_t set_val)
Upper Tresset.
color9_t
Click ctx object definition.
Definition: color9.h:194
color9_t::int_pin
digital_in_t int_pin
Definition: color9.h:195
color9_set_gain
void color9_set_gain(color9_t *ctx, uint8_t g_setting)
Function for gain setting.
color9_get_Ir
uint32_t color9_get_Ir(color9_t *ctx)
Get value of IR measurement reading.
color9_write_32
void color9_write_32(color9_t *ctx, uint8_t reg_addr, uint32_t treas_val)
Generic write function for 32 bits.
color9_cfg_t::sda
pin_name_t sda
Definition: color9.h:215
color9_cfg_setup
void color9_cfg_setup(color9_cfg_t *cfg)
Config Object Initialization function.