pac1921 2.0.0.0
pac1921.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 */
32// ----------------------------------------------------------------------------
33
34#ifndef PAC1921_H
35#define PAC1921_H
36
41#ifdef PREINIT_SUPPORTED
42#include "preinit.h"
43#endif
44
45#ifdef MikroCCoreVersion
46 #if MikroCCoreVersion >= 1
47 #include "delays.h"
48 #endif
49#endif
50
51#include "drv_digital_out.h"
52#include "drv_digital_in.h"
53#include "drv_i2c_master.h"
54
55// -------------------------------------------------------------- PUBLIC MACROS
65#define PAC1921_MAP_MIKROBUS( cfg, mikrobus ) \
66 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
67 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
68 cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN ); \
69 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
76#define PAC1921_RETVAL uint8_t
77
78#define PAC1921_OK 0x00
79#define PAC1921_INIT_ERROR 0xFF
86#define PAC1921_SLAVE_ADDRESS 0x4C
93#define PAC1921_GAIN_CFG_REG 0x00
94#define PAC1921_INTEGRATION_CFG_REG 0x01
95#define PAC1921_CONTROL_REG 0x02
96#define PAC1921_VBUS_RESULT_HIGH 0x10
97#define PAC1921_VBUS_RESULT_LOW 0x11
98#define PAC1921_VSENSE_RESULT_HIGH 0x12
99#define PAC1921_VSENSE_RESULT_LOW 0x13
100#define PAC1921_VSUM_ACCUMULATOR_HIGH 0x14
101#define PAC1921_VSUM_ACCUMULATOR_MID_H 0x15
102#define PAC1921_VSUM_ACCUMULATOR_MID_L 0x16
103#define PAC1921_VSUM_ACCUMULATOR_LOW 0x17
104#define PAC1921_ISUM_ACCUMULATOR_HIGH 0x18
105#define PAC1921_ISUM_ACCUMULATOR_MID_H 0x19
106#define PAC1921_ISUM_ACCUMULATOR_MID_L 0x1A
107#define PAC1921_ISUM_ACCUMULATOR_LOW 0x1B
108#define PAC1921_OVERFLOW_STATUS 0x1C
109#define PAC1921_VPOWER_RESULT_HIGH 0x1D
110#define PAC1921_VPOWER_RESULT_LOW 0x1E
111#define PAC1921_SAMPLES_HIGH 0x21
112#define PAC1921_SAMPLES_LOW 0x22
113#define PAC1921_PSUM_ACCUMULATOR_HIGH 0x23
114#define PAC1921_PSUM_ACCUMULATOR_MID_H 0x24
115#define PAC1921_PSUM_ACCUMULATOR_MID_M 0x25
116#define PAC1921_PSUM_ACCUMULATOR_MID_L 0x26
117#define PAC1921_PSUM_ACCUMULATOR_LOW 0x27
118#define PAC1921_PRODUCT_ID 0xFD
119#define PAC1921_MANUFACTURER_ID 0xFE
120#define PAC1921_REVISION 0xFF
127#define PAC1921_I_RESOLUTION_11_BIT 0x80
128#define PAC1921_I_RESOLUTION_14_BIT 0x00
129#define PAC1921_V_RESOLUTION_11_BIT 0x40
130#define PAC1921_V_RESOLUTION_14_BIT 0x00
131#define PAC1921_DIGITAL_I_GAIN_1X 0x00
132#define PAC1921_DIGITAL_I_GAIN_2X 0x08
133#define PAC1921_DIGITAL_I_GAIN_4X 0x10
134#define PAC1921_DIGITAL_I_GAIN_8X 0x18
135#define PAC1921_DIGITAL_I_GAIN_16X 0x20
136#define PAC1921_DIGITAL_I_GAIN_32X 0x28
137#define PAC1921_DIGITAL_I_GAIN_64X 0x30
138#define PAC1921_DIGITAL_I_GAIN_128X 0x38
139#define PAC1921_DIGITAL_V_GAIN_1X 0x00
140#define PAC1921_DIGITAL_V_GAIN_2X 0x01
141#define PAC1921_DIGITAL_V_GAIN_4X 0x02
142#define PAC1921_DIGITAL_V_GAIN_8X 0x03
143#define PAC1921_DIGITAL_V_GAIN_16X 0x04
144#define PAC1921_DIGITAL_V_GAIN_32X 0x05
145#define PAC1921_DIGITAL_V_GAIN_64X 0x06
146#define PAC1921_DIGITAL_V_GAIN_128X 0x07
153#define PAC1921_SAMPLE_RATE_1 0x00
154#define PAC1921_SAMPLE_RATE_2 0x10
155#define PAC1921_SAMPLE_RATE_4 0x20
156#define PAC1921_SAMPLE_RATE_8 0x30
157#define PAC1921_SAMPLE_RATE_16 0x40
158#define PAC1921_SAMPLE_RATE_32 0x50
159#define PAC1921_SAMPLE_RATE_64 0x60
160#define PAC1921_SAMPLE_RATE_128 0x70
161#define PAC1921_SAMPLE_RATE_256 0x80
162#define PAC1921_SAMPLE_RATE_512 0x90
163#define PAC1921_SAMPLE_RATE_1024 0xA0
164#define PAC1921_SAMPLE_RATE_2048 0xB0
165#define PAC1921_V_SENSE_FILTER_ON 0x08
166#define PAC1921_V_SENSE_FILTER_OFF 0x00
167#define PAC1921_V_BUS_FILTER_ON 0x04
168#define PAC1921_V_BUS_FILTER_OFF 0x00
169#define PAC1921_READ_INT_OVERRIDE_ON 0x02
170#define PAC1921_READ_INT_OVERRIDE_OFF 0x00
171#define PAC1921_FORCED_INTEGRATE_MODE_ON 0x01
172#define PAC1921_FORCED_INTEGRATE_MODE_OFF 0x00
179#define PAC1921_MEASUREMENT_MODE_V_POWER_PIN_CTRL 0x00
180#define PAC1921_MEASUREMENT_MODE_V_SENSE_FREE_RUN 0x40
181#define PAC1921_MEASUREMENT_MODE_V_BUS_FREE_RUN 0x80
182#define PAC1921_MEASUREMENT_MODE_V_POWER_FREE_RUN 0xC0
183#define PAC1921_MEASUREMENT_MODE_MASK 0xC0
184#define PAC1921_OUT_PIN_FULL_SCALE_RANGE_3V 0x00
185#define PAC1921_OUT_PIN_FULL_SCALE_RANGE_2V 0x10
186#define PAC1921_OUT_PIN_FULL_SCALE_RANGE_1_5_V 0x20
187#define PAC1921_OUT_PIN_FULL_SCALE_RANGE_1V 0x30
188#define PAC1921_TIMEOUT_ON 0x08
189#define PAC1921_TIMEOUT_OFF 0x00
190#define PAC1921_SLEEP_ON 0x04
191#define PAC1921_SLEEP_OFF 0x00
192#define PAC1921_FORCED_READ_MODE_ON 0x02
193#define PAC1921_FORCED_READ_MODE_OFF 0x00
194#define PAC1921_FORCED_RECALCULATE_MODE_ON 0x01
195#define PAC1921_FORCED_RECALCULATE_MODE_OFF 0x00
202#define PAC1921_VOLTAGE_MAX 10
203#define PAC1921_CURRENT_MAX 32
206 // End group macro
207// --------------------------------------------------------------- PUBLIC TYPES
216typedef struct
217{
218 uint8_t ctrl_byte;
219 uint8_t cfg_byte;
222 uint8_t iarp;
223 uint16_t output_fsr;
224
225} aux_vars_t;
226
230typedef struct
231{
232 uint8_t ctrl_byte;
233 uint8_t cfg_byte;
236 uint8_t iarp;
237 uint16_t output_fsr;
238
239} aux_cfg_t;
240
244typedef struct
245{
246 // Output pins
247
248 digital_out_t int_pin;
249
250 // Input pins
251
252 digital_in_t an;
253
254 // Modules
255
256 i2c_master_t i2c;
257
258 // ctx variable
259
262
263} pac1921_t;
264
268typedef struct
269{
270 // Communication gpio pins
271
272 pin_name_t scl;
273 pin_name_t sda;
274
275 // Additional gpio pins
276
277 pin_name_t an;
278 pin_name_t int_pin;
279
280 // static variable
281
282 uint32_t i2c_speed;
283 uint8_t i2c_address;
285
287
288 // End types group
289// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
290
296#ifdef __cplusplus
297extern "C"{
298#endif
299
309
319
328
339void pac1921_generic_write ( pac1921_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
340
352void pac1921_generic_read ( pac1921_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
353
365void pac1921_write_to_reg ( pac1921_t *ctx, uint8_t reg_addr, uint8_t *write_data, uint8_t len );
366
377float pac1921_get_measured_data ( pac1921_t *ctx, uint8_t measurement_mode, uint8_t sample_num );
378
387void pac1921_set_int_pin ( pac1921_t *ctx, uint8_t output );
388
397
398#ifdef __cplusplus
399}
400#endif
401#endif // _PAC1921_H_
402
403 // End public_function group
405
406// ------------------------------------------------------------------------- END
#define PAC1921_RETVAL
Definition pac1921.h:76
void pac1921_cfg_setup(pac1921_cfg_t *cfg)
Config Object Initialization function.
uint8_t pac1921_get_an_pin(pac1921_t *ctx)
AN pin get input function.
float pac1921_get_measured_data(pac1921_t *ctx, uint8_t measurement_mode, uint8_t sample_num)
Measured data read function.
PAC1921_RETVAL pac1921_init(pac1921_t *ctx, pac1921_cfg_t *cfg)
Initialization function.
void pac1921_write_to_reg(pac1921_t *ctx, uint8_t reg_addr, uint8_t *write_data, uint8_t len)
Register write function.
void pac1921_set_int_pin(pac1921_t *ctx, uint8_t output)
INT pin set output function.
void pac1921_default_cfg(pac1921_t *ctx)
Click Default Configuration function.
void pac1921_generic_write(pac1921_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
void pac1921_generic_read(pac1921_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
Auxiliary variables default cfg.
Definition pac1921.h:231
uint8_t iarp
Definition pac1921.h:236
uint8_t cfg_byte
Definition pac1921.h:233
uint8_t ctrl_byte
Definition pac1921.h:232
uint16_t output_fsr
Definition pac1921.h:237
uint8_t measurement_mode_old
Definition pac1921.h:234
uint8_t sample_num_old
Definition pac1921.h:235
Auxiliary variables structure.
Definition pac1921.h:217
uint8_t iarp
Definition pac1921.h:222
uint8_t cfg_byte
Definition pac1921.h:219
uint8_t ctrl_byte
Definition pac1921.h:218
uint16_t output_fsr
Definition pac1921.h:223
uint8_t measurement_mode_old
Definition pac1921.h:220
uint8_t sample_num_old
Definition pac1921.h:221
Click configuration structure definition.
Definition pac1921.h:269
uint32_t i2c_speed
Definition pac1921.h:282
aux_cfg_t aux_cfg
Definition pac1921.h:284
pin_name_t scl
Definition pac1921.h:272
pin_name_t int_pin
Definition pac1921.h:278
pin_name_t sda
Definition pac1921.h:273
pin_name_t an
Definition pac1921.h:277
uint8_t i2c_address
Definition pac1921.h:283
Click ctx object definition.
Definition pac1921.h:245
aux_vars_t aux
Definition pac1921.h:261
i2c_master_t i2c
Definition pac1921.h:256
digital_in_t an
Definition pac1921.h:252
digital_out_t int_pin
Definition pac1921.h:248
uint8_t slave_address
Definition pac1921.h:260