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
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 PAC1934_MAP_MIKROBUS( cfg, mikrobus ) \
67 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
68 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
69 cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
70 cfg.ale = MIKROBUS( mikrobus, MIKROBUS_INT )
77#define PAC1934_RETVAL uint8_t
78
79#define PAC1934_OK 0x00
80#define PAC1934_INIT_ERROR 0xFF
87#define PAC1934_I2C_ADDR 0x10
94#define PAC1934_REFRESH_CMD 0x00
101#define PAC1934_CTRL_REG 0x01
102
103#define PAC1934_CTRL_SAMPLE_RATE_8 0xC0
104#define PAC1934_CTRL_SAMPLE_RATE_64 0x80
105#define PAC1934_CTRL_SAMPLE_RATE_256 0x40
106#define PAC1934_CTRL_SAMPLE_RATE_1024 0x00
107#define PAC1934_CTRL_SINGLE_SHOT_MODE 0x10
114#define PAC1934_ACC_COUNT 0x02
121#define PAC1934_VPOWER1_ACC 0x03
122#define PAC1934_VPOWER2_ACC 0x04
123#define PAC1934_VPOWER3_ACC 0x05
124#define PAC1934_VPOWER4_ACC 0x06
131#define PAC1934_VBUS1 0x07
132#define PAC1934_VBUS2 0x08
133#define PAC1934_VBUS3 0x09
134#define PAC1934_VBUS4 0x0A
141#define PAC1934_VSENSE1 0x0B
142#define PAC1934_VSENSE2 0x0C
143#define PAC1934_VSENSE3 0x0D
144#define PAC1934_VSENSE4 0x0E
151#define PAC1934_VBUS1_AVG 0x0F
152#define PAC1934_VBUS2_AVG 0x10
153#define PAC1934_VBUS3_AVG 0x11
154#define PAC1934_VBUS4_AVG 0x12
161#define PAC1934_VSENSE1_AVG 0x13
162#define PAC1934_VSENSE2_AVG 0x14
163#define PAC1934_VSENSE3_AVG 0x15
164#define PAC1934_VSENSE4_AVG 0x16
171#define PAC1934_VPOWER1 0x17
172#define PAC1934_VPOWER2 0x18
173#define PAC1934_VPOWER3 0x19
174#define PAC1934_VPOWER4 0x1A
181#define PAC1934_CHANNEL_DIS 0x1C
182#define PAC1934_CHANNEL_DIS_ALL_CHA 0xF0
189#define PAC1934_NEG_PWR 0x1D
196#define PAC1934_REFRESH_G_CMD 0x1E
203#define PAC1934_REFRESH_V_CMD 0x1F
211#define PAC1934_SLOW 0x20
218#define PAC1934_CTRL_ACT 0x21
225#define PAC1934_DIS_ACT 0x22
232#define PAC1934_NEG_PWR_ACT 0x23
239#define PAC1934_CTRL_LAT 0x24
246#define PAC1934_DIS_LAT 0x25
253#define PAC1934_NEG_PWR_LAT 0x26
260#define PAC1934_PRODUCT_ID 0xFD
267#define PAC1934_MANUFACT_ID 0xFE
274#define PAC1934_REVISION_ID 0xFF
277 // End group macro
278// --------------------------------------------------------------- PUBLIC TYPES
287typedef struct
288{
289 // Output pins
290
291 digital_out_t rst;
292
293 // Input pins
294
295 digital_in_t ale;
296
297 // Modules
298
299 i2c_master_t i2c;
300
301 // ctx variable
302
304
305
306} pac1934_t;
307
311typedef 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 rst;
321
322 pin_name_t ale;
323
324 // static variable
325
326 uint32_t i2c_speed;
327 uint8_t i2c_address;
328
330
331 // End types group
332
333// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
334
340#ifdef __cplusplus
341extern "C"{
342#endif
343
353
373void pac1934_generic_write ( pac1934_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
374
385void pac1934_generic_read ( pac1934_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
386
397void pac1934_write_byte ( pac1934_t *ctx, uint8_t wr_addr, uint8_t wr_data );
398
409uint8_t pac1934_read_byte ( pac1934_t *ctx, uint8_t rd_addr );
410
421uint16_t pac1934_read_two_byte ( pac1934_t *ctx, uint8_t rd_addr );
422
433uint32_t pac1934_read_four_byte ( pac1934_t *ctx, uint8_t rd_addr );
434
445void pac1934_read_reg( pac1934_t *ctx, uint8_t reg_addr, uint8_t *output_data, uint8_t cnt );
446
458void pac1934_send_command ( pac1934_t *ctx, uint8_t wr_cmd );
459
475float pac1934_measure_voltage ( pac1934_t *ctx, uint8_t channel );
476
492float pac1934_measure_current ( pac1934_t *ctx, uint8_t channel );
493
509float pac1934_measure_power ( pac1934_t *ctx, uint8_t channel );
510
522float pac1934_calc_power ( pac1934_t *ctx, float voltage, float amperage );
523
538float pac1934_measure_energy ( pac1934_t *ctx, uint8_t chann, uint16_t samp_rate );
539
548
557
568
579
580#ifdef __cplusplus
581}
582#endif
583#endif // _PAC1934_H_
584
585 // End public_function group
587
588// ------------------------------------------------------------------------- END
#define PAC1934_RETVAL
Definition pac1934.h:77
uint8_t pac1934_check_interrupt(pac1934_t *ctx)
Check Interrupt function.
float pac1934_measure_voltage(pac1934_t *ctx, uint8_t channel)
Measure Voltage function.
PAC1934_RETVAL pac1934_init(pac1934_t *ctx, pac1934_cfg_t *cfg)
Initialization function.
void pac1934_read_reg(pac1934_t *ctx, uint8_t reg_addr, uint8_t *output_data, uint8_t cnt)
Read chosen number of bytes function.
void pac1934_generic_write(pac1934_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
float pac1934_measure_current(pac1934_t *ctx, uint8_t channel)
Measure Current function.
uint16_t pac1934_read_two_byte(pac1934_t *ctx, uint8_t rd_addr)
Read two bytes function.
float pac1934_measure_energy(pac1934_t *ctx, uint8_t chann, uint16_t samp_rate)
Measure Energy function.
float pac1934_calc_power(pac1934_t *ctx, float voltage, float amperage)
Calculate Power function.
float pac1934_measure_power(pac1934_t *ctx, uint8_t channel)
Measure Power function.
void pac1934_dev_enable(pac1934_t *ctx)
Enable device function.
void pac1934_write_byte(pac1934_t *ctx, uint8_t wr_addr, uint8_t wr_data)
Write one byte function.
uint32_t pac1934_read_four_byte(pac1934_t *ctx, uint8_t rd_addr)
Read four bytes function.
void pac1934_generic_read(pac1934_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
void pac1934_send_command(pac1934_t *ctx, uint8_t wr_cmd)
Send Command.
void pac1934_cfg_setup(pac1934_cfg_t *cfg)
Config Object Initialization function.
uint8_t pac1934_read_byte(pac1934_t *ctx, uint8_t rd_addr)
Read one byte function.
void pac1934_dev_disable(pac1934_t *ctx)
Disable device function.
void pac1934_dev_reset(pac1934_t *ctx)
Reset device function.
Click configuration structure definition.
Definition pac1934.h:312
uint32_t i2c_speed
Definition pac1934.h:326
pin_name_t ale
Definition pac1934.h:322
pin_name_t scl
Definition pac1934.h:315
pin_name_t sda
Definition pac1934.h:316
pin_name_t rst
Definition pac1934.h:320
uint8_t i2c_address
Definition pac1934.h:327
Click ctx object definition.
Definition pac1934.h:288
digital_in_t ale
Definition pac1934.h:295
i2c_master_t i2c
Definition pac1934.h:299
digital_out_t rst
Definition pac1934.h:291
uint8_t slave_address
Definition pac1934.h:303