把之前的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)
{