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

📄 i2c_gpio.c

📁 This file contains routines to write to and read from the I2C bus using the GPIO pins of the CS5530
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
//----------------------------------------------------------------------
//
//	void SendI2CStop(void)
//	
//	Sends an I2C stop signal on the bus.
//	
//----------------------------------------------------------------------
*/
void
SendI2CStop(void)
{
   I2CAL_output_data(0);
   I2CAL_output_clock(1);
   I2CAL_output_data(1);
}

/*
//----------------------------------------------------------------------
//
//	void SendI2CAck(void)
//	
//	Sends the Ack signal on the I2C bus.
//	
//----------------------------------------------------------------------
*/
void
SendI2CAck(void)
{
   I2CAL_output_data(0);
   I2CAL_output_clock(1);
   I2CAL_output_clock(0);
}

/*
//----------------------------------------------------------------------
//
//	void SendI2CNack(void)
//	
//	Sends the Nt-Ack signal on the I2C bus.
//	
//----------------------------------------------------------------------
*/
void
SendI2CNack(void)
{
   I2CAL_output_data(1);
   I2CAL_output_clock(1);
   I2CAL_output_clock(0);
}

/*
//----------------------------------------------------------------------
//
//	UInt8 SendI2CData( UInt8 inData )
//	
//	Sends a byte of data on the I2C bus and returns the TRUE if the slave ACK'd
//	the data.
//	
//	Input:	inData			- the byte of data to send          
//	Output:	(return)		- TRUE (1) if ACK was received, FALSE (0) if not
//	
//----------------------------------------------------------------------
*/
void
SendI2CData(unsigned char inData)
{
   unsigned char bit;

   /* Send all 8 bits of data byte, MSB to LSB */
   for (bit = 0x80; bit != 0; bit >>= 1) {
      if (inData & bit)
	 I2CAL_output_data(1);
      else
	 I2CAL_output_data(0);

      I2CAL_output_clock(1);
      I2CAL_output_clock(0);
   }
}

/*
//----------------------------------------------------------------------
//
//	UInt8 ReceiveI2CAck(  )
//	
//	Receives the Ack (or Nack) from the slave. 
//	
//	Output:	(return) - TRUE (1) if ACK was received, FALSE (0) if not
//	
//----------------------------------------------------------------------
*/
unsigned char
ReceiveI2CAck(void)
{
   unsigned char bit;

   /* Test for Ack/Nack */
   I2CAL_set_data_for_input();
   I2CAL_output_data(1);
   I2CAL_output_clock(1);
   bit = I2CAL_input_data();
   I2CAL_output_clock(0);
   I2CAL_set_data_for_output();
   return !bit;
}

/*	
//----------------------------------------------------------------------
//
//	unsigned char ReceiveI2CData(void)
//	
//	Receives a byte of data from the I2C bus.
//	
//	Output:	(return) - The data byte recehved from the bus
//	
//----------------------------------------------------------------------
*/
unsigned char
ReceiveI2CData(void)
{
   unsigned char data = 0;
   unsigned char x;

   /* make sure the data line is released */
   I2CAL_set_data_for_input();
   I2CAL_output_data(1);

   /* shift in the data */
   for (x = 0; x < 8; x++) {
      /* shift the data left   */
      I2CAL_output_clock(1);
      data <<= 1;
      data |= I2CAL_input_data();
      I2CAL_output_clock(0);
   }

   I2CAL_set_data_for_output();
   I2CAL_output_data(1);
   return data;
}

/*
//----------------------------------------------------------------------
//
//	void I2C_init(void)
//	
//	This routine initializes the I2C interface. Clients of the I2C.c
//	will call this routine before calling any other routine in the I2C.c
//	
//----------------------------------------------------------------------
*/

#if GFX_I2C_DYNAMIC
int
gpio_i2c_init(void)
#else
int
gfx_i2c_init(void)
#endif
{
   int errc;

   /* init I2CAL */
   errc = I2CAL_init();
   if (errc)
      return errc;

   /* set the clock and data lines to the proper states */
   I2CAL_output_clock(1);
   I2CAL_output_data(1);
   I2CAL_set_data_for_output();

   SendI2CStart();
   SendI2CStop();
   SendI2CStop();

   g_initialized = 1;

   return 0;
}

/*
//----------------------------------------------------------------------
//	
//	void I2C_cleanup(void)
//	
//	This routine disables the I2C interface. Clients of the I2C.c will not
//	call any other I2C routine after calling this routine.
//	
//----------------------------------------------------------------------
*/

#if GFX_I2C_DYNAMIC
void
gpio_i2c_cleanup(void)
#else
void
gfx_i2c_cleanup(void)
#endif
{
   if (g_initialized) {

      /* set the clock and data lines to a harmless state */
      I2CAL_output_clock(1);
      I2CAL_output_data(1);

      g_initialized = 0;
   }

   I2CAL_cleanup();
}

int
I2CAL_init(void)
{
   unsigned long l_reg;
   unsigned short reg;

   /* initialize the i2c port. */
   l_reg = gfx_pci_config_read(CS5530_GPIO);

   if (l_reg != 0x01001078)
      return 1;

   l_reg = gfx_pci_config_read(CS5530_GPIO);
   reg = (unsigned short)l_reg;

   /* both outputs, both high. */
   reg |= (SDADIR | SCLDIR | SDA | SCL);
   l_reg = reg;
   gfx_pci_config_write(CS5530_GPIO, l_reg);

   g_initialized = 1;

   return 0;
}

void
I2CAL_cleanup(void)
{
   if (g_initialized) {

      g_initialized = 0;
   }
}

/*
//--------------------------------------------------------------------------------
//
//	set the I2C clock line state
//
//--------------------------------------------------------------------------------
*/
void
I2CAL_output_clock(int inState)
{
   unsigned short reg;
   unsigned long value;

   value = gfx_pci_config_read(CS5530_GPIO);
   reg = (unsigned short)value;

   if (inState) {			/* write a 1. */
      reg |= SCL;
   } else {				/* write a 0. */
      reg &= ~SCL;
   }

   value = reg;
   gfx_pci_config_write(CS5530_GPIO, value);

   /* hold it for a minimum of 4.7us */
   gfx_delay_microseconds(5);
}

/*
//--------------------------------------------------------------------------------
//
//	set the I2C data line state
//
//--------------------------------------------------------------------------------
*/
void
I2CAL_output_data(int inState)
{
   unsigned short reg;
   unsigned long value;

   value = gfx_pci_config_read(CS5530_GPIO);
   reg = (unsigned short)value;

   if (inState) {			/* write a 1. */
      reg |= SDA;
   } else {
      /* write a 0. */
      reg &= ~SDA;
   }
   value = reg;
   gfx_pci_config_write(CS5530_GPIO, value);

   /* 250 ns setup time */
   gfx_delay_microseconds(1);
}

/*
//--------------------------------------------------------------------------------
//
//	read the state of the data line
//
//--------------------------------------------------------------------------------
*/
unsigned char
I2CAL_input_data(void)
{
   unsigned short reg;
   unsigned long value;

   value = gfx_pci_config_read(CS5530_GPIO);
   reg = (unsigned short)value;

   if (reg & SDA)
      return 1;
   else
      return 0;
}

/*
//--------------------------------------------------------------------------------
//
//	set the I2C data for input mode
//
//--------------------------------------------------------------------------------
*/
void
I2CAL_set_data_for_input(void)
{
   unsigned short reg;
   unsigned long value;

   value = gfx_pci_config_read(CS5530_GPIO);
   reg = (unsigned short)value;

   reg &= ~SDADIR;

   value = reg;

   gfx_pci_config_write(CS5530_GPIO, value);
}

/*
//--------------------------------------------------------------------------------
//
//	set the I2C data for output mode
//
//--------------------------------------------------------------------------------
*/
void
I2CAL_set_data_for_output(void)
{
   unsigned short reg;
   unsigned long value;

   value = gfx_pci_config_read(CS5530_GPIO);
   reg = (unsigned short)value;
   reg |= SDADIR;
   value = reg;

   gfx_pci_config_write(CS5530_GPIO, value);

}

/* END OF FILE */

 

⌨️ 快捷键说明

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