smoke  2.0.0.0
smoke.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 SMOKE_H
36 #define SMOKE_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 SMOKE_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 SMOKE_RETVAL uint8_t
73 
74 #define SMOKE_OK 0x00
75 #define SMOKE_INIT_ERROR 0xFF
76 #define SMOKE_PARAMETER_ERROR 0xFE
77 
83 #define SMOKE_I2C_ADDRESS 0x57
84 
95 #define SMOKE_INT_STAT1 0x00
96 #define SMOKE_INT_STAT2 0x01
97 #define SMOKE_INT_ENABLE1 0x02
98 #define SMOKE_INT_ENABLE2 0x03
99 
105 #define SMOKE_INT_FLAG_GET_MAIN 1
106 #define SMOKE_INT_FLAG_GET_TEMP_DATA 2
107 
108 #define SMOKE_INT_FLAG_SET_AFULL 0
109 #define SMOKE_INT_FLAG_SET_DATA_RDY 1
110 #define SMOKE_INT_FLAG_SET_ALS_OVF 2
111 #define SMOKE_INT_FLAG_SET_PROXY_THRESH 3
112 #define SMOKE_INT_FLAG_SET_TEMP 4
113 
114 #define SMOKE_INT_FLAG_SET_ENABLE 1
115 #define SMOKE_INT_FLAG_SET_DISABLE 0
116 
122 #define SMOKE_FIFO_WRITE_PTR 0x04
123 #define SMOKE_FIFO_OVERFLOW 0x05
124 #define SMOKE_FIFO_READ_PTR 0x06
125 #define SMOKE_FIFO_DATA 0x07
126 #define SMOKE_BUFFER_LENGTH 32
127 
133 #define SMOKE_REG_FIFO_CONFIG 0x08
134 #define SMOKE_REG_MODE_CONFIG 0x09
135 #define SMOKE_REG_PARTICLE_CONFIG 0x0A
136 #define SMOKE_REG_LED_RED_PULSE_AMP 0x0C
137 #define SMOKE_REG_LED_IR_PULSE_AMP 0x0D
138 #define SMOKE_REG_LED_GREEN_PULSE_AMP 0x0E
139 #define SMOKE_REG_LED_PROX_AMP 0x10
140 #define SMOKE_REG_LED_PROX_THRESH 0x30
141 #define SMOKE_REG_MULTI_LED_CONFIG1 0x11
142 #define SMOKE_REG_MULTI_LED_CONFIG2 0x12
143 #define SMOKE_REG_MIN_POWER_LEVEL 0x01
144 #define SMOKE_REG_AVG_POWER_LEVEL 0x1F
145 #define SMOKE_REG_MAX_POWER_LEVEL 0xFF
146 
152 #define SMOKE_DIE_TEMP_INT 0x1F
153 #define SMOKE_DIE_TEMP_FRAC 0x20
154 #define SMOKE_DIE_TEMP_CONFIG 0x21
155 
161 #define SMOKE_PROX_INT_THRESH 0x30
162 
168 #define SMOKE_REVISION_ID 0xFE
169 #define SMOKE_PART_ID 0xFF
170 
183 #define SMOKE_INT_A_FULL_MASK (uint8_t)~0b10000000
184 #define SMOKE_INT_A_FULL_ENABLE 0x80
185 #define SMOKE_INT_A_FULL_DISABLE 0x00
186 #define SMOKE_INT_DATA_RDY_MASK (uint8_t)~0b01000000
187 #define SMOKE_INT_DATA_RDY_ENABLE 0x40
188 #define SMOKE_INT_DATA_RDY_DISABLE 0x00
189 #define SMOKE_INT_ALC_OVF_MASK (uint8_t)~0b00100000
190 #define SMOKE_INT_ALC_OVF_ENABLE 0x20
191 #define SMOKE_INT_ALC_OVF_DISABLE 0x00
192 #define SMOKE_INT_PROX_INT_MASK (uint8_t)~0b00010000
193 #define SMOKE_INT_PROX_INT_ENABLE 0x10
194 #define SMOKE_INT_PROX_INT_DISABLE 0x00
195 #define SMOKE_INT_DIE_TEMP_RDY_MASK (uint8_t)~0b00000010
196 #define SMOKE_INT_DIE_TEMP_RDY_ENABLE 0x02
197 #define SMOKE_INT_DIE_TEMP_RDY_DISABLE 0x00
198 
204 #define SMOKE_SAMPLEAVG_MASK (uint8_t)~0b11100000
205 #define SMOKE_SAMPLEAVG_1 0x00
206 #define SMOKE_SAMPLEAVG_2 0x20
207 #define SMOKE_SAMPLEAVG_4 0x40
208 #define SMOKE_SAMPLEAVG_8 0x60
209 #define SMOKE_SAMPLEAVG_16 0x80
210 #define SMOKE_SAMPLEAVG_32 0xA0
211 #define SMOKE_ROLLOVER_MASK 0xEF
212 #define SMOKE_ROLLOVER_ENABLE 0x10
213 #define SMOKE_ROLLOVER_DISABLE 0x00
214 #define SMOKE_A_FULL_MASK 0xF0
215 
221 #define SMOKE_SHUTDOWN_MASK 0x7F
222 #define SMOKE_SHUTDOWN 0x80
223 #define SMOKE_WAKEUP 0x00
224 #define SMOKE_RESET_MASK 0xBF
225 #define SMOKE_RESET 0x40
226 #define SMOKE_MODE_MASK 0xF8
227 #define SMOKE_MODE_REDONLY 0x02
228 #define SMOKE_MODE_REDIRONLY 0x03
229 #define SMOKE_MODE_MULTILED 0x07
230 
236 #define SMOKE_ADCRANGE_MASK 0x9F
237 #define SMOKE_ADCRANGE_2048 0x00
238 #define SMOKE_ADCRANGE_4096 0x20
239 #define SMOKE_ADCRANGE_8192 0x40
240 #define SMOKE_ADCRANGE_16384 0x60
241 #define SMOKE_SAMPLERATE_MASK 0xE3
242 #define SMOKE_SAMPLERATE_50 0x00
243 #define SMOKE_SAMPLERATE_100 0x04
244 #define SMOKE_SAMPLERATE_200 0x08
245 #define SMOKE_SAMPLERATE_400 0x0C
246 #define SMOKE_SAMPLERATE_800 0x10
247 #define SMOKE_SAMPLERATE_1000 0x14
248 #define SMOKE_SAMPLERATE_1600 0x18
249 #define SMOKE_SAMPLERATE_3200 0x1C
250 #define SMOKE_PULSEWIDTH_MASK 0xFC
251 #define SMOKE_PULSEWIDTH_69 0x00
252 #define SMOKE_PULSEWIDTH_118 0x01
253 #define SMOKE_PULSEWIDTH_215 0x02
254 #define SMOKE_PULSEWIDTH_411 0x03
255 
261 #define SMOKE_SLOT1_MASK 0xF8
262 #define SMOKE_SLOT2_MASK 0x8F
263 #define SMOKE_SLOT3_MASK 0xF8
264 #define SMOKE_SLOT4_MASK 0x8F
265 
266 #define SMOKE_SLOT_NONE 0x00
267 #define SMOKE_SLOT_RED_LED 0x01
268 #define SMOKE_SLOT_IR_LED 0x02
269 #define SMOKE_SLOT_GREEN_LED 0x03
270 
271 #define SMOKE_SLOT_NONE_PILOT 0x04
272 #define SMOKE_SLOT_RED_PILOT 0x05
273 #define SMOKE_SLOT_IR_PILOT 0x06
274 #define SMOKE_SLOT_GREEN_PILOT 0x07
275  // End group macro
278 // --------------------------------------------------------------- PUBLIC TYPES
287 typedef struct
288 {
289  // Input pins
290 
291  digital_in_t int_pin;
292 
293  // Modules
294 
295  i2c_master_t i2c;
296 
297  // ctx variable
298 
299  uint8_t slave_address;
300 
301  uint8_t num_en_led;
302  uint32_t red_value;
303  uint32_t ir_value;
304  uint32_t green_value;
305 
306 } smoke_t;
307 
311 typedef struct
312 {
313  // Communication gpio pins
314 
315  pin_name_t scl;
316  pin_name_t sda;
317 
318  // Additional gpio pins
319 
320  pin_name_t int_pin;
321 
322  // static variable
323 
324  uint32_t i2c_speed;
325  uint8_t i2c_address;
326 
327 } smoke_cfg_t;
328 
332 typedef struct
333 {
334  uint8_t avg_samp;
335  uint8_t mode;
336  uint8_t adc_range;
337  uint8_t samp_rate;
338  uint8_t pulse_width;
339  uint8_t led_pow_lvl;
340 
342 
346 typedef struct
347 {
348  uint8_t reg_addr;
349  uint8_t mask;
350  uint8_t value;
351 
353  // End types group
355 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
356 
362 #ifdef __cplusplus
363 extern "C"{
364 #endif
365 
375 
385 
394 
404 void smoke_write_data ( smoke_t *ctx, uint8_t wr_addr, uint8_t wr_data );
405 
416 uint8_t smoke_read_data ( smoke_t *ctx, uint8_t rd_addr );
417 
428 void smoke_generic_read ( smoke_t *ctx, uint8_t rd_addr, uint8_t *buffer, uint8_t cnt );
429 
440 void smoke_set_new_value ( smoke_t *ctx, smoke_set_new_value_t *new_value_data );
441 
452 uint8_t smoke_get_intrrupt ( smoke_t *ctx, uint8_t flag );
453 
482 uint8_t smoke_enable_disable_interrupts ( smoke_t *ctx, uint8_t interrupt_flag, uint8_t enable_flag );
483 
493 uint8_t smoke_read_device_id ( smoke_t *ctx );
494 
501 void smoke_reset ( smoke_t *ctx );
502 
512 float smoke_read_temp ( smoke_t *ctx );
513 
535 uint8_t smoke_set_power ( smoke_t *ctx, uint8_t state );
536 
559 uint8_t smoke_set_led_mode ( smoke_t *ctx, uint8_t mode );
560 
584 uint8_t smoke_set_adc_range ( smoke_t *ctx, uint8_t adc_range );
585 
613 uint8_t smoke_set_sample_rate ( smoke_t *ctx, uint8_t sample_rate );
614 
638 uint8_t smoke_set_pulse_width ( smoke_t *ctx, uint8_t pulse_width );
639 
663 uint8_t smoke_set_led_amplitude ( smoke_t *ctx, uint8_t led, uint8_t amplitude );
664 
683 uint8_t smoke_set_proximity_amplitude_threshold( smoke_t *ctx, uint8_t threshold );
684 
696 uint8_t smoke_enable_slot ( smoke_t *ctx, uint8_t slot_num, uint8_t dev );
697 
706 
714 void smoke_clear_fifo ( smoke_t *ctx );
715 
725 uint8_t smoke_get_write_ptr ( smoke_t *ctx );
726 
736 uint8_t smoke_get_read_ptr ( smoke_t *ctx );
737 
759 uint8_t smoke_set_rollover_state( smoke_t *ctx, uint8_t state );
760 
779 uint8_t smoke_set_almost_full_trigger( smoke_t *ctx, uint8_t trigger_range );
780 
806 uint8_t smoke_set_sample_avarage ( smoke_t *ctx, uint8_t samples );
807 
832 uint8_t smoke_set_registers ( smoke_t *ctx, smoke_set_registers_t *registers );
833 
850 uint8_t smoke_read_leds ( smoke_t *ctx );
851 
859 uint8_t smoke_check_int ( smoke_t *ctx );
860 
861 #ifdef __cplusplus
862 }
863 #endif
864 #endif // _SMOKE_H_
865  // End public_function group
868 
869 // ------------------------------------------------------------------------- END
smoke_read_temp
float smoke_read_temp(smoke_t *ctx)
Read temperature in Centigrade function.
SMOKE_RETVAL
#define SMOKE_RETVAL
Definition: smoke.h:72
smoke_t::i2c
i2c_master_t i2c
Definition: smoke.h:295
smoke_disable_slots
void smoke_disable_slots(smoke_t *ctx)
Disable slots in MultiLED mode function.
smoke_enable_slot
uint8_t smoke_enable_slot(smoke_t *ctx, uint8_t slot_num, uint8_t dev)
Define slots in MultiLED mode function.
smoke_set_new_value
void smoke_set_new_value(smoke_t *ctx, smoke_set_new_value_t *new_value_data)
Set new value function.
smoke_t::slave_address
uint8_t slave_address
Definition: smoke.h:299
smoke_init
SMOKE_RETVAL smoke_init(smoke_t *ctx, smoke_cfg_t *cfg)
Initialization function.
smoke_set_registers_t::pulse_width
uint8_t pulse_width
Definition: smoke.h:338
smoke_cfg_t::int_pin
pin_name_t int_pin
Definition: smoke.h:320
smoke_set_registers_t::adc_range
uint8_t adc_range
Definition: smoke.h:336
smoke_generic_read
void smoke_generic_read(smoke_t *ctx, uint8_t rd_addr, uint8_t *buffer, uint8_t cnt)
Generic read function.
smoke_t::num_en_led
uint8_t num_en_led
Definition: smoke.h:301
smoke_set_led_amplitude
uint8_t smoke_set_led_amplitude(smoke_t *ctx, uint8_t led, uint8_t amplitude)
Set led amplitude.
smoke_set_new_value_t::mask
uint8_t mask
Definition: smoke.h:349
smoke_t::ir_value
uint32_t ir_value
Definition: smoke.h:303
smoke_write_data
void smoke_write_data(smoke_t *ctx, uint8_t wr_addr, uint8_t wr_data)
Generic write function.
smoke_set_registers_t::samp_rate
uint8_t samp_rate
Definition: smoke.h:337
smoke_set_rollover_state
uint8_t smoke_set_rollover_state(smoke_t *ctx, uint8_t state)
Set rollover state.
smoke_set_new_value_t
New value object definition.
Definition: smoke.h:347
smoke_set_new_value_t::value
uint8_t value
Definition: smoke.h:350
smoke_set_led_mode
uint8_t smoke_set_led_mode(smoke_t *ctx, uint8_t mode)
Enable leds.
smoke_set_adc_range
uint8_t smoke_set_adc_range(smoke_t *ctx, uint8_t adc_range)
Set ADC range.
smoke_set_registers
uint8_t smoke_set_registers(smoke_t *ctx, smoke_set_registers_t *registers)
Set registers values function.
smoke_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: smoke.h:324
smoke_get_intrrupt
uint8_t smoke_get_intrrupt(smoke_t *ctx, uint8_t flag)
Get desired interrupt function.
smoke_set_sample_avarage
uint8_t smoke_set_sample_avarage(smoke_t *ctx, uint8_t samples)
Set avarage samples.
smoke_t::green_value
uint32_t green_value
Definition: smoke.h:304
smoke_clear_fifo
void smoke_clear_fifo(smoke_t *ctx)
Clearing FIFO pointers function.
smoke_cfg_t
Click configuration structure definition.
Definition: smoke.h:312
smoke_default_cfg
void smoke_default_cfg(smoke_t *ctx)
Click Default Configuration function.
smoke_get_write_ptr
uint8_t smoke_get_write_ptr(smoke_t *ctx)
Read the FIFO Write Pointer function.
smoke_set_registers_t::led_pow_lvl
uint8_t led_pow_lvl
Definition: smoke.h:339
smoke_set_proximity_amplitude_threshold
uint8_t smoke_set_proximity_amplitude_threshold(smoke_t *ctx, uint8_t threshold)
Set proximity threshold.
smoke_set_registers_t::avg_samp
uint8_t avg_samp
Definition: smoke.h:334
smoke_reset
void smoke_reset(smoke_t *ctx)
Reset device function.
smoke_set_registers_t
Click registers object definition.
Definition: smoke.h:333
smoke_t
Click ctx object definition.
Definition: smoke.h:288
smoke_enable_disable_interrupts
uint8_t smoke_enable_disable_interrupts(smoke_t *ctx, uint8_t interrupt_flag, uint8_t enable_flag)
Enable or disable interrupt function.
smoke_cfg_t::i2c_address
uint8_t i2c_address
Definition: smoke.h:325
smoke_set_registers_t::mode
uint8_t mode
Definition: smoke.h:335
smoke_set_new_value_t::reg_addr
uint8_t reg_addr
Definition: smoke.h:348
smoke_t::red_value
uint32_t red_value
Definition: smoke.h:302
smoke_set_almost_full_trigger
uint8_t smoke_set_almost_full_trigger(smoke_t *ctx, uint8_t trigger_range)
Set almost full trigger.
smoke_read_device_id
uint8_t smoke_read_device_id(smoke_t *ctx)
Read device ID function.
smoke_cfg_setup
void smoke_cfg_setup(smoke_cfg_t *cfg)
Config Object Initialization function.
smoke_set_pulse_width
uint8_t smoke_set_pulse_width(smoke_t *ctx, uint8_t pulse_width)
Set pulse width.
smoke_cfg_t::sda
pin_name_t sda
Definition: smoke.h:316
smoke_get_read_ptr
uint8_t smoke_get_read_ptr(smoke_t *ctx)
Read the FIFO Read Pointer function.
smoke_read_data
uint8_t smoke_read_data(smoke_t *ctx, uint8_t rd_addr)
Generic read function.
smoke_set_power
uint8_t smoke_set_power(smoke_t *ctx, uint8_t state)
I2C power setter.
smoke_t::int_pin
digital_in_t int_pin
Definition: smoke.h:291
smoke_cfg_t::scl
pin_name_t scl
Definition: smoke.h:315
smoke_read_leds
uint8_t smoke_read_leds(smoke_t *ctx)
Function for reading enabled leds.
smoke_check_int
uint8_t smoke_check_int(smoke_t *ctx)
Get Interrupt state function.
smoke_set_sample_rate
uint8_t smoke_set_sample_rate(smoke_t *ctx, uint8_t sample_rate)
Set sample rate.