|
| 1 | +import os, fcntl |
| 2 | +from ctypes import * |
| 3 | + |
| 4 | +RASCAL_I2C = '/dev/i2c-0' |
| 5 | + |
| 6 | +# smbus_access read or write markers |
| 7 | +I2C_SMBUS_READ = 1 |
| 8 | +I2C_SMBUS_WRITE = 0 |
| 9 | + |
| 10 | +# SMBus transaction types (size parameter in the above functions) |
| 11 | +I2C_SMBUS_QUICK = 0 |
| 12 | +I2C_SMBUS_BYTE = 1 |
| 13 | +I2C_SMBUS_BYTE_DATA = 2 |
| 14 | +I2C_SMBUS_WORD_DATA= 3 |
| 15 | +I2C_SMBUS_PROC_CALL = 4 |
| 16 | +I2C_SMBUS_BLOCK_DATA = 5 |
| 17 | +I2C_SMBUS_I2C_BLOCK_BROKEN = 6 |
| 18 | +I2C_SMBUS_BLOCK_PROC_CALL = 7 # SMBus 2.0 |
| 19 | +I2C_SMBUS_I2C_BLOCK_DATA = 8 |
| 20 | + |
| 21 | +I2C_SLAVE = 0x0703 |
| 22 | +I2C_FUNCS = 0x0705 |
| 23 | +I2C_SMBUS = 0x0720 # SMBus-level access |
| 24 | + |
| 25 | +I2C_SCAN_BUSY = -2 |
| 26 | +I2C_SCAN_NODEV = -1 |
| 27 | +I2C_SCAN_SKIPPED = 0 |
| 28 | + |
| 29 | +# Data for SMBus Messages |
| 30 | +I2C_SMBUS_BLOCK_MAX = 32 # As specified in SMBus standard |
| 31 | +I2C_SMBUS_I2C_BLOCK_MAX = 32 # Not specified but we use same structure |
| 32 | + |
| 33 | +debug = False |
| 34 | + |
| 35 | +class i2c_smbus_data(Union): |
| 36 | + _fields_ = [('byte', c_ubyte), |
| 37 | + ('word', c_uint), |
| 38 | + ('block', c_ubyte * 34)] |
| 39 | + |
| 40 | +class i2c_smbus_ioctl_data(Structure): |
| 41 | + _fields_ = [('read_write', c_ubyte), |
| 42 | + ('command', c_ubyte), |
| 43 | + ('size', c_int), |
| 44 | + ('data', POINTER(i2c_smbus_data))] |
| 45 | + |
| 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 |
| 58 | + |
| 59 | +def i2c_smbus_write_quick(file, value): |
| 60 | + data = i2c_smbus_data(0) |
| 61 | + return i2c_smbus_access(file, value, 0, I2C_SMBUS_QUICK, pointer(data)) |
| 62 | + |
| 63 | +def i2c_smbus_read_byte(file): |
| 64 | + if debug: print '## i2c_smbus_read_byte ##' |
| 65 | + data = i2c_smbus_data(0) |
| 66 | + if (i2c_smbus_access(file, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, pointer(data)) == 0): |
| 67 | + return 0x0FF & data.byte |
| 68 | + else: |
| 69 | + return -1 |
| 70 | + |
| 71 | +def i2c_smbus_write_byte(file, value): |
| 72 | + if debug: print '## i2c_smbus_write_byte ##: value 0x{0:00X}'.format(value) |
| 73 | + data = i2c_smbus_data(0) |
| 74 | + return i2c_smbus_access(file, I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, pointer(data)) |
| 75 | + |
| 76 | +def i2c_smbus_read_byte_data(file, daddr): |
| 77 | + if debug: print '## i2c_smbus_read_byte_data ##: daddr {0}'.format(daddr) |
| 78 | + data = i2c_smbus_data(0) |
| 79 | + if (i2c_smbus_access(file, I2C_SMBUS_READ, daddr, I2C_SMBUS_BYTE_DATA, pointer(data)) == 0): |
| 80 | + return 0x0FF & data.byte |
| 81 | + else: |
| 82 | + return -1 |
| 83 | + |
| 84 | +def i2c_smbus_write_byte_data(file, daddr, value): |
| 85 | + if debug: print '## i2c_smbus_write_byte_data ##: daddr {0}, value=0x{1:00X}'.format(daddr, value) |
| 86 | + data = i2c_smbus_data(0) |
| 87 | + data.byte = value |
| 88 | + return i2c_smbus_access(file, I2C_SMBUS_WRITE, daddr, I2C_SMBUS_BYTE_DATA, pointer(data)) |
| 89 | + |
| 90 | +def i2c_smbus_read_word_data(file, daddr): |
| 91 | + if debug: print '## i2c_smbus_read_word_data ##: daddr {0}'.format(daddr) |
| 92 | + data = i2c_smbus_data(0) |
| 93 | + if (i2c_smbus_access(file, I2C_SMBUS_READ, daddr, I2C_SMBUS_WORD_DATA, pointer(data)) == 0): |
| 94 | + return 0x0FFFF & data.word |
| 95 | + else: |
| 96 | + return -1 |
| 97 | + |
| 98 | +def i2c_smbus_write_word_data(file, daddr, value): |
| 99 | + if debug: print '## i2c_smbus_write_word_data ##: daddr {0}, value=0x{1:0000X}'.format(daddr, value) |
| 100 | + data = i2c_smbus_data(0) |
| 101 | + data.word = value |
| 102 | + return i2c_smbus_access(file, I2C_SMBUS_WRITE, daddr, I2C_SMBUS_WORD_DATA, pointer(data)) |
| 103 | + |
| 104 | +# Main entry points |
| 105 | +def _i2cRead(addr, reg = 0, size = ''): |
| 106 | + if debug: print '## i2cRead ## addr={0}, reg={1}, size={2}'.format(addr, reg, size) |
| 107 | + try: |
| 108 | + file = os.open(RASCAL_I2C, os.O_RDWR) |
| 109 | + try: |
| 110 | + fcntl.ioctl(file, I2C_SLAVE, addr) |
| 111 | + if size.upper() == 'W': |
| 112 | + data = i2c_smbus_read_word_data(file, reg) |
| 113 | + elif size.upper() == 'B': |
| 114 | + data = i2c_smbus_read_byte_data(file, reg) |
| 115 | + else: |
| 116 | + data = i2c_smbus_read_byte(file) |
| 117 | + except Exception, e: |
| 118 | + if debug: print '## i2cRead ## Can\'t set slave addr {0}'.format(e) |
| 119 | + data = -1 |
| 120 | + os.close(file) |
| 121 | + except OSError: |
| 122 | + if debug: print '## i2cRead ## Can\'t open {0}'.format(RASCAL_I2C) |
| 123 | + data = -2 |
| 124 | + return data |
| 125 | + |
| 126 | +def _i2cWrite(addr, reg, value, size): |
| 127 | + if debug: print '## i2cWrite ## addr=0x{0:02x}, reg=0x{1:02x}, value=0x{2:04X}, size={3}'.format(addr, reg, value, size) |
| 128 | + try: |
| 129 | + file = os.open(RASCAL_I2C, os.O_RDWR) |
| 130 | + try: |
| 131 | + fcntl.ioctl(file, I2C_SLAVE, addr) |
| 132 | + if size.upper() == 'W': |
| 133 | + data = i2c_smbus_write_word_data(file, reg, value) |
| 134 | + elif size.upper() == 'B': |
| 135 | + data = i2c_smbus_write_byte_data(file, reg, value) |
| 136 | + else: |
| 137 | + data = i2c_smbus_write_byte(file, value) |
| 138 | + except Exception, e: |
| 139 | + if debug: print '## i2cWrite ## Can\'t set slave addr {0}'.format(e) |
| 140 | + data = -1 |
| 141 | + os.close(file) |
| 142 | + except OSError: |
| 143 | + if debug: print '## i2cWrite ## Can\'t open {0}'.format(RASCAL_I2C) |
| 144 | + data = -2 |
| 145 | + return data |
| 146 | + |
| 147 | +# Support for scanning i2C bus |
| 148 | +def probe_bus(file, addr): |
| 149 | + # Set slave address |
| 150 | + try: |
| 151 | + fcntl.ioctl(file, I2C_SLAVE, addr) |
| 152 | + if ((addr >= 0x30 and addr <= 0x37) or (addr >= 0x50 and addr <= 0x5F)): |
| 153 | + res = i2c_smbus_read_byte(file) |
| 154 | + else: |
| 155 | + res = i2c_smbus_write_quick(file, I2C_SMBUS_WRITE) |
| 156 | + if res < 0: |
| 157 | + return I2C_SCAN_NODEV |
| 158 | + else: |
| 159 | + return addr |
| 160 | + except Exception, e: |
| 161 | + return I2C_SCAN_BUSY |
| 162 | + |
| 163 | +# Address status: 0=out of range, -1=not present, -2=busy, otherwise device address |
| 164 | +def scanBus(first = 0x03, last = 0x77): |
| 165 | + file = os.open(RASCAL_I2C, os.O_RDWR) |
| 166 | + scan = {} |
| 167 | + for i in range(0, 128, 16): |
| 168 | + row = () |
| 169 | + for j in range(0, 16): |
| 170 | + addr = i + j |
| 171 | + # Skip unwanted addresses |
| 172 | + if (addr < first) or (addr > last): |
| 173 | + row += (I2C_SCAN_SKIPPED,) |
| 174 | + else: |
| 175 | + row += (probe_bus(file, addr),) |
| 176 | + scan[i] = row |
| 177 | + os.close(file) |
| 178 | + return scan |
| 179 | + |
| 180 | + |
| 181 | + |
0 commit comments