473,686 Members | 3,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python i2c ioctl


Hi,
I was trying to make to work directly l i2c with python with the
calls ioctl. But I have of the problems and I do not succeed to go
ahead.

this and l error

[root@axis-00408c000001 /usr/local/wrap]129# python pcf8591_ioctl.p y
Reading from 4 ch 8 bit A/D converter PCF8591
Traceback (most recent call last):
File "pcf8591_ioctl. py", line 269, in ?
main()
File "pcf8591_ioctl. py", line 229, in main
if i2c_open () < 0:
File "pcf8591_ioctl. py", line 168, in i2c_open
fcntl.ioctl(i2c _fd, _IO(ETRAXGPIO_I OCTYPE, IO_SETGET_OUTPU T), 0,
iomask)
TypeError: ioctl requires a file or file descriptor, an integer and
optionally a integer or buffer argument

the code

import fcntl
import os
############### ############### #####
#etrax.h
ETRAXGPIO_IOCTY PE = 43
GPIO_MINOR_A = 0
GPIO_MINOR_B = 1
GPIO_MINOR_LEDS = 2
GPIO_MINOR_G = 3
GPIO_MINOR_LAST = 3
IO_READBITS = 0x1
IO_SETBITS = 0x2
IO_CLRBITS = 0x3
IO_HIGHALARM = 0x4
IO_LOWALARM = 0x5
IO_CLRALARM = 0x6
IO_LEDACTIVE_SE T = 0x7
IO_READDIR = 0x8
IO_SETINPUT = 0x9
IO_SETOUTPUT = 0xA
IO_LED_SETBIT = 0xB
IO_LED_CLRBIT = 0xC
IO_SHUTDOWN = 0xD
IO_GET_PWR_BT = 0xE
IO_CFG_WRITE_MO DE = 0xF

def IO_CFG_WRITE_MO DE_VALUE(msb, data_mask, clk_mask):
( (((msb)&1) << 16) | (((data_mask) &0xFF) << 8) | ((clk_mask) &
0xFF) )
def IO_CFG_WRITE_MO DE_VALUE(msb, data_mask, clk_mask):
( (((msb)&1) << 16) | (((data_mask) &0xFF) << 8) | ((clk_mask) &
0xFF) )

IO_READ_INBITS = 0x10
IO_READ_OUTBITS = 0x11
IO_SETGET_INPUT = 0x12
IO_SETGET_OUTPU T = 0x13
############### ############### #########
############### ############### #########
#ioctl.h

_IOC_NRBITS = 8
_IOC_TYPEBITS= 8
_IOC_SIZEBITS =14
_IOC_DIRBITS = 2
_IOC_NRMASK = ((1 << _IOC_NRBITS)-1)
_IOC_TYPEMASK = ((1 << _IOC_TYPEBITS)-1)
_IOC_SIZEMASK =((1 << _IOC_SIZEBITS)-1)
_IOC_DIRMASK =((1 << _IOC_DIRBITS)-1)
_IOC_NRSHIFT =0
_IOC_TYPESHIFT =(_IOC_NRSHIFT+ _IOC_NRBITS)
_IOC_SIZESHIFT =(_IOC_TYPESHIF T+_IOC_TYPEBITS )
_IOC_DIRSHIFT =(_IOC_SIZESHIF T+_IOC_SIZEBITS )
_IOC_NONE=0
_IOC_WRITE=1
_IOC_READ=2
def _IOC(dir,type,n r,size):
(((dir) << _IOC_DIRSHIFT) |
((type) << _IOC_TYPESHIFT) |
((nr) << _IOC_NRSHIFT) |
((size) << _IOC_SIZESHIFT) )

def _IO(type,nr):
_IOC(_IOC_NONE, (type),(nr),0)

def _IOR(type,nr,si ze):
_IOC(_IOC_READ, (type),(nr),siz eof(size))

def _IOW(type,nr,si ze):
_IOC(_IOC_WRITE ,(type),(nr),si zeof(size))

def _IOWR(type,nr,s ize):
_IOC(_IOC_READ| _IOC_WRITE,(typ e),(nr),sizeof( size))

def _IOC_DIR(nr):
(((nr) >_IOC_DIRSHIF T) & _IOC_DIRMASK)
def _IOC_TYPE(nr):
(((nr) >_IOC_TYPESHIFT ) & _IOC_TYPEMASK)
def _IOC_NR(nr):
(((nr) >_IOC_NRSHIFT ) & _IOC_NRMASK)
def _IOC_SIZE(nr):
(((nr) >_IOC_SIZESHIFT ) & _IOC_SIZEMASK)
def IOC_IN():
(_IOC_WRITE << _IOC_DIRSHIFT)

def IOC_OUT():
(_IOC_READ << _IOC_DIRSHIFT)
def IOC_INOUT():
((_IOC_WRITE|_I OC_READ) << _IOC_DIRSHIFT)
def IOCSIZE_MASK():
(_IOC_SIZEMASK << _IOC_SIZESHIFT)
def IOCSIZE_SHIFT() :
(_IOC_SIZESHIFT )
############### ############### ########
############### ############### ########
############### ############### ########

I2C_DATA_LINE = 1<<24
I2C_CLOCK_LINE = 1<<25
#Get the SDA line state
def i2c_getbit():
value=fcntl.ioc tl(i2c_fd, _IO(ETRAXGPIO_I OCTYPE, IO_READBITS))
if ((value&(I2C_DA TA_LINE))==0):
return 0
else:
return 1

#Set the SDA line as output
def i2c_dir_out():
iomask = I2C_DATA_LINE
fcntl.ioctl(i2c _fd, _IO(ETRAXGPIO_I OCTYPE, IO_SETGET_OUTPU T), iomask)

#Set the SDA line as input
def i2c_dir_in():
iomask = I2C_DATA_LINE
fcntl.ioctl(i2c _fd, _IO(ETRAXGPIO_I OCTYPE, IO_SETGET_INPUT ), iomask)

#Set the SDA line state
def i2c_data(state) :
if (state==1):
i2c_dir_in()
else:
i2c_dir_out()
fcntl.ioctl(i2c _fd, _IO(ETRAXGPIO_I OCTYPE, IO_CLRBITS),
I2C_DATA_LINE)
#Set the SCL line state
def i2c_clk(state):
if (state==1):
ioctl(i2c_fd, _IO(ETRAXGPIO_I OCTYPE, IO_SETBITS), I2C_CLOCK_LINE)
else:
ioctl(i2c_fd, _IO(ETRAXGPIO_I OCTYPE, IO_CLRBITS), I2C_CLOCK_LINE)

#Read a byte from I2C bus and send the ack sequence
#Put islast = 1 is this is the last byte to receive from the slave

def i2c_inbyte(isla st):
value = 0

#Read data byte

i2c_clk(0)
i2c_dir_in()
for i in range (0,5):
i2c_clk(1)
bitvalue = i2c_getbit()
value |= bitvalue
if (i<7):
value = 1
i2c_clk(0)

if (islast==0):
#Send Ack if is not the last byte to read
i2c_dir_out()
i2c_data(0)
i2c_clk(1)
i2c_clk(0)
i2c_dir_in()
else:
#Doesn't send Ack if is the last byte to read
i2c_dir_in()
i2c_clk(1)
i2c_clk(0)
return value

#Open the GPIOB dev
def i2c_open():
i2c_fd = os.open("/dev/gpiog", os.O_RDWR)
iomask = I2C_CLOCK_LINE
fcntl.ioctl(i2c _fd, _IO(ETRAXGPIO_I OCTYPE, IO_SETGET_OUTPU T), iomask)
iomask = I2C_DATA_LINE
fcntl.ioctl(i2c _fd, _IO(ETRAXGPIO_I OCTYPE, IO_SETGET_INPUT ), iomask)
fcntl.ioctl(i2c _fd, _IO(ETRAXGPIO_I OCTYPE, IO_SETBITS),
I2C_DATA_LINE)
i2c_dir_in()
i2c_clk(1)
return i2c_fd
#Close the GPIOB dev
def i2c_close():
i2c_dir_in()
i2c_fd.close()
#Send a start sequence to I2C bus

def i2c_start():
i2c_clk(0)
i2c_data(1)
i2c_clk(1)
i2c_data(0)

#Send a stop sequence to I2C bus
def i2c_stop():
i2c_clk(0)
i2c_data(0)
i2c_clk(1)
i2c_data(1)

#Send a byte to the I2C bus and return the ack sequence from slave rtc
#0 = Nack, 1=Ack

def i2c_outbyte():
i2c_clk(0)
for i in range (0,8):
if (x & 0x80):
i2c_data(1)
else:
i2c_data(0)
i2c_clk(1)
i2c_clk(0)
x <<= 1
i2c_dir_in()
i2c_clk(1)
ack=i2c_getbit( )
i2c_clk(0)
if (ack==0):
return 1
else:
return 0

#PCF8591 address scheme
#| 1 | 0 | 0 | 1 | A2 | A1 | A0 | R/W |
#| i2c_fixed | i2c_addr | 1/0 |
def main ():
i2c_fixed = 0x09
i2c_addr = 0x01

print "Reading from 4 ch 8 bit A/D converter PCF8591"

if i2c_open () < 0:
print "i2c open error"
return 1

ch = 0

while "azz":
i2c_start ()

if i2c_outbyte ((i2c_fixed<<4) |(i2c_addr<<1)| 0) == 0:
print "NACK received"
i2c_stop ()
continue

if i2c_outbyte (ch) == 0:
print "NACK received"
i2c_stop ()
continue

i2c_stop ()
i2c_start ()

if i2c_outbyte((i2 c_fixed<<4)|(i2 c_addr<<1)|1) == 0:
print "NACK received"
i2c_stop ()
continue

i2c_inbyte (0)
value = i2c_inbyte (1)
i2c_stop ()

print "CH%d = %.4fv (%.4f hex/n)" %
(ch,value*0.012 941,value)

ch += 1

if ch == 4:
break;

i2c_close ()
return 0
main()
tnx all

Jun 14 '07 #1
1 6585
luca <sa*******@gmai l.comwrote:
>
I was trying to make to work directly l i2c with python with the
calls ioctl. But I have of the problems and I do not succeed to go
ahead.

[root@axis-00408c000001 /usr/local/wrap]129# python pcf8591_ioctl.p y
Reading from 4 ch 8 bit A/D converter PCF8591
Traceback (most recent call last):
File "pcf8591_ioctl. py", line 269, in ?
main()
File "pcf8591_ioctl. py", line 229, in main
if i2c_open () < 0:
File "pcf8591_ioctl. py", line 168, in i2c_open
fcntl.ioctl(i2c _fd, _IO(ETRAXGPIO_I OCTYPE, IO_SETGET_OUTPU T), 0,
iomask)
TypeError: ioctl requires a file or file descriptor, an integer and
optionally a integer or buffer argument
The error message pretty much tells you the problem. One of the parameters
is not correct. Notice this:
>def IO_CFG_WRITE_MO DE_VALUE(msb, data_mask, clk_mask):
( (((msb)&1) << 16) | (((data_mask) &0xFF) << 8) | ((clk_mask) &
0xFF) )
def IO_CFG_WRITE_MO DE_VALUE(msb, data_mask, clk_mask):
( (((msb)&1) << 16) | (((data_mask) &0xFF) << 8) | ((clk_mask) &
0xFF) )
Key question: what do these functions return?

Answer: nothing. You compute a value and then throw it away. Thus:
print IO_CFG_WRITE_MO DE_VALUE( 15, 0xfff, 0x0f0 )
This will print "None". Add a "return" before the expression.

The same thing is true of all of these functions. You are thinking that a
Python "def" is like a C "#define". It's not. It's like a C function.
Nothing is returned unless you return it.
>def _IOC(dir,type,n r,size):
(((dir) << _IOC_DIRSHIFT) |
((type) << _IOC_TYPESHIFT) |
((nr) << _IOC_NRSHIFT) |
((size) << _IOC_SIZESHIFT) )

def _IO(type,nr):
_IOC(_IOC_NONE, (type),(nr),0)

def _IOR(type,nr,si ze):
_IOC(_IOC_READ, (type),(nr),siz eof(size))

def _IOW(type,nr,si ze):
_IOC(_IOC_WRITE ,(type),(nr),si zeof(size))

def _IOWR(type,nr,s ize):
_IOC(_IOC_READ| _IOC_WRITE,(typ e),(nr),sizeof( size))

def _IOC_DIR(nr):
(((nr) >_IOC_DIRSHIF T) & _IOC_DIRMASK)
def _IOC_TYPE(nr):
(((nr) >_IOC_TYPESHIFT ) & _IOC_TYPEMASK)
def _IOC_NR(nr):
(((nr) >_IOC_NRSHIFT ) & _IOC_NRMASK)
def _IOC_SIZE(nr):
(((nr) >_IOC_SIZESHIFT ) & _IOC_SIZEMASK)
def IOC_IN():
(_IOC_WRITE << _IOC_DIRSHIFT)

def IOC_OUT():
(_IOC_READ << _IOC_DIRSHIFT)
def IOC_INOUT():
((_IOC_WRITE|_I OC_READ) << _IOC_DIRSHIFT)
def IOCSIZE_MASK():
(_IOC_SIZEMASK << _IOC_SIZESHIFT)
def IOCSIZE_SHIFT() :
(_IOC_SIZESHIFT )
However, there are more problems with this script. Example:
>#Get the SDA line state
def i2c_getbit():
value=fcntl.ioc tl(i2c_fd, _IO(ETRAXGPIO_I OCTYPE, IO_READBITS))
if ((value&(I2C_DA TA_LINE))==0):
return 0
else:
return 1
This will produce a runtime error, because the name "i2c_fd" is not
defined. Notice this:
>#Open the GPIOB dev
def i2c_open():
i2c_fd = os.open("/dev/gpiog", os.O_RDWR)
iomask = I2C_CLOCK_LINE
fcntl.ioctl(i2c _fd, _IO(ETRAXGPIO_I OCTYPE, IO_SETGET_OUTPU T), iomask)
iomask = I2C_DATA_LINE
fcntl.ioctl(i2c _fd, _IO(ETRAXGPIO_I OCTYPE, IO_SETGET_INPUT ), iomask)
fcntl.ioctl(i2c _fd, _IO(ETRAXGPIO_I OCTYPE, IO_SETBITS),
I2C_DATA_LIN E)
i2c_dir_in()
i2c_clk(1)
return i2c_fd
The result of the open is bound in a LOCAL variable called "i2c_fd". As
soon as the function returns, that variable goes away. You do RETURN the
value, but you don't store it anywhere:
>#PCF8591 address scheme
#| 1 | 0 | 0 | 1 | A2 | A1 | A0 | R/W |
#| i2c_fixed | i2c_addr | 1/0 |
def main ():
i2c_fixed = 0x09
i2c_addr = 0x01

print "Reading from 4 ch 8 bit A/D converter PCF8591"

if i2c_open () < 0:
print "i2c open error"
return 1
You should probably make this:

i2c_fd = i2c_open()
if i2c_fd < 0:
print "i2c open error"
return 1

But, of course, that's not enough either; i2c_fd is a local variable in
THIS function as well. You should change ALL of the functions so that they
accept an fd as the first parameter, and then pass i2c_fd into the
functions here.

Alternatively, you could create a wrapper class to hold the fd and make the
function methods on that class.
while "azz":
i2c_start ()
I'm curious. What do you think "while" statement is doing? The answer is
that it is an infinite loop; the string "azz" will always be true, so the
while statement will always loop. I can't guess what you really wanted
here.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 15 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4279
by: Javier | last post by:
Hi all!! I'm trying to use Python to help in testing software for the Windows Platform. The sw we develop at the company I work for sets up quite a number of "new" device drives in Windows, and as part of the testing process I'd need to be able to "query" them about their state and suchlike. The "interface" for doing so involves opening "device files" (of the form \\.\Device\CDCrypto) and sending them ioctls to query their state.
12
6819
by: Jay | last post by:
ok, i thought for 2 seconds i might have created a Keylogger in python but i still have one major think stopping me... PYTHON. when i run the program i have python create a file named keylog2.log and it then logs all keys pressed/typed in the python IDE into that file. All i want to know now is how do i hide or background python so that it will log all the keys pressed outside of Python. feel free to play around with my program... but...
3
23473
by: chris | last post by:
Here is my code for the call to ioctl: for ( ; ; ) { if ( (buf = malloc(len)) == NULL) errQuit("malloc error while allocating ifconf buffer"); ifc.ifc_len = len; ifc.ifc_buf = buf; if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
2
1698
by: Chris Johnson | last post by:
I have need to determine the capabilities of an optical drive. Right now I'm using SDL to find the drives, and ioctl to get the capabilities. Unfortunately, based on the output that I'm getting, either my method of interpretation is bad or these drives are misrepresenting themselves (which they shouldn't be, as other programs can use the abilites they would here appear to be lacking). If anyone has experience with the...
1
4559
by: klappnase | last post by:
Hello, I am writing an app that records from the soundcard using ossaudiodev. In the OSS programmer's guide they recommend when reading data fragments from the soundcard to use the fragment size as it is requested by the driver. According to the programmer's guide the ioctl call to determine the requested fragment size is: int frag_size;
0
325
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 398 open ( +5) / 3334 closed (+19) / 3732 total (+24) Bugs : 904 open ( -4) / 6011 closed (+36) / 6915 total (+32) RFE : 222 open ( -1) / 231 closed ( +2) / 453 total ( +1) New / Reopened Patches ______________________
6
4885
by: Bart Van Loon | last post by:
Hi all, I'm looking for a portable (FreeBSD and Linux) way of getting typical ifconfig information into Python. Some research on the web brought me to Linux only solutions http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439094 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439093
3
9054
by: Ognjen Bezanov | last post by:
Hello, I am trying to control a CD-ROM drive using python. The code I use is shown below.
6
3016
by: Eran.Yasso | last post by:
Hi all, I need to write a GUI app which do the followings: 1. My app waits for events from other app(actually this is a network adapter driver) running in kernel. The driver writes to file and signal my app to read the file. 2. My app also need o communicate this the driver using IOCTL. Can i do it in C#? can C# wait for events signaled from app written in C\C++ app running in on kernel and sends messages to device using
0
8514
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8932
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8766
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.