pac1934  2.0.0.0
pac1934.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 PAC1934_H
36 #define PAC1934_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 
42 // -------------------------------------------------------------- PUBLIC MACROS
52 #define PAC1934_MAP_MIKROBUS( cfg, mikrobus ) \
53  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
54  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
55  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
56  cfg.ale = MIKROBUS( mikrobus, MIKROBUS_INT )
57 
63 #define PAC1934_RETVAL uint8_t
64 
65 #define PAC1934_OK 0x00
66 #define PAC1934_INIT_ERROR 0xFF
67 
73 #define PAC1934_I2C_ADDR 0x10
74 
80 #define PAC1934_REFRESH_CMD 0x00
81 
87 #define PAC1934_CTRL_REG 0x01
88 
89 #define PAC1934_CTRL_SAMPLE_RATE_8 0xC0
90 #define PAC1934_CTRL_SAMPLE_RATE_64 0x80
91 #define PAC1934_CTRL_SAMPLE_RATE_256 0x40
92 #define PAC1934_CTRL_SAMPLE_RATE_1024 0x00
93 #define PAC1934_CTRL_SINGLE_SHOT_MODE 0x10
94 
100 #define PAC1934_ACC_COUNT 0x02
101 
107 #define PAC1934_VPOWER1_ACC 0x03
108 #define PAC1934_VPOWER2_ACC 0x04
109 #define PAC1934_VPOWER3_ACC 0x05
110 #define PAC1934_VPOWER4_ACC 0x06
111 
117 #define PAC1934_VBUS1 0x07
118 #define PAC1934_VBUS2 0x08
119 #define PAC1934_VBUS3 0x09
120 #define PAC1934_VBUS4 0x0A
121 
127 #define PAC1934_VSENSE1 0x0B
128 #define PAC1934_VSENSE2 0x0C
129 #define PAC1934_VSENSE3 0x0D
130 #define PAC1934_VSENSE4 0x0E
131 
137 #define PAC1934_VBUS1_AVG 0x0F
138 #define PAC1934_VBUS2_AVG 0x10
139 #define PAC1934_VBUS3_AVG 0x11
140 #define PAC1934_VBUS4_AVG 0x12
141 
147 #define PAC1934_VSENSE1_AVG 0x13
148 #define PAC1934_VSENSE2_AVG 0x14
149 #define PAC1934_VSENSE3_AVG 0x15
150 #define PAC1934_VSENSE4_AVG 0x16
151 
157 #define PAC1934_VPOWER1 0x17
158 #define PAC1934_VPOWER2 0x18
159 #define PAC1934_VPOWER3 0x19
160 #define PAC1934_VPOWER4 0x1A
161 
167 #define PAC1934_CHANNEL_DIS 0x1C
168 #define PAC1934_CHANNEL_DIS_ALL_CHA 0xF0
169 
175 #define PAC1934_NEG_PWR 0x1D
176 
182 #define PAC1934_REFRESH_G_CMD 0x1E
183 
189 #define PAC1934_REFRESH_V_CMD 0x1F
190 
197 #define PAC1934_SLOW 0x20
198 
204 #define PAC1934_CTRL_ACT 0x21
205 
211 #define PAC1934_DIS_ACT 0x22
212 
218 #define PAC1934_NEG_PWR_ACT 0x23
219 
225 #define PAC1934_CTRL_LAT 0x24
226 
232 #define PAC1934_DIS_LAT 0x25
233 
239 #define PAC1934_NEG_PWR_LAT 0x26
240 
246 #define PAC1934_PRODUCT_ID 0xFD
247 
253 #define PAC1934_MANUFACT_ID 0xFE
254 
260 #define PAC1934_REVISION_ID 0xFF
261  // End group macro
264 // --------------------------------------------------------------- PUBLIC TYPES
273 typedef struct
274 {
275  // Output pins
276 
277  digital_out_t rst;
278 
279  // Input pins
280 
281  digital_in_t ale;
282 
283  // Modules
284 
285  i2c_master_t i2c;
286 
287  // ctx variable
288 
289  uint8_t slave_address;
290 
291 
292 } pac1934_t;
293 
297 typedef struct
298 {
299  // Communication gpio pins
300 
301  pin_name_t scl;
302  pin_name_t sda;
303 
304  // Additional gpio pins
305 
306  pin_name_t rst;
307 
308  pin_name_t ale;
309 
310  // static variable
311 
312  uint32_t i2c_speed;
313  uint8_t i2c_address;
314 
315 } pac1934_cfg_t;
316  // End types group
318 
319 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
320 
326 #ifdef __cplusplus
327 extern "C"{
328 #endif
329 
338 void pac1934_cfg_setup ( pac1934_cfg_t *cfg );
339 
359 void pac1934_generic_write ( pac1934_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
360 
371 void pac1934_generic_read ( pac1934_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
372 
383 void pac1934_write_byte ( pac1934_t *ctx, uint8_t wr_addr, uint8_t wr_data );
384 
395 uint8_t pac1934_read_byte ( pac1934_t *ctx, uint8_t rd_addr );
396 
407 uint16_t pac1934_read_two_byte ( pac1934_t *ctx, uint8_t rd_addr );
408 
419 uint32_t pac1934_read_four_byte ( pac1934_t *ctx, uint8_t rd_addr );
420 
431 void pac1934_read_reg( pac1934_t *ctx, uint8_t reg_addr, uint8_t *output_data, uint8_t cnt );
432 
444 void pac1934_send_command ( pac1934_t *ctx, uint8_t wr_cmd );
445 
461 float pac1934_measure_voltage ( pac1934_t *ctx, uint8_t channel );
462 
478 float pac1934_measure_current ( pac1934_t *ctx, uint8_t channel );
479 
495 float pac1934_measure_power ( pac1934_t *ctx, uint8_t channel );
496 
508 float pac1934_calc_power ( pac1934_t *ctx, float voltage, float amperage );
509 
524 float pac1934_measure_energy ( pac1934_t *ctx, uint8_t chann, uint16_t samp_rate );
525 
533 void pac1934_dev_enable ( pac1934_t *ctx );
534 
542 void pac1934_dev_disable ( pac1934_t *ctx );
543 
553 void pac1934_dev_reset ( pac1934_t *ctx );
554 
564 uint8_t pac1934_check_interrupt ( pac1934_t *ctx );
565 
566 #ifdef __cplusplus
567 }
568 #endif
569 #endif // _PAC1934_H_
570  // End public_function group
573 
574 // ------------------------------------------------------------------------- END
pac1934_t::slave_address
uint8_t slave_address
Definition: pac1934.h:289
pac1934_init
PAC1934_RETVAL pac1934_init(pac1934_t *ctx, pac1934_cfg_t *cfg)
Initialization function.
pac1934_check_interrupt
uint8_t pac1934_check_interrupt(pac1934_t *ctx)
Check Interrupt function.
pac1934_calc_power
float pac1934_calc_power(pac1934_t *ctx, float voltage, float amperage)
Calculate Power function.
pac1934_cfg_t::i2c_address
uint8_t i2c_address
Definition: pac1934.h:313
pac1934_write_byte
void pac1934_write_byte(pac1934_t *ctx, uint8_t wr_addr, uint8_t wr_data)
Write one byte function.
pac1934_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: pac1934.h:312
pac1934_dev_disable
void pac1934_dev_disable(pac1934_t *ctx)
Disable device function.
pac1934_cfg_t::ale
pin_name_t ale
Definition: pac1934.h:308
pac1934_cfg_t::rst
pin_name_t rst
Definition: pac1934.h:306
pac1934_send_command
void pac1934_send_command(pac1934_t *ctx, uint8_t wr_cmd)
Send Command.
pac1934_t::i2c
i2c_master_t i2c
Definition: pac1934.h:285
pac1934_cfg_t
Click configuration structure definition.
Definition: pac1934.h:297
pac1934_cfg_t::scl
pin_name_t scl
Definition: pac1934.h:301
PAC1934_RETVAL
#define PAC1934_RETVAL
Definition: pac1934.h:63
pac1934_read_byte
uint8_t pac1934_read_byte(pac1934_t *ctx, uint8_t rd_addr)
Read one byte function.
pac1934_dev_enable
void pac1934_dev_enable(pac1934_t *ctx)
Enable device function.
pac1934_read_reg
void pac1934_read_reg(pac1934_t *ctx, uint8_t reg_addr, uint8_t *output_data, uint8_t cnt)
Read chosen number of bytes function.
pac1934_cfg_t::sda
pin_name_t sda
Definition: pac1934.h:302
pac1934_cfg_setup
void pac1934_cfg_setup(pac1934_cfg_t *cfg)
Config Object Initialization function.
pac1934_t::ale
digital_in_t ale
Definition: pac1934.h:281
pac1934_measure_power
float pac1934_measure_power(pac1934_t *ctx, uint8_t channel)
Measure Power function.
pac1934_read_four_byte
uint32_t pac1934_read_four_byte(pac1934_t *ctx, uint8_t rd_addr)
Read four bytes function.
pac1934_measure_current
float pac1934_measure_current(pac1934_t *ctx, uint8_t channel)
Measure Current function.
pac1934_t
Click ctx object definition.
Definition: pac1934.h:273
pac1934_dev_reset
void pac1934_dev_reset(pac1934_t *ctx)
Reset device function.
pac1934_read_two_byte
uint16_t pac1934_read_two_byte(pac1934_t *ctx, uint8_t rd_addr)
Read two bytes function.
pac1934_measure_voltage
float pac1934_measure_voltage(pac1934_t *ctx, uint8_t channel)
Measure Voltage function.
pac1934_generic_read
void pac1934_generic_read(pac1934_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
pac1934_measure_energy
float pac1934_measure_energy(pac1934_t *ctx, uint8_t chann, uint16_t samp_rate)
Measure Energy function.
pac1934_t::rst
digital_out_t rst
Definition: pac1934.h:277
pac1934_generic_write
void pac1934_generic_write(pac1934_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.