-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmax34417.c
241 lines (210 loc) · 6.18 KB
/
max34417.c
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
/*******************************************************************************
* Copyright (C) 2020-2022 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
*
******************************************************************************/
#include <stdint.h>
#include <stdlib.h>
#include "mxc_config.h"
#include "i2cm.h"
#include "tmr_utils.h"
#include "max34417.h"
#define MAX34417_I2CM MXC_I2CM0
#define MAX34417_I2CM_IDX 0
#define MAX34417_I2CM_SPEED I2CM_SPEED_400KHZ
#define MAX34417_I2C_ADDR 0x10
#define UPDATE_CMD 0x00
#define CONTROL_CMD 0x01
#define ACC_COUNT_CMD 0x02
#define PWR_ACC_1_CMD 0x03
#define PWR_ACC_2_CMD 0x04
#define PWR_ACC_3_CMD 0x05
#define PWR_ACC_4_CMD 0x06
#define BULK_PWR_CMD 0x10
#define BULK_VOLT_CMD 0x11
#define DEV_REV_ID_CMD 0x0F
#define CNTRL_MODE 0x80
#define CNTRL_CAM 0x40
#define CNTRL_SMM 0x20
#define CNTRL_PARK_EN 0x10
#define CNTRL_PARK1 0x08
#define CNTRL_PARK0 0x04
#define CNTRL_SLOW 0x02
#define CNTRL_OVF 0x01
static uint32_t convert_14to16( const uint8_t * p14 )
{
uint32_t v16;
v16 = p14[0] << 8;
v16 |= p14[1];
return v16 >> 2;
}
static uint32_t convert_24to32( const uint8_t * p24 )
{
uint32_t v32;
v32 = p24[0] << 16;
v32 |= p24[1] << 8;
v32 |= p24[2];
return v32;
}
static uint64_t convert_56to64( const uint8_t * p56 )
{
uint32_t msw, lsw;
msw = p56[0] << 16;
msw |= p56[1] << 8;
msw |= p56[2];
lsw = p56[3] << 24;
lsw |= p56[4] << 16;
lsw |= p56[5] << 8;
lsw |= p56[6];
return (uint64_t)msw << 32 | lsw;
}
int init_max34417(void)
{
int status;
uint8_t buf[2];
sys_cfg_i2cm_t i2cm_sys_cfg;
ioman_cfg_t io_cfg = IOMAN_I2CM(MAX34417_I2CM_IDX, 1, 0);
i2cm_sys_cfg.clk_scale = CLKMAN_SCALE_DIV_1;
i2cm_sys_cfg.io_cfg = io_cfg;
I2CM_Init(MAX34417_I2CM, &i2cm_sys_cfg, MAX34417_I2CM_SPEED);
buf[0] = DEV_REV_ID_CMD;
status = I2CM_Read(MAX34417_I2CM, MAX34417_I2C_ADDR, &buf[0], 1, &buf[1], 1);
if (status != 1) {
return status;
}
buf[0] = CONTROL_CMD;
buf[1] = CNTRL_MODE;
status = I2CM_Write(MAX34417_I2CM, MAX34417_I2C_ADDR, NULL, 0, buf, 2);
if (status != 2) {
return status;
}
max34417_update();
TMR_Delay(MXC_TMR0, MSEC(1));
buf[0] = CONTROL_CMD;
buf[1] = CNTRL_CAM | CNTRL_MODE;
status = I2CM_Write(MAX34417_I2CM, MAX34417_I2C_ADDR, NULL, 0, buf, 2);
if (status != 2) {
return status;
}
max34417_update();
TMR_Delay(MXC_TMR0, MSEC(1));
return 0;
}
int max34417_update(void)
{
int status;
uint8_t cmd;
cmd = UPDATE_CMD;
status = I2CM_Write(MAX34417_I2CM, MAX34417_I2C_ADDR, NULL, 0, &cmd, 1);
if (status != 1) {
return status;
}
return 0;
}
uint32_t max34417_acc_count( void )
{
uint8_t cmd;
static uint8_t buf[4];
cmd = ACC_COUNT_CMD;
if (I2CM_Read(MAX34417_I2CM, MAX34417_I2C_ADDR, &cmd, 1, buf, 4) != 4) {
return 1;
}
return convert_24to32( &buf[1] );
}
bool max34417_bulk_voltage( double voltage[4] )
{
uint8_t cmd;
uint16_t adc;
uint8_t i;
static uint8_t buf[9];
static const double adc2v = 24.0 / (16384.0 - 1.0);
cmd = BULK_VOLT_CMD;
if (I2CM_Read(MAX34417_I2CM, MAX34417_I2C_ADDR, &cmd, 1, buf, 9) != 9)
{
return false;
}
for (i=0;i<4;i++)
{
adc = convert_14to16( &buf[1+i*2] );
#if defined(MAX78002EVKIT)
/* swap physical channels 1 and 2 for AI87 EVKit */
if (i == 1)
voltage[2] = (double)adc * adc2v;
else if (i == 2)
voltage[1] = (double)adc * adc2v;
else
voltage[i] = (double)adc * adc2v;
#else
voltage[i] = (double)adc * adc2v;
#endif
}
return true;
}
bool max34417_bulk_energy( double energy_raw[4] )
{
uint8_t i;
uint8_t cmd;
#if defined(MAX78002EVKIT)
double raw;
#endif
static uint8_t buf[29];
cmd = BULK_PWR_CMD;
if (I2CM_Read(MAX34417_I2CM, MAX34417_I2C_ADDR, &cmd, 1, buf, 29) != 29) {
return false;
}
for (i=0;i<4;i++)
{
#if defined(MAX78002EVKIT)
/* swap physical channels 1 and 2 for AI87 EVKit */
raw = convert_56to64( &buf[1+i*7] );
if (i == 1)
energy_raw[2] = raw;
else if (i == 2)
energy_raw[1] = raw;
else
energy_raw[i] = raw;
#else
energy_raw[i] = convert_56to64( &buf[1+i*7] );
#endif
}
return true;
}
bool max34417_bulk_power( double power_raw[4] )
{
uint8_t i;
uint32_t acc_count = max34417_acc_count();
if( !max34417_bulk_energy( power_raw ) )
return false;
for (i=0;i<4;i++)
{
power_raw[i] = power_raw[i] / (double)acc_count;
}
return true;
}