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

📄 i2c.c

📁 linux内核源码
💻 C
📖 第 1 页 / 共 2 页
字号:
			ack = 1;		i2c_delay(CLOCK_HIGH_TIME/2);	}	/*	 * our clock is high now, make sure data is low	 * before we enable our output. If we keep data high	 * and enable output, we would generate a stop condition.	 */	i2c_data(I2C_DATA_LOW);	/*	 * end clock pulse	 */	i2c_enable();	i2c_dir_out();	i2c_clk(I2C_CLOCK_LOW);	i2c_delay(CLOCK_HIGH_TIME/4);	/*	 * enable output	 */	i2c_dir_out();	/*	 * remove ACK clock pulse	 */	i2c_data(I2C_DATA_HIGH);	i2c_delay(CLOCK_LOW_TIME/2);	return ack;}/*#---------------------------------------------------------------------------*#*# FUNCTION NAME: I2C::sendAck*#*# DESCRIPTION  : Send ACK on received data*#*#--------------------------------------------------------------------------*/voidi2c_sendack(void){	/*	 * enable output	 */	i2c_delay(CLOCK_LOW_TIME);	i2c_dir_out();	/*	 * set ack pulse high	 */	i2c_data(I2C_DATA_LOW);	/*	 * generate clock pulse	 */	i2c_delay(CLOCK_HIGH_TIME/6);	i2c_clk(I2C_CLOCK_HIGH);	i2c_delay(CLOCK_HIGH_TIME);	i2c_clk(I2C_CLOCK_LOW);	i2c_delay(CLOCK_LOW_TIME/6);	/*	 * reset data out	 */	i2c_data(I2C_DATA_HIGH);	i2c_delay(CLOCK_LOW_TIME);	i2c_dir_in();}/*#---------------------------------------------------------------------------*#*# FUNCTION NAME: i2c_sendnack*#*# DESCRIPTION  : Sends NACK on received data*#*#--------------------------------------------------------------------------*/voidi2c_sendnack(void){	/*	 * enable output	 */	i2c_delay(CLOCK_LOW_TIME);	i2c_dir_out();	/*	 * set data high	 */	i2c_data(I2C_DATA_HIGH);	/*	 * generate clock pulse	 */	i2c_delay(CLOCK_HIGH_TIME/6);	i2c_clk(I2C_CLOCK_HIGH);	i2c_delay(CLOCK_HIGH_TIME);	i2c_clk(I2C_CLOCK_LOW);	i2c_delay(CLOCK_LOW_TIME);	i2c_dir_in();}/*#---------------------------------------------------------------------------*#*# FUNCTION NAME: i2c_writereg*#*# DESCRIPTION  : Writes a value to an I2C device*#*#--------------------------------------------------------------------------*/inti2c_writereg(unsigned char theSlave, unsigned char theReg, 	     unsigned char theValue){	int error, cntr = 3;	unsigned long flags;	spin_lock(&i2c_lock);	do {		error = 0;		/*		 * we don't like to be interrupted		 */		local_irq_save(flags);		i2c_start();		/*		 * send slave address		 */		i2c_outbyte((theSlave & 0xfe));		/*		 * wait for ack		 */		if(!i2c_getack())			error = 1;		/*		 * now select register		 */		i2c_dir_out();		i2c_outbyte(theReg);		/*		 * now it's time to wait for ack		 */		if(!i2c_getack())			error |= 2;		/*		 * send register register data		 */		i2c_outbyte(theValue);		/*		 * now it's time to wait for ack		 */		if(!i2c_getack())			error |= 4;		/*		 * end byte stream		 */		i2c_stop();		/*		 * enable interrupt again		 */		local_irq_restore(flags);			} while(error && cntr--);	i2c_delay(CLOCK_LOW_TIME);	spin_unlock(&i2c_lock);	return -error;}/*#---------------------------------------------------------------------------*#*# FUNCTION NAME: i2c_readreg*#*# DESCRIPTION  : Reads a value from the decoder registers.*#*#--------------------------------------------------------------------------*/unsigned chari2c_readreg(unsigned char theSlave, unsigned char theReg){	unsigned char b = 0;	int error, cntr = 3;	unsigned long flags;	spin_lock(&i2c_lock);	do {		error = 0;		/*		 * we don't like to be interrupted		 */		local_irq_save(flags);		/*		 * generate start condition		 */		i2c_start();    		/*		 * send slave address		 */		i2c_outbyte((theSlave & 0xfe));		/*		 * wait for ack		 */		if(!i2c_getack())			error = 1;		/*		 * now select register		 */		i2c_dir_out();		i2c_outbyte(theReg);		/*		 * now it's time to wait for ack		 */		if(!i2c_getack())			error = 1;		/*		 * repeat start condition		 */		i2c_delay(CLOCK_LOW_TIME);		i2c_start();		/*		 * send slave address		 */		i2c_outbyte(theSlave | 0x01);		/*		 * wait for ack		 */		if(!i2c_getack())			error = 1;		/*		 * fetch register		 */		b = i2c_inbyte();		/*		 * last received byte needs to be nacked		 * instead of acked		 */		i2c_sendack();		/*		 * end sequence		 */		i2c_stop();		/*		 * enable interrupt again		 */		local_irq_restore(flags);			} while(error && cntr--);	spin_unlock(&i2c_lock);	return b;}static inti2c_open(struct inode *inode, struct file *filp){	return 0;}static inti2c_release(struct inode *inode, struct file *filp){	return 0;}/* Main device API. ioctl's to write or read to/from i2c registers. */static inti2c_ioctl(struct inode *inode, struct file *file,	  unsigned int cmd, unsigned long arg){	if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {		return -EINVAL;	}	switch (_IOC_NR(cmd)) {		case I2C_WRITEREG:			/* write to an i2c slave */			D(printk("i2cw %d %d %d\n", 				 I2C_ARGSLAVE(arg),				 I2C_ARGREG(arg),				 I2C_ARGVALUE(arg)));			return i2c_writereg(I2C_ARGSLAVE(arg),					    I2C_ARGREG(arg),					    I2C_ARGVALUE(arg));		case I2C_READREG:		{			unsigned char val;			/* read from an i2c slave */			D(printk("i2cr %d %d ", 				I2C_ARGSLAVE(arg),				I2C_ARGREG(arg)));			val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));			D(printk("= %d\n", val));			return val;		}					    		default:			return -EINVAL;	}		return 0;}static const struct file_operations i2c_fops = {	.owner    = THIS_MODULE,	.ioctl    = i2c_ioctl,	.open     = i2c_open,	.release  = i2c_release,};int __initi2c_init(void){	static int res = 0;	static int first = 1;	if (!first) {		return res;	}	/* Setup and enable the Port B I2C interface */#ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C	if ((res = cris_request_io_interface(if_i2c, "I2C"))) {		printk(KERN_CRIT "i2c_init: Failed to get IO interface\n");		return res;	}	*R_PORT_PB_I2C = port_pb_i2c_shadow |= 		IO_STATE(R_PORT_PB_I2C, i2c_en,  on) |		IO_FIELD(R_PORT_PB_I2C, i2c_d,   1)  |		IO_FIELD(R_PORT_PB_I2C, i2c_clk, 1)  |		IO_STATE(R_PORT_PB_I2C, i2c_oe_, enable);	port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir0);	port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir1);	*R_PORT_PB_DIR = (port_pb_dir_shadow |=			  IO_STATE(R_PORT_PB_DIR, dir0, input)  |			  IO_STATE(R_PORT_PB_DIR, dir1, output));#else        if ((res = cris_io_interface_allocate_pins(if_i2c,						   'b',                                                   CONFIG_ETRAX_I2C_DATA_PORT,						   CONFIG_ETRAX_I2C_DATA_PORT))) {		printk(KERN_WARNING "i2c_init: Failed to get IO pin for I2C data port\n");		return res;	} else if ((res = cris_io_interface_allocate_pins(if_i2c,							  'b',							  CONFIG_ETRAX_I2C_CLK_PORT,							  CONFIG_ETRAX_I2C_CLK_PORT))) {		cris_io_interface_free_pins(if_i2c,					    'b',					    CONFIG_ETRAX_I2C_DATA_PORT,					    CONFIG_ETRAX_I2C_DATA_PORT);		printk(KERN_WARNING "i2c_init: Failed to get IO pin for I2C clk port\n");	}#endif	return res;}static int __initi2c_register(void){	int res;	res = i2c_init();	if (res < 0)		return res;  	res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);	if(res < 0) {		printk(KERN_ERR "i2c: couldn't get a major number.\n");		return res;	}	printk(KERN_INFO "I2C driver v2.2, (c) 1999-2004 Axis Communications AB\n");		return 0;}/* this makes sure that i2c_register is called during boot */module_init(i2c_register);/****************** END OF FILE i2c.c ********************************/

⌨️ 快捷键说明

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