Beispiel #1
0
/*
 *  This function reads the slave device's register
 *
 *  Parameters:
 *      addr   - i2c Slave device address which register
 *                        to be read from
 *      reg    - Slave device's register to be read
 *
 *  Return Value:
 *      Register value
 */
unsigned char sm750_sw_i2c_read_reg(
	unsigned char addr,
	unsigned char reg
)
{
	unsigned char data;

	/* Send the Start signal */
	sw_i2c_start();

	/* Send the device address */
	sw_i2c_write_byte(addr);

	/* Send the register index */
	sw_i2c_write_byte(reg);

	/* Get the bus again and get the data from the device read address */
	sw_i2c_start();
	sw_i2c_write_byte(addr + 1);
	data = sw_i2c_read_byte(1);

	/* Stop swI2C and release the bus */
	sw_i2c_stop();

	return data;
}
Beispiel #2
0
/*---------------------------------------------------------------------------*/
u08 EEPROM_RandomRead(u16 Address)
// Description:
//   Random Read Operation. Data is read from the EEPROM. The EEPROM
//   address is defined with the parameter Address.
{

	char data_in;
#if (defined I2C_SW &&  I2C_SW == 1)
	sw_i2c_start();							// Send start signal

	sw_i2c_write(EEPROMAddress);// Send identifer I2C address  (10100000b)
	delay_time(I2C_DELAY);
	sw_i2c_write((unsigned char) (high_byte(Address)));// Send address to EEPROM
	delay_time(I2C_DELAY);
	sw_i2c_write((unsigned char) Address);// Send address to EEPROM
	delay_time(I2C_DELAY);
	sw_i2c_start();// Send I2C Start Transfer
	sw_i2c_write(EEPROMAddress + 1);// Send identifer I2C address  (10100001b)
	delay_time(I2C_DELAY);
	data_in = sw_i2c_read(0);// Read byte
	sw_i2c_stop();			 // Send I2C Stop Transfer
#else

	/* Dummy write: Transmite NULL depois gera um sinal de start repeated */
	IIC_transmite(EEPROMAddress, Address, NULL);
	IIC_recebe(EEPROMAddress, &data_in);
	

#endif
	return data_in;

}
Beispiel #3
0
/*---------------------------------------------------------------------------*/
INT8U RTC_RandomRead(INT8U Address)
// Description:
//   Random Read Operation. Data is read from the RTC. The RTC
//   address is defined with the parameter Address.
{

	INT8U data_in;
#if (defined I2C_SW &&  I2C_SW == 1)
	CRITICAL_SECTION_START();
	sw_i2c_start();							// Send start signal

	sw_i2c_write(DS1307Address);		// Send identifer I2C address  (10100000b)
	delay_time(I2C_DELAY);
	sw_i2c_write((INT8U) Address);			// Send address to EEPROM
	delay_time(I2C_DELAY);
	sw_i2c_start();							// Send I2C Start Transfer
	sw_i2c_write(DS1307Address | 1);	// Send identifer I2C address  (10100001b)
	delay_time(I2C_DELAY);
	data_in = sw_i2c_read(0);			// Read byte
	sw_i2c_stop();			 // Send I2C Stop Transfer
	
	CRITICAL_SECTION_END();
#else
	IIC_transmite(DS1307Address, Address, NULL);
	IIC_recebe(DS1307Address, &data_in);
#endif
	return data_in;

}
Beispiel #4
0
/*
 *  This function writes a value to the slave device's register
 *
 *  Parameters:
 *      addr            - i2c Slave device address which register
 *                        to be written
 *      reg             - Slave device's register to be written
 *      data            - Data to be written to the register
 *
 *  Result:
 *          0   - Success
 *         -1   - Fail
 */
long sm750_sw_i2c_write_reg(
	unsigned char addr,
	unsigned char reg,
	unsigned char data
)
{
	long ret = 0;

	/* Send the Start signal */
	sw_i2c_start();

	/* Send the device address and read the data. All should return success
	 * in order for the writing processed to be successful
	 */
	if ((sw_i2c_write_byte(addr) != 0) ||
	    (sw_i2c_write_byte(reg) != 0) ||
	    (sw_i2c_write_byte(data) != 0)) {
		ret = -1;
	}

	/* Stop i2c and release the bus */
	sw_i2c_stop();

	return ret;
}
Beispiel #5
0
void EEPROM_ByteWrite(u16 Address, u08 Data)
// Description:
//   Byte Write Operation. The communication via the I2C bus with an EEPROM
//   (2465) is realized. A data byte is written into a user defined address.
{

	/* i2c por SW */
#if (defined I2C_SW &&  I2C_SW == 1)
	sw_i2c_start();							// Send start signal
	sw_i2c_write(EEPROMAddress & 0b11111110);// Send identifier I2C address     0xA0 = 10100000b
	delay_time(I2C_DELAY);
	sw_i2c_write((unsigned char)(high_byte(Address)));// Send address to EEPROM (high byte address)
	delay_time(I2C_DELAY);
	sw_i2c_write((unsigned char) Address);// Send address to EEPROM  (low byte address) 
	delay_time(I2C_DELAY);
	sw_i2c_write(Data);// Send low byte to EEPROM  (data to write)
	delay_time(I2C_DELAY);// Delay a period of time to write   
	sw_i2c_stop();	   // Send I2C Stop Transfer
	delay_time(20);	   // Delay a period of time to write
	DelayTask(WRITE_CYCLE_TIME);
#else
	/* i2c por HW */

	IIC_transmite(EEPROMAddress, Address, &Data);

#endif
}
int sw_i2c_read(unsigned char chip, unsigned char location, unsigned char* buf, int count)
{
    int i;

#ifdef MROBE_100 /* does not use register addressing */
    (void) location;
#else    
    sw_i2c_start();
    sw_i2c_outb((chip & 0xfe) | SW_I2C_WRITE);
    if (!sw_i2c_getack())
    {
        sw_i2c_stop();
        return -1;
    }

    sw_i2c_outb(location);
    if (!sw_i2c_getack())
    {
        sw_i2c_stop();
        return -2;
    }
#endif  

    sw_i2c_start();
    sw_i2c_outb((chip & 0xfe) | SW_I2C_READ);
    if (!sw_i2c_getack())
    {
        sw_i2c_stop();
        return -3;
    }
    
    for (i=0; i<count-1; i++)
    {
      buf[i] = sw_i2c_inb();
      sw_i2c_ack();
    }

    /* 1byte min */
    buf[i] = sw_i2c_inb();
    sw_i2c_nack();
        
    sw_i2c_stop();
    
    return 0;
}
Beispiel #7
0
/*---------------------------------------------------------------------------*/
u08 EEPROM_CurrentAddressRead(void)
// Description:
//   Current Address Read Operation. Data is read from the EEPROM. The current
//   address from the EEPROM is used.
{

	u08 data_in;

#if (defined I2C_SW &&  I2C_SW == 1)
	sw_i2c_start();							// Send start signal
	sw_i2c_write(EEPROMAddress);// Send identifer I2C address  (10100000b)
	sw_i2c_start();			   // Send I2C Start Transfer
	sw_i2c_write(EEPROMAddress + 1);// Send identifer I2C address  (10100001b)
	data_in = sw_i2c_read(0);// Read byte
	sw_i2c_stop();			 // Send I2C Stop Transfer

#else
	IIC_recebe(EEPROMAddress, &data_in);
#endif
	return data_in;
}
Beispiel #8
0
/*---------------------------------------------------------------------------*/
void RTC_AckPolling(void)
// Description:
//   Acknowledge Polling. The RTC will not acknowledge if a write cycle is
//   in progress. It can be used to determine when a write cycle is completed.
{

	volatile INT32U count = 0x03FF, ack = 0;
	while ((count != 0))
	{

		count--;
		sw_i2c_start();
		if (sw_i2c_write(DS1307Address))
			count = 0;
		sw_i2c_stop();

	}
}
Beispiel #9
0
/*---------------------------------------------------------------------------*/
void EEPROM_AckPolling(void)
// Description:
//   Acknowledge Polling. The EEPROM will not acknowledge if a write cycle is
//   in progress. It can be used to determine when a write cycle is completed.
{

	volatile unsigned int count = 0x03FF, ack = 0;
	while ((count != 0))
	{

		count--;
		sw_i2c_start();
		if (sw_i2c_write(EEPROMAddress))
			count = 0;
		sw_i2c_stop();

	}
}
Beispiel #10
0
INT8U RTC_DS1307_Init(void)
{
#if (defined I2C_SW &&  I2C_SW == 1)
	INT8U ret = NO_I2C_ACK;
	CRITICAL_SECTION_START();
		sw_i2c_init();
	CRITICAL_SECTION_END();
	
	sw_i2c_start();							// Send start signal
	ret = sw_i2c_write(DS1307Address);		// Send identifer I2C address  (10100000b)
	delay_time(I2C_DELAY);
	sw_i2c_stop();	
	
	return ((ret == OK_I2C_ACK) ? TRUE : FALSE);
	
#else
	IIC_init();
#endif

}
Beispiel #11
0
/*---------------------------------------------------------------------------*/
INT8U RTC_CurrentAddressRead(void)
// Description:
//   Current Address Read Operation. Data is read from the RTC. The current
//   address from the RTC is used.
{

	INT8U data_in;

#if (defined I2C_SW &&  I2C_SW == 1)
	CRITICAL_SECTION_START();
	sw_i2c_start();				// Send I2C Start Transfer        				
	sw_i2c_write(DS1307Address | 1);	// Send identifer I2C address  (10100001b)
	data_in = sw_i2c_read(0);				// Read byte
	sw_i2c_stop();				// Send I2C Stop Transfer
	CRITICAL_SECTION_END();
#else
	IIC_recebe(DS1307Address, &data_in);
#endif
	return data_in;

}
Beispiel #12
0
void RTC_ByteWrite(INT8U Address, INT8U Data)
// Description:
//   Byte Write Operation. The communication via the I2C bus with an EEPROM
//   (2465) is realized. A data byte is written into a user defined address.
{

#if (defined I2C_SW &&  I2C_SW == 1)
	CRITICAL_SECTION_START();
	sw_i2c_start();							// Send start signal
	sw_i2c_write(DS1307Address & 0b11111110);// Send identifier I2C address     0xA0 = 10100000b
	delay_time(I2C_DELAY);
	sw_i2c_write((INT8U) Address);// Send address to RTC  (byte address) 
	delay_time(I2C_DELAY);
	sw_i2c_write(Data);					// Send low byte to RTC  (data to write)
	delay_time(I2C_DELAY); // Delay a period of time to write   
	sw_i2c_stop();	   // Send I2C Stop Transfer
	delay_time(20);	   // Delay a period of time to write
	CRITICAL_SECTION_END();
#else
	IIC_transmite(DS1307Address, Address, &Data);
#endif
}
int sw_i2c_write(unsigned char chip, unsigned char location, unsigned char* buf, int count)
{
    int i;

    sw_i2c_start();
    sw_i2c_outb((chip & 0xfe) | SW_I2C_WRITE);
    if (!sw_i2c_getack())
    {
        sw_i2c_stop();
        return -1;
    }

#ifdef MROBE_100 /* does not use register addressing */
    (void) location;
#else    
    sw_i2c_outb(location);
    if (!sw_i2c_getack())
    {
        sw_i2c_stop();
        return -2;
    }
#endif  

    for (i=0; i<count; i++)
    {
        sw_i2c_outb(buf[i]);
        if (!sw_i2c_getack())
        {
            sw_i2c_stop();
            return -3;
        }
    }

    sw_i2c_stop();
    
    return 0;
}