pwrmeter2  2.0.0.0
pwrmeter2.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 PWRMETER2_H
36 #define PWRMETER2_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_spi_master.h"
41 
42 // -------------------------------------------------------------- PUBLIC MACROS
53 #define PWRMETER2_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
55  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
56  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
57  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
58  cfg.md1 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
59  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
60  cfg.md0 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
61  cfg.dr = MIKROBUS( mikrobus, MIKROBUS_INT );
62 
68 #define PWRMETER2_RETVAL uint8_t
69 
70 #define PWRMETER2_OK 0x00
71 #define PWRMETER2_INIT_ERROR 0xFF
72 
78 #define PWRMETER2_CH0_ADC_DATA_REG 0x00
79 #define PWRMETER2_CH1_ADC_DATA_REG 0x01
80 #define PWRMETER2_MODUL_OUTPUT_REG 0x08
81 #define PWRMETER2_PHASE_CONFIG_REG 0x0A
82 #define PWRMETER2_GAIN_CONFIG_REG 0x0B
83 #define PWRMETER2_STATUS_COMM_REG 0x0C
84 #define PWRMETER2_CONFIG0_REG 0x0D
85 #define PWRMETER2_CONFIG1_REG 0x0E
86 #define PWRMETER2_OFFCAL_CH0_REG 0x0F
87 #define PWRMETER2_GAINCAL_CH0_REG 0x10
88 #define PWRMETER2_OFFCAL_CH1_REG 0x11
89 #define PWRMETER2_GAINCAL_CH1_REG 0x12
90 #define PWRMETER2_LOCK_CRC_REG 0x1F
91 
97 #define PWRMETER2_CH0_GAIN1 0x000000
98 #define PWRMETER2_CH0_GAIN2 0x000001
99 #define PWRMETER2_CH0_GAIN4 0x000002
100 #define PWRMETER2_CH0_GAIN8 0x000003
101 #define PWRMETER2_CH0_GAIN16 0x000004
102 #define PWRMETER2_CH0_GAIN32 0x000005
103 #define PWRMETER2_CH1_GAIN1 0x000000
104 #define PWRMETER2_CH1_GAIN2 0x000008
105 #define PWRMETER2_CH1_GAIN4 0x000010
106 #define PWRMETER2_CH1_GAIN8 0x000018
107 #define PWRMETER2_CH1_GAIN16 0x000020
108 #define PWRMETER2_CH1_GAIN32 0x000028
109 
115 #define PWRMETER2_READ_SAME_SINGLE_REG 0x000000
116 #define PWRMETER2_READ_REG_GROUPS 0x400000
117 #define PWRMETER2_READ_REG_TYPES 0x800000
118 #define PWRMETER2_READ_ENTIRE_REG_MAP 0xC00000
119 #define PWRMETER2_ADDR_COUNT_NOT_INCR_WRITE 0x000000
120 #define PWRMETER2_ADDR_COUNT_INCR_WRITE 0x200000
121 #define PWRMETER2_DR_PIN_HIGH_IMP_NOT_READY 0x000000
122 #define PWRMETER2_DR_PIN_LOGIC_HIGH_NOT_READY 0x100000
123 #define PWRMETER2_DATA_READY_LINK_DIS 0x000000
124 #define PWRMETER2_DATA_READY_LINK_EN 0x080000
125 #define PWRMETER2_CRC_WIDTH_16_BIT 0x000000
126 #define PWRMETER2_CRC_WIDTH_32_BIT 0x040000
127 #define PWRMETER2_DATA_WIDTH_16_BIT 0x000000
128 #define PWRMETER2_DATA_WIDTH_24_BIT 0x010000
129 #define PWRMETER2_DATA_WIDTH_32_BIT_WITH_ZEROS 0x020000
130 #define PWRMETER2_DATA_WIDTH_32_BIT_WITH_SIGN 0x030000
131 #define PWRMETER2_CRC_DIS 0x000000
132 #define PWRMETER2_CRC_EN 0x008000
133 #define PWRMETER2_CRCREG_INTERR_DIS 0x000000
134 #define PWRMETER2_CRCREG_INTERR_EN 0x004000
135 #define PWRMETER2_MODUL_OUTPUT_DIS 0x000000
136 #define PWRMETER2_MODUL_OUTPUT_EN 0x000800
137 
143 #define PWRMETER2_OFFCAL_DIS 0x000000
144 #define PWRMETER2_OFFCAL_EN 0x800000
145 #define PWRMETER2_GAINCAL_DIS 0x000000
146 #define PWRMETER2_GAINCAL_EN 0x400000
147 #define PWRMETER2_DITHERING_OFF 0x000000
148 #define PWRMETER2_DITHERING_STRENGTH_MIN 0x100000
149 #define PWRMETER2_DITHERING_STRENGTH_MED 0x200000
150 #define PWRMETER2_DITHERING_STRENGTH_MAX 0x300000
151 #define PWRMETER2_CURRENT_X_0_5 0x000000
152 #define PWRMETER2_CURRENT_X_0_66 0x040000
153 #define PWRMETER2_CURRENT_X_1 0x080000
154 #define PWRMETER2_CURRENT_X_2 0x0C0000
155 #define PWRMETER2_AMCLK_MCLK 0x000000
156 #define PWRMETER2_AMCLK_MCLK_PER_2 0x010000
157 #define PWRMETER2_AMCLK_MCLK_PER_4 0x020000
158 #define PWRMETER2_AMCLK_MCLK_PER_8 0x030000
159 #define PWRMETER2_OSR_32 0x000000
160 #define PWRMETER2_OSR_64 0x002000
161 #define PWRMETER2_OSR_128 0x004000
162 #define PWRMETER2_OSR_256 0x006000
163 #define PWRMETER2_OSR_512 0x008000
164 #define PWRMETER2_OSR_1024 0x00A000
165 #define PWRMETER2_OSR_2048 0x00C000
166 #define PWRMETER2_OSR_4096 0x00E000
167 
173 #define PWRMETER2_CH0_NORMAL_MODE 0x000000
174 #define PWRMETER2_CH1_NORMAL_MODE 0x000000
175 #define PWRMETER2_CH0_SOFT_RESET_MODE 0x010000
176 #define PWRMETER2_CH1_SOFT_RESET_MODE 0x020000
177 #define PWRMETER2_CH0_SHUTDOWN_MODE 0x000100
178 #define PWRMETER2_CH1_SHUTDOWN_MODE 0x000200
179 #define PWRMETER2_INTERNAL_VOL_REF_EN 0x000000
180 #define PWRMETER2_INTERNAL_VOL_REF_DIS 0x000080
181 #define PWRMETER2_INTERNAL_CLOCK_EN 0x000000
182 #define PWRMETER2_INTERNAL_CLOCK_DIS 0x000040
183 
189 #define PWRMETER2_CH0_CHECK_DIS 0x00
190 #define PWRMETER2_CH1_CHECK_DIS 0x00
191 #define PWRMETER2_CH0_CHECK_EN 0x01
192 #define PWRMETER2_CH1_CHECK_EN 0x02
193  // End group macro
196 // --------------------------------------------------------------- PUBLIC TYPES
205 typedef struct
206 {
207  // Output pins
208 
209  digital_out_t rst;
210  digital_out_t cs;
211 
212  // Input pins
213 
214  digital_in_t md1;
215  digital_in_t md0;
216  digital_in_t dr;
217 
218  // Modules
219 
220  spi_master_t spi;
221  pin_name_t chip_select;
222  uint8_t dr_link;
223 
224 } pwrmeter2_t;
225 
229 typedef struct
230 {
231  // Communication gpio pins
232 
233  pin_name_t miso;
234  pin_name_t mosi;
235  pin_name_t sck;
236  pin_name_t cs;
237 
238  // Additional gpio pins
239 
240  pin_name_t md1;
241  pin_name_t rst;
242  pin_name_t md0;
243  pin_name_t dr;
244 
245  // static variable
246 
247  uint32_t spi_speed;
248  spi_master_mode_t spi_mode;
249  spi_master_chip_select_polarity_t cs_polarity;
250 
252  // End types group
254 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
255 
260 #ifdef __cplusplus
261 extern "C"{
262 #endif
263 
273 
282 
290 void pwrmeter2_default_cfg ( pwrmeter2_t *ctx );
291 
304 (
305  pwrmeter2_t *ctx,
306  uint8_t *wr_buf,
307  uint16_t wr_len,
308  uint8_t *rd_buf,
309  uint16_t rd_len
310 );
311 
321 uint8_t pwrmeter2_write_reg ( pwrmeter2_t *ctx, uint8_t register_address, uint32_t transfer_data );
322 
333 uint8_t pwrmeter2_read_reg ( pwrmeter2_t *ctx, uint8_t register_address, uint32_t *data_out, uint8_t n_data );
334 
343 uint8_t pwrmeter2_check_data_ready ( pwrmeter2_t *ctx, uint8_t sel_chann );
344 
354 uint8_t pwrmeter2_read_conv_data ( pwrmeter2_t *ctx, uint32_t *data_ch_0, uint32_t *data_ch_1 );
355 
366 void pwrmeter2_get_data ( pwrmeter2_t *ctx, int32_t *voltage_data, int32_t *current_data, uint32_t *power_data );
367 
375 void pwrmeter2_reset( pwrmeter2_t *ctx );
376 
384 uint8_t pwrmeter2_get_modulator_1 ( pwrmeter2_t *ctx );
385 
393 uint8_t pwrmeter2_get_modulator_0 ( pwrmeter2_t *ctx );
394 
402 uint8_t pwrmeter2_check_ready_pin ( pwrmeter2_t *ctx );
403 
404 #ifdef __cplusplus
405 }
406 #endif
407 #endif // _PWRMETER2_H_
408  // End public_function group
411 
412 // ------------------------------------------------------------------------- END
pwrmeter2_check_ready_pin
uint8_t pwrmeter2_check_ready_pin(pwrmeter2_t *ctx)
Ready Pin Check function.
pwrmeter2_cfg_t::miso
pin_name_t miso
Definition: pwrmeter2.h:233
pwrmeter2_t::rst
digital_out_t rst
Definition: pwrmeter2.h:209
pwrmeter2_t::dr_link
uint8_t dr_link
Definition: pwrmeter2.h:222
pwrmeter2_reset
void pwrmeter2_reset(pwrmeter2_t *ctx)
Reset function.
pwrmeter2_t::dr
digital_in_t dr
Definition: pwrmeter2.h:216
pwrmeter2_generic_transfer
void pwrmeter2_generic_transfer(pwrmeter2_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
pwrmeter2_read_reg
uint8_t pwrmeter2_read_reg(pwrmeter2_t *ctx, uint8_t register_address, uint32_t *data_out, uint8_t n_data)
Generic Read function.
pwrmeter2_write_reg
uint8_t pwrmeter2_write_reg(pwrmeter2_t *ctx, uint8_t register_address, uint32_t transfer_data)
Generic Write function.
pwrmeter2_get_modulator_1
uint8_t pwrmeter2_get_modulator_1(pwrmeter2_t *ctx)
Modulator 1 Get function.
pwrmeter2_cfg_t::rst
pin_name_t rst
Definition: pwrmeter2.h:241
pwrmeter2_cfg_t::dr
pin_name_t dr
Definition: pwrmeter2.h:243
pwrmeter2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: pwrmeter2.h:249
pwrmeter2_default_cfg
void pwrmeter2_default_cfg(pwrmeter2_t *ctx)
Click Default Configuration function.
pwrmeter2_t::md1
digital_in_t md1
Definition: pwrmeter2.h:214
pwrmeter2_cfg_t::cs
pin_name_t cs
Definition: pwrmeter2.h:236
pwrmeter2_t::chip_select
pin_name_t chip_select
Definition: pwrmeter2.h:221
pwrmeter2_cfg_setup
void pwrmeter2_cfg_setup(pwrmeter2_cfg_t *cfg)
Config Object Initialization function.
pwrmeter2_read_conv_data
uint8_t pwrmeter2_read_conv_data(pwrmeter2_t *ctx, uint32_t *data_ch_0, uint32_t *data_ch_1)
Read Converted Data function.
pwrmeter2_cfg_t::md1
pin_name_t md1
Definition: pwrmeter2.h:240
pwrmeter2_t::spi
spi_master_t spi
Definition: pwrmeter2.h:220
pwrmeter2_get_modulator_0
uint8_t pwrmeter2_get_modulator_0(pwrmeter2_t *ctx)
Modulator 0 Get function.
pwrmeter2_t::cs
digital_out_t cs
Definition: pwrmeter2.h:210
pwrmeter2_check_data_ready
uint8_t pwrmeter2_check_data_ready(pwrmeter2_t *ctx, uint8_t sel_chann)
Data Ready Check function.
pwrmeter2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: pwrmeter2.h:248
pwrmeter2_t
Click ctx object definition.
Definition: pwrmeter2.h:205
pwrmeter2_cfg_t::spi_speed
uint32_t spi_speed
Definition: pwrmeter2.h:247
pwrmeter2_cfg_t::mosi
pin_name_t mosi
Definition: pwrmeter2.h:234
pwrmeter2_cfg_t::md0
pin_name_t md0
Definition: pwrmeter2.h:242
pwrmeter2_get_data
void pwrmeter2_get_data(pwrmeter2_t *ctx, int32_t *voltage_data, int32_t *current_data, uint32_t *power_data)
Data Get function.
pwrmeter2_init
PWRMETER2_RETVAL pwrmeter2_init(pwrmeter2_t *ctx, pwrmeter2_cfg_t *cfg)
Initialization function.
pwrmeter2_t::md0
digital_in_t md0
Definition: pwrmeter2.h:215
PWRMETER2_RETVAL
#define PWRMETER2_RETVAL
Definition: pwrmeter2.h:68
pwrmeter2_cfg_t
Click configuration structure definition.
Definition: pwrmeter2.h:229
pwrmeter2_cfg_t::sck
pin_name_t sck
Definition: pwrmeter2.h:235