Example #1
0
unsigned char I2C_ReadBit()
{
	I2C_DATA_HI();

	I2C_CLOCK_HI();
	I2C_DELAY();

	unsigned char c = digitalRead(C,0);

	I2C_CLOCK_LO();
	I2C_DELAY();

	return c;
}
unsigned char I2C_ReadBit()
{
	I2C_DATA_HI();

	I2C_CLOCK_HI();
	I2C_DELAY();

	unsigned char c = I2C_PIN;

	I2C_CLOCK_LO();
	I2C_DELAY();

	return ( c >> I2C_DAT ) & 1;
}
Example #3
0
void I2C_Start()
{
	// set both to high at the same time
	// I2C_DDR &= ~( ( 1 << I2C_DAT ) | ( 1 << I2C_CLK ) );
	I2C_CLOCK_HI();
	I2C_DATA_HI();
	I2C_DELAY();

	I2C_DATA_LO();
	I2C_DELAY();

	I2C_CLOCK_LO();
	I2C_DELAY();
}
Example #4
0
/*I2C起始位*/
void i2cStart(void)
{	
	/*SDA=1, SCL=1 */
	I2cData(HIGH);
	I2cClk(HIGH);
    I2C_DELAY(CLOCK_HIGH_TIME);

	/*SCL=1, SDA=0*/
	I2cData(LOW);
	I2C_DELAY(START_CONDITION_HOLD_TIME+3);

	/*SCL=0 SDA=0*/
	I2cClk(LOW);
	I2C_DELAY(CLOCK_LOW_TIME);
}
Example #5
0
/**
 * \brief I2C Wait Ack,Internal function
 * \return 应答信号
 */
static bool I2C_WaitAck(void)
{
    uint8_t ack;
    SDA_DDR_IN();
    SCL_L();
    
    I2C_DELAY();
    SCL_H();
    I2C_DELAY();
    ack = SDA_IN();
    SCL_L();
    SDA_DDR_OUT();
    
    return ack;
}
Example #6
0
/*I2C停止位*/
void I2cStop(void)
{
	/*SCL=0, SDA=0 */
	I2cData(LOW);
	I2C_DELAY(CLOCK_LOW_TIME*2);

	/*SCL=1, SDA=0*/
	I2cClk(HIGH);
	I2C_DELAY(CLOCK_HIGH_TIME*9);

	/*SCL=1 SDA=1*/
	I2cData(HIGH);
	I2C_DELAY(STOP_CONDITION_HOLD_TIME);
    I2C_DELAY(CLOCK_LOW_TIME*3);
}
Example #7
0
File: i2c.c Project: 211217613/uhd
static bool _i2c_read_byte_ex(io_pin_t sda, io_pin_t scl, uint8_t* value, bool pull_up)
{
    // Assumes:
    //  SDA output is LOW
    //  SCL output is LOW
	
	io_input_pin(sda);
	if (pull_up)
		io_set_pin(sda);	// OK to leave line floating for a moment (better not to drive as slave will be pulling it to ground)

    (*value) = 0x00;

    for (uint8_t i = 0; i < 8; ++i)
    {
//		if (pull_up)
//			io_set_pin(scl);	// [Not ideal with pull-up]
        io_input_pin(scl);	// Release HIGH
		if (pull_up)
			io_set_pin(scl);
        I2C_DELAY(I2C_DEFAULT_SCL_HIGH_PERIOD);
#ifdef I2C_ALLOW_CLOCK_STRETCH
		uint8_t retries = I2C_DEFAULT_MAX_BUS_RETRIES;
		while (io_test_pin(scl) == false)	// Clock stretch requested?
		{
			I2C_DELAY(I2C_DEFAULT_BUS_WAIT);
			if (--retries == 0)
			{
				debug_log_ex("I2C:R ");
				debug_log_hex(scl);
				debug_blink_rev(5);
				return false;
			}				
		}
#endif // I2C_ALLOW_CLOCK_STRETCH
        (*value) |= ((io_test_pin(sda) ? 0x1 : 0x0) << (7 - i));   // MSB first

		if (pull_up)
			io_clear_pin(scl);
        io_output_pin(scl);	// Drive LOW (not ideal with pull-up)
//		if (pull_up)
//			io_clear_pin(scl);
        I2C_DELAY(I2C_DEFAULT_SCL_LOW_PERIOD);
    }

    // Not necessary to ACK since it's only this one byte

    return true;
}
Example #8
0
static int EEPROM_EnterWriteMode()
{
    if(ROM_BYTE_Write(EEPPROT, 1, 0x9F) != TRUE)		// Protection register UNLOCK!
		return -1;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPCOMM, 1, EEP_WENL | EEP_STNBY) != TRUE)		// stnby mode (added by kyi)
		return -2;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPCFG, 1, EEP_WEN | AUTOADR) != TRUE)	// Auto Address increase mode
		return -4;
    I2C_DELAY();

	return TRUE;
}
Example #9
0
/**
 * \brief I2C 发送一个字节数据,Internal function
 * \param[in] data 待发送的数据(字节)
 * \retval None
 */
static void I2C_SendByte(uint8_t data)
{
    volatile uint8_t i;
    
    i = 8;
    while(i--)
    {
        if(data & 0x80) SDA_H();
        else SDA_L();
        data <<= 1;
        I2C_DELAY();
        SCL_H();
        I2C_DELAY();
        SCL_L();
    }

}
Example #10
0
/*I2C写比特位*/
void I2cWriteBit(BYTE x)
{
	I2cClk(LOW);
	I2C_DELAY(CLOCK_LOW_TIME);

	if (x & 0x80) /* the MSB is sent out first*/
		I2cData(HIGH);
	else
		I2cData(LOW);

	I2C_DELAY(CLOCK_LOW_TIME);
	I2cClk(HIGH);
	I2C_DELAY(CLOCK_HIGH_TIME*15);
	I2cClk(LOW);
	I2C_DELAY(CLOCK_LOW_TIME);
/*	I2cData(HIGH);*/
}
Example #11
0
void TwoWire::set_scl(bool state) {
    I2C_DELAY(this->i2c_delay);
    digitalWrite(this->scl_pin,state);
    //Allow for clock stretching - dangerous currently
    if (state == HIGH) {
        while(digitalRead(this->scl_pin) == 0);
    }
}
Example #12
0
File: i2c.c Project: 211217613/uhd
static bool _i2c_start_ex(io_pin_t sda, io_pin_t scl, bool pull_up)
{
	// Assumes: SDA/SCL are both inputs

	uint8_t retries = I2C_DEFAULT_MAX_BUS_RETRIES;
	while ((io_test_pin(sda) == false) || (io_test_pin(scl) == false))
	{
		I2C_DELAY(I2C_DEFAULT_BUS_WAIT);
		if (retries-- == 0)
		{
debug_log("I2C:S1");
			return false;
		}			
	}
	
	// START condition
//	if (pull_up == false)
		io_clear_pin(sda);	// Set LOW before switching to output
	io_output_pin(sda);
//	if (pull_up)
//		io_clear_pin(sda);
	I2C_DELAY(I2C_DEFAULT_SCL_LOW_PERIOD);  // Thd, sta
	
	retries = I2C_DEFAULT_MAX_BUS_RETRIES;
	while (io_test_pin(scl) == false)	// SCL should remain high
	{
		I2C_DELAY(I2C_DEFAULT_BUS_WAIT);
		if (retries-- == 0)
		{
			io_input_pin(sda);
debug_log_ex("I2C:S2", false);
debug_log_hex(scl);
			return false;
		}			
	}

//	if (pull_up == false)
		io_clear_pin(scl);
	io_output_pin(scl);
//	if (pull_up)
//		io_clear_pin(scl);
	I2C_DELAY(I2C_DEFAULT_SCL_LOW_PERIOD / 2);   // MAGIC

	return true;
}
Example #13
0
/*I2C读比特位*/
BYTE I2cReadBit(void)
{
	volatile BYTE x;
    FAST int iLocKey;

	I2cClk(LOW);
	I2C_DELAY(CLOCK_LOW_TIME );
	I2cClk(HIGH);
	I2C_DELAY(CLOCK_HIGH_TIME);

	iLocKey = intLock ();
    x = PinDataRead();
    intUnlock (iLocKey);

	I2cClk(LOW);
	I2C_DELAY(CLOCK_LOW_TIME);
	return x;
}
void I2C_Init()
{
	I2C_PORT &= ~( ( 1 << I2C_DAT ) | ( 1 << I2C_CLK ) );

	I2C_CLOCK_HI();
	I2C_DATA_HI();

	I2C_DELAY();
}
Example #15
0
/**
 * \brief I2C 接收一个字节数据,Internal function
 * \return 待接收的数据(字节)
 */
static uint8_t I2C_GetByte(void)
{
    uint8_t i,byte;
    
    i = 8;
    byte = 0;

    SDA_DDR_IN();
    while(i--)
    {
        SCL_L();
        I2C_DELAY();
        SCL_H();
        I2C_DELAY();
        byte = (byte<<1)|(SDA_IN() & 1);
    }
    SCL_L();
    SDA_DDR_OUT();
    return byte;
}
Example #16
0
void I2C_Init()
{
	// I2C_PORT &= ~( ( 1 << I2C_DAT ) | ( 1 << I2C_CLK ) );
	digitalWrite(C,0,LOW);
	digitalWrite(C,1,LOW);

	I2C_CLOCK_HI();
	I2C_DATA_HI();

	I2C_DELAY();
}
Example #17
0
void I2C_WriteBit( unsigned char c )
{
	if ( c > 0 )
	{
		I2C_DATA_HI();
	}
	else
	{
		I2C_DATA_LO();
	}

	I2C_CLOCK_HI();
	I2C_DELAY();
	
	I2C_CLOCK_LO();
	I2C_DELAY();

	if ( c > 0 )
	{
		I2C_DATA_LO();
	}
}
Example #18
0
int	NTS_MOSC_Set(void)
{
	int tErr = 0;

	tErr = ROM_BYTE_Write( EEP_SFR_I2C|PLLPROT, 1, EEP_WEN | 0x7F);									// Protection Unlock
	if(tErr!= TRUE)
		return -1;

	I2C_DELAY();

	tErr = ROM_BYTE_Write( EEP_SFR_I2C|MCLKSEL, 1, EEP_WEN | 0x02);									// MOSC : Internal Div Clock, Rev0 10.65MHz
	if(tErr!= TRUE)
		return -2;
	I2C_DELAY();

	tErr = ROM_BYTE_Write( EEP_SFR_I2C|PLLPROT, 1, EEP_WEN | 0x00);									// Protection Lock
	if(tErr!= TRUE)
		return -4;
	I2C_DELAY()

	return TRUE;
}
Example #19
0
/*****************************************************************************
*   函数名      : char COMMON_I2C_Receive_Byte(char ackn)
*   功能        :  I2C从总线上读取一个字节
*   输入参数    : void
*   输出参数    : none
*   返回值说明  : 读到的8位数据
*****************************************************************************/
char COMMON_I2C_Receive_Byte(char ackn)
{
    char cLoop = 0 ;
    char cReceivedByte = 0;
    bool bTmp=0;

    SDA_INPUT_MODE;             /* Make SDA an input */
    SCL_L;                      /* Reset SCL */
    I2C_DELAY(100);
    for (cLoop=8; cLoop>0; cLoop--)
    {
        SCL_H;                  /* Set SCL */
        I2C_DELAY(100);
        cReceivedByte <<= 1;    /* Rotate data */
        SDA_INPUT_DATA(bTmp);   /* Read SDA -> data */
        if(1 == bTmp )
        {
            cReceivedByte |= 1;  
        }
        I2C_DELAY(100);
        SCL_L;
        I2C_DELAY(200);
    }

    SDA_OUTPUT_MODE;            /* SDA is turned in an output to write the ACK on the data line */
    I2C_DELAY(100);
    if (0 == ackn)
    {
        SDA_L;                  /* SDA = ACK bit */
    }
    else
    {
        SDA_H;                  /* SDA = ACK bit */
    }
    I2C_DELAY(100);
    SCL_H;                      /* Set SCL */
    I2C_DELAY(100);
    SCL_L;                    //Reset SCL
    I2C_DELAY(100);

    return(cReceivedByte);
}
Example #20
0
/*****************************************************************************
*   函数名      : char COMMON_I2C_Send_Byte(char cByteToSend)
*   功能        :  I2C发送一个字节出去
*   输入参数    : cByteToSend: 要发送的byte
*   输出参数    : none
*   返回值说明  : COMMON_I2C_OK:    ok
                   DD_TS_ERROR: error
*****************************************************************************/
char COMMON_I2C_Send_Byte(char cByteToSend)
{
	char cLoop=0;
	char ack_singal=0;
	
    SCL_L;                  //Reset SCL
    SDA_OUTPUT_MODE;        //Enable SDA output
    for (cLoop=8; cLoop>0; cLoop--)
    {
        if (0x80 == (cByteToSend & 0x80))
        {
            SDA_H;          //Send one to SDA pin
        }
        else
        {                   
            SDA_L;          //Send zero to SDA pin
        }
        I2C_DELAY(100);
        SCL_H;              //Set SCL
        I2C_DELAY(200);
        SCL_L;              //Reset SCL
        I2C_DELAY(100);
        cByteToSend <<= 1;   //Rotate data
    }

    SDA_INPUT_MODE;         //SDA becomes an input for the ACKN
    I2C_DELAY(100);
    SCL_H;                  //Set SCL
    I2C_DELAY(100);
    SDA_INPUT_DATA(ack_singal);  //Check SDA for ACKN

    SCL_L;
    I2C_DELAY(100);


    if (0 == ack_singal)
    {
        return COMMON_I2C_OK;
    }
    else
    {
        return COMMON_I2C_ERROR;
    }
}
Example #21
0
static int EEPROM_EnterWriteMode_write(unsigned int addr, unsigned int length, unsigned char *DATA)
{
    unsigned char	eep_data = 0;

    if(ROM_BYTE_Write(EEPPAG0, 1, (unsigned char) (EEP_WEN | addr)) != TRUE)		// page0 addr write (low addr)
		return -5;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPPAG1, 1, (unsigned char) (EEP_WEN | (addr >> 7))) != TRUE)		// page1 addr write (high addr)
		return -6;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPCOMM, 1, EEP_WENL | EEP_LOAD1) != TRUE)		// load1 command
		return -7;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPCOMM, 1, EEP_WENL | EEP_LOAD2) != TRUE)		// load2 command
		return -8;
	I2C_DELAY();

	if(SMB_Write(0, length, DATA) != TRUE)		// data write
		return -9;

	if(ROM_BYTE_Write(EEPCOMM, 1, EEP_WENL | EEP_ERPRG) != TRUE)		// erase & programming
		return -10;
	I2C_DELAY();

	do{
		if(ROM_BYTE_Read(EEPCFG, 1, &eep_data) != TRUE)				// check ready bit
			return -11;
		I2C_DELAY();
	}while((eep_data & 0x01) != EEPRDY);

    if(ROM_BYTE_Write(EEPCOMM, 1, EEP_WENL | EEP_STNBY) != TRUE)		// stnby mode
		return -12;
	I2C_DELAY();

	return TRUE;
}
Example #22
0
unsigned char I2C_Read( unsigned char ack )
{
	unsigned char res = 0;
	char i;

	for (i=0;i<8;i++)
	{
		res <<= 1;
		res |= I2C_ReadBit();
	}

	if ( ack > 0)
	{
		I2C_WriteBit( 0 );
	}
	else
	{
		I2C_WriteBit( 1 );
	}

	I2C_DELAY();

	return res;
}
Example #23
0
void TwoWire::set_sda(bool state) {
    I2C_DELAY(this->i2c_delay);
    digitalWrite(this->sda_pin, state);
}
Example #24
0
SDWORD TestCpld(void)
{
    BYTE byData[5] = {0};
    BYTE bySlave = 0xca;
    BYTE i2c_br_ctrl = 0; /* I2C read control */
    BYTE byIicPort = 0;
    
    STATUS semStatus;                           /*i2c同步信号量*/

	if (0==I2Cisinit)
	{
		dev_I2cInit( );
	}
    semStatus = semTake(semI2cLock, WAIT_FOREVER); /*获得信号量*/
    
    if(ERROR == semStatus)
    {
        logMsg("i2c semTake ERROR\n", 0, 0, 0, 0, 0, 0);
        return;
    }
    
    if(0 == byIicPort)
    {
        sCurI2cPort.byClkPin = IICCLK0;
        sCurI2cPort.byDataPin = IICDATA0;
    }
    else if(1 == byIicPort)
    {
        sCurI2cPort.byClkPin = IICCLK1;
        sCurI2cPort.byDataPin = IICDATA1;
    }

#ifdef I2C_DEBUG    
/*	printf("\n i2cRead() slave address is 0x%08x, addr: 0x%08x", bySlave, dwAddr);*/
#endif /* I2C_DEBUG */
	i2c_ack_stat=0x0;
    i2c_br_ctrl = bySlave | I2C_READ;
 
	if(1)/*0 == I2cDeviceBusy(bySlave))*/
    {
		i2cStart();
		I2cWriteByte(i2c_br_ctrl);
		ack1=I2cReadBit(); /*read ack from slave*/
		if(ack1!=0)
			i2c_ack_stat |= 0x3; 
        I2C_DELAY(CLOCK_LOW_TIME*3);
		byData[0] = I2cReadByte();
        I2cWriteBit(0x0); /*send ack*/
        I2cData(HIGH);
        byData[1] = I2cReadByte();
        I2cWriteBit(0x0); /*send ack*/
        I2cData(HIGH);
        byData[2] = I2cReadByte();
        I2cWriteBit(0x00); /*send ack*/
        I2cData(HIGH);
        byData[3] = I2cReadByte();
        I2cWriteBit(0x00); /*send ack*/
        I2cData(HIGH);
        byData[4] = I2cReadByte();
        I2cWriteBit(0x80); /*send ack*/
        I2cData(HIGH);
		I2cStop();
    }

#ifdef I2C_DEBUG 
	if(i2c_ack_stat!=0)
		printf("\nError in read, ack not recd, i2c_ack_stat=0x%x\n",i2c_ack_stat);
#endif
    printf("\Data[0] = 0x%2x, Data[1] = 0x%2x, Data[2] = 0x%2d, Data[3] = 0x%2d, Data[4] = 0x%2d\n", byData[0], byData[1], byData[2], byData[3], byData[4]);

    semGive(semI2cLock);    /*释放信号量*/

    if(0 != i2c_ack_stat)
    {
	    return SYS_ERROR;
    }
    return SYS_OK;
}
Example #25
0
File: i2c.c Project: 211217613/uhd
static bool _i2c_stop_ex(io_pin_t sda, io_pin_t scl, bool pull_up)
{
	// Assumes:
	//	SCL is output & LOW
	//	SDA is input (Hi-Z, or pull-up enabled)
	
	// Assuming pull-up already enabled
	//if (pull_up)
	//	io_set_pin(sda);
	
	bool result = true;
	
	// SDA should be HIGH after ACK has been clocked away
//	bool skip_drive = false;
	uint8_t retries = 0;
	while (io_test_pin(sda) == false)
	{
		if (retries == I2C_DEFAULT_MAX_ACK_RETRIES)
		{
			debug_log_ex("I2C:STP ", false);
			debug_log_hex(sda);
			debug_blink_rev(4);
			
//			skip_drive = true;
			result = false;
			break;	// SDA is being held low?!
		}

		++retries;
		I2C_DELAY(I2C_DEFAULT_RETRY_DELAY);
	}
	
	// STOP condition
//	if ((pull_up == false) || (skip_drive))
		io_clear_pin(sda);	// Don't tri-state if internal pull-up is used
//	//else
//	// Pin will now be driven, but having checked SDA is HIGH above means slave's SDA should be Open Collector (i.e. it won't blow up)
	io_output_pin(sda);	// Drive LOW
//	if (pull_up)
//		io_clear_pin(sda);

	///////////////////////////////////
	
//	if (pull_up)
//		io_set_pin(scl);	// Don't tri-state if internal pull-up is used. Line will be driven, but assuming this is the only master on the clock line (i.e. no one else will pull it low).
	io_input_pin(scl);
	if (pull_up)
		io_set_pin(scl);
	I2C_DELAY(I2C_DEFAULT_STOP_TIME);
	
	///////////////////////////////////

//	if ((pull_up) && (skip_drive == false))
//		io_set_pin(sda);	// Don't tri-state if internal pull-up is used
	io_input_pin(sda);
//	if ((pull_up) && (skip_drive))
		io_set_pin(sda);
	I2C_DELAY(I2C_DEFAULT_BUS_FREE_TIME);
	
	return result;
}
Example #26
0
File: i2c.c Project: 211217613/uhd
bool i2c_read2_ex(io_pin_t sda, io_pin_t scl, uint8_t addr, uint8_t subaddr, uint8_t* value, bool pull_up)
{
	if (_i2c_start_ex(sda, scl, pull_up) == false)
		return false;

	if (_i2c_write_byte_ex(sda, scl, addr & ~0x01, pull_up) == false)
	{
#ifdef I2C_EXTRA_DEBUGGING
		//debug_log_ex("R21:", false);
		debug_log("R21");
		//debug_log_hex(addr);
#endif // I2C_EXTRA_DEBUGGING
		goto i2c_read2_fail;
	}

	if (_i2c_write_byte_ex(sda, scl, subaddr, pull_up) == false)
	{
#ifdef I2C_EXTRA_DEBUGGING
		//debug_log_ex("R22:", false);
		debug_log("R22");
		//debug_log_hex(subaddr);
#endif // I2C_EXTRA_DEBUGGING
		goto i2c_read2_fail;
	}
	
	io_input_pin(scl);
	if (pull_up)
		io_set_pin(scl);
	I2C_DELAY(I2C_DEFAULT_BUS_WAIT);
	
	if (_i2c_start_ex(sda, scl, pull_up) == false)
	{
		return false;
	}
	
	if (_i2c_write_byte_ex(sda, scl, addr | 0x01, pull_up) == false)
	{
#ifdef I2C_EXTRA_DEBUGGING
		//debug_log_ex("R23:", false);
		debug_log("R23");
		//debug_log_hex(addr);
#endif // I2C_EXTRA_DEBUGGING
		goto i2c_read2_fail;
	}

	if (_i2c_read_byte_ex(sda, scl, value, pull_up) == false)
	{
#ifdef I2C_EXTRA_DEBUGGING
		//debug_log_ex("R24:", false);
		debug_log("R24");
		//debug_log_hex(*value);
#endif // I2C_EXTRA_DEBUGGING
		goto i2c_read2_fail;
	}
	
	if (_i2c_stop_ex(sda, scl, pull_up) == false)
	{
#ifdef I2C_EXTRA_DEBUGGING
		debug_log("R25");
#endif // I2C_EXTRA_DEBUGGING
	}

	return true;
i2c_read2_fail:
	_i2c_abort_ex(sda, scl, pull_up);
	return false;
}
Example #27
0
SDWORD I2cReadTest(BYTE byIicPort, BYTE bySlave, DWORD dwAddr)
{
	BYTE byData = 0;
    volatile BYTE i2c_bw_ctrl; /* I2C write control */
    volatile BYTE i2c_br_ctrl; /* I2C read control */
    volatile BYTE i2c_addr_00; /* I2C address (0) */

    STATUS semStatus;                           /*i2c同步信号量*/

    semStatus = semTake(semI2cLock, WAIT_FOREVER); /*获得信号量*/
    
    if(ERROR == semStatus)
    {
        logMsg("i2c semTake ERROR\n", 0, 0, 0, 0, 0, 0);
        return;
    }
    
    if(0 == byIicPort)
    {
        sCurI2cPort.byClkPin = IICCLK0;
        sCurI2cPort.byDataPin = IICDATA0;
    }
    else if(1 == byIicPort)
    {
        sCurI2cPort.byClkPin = IICCLK1;
        sCurI2cPort.byDataPin = IICDATA1;
    }

#ifdef I2C_DEBUG    
	printf("\n i2cRead() slave address is 0x%08x, addr: 0x%08x", bySlave, dwAddr);
#endif /* I2C_DEBUG */
	i2c_ack_stat=0x0;
    i2c_bw_ctrl = bySlave | I2C_WRITE;
    i2c_br_ctrl = bySlave | I2C_READ;
    /*i2c_addr_01 = I2C_W1_ADDR(addr);*/
    i2c_addr_00 = I2C_W0_ADDR(dwAddr);

	if(1)
    {
		i2cStart();
		I2cWriteByte(i2c_bw_ctrl);
		ack1=I2cReadBit(); /*read ack from slave*/
		if(ack1!=0)
			i2c_ack_stat |= 0x1; 
        I2C_DELAY(CLOCK_LOW_TIME*3);
		/*I2cWriteByte(i2c_addr_01);*/
		/*ack1=I2cReadBit();*/ /*read ack from slave*/
		/*if(ack1!=0)
			i2c_ack_stat |= 0x2; */
		I2cWriteByte(i2c_addr_00);
		ack1=I2cReadBit(); /*read ack from slave*/
		if(ack1!=0)
			i2c_ack_stat |= 0x2; 
		i2cStart();
        I2C_DELAY(CLOCK_LOW_TIME*3);
		I2cWriteByte(i2c_br_ctrl);
		ack1=I2cReadBit(); /*read ack from slave*/
		if(ack1!=0)
			i2c_ack_stat |= 0x3; 
        I2C_DELAY(CLOCK_LOW_TIME*3);
		byData = I2cReadByte();

		I2cStop();
    }

#ifdef I2C_DEBUG 
	if(i2c_ack_stat!=0)
		printf("\nError in read, ack not recd, i2c_ack_stat=0x%x\n",i2c_ack_stat);
#endif
	printf("byData: %x \n", byData);
    semGive(semI2cLock);    /*释放信号量*/

    if(0 != i2c_ack_stat)
    {
	    return SYS_ERROR;
    }
    return SYS_OK;
}
Example #28
0
File: i2c.c Project: 211217613/uhd
/*
static void _i2c_abort(io_pin_t sda, io_pin_t scl)
{
	_i2c_abort_ex(sda, scl, false);
}
*/
static bool _i2c_write_byte_ex(io_pin_t sda, io_pin_t scl, uint8_t value, bool pull_up)
{
    // Assumes:
    //  SDA output is LOW
    //  SCL output is LOW

    for (uint8_t i = 0; i < 8; ++i)
    {
		bool b = ((value & (0x01 << (7 - i))) != 0x00);	// MSB first
		
		if (b)
		{
			if (pull_up)
			{
//				io_set_pin(sda);	// This is bad (will drive line for a moment), but more stable than letting line float
				io_input_pin(sda);
				io_set_pin(sda);
			}				
			else
				io_input_pin(sda);	// Release HIGH
			
			if (io_test_pin(sda) == false)
			{
				debug_log("I2C:WR ");
				debug_log_hex(sda);
				debug_blink_rev(1);
				return false;
			}			
		}			
		else
		{
			if (pull_up)
			{
//				if (io_is_output(sda))
					io_clear_pin(sda);
//				else
//				{
					io_output_pin(sda);	// [This is bad (will drive line for a moment), but more stable than letting line float]
//					io_clear_pin(sda);
//				}
			}
			else
			{
				io_enable_pin(sda, false);
				io_output_pin(sda);	// Drive LOW
			}				
		}
		
		///////////////////////////////

        io_input_pin(scl);	// Release HIGH
		if (pull_up)
			io_set_pin(scl);
        I2C_DELAY(I2C_DEFAULT_SCL_HIGH_PERIOD);
#ifdef I2C_ALLOW_CLOCK_STRETCH
		uint8_t retries = I2C_DEFAULT_MAX_BUS_RETRIES;
		while (io_test_pin(scl) == false)	// Clock stretch requested?
		{
			I2C_DELAY(I2C_DEFAULT_BUS_WAIT);
			if (--retries == 0)
			{
				io_input_pin(sda);	// Release HIGH
				if (pull_up)
					io_set_pin(sda);
				
				debug_log_ex("I2C:STRTCH ", false);
				debug_log_hex(scl);
				debug_blink_rev(2);
				return false;
			}
		}
#endif // I2C_ALLOW_CLOCK_STRETCH
		if (pull_up)
			io_clear_pin(scl);
        io_output_pin(scl);	// Drive LOW
        I2C_DELAY(I2C_DEFAULT_SCL_LOW_PERIOD);
    }

    io_input_pin(sda);	// Release HIGH
	if (pull_up)
		io_set_pin(sda);	// Assuming letting line float won't confuse slave when pulling line LOW for ACK
    I2C_DELAY(I2C_DEFAULT_SCL_HIGH_PERIOD);

    uint8_t retries = 0;
    while ((_i2c_disable_ack_check == false) && (io_test_pin(sda)))
    {
        if (retries == I2C_DEFAULT_MAX_ACK_RETRIES)
		{
			debug_log_ex("I2C:ACK ", false);
			debug_log_hex_ex(sda, false);
			debug_log_hex(value);
			debug_blink_rev(3);
            return false;	// Will abort and not release bus - done by caller
		}

        ++retries;
        I2C_DELAY(I2C_DEFAULT_RETRY_DELAY);
    }

    // Clock away acknowledge
//	if (pull_up)
//		io_set_pin(scl);
    io_input_pin(scl);	// Release HIGH
	if (pull_up)
		io_set_pin(scl);
    I2C_DELAY(I2C_DEFAULT_SCL_HIGH_PERIOD);

	if (pull_up)
		io_clear_pin(scl);
    io_output_pin(scl);	// Drive LOW
//	if (pull_up)
//		io_clear_pin(scl);
    I2C_DELAY(I2C_DEFAULT_SCL_LOW_PERIOD);

    return true;
}
Example #29
0
int EEPROM_EraseAll()
{
	int	addr = 0;
	unsigned char eep_data = 0;

	if(ROM_BYTE_Write(EEPPROT, 1, 0x9F) != TRUE)		// Protection register UNLOCK!
		return -1;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPCOMM, 1, EEP_WENL | EEP_STNBY) != TRUE)		// stnby mode (added by kyi)
		return -2;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPMODE, 1, EEP_WEN | ALL_PAGE_ERASE) != TRUE)		// eeprom main mode access
		return -3;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPCFG, 1, EEP_WEN | AUTOADR) != TRUE)		// eeprom main mode access
		return -3;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPPAG0, 1, (unsigned char) (EEP_WEN | 0)) != TRUE)		// eeprom main mode access
		return -4;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPPAG1, 1, (unsigned char) (EEP_WEN | (0 >> 7))) != TRUE)		// eeprom main mode access
		return -5;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPCOMM, 1, EEP_WENL | EEP_LOAD1) != TRUE)		// eeprom main mode access
		return -6;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPCOMM, 1, EEP_WENL | EEP_LOAD2) != TRUE)		// eeprom main mode access
		return -7;
	I2C_DELAY();

	if(SMB_Write(0, EEP_PAGE_SIZE, &eep_data) != TRUE)		// data write
		return -8;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPCOMM, 1, EEP_WENL | EEP_ERPRG) != TRUE)		// eeprom main mode access
		return -9;
	I2C_DELAY();

    do
    {
        if(ROM_BYTE_Read(EEPCFG, 1, &eep_data) != TRUE)		// eeprom main mode access
            return -10;
        I2C_DELAY();
    }
    while((eep_data & 0x01) != EEPRDY);


    if(ROM_BYTE_Write(EEPCOMM, 1, EEP_WENL | EEP_STNBY) != TRUE)		// eeprom main mode access
		return -11;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPMODE, 1, EEP_WEN | MAIN_MODE) != TRUE)		// eeprom main mode access
		return -12;
	I2C_DELAY();

	if(ROM_BYTE_Write(EEPPROT, 1, EEP_WEN | 0x00) != TRUE)		// protection register LCOK!
		return -13;
	I2C_DELAY();

	return TRUE;
}
Example #30
0
void TKey_Firmware_Update(void)
{
	printk("[Touchkey] Tkey_Firmware_Update +++++\n");

    int rtn;
    unsigned int i;
    unsigned int fwsize, NumPages;
    unsigned char  *firmware_data = NTS_firmware;

    // Step 1: Enter Test mode to access EEPROM...
    TestMode_IN();

    mdelay(200);//Delay_ms(200);

    // Step 1-1: Set NTS System clock to be more faster abt 2.5Mhz.
    if (NTS_MOSC_Set()==TRUE)
    {
        rtn = EEPROM_EraseAll();
    	printk("[Touchkey] EEPROM_EraseAll - rtn : %d\n", rtn);        
        if (rtn==TRUE)
        {
            // Enter Write Mode
            rtn=EEPROM_EnterWriteMode();
        }
    	printk("[Touchkey] EEPROM_EnterWriteMode - rtn : %d\n", rtn);                

        if (rtn==TRUE)
        {
            fwsize =  *firmware_data++ << 8;
            fwsize += *firmware_data++;

        	printk("[Touchkey] FW_SIZE = 0x%x\n", fwsize);                                            
            NumPages = fwsize / EEP_PAGE_SIZE;
        	printk("[Touchkey] Num_Page = 0x%x\n", NumPages);                                                        
            for(i=0;i<NumPages;i++)
            {
                rtn = EEPROM_EnterWriteMode_write(i, EEP_PAGE_SIZE, firmware_data);
				printk("[Touchkey] Firmware write data 0x%x, size %d \n",firmware_data,EEP_PAGE_SIZE,0);	                
                firmware_data += EEP_PAGE_SIZE;
            }

            if ((rtn==TRUE) && (fwsize % EEP_PAGE_SIZE))
            {
                rtn = EEPROM_EnterWriteMode_write(i, fwsize % EEP_PAGE_SIZE, firmware_data);
				printk("[Touchkey] Firmware write data 0x%x, size %d \n",firmware_data, fwsize % EEP_PAGE_SIZE,0);	                
            }
        }

        if (rtn==TRUE)
        {
            // protection register LCOK!
            ROM_BYTE_Write(EEPMODE, 1, EEP_WEN | MAIN_MODE);
            I2C_DELAY();
            ROM_BYTE_Write(EEPPROT, 1, EEP_WEN | 0x00);
            I2C_DELAY();
        }
    }
    else
    {
        //예외처리

    }

    // Step 3: Exit Test Mode
    TestMode_OUT();

	printk("[Touchkey] Tkey_Firmware_Update -----\n");    
}