line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#include "EXTERN.h" |
2
|
|
|
|
|
|
|
#include "perl.h" |
3
|
|
|
|
|
|
|
#include "XSUB.h" |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
#include |
6
|
|
|
|
|
|
|
#include |
7
|
|
|
|
|
|
|
#include |
8
|
|
|
|
|
|
|
#include |
9
|
|
|
|
|
|
|
#include |
10
|
|
|
|
|
|
|
#include |
11
|
|
|
|
|
|
|
#include |
12
|
|
|
|
|
|
|
#include |
13
|
|
|
|
|
|
|
#include |
14
|
|
|
|
|
|
|
#include |
15
|
|
|
|
|
|
|
#include "eeprom.h" |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
int write_cycle_time = 0; |
18
|
|
|
|
|
|
|
|
19
|
0
|
|
|
|
|
|
static int _writeAddress(int fd, __u8 buf[2]){ |
20
|
0
|
|
|
|
|
|
int r = i2c_smbus_write_byte_data(fd, buf[0], buf[1]); |
21
|
0
|
0
|
|
|
|
|
if(r < 0){ |
22
|
0
|
|
|
|
|
|
fprintf(stderr, "Error _writeAddress: %s\n", strerror(errno)); |
23
|
0
|
|
|
|
|
|
croak("_writeAddress() failed to write to the i2c bus\n"); |
24
|
|
|
|
|
|
|
} |
25
|
0
|
|
|
|
|
|
usleep(10); |
26
|
0
|
|
|
|
|
|
return r; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
static int _writeByte(int fd, __u8 buf[3]){ |
30
|
|
|
|
|
|
|
int r; |
31
|
0
|
|
|
|
|
|
r = i2c_smbus_write_word_data(fd, buf[0], buf[2] << 8 | buf[1]); |
32
|
0
|
0
|
|
|
|
|
if(r < 0){ |
33
|
0
|
|
|
|
|
|
fprintf(stderr, "Error _writeByte: %s\n", strerror(errno)); |
34
|
0
|
|
|
|
|
|
croak("_writeByte() failed to write to the i2c bus\n"); |
35
|
|
|
|
|
|
|
} |
36
|
0
|
|
|
|
|
|
usleep(10); |
37
|
0
|
|
|
|
|
|
return r; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
static int _writeBlock(int fd, __u8 eepromAddr, int len, __u8 *data){ |
41
|
|
|
|
|
|
|
int r; |
42
|
0
|
|
|
|
|
|
r = i2c_smbus_write_block_data(fd, eepromAddr, len, data); |
43
|
0
|
0
|
|
|
|
|
if(r < 0){ |
44
|
0
|
|
|
|
|
|
fprintf(stderr, "Error _writeBlock: %s\n", strerror(errno)); |
45
|
0
|
|
|
|
|
|
croak("_writeBlock() failed to write to the i2c bus\n"); |
46
|
|
|
|
|
|
|
} |
47
|
0
|
|
|
|
|
|
usleep(10); |
48
|
0
|
|
|
|
|
|
return r; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
int eeprom_init(char *dev_fqn, int addr, int delay){ |
52
|
|
|
|
|
|
|
int funcs, fd, r; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
fd = open(dev_fqn, O_RDWR); |
55
|
0
|
0
|
|
|
|
|
if(fd <= 0) |
56
|
|
|
|
|
|
|
{ |
57
|
0
|
|
|
|
|
|
fprintf(stderr, "Error eeprom_init: %s\n", strerror(errno)); |
58
|
0
|
|
|
|
|
|
return -1; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
// set working device |
62
|
0
|
0
|
|
|
|
|
if( ( r = ioctl(fd, I2C_SLAVE, addr)) < 0) |
63
|
|
|
|
|
|
|
{ |
64
|
0
|
|
|
|
|
|
fprintf(stderr, "Error opening EEPROM i2c connection: %s\n", strerror(errno)); |
65
|
0
|
|
|
|
|
|
return -1; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
write_cycle_time = delay; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
return fd; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
int eeprom_close(int fd){ |
74
|
0
|
|
|
|
|
|
close(fd); |
75
|
0
|
|
|
|
|
|
return 0; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
int eeprom_read_current_byte(int fd){ |
79
|
0
|
|
|
|
|
|
ioctl(fd, BLKFLSBUF); // clear kernel read buffer |
80
|
0
|
|
|
|
|
|
return i2c_smbus_read_byte(fd); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
int eeprom_read(int fd, int mem_addr){ |
84
|
|
|
|
|
|
|
int r; |
85
|
0
|
|
|
|
|
|
ioctl(fd, BLKFLSBUF); // clear kernel read buffer |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
__u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff }; |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
r = _writeAddress(fd, buf); |
90
|
|
|
|
|
|
|
|
91
|
0
|
0
|
|
|
|
|
if (r < 0){ |
92
|
0
|
|
|
|
|
|
return r; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
return(i2c_smbus_read_byte(fd)); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
int eeprom_write(int fd, int mem_addr, int data){ |
99
|
0
|
|
|
|
|
|
__u8 buf[3] = { |
100
|
0
|
|
|
|
|
|
(__u8)(mem_addr >> 8) & 0x00ff, |
101
|
|
|
|
|
|
|
(__u8)mem_addr & 0x00ff, |
102
|
0
|
|
|
|
|
|
(__u8)data |
103
|
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
int ret = _writeByte(fd, buf); |
106
|
0
|
0
|
|
|
|
|
if (ret == 0 && write_cycle_time != 0) { |
|
|
0
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
usleep(1000 * write_cycle_time); |
108
|
|
|
|
|
|
|
} |
109
|
0
|
|
|
|
|
|
return ret; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
int eeprom_write_block(int fd, int mem_addr, int data){ |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
__u8 addr_msb = (mem_addr >> 8) & 0x00ff; |
115
|
0
|
|
|
|
|
|
__u8 buf[2] = { |
116
|
|
|
|
|
|
|
mem_addr & 0x00ff, |
117
|
|
|
|
|
|
|
data |
118
|
|
|
|
|
|
|
}; |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
int ret = _writeByte(fd, buf); |
121
|
|
|
|
|
|
|
|
122
|
0
|
0
|
|
|
|
|
if (ret == 0 && write_cycle_time != 0) { |
|
|
0
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
usleep(1000 * write_cycle_time); |
124
|
|
|
|
|
|
|
} |
125
|
0
|
|
|
|
|
|
return ret; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
MODULE = RPi::EEPROM::AT24C32 PACKAGE = RPi::EEPROM::AT24C32 |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
PROTOTYPES: DISABLE |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
int |
133
|
|
|
|
|
|
|
eeprom_init (dev_fqn, addr, delay) |
134
|
|
|
|
|
|
|
char * dev_fqn |
135
|
|
|
|
|
|
|
int addr |
136
|
|
|
|
|
|
|
int delay |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
int |
139
|
|
|
|
|
|
|
eeprom_close (fd) |
140
|
|
|
|
|
|
|
int fd |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
int |
143
|
|
|
|
|
|
|
eeprom_read_current_byte (fd) |
144
|
|
|
|
|
|
|
int fd |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
int |
147
|
|
|
|
|
|
|
eeprom_read (fd, mem_addr) |
148
|
|
|
|
|
|
|
int fd |
149
|
|
|
|
|
|
|
int mem_addr |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
int |
152
|
|
|
|
|
|
|
eeprom_write (fd, mem_addr, data) |
153
|
|
|
|
|
|
|
int fd |
154
|
|
|
|
|
|
|
int mem_addr |
155
|
|
|
|
|
|
|
int data |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
int |
158
|
|
|
|
|
|
|
eeprom_write_block (fd, mem_addr, data) |
159
|
|
|
|
|
|
|
int fd |
160
|
|
|
|
|
|
|
int mem_addr |
161
|
|
|
|
|
|
|
int data |
162
|
|
|
|
|
|
|
|