Project

General

Profile

RE: I2C Access with 5CSX Development Board » ltc2945.cpp

Michael Williamson, 06/17/2014 11:04 AM

 
1
/**
2
 *      \file   ltc2945.cpp
3
 *
4
 *      \brief  Class for LTC2945 power monitor chip.
5
 *
6
 *	\author Michael Williamson <michael.williamson@criticallink.com>
7
 *
8
 *     o  0
9
 *     | /       Copyright (c) 2014
10
 *    (CL)---o   Critical Link, LLC
11
 *      \
12
 *       O
13
 */
14
#include "ltc2945.h"
15
#include "i2c-dev.h"
16

    
17
#include <stdio.h>
18
#include <stdlib.h>
19

    
20
#define REG_DELTA_SENSE_MSB 0x14
21
#define REG_DELTA_SENSE_LSB 0x15
22

    
23
using namespace MityDSP;
24

    
25
tcLTC2945::tcLTC2945(int anBus, int anAddr, float Rsens, float Vin)
26
: tcI2CDevice(anBus, anAddr)
27
, mnVin(Vin)
28
, mnRsens(Rsens)
29
{
30
}
31

    
32
int tcLTC2945::GetDeltaSense(float& volts)
33
{
34
	int temp, val;
35
	temp = i2c_smbus_read_byte_data(mnFd, REG_DELTA_SENSE_MSB);
36
	if (temp < 0)
37
	{
38
		perror(__func__);
39
		return temp;
40
	}
41
	val |= temp << 8;
42
	val &= 0x00FF00;
43

    
44
	temp = i2c_smbus_read_byte_data(mnFd, REG_DELTA_SENSE_LSB);
45
	if (temp < 0)
46
	{
47
		perror("%s:");
48
		return temp;
49
	}
50
	val |= (temp & 0x00FF);
51
	val >>= 4;
52
	volts = val * 0.000025;
53
	return 0;
54
}
55

    
56
int tcLTC2945::Initialize(void)
57
{
58
    int  ret;
59
    char config;
60
    return Open();
61
}
62

    
63
int tcLTC2945::GetPowerW(float& power)
64
{
65
	int rv;
66
	float volts;
67
	rv = GetDeltaSense(volts);
68
	if (rv)
69
		return rv;
70

    
71
	float current = volts/mnRsens;
72
	power = current*mnVin;
73
	return 0;
74
}
75

    
76
#ifdef LTC2945_APP
77

    
78
#include <stdlib.h>
79
#include <unistd.h>
80
#include <string.h>
81

    
82
using namespace MityDSP;
83

    
84
int main(int argc, char* argv[])
85
{
86
	tcLTC2945* lpTmp;
87
	int addr = 0x6F; // 110 1111
88
	int bus = 1;
89
	int opt;
90
	opterr = 0;
91
	int rv;
92
	float Rsens = 0.01;
93
	float Vin = 5.00;
94

    
95
	while ((opt = getopt(argc, argv, "a:b:r:v:h")) != -1)
96
	{
97
		switch (opt)
98
		{
99
		case 'a':
100
			sscanf(optarg,"%x",&addr);
101
			break;
102

    
103
		case 'b':
104
			bus = strtoul(optarg,NULL,10);
105
			break;
106

    
107
		case 'v':
108
			sscanf(optarg,"%f", &Vin);
109
			break;
110

    
111
		case 'r':
112
			sscanf(optarg,"%f", &Rsens);
113
			break;
114

    
115
		case 'h': /* '?' */
116
		default: /* '?' */
117
			fprintf(stderr, "Usage: %s [-b <busnum>] [-a <addr>] [-v <Vin>] [-r <Rsens>]\n", argv[0]);
118
			fprintf(stderr, "Defaults: bus = %d, addr=0x%x\n", bus, addr);
119
			fprintf(stderr, "Defaults: Vin = %f, Rsens=0x%f\n", Vin, Rsens);
120
			return -1;
121
		}
122
	}
123

    
124
	float power;
125
	float volts;
126

    
127
	lpTmp = new tcLTC2945(bus, addr, Rsens, Vin);
128
	lpTmp->Initialize();
129

    
130
	rv = lpTmp->GetDeltaSense(volts);
131
	rv = lpTmp->GetPowerW(power);
132
	if (rv)
133
		fprintf(stderr, "unable to read power [%d, 0x%x]\n", rv, rv);
134
	else
135
		printf("Read %.5f volts on Vsens, computed power is %.3f W\n", volts, power);
136

    
137
	delete lpTmp;
138

    
139
	return 0;
140

    
141
}
142

    
143
#endif
(1-1/6) Go to top
Add picture from clipboard (Maximum size: 1 GB)