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

📄 xiic_l.c

📁 FPGA 并行NOR FLash的操作相关
💻 C
📖 第 1 页 / 共 3 页
字号:
/* $Id: xiic_l.c,v 1.3 2007/12/17 19:15:38 meinelte Exp $ *//********************************************************************************       XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"*       AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND*       SOLUTIONS FOR XILINX DEVICES.  BY PROVIDING THIS DESIGN, CODE,*       OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE,*       APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION*       THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,*       AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE*       FOR YOUR IMPLEMENTATION.  XILINX EXPRESSLY DISCLAIMS ANY*       WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE*       IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR*       REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF*       INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS*       FOR A PARTICULAR PURPOSE.**       (c) Copyright 2002-2007 Xilinx Inc.*       All rights reserved.*******************************************************************************//*****************************************************************************//**** @file xiic_l.c** This file contains low-level driver functions that can be used to access the* device in normal and dynamic controller mode. The user should refer to the* hardware device specification for more details of the device operation.** <pre>* MODIFICATION HISTORY:** Ver   Who  Date     Changes* ----- --- -------   -----------------------------------------------* 1.01b jhl 05/13/02  First release* 1.01b jhl 10/14/02  Corrected bug in the receive function, the setup of the*                     interrupt status mask was not being done in the loop such*                     that a read would sometimes fail on the last byte because*                     the transmit error which should have been ignored was*                     being used.  This would leave an extra byte in the FIFO*                     and the bus throttled such that the next operation would*                     also fail.  Also updated the receive function to not*                     disable the device after the last byte until after the*                     bus transitions to not busy which is more consistent*                     with the expected behavior.* 1.01c ecm  12/05/02 new rev* 1.02a mta  03/09/06 Implemented Repeated Start in the Low Level Driver.* 1.03a mta  04/04/06 Implemented Dynamic IIC core routines.* 1.03a ecm  06/15/06 Fixed the hang in low_level_eeprom_test with -O0*                     Added polling loops for BNB to allow the slave to*                     respond correctly. Also added polling loop prior*                     to reset in _Recv.* 1.13a wgr  03/22/07 Converted to new coding style.* 1.13b ecm  11/29/07 added BB polling loops to the DynSend and DynRecv*					  routines to handle the race condition with BNB in IISR.* </pre>*****************************************************************************//***************************** Include Files *******************************/#include "xbasic_types.h"#include "xio.h"#include "xiic_l.h"/************************** Constant Definitions ***************************//**************************** Type Definitions *****************************//***************** Macros (Inline Functions) Definitions *******************//************************** Function Prototypes ****************************/static unsigned RecvData(u32 BaseAddress, u8 *BufferPtr,			 unsigned ByteCount, u8 Option);static unsigned SendData(u32 BaseAddress, u8 *BufferPtr,			 unsigned ByteCount, u8 Option);static unsigned DynRecvData(u32 BaseAddress, u8 *BufferPtr, u8 ByteCount);static unsigned DynSendData(u32 BaseAddress, u8 *BufferPtr,			    u8 ByteCount, u8 Option);/************************** Variable Definitions **************************//****************************************************************************//*** Receive data as a master on the IIC bus.  This function receives the data* using polled I/O and blocks until the data has been received. It only* supports 7 bit addressing mode of operation. The user is responsible for* ensuring the bus is not busy if multiple masters are present on the bus.** @param    BaseAddress contains the base address of the IIC device.* @param    Address contains the 7 bit IIC address of the device to send the*           specified data to.* @param    BufferPtr points to the data to be sent.* @param    ByteCount is the number of bytes to be sent.* @param    Option indicates whether to hold or free the bus after reception*           of data, XIIC_STOP = end with STOP condition, XIIC_REPEATED_START*           = don't end with STOP condition.** @return** The number of bytes received.** @note** None*******************************************************************************/unsigned XIic_Recv(u32 BaseAddress, u8 Address,		   u8 *BufferPtr, unsigned ByteCount, u8 Option){	u8 CntlReg;	unsigned RemainingByteCount;	volatile u8 StatusReg;	/* Tx error is enabled incase the address (7 or 10) has no device to answer	 * with Ack. When only one byte of data, must set NO ACK before address goes	 * out therefore Tx error must not be enabled as it will go off immediately	 * and the Rx full interrupt will be checked.  If full, then the one byte	 * was received and the Tx error will be disabled without sending an error	 * callback msg.	 */	XIic_mClearIisr(BaseAddress,			XIIC_INTR_RX_FULL_MASK | XIIC_INTR_TX_ERROR_MASK |			XIIC_INTR_ARB_LOST_MASK);	/* Set receive FIFO occupancy depth for 1 byte (zero based)	 */	XIo_Out8(BaseAddress + XIIC_RFD_REG_OFFSET, 0);	/* Check to see if already Master on the Bus.	 * If Repeated Start bit is not set send Start bit by setting MSMS bit else	 * Send the address.	 */	CntlReg = XIo_In8(BaseAddress + XIIC_CR_REG_OFFSET);	if ((CntlReg & XIIC_CR_REPEATED_START_MASK) == 0) {		/* 7 bit slave address, send the address for a read operation		 * and set the state to indicate the address has been sent		 */		XIic_mSend7BitAddress(BaseAddress, Address,				      XIIC_READ_OPERATION);		/* MSMS gets set after putting data in FIFO. Start the master receive		 * operation by setting CR Bits MSMS to Master, if the buffer is only one		 * byte, then it should not be acknowledged to indicate the end of data		 */		CntlReg = XIIC_CR_MSMS_MASK | XIIC_CR_ENABLE_DEVICE_MASK;		if (ByteCount == 1) {			CntlReg |= XIIC_CR_NO_ACK_MASK;		}		/* Write out the control register to start receiving data and call the		 * function to receive each byte into the buffer		 */		XIo_Out8(BaseAddress + XIIC_CR_REG_OFFSET, CntlReg);		/* Clear the latched interrupt status for the bus not busy bit which must		 * be done while the bus is busy		 */		StatusReg = XIo_In8(BaseAddress + XIIC_SR_REG_OFFSET);		while ((StatusReg & XIIC_SR_BUS_BUSY_MASK) == 0) {			StatusReg = XIo_In8(BaseAddress + XIIC_SR_REG_OFFSET);		}		XIic_mClearIisr(BaseAddress, XIIC_INTR_BNB_MASK);	}	else {		/* Already owns the Bus indicating that its a Repeated Start call.		 * 7 bit slave address, send the address for a read operation		 * and set the state to indicate the address has been sent		 */		XIic_mSend7BitAddress(BaseAddress, Address,				      XIIC_READ_OPERATION);	}	/* Try to receive the data from the IIC bus */	RemainingByteCount =		RecvData(BaseAddress, BufferPtr, ByteCount, Option);	CntlReg = XIo_In8(BaseAddress + XIIC_CR_REG_OFFSET);	if ((CntlReg & XIIC_CR_REPEATED_START_MASK) == 0) {		/* The receive is complete, disable the IIC device if the Option is		 * to release the Bus after Reception of data and return the number of		 * bytes that was received		 */		XIo_Out8(BaseAddress + XIIC_CR_REG_OFFSET, 0);	}	/* Return the number of bytes that was received */	return ByteCount - RemainingByteCount;}/******************************************************************************** Receive the specified data from the device that has been previously addressed* on the IIC bus.  This function assumes that the 7 bit address has been sent* and it should wait for the transmit of the address to complete.** @param    BaseAddress contains the base address of the IIC device.* @param    BufferPtr points to the buffer to hold the data that is received.* @param    ByteCount is the number of bytes to be received.* @param    Option indicates whether to hold or free the bus after reception*           of data, XIIC_STOP = end with STOP condition, XIIC_REPEATED_START*           = don't end with STOP condition.** @return** The number of bytes remaining to be received.** @note** This function does not take advantage of the receive FIFO because it is* designed for minimal code space and complexity.  It contains loops that* that could cause the function not to return if the hardware is not working.** This function assumes that the calling function will disable the IIC device* after this function returns.*******************************************************************************/static unsigned RecvData(u32 BaseAddress, u8 *BufferPtr,			 unsigned ByteCount, u8 Option){	u8 CntlReg;	u32 IntrStatusMask;	u32 IntrStatus;	/* Attempt to receive the specified number of bytes on the IIC bus */	while (ByteCount > 0) {		/* Setup the mask to use for checking errors because when receiving one		 * byte OR the last byte of a multibyte message an error naturally		 * occurs when the no ack is done to tell the slave the last byte		 */		if (ByteCount == 1) {			IntrStatusMask =				XIIC_INTR_ARB_LOST_MASK | XIIC_INTR_BNB_MASK;		}		else {			IntrStatusMask =				XIIC_INTR_ARB_LOST_MASK |				XIIC_INTR_TX_ERROR_MASK | XIIC_INTR_BNB_MASK;		}		/* Wait for the previous transmit and the 1st receive to complete		 * by checking the interrupt status register of the IPIF		 */		while (1) {			IntrStatus = XIIC_READ_IISR(BaseAddress);			if (IntrStatus & XIIC_INTR_RX_FULL_MASK) {				break;			}			/* Check the transmit error after the receive full because when			 * sending only one byte transmit error will occur because of the			 * no ack to indicate the end of the data			 */			if (IntrStatus & IntrStatusMask) {				return ByteCount;			}		}		CntlReg = XIo_In8(BaseAddress + XIIC_CR_REG_OFFSET);		/* Special conditions exist for the last two bytes so check for them		 * Note that the control register must be setup for these conditions		 * before the data byte which was already received is read from the		 * receive FIFO (while the bus is throttled		 */		if (ByteCount == 1) {			if (Option == XIIC_STOP) {				/* If the Option is to release the bus after the last data				 * byte, it has already been read and no ack has been done, so				 * clear MSMS while leaving the device enabled so it can get off				 * the IIC bus appropriately with a stop.				 */				XIo_Out8(BaseAddress + XIIC_CR_REG_OFFSET,					 XIIC_CR_ENABLE_DEVICE_MASK);			}		}		/* Before the last byte is received, set NOACK to tell the slave IIC		 * device that it is the end, this must be done before reading the byte		 * from the FIFO		 */		if (ByteCount == 2) {			/* Write control reg with NO ACK allowing last byte to			 * have the No ack set to indicate to slave last byte read.			 */			XIo_Out8(BaseAddress + XIIC_CR_REG_OFFSET,				 CntlReg | XIIC_CR_NO_ACK_MASK);		}		/* Read in data from the FIFO and unthrottle the bus such that the		 * next byte is read from the IIC bus		 */		*BufferPtr++ = XIo_In8(BaseAddress + XIIC_DRR_REG_OFFSET);		if ((ByteCount == 1) && (Option == XIIC_REPEATED_START)) {			/* RSTA bit should be set only when the FIFO is completely Empty.			 */			XIo_Out8(BaseAddress + XIIC_CR_REG_OFFSET,				 XIIC_CR_ENABLE_DEVICE_MASK | XIIC_CR_MSMS_MASK				 | XIIC_CR_REPEATED_START_MASK);		}		/* Clear the latched interrupt status so that it will be updated with		 * the new state when it changes, this must be done after the receive		 * register is read		 */		XIic_mClearIisr(BaseAddress, XIIC_INTR_RX_FULL_MASK |				XIIC_INTR_TX_ERROR_MASK |

⌨️ 快捷键说明

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