Linux i2c 读写程序
/*
This software uses a BSD license.
Copyright (c) 2010, Sean Cross / chumby industries
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
* Neither the name of Sean Cross / chumby industries nor the names
of its contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#define I2C_FILE_NAME "/dev/i2c-1"
#define USAGE_MESSAGE \
"Usage:\n" \
" %s r [addr] [register] " \
"to read value from [register]\n" \
" %s w [addr] [register] [value] " \
"to write a value [value] to register [register]\n" \
""
static int set_i2c_register(int file,
unsigned char addr,
unsigned char reg,
unsigned char value) {
unsigned char outbuf[2];
struct i2c_rdwr_ioctl_data packets;
struct i2c_msg messages[1];
messages[0].addr = addr;
messages[0].flags = 0;
messages[0].len = sizeof(outbuf);
messages[0].buf = outbuf;
/* The first byte indicates which register we'll write */
outbuf[0] = reg;
/*
* The second byte indicates the value to write. Note that for many
* devices, we can write multiple, sequential registers at once by
* simply making outbuf bigger.
*/
outbuf[1] = value;
/* Transfer the i2c packets to the kernel and verify it worked */
packets.msgs = messages;
packets.nmsgs = 1;
if(ioctl(file, I2C_RDWR, &packets) < 0) {
perror("Unable to send data");
return 1;
}
return 0;
}
static int get_i2c_register(int file,
unsigned char addr,
unsigned char reg,
unsigned char *val) {
unsigned char inbuf, outbuf;
struct i2c_rdwr_ioctl_data packets;
struct i2c_msg messages[2];
/*
* In order to read a register, we first do a "dummy write" by writing
* 0 bytes to the register we want to read from. This is similar to
* the packet in set_i2c_register, except it's 1 byte rather than 2.
*/
outbuf = reg;
messages[0].addr = addr;
messages[0].flags = 0;
messages[0].len = sizeof(outbuf);
messages[0].buf = &outbuf;
/* The data will get returned in this structure */
messages[1].addr = addr;
messages[1].flags = I2C_M_RD/* | I2C_M_NOSTART*/;
messages[1].len = sizeof(inbuf);
messages[1].buf = &inbuf;
/* Send the request to the kernel and get the result back */
packets.msgs = messages;
packets.nmsgs = 2;
if(ioctl(file, I2C_RDWR, &packets) < 0) {
perror("Unable to send data");
return 1;
}
*val = inbuf;
return 0;
}
int main(int argc, char **argv) {
int i2c_file;
// Open a connection to the I2C userspace control file.
if ((i2c_file = open(I2C_FILE_NAME, O_RDWR)) < 0) {
perror("Unable to open i2c control file");
exit(1);
}
if(argc > 3 && !strcmp(argv[1], "r")) {
int addr = strtol(argv[2], NULL, 0);
int reg = strtol(argv[3], NULL, 0);
unsigned char value;
if(get_i2c_register(i2c_file, addr, reg, &value)) {
printf("Unable to get register!\n");
}
else {
printf("Register %d: %d (%x)\n", reg, (int)value, (int)value);
}
}
else if(argc > 4 && !strcmp(argv[1], "w")) {
int addr = strtol(argv[2], NULL, 0);
int reg = strtol(argv[3], NULL, 0);
int value = strtol(argv[4], NULL, 0);
if(set_i2c_register(i2c_file, addr, reg, value)) {
printf("Unable to get register!\n");
}
else {
printf("Set register %x: %d (%x)\n", reg, value, value);
}
}
else {
fprintf(stderr, USAGE_MESSAGE, argv[0], argv[0]);
}
close(i2c_file);
return 0;
}
参考链接:http://blog.csdn.net/zfzf294990051/article/details/17322621
Linux i2c 读写程序的更多相关文章
- Linux下读写芯片的I2C寄存器
要想在Linux下读写芯片的I2C寄存器,一般需要在Linux编写一份该芯片的I2C驱动,关于Linux下如何编写I2C驱动,前一篇文章<手把手教你写Linux I2C设备驱动>已经做了初 ...
- linux i2c 设备节点读写
最近需要操作24C02,封装了一下函数方便以后操作. 参考链接: https://my.oschina.net/handawei/blog/68526 http://blog.csdn.net/one ...
- Linux+I2C总线分析(主要是probe的方式)
Linux I2C 总线浅析 ㈠ Overview Linux的I2C体系结构分为3个组成部分: ·I2C核心: I2C核心提供了I2C总线驱动和设备驱动的注册.注销方法,I2C通信方法(即“algo ...
- Linux I2C总线设备驱动模型分析(ov7740)
1. 框架1.1 硬件协议简介1.2 驱动框架1.3 bus-drv-dev模型及写程序a. 设备的4种构建方法a.1 定义一个i2c_board_info, 里面有:名字, 设备地址 然后i2c_r ...
- linux i2c驱动架构-dm368 i2c驱动分析
linux i2c驱动架构-dm368 i2c驱动分析 在阅读本文最好先熟悉一种i2c设备的驱动程序,并且浏览一下i2c-core.c以及芯片提供商的提供的i2c总线驱动(i2c-davinc ...
- Linux I2C设备驱动
i2c设备:ts.camera.audio.gsensor.e2prom I2C基本协议: 写:开始 -> 设备地址 -> 写标志 -> 应答 -> 内部地址 -> 应答 ...
- Linux I2C驱动分析(三)----i2c_dev驱动和应用层分析 【转】
本文转载自:http://blog.chinaunix.net/uid-21558711-id-3959287.html 分类: LINUX 原文地址:Linux I2C驱动分析(三)----i2c_ ...
- Linux I2C核心、总线和设备驱动
目录 更新记录 一.Linux I2C 体系结构 1.1 Linux I2C 体系结构的组成部分 1.2 内核源码文件 1.3 重要的数据结构 二.Linux I2C 核心 2.1 流程 2.2 主要 ...
- Linux I2C总线控制器驱动(S3C2440)
s3c2440的i2c控制器驱动(精简DIY),直接上代码,注释很详细: #include <linux/kernel.h> #include <linux/module.h> ...
随机推荐
- Golang入门教程(十二)安装注意事项
1.$GOPATH (1)go 命令依赖一个重要的环境变量:$GOPATH .注:这个不是Go安装目录 (2) (3) (4)git 安装 (5)包管理对应关系 (6)安装完之后bee 工具后,bee ...
- vertica系列:解锁table
Vertica 表发生死锁后, 通过下面3个查询即可解锁. --步骤1: 找到被锁表的 transaction_idselect transaction_id, t.* from v_monitor. ...
- C JAVA你可能不知道的那些编程细节
关于printf的格式化字符 %* *与其它占位符结合使用, *将首先被一个 int 变量值代替后再被格式化. 如 printf("%.*s.", 2, "Hello&q ...
- 数据结构Java实现01----线性表与顺序表
一.线性结构: 如果一个数据元素序列满足: (1)除第一个和最后一个数据元素外,每个数据元素只有一个前驱数据元素和一个后继数据元素: (2)第一个数据元素没有前驱数据元素: (3)最后一个数据元素没有 ...
- luogu 3538/bzoj 2795 Poi2008 哈希+质数结论
题意:给定一个子串,询问一些子区间内的最短循环节(循环节是越短条件约束越多) 开始一看那就哈希处理然后暴力枚举循环节,然后按照循环节长度暴力向后比较,本地测试40,洛谷60 #include<b ...
- rabbitMQ学习1:消息队列介绍与rabbitmq安装使用
1. 什么是消息队列 生活里的消息队列,如同邮局的邮箱, 如果没邮箱的话, 邮件必须找到邮件那个人,递给他,才玩完成,那这个任务会处理的很麻烦,很慢,效率很低 但是如果有了邮箱, 邮件直接丢给邮箱,用 ...
- Hadoop之HDFS思维导图
- Kotlin中,lateinit 和 lazy{} 的区别
1.lazy{} 只能用在val类型, lateinit 只能用在var类型 如 : val name: String by lazy { "sherlbon" }lateinit ...
- react-踩坑记录——页面底部多出一倍高度的空白
挂载slider组件后页面底部多出一倍高度的空白,如下: slider组件内容⬇️: class Slider extends Component{ constructor(){ super(); } ...
- 实验吧 smali文件分析
题目链接:http://www.shiyanbar.com/ctf/1871 好吧,这题真是基础中的基础,基础中的战斗机. 不光没有任何绕弯的地方,逻辑有且仅有一个.... 多说无益,使用工具jadx ...