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

📄 i2c.c.svn-base

📁 u-boot for S3c2443 processor
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
	  txbd->status |= BD_I2C_TX_START;	if (flags & I2CF_STOP_COND)	  txbd->status |= BD_SC_LAST | BD_SC_WRAP;	/* Copy data to send into buffer */	PRINTD(("[I2C] copy data...\n"));	for(j = 0; j < size; i++, j++)	  txbd->addr[i] = dataout[j];	PRINTD(("[I2C] txbd: length=0x%04x status=0x%04x addr[0]=0x%02x addr[1]=0x%02x\n",		   txbd->length,		   txbd->status,		   txbd->addr[0],		   txbd->addr[1]));	/* advance state */	state->tx_buf += txbd->length;	state->tx_space -= txbd->length;	state->tx_idx++;	state->txbd = (void*)(txbd + 1);	return 0;}staticint i2c_receive(i2c_state_t *state,				unsigned char address,				unsigned char secondary_address,				unsigned int flags,				unsigned short size_to_expect,				unsigned char *datain){	volatile I2C_BD *rxbd, *txbd;	PRINTD(("[I2C] i2c_receive %02d %02d %02d\n", address, secondary_address, flags));	/* Expected to receive too much */	if (size_to_expect > I2C_RXTX_LEN)	  return I2CERR_MSG_TOO_LONG;	/* no more free bds */	if (state->tx_idx >= NUM_TX_BDS || state->rx_idx >= NUM_RX_BDS		 || state->tx_space < 2)	  return I2CERR_NO_BUFFERS;	rxbd = (I2C_BD *)state->rxbd;	txbd = (I2C_BD *)state->txbd;	PRINTD(("[I2C] rxbd = %08x\n", (int)rxbd));	PRINTD(("[I2C] txbd = %08x\n", (int)txbd));	txbd->addr = state->tx_buf;	/* set up TXBD for destination address */	if (flags & I2CF_ENABLE_SECONDARY)	{		txbd->length = 2;		txbd->addr[0] = address << 1;   /* Write data */		txbd->addr[1] = secondary_address;  /* Internal address */		txbd->status = BD_SC_READY;	}	else	{		txbd->length = 1 + size_to_expect;		txbd->addr[0] = (address << 1) | 0x01;		txbd->status = BD_SC_READY;		memset(&txbd->addr[1], 0, txbd->length);	}	/* set up rxbd for reception */	rxbd->status = BD_SC_EMPTY;	rxbd->length = size_to_expect;	rxbd->addr = datain;	txbd->status |= BD_I2C_TX_START;	if (flags & I2CF_STOP_COND)	{		txbd->status |= BD_SC_LAST | BD_SC_WRAP;		rxbd->status |= BD_SC_WRAP;	}	PRINTD(("[I2C] txbd: length=0x%04x status=0x%04x addr[0]=0x%02x addr[1]=0x%02x\n",		   txbd->length,		   txbd->status,		   txbd->addr[0],		   txbd->addr[1]));	PRINTD(("[I2C] rxbd: length=0x%04x status=0x%04x addr[0]=0x%02x addr[1]=0x%02x\n",		   rxbd->length,		   rxbd->status,		   rxbd->addr[0],		   rxbd->addr[1]));	/* advance state */	state->tx_buf += txbd->length;	state->tx_space -= txbd->length;	state->tx_idx++;	state->txbd = (void*)(txbd + 1);	state->rx_idx++;	state->rxbd = (void*)(rxbd + 1);	return 0;}staticint i2c_doio(i2c_state_t *state){	volatile immap_t *immap = (immap_t *)CFG_IMMR ;	volatile iic_t *iip;	volatile i2c8260_t *i2c	= (i2c8260_t *)&immap->im_i2c;	volatile I2C_BD *txbd, *rxbd;	int  n, i, b, rxcnt = 0, rxtimeo = 0, txcnt = 0, txtimeo = 0, rc = 0;	uint dpaddr;	PRINTD(("[I2C] i2c_doio\n"));	if (state->tx_idx <= 0 && state->rx_idx <= 0) {		PRINTD(("[I2C] No I/O is queued\n"));		return I2CERR_QUEUE_EMPTY;	}	dpaddr = *((unsigned short*)(&immap->im_dprambase[PROFF_I2C_BASE]));	iip = (iic_t *)&immap->im_dprambase[dpaddr];	iip->iic_rbptr = iip->iic_rbase;	iip->iic_tbptr = iip->iic_tbase;	/* Enable I2C */	PRINTD(("[I2C] Enabling I2C...\n"));	i2c->i2c_i2mod |= 0x01;	/* Begin transmission */	i2c->i2c_i2com |= 0x80;	/* Loop until transmit & receive completed */	if ((n = state->tx_idx) > 0) {		txbd = ((I2C_BD*)state->txbd) - n;		for (i = 0; i < n; i++) {			txtimeo += TOUT_LOOP * txbd->length;			txbd++;		}		txbd--; /* wait until last in list is done */		PRINTD(("[I2C] Transmitting...(txbd=0x%08lx)\n", (ulong)txbd));		udelay(START_DELAY_US);	/* give it time to start */		while((txbd->status & BD_SC_READY) && (++txcnt < txtimeo)) {			udelay(DELAY_US);			if (ctrlc())				return (-1);			__asm__ __volatile__ ("eieio");		}	}	if (txcnt < txtimeo && (n = state->rx_idx) > 0) {		rxbd = ((I2C_BD*)state->rxbd) - n;		for (i = 0; i < n; i++) {			rxtimeo += TOUT_LOOP * rxbd->length;			rxbd++;		}		rxbd--; /* wait until last in list is done */		PRINTD(("[I2C] Receiving...(rxbd=0x%08lx)\n", (ulong)rxbd));		udelay(START_DELAY_US);	/* give it time to start */		while((rxbd->status & BD_SC_EMPTY) && (++rxcnt < rxtimeo)) {			udelay(DELAY_US);			if (ctrlc())				return (-1);			__asm__ __volatile__ ("eieio");		}	}	/* Turn off I2C */	i2c->i2c_i2mod &= ~0x01;	if ((n = state->tx_idx) > 0) {		for (i = 0; i < n; i++) {			txbd = ((I2C_BD*)state->txbd) - (n - i);			if ((b = txbd->status & BD_I2C_TX_ERR) != 0) {				if (state->err_cb != NULL)					(*state->err_cb)(I2CECB_TX_ERR|b, i,						state->cb_data);				if (rc == 0)					rc = I2CERR_IO_ERROR;			}		}	}	if ((n = state->rx_idx) > 0) {		for (i = 0; i < n; i++) {			rxbd = ((I2C_BD*)state->rxbd) - (n - i);			if ((b = rxbd->status & BD_I2C_RX_ERR) != 0) {				if (state->err_cb != NULL)					(*state->err_cb)(I2CECB_RX_ERR|b, i,						state->cb_data);				if (rc == 0)					rc = I2CERR_IO_ERROR;			}		}	}	if ((txtimeo > 0 && txcnt >= txtimeo) || \	    (rxtimeo > 0 && rxcnt >= rxtimeo)) {		if (state->err_cb != NULL)			(*state->err_cb)(I2CECB_TIMEOUT, -1, state->cb_data);		if (rc == 0)			rc = I2CERR_TIMEOUT;	}	return (rc);}static voidi2c_probe_callback(int flags, int xnum, void *data){	/*	 * the only acceptable errors are a transmit NAK or a receive	 * overrun - tx NAK means the device does not exist, rx OV	 * means the device must have responded to the slave address	 * even though the transfer failed	 */	if (flags == (I2CECB_TX_ERR|I2CECB_TX_NAK))		*(int *)data |= 1;	if (flags == (I2CECB_RX_ERR|I2CECB_RX_OV))		*(int *)data |= 2;}inti2c_probe(uchar chip){	i2c_state_t state;	int rc, err_flag;	uchar buf[1];	i2c_newio(&state);	state.err_cb = i2c_probe_callback;	state.cb_data = (void *) &err_flag;	err_flag = 0;	rc = i2c_receive(&state, chip, 0, I2CF_START_COND|I2CF_STOP_COND, 1, buf);	if (rc != 0)		return (rc);	/* probe failed */	rc = i2c_doio(&state);	if (rc == 0)		return (0);	/* device exists - read succeeded */	if (rc == I2CERR_TIMEOUT)		return (-1);	/* device does not exist - timeout */	if (rc != I2CERR_IO_ERROR || err_flag == 0)		return (rc);	/* probe failed */	if (err_flag & 1)		return (-1);	/* device does not exist - had transmit NAK */	return (0);	/* device exists - had receive overrun */}inti2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len){	i2c_state_t state;	uchar xaddr[4];	int rc;	xaddr[0] = (addr >> 24) & 0xFF;	xaddr[1] = (addr >> 16) & 0xFF;	xaddr[2] = (addr >>  8) & 0xFF;	xaddr[3] =  addr        & 0xFF;#ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW	 /*	  * EEPROM chips that implement "address overflow" are ones	  * like Catalyst 24WC04/08/16 which has 9/10/11 bits of address	  * and the extra bits end up in the "chip address" bit slots.	  * This makes a 24WC08 (1Kbyte) chip look like four 256 byte	  * chips.	  *	  * Note that we consider the length of the address field to still	  * be one byte because the extra address bits are hidden in the	  * chip address.	  */	chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);#endif	i2c_newio(&state);	rc = i2c_send(&state, chip, 0, I2CF_START_COND, alen, &xaddr[4-alen]);	if (rc != 0) {		printf("i2c_read: i2c_send failed (%d)\n", rc);		return 1;	}	rc = i2c_receive(&state, chip, 0, I2CF_STOP_COND, len, buffer);	if (rc != 0) {		printf("i2c_read: i2c_receive failed (%d)\n", rc);		return 1;	}	rc = i2c_doio(&state);	if (rc != 0) {		printf("i2c_read: i2c_doio failed (%d)\n", rc);		return 1;	}	return 0;}inti2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len){	i2c_state_t state;	uchar xaddr[4];	int rc;	xaddr[0] = (addr >> 24) & 0xFF;	xaddr[1] = (addr >> 16) & 0xFF;	xaddr[2] = (addr >>  8) & 0xFF;	xaddr[3] =  addr        & 0xFF;#ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW	 /*	  * EEPROM chips that implement "address overflow" are ones	  * like Catalyst 24WC04/08/16 which has 9/10/11 bits of address	  * and the extra bits end up in the "chip address" bit slots.	  * This makes a 24WC08 (1Kbyte) chip look like four 256 byte	  * chips.	  *	  * Note that we consider the length of the address field to still	  * be one byte because the extra address bits are hidden in the	  * chip address.	  */	chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);#endif	i2c_newio(&state);	rc = i2c_send(&state, chip, 0, I2CF_START_COND, alen, &xaddr[4-alen]);	if (rc != 0) {		printf("i2c_write: first i2c_send failed (%d)\n", rc);		return 1;	}	rc = i2c_send(&state, 0, 0, I2CF_STOP_COND, len, buffer);	if (rc != 0) {		printf("i2c_write: second i2c_send failed (%d)\n", rc);		return 1;	}	rc = i2c_doio(&state);	if (rc != 0) {		printf("i2c_write: i2c_doio failed (%d)\n", rc);		return 1;	}	return 0;}uchari2c_reg_read(uchar chip, uchar reg){	char buf;	i2c_read(chip, reg, 1, &buf, 1);	return (buf);}voidi2c_reg_write(uchar chip, uchar reg, uchar val){	i2c_write(chip, reg, 1, &val, 1);}#endif	/* CONFIG_HARD_I2C */

⌨️ 快捷键说明

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