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  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef PAC1921_H
36 #define PAC1921_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 PAC1921_MAP_MIKROBUS( cfg, mikrobus ) \
53  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
54  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
55  cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN ); \
56  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
57 
63 #define PAC1921_RETVAL uint8_t
64 
65 #define PAC1921_OK 0x00
66 #define PAC1921_INIT_ERROR 0xFF
67 
73 #define PAC1921_SLAVE_ADDRESS 0x4C
74 
80 #define PAC1921_GAIN_CFG_REG 0x00
81 #define PAC1921_INTEGRATION_CFG_REG 0x01
82 #define PAC1921_CONTROL_REG 0x02
83 #define PAC1921_VBUS_RESULT_HIGH 0x10
84 #define PAC1921_VBUS_RESULT_LOW 0x11
85 #define PAC1921_VSENSE_RESULT_HIGH 0x12
86 #define PAC1921_VSENSE_RESULT_LOW 0x13
87 #define PAC1921_VSUM_ACCUMULATOR_HIGH 0x14
88 #define PAC1921_VSUM_ACCUMULATOR_MID_H 0x15
89 #define PAC1921_VSUM_ACCUMULATOR_MID_L 0x16
90 #define PAC1921_VSUM_ACCUMULATOR_LOW 0x17
91 #define PAC1921_ISUM_ACCUMULATOR_HIGH 0x18
92 #define PAC1921_ISUM_ACCUMULATOR_MID_H 0x19
93 #define PAC1921_ISUM_ACCUMULATOR_MID_L 0x1A
94 #define PAC1921_ISUM_ACCUMULATOR_LOW 0x1B
95 #define PAC1921_OVERFLOW_STATUS 0x1C
96 #define PAC1921_VPOWER_RESULT_HIGH 0x1D
97 #define PAC1921_VPOWER_RESULT_LOW 0x1E
98 #define PAC1921_SAMPLES_HIGH 0x21
99 #define PAC1921_SAMPLES_LOW 0x22
100 #define PAC1921_PSUM_ACCUMULATOR_HIGH 0x23
101 #define PAC1921_PSUM_ACCUMULATOR_MID_H 0x24
102 #define PAC1921_PSUM_ACCUMULATOR_MID_M 0x25
103 #define PAC1921_PSUM_ACCUMULATOR_MID_L 0x26
104 #define PAC1921_PSUM_ACCUMULATOR_LOW 0x27
105 #define PAC1921_PRODUCT_ID 0xFD
106 #define PAC1921_MANUFACTURER_ID 0xFE
107 #define PAC1921_REVISION 0xFF
108 
114 #define PAC1921_I_RESOLUTION_11_BIT 0x80
115 #define PAC1921_I_RESOLUTION_14_BIT 0x00
116 #define PAC1921_V_RESOLUTION_11_BIT 0x40
117 #define PAC1921_V_RESOLUTION_14_BIT 0x00
118 #define PAC1921_DIGITAL_I_GAIN_1X 0x00
119 #define PAC1921_DIGITAL_I_GAIN_2X 0x08
120 #define PAC1921_DIGITAL_I_GAIN_4X 0x10
121 #define PAC1921_DIGITAL_I_GAIN_8X 0x18
122 #define PAC1921_DIGITAL_I_GAIN_16X 0x20
123 #define PAC1921_DIGITAL_I_GAIN_32X 0x28
124 #define PAC1921_DIGITAL_I_GAIN_64X 0x30
125 #define PAC1921_DIGITAL_I_GAIN_128X 0x38
126 #define PAC1921_DIGITAL_V_GAIN_1X 0x00
127 #define PAC1921_DIGITAL_V_GAIN_2X 0x01
128 #define PAC1921_DIGITAL_V_GAIN_4X 0x02
129 #define PAC1921_DIGITAL_V_GAIN_8X 0x03
130 #define PAC1921_DIGITAL_V_GAIN_16X 0x04
131 #define PAC1921_DIGITAL_V_GAIN_32X 0x05
132 #define PAC1921_DIGITAL_V_GAIN_64X 0x06
133 #define PAC1921_DIGITAL_V_GAIN_128X 0x07
134 
140 #define PAC1921_SAMPLE_RATE_1 0x00
141 #define PAC1921_SAMPLE_RATE_2 0x10
142 #define PAC1921_SAMPLE_RATE_4 0x20
143 #define PAC1921_SAMPLE_RATE_8 0x30
144 #define PAC1921_SAMPLE_RATE_16 0x40
145 #define PAC1921_SAMPLE_RATE_32 0x50
146 #define PAC1921_SAMPLE_RATE_64 0x60
147 #define PAC1921_SAMPLE_RATE_128 0x70
148 #define PAC1921_SAMPLE_RATE_256 0x80
149 #define PAC1921_SAMPLE_RATE_512 0x90
150 #define PAC1921_SAMPLE_RATE_1024 0xA0
151 #define PAC1921_SAMPLE_RATE_2048 0xB0
152 #define PAC1921_V_SENSE_FILTER_ON 0x08
153 #define PAC1921_V_SENSE_FILTER_OFF 0x00
154 #define PAC1921_V_BUS_FILTER_ON 0x04
155 #define PAC1921_V_BUS_FILTER_OFF 0x00
156 #define PAC1921_READ_INT_OVERRIDE_ON 0x02
157 #define PAC1921_READ_INT_OVERRIDE_OFF 0x00
158 #define PAC1921_FORCED_INTEGRATE_MODE_ON 0x01
159 #define PAC1921_FORCED_INTEGRATE_MODE_OFF 0x00
160 
166 #define PAC1921_MEASUREMENT_MODE_V_POWER_PIN_CTRL 0x00
167 #define PAC1921_MEASUREMENT_MODE_V_SENSE_FREE_RUN 0x40
168 #define PAC1921_MEASUREMENT_MODE_V_BUS_FREE_RUN 0x80
169 #define PAC1921_MEASUREMENT_MODE_V_POWER_FREE_RUN 0xC0
170 #define PAC1921_OUT_PIN_FULL_SCALE_RANGE_3V 0x00
171 #define PAC1921_OUT_PIN_FULL_SCALE_RANGE_2V 0x10
172 #define PAC1921_OUT_PIN_FULL_SCALE_RANGE_1_5_V 0x20
173 #define PAC1921_OUT_PIN_FULL_SCALE_RANGE_1V 0x30
174 #define PAC1921_TIMEOUT_ON 0x08
175 #define PAC1921_TIMEOUT_OFF 0x00
176 #define PAC1921_SLEEP_ON 0x04
177 #define PAC1921_SLEEP_OFF 0x00
178 #define PAC1921_FORCED_READ_MODE_ON 0x02
179 #define PAC1921_FORCED_READ_MODE_OFF 0x00
180 #define PAC1921_FORCED_RECALCULATE_MODE_ON 0x01
181 #define PAC1921_FORCED_RECALCULATE_MODE_OFF 0x00
182 
188 #define PAC1921_VOLTAGE_MAX 10
189 #define PAC1921_CURRENT_MAX 32
190  // End group macro
193 // --------------------------------------------------------------- PUBLIC TYPES
202 typedef struct
203 {
204  uint8_t ctrl_byte;
205  uint8_t cfg_byte;
207  uint8_t sample_num_old;
208  uint8_t iarp;
209  uint16_t output_fsr;
210 
211 } aux_vars_t;
212 
216 typedef struct
217 {
218  uint8_t ctrl_byte;
219  uint8_t cfg_byte;
221  uint8_t sample_num_old;
222  uint8_t iarp;
223  uint16_t output_fsr;
224 
225 } aux_cfg_t;
226 
230 typedef struct
231 {
232  // Output pins
233 
234  digital_out_t int_pin;
235 
236  // Input pins
237 
238  digital_in_t an;
239 
240  // Modules
241 
242  i2c_master_t i2c;
243 
244  // ctx variable
245 
246  uint8_t slave_address;
248 
249 } pac1921_t;
250 
254 typedef struct
255 {
256  // Communication gpio pins
257 
258  pin_name_t scl;
259  pin_name_t sda;
260 
261  // Additional gpio pins
262 
263  pin_name_t an;
264  pin_name_t int_pin;
265 
266  // static variable
267 
268  uint32_t i2c_speed;
269  uint8_t i2c_address;
271 
272 } pac1921_cfg_t;
273  // End types group
275 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
276 
282 #ifdef __cplusplus
283 extern "C"{
284 #endif
285 
294 void pac1921_cfg_setup ( pac1921_cfg_t *cfg );
295 
305 
313 void pac1921_default_cfg ( pac1921_t *ctx );
314 
325 void pac1921_generic_write ( pac1921_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
326 
338 void pac1921_generic_read ( pac1921_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
339 
351 void pac1921_write_to_reg ( pac1921_t *ctx, uint8_t reg_addr, uint8_t *write_data, uint8_t len );
352 
363 float pac1921_get_measured_data ( pac1921_t *ctx, uint8_t measurement_mode, uint8_t sample_num );
364 
373 void pac1921_set_int_pin ( pac1921_t *ctx, uint8_t output );
374 
382 uint8_t pac1921_get_an_pin ( pac1921_t *ctx );
383 
384 #ifdef __cplusplus
385 }
386 #endif
387 #endif // _PAC1921_H_
388  // End public_function group
391 
392 // ------------------------------------------------------------------------- END
void pac1921_generic_write(pac1921_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
i2c_master_t i2c
Definition: pac1921.h:242
void pac1921_write_to_reg(pac1921_t *ctx, uint8_t reg_addr, uint8_t *write_data, uint8_t len)
Register write function.
digital_in_t an
Definition: pac1921.h:238
void pac1921_default_cfg(pac1921_t *ctx)
Click Default Configuration function.
aux_cfg_t aux_cfg
Definition: pac1921.h:270
uint8_t cfg_byte
Definition: pac1921.h:205
aux_vars_t aux
Definition: pac1921.h:247
uint8_t iarp
Definition: pac1921.h:222
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.
#define PAC1921_RETVAL
Definition: pac1921.h:63
Auxiliary variables structure.
Definition: pac1921.h:202
uint16_t output_fsr
Definition: pac1921.h:209
Click configuration structure definition.
Definition: pac1921.h:254
pin_name_t scl
Definition: pac1921.h:258
uint8_t ctrl_byte
Definition: pac1921.h:204
uint8_t iarp
Definition: pac1921.h:208
uint32_t i2c_speed
Definition: pac1921.h:268
uint8_t pac1921_get_an_pin(pac1921_t *ctx)
AN pin get input function.
void pac1921_generic_read(pac1921_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
uint8_t measurement_mode_old
Definition: pac1921.h:206
void pac1921_set_int_pin(pac1921_t *ctx, uint8_t output)
INT pin set output function.
uint16_t output_fsr
Definition: pac1921.h:223
Click ctx object definition.
Definition: pac1921.h:230
uint8_t sample_num_old
Definition: pac1921.h:207
uint8_t cfg_byte
Definition: pac1921.h:219
pin_name_t an
Definition: pac1921.h:263
void pac1921_cfg_setup(pac1921_cfg_t *cfg)
Config Object Initialization function.
digital_out_t int_pin
Definition: pac1921.h:234
uint8_t ctrl_byte
Definition: pac1921.h:218
pin_name_t sda
Definition: pac1921.h:259
uint8_t sample_num_old
Definition: pac1921.h:221
uint8_t slave_address
Definition: pac1921.h:246
pin_name_t int_pin
Definition: pac1921.h:264
Auxiliary variables default cfg.
Definition: pac1921.h:216
uint8_t measurement_mode_old
Definition: pac1921.h:220
uint8_t i2c_address
Definition: pac1921.h:269