presence  2.0.0.0
presence.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 PRESENCE_H
36 #define PRESENCE_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 // -------------------------------------------------------------- PUBLIC MACROS
66 #define PRESENCE_MAP_MIKROBUS( cfg, mikrobus ) \
67  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
68  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
69  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
70 
76 #define PRESENCE_OK 0
77 #define PRESENCE_ERROR -1
78 
84 #define PRESENCE_REG_TP_OBJECT_MSB 0x01
85 #define PRESENCE_REG_TP_OBJECT_MID 0x02
86 #define PRESENCE_REG_TP_OBJECT_LSB 0x03
87 #define PRESENCE_REG_TP_AMBIENT_MSB 0x03
88 #define PRESENCE_REG_TP_AMBIENT_LSB 0x04
89 #define PRESENCE_REG_TP_OBJECT_LP1_MSB 0x05
90 #define PRESENCE_REG_TP_OBJECT_LP1_MID 0x06
91 #define PRESENCE_REG_TP_OBJECT_LP1_LSB 0x07
92 #define PRESENCE_REG_TP_OBJECT_LP2_MSB 0x07
93 #define PRESENCE_REG_TP_OBJECT_LP2_MID 0x08
94 #define PRESENCE_REG_TP_OBJECT_LP2_LSB 0x09
95 #define PRESENCE_REG_TP_AMBIENT_LP3_MSB 0x0A
96 #define PRESENCE_REG_TP_AMBIENT_LP3_LSB 0x0B
97 #define PRESENCE_REG_TP_OBJECT_LP2F_MSB 0x0C
98 #define PRESENCE_REG_TP_OBJECT_LP2F_MID 0x0D
99 #define PRESENCE_REG_TP_OBJECT_LP2F_LSB 0x0E
100 #define PRESENCE_REG_TP_PRESENCE 0x0F
101 #define PRESENCE_REG_TP_MOTION 0x10
102 #define PRESENCE_REG_TP_AMBIENT_SHOCK 0x11
103 #define PRESENCE_REG_INTERRUPT_STATUS 0x12
104 #define PRESENCE_REG_CHIP_STATUS 0x13
105 #define PRESENCE_REG_LOW_PASS_TIME_1 0x14
106 #define PRESENCE_REG_LOW_PASS_TIME_2 0x15
107 #define PRESENCE_REG_TP_PRESENCE_THR 0x16
108 #define PRESENCE_REG_TP_MOTION_THR 0x17
109 #define PRESENCE_REG_TP_AMB_SHOCK_THR 0x18
110 #define PRESENCE_REG_INTERRUPT_MASK_1 0x19
111 #define PRESENCE_REG_INTERRUPT_MASK_2 0x1A
112 #define PRESENCE_REG_TIMER_INTERRUPT 0x1B
113 #define PRESENCE_REG_TP_OT_THR_1 0x1C
114 #define PRESENCE_REG_TP_OT_THR_2 0x1D
115 #define PRESENCE_REG_EEPROM_CTRL 0x1F
116 
122 #define PRESENCE_EEPROM_PROTOCOL 0x20
123 #define PRESENCE_EEPROM_CHECKSUM_1 0x21
124 #define PRESENCE_EEPROM_CHECKSUM_2 0x22
125 #define PRESENCE_EEPROM_LOOKUP 0x29
126 #define PRESENCE_EEPROM_PTAT25_1 0x2A
127 #define PRESENCE_EEPROM_PTAT25_2 0x2B
128 #define PRESENCE_EEPROM_M_1 0x2C
129 #define PRESENCE_EEPROM_M_2 0x2D
130 #define PRESENCE_EEPROM_U0_1 0x2E
131 #define PRESENCE_EEPROM_U0_2 0x2F
132 #define PRESENCE_EEPROM_UOUT_1 0x30
133 #define PRESENCE_EEPROM_UOUT_2 0x31
134 #define PRESENCE_EEPROM_TOBJ 0x32
135 #define PRESENCE_EEPROM_SLAVE_ADDR 0x3F
136 
142 #define PRESENCE_DEVICE_SLAVE_ADDRESS 0x0C
143 #define PRESENCE_DEVICE_GEN_CALL_ADDRESS 0x00
144 
150 #define PRESENCE_LOW_PASS_TIME_512S 0x00
151 #define PRESENCE_LOW_PASS_TIME_256S 0x01
152 #define PRESENCE_LOW_PASS_TIME_128S 0x02
153 #define PRESENCE_LOW_PASS_TIME_64S 0x03
154 #define PRESENCE_LOW_PASS_TIME_32S 0x04
155 #define PRESENCE_LOW_PASS_TIME_16S 0x05
156 #define PRESENCE_LOW_PASS_TIME_8S 0x08
157 #define PRESENCE_LOW_PASS_TIME_4S 0x09
158 #define PRESENCE_LOW_PASS_TIME_2S 0x0A
159 #define PRESENCE_LOW_PASS_TIME_1S 0x0B
160 #define PRESENCE_LOW_PASS_TIME_0P50S 0x0C
161 #define PRESENCE_LOW_PASS_TIME_0P25S 0x0D
162 
168 #define PRESENCE_INT_MASK1_TP_OT 0x10
169 #define PRESENCE_INT_MASK1_PRESENCE 0x08
170 #define PRESENCE_INT_MASK1_MOTION 0x04
171 #define PRESENCE_INT_MASK1_AMB_SHOCK 0x02
172 #define PRESENCE_INT_MASK1_TIMER 0x01
173 
179 #define PRESENCE_INT_MASK2_TPOT_DIR 0x10
180 #define PRESENCE_INT_MASK2_SRC_TPOBJ_LP2 ( 0x00 << 2 )
181 #define PRESENCE_INT_MASK2_SRC_LP1_LP2 ( 0x01 << 2 )
182 #define PRESENCE_INT_MASK2_SRC_TPOBJ_LP2FROZEN ( 0x02 << 2 )
183 #define PRESENCE_INT_MASK2_SRC_LP1_LP2FROZEN ( 0x03 << 2 )
184 #define PRESENCE_INT_MASK2_CYCLE_TIME_30MS 0x00
185 #define PRESENCE_INT_MASK2_CYCLE_TIME_60MS 0x01
186 #define PRESENCE_INT_MASK2_CYCLE_TIME_120MS 0x02
187 #define PRESENCE_INT_MASK2_CYCLE_TIME_140MS 0x04
188  // End group macro
191 // --------------------------------------------------------------- PUBLIC TYPES
197 typedef struct
198 {
199  uint16_t ptat25_data;
200  uint16_t m_data;
201  uint16_t u0_data;
202  uint8_t tobj_data;
203  uint32_t uout_data;
204  uint16_t checksum_data;
205 
206 } eeprom_data_t;
207 
211 typedef struct
212 {
213  // Input pins
214  digital_in_t int_pin;
215 
216  // Modules
217  i2c_master_t i2c;
218 
219  // ctx variable
220  uint8_t slave_address;
221 
223 
224 } presence_t;
225 
229 typedef struct
230 {
231  // Communication gpio pins
232  pin_name_t scl;
233  pin_name_t sda;
234 
235  // Additional gpio pins
236  pin_name_t int_pin;
237 
238  // static variable
239  uint32_t i2c_speed;
240  uint8_t i2c_address;
241 
243 
244  // End types group
246 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
247 
253 #ifdef __cplusplus
254 extern "C"{
255 #endif
256 
266 
276 
290 
304 err_t presence_generic_write ( presence_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
305 
319 err_t presence_generic_read ( presence_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
320 
329 
338 
353 
364 
376 err_t presence_ambient_temperature( presence_t *ctx, float *temperature );
377 
389 err_t presence_object_temperature( presence_t *ctx, float *temperature );
390 
391 #ifdef __cplusplus
392 }
393 #endif
394 #endif // _PRESENCE_H_
395  // End public_function group
398 
399 // ------------------------------------------------------------------------- END
presence_t::eeprom_data
eeprom_data_t eeprom_data
Definition: presence.h:222
presence_cfg_t::sda
pin_name_t sda
Definition: presence.h:233
presence_t::i2c
i2c_master_t i2c
Definition: presence.h:217
presence_cfg_t::i2c_address
uint8_t i2c_address
Definition: presence.h:240
eeprom_data_t::m_data
uint16_t m_data
Definition: presence.h:200
presence_get_int_state
uint8_t presence_get_int_state(presence_t *ctx)
Get interrupt state function.
presence_ambient_temperature
err_t presence_ambient_temperature(presence_t *ctx, float *temperature)
Ambient Temperature.
presence_general_call_addr
void presence_general_call_addr(presence_t *ctx)
General call address.
presence_t::int_pin
digital_in_t int_pin
Definition: presence.h:214
eeprom_data_t::tobj_data
uint8_t tobj_data
Definition: presence.h:202
eeprom_data_t
Definition: presence.h:198
presence_cfg_setup
void presence_cfg_setup(presence_cfg_t *cfg)
Config Object Initialization function.
eeprom_data_t::uout_data
uint32_t uout_data
Definition: presence.h:203
presence_init
err_t presence_init(presence_t *ctx, presence_cfg_t *cfg)
Initialization function.
presence_cfg_t::int_pin
pin_name_t int_pin
Definition: presence.h:236
presence_t
Click ctx object definition.
Definition: presence.h:212
presence_object_temperature
err_t presence_object_temperature(presence_t *ctx, float *temperature)
Object Temperature.
presence_get_calib_constants
float presence_get_calib_constants(presence_t *ctx)
Calibration constant K.
presence_default_cfg
err_t presence_default_cfg(presence_t *ctx)
Presence default configuration function.
eeprom_data_t::u0_data
uint16_t u0_data
Definition: presence.h:201
presence_generic_write
err_t presence_generic_write(presence_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
presence_generic_read
err_t presence_generic_read(presence_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
eeprom_data_t::checksum_data
uint16_t checksum_data
Definition: presence.h:204
eeprom_data_t::ptat25_data
uint16_t ptat25_data
Definition: presence.h:199
presence_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: presence.h:239
presence_cfg_t::scl
pin_name_t scl
Definition: presence.h:232
presence_eeprom_process
err_t presence_eeprom_process(presence_t *ctx)
EEPRM PROCESS.
presence_cfg_t
Click configuration structure definition.
Definition: presence.h:230
presence_t::slave_address
uint8_t slave_address
Definition: presence.h:220