Subversion Repositories svnkaklik

Rev

Rev 341 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
341 kaklik 1
/*****************************************************************************/
2
/*
3
 *  BoardController.cpp - communication with NGW100 Board controller
4
 *
5
 *      Copyright (C) 2007  Karel Hojdar
6
 *
7
 *      This program is free software; you can redistribute it and/or modify
8
 *      it under the terms of the GNU General Public License as published by
9
 *      the Free Software Foundation; either version 2 of the License, or
10
 *      (at your option) any later version.
11
 *
12
 *      This program is distributed in the hope that it will be useful,
13
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *      GNU General Public License for more details.
16
 *
17
 *
18
 *
19
 *  Revision history
20
 *    15.06.2007   1.0   Initial release
21
 */
22
/*****************************************************************************/
23
 
24
#include <iostream>
25
 
26
#include <getopt.h>
27
#include <errno.h>
28
#include <string.h>
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <unistd.h>
32
#include "linux/i2c-dev.h"
33
#include <sys/ioctl.h>
34
#include <sys/types.h>
35
#include <sys/stat.h>
36
#include <fcntl.h>
37
 
38
using namespace std;
39
 
40
static char* version = "BoardController, version 0.1";
41
 
42
static char* help = "Usage: BoardController [OPTION]\n  -I\t\tshow ID\n  -M\t\tshow model\n  -R\t\tshow revision\n  -S\t\tshow serial no.\n  -T\t\tshow temperature\n\n";
43
 
44
#define I2C_SLAVE	0x0703	/* Change slave address			*/
45
#define I2C_RDWR	0x0707	/* Combined R/W transfer (one stop only)*/
46
 
47
#define BC_Addr	0x0B
48
 
49
void DoExit(int ex)
50
{
51
	exit(ex);
52
}
53
 
54
unsigned char xfunc[5] = {0x99, 0x9A, 0x9B, 0x9E, 0x8D};
55
unsigned char xlen[5] = {8, 6, 1, 15, 2};
56
 
57
int main(int argc, char *argv[], char *envp[])
58
{
59
	char	*progname;
345 kaklik 60
	int c, func = 0, Len;
341 kaklik 61
	int	i2cbus = 0;
62
	char filename[20], Buf[64];
63
	int file;
64
 
65
	progname = strrchr(argv[0], '/');
66
	progname = progname ? progname + 1 : argv[0];
67
 
68
	while ((c = getopt (argc, argv, "IiMmRrSsTth")) != -1)
69
		switch (c)
70
		{
71
		case 'I':
72
		case 'i':
73
			func = 0;
74
			break;
75
 
76
		case 'M':
77
		case 'm':
78
			func = 1;
79
			break;
80
 
81
		case 'R':
82
		case 'r':
83
			func = 2;
84
			break;
85
 
86
		case 'S':
87
		case 's':
88
			func = 3;
89
			break;
90
 
91
		case 'T':
92
		case 't':
93
			func = 4;
94
			break;
95
 
96
		case 'h':
97
			printf ("%s\n%s", version, help);
98
			return 1;
99
 
100
		case '?':
101
			printf ("Unknown option `-%c', try %s -h.\n", optopt,progname);
102
			return 1;
103
		}
104
 
105
	sprintf(filename, "/dev/i2c-%d", i2cbus);
106
	file = open(filename, O_RDWR);
107
 
108
	if (file < 0)
109
	{
110
		cerr << "Could not open /dev/i2c-0." << endl;
111
		return -1;
112
	}
113
 
114
	if (ioctl(file, I2C_SLAVE, BC_Addr) == -1)
115
	{
116
		fprintf(stderr, "Failed to set address to 0x%02x.\n", BC_Addr);
117
		DoExit(-2);
118
	}
119
 
120
	int Loops = 0;
121
 
122
	do
123
	{
124
		Buf[0] = xfunc[func];
125
		if ( write(file, Buf, 1) != 1)
126
		{
127
			fprintf(stderr, "Failed to write byte to address to 0x%02x, errno %i.\n", BC_Addr, errno);
128
			DoExit(-3);
129
		}
130
 
131
		if (read(file, Buf, 1) != 1)
132
		{
133
			fprintf(stderr, "Failed to read response length, errno %i.\n", errno);
134
			DoExit(-4);
135
		}
136
 
137
		Len = Buf[0];
138
		if (read(file, Buf, Len) != Len)
139
		{
140
			fprintf(stderr, "Failed to read response, errno %i.\n", errno);
141
			DoExit(-5);
142
		}
143
 
144
		Loops++;
145
	} while (Len != xlen[func]);
146
 
147
	if (Loops > 1)
148
		fprintf(stderr, "After %i attempts got: \n", Loops);
149
 
150
	switch (func)
151
	{
152
		case 0:
153
			Buf[Len] = 0x00;
154
			fprintf(stdout, "Board ID is %s.\n", Buf);
155
			break;
156
		case 1:
157
			Buf[Len] = 0x00;
158
			fprintf(stdout, "Model of the board is %s.\n", Buf);
159
			break;
160
		case 2:
161
			fprintf(stdout, "Revision of the board is 0x%02X.\n", Buf[0]);
162
			break;
163
		case 3:
164
			Buf[Len] = 0x00;
165
			fprintf(stdout, "Serial number of the board is %s.\n", Buf);
166
			break;
167
		case 4:
168
			fprintf(stdout, "Temperature is %i or %i.\n", (Buf[0] << 8) + Buf[1], (Buf[1] << 8) + Buf[0]);
169
			break;
170
 
171
	}
172
 
173
	close(file);
174
 
175
	return 0;
176
}