@@ -44,124 +44,124 @@ class i2c_smbus_ioctl_data(Structure):
44
44
('data' , POINTER (i2c_smbus_data ))]
45
45
46
46
def i2c_smbus_access (file , read_write , command , size , data ):
47
- try :
48
- args = i2c_smbus_ioctl_data (read_write , command , size , data )
49
- return fcntl .ioctl (file , I2C_SMBUS , args )
50
- except IOError , e :
51
- if debug : print '## i2c_smbus_access ##: IOError {0}' .format (e )
52
- return - 1
53
- except IOError , e :
54
- return - 1
55
- except Exception , e :
56
- print '## i2c_smbus_access ## {0}' .format (e )
57
- return - 1
47
+ args = i2c_smbus_ioctl_data (read_write , command , size , data )
48
+ return fcntl .ioctl (file , I2C_SMBUS , args )
58
49
59
50
def i2c_smbus_write_quick (file , mode ):
60
- if debug : print '## i2c_smbus_write_byte ##: mode 0x{0:00X }' .format (mode )
51
+ if debug : print '## i2c_smbus_write_quick ##: mode 0x{0:02X }' .format (mode )
61
52
data = i2c_smbus_data (0 )
62
- return i2c_smbus_access (file , mode , 0 , I2C_SMBUS_QUICK , pointer (data ))
53
+ i2c_smbus_access (file , mode , 0 , I2C_SMBUS_QUICK , pointer (data ))
63
54
64
55
def i2c_smbus_read_byte (file ):
65
56
if debug : print '## i2c_smbus_read_byte ##'
66
57
data = i2c_smbus_data (0 )
67
- if (i2c_smbus_access (file , I2C_SMBUS_READ , 0 , I2C_SMBUS_BYTE , pointer (data )) == 0 ):
68
- return 0x0FF & data .byte
69
- else :
70
- return - 1
58
+ i2c_smbus_access (file , I2C_SMBUS_READ , 0 , I2C_SMBUS_BYTE , pointer (data ))
59
+ return 0x0FF & data .byte
71
60
72
61
def i2c_smbus_write_byte (file , daddr ):
73
- if debug : print '## i2c_smbus_write_byte ##: daddr 0x{0:00X }' .format (daddr )
62
+ if debug : print '## i2c_smbus_write_byte ##: daddr 0x{0:02X }' .format (daddr )
74
63
data = i2c_smbus_data (0 )
75
- return i2c_smbus_access (file , I2C_SMBUS_WRITE , daddr , I2C_SMBUS_BYTE , pointer (data ))
64
+ i2c_smbus_access (file , I2C_SMBUS_WRITE , daddr , I2C_SMBUS_BYTE , pointer (data ))
76
65
77
66
def i2c_smbus_read_byte_data (file , daddr ):
78
- if debug : print '## i2c_smbus_read_byte_data ##: daddr 0x{0:00X }' .format (daddr )
67
+ if debug : print '## i2c_smbus_read_byte_data ##: daddr 0x{0:02X }' .format (daddr )
79
68
data = i2c_smbus_data (0 )
80
- if (i2c_smbus_access (file , I2C_SMBUS_READ , daddr , I2C_SMBUS_BYTE_DATA , pointer (data )) == 0 ):
81
- return 0x0FF & data .byte
82
- else :
83
- return - 1
69
+ i2c_smbus_access (file , I2C_SMBUS_READ , daddr , I2C_SMBUS_BYTE_DATA , pointer (data ))
70
+ return 0x0FF & data .byte
84
71
85
72
def i2c_smbus_write_byte_data (file , daddr , value ):
86
- if debug : print '## i2c_smbus_write_byte_data ##: daddr 0x{0:00X }, value=0x{1:00X }' .format (daddr , value )
73
+ if debug : print '## i2c_smbus_write_byte_data ##: daddr 0x{0:02X }, value=0x{1:02X }' .format (daddr , value )
87
74
data = i2c_smbus_data (0 )
88
75
data .byte = value
89
- return i2c_smbus_access (file , I2C_SMBUS_WRITE , daddr , I2C_SMBUS_BYTE_DATA , pointer (data ))
76
+ i2c_smbus_access (file , I2C_SMBUS_WRITE , daddr , I2C_SMBUS_BYTE_DATA , pointer (data ))
90
77
91
78
def i2c_smbus_read_word_data (file , daddr ):
92
- if debug : print '## i2c_smbus_read_word_data ##: daddr 0x{0:00X }' .format (daddr )
79
+ if debug : print '## i2c_smbus_read_word_data ##: daddr 0x{0:02X }' .format (daddr )
93
80
data = i2c_smbus_data (0 )
94
- if (i2c_smbus_access (file , I2C_SMBUS_READ , daddr , I2C_SMBUS_WORD_DATA , pointer (data )) == 0 ):
95
- return 0x0FFFF & data .word
96
- else :
97
- return - 1
81
+ i2c_smbus_access (file , I2C_SMBUS_READ , daddr , I2C_SMBUS_WORD_DATA , pointer (data ))
82
+ return 0x0FFFF & data .word
98
83
99
84
def i2c_smbus_write_word_data (file , daddr , value ):
100
- if debug : print '## i2c_smbus_write_word_data ##: daddr 0x{0:00X }, value=0x{1:0000X }' .format (daddr , value )
85
+ if debug : print '## i2c_smbus_write_word_data ##: daddr 0x{0:02X }, value=0x{1:04X }' .format (daddr , value )
101
86
data = i2c_smbus_data (0 )
102
87
data .word = value
103
- return i2c_smbus_access (file , I2C_SMBUS_WRITE , daddr , I2C_SMBUS_WORD_DATA , pointer (data ))
88
+ i2c_smbus_access (file , I2C_SMBUS_WRITE , daddr , I2C_SMBUS_WORD_DATA , pointer (data ))
89
+
90
+ def i2c_smbus_read_i2c_block_data (file , daddr , length ):
91
+ if debug : print '## i2c_smbus_read_i2c_block_data ##: daddr 0x{0:02X}, length 0x{1:02X}' .format (daddr , length )
92
+ data = i2c_smbus_data (0 )
93
+ if length > 32 :
94
+ length = 32
95
+ data .block [0 ] = length
96
+ i2c_smbus_access (file , I2C_SMBUS_READ , daddr ,
97
+ I2C_SMBUS_I2C_BLOCK_BROKEN if length == 32 else I2C_SMBUS_I2C_BLOCK_DATA , pointer (data ))
98
+ values = []
99
+ for i in range (data .block [0 ]):
100
+ values .append (data .block [i + 1 ])
101
+ return values
102
+
103
+ def i2c_smbus_write_i2c_block_data (file , daddr , length , values ):
104
+ if debug : print '## i2c_smbus_write_i2c_block_data ##: daddr 0x{0:02X}, length 0x{1:02X}' .format (daddr , length )
105
+ data = i2c_smbus_data (0 )
106
+ if length > 32 :
107
+ length = 32
108
+ data .block [0 ] = length
109
+ for i in range (length ):
110
+ data .block [i + 1 ] = values [i ]
111
+ i2c_smbus_access (file , I2C_SMBUS_WRITE , daddr , I2C_SMBUS_I2C_BLOCK_BROKEN , pointer (data ))
104
112
105
113
# Main entry points
106
- def _i2cRead (addr , reg = 0 , size = '' ):
107
- if debug : print '## i2cRead ## addr={0}, reg={1}, size={2}' .format (addr , reg , size )
108
- try :
109
- file = os .open (RASCAL_I2C , os .O_RDWR )
110
- try :
111
- fcntl .ioctl (file , I2C_SLAVE , addr )
112
- if size .upper () == 'W' :
113
- data = i2c_smbus_read_word_data (file , reg )
114
- elif size .upper () == 'B' :
115
- data = i2c_smbus_read_byte_data (file , reg )
116
- else :
117
- data = i2c_smbus_read_byte (file )
118
- except Exception , e :
119
- if debug : print '## i2cRead ## Can\' t set slave addr {0}' .format (e )
120
- data = - 1
121
- os .close (file )
122
- except OSError :
123
- if debug : print '## i2cRead ## Can\' t open {0}' .format (RASCAL_I2C )
124
- data = - 2
114
+ def _i2cRead (addr , reg = 0 , size = '' , length = 0 ):
115
+ if debug : print '## i2cRead ## addr={0}, reg={1}, size={2}, length={3}' .format (addr , reg , size , length )
116
+ file = os .open (RASCAL_I2C , os .O_RDWR )
117
+ fcntl .ioctl (file , I2C_SLAVE , addr )
118
+ if size .upper () == 'I' and length > 0 :
119
+ data = i2c_smbus_read_i2c_block_data (file , reg , length )
120
+ elif size .upper () == 'W' :
121
+ data = i2c_smbus_read_word_data (file , reg )
122
+ elif size .upper () == 'B' :
123
+ data = i2c_smbus_read_byte_data (file , reg )
124
+ else :
125
+ data = i2c_smbus_read_byte (file )
126
+ os .close (file )
125
127
return data
126
128
127
129
def _i2cWrite (addr , reg , value = '' , size = 'B' ):
128
130
if value == '' : size = 'C' ; value = 0
129
131
if debug : print '## i2cWrite ## addr=0x{0:02x}, reg=0x{1:02x}, value=0x{2:04X}, size={3}' .format (addr , reg , value , size )
130
- try :
131
- file = os .open (RASCAL_I2C , os .O_RDWR )
132
- try :
133
- fcntl .ioctl (file , I2C_SLAVE , addr )
134
- if size .upper () == 'W' :
135
- data = i2c_smbus_write_word_data (file , reg , value )
136
- elif size .upper () == 'B' :
137
- data = i2c_smbus_write_byte_data (file , reg , value )
138
- else :
139
- data = i2c_smbus_write_byte (file , reg )
140
- except Exception , e :
141
- if debug : print '## i2cWrite ## Can\' t set slave addr {0}' .format (e )
142
- data = - 1
143
- os .close (file )
144
- except OSError :
145
- if debug : print '## i2cWrite ## Can\' t open {0}' .format (RASCAL_I2C )
146
- data = - 2
132
+ file = os .open (RASCAL_I2C , os .O_RDWR )
133
+ fcntl .ioctl (file , I2C_SLAVE , addr )
134
+ if size .upper () == 'I' and isinstance (value , list ) and len (value ) > 0 :
135
+ data = i2c_smbus_write_i2c_block_data (file , reg , len (value ), value )
136
+ elif size .upper () == 'W' :
137
+ data = i2c_smbus_write_word_data (file , reg , value )
138
+ elif size .upper () == 'B' :
139
+ data = i2c_smbus_write_byte_data (file , reg , value )
140
+ else :
141
+ data = i2c_smbus_write_byte (file , reg )
142
+ os .close (file )
147
143
return data
148
144
149
145
# Support for scanning i2C bus
150
146
def probe_bus (file , addr ):
151
147
# Set slave address
152
148
try :
153
149
fcntl .ioctl (file , I2C_SLAVE , addr )
154
- if ((addr >= 0x30 and addr <= 0x37 ) or (addr >= 0x50 and addr <= 0x5F )):
155
- res = i2c_smbus_read_byte (file )
156
- else :
157
- res = i2c_smbus_write_quick (file , I2C_SMBUS_WRITE )
158
- if res < 0 :
159
- return I2C_SCAN_NODEV
160
- else :
150
+ try :
151
+ if ((addr >= 0x30 and addr <= 0x37 ) or (addr >= 0x50 and addr <= 0x5F )):
152
+ res = i2c_smbus_read_byte (file )
153
+ else :
154
+ res = i2c_smbus_write_quick (file , I2C_SMBUS_WRITE )
161
155
return addr
162
- except Exception , e :
156
+ except IOError :
157
+ return I2C_SCAN_NODEV
158
+ except Exception as e :
159
+ print '## probe_bus ## Unexpected exception: probe address {0}' .format (e )
160
+ except IOError :
163
161
return I2C_SCAN_BUSY
164
-
162
+ except Exception as e :
163
+ print '## probe_bus ## Unexpected exception: set slave address {0}' .format (e )
164
+
165
165
# Address status: 0=out of range, -1=not present, -2=busy, otherwise device address
166
166
def scanBus (first = 0x03 , last = 0x77 ):
167
167
file = os .open (RASCAL_I2C , os .O_RDWR )
@@ -178,6 +178,3 @@ def scanBus(first = 0x03, last = 0x77):
178
178
scan [i ] = row
179
179
os .close (file )
180
180
return scan
181
-
182
-
183
-
0 commit comments