irsense  2.0.0.0
irsense.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 IRSENSE_H
36 #define IRSENSE_H
37 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_out.h"
53 #include "drv_digital_in.h"
54 #include "drv_i2c_master.h"
55 
56 
57 // -------------------------------------------------------------- PUBLIC MACROS
67 #define IRSENSE_MAP_MIKROBUS( cfg, mikrobus ) \
68  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
69  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
70  cfg.pdn = MIKROBUS( mikrobus, MIKROBUS_CS ); \
71  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
72 
78 #define IRSENSE_RETVAL uint8_t
79 
80 #define IRSENSE_OK 0x00
81 #define IRSENSE_INIT_ERROR 0xFF
82 
88 #define IRSENSE_I2C_ADDRESS_0 0x64
89 #define IRSENSE_I2C_ADDRESS_1 0x65
90 #define IRSENSE_I2C_ADDRESS_2 0x66
91 
97 #define IRSENSE_REG_WIA1 0x00
98 #define IRSENSE_REG_WIA2 0x01
99 #define IRSENSE_REG_INFO1 0x02
100 #define IRSENSE_REG_INFO2 0x03
101 #define IRSENSE_REG_INTST 0x04
102 #define IRSENSE_REG_ST1 0x05
103 #define IRSENSE_REG_IR1_LOW 0x06
104 #define IRSENSE_REG_IR1_HIGH 0x07
105 #define IRSENSE_REG_IR2_LOW 0x08
106 #define IRSENSE_REG_IR2_HIGH 0x09
107 #define IRSENSE_REG_IR3_LOW 0x0A
108 #define IRSENSE_REG_IR3_HIGH 0x0B
109 #define IRSENSE_REG_IR4_LOW 0x0C
110 #define IRSENSE_REG_IR4_HIGH 0x0D
111 #define IRSENSE_REG_TMP_LOW 0x0E
112 #define IRSENSE_REG_TMP_HIGH 0x0F
113 #define IRSENSE_REG_ST2 0x10
114 #define IRSENSE_REG_ETH13H_LOW 0x11
115 #define IRSENSE_REG_ETH13H_HIGH 0x12
116 #define IRSENSE_REG_ETH13L_LOW 0x13
117 #define IRSENSE_REG_ETH13L_HIGH 0x14
118 #define IRSENSE_REG_ETH24H_LOW 0x15
119 #define IRSENSE_REG_ETH24H_HIGH 0x16
120 #define IRSENSE_REG_ETH24L_LOW 0x17
121 #define IRSENSE_REG_ETH24L_HIGH 0x18
122 #define IRSENSE_REG_EHYS13 0x19
123 #define IRSENSE_REG_EHYS24 0x1A
124 #define IRSENSE_REG_EINTEN 0x1B
125 #define IRSENSE_REG_ECNTL1 0x1C
126 #define IRSENSE_REG_CNTL2 0x1D
127 
133 #define IRSENSE_EEPROM_MAP_EKEY 0x50
134 #define IRSENSE_EEPROM_MAP_ETH13H_LOW 0x51
135 #define IRSENSE_EEPROM_MAP_ETH13H_HIGH 0x52
136 #define IRSENSE_EEPROM_MAP_ETH13L_LOW 0x53
137 #define IRSENSE_EEPROM_MAP_ETH13L_HIGH 0x54
138 #define IRSENSE_EEPROM_MAP_ETH24H_LOW 0x55
139 #define IRSENSE_EEPROM_MAP_ETH24H_HIGH 0x56
140 #define IRSENSE_EEPROM_MAP_ETH24L_LOW 0x57
141 #define IRSENSE_EEPROM_MAP_ETH24L_HIGH 0x58
142 #define IRSENSE_EEPROM_MAP_EHYS13 0x59
143 #define IRSENSE_EEPROM_MAP_EHYS24 0x5A
144 #define IRSENSE_EEPROM_MAP_EINTEN 0x5B
145 #define IRSENSE_EEPROM_MAP_ECNTL1 0x5C
146 
152 #define IRSENSE_ECNTL1_MODE_STAND_BY 0x00
153 #define IRSENSE_ECNTL1_MODE_EEPROM_ACCESS 0x01
154 #define IRSENSE_ECNTL1_MODE_SINGLE_SHOT 0x02
155 #define IRSENSE_ECNTL1_MODE_BE_PROHIBITED 0x03
156 #define IRSENSE_ECNTL1_MODE_CONTINUOUS_0 0x04
157 #define IRSENSE_ECNTL1_MODE_CONTINUOUS_1 0x05
158 #define IRSENSE_ECNTL1_MODE_CONTINUOUS_2 0x06
159 #define IRSENSE_ECNTL1_MODE_CONTINUOUS_3 0x07
160 #define IRSENSE_ECNTL1_CUTOFF_FREQUENCY_0_3_Hz 0x00
161 #define IRSENSE_ECNTL1_CUTOFF_FREQUENCY_0_6_Hz 0x08
162 #define IRSENSE_ECNTL1_CUTOFF_FREQUENCY_1_1_Hz 0x10
163 #define IRSENSE_ECNTL1_CUTOFF_FREQUENCY_2_2_Hz 0x18
164 #define IRSENSE_ECNTL1_CUTOFF_FREQUENCY_4_4_Hz 0x20
165 #define IRSENSE_ECNTL1_CUTOFF_FREQUENCY_8_8_Hz 0x28
166 
172 #define IRSENSE_EINTEN_IR13_HIGH_INT_DISABLE 0x00
173 #define IRSENSE_EINTEN_IR13_HIGH_INT_ENABLE 0x01
174 #define IRSENSE_EINTEN_IR13_LOW_INT_DISABLE 0x00
175 #define IRSENSE_EINTEN_IR13_LOW_INT_ENABLE 0x01
176 #define IRSENSE_EINTEN_IR24_HIGH_INT_DISABLE 0x00
177 #define IRSENSE_EINTEN_IR24_HIGH_INT_ENABLE 0x01
178 #define IRSENSE_EINTEN_IR24_LOW_INT_DISABLE 0x00
179 #define IRSENSE_EINTEN_IR24_LOW_INT_ENABLE 0x01
180 #define IRSENSE_EINTEN_DRI_INT_DISABLE 0x00
181 #define IRSENSE_EINTEN_DRI_INT_ENABLE 0x01
182 
183 #define IRSENSE_CNTL2_SOFT_RESET 0x01
184 #define IRSENSE_EEPROM_MAP_VAL_EKEY 0xA5
185 #define IRSENSE_ENABLE_SETTINGS_EEPROM_MODE 0x38
186 #define IRSENSE_ENABLE_EEPROM_MODE 0xC1
187 #define IRSENSE_EINTEN_RESET_VALUE 0xC0
188  // End group macro
191 // --------------------------------------------------------------- PUBLIC TYPES
200 typedef struct
201 {
202  // Output pins
203 
204  digital_out_t pdn;
205 
206  // Input pins
207 
208  digital_in_t int_pin;
209 
210  // Modules
211 
212  i2c_master_t i2c;
213 
214  // ctx variable
215 
216  uint8_t slave_address;
217 
218 } irsense_t;
219 
223 typedef struct
224 {
225  int16_t bottom_area;
226  int16_t left_area;
227  int16_t right_area;
228  int16_t top_area;
229 
231 
235 typedef struct
236 {
237  uint8_t ir13h;
238  uint8_t ir13l;
239  uint8_t ir24h;
240  uint8_t ir24l;
241  uint8_t dr;
243 
247  typedef struct
248 {
249  uint16_t eth24h;
250  uint16_t eth24l;
251  uint16_t eth13h;
252  uint16_t eth13l;
253 } irsense_reg_t;
254 
258  typedef struct
259 {
260  uint16_t eeth24h;
261  uint16_t eeth24l;
262  uint16_t eeth13h;
263  uint16_t eeth13l;
265 
269 typedef struct
270 {
271  // Communication gpio pins
272 
273  pin_name_t scl;
274  pin_name_t sda;
275 
276  // Additional gpio pins
277 
278  pin_name_t pdn;
279  pin_name_t int_pin;
280 
281  // static variable
282 
283  uint32_t i2c_speed;
284  uint8_t i2c_address;
285 
286 } irsense_cfg_t;
287  // End types group
289 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
290 
296 #ifdef __cplusplus
297 extern "C"{
298 #endif
299 
309 
318 
329 void irsense_generic_write ( irsense_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
330 
342 void irsense_generic_read ( irsense_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
343 
355  void irsense_write_byte ( irsense_t *ctx, uint8_t reg_address, uint8_t write_data );
356 
367  uint8_t irsense_read_byte ( irsense_t *ctx, uint8_t reg_address );
368 
379  uint16_t irsense_read_data ( irsense_t *ctx, uint8_t reg_address_low );
380 
391 
402 
413 
424 
455 
474  uint8_t irsense_get_status ( irsense_t *ctx );
475 
508  void irsense_set_configuration ( irsense_t *ctx, uint8_t register_value );
509 
528  void irsense_set_mode ( irsense_t *ctx, uint8_t mode );
529 
548  void irsense_set_cutoff_frequency ( irsense_t *ctx, uint8_t frequency );
549 
565 
582 
593 
603  void irsense_refresh ( irsense_t *ctx );
604 
618  int16_t irsense_get_ir1 ( irsense_t *ctx );
619 
633  int16_t irsense_get_ir2 ( irsense_t *ctx );
634 
648  int16_t irsense_get_ir3 ( irsense_t *ctx );
649 
663  int16_t irsense_get_ir4 ( irsense_t *ctx );
664 
681 
697 
713 
725  void irsense_set_threshold_ir2_ir4 ( irsense_t *ctx, uint8_t value );
726 
742 
754  void irsense_set_threshold_eeprom_ir2_ir4 ( irsense_t *ctx, uint8_t value );
755 
767  void irsense_set_threshold_ir1_ir3 ( irsense_t *ctx, uint8_t value );
768 
783 
795  void irsense_set_threshold_eeprom_ir1_ir3( irsense_t *ctx, uint8_t value );
796 
809 
826 
838  void irsense_set_hysteresis_ir2_ir4 ( irsense_t *ctx, uint8_t value );
839 
851  void irsense_set_hysteresis_eeprom_ir2_ir4 ( irsense_t *ctx, uint8_t value );
852 
864  void irsense_set_hysteresis_ir1_ir3 ( irsense_t *ctx, uint8_t value );
865 
878 
891  void irsense_read_hysteresis ( irsense_t *ctx, uint8_t *ehys24, uint8_t *ehys13 );
892 
905  void irsense_read_hysteresis_eeprom ( irsense_t *ctx, uint8_t *eehys24, uint8_t *eehys13 );
906 
919 
931 
947 
948 
949 #ifdef __cplusplus
950 }
951 #endif
952 #endif // _IRSENSE_H_
953  // End public_function group
956 
957 // ------------------------------------------------------------------------- END
irsense_read_interrupt_status
uint8_t irsense_read_interrupt_status(irsense_t *ctx)
Get interrupts activate or inactivate corresponding status value function.
irsense_reg_t::eth13h
uint16_t eth13h
Definition: irsense.h:251
irsense_t::pdn
digital_out_t pdn
Definition: irsense.h:204
irsense_eint_t::ir13l
uint8_t ir13l
Definition: irsense.h:238
irsense_check_int_states
uint8_t irsense_check_int_states(irsense_t *ctx)
Check state of INT pin function.
irsense_read_hysteresis_eeprom
void irsense_read_hysteresis_eeprom(irsense_t *ctx, uint8_t *eehys24, uint8_t *eehys13)
Get Hysteresis EEPROM value for all channes function.
irsense_set_mode
void irsense_set_mode(irsense_t *ctx, uint8_t mode)
Set the mode function.
irsense_set_threshold_eeprom_ir1_ir3
void irsense_set_threshold_eeprom_ir1_ir3(irsense_t *ctx, uint8_t value)
Set high or low EEPROM threshold for channel 1 and channel 3 ( Ir1-Ir3 ) function.
irsense_cfg_t::pdn
pin_name_t pdn
Definition: irsense.h:278
irsense_set_hysteresis_ir1_ir3
void irsense_set_hysteresis_ir1_ir3(irsense_t *ctx, uint8_t value)
Set hysteresis setting of threshold level for channel 1 and channel 3 ( Ir1-Ir3 ) function.
irsense_eeprom_t::eeth13h
uint16_t eeth13h
Definition: irsense.h:262
irsense_read_byte
uint8_t irsense_read_byte(irsense_t *ctx, uint8_t reg_address)
Generic read the byte of data function.
irsense_get_ir4
int16_t irsense_get_ir4(irsense_t *ctx)
Get value of sensor channel 4 function.
irsense_eint_t::ir13h
uint8_t ir13h
Definition: irsense.h:237
irsense_cfg_setup
void irsense_cfg_setup(irsense_cfg_t *cfg)
Config Object Initialization function.
irsense_set_threshold_ir2_ir4
void irsense_set_threshold_ir2_ir4(irsense_t *ctx, uint8_t value)
Set high or low threshold for channel 2 and channel 4 ( Ir2-Ir4 ) function.
irsense_ir_data_t::left_area
int16_t left_area
Definition: irsense.h:226
irsense_refresh
void irsense_refresh(irsense_t *ctx)
Refresh by read dummy data function.
irsense_t
Click ctx object definition.
Definition: irsense.h:201
irsense_t::slave_address
uint8_t slave_address
Definition: irsense.h:216
irsense_write_byte
void irsense_write_byte(irsense_t *ctx, uint8_t reg_address, uint8_t write_data)
Generic write the byte of data function.
irsense_t::i2c
i2c_master_t i2c
Definition: irsense.h:212
irsense_reg_t::eth13l
uint16_t eth13l
Definition: irsense.h:252
irsense_cfg_t
Click configuration structure definition.
Definition: irsense.h:270
irsense_read_threshold_eeprom
void irsense_read_threshold_eeprom(irsense_t *ctx, irsense_eeprom_t *eeprom)
Get threshold EEPROM value for all channels function.
irsense_reg_t
Register address structure definition.
Definition: irsense.h:248
irsense_set_threshold_ir1_ir3
void irsense_set_threshold_ir1_ir3(irsense_t *ctx, uint8_t value)
Set high or low threshold for channel 1 and channel 3 ( Ir1-Ir3 ) function.
irsense_eint_t::ir24l
uint8_t ir24l
Definition: irsense.h:240
irsense_eeprom_t::eeth24l
uint16_t eeth24l
Definition: irsense.h:261
irsense_eeprom_t::eeth24h
uint16_t eeth24h
Definition: irsense.h:260
irsense_ir_data_t::bottom_area
int16_t bottom_area
Definition: irsense.h:225
irsense_check_overrun
uint8_t irsense_check_overrun(irsense_t *ctx)
Checks to see if data overrun flag is set in the status register function.
irsense_set_interrupts_reg
void irsense_set_interrupts_reg(irsense_t *ctx, irsense_eint_t *eint)
Sets interrupts activate or inactivate corresponding function.
irsense_set_hysteresis_ir2_ir4
void irsense_set_hysteresis_ir2_ir4(irsense_t *ctx, uint8_t value)
Set hysteresis setting of threshold level for channel 2 and channel 4 ( Ir2-Ir4 ) function.
irsense_read_data
uint16_t irsense_read_data(irsense_t *ctx, uint8_t reg_address_low)
Generic read 16-bit of data function.
irsense_cfg_t::i2c_address
uint8_t i2c_address
Definition: irsense.h:284
irsense_set_hysteresis_eeprom_ir2_ir4
void irsense_set_hysteresis_eeprom_ir2_ir4(irsense_t *ctx, uint8_t value)
Set hysteresis EEPROM setting of threshold level for channel 2 and channel 4 ( Ir2-Ir4 ) function.
irsense_get_threshold_ir2_ir4
uint16_t irsense_get_threshold_ir2_ir4(irsense_t *ctx)
Get threshold value for channel 2 and channel 4 ( Ir2-Ir4 ) function.
irsense_cfg_t::sda
pin_name_t sda
Definition: irsense.h:274
irsense_get_ir_sense
void irsense_get_ir_sense(irsense_t *ctx, irsense_ir_data_t *ir_data)
Get value of sensor all channels function.
irsense_ir_data_t::right_area
int16_t right_area
Definition: irsense.h:227
irsense_generic_write
void irsense_generic_write(irsense_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
irsense_get_ir1
int16_t irsense_get_ir1(irsense_t *ctx)
Get value of sensor channel 1 function.
irsense_set_threshold_eeprom_ir2_ir4
void irsense_set_threshold_eeprom_ir2_ir4(irsense_t *ctx, uint8_t value)
Set high or low EEPROM threshold for channel 2 and channel 4 ( Ir2-Ir4 ) function.
irsense_eint_t::ir24h
uint8_t ir24h
Definition: irsense.h:239
irsense_power_on
void irsense_power_on(irsense_t *ctx)
Enable the sensor function.
irsense_get_status
uint8_t irsense_get_status(irsense_t *ctx)
Get status value function.
irsense_t::int_pin
digital_in_t int_pin
Definition: irsense.h:208
irsense_init
IRSENSE_RETVAL irsense_init(irsense_t *ctx, irsense_cfg_t *cfg)
Initialization function.
irsense_enable_data_ready_interrupt
void irsense_enable_data_ready_interrupt(irsense_t *ctx)
Sets interrupts source settings function.
irsense_get_ir3
int16_t irsense_get_ir3(irsense_t *ctx)
Get value of sensor channel 3 function.
irsense_get_company_code
uint8_t irsense_get_company_code(irsense_t *ctx)
Get company code function.
irsense_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: irsense.h:283
irsense_check_available
uint8_t irsense_check_available(irsense_t *ctx)
Checks to see if DRDY flag is set in the status register function.
irsense_read_hysteresis
void irsense_read_hysteresis(irsense_t *ctx, uint8_t *ehys24, uint8_t *ehys13)
Get Hysteresis value for all channes function.
irsense_cfg_t::scl
pin_name_t scl
Definition: irsense.h:273
irsense_ir_data_t
IR data structure definition.
Definition: irsense.h:224
irsense_ir_data_t::top_area
int16_t top_area
Definition: irsense.h:228
irsense_reg_t::eth24h
uint16_t eth24h
Definition: irsense.h:249
irsense_get_temperature_f
float irsense_get_temperature_f(irsense_t *ctx)
Get the temperature in Fahrenheit degrees function.
irsense_cfg_t::int_pin
pin_name_t int_pin
Definition: irsense.h:279
irsense_get_temperature
float irsense_get_temperature(irsense_t *ctx)
Get the temperature in degrees Celsius function.
irsense_eeprom_t
EEPROM map structure definition.
Definition: irsense.h:259
irsense_set_configuration
void irsense_set_configuration(irsense_t *ctx, uint8_t register_value)
Set configuration function.
irsense_read_threshold
void irsense_read_threshold(irsense_t *ctx, irsense_reg_t *reg)
Get threshold value for all channels function.
irsense_power_off
void irsense_power_off(irsense_t *ctx)
Disable the sensor function.
irsense_get_ir2
int16_t irsense_get_ir2(irsense_t *ctx)
Get value of sensor channel 2 function.
IRSENSE_RETVAL
#define IRSENSE_RETVAL
Definition: irsense.h:78
irsense_get_device_id
uint8_t irsense_get_device_id(irsense_t *ctx)
Get device ID function.
irsense_get_threshold_ir1_ir3
uint16_t irsense_get_threshold_ir1_ir3(irsense_t *ctx)
Get threshold value for channel 1 and channel 3 ( Ir1-Ir3 ) function.
irsense_eint_t::dr
uint8_t dr
Definition: irsense.h:241
irsense_eeprom_t::eeth13l
uint16_t eeth13l
Definition: irsense.h:263
irsense_reg_t::eth24l
uint16_t eth24l
Definition: irsense.h:250
irsense_set_cutoff_frequency
void irsense_set_cutoff_frequency(irsense_t *ctx, uint8_t frequency)
Set the cutoff frequency function.
irsense_set_hysteresis_eeprom_ir1_ir3
void irsense_set_hysteresis_eeprom_ir1_ir3(irsense_t *ctx, uint8_t value)
Set hysteresis EEPROM setting of threshold level for channel 1 and channel 3 ( Ir1-Ir3 ) function.
irsense_soft_reset
void irsense_soft_reset(irsense_t *ctx)
Soft reset function.
irsense_eint_t
Enable interrupt structure definition.
Definition: irsense.h:236
irsense_generic_read
void irsense_generic_read(irsense_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.