forked from Openvario/sensord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24c16.c
168 lines (139 loc) · 3.2 KB
/
24c16.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
#include "24c16.h"
#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
int update_checksum(t_eeprom_data* data)
{
char* p_data;
int i;
char checksum=0x00;
p_data = (char*)data;
for (i=0; i<sizeof(data); i++)
{
checksum += *p_data;
p_data++;
}
//printf("Checksum: %x\n", checksum);
data->checksum = checksum;
return (0);
}
int eeprom_read_data(t_24c16 *eeprom, t_eeprom_data *data)
{
int result;
// read eeprom data to struct
result = eeprom_read(eeprom, (char*)data, 0x00, sizeof(*data));
// verify checksum
if (!verify_checksum(data))
{
printf("EEPROM content not valid !!\n");
printf("Please use -i to initialize EEPROM !!\n");
return 2;
}
else
{
return 0;
}
}
char verify_checksum(t_eeprom_data* data)
{
char* p_data;
int i;
char checksum=0x00;
p_data = (char*)data;
for (i=0; i<sizeof(data); i++)
{
checksum += *p_data;
p_data++;
}
//printf("Checksum read: %x\n", data->checksum);
//printf("Checksum calc: %x\n", checksum);
if (checksum == data->checksum)
{
return (1);
}
else
{
return(0);
}
}
int eeprom_open(t_24c16 *eeprom, unsigned char i2c_address)
{
// local variables
int fd;
char s;
int ret_code = 0;
char offset = 0x00;
// try to open I2C Bus
fd = open("/dev/i2c-1", O_RDWR);
if (fd < 0) {
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
ret_code = 1;
}
if (ioctl(fd, I2C_SLAVE, i2c_address) < 0) {
fprintf(stderr, "ioctl error: %s\n", strerror(errno));
ret_code = 1;
}
// assign file handle to sensor object
eeprom->fd = fd;
eeprom->address = i2c_address;
//write address offset to eeprom
if ((write(eeprom->fd, (void*)&offset, 1)) != 1) { // Send register we want to read from
//printf("Error writing to i2c slave (%s)\n", __func__);
ret_code = 1;
}
if (read(eeprom->fd, &s, 1) != 1) { // Read back data into buf[]
ret_code = 1;
}
if (ret_code == 0)
{
printf("Opened 24C16 on 0x%x\n", i2c_address);
}
else
{
printf("Opened 24C16 on 0x%x failed !!!\n", i2c_address);
}
return (ret_code);
}
char eeprom_write(t_24c16 *eeprom, char *s, unsigned char offset, unsigned char count)
{
unsigned char buf[2];
int i;
//memcpy(&buf[1], s, count);
//write data at offset to eeprom
buf[0] = offset; // This is the register we want to read from
// write byte by byte to EEPROM
for (i=0; i<count;i++)
{
// copy data to write buffer
buf[1]=*(s);
//printf("buf[1]: '%c'\n",buf[1]);
// Write data to EEPROM
if ((write(eeprom->fd, &buf[0], 2)) != 2) { // Send register we want to read from
printf("Error writing to i2c slave (%s)\n", __func__);
return(1);
}
// give EEPROM time for write ...
usleep(10000);
buf[0]++;
s++;
}
return(0);
}
char eeprom_read(t_24c16 *eeprom, char *s, char offset, char count)
{
//write address offset to eeprom
if ((write(eeprom->fd, &offset, 1)) != 1) { // Send register we want to read from
printf("Error writing to i2c slave (%s)\n", __func__);
return(1);
}
if (read(eeprom->fd, s, count) != count) { // Read back data into buf[]
printf("Unable to read from slave\n");
return(1);
}
return(0);
}