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

📄 lpc177x_8x_i2c.c

📁 NXPl788上lwip的无操作系统移植,基于Embest开发板
💻 C
📖 第 1 页 / 共 3 页
字号:
	//get buffer to send/receive
	txdat = (uint8_t *) &TransferCfg->tx_data[TransferCfg->tx_count];
	rxdat = (uint8_t *) &TransferCfg->rx_data[TransferCfg->rx_count];

	switch(CodeStatus)
	{
		case I2C_I2STAT_M_TX_START:
		case I2C_I2STAT_M_TX_RESTART:
		//case I2C_I2STAT_M_RX_START:
		//case I2C_I2STAT_M_RX_RESTART
			// Send data first
			if(TransferCfg->tx_count < TransferCfg->tx_length)
			{
				/* Send slave address + WR direction bit = 0 ----------------------------------- */
				I2C_SendByte(I2Cx, (TransferCfg->sl_addr7bit << 1));
				Ret = I2C_BYTE_SENT;
			}
			else if (TransferCfg->rx_count  < TransferCfg->rx_length)
			{
				/* Send slave address + RD direction bit = 1 ----------------------------------- */
				I2C_SendByte(I2Cx, ((TransferCfg->sl_addr7bit << 1) | 0x01));
				Ret = I2C_BYTE_SENT;
			}
			break;
		case I2C_I2STAT_M_TX_SLAW_ACK:
		case I2C_I2STAT_M_TX_DAT_ACK:
			
			if(TransferCfg->tx_count < TransferCfg->tx_length)
			{
				I2C_SendByte(I2Cx, *txdat);
				
				txdat++;

				TransferCfg->tx_count++;

				Ret = I2C_BYTE_SENT;
			}
			else
			{
				I2C_Stop(I2Cx);

				Ret = I2C_SEND_END;
				
			}
			break;
		case I2C_I2STAT_M_TX_DAT_NACK:
			I2C_Stop(I2Cx);
			Ret = I2C_SEND_END;
			break;
		case I2C_I2STAT_M_RX_ARB_LOST:
        //case I2C_I2STAT_M_TX_ARB_LOST:
			I2Cx->CONSET = I2C_I2CONSET_STA|I2C_I2CONSET_AA;
			I2Cx->CONCLR = I2C_I2CONCLR_SIC;
			break;
		case I2C_I2STAT_M_RX_SLAR_ACK:
			I2Cx->CONSET = I2C_I2CONSET_AA;
			I2Cx->CONCLR = I2C_I2CONCLR_SIC;

			Ret = I2C_BYTE_RECV;
			break;
		case I2C_I2STAT_M_RX_DAT_ACK:
			if (TransferCfg->rx_count <TransferCfg->rx_length)
			{
				if (TransferCfg->rx_count < (TransferCfg->rx_length - 2))
				{
					I2C_GetByte(I2Cx, &tmp, TRUE);

					Ret = I2C_BYTE_RECV;
					
				}
				else  // the next byte is the last byte, send NACK instead.
				 {
					I2C_GetByte(I2Cx, &tmp, FALSE);
					Ret = I2C_BYTE_RECV;
				 }
				*rxdat++ = tmp;

				TransferCfg->rx_count++;
			}
			 else
			 {
				Ret = I2C_RECV_END;
			}
			
			break;
		case I2C_I2STAT_M_RX_DAT_NACK:
			I2C_GetByte(I2Cx, &tmp, FALSE);
			*rxdat++ = tmp;
			TransferCfg->rx_count++;
			I2C_Stop(I2Cx);
			Ret = I2C_RECV_END;
			break;
		case I2C_I2STAT_M_RX_SLAR_NACK:
		case I2C_I2STAT_M_TX_SLAW_NACK:
		case I2C_I2STAT_BUS_ERROR:
			// Send STOP condition
			I2C_Stop(I2Cx);
			Ret = I2C_ERR;
			break;
        /* No status information */
		case I2C_I2STAT_NO_INF:
          if ((TransferCfg->tx_count <TransferCfg->tx_length)||
               (TransferCfg->rx_count <TransferCfg->rx_length))
          {
            I2C_Stop(I2Cx);
            Ret = I2C_ERR;
          }
          else
          {
            Ret = I2C_RECV_END;
          }
          break;
        default:
			I2Cx->CONCLR = I2C_I2CONCLR_SIC;
			break;
	}
	
	return Ret;
}

/*********************************************************************//**
 * @brief 		Handle I2C Slave states.
 * @param[in]	I2Cx	I2C peripheral selected, should be:
 * 				- I2C_0
 * 				- I2C_1
 * 				- I2C_2
 * @param[in]	CodeStatus	I2C state
 * @param[in]	TransferCfg   Pointer to a I2C_S_SETUP_Type structure that
 * 								contains specified information about the
 * 								configuration for master transfer.
 * @return 		It can be
 *				- I2C_OK
 *				-I2C_BYTE_RECV
 *				-I2C_BYTE_SENT
 *				-I2C_SEND_END
 *				-I2C_RECV_END
 *				- I2C_ERR
 *				- I2C_NAK_RECV
 **********************************************************************/
int32_t I2C_SlaveHanleStates(en_I2C_unitId i2cId, uint32_t CodeStatus, I2C_S_SETUP_Type *TransferCfg)
{
	LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
	int32_t Ret = I2C_OK;
	uint8_t *txdat;
	uint8_t *rxdat;

	//get buffer to send/receive
	txdat = (uint8_t *) &TransferCfg->tx_data[TransferCfg->tx_count];
	rxdat = (uint8_t *) &TransferCfg->rx_data[TransferCfg->rx_count];
	
	switch (CodeStatus)
	{
		/* Reading phase -------------------------------------------------------- */
		/* Own SLA+R has been received, ACK has been returned */
		case I2C_I2STAT_S_RX_SLAW_ACK:

		/* General call address has been received, ACK has been returned */
		case I2C_I2STAT_S_RX_GENCALL_ACK:
			I2Cx->CONSET = I2C_I2CONSET_AA;
			I2Cx->CONCLR = I2C_I2CONCLR_SIC;
			break;
		/* Arbitration has been lost in Slave Address + R/W bit as bus Master. General Call has
			been received and ACK has been returned.*/
		case I2C_I2STAT_S_RX_ARB_LOST_M_GENCALL:
			I2Cx->CONSET = I2C_I2CONSET_AA|I2C_I2CONSET_STA;
			I2Cx->CONCLR = I2C_I2CONCLR_SIC;
			break;
		/* Previously addressed with own SLA;
		 * DATA byte has been received;
		 * ACK has been returned */
		case I2C_I2STAT_S_RX_ARB_LOST_M_SLA:
		case I2C_I2STAT_S_RX_PRE_SLA_DAT_ACK:

			/*
			 * All data bytes that over-flow the specified receive
			 * data length, just ignore them.
			 */
			if ((TransferCfg->rx_count < TransferCfg->rx_length) && (TransferCfg->rx_data != NULL))
			{
				*rxdat++ = (uint8_t)I2Cx->DAT;

				TransferCfg->rx_count++;

				Ret = I2C_BYTE_RECV;
			}
			if(TransferCfg->rx_count == (TransferCfg->rx_length) ) {
				I2Cx->CONCLR = I2C_I2CONCLR_AAC|I2C_I2CONCLR_SIC;
				Ret = I2C_BYTE_RECV;
			}
			else {
				I2Cx->CONSET = I2C_I2CONSET_AA;
				I2Cx->CONCLR = I2C_I2CONCLR_SIC;
			}
			
			break;
		/* DATA has been received, Only the first data byte will be received with ACK. Additional
				data will be received with NOT ACK. */
		case I2C_I2STAT_S_RX_PRE_GENCALL_DAT_ACK:
			if ((TransferCfg->rx_count < TransferCfg->rx_length) && (TransferCfg->rx_data != NULL))
			{
				*rxdat++ = (uint8_t)I2Cx->DAT;

				TransferCfg->rx_count++;

				Ret = I2C_BYTE_RECV;
			}
			I2Cx->CONCLR = I2C_I2CONCLR_AAC|I2C_I2CONCLR_SIC;
			break;

		/* Writing phase -------------------------------------------------------- */
		/* Own SLA+R has been received, ACK has been returned */
		case I2C_I2STAT_S_TX_SLAR_ACK:

		/* Data has been transmitted, ACK has been received */
		case I2C_I2STAT_S_TX_DAT_ACK:
			/*
			 * All data bytes that over-flow the specified receive
			 * data length, just ignore them.
			 */
			if ((TransferCfg->tx_count < TransferCfg->tx_length) && (TransferCfg->tx_data != NULL))
			{
				I2Cx->DAT = *txdat++;

				TransferCfg->tx_count++;

				Ret = I2C_BYTE_SENT;
			}

			I2Cx->CONSET = I2C_I2CONSET_AA;
			I2Cx->CONCLR = I2C_I2CONCLR_SIC;
			break;
		/* Arbitration lost in Slave Address and R/W bit as bus Master. Own Slave Address + Read
				has been received, ACK has been returned. */
		case I2C_I2STAT_S_TX_ARB_LOST_M_SLA:
			if ((TransferCfg->tx_count < TransferCfg->tx_length) && (TransferCfg->tx_data != NULL))
			{
				I2Cx->DAT = *txdat++;

				TransferCfg->tx_count++;

				Ret = I2C_BYTE_SENT;
			}
			I2Cx->CONSET = I2C_I2CONSET_AA|I2C_I2CONSET_STA;
			I2Cx->CONCLR = I2C_I2CONCLR_SIC;
			break;
			
		case I2C_I2STAT_S_TX_LAST_DAT_ACK:
		/* Data has been transmitted, NACK has been received,
		 * that means there's no more data to send, exit now */
		/*
		 * Note: Don't wait for stop event since in slave transmit mode,
		 * since there no proof lets us know when a stop signal has been received
		 * on slave side.
		 */
		case I2C_I2STAT_S_TX_DAT_NACK:
			I2Cx->CONSET = I2C_I2CONSET_AA;
			I2Cx->CONCLR = I2C_I2CONCLR_SIC;
			Ret = I2C_SEND_END;
			break;

		/* Previously addressed with own SLA;
		 * DATA byte has been received;
		 * NOT ACK has been returned */
		case I2C_I2STAT_S_RX_PRE_SLA_DAT_NACK:

		/* DATA has been received, NOT ACK has been returned */
		case I2C_I2STAT_S_RX_PRE_GENCALL_DAT_NACK:
			I2Cx->CONSET = I2C_I2CONSET_AA;
			I2Cx->CONCLR = I2C_I2CONCLR_SIC;
			Ret = I2C_RECV_END;
			break;

		/*
		 * Note that: Return code only let us know a stop condition mixed
		 * with a repeat start condition in the same code value.
		 * So we should provide a time-out. In case this is really a stop
		 * condition, this will return back after time out condition. Otherwise,
		 * next session that is slave receive data will be completed.
		 */

		/* A Stop or a repeat start condition */
		case I2C_I2STAT_S_RX_STA_STO_SLVREC_SLVTRX:
			I2Cx->CONSET = I2C_I2CONSET_AA;
			I2Cx->CONCLR = I2C_I2CONCLR_SIC;
			Ret = I2C_STA_STO_RECV;
			break;

		/* No status information */
		case I2C_I2STAT_NO_INF:
		/* Other status must be captured */
		default:
			I2Cx->CONSET = I2C_I2CONSET_AA;
			I2Cx->CONCLR = I2C_I2CONCLR_SIC;
			break;

	}

	return Ret;
}
/*********************************************************************//**
 * @brief 		General Master Interrupt handler for I2C peripheral
 * @param[in]	I2Cx	I2C peripheral selected, should be:
 * 				- LPC_I2C
 * 				- LPC_I2C1
 * 				- LPC_I2C2
 * @return 		None
 **********************************************************************/
void I2C_MasterHandler(en_I2C_unitId i2cId)
{
	LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
	uint8_t returnCode;
	I2C_M_SETUP_Type *txrx_setup;
	int32_t Ret = I2C_OK;

	txrx_setup = (I2C_M_SETUP_Type *) i2cdat[i2cId].txrx_setup;

	returnCode = (I2Cx->STAT & I2C_STAT_CODE_BITMASK);

	// Save current status
	txrx_setup->status = returnCode;

	Ret = I2C_MasterHanleStates(i2cId, returnCode, txrx_setup);

	if(I2C_CheckError(Ret))
	{
		if(txrx_setup->retransmissions_count < txrx_setup->retransmissions_max)
		{
			// Retry
			txrx_setup->retransmissions_count ++;
			txrx_setup->tx_count = 0;
			txrx_setup->rx_count = 0;
			// Reset STA, STO, SI
	        I2Cx->CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;
			I2Cx->CONSET = I2C_I2CONSET_STA;
			return;
		}
		else
		{
			goto s_int_end;
		}
	}
	else if (Ret & I2C_SEND_END)
	{
		// If no need to wait for data from Slave
		if(txrx_setup->rx_count >= (txrx_setup->rx_length)) 
		{
			goto s_int_end;
		}
		else	// Start to wait for data from Slave
		{
			// Reset STA, STO, SI
			I2Cx->CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;
			I2Cx->CONSET = I2C_I2CONSET_STA;
			return;
		}
	}
	else if (Ret & I2C_RECV_END) 
	{
		goto s_int_end;
	}
	else
	{
		return;
	}

s_int_end:
	// Disable interrupt
	I2C_IntCmd(i2cId, FALSE);

	I2Cx->CONCLR = I2C_I2CONCLR_AAC | I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC;

	I2C_MasterComplete[i2cId] = TRUE;
		
}


/*********************************************************************//**
 * @brief 		General Slave Interrupt handler for I2C peripheral
 * @param[in]	I2Cx	I2C peripheral selected, should be:
 *  			- LPC_I2C0
 *  			- LPC_I2C1
 *  			- LPC_I2C2
 * @return 		None
 **********************************************************************/
void I2C_SlaveHandler (en_I2C_unitId i2cId)
{
	LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);

	uint8_t returnCode;
	I2C_S_SETUP_Type *txrx_setup;
	uint32_t timeout;
	int32_t Ret = I2C_OK;

	txrx_setup = (I2C_S_SETUP_Type *) i2cdat[i2cId].txrx_setup;

handle_state:

	returnCode = (I2Cx->STAT & I2C_STAT_CODE_BITMASK);
	// Save current status
	txrx_setup->status = returnCode;


	Ret = I2C_SlaveHanleStates(i2cId, returnCode, txrx_setup);

	if(I2C_CheckError(Ret))
	{
		goto s_int_end;
	}
	else if (Ret & I2C_STA_STO_RECV)
	{
		// Temporally lock the interrupt for timeout condition
		I2C_IntCmd(i2cId, FALSE);
		// enable time out
		timeout = I2C_SLAVE_TIME_OUT;
		while(1)
		{
			if (I2Cx->CONSET & I2C_I2CONSET_SI)
			{
				// re-Enable interrupt
				I2C_IntCmd(i2cId, TRUE);
				goto handle_state;
			}
			else
			{
				timeout--;
				if (timeout == 0)
				{
					// timeout occur, it's really a stop condition
					txrx_setup->status |= I2C_SETUP_STATUS_DONE;
					goto s_int_end;
				}
			}
		}	
	}
	else if(Ret &I2C_SEND_END)
	{
		goto s_int_end;
	}
	else
	{
		return;
	}

s_int_end:
	// Disable interrupt
	I2C_IntCmd(i2cId, FALSE);
	I2Cx->CONCLR = I2C_I2CONCLR_AAC | I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC;

	I2C_SlaveComplete[i2cId] = TRUE;
}

/*********************************************************************//**
 * @brief 		Transmit and Receive data in master mode
 * @param[in]	I2Cx			I2C peripheral selected, should be:
 *				- LPC_I2C0
 *				- LPC_I2C1
 *				- LPC_I2C2
 * @param[in]	TransferCfg		Pointer to a I2C_M_SETUP_Type structure that

⌨️ 快捷键说明

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