⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_iic.c

📁 这是一个linux上的iic设备驱动的原型,是自己用于linux i2c驱动练习用的,希望对大家有帮助
💻 C
字号:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/version.h>
#include <linux/init.h>

#include <linux/i2c.h>
#include <linux/videodev.h>

#include <linux/i2c-algo-s3c2440.h>
#include <linux/i2c-s3c2440.h>

static int debug;	/* insmod parameter */
static int this_adap;


static struct i2c_client client_template;

#define TLV320AIC32_IIC_ADDR	0x18

/* Addresses to scan */
static unsigned short normal_i2c[] = {I2C_CLIENT_END};
static unsigned short normal_i2c_range[] = {TLV320AIC32_IIC_ADDR, TLV320AIC32_IIC_ADDR+1, I2C_CLIENT_END};
static unsigned short probe[2]        = { I2C_CLIENT_END, I2C_CLIENT_END };
static unsigned short probe_range[2]  = { I2C_CLIENT_END, I2C_CLIENT_END };
static unsigned short ignore[2]       = { I2C_CLIENT_END, I2C_CLIENT_END };
static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
static unsigned short force[2]        = { I2C_CLIENT_END, I2C_CLIENT_END };

static struct i2c_client_address_data addr_data = {
	normal_i2c, normal_i2c_range, 
	probe, probe_range, 
	ignore, ignore_range, 
	force
};





/* ---------------------------------------------------------------------- */

static int 
test_attach(struct i2c_adapter *adap, int addr,
	     unsigned short flags, int kind)
{

	static unsigned char send_buffer[5];
	static unsigned char recv_buffer[5];
	
	struct i2c_client *client;
	int i,send_ret,recv_ret;
	if (this_adap > 0)
		return -1;
	this_adap++;
	
        client_template.adapter = adap;
        client_template.addr = addr;

	client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
        if (client == NULL)
                return -ENOMEM;
        memcpy(client, &client_template, sizeof(struct i2c_client));

	printk("test: SAB3036 found, addr:%x \n",addr);

        i2c_attach_client(client);
	//MOD_INC_USE_COUNT;

	send_buffer[0]=0x00;//reg addr
	send_buffer[1]=0x00;//write value
	i2c_master_send(client, send_buffer, 2);

                send_buffer[0]=0x00;//reg addr
                send_ret=i2c_master_send(client, send_buffer,1);
                recv_ret=i2c_master_recv(client, recv_buffer,1);
                printk("test: reg read value: %x  send_ret:%d,recv_ret:%d\n",recv_buffer[0],send_ret,recv_ret);


	for(i=0;i<50;i++){
		send_buffer[0]=i;//reg addr
		send_ret=i2c_master_send(client, send_buffer,1);
		recv_ret=i2c_master_recv(client, recv_buffer,1);
                printk("test: reg addr : %d ,reg read value: %x  send_ret:%d,recv_ret:%d\n",i,recv_buffer[0],send_ret,recv_ret);
	}


	return 0;
}

static int 
test_detach(struct i2c_client *c)
{
	//MOD_DEC_USE_COUNT;
	return 0;
}

static int 
test_command(struct i2c_client *client, unsigned int cmd, void *arg)
{
	return 0;
}

static int 
test_probe(struct i2c_adapter *adap)
{
	this_adap = 0;
	printk("probing~~~\n");
	if (adap->id == (I2C_ALGO_S3C2440 ){
		
		printk("probing222~~~\n");
		return i2c_probe(adap, &addr_data, test_attach);
	
	}
	return 0;
}

/* ----------------------------------------------------------------------- */

static struct i2c_driver 
i2c_driver_test = 
{
	"sab3036",		/* name       */
	I2C_DRIVERID_SAB3036,	/* ID         */
        I2C_DF_NOTIFY,
	test_probe,
	test_detach,
	test_command
};

static struct i2c_client client_template =
{
        "TEST_IIC",		/* name       */
        -1,
        0,
        0,
        NULL,
        &i2c_driver_test
};

EXPORT_NO_SYMBOLS;

int __init
test_iic_init(void)
{
	i2c_add_driver(&i2c_driver_test);
	return 0;
}

void __exit
test_iic_exit(void)
{
	i2c_del_driver(&i2c_driver_test);
}

MODULE_DESCRIPTION("SAB3036 test driver");
MODULE_AUTHOR("Philip Blundell <philb@gnu.org>");
MODULE_LICENSE("GPL");

MODULE_PARM(debug,"i");
MODULE_PARM_DESC(debug,"Enable debugging output");

module_init(test_iic_init);
module_exit(test_iic_exit);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -