no-OS
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ade7753.h
Go to the documentation of this file.
1/***************************************************************************/
33#ifndef __ADE7753_H__
34#define __ADE7753_H__
35
36#include <stdbool.h>
37#include <stdint.h>
38#include <string.h>
39#include "no_os_util.h"
40#include "no_os_spi.h"
41#include "no_os_gpio.h"
42#include "no_os_irq.h"
43#include "no_os_delay.h"
44#include "no_os_units.h"
45#include "no_os_alloc.h"
46#include "no_os_crc16.h"
47#include "no_os_print_log.h"
48#include <stdlib.h>
49#include <errno.h>
50#include <math.h>
51
52/* SPI commands */
53#define ADE7753_SPI_READ NO_OS_BIT(7)
54
55/* Version product */
56#define ADE7753_VERSION 0x3F
57#define ADE7753_REG_VERSION_PRODUCT 0x14
58
59/* Miscellaneous Definitions */
60#define IS_6BITS_REG(x) (((x) == ADE7753_REG_PHCAL) || ((x) == ADE7753_REG_CHKSUM))
61#define IS_8BITS_REG(x) (((x) >= ADE7753_REG_CH1OS && (x) <= ADE7753_REG_GAIN) \
62 || ((x) == ADE7753_REG_WDIV) || ((x) == ADE7753_REG_VADIV) || \
63 ((x) >= ADE7753_REG_SAGCYC && (x) <= ADE7753_REG_VPKLVL) || ((x) == ADE7753_REG_TEMP) \
64 || ((x) == ADE7753_REG_TMODE) || ((x) == ADE7753_REG_DIEREV))
65#define IS_12BITS_REG(x) (((x) == ADE7753_REG_WGAIN) || ((x) >= ADE7753_REG_IRMSOS && (x) <= ADE7753_REG_VAGAIN) \
66 || ((x) >= ADE7753_REG_CFNUM && (x) <= ADE7753_REG_CFDEN) || ((x) == ADE7753_REG_ZXTOUT))
67#define IS_16BITS_REG(x) (((x) >= ADE7753_REG_MODE && (x) <= ADE7753_REG_RSTSTATUS) \
68 || ((x) == ADE7753_REG_APOS) || ((x) == ADE7753_REG_LINECYC) || ((x) == ADE7753_REG_PERIOD))
69#define IS_24BITS_REG(x) (((x) >= ADE7753_REG_WAVEFORM && (x) <= ADE7753_REG_LVARENERGY) || \
70 ((x) >= ADE7753_REG_IRMS && (x) <= ADE7753_REG_VRMS) || \
71 ((x) >= ADE7753_REG_IPEAK && (x) <= ADE7753_REG_RSTVPEAK))
72
73/* Reset Delay */
74#define ADE7753_RESET_DEL 1
75
76/* ENABLE and DISABLE */
77#define ENABLE 1u
78#define DISABLE 0u
79
80/* ADE7753 Register Map */
81#define ADE7753_REG_WAVEFORM 0x01
82#define ADE7753_REG_AENERGY 0x02
83#define ADE7753_REG_RAENERGY 0x03
84#define ADE7753_REG_LAENERGY 0x04
85#define ADE7753_REG_VAENERGY 0x05
86#define ADE7753_REG_RVAENERGY 0x06
87#define ADE7753_REG_LVAENERGY 0x07
88#define ADE7753_REG_LVARENERGY 0x08
89#define ADE7753_REG_MODE 0x09
90#define ADE7753_REG_IRQEN 0x0A
91#define ADE7753_REG_STATUS 0x0B
92#define ADE7753_REG_RSTSTATUS 0x0C
93#define ADE7753_REG_CH1OS 0x0D
94#define ADE7753_REG_CH2OS 0X0E
95#define ADE7753_REG_GAIN 0x0F
96#define ADE7753_REG_PHCAL 0x10
97#define ADE7753_REG_APOS 0x11
98#define ADE7753_REG_WGAIN 0x12
99#define ADE7753_REG_WDIV 0x13
100#define ADE7753_REG_CFNUM 0x14
101#define ADE7753_REG_CFDEN 0x15
102#define ADE7753_REG_IRMS 0x16
103#define ADE7753_REG_VRMS 0x17
104#define ADE7753_REG_IRMSOS 0x18
105#define ADE7753_REG_VRMSOS 0x19
106#define ADE7753_REG_VAGAIN 0x1A
107#define ADE7753_REG_VADIV 0X1B
108#define ADE7753_REG_LINECYC 0x1C
109#define ADE7753_REG_ZXTOUT 0x1D
110#define ADE7753_REG_SAGCYC 0x1E
111#define ADE7753_REG_SAGLVL 0x1F
112#define ADE7753_REG_IPKLVL 0x20
113#define ADE7753_REG_VPKLVL 0x21
114#define ADE7753_REG_IPEAK 0x22
115#define ADE7753_REG_RSTIPEAK 0x23
116#define ADE7753_REG_VPEAK 0x24
117#define ADE7753_REG_RSTVPEAK 0x25
118#define ADE7753_REG_TEMP 0x26
119#define ADE7753_REG_PERIOD 0x27
120#define ADE7753_REG_TMODE 0x3D
121#define ADE7753_REG_CHKSUM 0x3E
122#define ADE7753_REG_DIEREV 0x3F
123
124/* ADE7753_REG_MODE Bit Definition */
125#define ADE7753_POAM_MSK NO_OS_BIT(15)
126#define ADE7753_WAVSEL_MSK NO_OS_GENMASK(14, 13)
127#define ADE7753_DTRT_MSK NO_OS_GENMASK(12, 11)
128#define ADE7753_SWAP_MSK NO_OS_BIT(10)
129#define ADE7753_DISCH2_MSK NO_OS_BIT(9)
130#define ADE7753_DISCH1_MSK NO_OS_BIT(8)
131#define ADE7753_CYCMODE_MSK NO_OS_BIT(7)
132#define ADE7753_SWRST_MSK NO_OS_BIT(6)
133#define ADE7753_TEMPSEL_MSK NO_OS_BIT(5)
134#define ADE7753_ASUSPEND_MSK NO_OS_BIT(4)
135#define ADE7753_DISSAG_MSK NO_OS_BIT(3)
136#define ADE7753_DISCF_MSK NO_OS_BIT(2)
137#define ADE7753_DISLPF2_MSK NO_OS_BIT(1)
138#define ADE7753_DISHPF_MSK NO_OS_BIT(0)
139
140/* ADE7753_REG_STATUS/ADE7753_REG_RSTSTATUS/ADE7753_REG_IRQEN Bit Definition */
141#define ADE7753_PNEG_MSK NO_OS_BIT(14)
142#define ADE7753_PPOS_MSK NO_OS_BIT(13)
143#define ADE7753_ZXTO_MSK NO_OS_BIT(12)
144#define ADE7753_VAEOF_MSK NO_OS_BIT(11)
145#define ADE7753_VAEHF_MSK NO_OS_BIT(10)
146#define ADE7753_PKI_MSK NO_OS_BIT(9)
147#define ADE7753_PKV_MSK NO_OS_BIT(8)
148#define ADE7753_AEOF_MSK NO_OS_BIT(7)
149#define ADE7753_RESET_MSK NO_OS_BIT(6)
150#define ADE7753_TEMP_MSK NO_OS_BIT(5)
151#define ADE7753_ZX_MSK NO_OS_BIT(4)
152#define ADE7753_WSMP_MSK NO_OS_BIT(3)
153#define ADE7753_CYCEND_MSK NO_OS_BIT(2)
154#define ADE7753_SAG_MSK NO_OS_BIT(1)
155#define ADE7753_AEHF_MSK NO_OS_BIT(0)
156
157/* ADE7753_REG_CH1OS Bit Definition */
158#define ADE7753_INTEGRATOR_MSK NO_OS_BIT(7)
159#define ADE7753_OFFSET_MSK NO_OS_GENMASK(5, 0)
160
161/* ADE7753_REG_GAIN Bit Definition */
162#define ADE7753_PGA2_GAIN_MSK NO_OS_GENMASK(7, 5)
163#define ADE7753_FULL_SCALE_MSK NO_OS_GENMASK(4, 3)
164#define ADE7753_PGA1_GAIN_MSK NO_OS_GENMASK(2, 0)
165
171 /* Gain */
172 /* gain 1 */
174 /* gain 2 */
176 /* gain 4 */
178 /* gain 8 */
180 /* gain 16 */
182};
183
189 /* Full scale select */
190 /* 0.5V */
192 /* 0.25V */
194 /* 0.125V */
196};
197
203 /* update rate */
204 /* 27.9 kSPS (CLKIN/128) */
206 /* 14 kSPS (CLKIN/256) */
208 /* 7 kSPS (CLKIN/512) */
210 /* 3.5 kSPS (CLKIN/1024) */
212};
213
219 /* 24 bits active power signal (output of LPF2) */
221 /* 24 bits Channel 1 */
223 /* 24 bits Channel 2 */
225};
226
239
254
267
278
287
288/* Initialize the device. */
289int ade7753_init(struct ade7753_dev **device,
291
292/* Read device register. */
293int ade7753_read(struct ade7753_dev *dev, uint16_t reg_addr,
294 int32_t *reg_data);
295
296/* Write device register. */
297int ade7753_write(struct ade7753_dev *dev, uint16_t reg_addr,
298 uint32_t reg_data);
299
300/* Update specific register bits. */
301int ade7753_update_bits(struct ade7753_dev *dev, uint16_t reg_addr,
302 uint32_t mask, uint32_t reg_data);
303
304/* Remove the device and release resources. */
305int ade7753_remove(struct ade7753_dev *dev);
306
307/* Reset the device using SW reset. */
308int ade7753_sw_reset(struct ade7753_dev *dev);
309
310/* Reset the device using HW reset. */
311int ade7753_hw_reset(struct ade7753_dev *dev);
312
313/* Version product */
314int ade7753_version_product(struct ade7753_dev *dev, uint32_t *data_read);
315
316/* Read with reset Ipeak val */
317int ade7753_read_reset_iapk_val(struct ade7753_dev *dev, uint32_t *val);
318
319/* Read with reset Vpeak val */
320int ade7753_reset_vpk_val(struct ade7753_dev *dev, uint32_t *val);
321
322/* Get interrupt indicator from STATUS register */
323int ade7753_get_int_status(struct ade7753_dev *dev, uint32_t msk,
324 uint8_t *status);
325
326/* Clear irq STATUS flags */
327int ade7753_clear_irq_status(struct ade7753_dev *dev, int32_t *reg_data);
328
329/* Enable/Disable interrupt */
330int ade7753_enable_irq(struct ade7753_dev *dev, uint32_t msk, uint8_t en);
331
332/* Select waveform register update rate */
334 enum ade7753_dtrt_e sel);
335
336/* Select source of sampled data for wave register */
338 enum ade7753_wavsel_e sel);
339
340/* Read energy values */
341int ade7753_energy_vals(struct ade7753_dev *dev,
342 struct ade7753_energy_values *data);
343
344/* Read rms values */
345int ade7753_rms_vals(struct ade7753_dev *dev,
346 struct ade7753_rms_values *data);
347
348/* Read period value */
349int ade7753_period_val(struct ade7753_dev *dev,
350 struct ade7753_period_value *data);
351
352#endif /* __ADE7753_H__ */
struct ad7616_init_param init_param
Definition ad7616_sdz.c:107
int ade7753_sw_reset(struct ade7753_dev *dev)
Reset the device using SW reset.
Definition ade7753.c:306
int ade7753_get_int_status(struct ade7753_dev *dev, uint32_t msk, uint8_t *status)
Get interrupt indicator from STATUS register.
Definition ade7753.c:423
int ade7753_period_val(struct ade7753_dev *dev, struct ade7753_period_value *data)
Read period value.
Definition ade7753.c:611
int ade7753_wave_sample_data_source(struct ade7753_dev *dev, enum ade7753_wavsel_e sel)
Select source of sampled data for wave register.
Definition ade7753.c:520
ade7753_wavsel_e
ADE7753 select the source of the sampled data for the waveform register.
Definition ade7753.h:218
@ ADE7753_24BITS_ACTIVE_POWER
Definition ade7753.h:220
@ ADE7753_24BITS_CH2
Definition ade7753.h:224
@ ADE7753_24BITS_CH1
Definition ade7753.h:222
int ade7753_rms_vals(struct ade7753_dev *dev, struct ade7753_rms_values *data)
Read rms values.
Definition ade7753.c:580
int ade7753_clear_irq_status(struct ade7753_dev *dev, int32_t *reg_data)
Clear irq status flags.
Definition ade7753.c:449
int ade7753_enable_irq(struct ade7753_dev *dev, uint32_t msk, uint8_t en)
Enable/Disable interrupt.
Definition ade7753.c:473
int ade7753_read(struct ade7753_dev *dev, uint16_t reg_addr, int32_t *reg_data)
Read device register.
Definition ade7753.c:135
int ade7753_write(struct ade7753_dev *dev, uint16_t reg_addr, uint32_t reg_data)
Write device register.
Definition ade7753.c:198
ade7753_dtrt_e
ADE7753 select the waveform register update rate.
Definition ade7753.h:202
@ ADE7753_UPDATE_27_9KSPS
Definition ade7753.h:205
@ ADE7753_UPDATE_3_5KSPS
Definition ade7753.h:211
@ ADE7753_UPDATE_14KSPS
Definition ade7753.h:207
@ ADE7753_UPDATE_7KSPS
Definition ade7753.h:209
int ade7753_reset_vpk_val(struct ade7753_dev *dev, uint32_t *val)
read with reset Vpeak val
Definition ade7753.c:399
int ade7753_remove(struct ade7753_dev *dev)
Remove the device and release resources.
Definition ade7753.c:273
int ade7753_version_product(struct ade7753_dev *dev, uint32_t *data_read)
Version product.
Definition ade7753.c:352
ade7753_pga_gain_e
ADE7753 select the PGA gain.
Definition ade7753.h:170
@ ADE7753_PGA_GAIN_16
Definition ade7753.h:181
@ ADE7753_PGA_GAIN_2
Definition ade7753.h:175
@ ADE7753_PGA_GAIN_4
Definition ade7753.h:177
@ ADE7753_PGA_GAIN_8
Definition ade7753.h:179
@ ADE7753_PGA_GAIN_1
Definition ade7753.h:173
int ade7753_update_bits(struct ade7753_dev *dev, uint16_t reg_addr, uint32_t mask, uint32_t reg_data)
Update specific register bits.
Definition ade7753.c:251
int ade7753_energy_vals(struct ade7753_dev *dev, struct ade7753_energy_values *data)
Read energy values.
Definition ade7753.c:549
int ade7753_hw_reset(struct ade7753_dev *dev)
Reset the device using HW reset.
Definition ade7753.c:326
int ade7753_init(struct ade7753_dev **device, struct ade7753_init_param init_param)
Initialize the device.
Definition ade7753.c:59
ade7753_ch_full_scale_sel_e
ADE7753 Channel 1 full-scale select.
Definition ade7753.h:188
@ ADE7753_FS_0_25
Definition ade7753.h:193
@ ADE7753_FS_0_5
Definition ade7753.h:191
@ ADE7753_FS_0_125
Definition ade7753.h:195
int ade7753_read_reset_iapk_val(struct ade7753_dev *dev, uint32_t *val)
read with reset Ipeak val
Definition ade7753.c:376
int ade7753_wave_update_rate(struct ade7753_dev *dev, enum ade7753_dtrt_e sel)
Select waveform register update rate.
Definition ade7753.c:487
Error macro definition for ARM Compiler.
Header file of CRC-16 computation.
Header file of Delay functions.
Header file of GPIO Interface.
Header file of IRQ interface.
Print messages helpers.
Header file of SPI Interface.
Header file of Units.
Header file of utility functions.
ADE7753 Device structure.
Definition ade7753.h:244
struct no_os_irq_ctrl_desc * zx_ctrl
Definition ade7753.h:252
struct no_os_spi_desc * spi_desc
Definition ade7753.h:246
struct no_os_gpio_desc * gpio_reset
Definition ade7753.h:248
struct no_os_irq_ctrl_desc * irq_ctrl
Definition ade7753.h:250
ADE7753 energy registers values.
Definition ade7753.h:259
int32_t acc_reactive_energy_reg_val
Definition ade7753.h:263
int32_t active_energy_reg_val
Definition ade7753.h:261
int32_t apparent_energy_reg_val
Definition ade7753.h:265
ADE7753 Device initialization parameters.
Definition ade7753.h:231
struct no_os_gpio_init_param * gpio_reset
Definition ade7753.h:235
struct no_os_spi_init_param * spi_init
Definition ade7753.h:233
struct no_os_irq_ctrl_desc * irq_ctrl
Definition ade7753.h:237
ADE7753 pq values.
Definition ade7753.h:283
uint32_t period_reg_val
Definition ade7753.h:285
ADE7753 rms registers values.
Definition ade7753.h:272
uint32_t current_rms_reg_val
Definition ade7753.h:274
uint32_t voltage_rms_reg_val
Definition ade7753.h:276
Definition ad9361_util.h:63
Structure holding the GPIO descriptor.
Definition no_os_gpio.h:84
Structure holding the parameters for GPIO initialization.
Definition no_os_gpio.h:67
Definition no_os_irq.h:117
Structure holding SPI descriptor.
Definition no_os_spi.h:180
Structure holding the parameters for SPI initialization.
Definition no_os_spi.h:128