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 
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_spi_master.h"
55 
56 // -------------------------------------------------------------- PUBLIC MACROS
67 #define PWRMETER2_MAP_MIKROBUS( cfg, mikrobus ) \
68  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
69  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
70  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
71  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
72  cfg.md1 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
73  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
74  cfg.md0 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
75  cfg.dr = MIKROBUS( mikrobus, MIKROBUS_INT );
76 
82 #define PWRMETER2_OK 0
83 #define PWRMETER2_ERROR -1
84 
90 #define PWRMETER2_REG_CH0_ADC_DATA 0x00
91 #define PWRMETER2_REG_CH1_ADC_DATA 0x01
92 #define PWRMETER2_REG_MODUL_OUTPUT 0x08
93 #define PWRMETER2_REG_PHASE_CONFIG 0x0A
94 #define PWRMETER2_REG_GAIN_CONFIG 0x0B
95 #define PWRMETER2_REG_STATUS_COMM 0x0C
96 #define PWRMETER2_REG_CONFIG0 0x0D
97 #define PWRMETER2_REG_CONFIG1 0x0E
98 #define PWRMETER2_REG_OFFCAL_CH0 0x0F
99 #define PWRMETER2_REG_GAINCAL_CH0 0x10
100 #define PWRMETER2_REG_OFFCAL_CH1 0x11
101 #define PWRMETER2_REG_GAINCAL_CH1 0x12
102 #define PWRMETER2_REG_LOCK_CRC 0x1F
103 
109 #define PWRMETER2_CH0_GAIN1 0x000000ul
110 #define PWRMETER2_CH0_GAIN2 0x000001ul
111 #define PWRMETER2_CH0_GAIN4 0x000002ul
112 #define PWRMETER2_CH0_GAIN8 0x000003ul
113 #define PWRMETER2_CH0_GAIN16 0x000004ul
114 #define PWRMETER2_CH0_GAIN32 0x000005ul
115 #define PWRMETER2_CH1_GAIN1 0x000000ul
116 #define PWRMETER2_CH1_GAIN2 0x000008ul
117 #define PWRMETER2_CH1_GAIN4 0x000010ul
118 #define PWRMETER2_CH1_GAIN8 0x000018ul
119 #define PWRMETER2_CH1_GAIN16 0x000020ul
120 #define PWRMETER2_CH1_GAIN32 0x000028ul
121 
127 #define PWRMETER2_READ_SAME_SINGLE_REG 0x000000ul
128 #define PWRMETER2_READ_REG_GROUPS 0x400000ul
129 #define PWRMETER2_READ_REG_TYPES 0x800000ul
130 #define PWRMETER2_READ_ENTIRE_REG_MAP 0xC00000ul
131 #define PWRMETER2_ADDR_COUNT_NOT_INCR_WRITE 0x000000ul
132 #define PWRMETER2_ADDR_COUNT_INCR_WRITE 0x200000ul
133 #define PWRMETER2_DR_PIN_HIGH_IMP_NOT_READY 0x000000ul
134 #define PWRMETER2_DR_PIN_LOGIC_HIGH_NOT_READY 0x100000ul
135 #define PWRMETER2_DATA_READY_LINK_DIS 0x000000ul
136 #define PWRMETER2_DATA_READY_LINK_EN 0x080000ul
137 #define PWRMETER2_CRC_WIDTH_16_BIT 0x000000ul
138 #define PWRMETER2_CRC_WIDTH_32_BIT 0x040000ul
139 #define PWRMETER2_DATA_WIDTH_16_BIT 0x000000ul
140 #define PWRMETER2_DATA_WIDTH_24_BIT 0x010000ul
141 #define PWRMETER2_DATA_WIDTH_32_BIT_WITH_ZEROS 0x020000ul
142 #define PWRMETER2_DATA_WIDTH_32_BIT_WITH_SIGN 0x030000ul
143 #define PWRMETER2_CRC_DIS 0x000000ul
144 #define PWRMETER2_CRC_EN 0x008000ul
145 #define PWRMETER2_CRCREG_INTERR_DIS 0x000000ul
146 #define PWRMETER2_CRCREG_INTERR_EN 0x004000ul
147 #define PWRMETER2_MODUL_OUTPUT_DIS 0x000000ul
148 #define PWRMETER2_MODUL_OUTPUT_EN 0x000800ul
149 
155 #define PWRMETER2_OFFCAL_DIS 0x000000ul
156 #define PWRMETER2_OFFCAL_EN 0x800000ul
157 #define PWRMETER2_GAINCAL_DIS 0x000000ul
158 #define PWRMETER2_GAINCAL_EN 0x400000ul
159 #define PWRMETER2_DITHERING_OFF 0x000000ul
160 #define PWRMETER2_DITHERING_STRENGTH_MIN 0x100000ul
161 #define PWRMETER2_DITHERING_STRENGTH_MED 0x200000ul
162 #define PWRMETER2_DITHERING_STRENGTH_MAX 0x300000ul
163 #define PWRMETER2_CURRENT_X_0_5 0x000000ul
164 #define PWRMETER2_CURRENT_X_0_66 0x040000ul
165 #define PWRMETER2_CURRENT_X_1 0x080000ul
166 #define PWRMETER2_CURRENT_X_2 0x0C0000ul
167 #define PWRMETER2_AMCLK_MCLK 0x000000ul
168 #define PWRMETER2_AMCLK_MCLK_PER_2 0x010000ul
169 #define PWRMETER2_AMCLK_MCLK_PER_4 0x020000ul
170 #define PWRMETER2_AMCLK_MCLK_PER_8 0x030000ul
171 #define PWRMETER2_OSR_32 0x000000ul
172 #define PWRMETER2_OSR_64 0x002000ul
173 #define PWRMETER2_OSR_128 0x004000ul
174 #define PWRMETER2_OSR_256 0x006000ul
175 #define PWRMETER2_OSR_512 0x008000ul
176 #define PWRMETER2_OSR_1024 0x00A000ul
177 #define PWRMETER2_OSR_2048 0x00C000ul
178 #define PWRMETER2_OSR_4096 0x00E000ul
179 
185 #define PWRMETER2_CH0_NORMAL_MODE 0x000000ul
186 #define PWRMETER2_CH1_NORMAL_MODE 0x000000ul
187 #define PWRMETER2_CH0_SOFT_RESET_MODE 0x010000ul
188 #define PWRMETER2_CH1_SOFT_RESET_MODE 0x020000ul
189 #define PWRMETER2_CH0_SHUTDOWN_MODE 0x000100ul
190 #define PWRMETER2_CH1_SHUTDOWN_MODE 0x000200ul
191 #define PWRMETER2_INTERNAL_VOL_REF_EN 0x000000ul
192 #define PWRMETER2_INTERNAL_VOL_REF_DIS 0x000080ul
193 #define PWRMETER2_INTERNAL_CLOCK_EN 0x000000ul
194 #define PWRMETER2_INTERNAL_CLOCK_DIS 0x000040ul
195 
201 #define PWRMETER2_CH0_CHECK_DIS 0x00
202 #define PWRMETER2_CH1_CHECK_DIS 0x00
203 #define PWRMETER2_CH0_CHECK_EN 0x01
204 #define PWRMETER2_CH1_CHECK_EN 0x02
205  // End group macro
208 // --------------------------------------------------------------- PUBLIC TYPES
217 typedef struct
218 {
219  // Output pins
220  digital_out_t rst;
221 
222  // Input pins
223  digital_in_t md1;
224  digital_in_t md0;
225  digital_in_t dr;
226 
227  // Modules
228  spi_master_t spi;
229  pin_name_t chip_select;
230 
231 } pwrmeter2_t;
232 
236 typedef struct
237 {
238  // Communication gpio pins
239  pin_name_t miso;
240  pin_name_t mosi;
241  pin_name_t sck;
242  pin_name_t cs;
243 
244  // Additional gpio pins
245  pin_name_t md1;
246  pin_name_t rst;
247  pin_name_t md0;
248  pin_name_t dr;
249 
250  // static variable
251  uint32_t spi_speed;
252  spi_master_mode_t spi_mode;
253  spi_master_chip_select_polarity_t cs_polarity;
254 
256  // End types group
258 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
259 
264 #ifdef __cplusplus
265 extern "C"{
266 #endif
267 
277 
289 
301 
314 err_t pwrmeter2_write_reg ( pwrmeter2_t *ctx, uint8_t reg, uint32_t data_in );
315 
329 err_t pwrmeter2_read_reg ( pwrmeter2_t *ctx, uint8_t reg, uint32_t *data_out, uint8_t len );
330 
343 err_t pwrmeter2_read_conv_data ( pwrmeter2_t *ctx, uint32_t *data_ch_0, uint32_t *data_ch_1 );
344 
358 err_t pwrmeter2_get_data ( pwrmeter2_t *ctx, float *voltage, float *current, float *power );
359 
368 
377 
386 
395 
396 #ifdef __cplusplus
397 }
398 #endif
399 #endif // _PWRMETER2_H_
400  // End public_function group
403 
404 // ------------------------------------------------------------------------- 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:239
pwrmeter2_t::rst
digital_out_t rst
Definition: pwrmeter2.h:220
pwrmeter2_reset
void pwrmeter2_reset(pwrmeter2_t *ctx)
Reset function.
pwrmeter2_t::dr
digital_in_t dr
Definition: pwrmeter2.h:225
pwrmeter2_write_reg
err_t pwrmeter2_write_reg(pwrmeter2_t *ctx, uint8_t reg, uint32_t data_in)
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:246
pwrmeter2_cfg_t::dr
pin_name_t dr
Definition: pwrmeter2.h:248
pwrmeter2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: pwrmeter2.h:253
pwrmeter2_t::md1
digital_in_t md1
Definition: pwrmeter2.h:223
pwrmeter2_cfg_t::cs
pin_name_t cs
Definition: pwrmeter2.h:242
pwrmeter2_t::chip_select
pin_name_t chip_select
Definition: pwrmeter2.h:229
pwrmeter2_cfg_setup
void pwrmeter2_cfg_setup(pwrmeter2_cfg_t *cfg)
Config Object Initialization function.
pwrmeter2_read_conv_data
err_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:245
pwrmeter2_t::spi
spi_master_t spi
Definition: pwrmeter2.h:228
pwrmeter2_default_cfg
err_t pwrmeter2_default_cfg(pwrmeter2_t *ctx)
Click Default Configuration function.
pwrmeter2_get_modulator_0
uint8_t pwrmeter2_get_modulator_0(pwrmeter2_t *ctx)
Modulator 0 Get function.
pwrmeter2_get_data
err_t pwrmeter2_get_data(pwrmeter2_t *ctx, float *voltage, float *current, float *power)
Data Get function.
pwrmeter2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: pwrmeter2.h:252
pwrmeter2_read_reg
err_t pwrmeter2_read_reg(pwrmeter2_t *ctx, uint8_t reg, uint32_t *data_out, uint8_t len)
Generic Read function.
pwrmeter2_t
Click ctx object definition.
Definition: pwrmeter2.h:218
pwrmeter2_cfg_t::spi_speed
uint32_t spi_speed
Definition: pwrmeter2.h:251
pwrmeter2_cfg_t::mosi
pin_name_t mosi
Definition: pwrmeter2.h:240
pwrmeter2_cfg_t::md0
pin_name_t md0
Definition: pwrmeter2.h:247
pwrmeter2_t::md0
digital_in_t md0
Definition: pwrmeter2.h:224
pwrmeter2_init
err_t pwrmeter2_init(pwrmeter2_t *ctx, pwrmeter2_cfg_t *cfg)
Initialization function.
pwrmeter2_cfg_t
Click configuration structure definition.
Definition: pwrmeter2.h:237
pwrmeter2_cfg_t::sck
pin_name_t sck
Definition: pwrmeter2.h:241