forked from xxzl0130/CS5460
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCS5460.cpp
338 lines (299 loc) · 6.44 KB
/
CS5460.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "CS5460.h"
#include <math.h>
/**
* \brief constructor without arguments
*/
CS5460::CS5460(): resetPin(PIN_NDEFINED), edirPin(PIN_NDEFINED), eoutPin(PIN_NDEFINED),
csPin(PIN_NDEFINED),currentGain(1.0),voltageGain(1.0),powerGain(1.0),clkFreq(4096000L), meaFreq(1)
{
}
/**
* \brief constructor with arguments
* \param _cs chip select pin
* \param _reset rest pin
* \param _edir EDIR pin
* \param _eout EOUT pin
*/
CS5460::CS5460(uint8_t _cs, uint8_t _reset, uint8_t _edir, uint8_t _eout):currentGain(1.0)
, voltageGain(1.0),powerGain(1.0),clkFreq(4096000L),meaFreq(1)
{
csPin = _cs;
pinMode(csPin, OUTPUT);
resetPin = _reset;
if (_reset != PIN_NDEFINED)
{
pinMode(resetPin, OUTPUT);
}
edirPin = _edir;
if (_edir != PIN_NDEFINED)
{
pinMode(edirPin, INPUT);
}
eoutPin = _eout;
if (_eout != PIN_NDEFINED)
{
pinMode(eoutPin, INPUT);
}
}
/**
* \brief Initialize the chip with sync signal and places the chip in the command
* mode where it waits until a valid command is received.
*/
void CS5460::init() const
{
pinMode(12, INPUT_PULLUP);
select();
SPI.begin();
SPI.beginTransaction(SETTING);
if(resetPin != PIN_NDEFINED)
{
digitalWrite(resetPin, HIGH);
}
SPI.transfer(SYNC1);
SPI.transfer(SYNC1);
SPI.transfer(SYNC1);
SPI.transfer(SYNC0);
SPI.endTransaction();
unselect();
}
/**
* \brief read a register and return the 24-bit data
* \param reg register address
* \return 24-bit raw data in MSB
*/
uint32_t CS5460::readRegister(uint8_t reg) const
{
uint32_t data = 0;
select();
SPI.beginTransaction(SETTING);
reg &= READ_REGISTER;
SPI.transfer(reg);
for(uint8_t i = 0;i < 3;++i)
{
data <<= 8;
data |= SPI.transfer(SYNC0);
}
SPI.endTransaction();
unselect();
return data;
}
/**
* \brief write a register for max 24-bit data
* \param reg register address
* \param cmd at most 24-bit data, more bits will be ignored
*/
void CS5460::writeRegister(uint8_t reg, uint32_t cmd) const
{
int32Data data;
select();
SPI.beginTransaction(SETTING);
reg |= WRITE_REGISTER;
SPI.transfer(reg);
data.data32 = cmd;
for(int8_t i = 2;i >=0; --i)
{// LSB in memory
SPI.transfer(data.data8[i]);
}
SPI.endTransaction();
unselect();
}
/**
* \brief clear some bits in status register
* \param cmd status command
*/
void CS5460::clearStatus(uint32_t cmd)
{
writeRegister(STATUS_REGISTER, cmd);
}
void CS5460::startSingleConvert()
{
send(START_SINGLE_CPNVERT);
}
void CS5460::startMultiConvert()
{
send(START_MULTI_CONVERT);
}
void CS5460::resetChip() const
{
if(resetPin != PIN_NDEFINED)
{
digitalWrite(resetPin, LOW);
delayMicroseconds(50);
digitalWrite(resetPin, HIGH);
delayMicroseconds(50);
}
else
{
writeRegister(CONFIG_REGISTER, CHIP_RESET);
delayMicroseconds(50);
}
}
double CS5460::getCurrent()
{
return signed2float(readRegister(LAST_CURRENT_REGISTER)) * currentGain;
}
uint32_t CS5460::getRawCurrent()
{
return readRegister(LAST_CURRENT_REGISTER);
}
double CS5460::getVoltage()
{
return signed2float(readRegister(LAST_VOLTAGE_REGISTER)) * voltageGain;
}
uint32_t CS5460::getRawVoltage()
{
return readRegister(LAST_VOLTAGE_REGISTER);
}
double CS5460::getPower()
{
return signed2float(readRegister(LAST_POWER_REGISTER)) * powerGain;
}
uint32_t CS5460::getRawPower()
{
return readRegister(LAST_POWER_REGISTER);
}
double CS5460::getRMSCurrent()
{
return unsigned2float(readRegister(RMS_CURRENT_REGISTER)) * currentGain;
}
uint32_t CS5460::getRawRMSCurrent()
{
return readRegister(RMS_CURRENT_REGISTER);
}
double CS5460::getRMSVoltage()
{
return unsigned2float(readRegister(RMS_VOLTAGE_REGISTER)) * voltageGain;
}
uint32_t CS5460::getRawRMSVoltage()
{
return readRegister(RMS_VOLTAGE_REGISTER);
}
double CS5460::getApparentPower()
{
return getRMSCurrent() * getRMSVoltage();
}
double CS5460::getPowerFactor()
{
double factor = getEnergy() / getApparentPower();
if (isnan(factor))
return 0.0;
return constrain(factor,-1.0,1.0);
}
uint32_t CS5460::getStatus()
{
return readRegister(STATUS_REGISTER);
}
void CS5460::select() const
{
digitalWrite(csPin, LOW);
}
void CS5460::unselect() const
{
digitalWrite(csPin, HIGH);
}
void CS5460::setCurrentGain(double gain)
{
currentGain = gain;
powerGain = currentGain * voltageGain;
}
void CS5460::setVoltageGain(double gain)
{
voltageGain = gain;
powerGain = currentGain * voltageGain;
}
/**
* \brief send a 8-bit command with no return
* \param cmd 8-bit command
*/
void CS5460::send(uint8_t cmd)
{
select();
SPI.beginTransaction(SETTING);
SPI.transfer(cmd);
SPI.endTransaction();
unselect();
}
/**
* \brief calibrate voltage/current gain/offset register
* \param cmd calibrate command, must be commands for calibrate control
*/
void CS5460::calibrate(uint8_t cmd)
{
cmd = CALIBRATE_CONTROL | (cmd & CALIBRATE_ALL);
send(cmd);
while(!(getStatus() & DATA_READY));
// wait until data ready;
clearStatus(DATA_READY);
}
uint32_t CS5460::calibrateVoltageOffset()
{
calibrate(CALIBRATE_VOLTAGE | CALIBRATE_OFFSET);
return readRegister(VOLTAGE_OFFSET_REGISTER);
}
uint32_t CS5460::calibrateVoltageGain()
{
calibrate(CALIBRATE_VOLTAGE | CALIBRATE_GAIN);
return readRegister(VOLTAGE_GAIN_REGISTER);
}
uint32_t CS5460::calibrateCurrentOffset()
{
calibrate(CALIBRATE_CURRENT | CALIBRATE_OFFSET);
return readRegister(CURRENT_OFFSET_REGISTER);
}
uint32_t CS5460::calibrateCurrentGain()
{
calibrate(CALIBRATE_CURRENT | CALIBRATE_GAIN);
return readRegister(CURRENT_GAIN_REGISTER);
}
double CS5460::signed2float(int32_t data)
{
if(data & SIGN_BIT)
{// signed
// clear sign bit
data ^= SIGN_BIT;
// make it neg
data = data - SIGNED_OUTPUT_MAX;
}
return double(data) / SIGNED_OUTPUT_MAX;
}
double CS5460::unsigned2float(uint32_t data)
{
return double(data) / UNSIGNED_OUTPUT_MAX;
}
double CS5460::getEnergy()
{
return signed2float(readRegister(TOTAL_ENERGY_REGISTER)) * powerGain * meaFreq;
}
uint32_t CS5460::getRawEnergy()
{
return readRegister(TOTAL_ENERGY_REGISTER);
}
/**
* \brief set chip clk frequency and set divide K properly.
* \param freq clk frequency (Hz)
*/
void CS5460::setFrequency(uint32_t freq)
{
clkFreq = freq;
if(freq <= 5000000L)
{
writeRegister(CONFIG_REGISTER, DIVIDE_K_1);
}
else if(freq <= 10000000L)
{
writeRegister(CONFIG_REGISTER, DIVIDE_K_2);
}
else
{
writeRegister(CONFIG_REGISTER, DIVIDE_K_4);
}
}
/**
* \brief set chip measure (report) frequency and set energy gain properly.
* \param freq frequency (Hz)
*/
void CS5460::setMeasureFrequency(uint32_t freq)
{
meaFreq = freq;
writeRegister(CYCLE_COUNT_REGISTER, clkFreq / 1024 / freq);
}