current3  2.0.0.0
current3.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 CURRENT3_H
36 #define CURRENT3_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 CURRENT3_MAP_MIKROBUS( cfg, mikrobus ) \
67  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
68  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
69  cfg.trm = MIKROBUS( mikrobus, MIKROBUS_RST ); \
70  cfg.alr = MIKROBUS( mikrobus, MIKROBUS_INT )
71 
77 #define CURRENT3_OK 0
78 #define CURRENT3_ERROR -1
79 
85 #define CURRENT3_I2C_SLAVE_ADDR 0x4C
86 #define CURRENT3_ALERT_RESPONSE_ADDRESS 0x0C
87 
93 #define CURRENT3_REG_INT_DIODE_DATA_HIGH_BYTE 0x00
94 #define CURRENT3_REG_EXT_DIODE_1_DATA_HIGH_BYTE 0x01
95 #define CURRENT3_REG_STATUS 0x02
96 #define CURRENT3_REG_CFG 0x03
97 #define CURRENT3_REG_CONVERSION_RATE 0x04
98 #define CURRENT3_REG_INT_DIODE_HIGH_LIMIT 0x05
99 #define CURRENT3_REG_INT_DIODE_LOW_LIMIT 0x06
100 #define CURRENT3_REG_EXT_DIODE_1_HIGH_LIMIT_HIGH_BYTE 0x07
101 #define CURRENT3_REG_EXT_DIODE_1_LOW_LIMIT_HIGH_BYTE 0x08
102 #define CURRENT3_REG_ONE_SHOT 0x0F
103 #define CURRENT3_REG_EXT_DIODE_1_DATA_LOW_BYTE 0x10
104 #define CURRENT3_REG_EXT_DIODE_1_HIGH_LIMIT_LOW_BYTE 0x13
105 #define CURRENT3_REG_EXT_DIODE_1_LOW_LIMIT_LOW_BYTE 0x14
106 #define CURRENT3_REG_EXT_DIODE_1_TCRIT_LIMIT 0x19
107 #define CURRENT3_REG_EXT_DIODE_FAULT 0x1B
108 #define CURRENT3_REG_CHANNEL_MASK_REGISTER 0x1F
109 #define CURRENT3_REG_INT_DIODE_TCRIT_LIMIT 0x20
110 #define CURRENT3_REG_TCRIT_HYSTERESIS 0x21
111 #define CURRENT3_REG_CONSECUTIVE_ALERT 0x22
112 #define CURRENT3_REG_EXT_DIODE_1_BETA_CFG 0x25
113 #define CURRENT3_REG_EXT_DIODE_1_IDEALITY_FACTOR 0x27
114 #define CURRENT3_REG_INT_DIODE_DATA_LOW_BYTE 0x29
115 #define CURRENT3_REG_HIGH_LIMIT_STATUS 0x35
116 #define CURRENT3_REG_LOW_LIMIT_STATUS 0x36
117 #define CURRENT3_REG_CRIT_LIMIT_STATUS 0x37
118 #define CURRENT3_REG_INT_DIODE_HIGH_BYTE 0x38
119 #define CURRENT3_REG_INT_DIODE_LOW_BYTE 0x39
120 #define CURRENT3_REG_EXT_DIODE_1_HIGH_BYTE 0x3A
121 #define CURRENT3_REG_EXT_DIODE_1_LOW_BYTE 0x3B
122 #define CURRENT3_REG_AVERAGING_CONTROL 0x40
123 
129 #define CURRENT3_REG_VOLTAGE_SAMPLING_CFG 0x50
130 #define CURRENT3_REG_CURRENT_SENSE_SAMPLING_CFG 0x51
131 #define CURRENT3_REG_PEAK_DETECTION_CONFIG 0x52
132 #define CURRENT3_REG_SENSE_VOLTAGE_HIGH_BYTE 0x54
133 #define CURRENT3_REG_SENSE_VOLTAGE_LOW_BYTE 0x55
134 #define CURRENT3_REG_SOURCE_VOLTAGE_HIGH_BYTE 0x58
135 #define CURRENT3_REG_SOURCE_VOLTAGE_LOW_BYTE 0x59
136 #define CURRENT3_REG_POWER_RATIO_HIGH_BYTE 0x5B
137 #define CURRENT3_REG_POWER_RATIO_LOW_BYTE 0x5C
138 
144 #define CURRENT3_REG_SENSE_VOLTAGE_HIGH_LIMIT 0x60
145 #define CURRENT3_REG_SENSE_VOLTAGE_LOW_LIMIT 0x61
146 #define CURRENT3_REG_SOURCE_VOLTAGE_HIGH_LIMIT 0x64
147 #define CURRENT3_REG_SOURCE_VOLTAGE_LOW_LIMIT 0x65
148 #define CURRENT3_REG_SENSE_VOLTAGE_VCRIT_LIMIT 0x66
149 #define CURRENT3_REG_SOURCE_VOLTAGE_VCRIT_LIMIT 0x68
150 #define CURRENT3_REG_SENSE_VCRIT_HYSTERESIS 0x69
151 #define CURRENT3_REG_SOURCE_VOLTAGE_VCRIT_HYSTERESIS 0x6A
152 #define CURRENT3_REG_PRODUCT_FEATURES 0xFC
153 #define CURRENT3_REG_PRODUCT_ID 0xFD
154 #define CURRENT3_REG_SMSC_ID 0xFE
155 #define CURRENT3_REG_REVISION 0xFF
156 
162 #define CURRENT3_DEV_ID 0x39
163 
169 #define CURRENT3_TEMP_INTERNAL_DIODE 0x00
170 #define CURRENT3_TEMP_EXTERNAL_DIODE 0x01
171 
182 #define CURRENT3_PK_ALERT 0x00
183 #define CURRENT3_PK_THERM 0x01
184 
190 #define CURRENT3_V_QUEUE_1 0x00
191 #define CURRENT3_V_QUEUE_2 0x01
192 #define CURRENT3_V_QUEUE_3 0x02
193 #define CURRENT3_V_QUEUE_4 0x03
194 
195 #define CURRENT3_V_AVG_DISABLE 0x00
196 #define CURRENT3_V_AVG_2x 0x01
197 #define CURRENT3_V_AVG_4x 0x02
198 #define CURRENT3_V_AVG_8x 0x03
199 
211 #define CURRENT3_NUM_OF_CONS_OUT_LIMIT_MEASUR_1 0x00
212 #define CURRENT3_NUM_OF_CONS_OUT_LIMIT_MEASUR_2 0x01
213 #define CURRENT3_NUM_OF_CONS_OUT_LIMIT_MEASUR_3 0x02
214 #define CURRENT3_NUM_OF_CONS_OUT_LIMIT_MEASUR_4 0x03
215 
221 #define CURRENT3_CURRENT_SENSE_AVG_1X 0x00
222 #define CURRENT3_CURRENT_SENSE_AVG_2X 0x01
223 #define CURRENT3_CURRENT_SENSE_AVG_4X 0x02
224 #define CURRENT3_CURRENT_SENSE_AVG_8X 0x03
225 
231 #define CURRENT3_CS_SAMP_TIME_82ms 0x00
232 #define CURRENT3_CS_SAMP_TIME_164ms 0x02
233 #define CURRENT3_CS_SAMP_TIME_328ms 0x03
234 
240 #define CURRENT3_SENSOR_RANGE_BIT_MASK 0x03
241 #define CURRENT3_SENSOR_RANGE_10_mV 0x00
242 #define CURRENT3_SENSOR_RANGE_20_mV 0x01
243 #define CURRENT3_SENSOR_RANGE_40_mV 0x02
244 #define CURRENT3_SENSOR_RANGE_80_mV 0x03
245 
257 #define CURRENT3_MASK_ALL_ALERT_NOT_MASKED 0x00
258 #define CURRENT3_MASK_ALL_ALERT_INTERRUPT_MODE 0x01
259 
265 #define CURRENT3_TMEAS_STOP_MEASURING_ALL 0x00
266 #define CURRENT3_TMEAS_STOP_NOT_MEASURING 0x01
267 
273 #define CURRENT3_ALERT_COMP_MODE_INTERRUPT 0x00
274 #define CURRENT3_ALERT_COMP_MODE_COMPARATOR 0x01
275 
281 #define CURRENT3_DIS_REC1_ENABLED 0x00
282 #define CURRENT3_DIS_REC1_DISABLED 0x01
283 
289 #define CURRENT3_IMEAS_SOURCE_SENSE_VOLTAGE_STOP_MEASUR 0x00
290 #define CURRENT3_IMEAS_SOURCE_SENSE_VOLTAGE_START_MEASUR 0x01
291 
297 #define CURRENT3_DAVG_DIS_ENABLED 0x00
298 #define CURRENT3_DAVG_DIS_DISABLED 0x01
299 
305 #define CURRENT3_SENSOR_RANGE_VALUE_10_mV 10.0
306 #define CURRENT3_SENSOR_RANGE_VALUE_20_mV 20.0
307 #define CURRENT3_SENSOR_RANGE_VALUE_40_mV 40.0
308 #define CURRENT3_SENSOR_RANGE_VALUE_80_mV 80.0
309 #define CURRENT3_DEFAULT_RSENSE 10.0
310 #define CURRENT3_FULL_SCALE_SENSE_VOLTAGE 2047
311 #define CURRENT3_CON_FCT_mV_TO_V 1000
312 #define CURRENT3_SOURCE_VOLTAGE_CALC_FACT 0.0117
313 #define CURRENT3_TEMPERATURE_CALC_FACT 0.125
314  // End group macro
317 // --------------------------------------------------------------- PUBLIC TYPES
326 typedef struct
327 {
328  // Input pins
329 
330  digital_in_t trm;
331  digital_in_t alr;
332 
333  // Modules
334 
335  i2c_master_t i2c;
336 
337  // ctx variable
338 
339  uint8_t slave_address;
340 
341 } current3_t;
342 
346 typedef struct
347 {
348  // Communication gpio pins
349 
350  pin_name_t scl;
351  pin_name_t sda;
352 
353  // Additional gpio pins
354 
355  pin_name_t trm;
356  pin_name_t alr;
357 
358  // static variable
359 
360  uint32_t i2c_speed;
361  uint8_t i2c_address;
362 
364 
368 typedef struct
369 {
370  uint8_t mask_all;
371  uint8_t tmeas_stop;
372  uint8_t alert_comp;
373  uint8_t dis_rec1;
374  uint8_t imeas_stop;
375  uint8_t davg_dis;
376 }
378 
382 typedef struct
383 {
384  uint8_t busy_data;
385  uint8_t peak_data;
386  uint8_t high_data;
387  uint8_t low_data;
388  uint8_t fault_data;
389  uint8_t crit_data;
390 }
392 
396 typedef struct
397 {
398  uint8_t pk_alert_therm;
399  uint8_t v_queue;
400  uint8_t v_avg;
401 }
403 
407 typedef struct
408 {
409  uint8_t cs_queue;
410  uint8_t cs_samp_avg;
411  uint8_t cs_samp_time;
412  uint8_t cs_rng;
413 }
415  // End types group
417 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
418 
424 #ifdef __cplusplus
425 extern "C"{
426 #endif
427 
437 
449 
458 
472 err_t current3_generic_write ( current3_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
473 
487 err_t current3_generic_read ( current3_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
488 
505 float current3_get_temperature ( current3_t *ctx, uint8_t temp_sel );
506 
517 
528 
539 
550 
561 
572 
583 
602 
614 
627 
639 
654 
655 #ifdef __cplusplus
656 }
657 #endif
658 #endif // _CURRENT3_H_
659  // End public_function group
662 
663 // ------------------------------------------------------------------------- END
current3_generic_read
err_t current3_generic_read(current3_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Generic read function.
current3_init
err_t current3_init(current3_t *ctx, current3_cfg_t *cfg)
Initialization function.
current3_check_therm
uint8_t current3_check_therm(current3_t *ctx)
Check therm ststus function.
current3_status_data_t::busy_data
uint8_t busy_data
Definition: current3.h:384
current3_cfg_data_t::mask_all
uint8_t mask_all
Definition: current3.h:370
current3_default_cfg
void current3_default_cfg(current3_t *ctx, current3_sense_cfg_data_t sense_cfg_data)
Click Default Configuration function.
current3_set_configuration
void current3_set_configuration(current3_t *ctx, current3_cfg_data_t cfg_data)
Set configuration function.
current3_set_voltage_sampling_config
void current3_set_voltage_sampling_config(current3_t *ctx, current3_voltage_cfg_data_t vtg_cfg_data)
Set voltage sampling configuration function.
current3_status_data_t
Click status data structure definition.
Definition: current3.h:383
current3_get_sense_voltage
int16_t current3_get_sense_voltage(current3_t *ctx)
Get sense voltage function.
current3_status_data_t::peak_data
uint8_t peak_data
Definition: current3.h:385
current3_cfg_t::trm
pin_name_t trm
Definition: current3.h:355
current3_get_current
float current3_get_current(current3_t *ctx)
Get current measurement function.
current3_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: current3.h:360
current3_cfg_t::sda
pin_name_t sda
Definition: current3.h:351
current3_t::alr
digital_in_t alr
Definition: current3.h:331
current3_status_data_t::low_data
uint8_t low_data
Definition: current3.h:387
current3_status_data_t::crit_data
uint8_t crit_data
Definition: current3.h:389
current3_set_sense_sampling_config
void current3_set_sense_sampling_config(current3_t *ctx, current3_sense_cfg_data_t sense_cfg_data)
Set sense sampling configuration function.
current3_voltage_cfg_data_t::v_avg
uint8_t v_avg
Definition: current3.h:400
current3_get_temperature
float current3_get_temperature(current3_t *ctx, uint8_t temp_sel)
Get temperature function.
current3_voltage_cfg_data_t::pk_alert_therm
uint8_t pk_alert_therm
Definition: current3.h:398
current3_sense_cfg_data_t::cs_rng
uint8_t cs_rng
Definition: current3.h:412
current3_voltage_cfg_data_t::v_queue
uint8_t v_queue
Definition: current3.h:399
current3_cfg_data_t::imeas_stop
uint8_t imeas_stop
Definition: current3.h:374
current3_get_source_voltage
float current3_get_source_voltage(current3_t *ctx)
Get source voltage function.
current3_cfg_t
Click configuration structure definition.
Definition: current3.h:347
current3_get_voltage_sampling_config
void current3_get_voltage_sampling_config(current3_t *ctx, current3_voltage_cfg_data_t *vtg_cfg_data)
Get voltage sampling configuration function.
current3_cfg_t::scl
pin_name_t scl
Definition: current3.h:350
current3_voltage_cfg_data_t
Click voltage configuration structure definition.
Definition: current3.h:397
current3_t::slave_address
uint8_t slave_address
Definition: current3.h:339
current3_cfg_t::alr
pin_name_t alr
Definition: current3.h:356
current3_cfg_t::i2c_address
uint8_t i2c_address
Definition: current3.h:361
current3_t
Click ctx object definition.
Definition: current3.h:327
current3_check_alert
uint8_t current3_check_alert(current3_t *ctx)
Check alert ststus function.
current3_generic_write
err_t current3_generic_write(current3_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Generic write function.
current3_t::trm
digital_in_t trm
Definition: current3.h:330
current3_get_status
void current3_get_status(current3_t *ctx, current3_status_data_t *status_cfg)
Get status function.
current3_sense_cfg_data_t::cs_samp_time
uint8_t cs_samp_time
Definition: current3.h:411
current3_sense_cfg_data_t
Click sense configuration structure definition.
Definition: current3.h:408
current3_cfg_data_t
Click configuration data definition.
Definition: current3.h:369
current3_sense_cfg_data_t::cs_samp_avg
uint8_t cs_samp_avg
Definition: current3.h:410
current3_status_data_t::fault_data
uint8_t fault_data
Definition: current3.h:388
current3_cfg_data_t::tmeas_stop
uint8_t tmeas_stop
Definition: current3.h:371
current3_sense_cfg_data_t::cs_queue
uint8_t cs_queue
Definition: current3.h:409
current3_get_sense_sampling_config
void current3_get_sense_sampling_config(current3_t *ctx, current3_sense_cfg_data_t *sense_cfg_data)
Get sense sampling configuration function.
current3_status_data_t::high_data
uint8_t high_data
Definition: current3.h:386
current3_t::i2c
i2c_master_t i2c
Definition: current3.h:335
current3_cfg_setup
void current3_cfg_setup(current3_cfg_t *cfg)
Config Object Initialization function.
current3_cfg_data_t::davg_dis
uint8_t davg_dis
Definition: current3.h:375
current3_get_configuration
void current3_get_configuration(current3_t *ctx, current3_cfg_data_t *cfg_data)
Get configuration function.
current3_cfg_data_t::alert_comp
uint8_t alert_comp
Definition: current3.h:372
current3_cfg_data_t::dis_rec1
uint8_t dis_rec1
Definition: current3.h:373