示例#1
0
/*---------------------------------------------------------------------------------------------------------*/
void I2C_MasterTx(uint32_t u32Status)
{
    if(u32Status == 0x08)                       /* START has been transmitted */
    {
        I2C_SET_DATA(I2C0, g_u8DeviceAddr << 1);    /* Write SLA+W to Register I2CDAT */
        I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
    }
    else if(u32Status == 0x18)                  /* SLA+W has been transmitted and ACK has been received */
    {
        I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
        I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
    }
    else if(u32Status == 0x20)                  /* SLA+W has been transmitted and NACK has been received */
    {
        I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STA_STO_SI);
    }
    else if(u32Status == 0x28)                  /* DATA has been transmitted and ACK has been received */
    {
        if(g_u8DataLen != 3)
        {
            I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
            I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI);
        }
        else
        {
            I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STO_SI);
            g_u8EndFlag = 1;
        }
    }
    else
    {
        /* TO DO */
        printf("Status 0x%x is NOT processed\n", u32Status);
    }
}
/*---------------------------------------------------------------------------------------------------------*/
void I2C_MasterTx(uint32_t u32Status)
{
    if (u32Status == 0x08) {                    /* START has been transmitted */
        I2C_SET_DATA(I2C0, g_u8DeviceAddr);//g_u8DeviceAddr << 1);  /* Write SLA+W to Register I2CDAT */
        I2C_SET_CONTROL_REG(I2C0, I2C_SI);
    } else if (u32Status == 0x18) {             /* SLA+W has been transmitted and ACK has been received */
        I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
        I2C_SET_CONTROL_REG(I2C0, I2C_SI);
    } else if (u32Status == 0x20) {             /* SLA+W has been transmitted and NACK has been received */
        I2C_SET_CONTROL_REG(I2C0, I2C_STA | I2C_STO | I2C_SI);
    } else if (u32Status == 0x28) {             /* DATA has been transmitted and ACK has been received */
        if (g_u8DataLen != 2) {      // 3) {
            I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
            I2C_SET_CONTROL_REG(I2C0, I2C_SI);
        } else {
            I2C_SET_CONTROL_REG(I2C0, I2C_STO | I2C_SI);
            g_u8EndFlag = 1;
        }
    } else {
        /* TO DO */
	#ifdef I2C_TIMEOUT_ENABLE
		if(i2c_novalidack_timeout++>I2C_TIMEOUT_NOVALIDACK_COUNT)
		{
			I2C_SET_CONTROL_REG(I2C0, I2C_STO | I2C_SI);
			g_u8EndFlag = 1;
		}
	#endif

       // printf("Status 0x%x is NOT processed\n", u32Status);
    }
}
uint8_t MPU6050_I2C_SingleRead(uint8_t index)
{
	uint8_t tmp;
	      I2C_START(MPU6050_I2C_PORT);                         //Start
	      I2C_WAIT_READY(MPU6050_I2C_PORT);
        MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		
	
	      I2C_SET_DATA(MPU6050_I2C_PORT, MPU6050_I2C_SLA);         //send slave address+W
	      I2C_SET_CONTROL_REG(MPU6050_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(MPU6050_I2C_PORT);
        MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		

	      I2C_SET_DATA(MPU6050_I2C_PORT, index);               //send index
	      I2C_SET_CONTROL_REG(MPU6050_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(MPU6050_I2C_PORT);
        MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		
	
	      I2C_SET_CONTROL_REG(MPU6050_I2C_PORT, I2C_STA | I2C_SI);	//Start
	      I2C_WAIT_READY(MPU6050_I2C_PORT);
        MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag	

		    I2C_SET_DATA(MPU6050_I2C_PORT, (MPU6050_I2C_SLA+1));     //send slave address+R
	      I2C_SET_CONTROL_REG(MPU6050_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(MPU6050_I2C_PORT);
        MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag								
	
	      I2C_SET_CONTROL_REG(MPU6050_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(MPU6050_I2C_PORT);
        MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag								
				tmp = I2C_GET_DATA(MPU6050_I2C_PORT);                //read data   
	
	      I2C_STOP(MPU6050_I2C_PORT); //STOP
				return tmp;
}
示例#4
0
/*---------------------------------------------------------------------------------------------------------*/
void I2C_MasterRx(uint32_t u32Status)
{
    if (u32Status == 0x08) {                    /* START has been transmitted and prepare SLA+W */
        I2C_SET_DATA(I2C3, (g_u8DeviceAddr << 1)); /* Write SLA+W to Register I2CDAT */
        I2C_SET_CONTROL_REG(I2C3, I2C_SI);
    } else if (u32Status == 0x18) {             /* SLA+W has been transmitted and ACK has been received */
        I2C_SET_DATA(I2C3, g_au8TxData[g_u8DataLen++]);
        I2C_SET_CONTROL_REG(I2C3, I2C_SI);
    } else if (u32Status == 0x20) {             /* SLA+W has been transmitted and NACK has been received */
        I2C_SET_CONTROL_REG(I2C3, I2C_STA | I2C_STO | I2C_SI);
    } else if (u32Status == 0x28) {             /* DATA has been transmitted and ACK has been received */
        if (g_u8DataLen != 2) {
            I2C_SET_DATA(I2C3, g_au8TxData[g_u8DataLen++]);
            I2C_SET_CONTROL_REG(I2C3, I2C_SI);
        } else {
            I2C_SET_CONTROL_REG(I2C3, I2C_STA | I2C_SI);
        }
    } else if (u32Status == 0x10) {             /* Repeat START has been transmitted and prepare SLA+R */
        I2C_SET_DATA(I2C3, (g_u8DeviceAddr << 1) | 0x01);  /* Write SLA+R to Register I2CDAT */
        I2C_SET_CONTROL_REG(I2C3, I2C_SI);
    } else if (u32Status == 0x40) {             /* SLA+R has been transmitted and ACK has been received */
        I2C_SET_CONTROL_REG(I2C3, I2C_SI);
    } else if (u32Status == 0x58) {             /* DATA has been received and NACK has been returned */
        g_u8RxData = I2C_GET_DATA(I2C3);
        I2C_SET_CONTROL_REG(I2C3, I2C_STO | I2C_SI);
        g_u8EndFlag = 1;
    } else {
        /* TO DO */
        printf("Status 0x%x is NOT processed\n", u32Status);
    }
}
uint8_t LM75A_I2C_SingleRead(uint8_t index)
{
	uint8_t tmp;
	      I2C_START(LM75A_I2C_PORT);                         //Start
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		
	
	      I2C_SET_DATA(LM75A_I2C_PORT, LM75A_I2C_SLA);         //send slave address+W
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		

	      I2C_SET_DATA(LM75A_I2C_PORT, index);               //send index
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		
	
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_STA | I2C_SI);	//Start
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag	

		    I2C_SET_DATA(LM75A_I2C_PORT, (LM75A_I2C_SLA+1));     //send slave address+R
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag								
	
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag								
				tmp = I2C_GET_DATA(LM75A_I2C_PORT);                //read data   
	
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI|I2C_STO);//Stop
				return tmp;
}
uint16_t LM75A_I2C_DoubleRead(uint8_t index)
{
	uint8_t msb, lsb;
	uint16_t tmp;
	      I2C_START(LM75A_I2C_PORT);                         //Start
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		
	
	      I2C_SET_DATA(LM75A_I2C_PORT, LM75A_I2C_SLA);      //send slave address+W
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		

	      I2C_SET_DATA(LM75A_I2C_PORT, index);               //send index
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		
	
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_STA | I2C_SI);	//Start
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag	

		    I2C_SET_DATA(LM75A_I2C_PORT, (LM75A_I2C_SLA+1));     //send slave address+R
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag								
	
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag								
				msb = I2C_GET_DATA(LM75A_I2C_PORT);                //read data   

	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag								
				lsb = I2C_GET_DATA(LM75A_I2C_PORT);                //read data  				
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI|I2C_STO);//Stop
				
				tmp = msb;
				tmp = tmp<<8 | lsb;
				return tmp;
}
示例#7
0
uint8_t TwoWire::endTransmission(uint8_t sendStop) {
	
	int sent = 0;	/* Send data */
	int timeout_=TIMEOUT;
	while(timeout_--)
	{
		/* Send start */
		I2C_START(i2c);	
		I2C_WAIT_READY(i2c);			
	
		/* Send control byte */
		I2C_SET_DATA(i2c, txAddress);
		I2C_SET_CONTROL_REG(i2c, I2C_SI);
		I2C_WAIT_READY(i2c);			
		if(I2C_GET_STATUS(i2c)!=0x18) 
		{ 
			I2C_SET_CONTROL_REG(i2c, I2C_STO | I2C_SI);
			continue;
		}
		sent = 0;
		while (sent < txBufferLength) {
			I2C_SET_DATA(i2c, txBuffer[sent++]);
			I2C_SET_CONTROL_REG(i2c, I2C_SI);
			I2C_WAIT_READY(i2c);	
			if(I2C_GET_STATUS(i2c)!=0x28) { 
				I2C_SET_CONTROL_REG(i2c, I2C_STO | I2C_SI);
				continue;
			}			
		}
		if(sendStop==true)
		{
			/* Send stop */
			I2C_SET_CONTROL_REG(i2c, I2C_STO | I2C_SI);  				
		}
		break;
	}	
	// empty buffer
	txBufferLength = 0;
	status = MASTER_IDLE;	
	return sent;	
}
示例#8
0
/*---------------------------------------------------------------------------------------------------------*/
void I2C_SlaveTRx(uint32_t u32Status)
{
    if(u32Status == 0x60)                       /* Own SLA+W has been receive; ACK has been return */
    {
        g_u8SlvDataLen = 0;
        I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
    }
    else if(u32Status == 0x80)                 /* Previously address with own SLA address
                                                   Data has been received; ACK has been returned*/
    {
        g_au8SlvRxData[g_u8SlvDataLen] = (unsigned char)I2C_GET_DATA(I2C0);
        g_u8SlvDataLen++;

        if(g_u8SlvDataLen == 2)
        {
            slave_buff_addr = (g_au8SlvRxData[0] << 8) + g_au8SlvRxData[1];
        }
        if(g_u8SlvDataLen == 3)
        {
            g_au8SlvData[slave_buff_addr] = g_au8SlvRxData[2];
            g_u8SlvDataLen = 0;
        }
        I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
    }
    else if(u32Status == 0xA8)                  /* Own SLA+R has been receive; ACK has been return */
    {
        I2C_SET_DATA(I2C0, g_au8SlvData[slave_buff_addr]);
        slave_buff_addr++;
        I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
    }
    else if(u32Status == 0xC0)                 /* Data byte or last data in I2CDAT has been transmitted
                                                   Not ACK has been received */
    {
        I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
    }
    else if(u32Status == 0x88)                 /* Previously addressed with own SLA address; NOT ACK has
                                                   been returned */
    {
        g_u8SlvDataLen = 0;
        I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
    }
    else if(u32Status == 0xA0)                 /* A STOP or repeated START has been received while still
                                                   addressed as Slave/Receiver*/
    {
        g_u8SlvDataLen = 0;
        I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
    }
    else
    {
        /* TO DO */
        printf("Status 0x%x is NOT processed\n", u32Status);
    }
}
void MPU6050_I2C_SingleWrite(uint8_t index, uint8_t data)
{
        I2C_START(MPU6050_I2C_PORT);                         //Start
	      I2C_WAIT_READY(MPU6050_I2C_PORT);
        MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag	
	
	      I2C_SET_DATA(MPU6050_I2C_PORT, MPU6050_I2C_SLA);         //send slave address
	      I2C_SET_CONTROL_REG(MPU6050_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(MPU6050_I2C_PORT);
        MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		

	      I2C_SET_DATA(MPU6050_I2C_PORT, index);               //send index
	      I2C_SET_CONTROL_REG(MPU6050_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(MPU6050_I2C_PORT);
        MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		

	      I2C_SET_DATA(MPU6050_I2C_PORT, data);                //send Data
	      I2C_SET_CONTROL_REG(MPU6050_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(MPU6050_I2C_PORT);
        MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		
				
				I2C_STOP(MPU6050_I2C_PORT); //STOP
}
void LM75A_I2C_SingleWrite(uint8_t index, uint8_t data)
{
        I2C_START(LM75A_I2C_PORT);                         //Start
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag	
	
	      I2C_SET_DATA(LM75A_I2C_PORT, LM75A_I2C_SLA);         //send slave address
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		

	      I2C_SET_DATA(LM75A_I2C_PORT, index);               //send index
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		

	      I2C_SET_DATA(LM75A_I2C_PORT, data);                //send Data
	      I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI);
	      I2C_WAIT_READY(LM75A_I2C_PORT);
        LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk;   //clear flag		
				
				I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI|I2C_STO);//Stop
}
示例#11
0
/*************************************************************************
 Send one byte to I2C device
 
 Input:    byte to be transfered
 Return:   0 write successful 
           1 write failed
*************************************************************************/
unsigned char lib_i2c_write(unsigned char data)
{
    I2C_SET_DATA(I2C, data);
    I2C_SET_CONTROL_REG(I2C, I2C_SI);
    if (!I2C_WAIT_READY_ERROR(I2C))
        return 2;
    // Check ACK
    uint8_t status = I2C_GET_STATUS(I2C);
    // Master TX/RX Address/Data ACKs
    if (status != 0x18 && status != 0x28 && status != 0x40 && status != 0x50)
        return 1;

    return 0;
}
示例#12
0
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) {
	if (quantity > BUFFER_LENGTH) quantity = BUFFER_LENGTH;
	int readed = 0;
	int timeout_=TIMEOUT;
	while(timeout_--)
	{
		/* Send start */
		I2C_START(i2c);	
		I2C_WAIT_READY(i2c);
		
		/* Send control byte */
		I2C_SET_DATA(i2c, (address<<1)+1);			
		I2C_SET_CONTROL_REG(i2c, I2C_SI | I2C_AA);
		I2C_WAIT_READY(i2c);	
	
		if(I2C_GET_STATUS(i2c)!=0x40)
		{ /* Send stop */			
			I2C_SET_CONTROL_REG(i2c, I2C_STO | I2C_SI);	
			continue;	
		}
		readed = 0;		 	 
		while (readed < quantity){				
			/* Read data */
			if((readed+1)==quantity)			
				I2C_SET_CONTROL_REG(i2c, I2C_SI);
			else
				I2C_SET_CONTROL_REG(i2c, I2C_SI | I2C_AA);
			I2C_WAIT_READY(i2c);			
			rxBuffer[readed++] = I2C_GET_DATA(i2c);
		};	
		if(sendStop==true){
					/* Send stop */
			I2C_SET_CONTROL_REG(i2c, I2C_STO | I2C_SI);				
		}else
			I2C_SET_CONTROL_REG(i2c, I2C_SI);					
		
		break;
	}
			
	// set rx buffer iterator vars
	rxBufferIndex = 0;
	rxBufferLength = readed;		
		
	return readed;			
}
示例#13
0
void I2C_MasterRx_var(uint32_t u32Status)
{
	uint16_t i=0;
	if (u32Status == 0x08) 
	{                    /* START has been transmitted and prepare SLA+W */
		I2C_SET_DATA(I2C0, g_u8DeviceAddr);//(g_u8DeviceAddr << 1)); /* Write SLA+W to Register I2CDAT */
		I2C_SET_CONTROL_REG(I2C0, I2C_SI);
	} 
	else if (u32Status == 0x18 )
	{             /* SLA+W has been transmitted and ACK has been received */
		I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
		I2C_SET_CONTROL_REG(I2C0, I2C_SI);
	} 
	else if (u32Status == 0x20) 
	{             /* SLA+W has been transmitted and NACK has been received */
		I2C_SET_CONTROL_REG(I2C0, I2C_STA | I2C_STO | I2C_SI);
	} 
	else if (u32Status == 0x28) 
	{             /* DATA has been transmitted and ACK has been received */
	#if 0//def DEBUG_ENABLE
		printf("Status rx 0x%x\n", u32Status);
	#endif
		for(i=0;i<65535;i++);//very important
	//	for(i=0;i<50000;i++);
	//	for(i=0;i<50000;i++);

		if (g_u8DataLen != 1)   // 2
		{   
			I2C_SET_DATA(I2C0, g_au8TxData[g_u8DataLen++]);
			I2C_SET_CONTROL_REG(I2C0, I2C_SI);
		}
		else 
		{
			I2C_SET_CONTROL_REG(I2C0, I2C_STA | I2C_SI);
		}
	} 
	else if (u32Status == 0x10) 
	{             /* Repeat START has been transmitted and prepare SLA+R */
		I2C_SET_DATA(I2C0, g_u8DeviceAddr  | 0x01);//(g_u8DeviceAddr << 1) | 0x01);  /* Write SLA+R to Register I2CDAT */
		I2C_SET_CONTROL_REG(I2C0, I2C_SI);
	} 
	else if (u32Status == 0x40) 
	{             /* SLA+R has been transmitted and ACK has been received */
	#if 0//def DEBUG_ENABLE
		printf("Status rx 0x%x\n", u32Status);
	#endif

		I2C_SET_CONTROL_REG(I2C0, I2C_SI|I2C_AA);
	} 
	#if 1//ericyang add for HDC1000
	else if (u32Status == 0x50) 
	{             /* DATA has been received and ACK has been returned */
		g_u8RxData = I2C_GET_DATA(I2C0);
		//I2C_SET_CONTROL_REG(I2C0, I2C_STO | I2C_SI);
		I2C_SET_CONTROL_REG(I2C0,   I2C_SI);
		//g_u8EndFlag = 1;
	}
	#endif
	else if (u32Status == 0x58) 
	{             /* DATA has been received and NACK has been returned */		
			g_u8RxData1 = I2C_GET_DATA(I2C0);
			I2C_SET_CONTROL_REG(I2C0, I2C_STO | I2C_SI);
			g_u8EndFlag = 1;
	#ifdef DEBUG_ENABLE
		printf("g_u8RxData 0x%x  g_u8RxData1:0x%x\n", g_u8RxData,g_u8RxData1);
	#endif		
	} 
	else 
	{
	/* TO DO */
	#ifdef I2C_TIMEOUT_ENABLE
		if(i2c_novalidack_timeout++>I2C_TIMEOUT_NOVALIDACK_COUNT)
		{
			I2C_SET_CONTROL_REG(I2C0, I2C_STO | I2C_SI);
			g_u8EndFlag = 1;
		}
	#endif
	#ifdef DEBUG_ENABLE
		printf("Status rxvar 0x%x is NOT processed\n", u32Status);
	#endif
	}
}
示例#14
0
/*---------------------------------------------------------------------------------------------------------*/
void I2C_MS_Slave(uint32_t status)
{

	if ((status == 0x60) || (status == 0x68))       /* SLA+W has been received and ACK has been returned */
	{
		I2CSlvStage = 0;
		I2CSlvState = 0;
		I2CCurrentLen = 0;
		I2C_SET_CONTROL_REG(I2C_MS_PORT, I2C_CTL_SI_AA);
	}
	else if (status == 0x80)                        /* DATA has been received and ACK has been returned */
	{
		if (I2CSlvStage == 0)
		{
			if(I2CSlvState == 0)
			{
				I2CSlvCmd = (unsigned char) I2C_GET_DATA(I2C_MS_PORT);
				I2CSlvState = 1;
				if(I2CSlvCmd==4 || I2CSlvCmd==6)
					I2CMS_SlvRxFin=0;
			}
			else if (I2CSlvState == 1)
			{
				I2CSlvCmd = ((unsigned char) I2C_GET_DATA(I2C_MS_PORT)<<8 | I2CSlvCmd);
				I2CSlvState = 0;
				I2CSlvStage = 1;
			}
		}
		else if (I2CSlvStage == 1)
		{
			if(I2CSlvState==0)
			{
				I2CMS_SlvRxData[I2CCurrentLen++] = (unsigned char) I2C_GET_DATA(I2C_MS_PORT);
				I2CDataEndBit = I2CMS_SlvRxData[0];
				I2CSlvState=1;
			}
			else if(I2CSlvState==1)
			{
				I2CMS_SlvRxData[I2CCurrentLen++] = (unsigned char) I2C_GET_DATA(I2C_MS_PORT);
				I2CDataEndBit = (I2CMS_SlvRxData[1]<<8)|(I2CDataEndBit);
				I2CSlvState=0;
				I2CSlvStage=2;
			}
		}
		else if (I2CSlvStage == 2)
		{
			if(I2CCurrentLen < I2CDataEndBit)
			{
				I2CMS_SlvRxData[I2CCurrentLen++] = (unsigned char) I2C_GET_DATA(I2C_MS_PORT);
			}
			if(I2CCurrentLen == I2CDataEndBit)
			{
				I2CMS_SlvRxFin=1;
			}
		}
		I2C_SET_CONTROL_REG(I2C_MS_PORT, I2C_CTL_SI_AA);
	}
	else if ((status == 0xB0) || (status == 0xA8))  /* SLA+R has been received and ACK has been returned */
	{
		I2CCurrentLen = 0;								//I2C is awake and Sleep flag is unset
		I2CSlvState = 1;
		if(I2CSlv_InitFlag==0)
		{
			I2CSlv_readyFlag=0;
			I2CDataEndBit = DEV_NOT_INIT;
			I2C_SET_DATA(I2C_MS_PORT, DEV_NOT_INIT);
			I2CCurrentLen++;
		}
		else if(I2CSlv_InitFlag==1)
		{
			I2CSlv_readyFlag = 1;
			switch (I2CSlvCmd)
			{
			case 1:																																			//Device descriptor
				I2CDataEndBit = I2CMS_SlvTxDevDesData[0];
				I2C_SET_DATA(I2C_MS_PORT, I2CMS_SlvTxDevDesData[I2CCurrentLen++]);
				break;
			case 2:																																			//Report descriptor
				I2CDataEndBit = I2CMS_SlvTxRPDesData[0];
				I2C_SET_DATA(I2C_MS_PORT, I2CMS_SlvTxRPDesData[I2CCurrentLen++]);
				break;
			case 3:																																			//Input report
				I2CDataEndBit = I2CMS_SlvTxInRPData[0];
				I2C_SET_DATA(I2C_MS_PORT, I2CMS_SlvTxInRPData[I2CCurrentLen++]);
				break;
			case 5:																																			//Feature report
				I2CDataEndBit = I2CMS_SlvTxGetFTData[0];
				I2C_SET_DATA(I2C_MS_PORT, I2CMS_SlvTxGetFTData[I2CCurrentLen++]);
				break;
			}
		}
		I2C_SET_CONTROL_REG(I2C_MS_PORT, I2C_CTL_SI_AA);
	}
	else if (status == 0xB8)                        /* DATA has been transmitted and ACK has been received */
	{
		if(I2CCurrentLen < I2CDataEndBit)
		{
			if(I2CSlv_readyFlag==0)
			{
				I2C_SET_DATA(I2C_MS_PORT, 0);
				I2CCurrentLen++;
			}
			else if(I2CSlv_readyFlag==1)
			{
				switch (I2CSlvCmd)
				{
				case 1:
					I2C_SET_DATA(I2C_MS_PORT, I2CMS_SlvTxDevDesData[I2CCurrentLen++]);
					break;
				case 2:
					I2C_SET_DATA(I2C_MS_PORT, I2CMS_SlvTxRPDesData[I2CCurrentLen++]);
					break;
				case 3:
					I2C_SET_DATA(I2C_MS_PORT, I2CMS_SlvTxInRPData[I2CCurrentLen++]);
					break;
				case 5:
					I2C_SET_DATA(I2C_MS_PORT, I2CMS_SlvTxGetFTData[I2CCurrentLen++]);
					break;
				}
			}
		}
		I2C_SET_CONTROL_REG(I2C_MS_PORT, I2C_CTL_SI_AA);
	}
	else if (status == 0xC0)                /* DATA has been transmitted and NACK has been received */
	{
		I2C_SET_CONTROL_REG(I2C_MS_PORT, I2C_CTL_SI_AA);
	}
	else if(status == 0x88)                 /* Previously addressed with own SLA address; NOT ACK has
                                                   been returned */
	{
		I2C_SET_CONTROL_REG(I2C_MS_PORT, I2C_CTL_SI_AA);
	}
	else if (status == 0xA0)                /* STOP or Repeat START has been received */
	{
		I2C_SET_CONTROL_REG(I2C_MS_PORT, I2C_CTL_SI_AA);
	}
	else
	{
		I2C_SET_CONTROL_REG(I2C_MS_PORT, I2C_CTL_SI);
		//DEBUG_PRINT("Status 0x%x is NOT processed\n", status);
	}
}
示例#15
0
void TwoWire::I2C_SlaveTRx(uint32_t u32Status)
{
    if(u32Status == 0x60)                       /* Own SLA+W has been receive; ACK has been return */
    {
    	 status = SLAVE_RECV;
        srvBufferLength = 0;
        I2C_SET_CONTROL_REG(i2c, I2C_SI_AA);
    }
    else if(u32Status == 0x80 || u32Status==0x10)                 /* Previously address with own SLA address
                                                   Data has been received; ACK has been returned*/
    {
        srvBuffer[srvBufferLength] = (unsigned char) I2C_GET_DATA(i2c);
        srvBufferLength++;

        I2C_SET_CONTROL_REG(i2c, I2C_SI_AA);
    }
    else if(u32Status == 0xA8)                  /* Own SLA+R has been receive; ACK has been return */
    {				    		        
     
				// Alert calling program to generate a response ASAP
				if (onRequestCallback && status != SLAVE_SEND)
				{
					srvBufferLength = 0;
					srvBufferIndex = 0;	
					onRequestCallback();
				}			
  			status = SLAVE_SEND;  							
				if (srvBufferIndex < srvBufferLength)
				{						
						//Serial.print("==============>");
						//Serial.println((char)srvBuffer[srvBufferIndex]);
						I2C_SET_DATA(i2c, srvBuffer[srvBufferIndex++]);						
						I2C_SET_CONTROL_REG(i2c, I2C_SI_AA);						
				}
				
				if (srvBufferIndex == srvBufferLength)
						status = SLAVE_IDLE;
		}else if(u32Status == 0xB8)	{
				if (srvBufferIndex < srvBufferLength){				
						//Serial.print("==============>");
						//Serial.println((char)srvBuffer[srvBufferIndex]);		
						I2C_SET_DATA(i2c, srvBuffer[srvBufferIndex++]);						
						I2C_SET_CONTROL_REG(i2c, I2C_SI_AA);						
				}
				
				if (srvBufferIndex == srvBufferLength)
						status = SLAVE_IDLE;			
		}
    else if(u32Status == 0xC0)                 /* Data byte or last data in I2CDAT has been transmitted
                                                   Not ACK has been received */
    {
        I2C_SET_CONTROL_REG(i2c, I2C_SI_AA);
    }
    else if(u32Status == 0x88)                 /* Previously addressed with own SLA address; NOT ACK has
                                                   been returned */
    {
        srvBufferLength = 0;
        I2C_SET_CONTROL_REG(i2c, I2C_SI_AA);
    }
    else if(u32Status == 0xA0)                 /* A STOP or repeated START has been received while still
                                                   addressed as Slave/Receiver*/
    {
        srvBufferIndex = 0;
        I2C_SET_CONTROL_REG(i2c, I2C_SI_AA);
 
 
 			if (status != SLAVE_IDLE)
 			{
 				for (uint8_t i = 0; i < srvBufferLength; ++i)
					rxBuffer[i] = srvBuffer[i];
				rxBufferIndex = 0;
				rxBufferLength = srvBufferLength;				             
				onReceiveCallback( rxBufferLength); // Alert calling program
				status = SLAVE_IDLE;
			}
    }
}