Not smBUS

(** There is a new version of this called easyi2c here **)

** Update 14 March 2016:

There is now an alternative to notSMB written in python, it does not need installing and is very simple to use.

This is a simple Python access to I2C which consists mainly of one function and works as follows:

from notsmb import notSMB
bus = notSMB(0)

#read 4 bytes form an I2C device with address 0x90
l = bus.i2c(0x90,[],4)

#write 4 bytes form an I2C device with address 0x90
l = bus.i2c(0x90,[1,2,3,4],0)

#send a command byte 0x22 followed by another byte 6 then read 2 bytes
l = bus.i2c(0x90,[0x22,6],2)

The main reason for creating this was that I needed i2c access from Python and all that was available was smbus, which although uses i2c, is not really i2c access.

There is another function called detect that will return a list of I2C devices on the bus:

list = bus.detect()

Installation

sudo apt-get install python-dev   (takes quite a while)

*** Check that this installs okay it may need apt-get update. If it does not install okay then notsmb will not install.

mkdir notsmb

cd notsmb

Download the notsmb_1_0.zip file into that directory (file name may be different)

wget http://www.byvac.com/downloads/sws/notsmb_1_0.zip

unzip notsmb_1_0.zip

sudo python setup.py install

There will be a few errors shown but it should be okay.

Examples

To read the time registers of the DS1307 Real time clock:

bus.i2c(0x68,[0],8)

The results will be something like

[3, 70, 7, 4, 17, 9, 19, 0]

Which from the data sheet is seconds, minutes, hours, day, date, month, year and control. Note that the result is in BCD so it may not make too much sense as it is.

To set the time to 5:03 Wednesday 25 Sept 2013 would be something like below, not exactly as the values need coding to BCD

bus.i2c(0x68,[0,3,5,3,25,9,13],0)

Reading a 24C32 EEEPROM

Read the first 10 values:

bus.i2c(0x28,[0,0],10)

Read bytes at address 30:

bus.i2c(0x28,[0,30],10)

Write 7 & 8 to address 45,46:

bus.i2c(0x28,[0,45,7],0)

Improvements

There is lots of scope for improvement but in particular there is a hard coded delay between i2c access. This is necessary because long transactions will overwhelm the i2c system. At some point in time when accessing the I2C bus the protocol will wait for the slave to complete its function, this is all part of I2C and in fact any serial communication so there must be a method to tell the calling program to wait, some sort of ready function.

I don't yet know how to wait for the I2C, there may be an ioctl() command for this.

FAQ

Trying to install using "sudo python setup.py install" Gives the following errors and does not install.

notsmbmodule.c:138:3: warning: implicit declaration of function ‘PyInt_Check’ [-Wimplicit-function-declaration]
notsmbmodule.c:142:3: warning: implicit declaration of function ‘PyInt_AS_LONG’ [-Wimplicit-function-declaration]
notsmbmodule.c: In function ‘notsmb_i2c’:
notsmbmodule.c:179:13: warning: array subscript has type ‘char’ [-Wchar-subscripts]
notsmbmodule.c:200:13: warning: array subscript has type ‘char’ [-Wchar-subscripts]
notsmbmodule.c:201:13: warning: array subscript has type ‘char’ [-Wchar-subscripts]
notsmbmodule.c: In function ‘notsmb_discover’:
notsmbmodule.c:218:9: warning: variable ‘t’ set but not used [-Wunused-but-set-variable]
notsmbmodule.c: In function ‘notSMB_dealloc’:
notsmbmodule.c:262:6: error: ‘notSMB’ has no member named ‘ob_type’
notsmbmodule.c: At top level:
notsmbmodule.c:281:2: warning: missing braces around initializer [-Wmissing-braces]
notsmbmodule.c:281:2: warning: (near initialization for ‘notSMB_type.ob_base.ob_base’) [-Wmissing-braces]
notsmbmodule.c:283:2: warning: initialization makes integer from pointer without a cast [enabled by default]
notsmbmodule.c:283:2: warning: (near initialization for ‘notSMB_type.tp_basicsize’) [enabled by default]
notsmbmodule.c:286:2: warning: initialization from incompatible pointer type [enabled by default]
notsmbmodule.c:286:2: warning: (near initialization for ‘notSMB_type.tp_print’) [enabled by default]
notsmbmodule.c:301:2: warning: initialization makes pointer from integer without a cast [enabled by default]
notsmbmodule.c:301:2: warning: (near initialization for ‘notSMB_type.tp_doc’) [enabled by default]
notsmbmodule.c:302:2: warning: initialization from incompatible pointer type [enabled by default]
notsmbmodule.c:302:2: warning: (near initialization for ‘notSMB_type.tp_traverse’) [enabled by default]
notsmbmodule.c:309:2: warning: initialization from incompatible pointer type [enabled by default]
notsmbmodule.c:309:2: warning: (near initialization for ‘notSMB_type.tp_members’) [enabled by default]
notsmbmodule.c:317:2: warning: initialization from incompatible pointer type [enabled by default]
notsmbmodule.c:317:2: warning: (near initialization for ‘notSMB_type.tp_alloc’) [enabled by default]
notsmbmodule.c:319:2: warning: initialization from incompatible pointer type [enabled by default]
notsmbmodule.c:319:2: warning: (near initialization for ‘notSMB_type.tp_free’) [enabled by default]
notsmbmodule.c: In function ‘initnotsmb’:
notsmbmodule.c:332:3: warning: ‘return’ with no value, in function returning non-void [-Wreturn-type]
notsmbmodule.c:334:2: warning: implicit declaration of function ‘Py_InitModule3’ [-Wimplicit-function-declaration]
notsmbmodule.c:334:4: warning: assignment makes pointer from integer without a cast [enabled by default]
error: command 'gcc' failed with exit status 1

Answer

The above errors are generated if python 3 is installed. notsmb is currently designed for python version 2.7 whaich is the Raspberry Pi default version.