LINUX IIC总线 MISC driver

来源:互联网 发布:淘宝客域名备案 编辑:程序博客网 时间:2024/06/03 22:04

1.IIC函数

   1.1读写一个字节

/** * i2c_smbus_write_byte_data - SMBus "write byte" protocol * @client: Handle to slave device * @command: Byte interpreted by slave * @value: Byte being written * * This executes the SMBus "write byte" protocol, returning negative errno * else zero on success. */s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,u8 value)NOTE:start+device addr(write) + command (register addr)+value(register value)/** * i2c_smbus_read_byte_data - SMBus "read byte" protocol * @client: Handle to slave device * @command: Byte interpreted by slave * * This executes the SMBus "read byte" protocol, returning negative errno * else a data byte received from the device. */s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)NOTE:start+device addr(write) + command (register addr)+repeat start+device addr(write)->返回command地址的value

 1.2读写多个字节

/* Returns the number of read bytes */s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,u8 length, u8 *values)/** * i2c_smbus_write_block_data - SMBus "block write" protocol * @client: Handle to slave device * @command: Byte interpreted by slave * @length: Size of data block; SMBus allows at most 32 bytes * @values: Byte array which will be written. * * This executes the SMBus "block write" protocol, returning negative errno * else zero on success. */s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command, u8 length, const u8 *values)





2.IIC MISC最小驱动:

#include <linux/kernel.h>#include <linux/init.h>#include <linux/module.h>#include <linux/i2c.h>#include <linux/err.h>#include <linux/slab.h>#include <linux/delay.h>#include <linux/of.h>struct myiic_data {structdevice *dev;struct  regmap *regmap;structmutex lock;/*myiic 设备数据*/int number;};struct i2c_client *ptr_myiic_client=NULL;/* sysfs callbacks -----> write*/static ssize_t set_number(struct device *dev,struct device_attribute *attr,const char *buf, size_t count){struct myiic_data *data = dev_get_drvdata(dev);unsigned long number;int err = kstrtoul(buf, 10, &number);printk("Func=%s,%d number=%d\n",__FUNCTION__,__LINE__,number);if (err == 0) {mutex_lock(&data->lock);data->number = number;mutex_unlock(&data->lock);return count;}return err;}/* sysfs callbacks -----> read*/static ssize_t show_number(struct device *dev,     struct device_attribute *attr, char *buf){struct myiic_data *data = dev_get_drvdata(dev);/*读数据*///dev_dat = i2c_smbus_read_byte_data(ptr_myiic_client,0xD0);return sprintf(buf, "%d\n",data->number);}/*S_IWUSR:write enableS_IRUGO:read enable*//*     文件名字    读写属性      读回调函数    写回调函数           */static DEVICE_ATTR(number, S_IWUSR|S_IRUGO, show_number, set_number);static struct attribute *myiic_attributes[] = {&dev_attr_number.attr,NULL};static const struct attribute_group myiic_attr_group = {.attrs = myiic_attributes,};static int myiic_i2c_probe(struct i2c_client *client,      const struct i2c_device_id *id){int retValue = 0;int err = 0;struct myiic_data *data;data = kzalloc(sizeof(struct myiic_data), GFP_KERNEL);if (!data) {err = -ENOMEM;goto exit;}ptr_myiic_client = client;dev_set_drvdata(&client->dev, data);data->dev = &client->dev;//data->regmap = regmap;mutex_init(&data->lock);retValue = i2c_smbus_read_byte_data(client,0xD0);printk("Func=%s,%d retValue=0x%x\n",__FUNCTION__,__LINE__,retValue);/* Register sysfs hooks */err = sysfs_create_group(&client->dev.kobj, &myiic_attr_group);printk("Func=%s,%d  Register sysfs hooks err=%d\n",__FUNCTION__,__LINE__,err);if (err)goto exit_free;printk("Func=%s,%d ,myiic init success!!\n",__FUNCTION__,__LINE__);return 0;exit_free:ptr_myiic_client = NULL;kfree(data);exit:return err;}static int myiic_i2c_remove(struct i2c_client *client){struct myiic_data *data = dev_get_drvdata(&client->dev);sysfs_remove_group(&data->dev->kobj, &myiic_attr_group);kfree(data);ptr_myiic_client = NULL;printk("Func=%s,%d ,myiic remove success!!\n",__FUNCTION__,__LINE__);return 0;}static const struct i2c_device_id myiic_id[] = {{  "xxxx", 0 },/*修改设备树ID*/{}};MODULE_DEVICE_TABLE(i2c, myiic_id);static struct i2c_driver myiic_i2c_driver = {.driver = {.owner= THIS_MODULE,.name= "xxxx",/*MISC name*/},.id_table= myiic_id,.probe= myiic_i2c_probe,.remove= myiic_i2c_remove,/*.detect= bme680_i2c_detect,.address_list= normal_i2c*/};module_i2c_driver(myiic_i2c_driver);MODULE_AUTHOR("crazyuav <crazyuav@xxx.com>");MODULE_DESCRIPTION("myiic I2C bus driver");MODULE_LICENSE("GPL");


原创粉丝点击