/*
* smbusmodule.c - Python bindings for Linux SMBus access through i2c-dev
* Copyright (C) 2005-2007 Mark M. Hoffman <mhoffman@lightlink.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <Python.h>
#include "structmember.h"
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
/*
** These are required to build this module against Linux older than 2.6.23.
*/
#ifndef I2C_SMBUS_I2C_BLOCK_BROKEN
#undef I2C_SMBUS_I2C_BLOCK_DATA
#define I2C_SMBUS_I2C_BLOCK_BROKEN 6
#define I2C_SMBUS_I2C_BLOCK_DATA 8
#endif
PyDoc_STRVAR(SMBus_module_doc,
"This module defines an object type that allows SMBus transactions\n"
"on hosts running the Linux kernel. The host kernel must have I2C\n"
"support, I2C device interface support, and a bus adapter driver.\n"
"All of these can be either built-in to the kernel, or loaded from\n"
"modules.\n"
"\n"
"Because the I2C device interface is opened R/W, users of this\n"
"module usually must have root permissions.\n");
typedef struct {
PyObject_HEAD
int fd; /* open file descriptor: /dev/i2c-?, or -1 */
int addr; /* current client SMBus address */
int pec; /* !0 => Packet Error Codes enabled */
} SMBus;
static PyObject *
SMBus_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
SMBus *self;
if ((self = (SMBus *)type->tp_alloc(type, 0)) == NULL)
return NULL;
self->fd = -1;
self->addr = -1;
self->pec = 0;
return (PyObject *)self;
}
PyDoc_STRVAR(SMBus_close_doc,
"close()\n\n"
"Disconnects the object from the bus.\n");
static PyObject *
SMBus_close(SMBus *self)
{
if ((self->fd != -1) && (close(self->fd) == -1)) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
self->fd = -1;
self->addr = -1;
self->pec = 0;
Py_INCREF(Py_None);
return Py_None;
}
static void
SMBus_dealloc(SMBus *self)
{
PyObject *ref = SMBus_close(self);
Py_XDECREF(ref);
#if PY_MAJOR_VERSION >= 3
Py_TYPE(self)->tp_free((PyObject *)self);
#else
self->ob_type->tp_free((PyObject *)self);
#endif
}
#define MAXPATH 16
PyDoc_STRVAR(SMBus_open_doc,
"open(bus)\n\n"
"Connects the object to the specified SMBus.\n");
static PyObject *
SMBus_open(SMBus *self, PyObject *args, PyObject *kwds)
{
int bus;
char path[MAXPATH];
static char *kwlist[] = {"bus", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:open", kwlist, &bus))
return NULL;
if (snprintf(path, MAXPATH, "/dev/i2c-%d", bus) >= MAXPATH) {
PyErr_SetString(PyExc_OverflowError,
"Bus number is invalid.");
return NULL;
}
if ((self->fd = open(path, O_RDWR, 0)) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static int
SMBus_init(SMBus *self, PyObject *args, PyObject *kwds)
{
int bus = -1;
static char *kwlist[] = {"bus", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:__init__",
kwlist, &bus))
return -1;
if (bus >= 0) {
SMBus_open(self, args, kwds);
if (PyErr_Occurred())
return -1;
}
return 0;
}
/*
* private helper function, 0 => success, !0 => error
*/
static int
SMBus_set_addr(SMBus *self, int addr)
{
int ret = 0;
if (self->addr != addr) {
ret = ioctl(self->fd, I2C_SLAVE, addr);
self->addr = addr;
}
return ret;
}
#define SMBus_SET_ADDR(self, addr) do { \
if (SMBus_set_addr(self, addr)) { \
PyErr_SetFromErrno(PyExc_IOError); \
return NULL; \
} \
} while(0)
PyDoc_STRVAR(SMBus_write_quick_doc,
"write_quick(addr)\n\n"
"Perform SMBus Quick transaction.\n");
static PyObject *
SMBus_write_quick(SMBus *self, PyObject *args)
{
int addr;
__s32 result;
if (!PyArg_ParseTuple(args, "i:write_quick", &addr))
return NULL;
SMBus_SET_ADDR(self, addr);
if ((result = i2c_smbus_write_quick(self->fd, I2C_SMBUS_WRITE))) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
PyDoc_STRVAR(SMBus_read_byte_doc,
"read_byte(addr) -> result\n\n"
"Perform SMBus Read Byte transaction.\n");
static PyObject *
SMBus_read_byte(SMBus *self, PyObject *args)
{
int addr;
__s32 result;
if (!PyArg_ParseTuple(args, "i:read_byte", &addr))
return NULL;
SMBus_SET_ADDR(self, addr);
if ((result = i2c_smbus_read_byte(self->fd)) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
return Py_BuildValue("l", (long)result);
}
PyDoc_STRVAR(SMBus_write_byte_doc,
"write_byte(addr, val)\n\n"
"Perform SMBus Write Byte transaction.\n");
static PyObject *
SMBus_write_byte(SMBus *self, PyObject *args)
{
int addr, val;
__s32 result;
if (!PyArg_ParseTuple(args, "ii:write_byte", &addr, &val))
return NULL;
SMBus_SET_ADDR(self, addr);
if ((result = i2c_smbus_write_byte(self->fd, (__u8)val)) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
PyDoc_STRVAR(SMBus_read_byte_data_doc,
"read_byte_data(addr, cmd) -> result\n\n"
"Perform SMBus Read Byte Data transaction.\n");
static PyObject *
SMBus_read_byte_data(SMBus *self, PyObject *args)
{
int addr, cmd;
__s32 result;
if (!PyArg_ParseTuple(args, "ii:read_byte_data", &addr, &cmd))
return NULL;
SMBus_SET_ADDR(self, addr);
if ((result = i2c_smbus_read_byte_data(self->fd, (__u8)cmd)) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
return Py_BuildValue("l", (long)result);
}
PyDoc_STRVAR(SMBus_write_byte_data_doc,
"write_byte_data(addr, cmd, val)\n\n"
"Perform SMBus Write Byte Data transaction.\n");
static PyObject *
SMBus_write_byte_data(SMBus *self, PyObject *args)
{
int addr, cmd, val;
__s32 result;
if (!PyArg_ParseTuple(args, "iii:write_byte_data", &addr, &cmd, &val))
return NULL;
SMBus_SET_ADDR(self, addr);
if ((result = i2c_smbus_write_byte_data(self->fd,
(__u8)cmd, (__u8)val)) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
PyDoc_STRVAR(SMBus_read_word_data_doc,
"read_word_data(addr, cmd) -> result\n\n"
"Perform SMBus Read Word Data transaction.\n");
static PyObject *
SMBus_read_word_data(SMBus *self, PyObject *args)
{
int addr, cmd;
__s32 result;
if (!PyArg_ParseTuple(args, "ii:read_word_data", &addr, &cmd))
return NULL;
SMBus_SET_ADDR(self, addr);
if ((result = i2c_smbus_read_word_data(self->fd, (__u8)cmd)) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
return Py_BuildValue("l", (long)result);
}
PyDoc_STRVAR(SMBus_write_word_data_doc,
"write_word_data(addr, cmd, val)\n\n"
"Perform SMBus Write Word Data transaction.\n");
static PyObject *
SMBus_write_word_data(SMBus *self, PyObject *args)
{
int addr, cmd, val;
__s32 result;
if (!PyArg_ParseTuple(args, "iii:write_word_data", &addr, &cmd, &val))
return NULL;
SMBus_SET_ADDR(self, addr);
if ((result = i2c_smbus_write_word_data(self->fd,
(__u8)cmd, (__u16)val)) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
PyDoc_STRVAR(SMBus_process_call_doc,
"process_call(addr, cmd, val)\n\n"
"Perform SMBus Process Call transaction.\n");
static PyObject *
SMBus_process_call(SMBus *self, PyObject *args)
{
int addr, cmd, val;
__s32 result;
if (!PyArg_ParseTuple(args, "iii:process_call", &addr, &cmd, &val))
return NULL;
SMBus_SET_ADDR(self, addr);
if ((result = i2c_smbus_process_call(self->fd,
(__u8)cmd, (__u16)val)) == -1
没有合适的资源?快使用搜索试试~ 我知道了~
linux自带的I2C工具
共87个文件
c:13个
8:9个
o:8个
需积分: 41 23 下载量 79 浏览量
2018-11-29
09:17:33
上传
评论
收藏 2.63MB ZIP 举报
温馨提示
自己交叉编译并且成功在嵌入式板子上正常使用,希望对后面需要的朋友能有所帮助。
资源推荐
资源详情
资源评论
收起资源包目录
i2c-tools-4.0.zip (87个子文件)
i2c-tools-4.0
i2c-tools-4.0
COPYING.LGPL 26KB
make.sh 223B
COPYING 18KB
lib
libi2c.map 414B
libi2c.a 3KB
libi2c.so 7KB
smbus.o 2KB
smbus.c 6KB
smbus.ao 2KB
libi2c.so.0.1.0 7KB
libi2c.so.0 7KB
Module.mk 3KB
i2ctool
i2cdump 465KB
i2cset 464KB
i2cget 462KB
i2cdetect 463KB
i2ctransfer 462KB
eepromer
eepromer.c 13KB
README.eeprom 3KB
README.eepromer 893B
Makefile 157B
eepromer.8 2KB
.gitignore 18B
README 2KB
eeprom.8 2KB
eeprom.c 8KB
tools
i2cdump.8 4KB
i2cbusses.o 6KB
i2cdump 465KB
i2ctransfer.o 6KB
i2ctransfer.c 8KB
i2cset 464KB
i2cget.o 5KB
i2cdetect.8 3KB
i2ctransfer.8 5KB
i2cget.c 6KB
i2cdetect.o 6KB
util.o 1KB
i2cget 462KB
util.c 1KB
i2cdump.c 12KB
i2cdetect 463KB
Makefile 1KB
i2ctransfer 462KB
i2cset.8 5KB
i2cdetect.c 9KB
i2cbusses.h 1KB
util.h 910B
i2cset.c 11KB
i2cset.o 7KB
libi2c.so.0 12KB
i2cbusses.c 10KB
.gitignore 49B
Module.mk 3KB
i2cget.8 4KB
i2cdump.o 7KB
CHANGES 7KB
eeprom
ddcmon 13KB
decode-edid 5KB
decode-dimms 83KB
decode-vaio 6KB
decode-dimms.1 2KB
decode-vaio.1 1KB
Module.mk 1KB
README 806B
eeprog
README.eeprog 1KB
eeprog.8 2KB
eeprog.c 7KB
24cXX.c 5KB
.gitignore 8B
Module.mk 2KB
24cXX.h 2KB
stub
i2c-stub-from-dump.8 2KB
i2c-stub-from-dump 5KB
Module.mk 798B
Makefile 1KB
version.h 22B
.gitignore 43B
README 3KB
py-smbus
smbusmodule.c 17KB
setup.py 537B
Module.mk 690B
README 731B
include
i2c
smbus.h 2KB
linux
i2c-dev.h 2KB
Module.mk 783B
pax_global_header 52B
共 87 条
- 1
资源评论
TinusChen
- 粉丝: 118
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功