I2C 通信 api

把之前的oled从rtos移植到linux时,抽了个IIC通信api粗来 ⊙▽⊙

Header: i2c-api.h

//
//  i2c-api.h
//  i2c-api
//
//  Created by MetalSeed on 15/4/11.
//  Copyright (c) 2015年 MetalSeed. All rights reserved.
//

#ifndef i2c_api_i2c_api_h
#define i2c_api_i2c_api_h

#include <linux/i2c-dev.h> // include I2C_FUNC_SMBUS_
#include <linux/i2c.h>

#define IIC_TYPE_UNKNOWN    0
#define IIC_TPPE_OLED       1
#define IIC_TYPE_8BIT_ADDR  2
#define IIC_TYPE_16BIT_ADDR 3

struct i2c_obj
{
    char *dev;  // device file i.e. /dev/i2c-N
    int addr;   // i2c address
    int fd;     // file descriptor
    int type;   // device type
};

/*
 * opens the i2c device at [dev_fqn] (i.e. /dev/i2c-N) whose address is
 * [addr] and set the i2c_object [e]
 */
int i2c_open(char *dev_fqn, int addr, int type, struct i2c_obj*);
/*
 * closees the i2c device [e]
 */
int i2c_close(struct i2c_obj *e);
/*
 * read and returns the i2c byte at memory address [mem_addr]
 * Note: i2c must have been selected by ioctl(fd,I2C_SLAVE,address)
 */
int i2c_read_byte(struct i2c_obj* e, __u16 mem_addr);
/*
 * read the current byte
 * Note: i2c must have been selected by ioctl(fd,I2C_SLAVE,address)
 */
int i2c_read_current_byte(struct i2c_obj *e);
/*
 * writes [data] at memory address [mem_addr]
 * Note: i2c must have been selected by ioctl(fd,I2C_SLAVE,address)
 */
int i2c_write_byte(struct i2c_obj *e, __u16 mem_addr, __u8 data);

#endif

Source: i2c-api.c

//
//  main.c
//  i2c-api
//
//  Created by MetalSeed on 15/4/11.
//  Copyright (c) 2015年 MetalSeed. All rights reserved.
//

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/fs.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <assert.h>
#include <string.h>

#include "i2c-api.h"

//
//
// i2c smbus func
//
//

static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
                                     int size, union i2c_smbus_data *data)
{
    struct i2c_smbus_ioctl_data args;

    args.read_write = read_write;
    args.command = command;
    args.size = size;
    args.data = data;
    return ioctl(file,I2C_SMBUS,&args);
}

static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
{
    return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
}

static inline __s32 i2c_smbus_read_byte(int file)
{
    union i2c_smbus_data data;
    if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))
        return -1;
    else
        return 0x0FF & data.byte;
}

static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
{
    return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,
                            I2C_SMBUS_BYTE,NULL);
}

static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
{
    union i2c_smbus_data data;
    if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
                         I2C_SMBUS_BYTE_DATA,&data))
        return -1;
    else
        return 0x0FF & data.byte;
}

static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,
                                              __u8 value)
{
    union i2c_smbus_data data;
    data.byte = value;
    return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
                            I2C_SMBUS_BYTE_DATA, &data);
}

static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
{
    union i2c_smbus_data data;
    if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
                         I2C_SMBUS_WORD_DATA,&data))
        return -1;
    else
        return 0x0FFFF & data.word;
}

static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,
                                              __u16 value)
{
    union i2c_smbus_data data;
    data.word = value;
    return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
                            I2C_SMBUS_WORD_DATA, &data);
}

static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
{
    union i2c_smbus_data data;
    data.word = value;
    if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
                         I2C_SMBUS_PROC_CALL,&data))
        return -1;
    else
        return 0x0FFFF & data.word;
}

/* Returns the number of read bytes */
static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,
                                              __u8 *values)
{
    union i2c_smbus_data data;
    int i;
    if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
                         I2C_SMBUS_BLOCK_DATA,&data))
        return -1;
    else {
        for (i = 1; i <= data.block[0]; i++)
            values[i-1] = data.block[i];
        return data.block[0];
    }
}

static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,
                                               __u8 length, __u8 *values)
{
    union i2c_smbus_data data;
    int i;
    if (length > 32)
        length = 32;
    for (i = 1; i <= length; i++)
        data.block[i] = values[i-1];
    data.block[0] = length;
    return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
                            I2C_SMBUS_BLOCK_DATA, &data);
}

/* Returns the number of read bytes */
static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,
                                                  __u8 *values)
{
    union i2c_smbus_data data;
    int i;
    if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
                         I2C_SMBUS_I2C_BLOCK_DATA,&data))
        return -1;
    else {
        for (i = 1; i <= data.block[0]; i++)
            values[i-1] = data.block[i];
        return data.block[0];
    }
}

static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
                                                   __u8 length, __u8 *values)
{
    union i2c_smbus_data data;
    int i;
    if (length > 32)
        length = 32;
    for (i = 1; i <= length; i++)
        data.block[i] = values[i-1];
    data.block[0] = length;
    return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
                            I2C_SMBUS_I2C_BLOCK_DATA, &data);
}

/* Returns the number of read bytes */
static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,
                                                 __u8 length, __u8 *values)
{
    union i2c_smbus_data data;
    int i;
    if (length > 32)
        length = 32;
    for (i = 1; i <= length; i++)
        data.block[i] = values[i-1];
    data.block[0] = length;
    if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
                         I2C_SMBUS_BLOCK_PROC_CALL,&data))
        return -1;
    else {
        for (i = 1; i <= data.block[0]; i++)
            values[i-1] = data.block[i];
        return data.block[0];
    }
}

static int i2c_write_1b(struct i2c_obj *e, __u8 buf)
{
    int r;
    // we must simulate a plain I2C byte write with SMBus functions
    r = i2c_smbus_write_byte(e->fd, buf);
    if(r < 0)
        fprintf(stderr, "Error i2c_write_1b: %s\n", strerror(errno));
    usleep(10);
    return r;
}

static int i2c_write_2b(struct i2c_obj *e, __u8 buf[2])
{
    int r;
    // we must simulate a plain I2C byte write with SMBus functions
    r = i2c_smbus_write_byte_data(e->fd, buf[0], buf[1]);
    if(r < 0)
        fprintf(stderr, "Error i2c_write_2b: %s\n", strerror(errno));
    usleep(10);
    return r;
}

static int i2c_write_3b(struct i2c_obj *e, __u8 buf[3])
{
    int r;
    // we must simulate a plain I2C byte write with SMBus functions
    // the __u16 data field will be byte swapped by the SMBus protocol
    r = i2c_smbus_write_word_data(e->fd, buf[0], buf[2] << 8 | buf[1]);
    if(r < 0)
        fprintf(stderr, "Error i2c_write_3b: %s\n", strerror(errno));
    usleep(10);
    return r;
}

//
//
// i2c api
//
//
#define CHECK_I2C_FUNC( var, label ) \
        do {    if(0 == (var & label)) { fprintf(stderr, "\nError: " #label " function is required. Program halted.\n\n"); exit(1); }   } while(0);

/*
 * opens the i2c device at [dev_fqn] (i.e. /dev/i2c-N) whose address is
 * [addr] and set the i2c_object [e]
 */
int i2c_open(char *dev_fqn, int addr, int type, struct i2c_obj* e)
{
    int funcs, fd, r;
    e->fd = e->addr = 0;
    e->dev = 0;

    fd = open(dev_fqn, O_RDWR);
    if(fd <= 0)
    {
        fprintf(stderr, "Error i2c_obj_open: %s\n", strerror(errno));
        return -1;
    }

    // get funcs list
    if((r = ioctl(fd, I2C_FUNCS, &funcs) < 0))
    {
        fprintf(stderr, "Error i2c_obj_open: %s\n", strerror(errno));
        return -1;
    }

    // check for req funcs
    CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE );
    CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE );
    CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA );
    CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE_DATA );
    CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_WORD_DATA );
    CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_WORD_DATA );

    // set working device
    if( ( r = ioctl(fd, I2C_SLAVE, addr)) < 0)
    {
        fprintf(stderr, "Error i2c_obj_open: %s\n", strerror(errno));
        return -1;
    }
    e->fd = fd;
    e->addr = addr;
    e->dev = dev_fqn;
    e->type = type;
    return 0;
}

/*
 * closees the i2c device [e]
 */
int i2c_close(struct i2c_obj *e);
{
    close(e->fd);
    e->fd = -1;
    e->dev = 0;
    e->type = IIC_TYPE_UNKNOWN;
    return 0;
}

/*
 * read and returns the i2c byte at memory address [mem_addr]
 * Note: i2c must have been selected by ioctl(fd,I2C_SLAVE,address)
 */
int i2c_read_byte(struct i2c_obj* e, __u16 mem_addr);
{
    int r;
    ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer
    if(e->type == IIC_TYPE_8BIT_ADDR)
    {
        __u8 buf =  mem_addr & 0x0ff;
        r = i2c_write_1b(e, buf);
    } else if(e->type == IIC_TYPE_16BIT_ADDR) {
        __u8 buf[2] = { (mem_addr >> 8) & 0x0ff, mem_addr & 0x0ff };
        r = i2c_write_2b(e, buf);
    } else {
        fprintf(stderr, "ERR: unknown i2c_obj type\n");
        return -1;
    }
    if (r < 0)
        return r;
    r = i2c_smbus_read_byte(e->fd);
    return r;
}

/*
 * read the current byte
 * Note: i2c must have been selected by ioctl(fd,I2C_SLAVE,address)
 */
int i2c_read_current_byte(struct i2c_obj *e)
{
    ioctl(e->fd, BLKFLSBUF); // clear kernel read buffer
    return i2c_smbus_read_byte(e->fd);
}

/*
 * writes [data] at memory address [mem_addr]
 * Note: i2c must have been selected by ioctl(fd,I2C_SLAVE,address)
 */
int i2c_write_byte(struct i2c_obj *e, __u16 mem_addr, __u8 data)
{
    if(e->type == IIC_TYPE_8BIT_ADDR) {
        __u8 buf[2] = { mem_addr & 0x00ff, data };
        return i2c_write_2b(e, buf);
    } else if(e->type == IIC_TYPE_16BIT_ADDR) {
        __u8 buf[3] =
        { (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };
        return i2c_write_3b(e, buf);
    }
    fprintf(stderr, "ERR: unknown i2c_obj type\n");
    return -1;
}

细节不完善的地方,日后有空的时候再补充。 >.<

时间: 2024-08-11 09:47:29

I2C 通信 api的相关文章

【转载】GPIO模拟i2c通信

I2C总线的通信过程(见图4-8)主要包含三个主要阶段:起始阶段.数据传输阶段和终止阶段. 1. 起始阶段 在I2C总线不工作的情况下,SDA(数据线)和SCL(时钟线)上的信号均为高电平.如果此时主机需要发起新的通信请求,那么需要首先通过SDA和SCL发出起始标志.当SCL为高电平时,SDA电平从高变低,这一变化表示完成了通信的起始条件. 在起始条件和数据通信之间,通常会有延时要求,具体的指标会在设备厂商的规格说明书中给出. 2. 数据传输阶段 I2C总线的数据通信是以字节(8位)作为基本单位

嵌入式Linux裸机开发(十三)——I2C通信

嵌入式Linux裸机开发(十三)--I2C通信 一.IIC协议 1.IIC总线简介 I2C(Inter-Integrated Circuit)总线是一种由PHILIPS公司开发的两线式串行总线,用于连接微控制器及其外围设备.I2C总线是一种串行数据总线,只有二根信号线,一根是双向的数据线SDA,另一根是时钟线SCL.在 I2C总线上传送的一个数据字节由八位组成.总线对每次传送的字节数没有限制,但每个字节后必须跟一位应答位. IIC总线是一种串行总线,用于连接微控制器及其外围设备,具有以下特点:

I2C通信详解

什么是I2C通信 物理接口:SCL + SDA (1)SCL(serial clock):时钟线,传输CLK信号,一般是I2C主设备向从设备提供时钟的通道 (2)SDA(serial data):数据线,I2C通信的通信数据都通过SDA线来传输 通信特征:串行.同步.非差分.低速率 (1)I2C属于串行通信,所有的数据以位为单位在SDA上串行传输 (2)同步通信就是通信双方工作在同一个时钟下面,一般是通信的A方通过一根CLK线传输A自己的时钟给B,B工作在A传输的时钟下.所以通信的显著特征就是:

由于用mpu6050模块,所以要用上i2c通信原理。

i2c通信原理 i2c总线只有两根双向信号线,一根是数据线SDA,一根是时钟线SCL. 每个接到i2c总线上的器件都有唯一的地址,主机与其他器件之间的数据传送可以是由主机发送给其他器件.主机为发送器,从机为接收器.在80c51单片机系统中,通常80C51单片机为主机, 1:数据位的有效规定: 1.1:总线在进行数据传送时,时钟信号为高电平期间,数据线上的数据必须保持稳定,时钟线为低电平期间时,数据线上的高电平和低电平才能允许变化. 1.2:起始信号和终止信号都是由主机发出的,起始信号产生后,总线

I2C通信

项目之前研究了I2C通信协议的实现,完成FPGA对视频解码芯片SAA7111A的初始化配置,设计实现了I2C主机对从机(SAA7111A)32个寄存器的写操作,因此只简单实现了I2C的写时序. 这次重新梳理学习了I2C协议,借助黑金开发板设计I2C主机控制器完成对EEPROM(24LC02)的读写操作,设计单字节的写时序和随机读时序.通过按键将数据先入EEPROM,再通过按键选择将数据显示在数码管上进行验证. 1. 时序介绍 主要的时序如下所示: 数据线SDA在空闲状态时为高电平,在SCL高电平

[HTML5_Web Workers+Sockets]HTML5 通信API 跨域门槛将不再高、数据推送也不再是梦

前言 HTML5新增通信相关两个API,跨文档消息传输与WEB Sockets API, 跨文档消息传输功能,可以在不同网页文档,不同端口(跨域情况下)进行消息传递. 使用web sockets api 可以让客户端与服务器端通过socket端口传递数据,这样便可以使用数据推送技术. 跨文档消息传输 在之前我们若想跨域获取信息会花很多功夫,现在只要获取网页所在窗口对象实例变可以实现互相通信. 首先要想从其他窗口接受发过来的消息需要对其窗口对象进行监听: window.addevntListene

树莓派学习之I2C通信

最近飞兆杯的比赛选择了循迹小车,其中用到的ldc1314芯片是基于I2c通信的,虽然项目并没有成功,但是还是学习了树莓派的I2c通信相关的一些知识! 一.配置I2C接口,读取i2c设备的地址 1.可以通过raspi-config配置工具配置使能I2C,或者通过编辑/boot/config.txt文件,加入一下一行内容: dtparam=i2c1=ondtparam=i2c_arm=on 2.安装I2C库和工具 $sudo apt-get install i2c-tools 3.当连接上I2C设备

I2C通信基本原理及其实现

I2C是一种总线式结构,它只需要SCL时钟信号线与SDA数据线,两根线就能将连接与总线上的设备实现数据通信,由于它的简便的构造设计,于是成为一种较为常用的通信方式. 由于I2C采用的是主从式通信方式,所以,通信的过程完全由主设备仲裁.在通信之前,必须由主设备发送一个起始信号,决定数据是否可以开始传送,并且在结束通信时,必须再由主设备发送一个结束信号,以表示通信已经结束. 因为,通信之前,主设备需要发送一个起始信号,所以,先讲一下起始信号.通过上面的图就可以知道(上图中的第一个波形图是SDA数据线

Windows 多进程通信API总结

在一个大型的应用系统中,往往需要多个进程相互协作,进程间通信(IPC,Inter Process Communication)就显得比较重要了.在Linux系统中,有很多种IPC机制,比如说,信号(signal).管道(pipe).消息队列(message queue).信号量(semaphore)和共享内存(shared memory).套接字(socket)等,其实Windows操作系统也支持这些东西.在IBM的Developerworks发现了一篇关于Windows与Linux 之间IPC