int a[2]={2,2}; int b[2]={0}; int d[2]={0}; int e[8]={0}; int j,h; int c[2]={0}; void I2CWaitTIP(alt_u32 base);
/********************************************************************************************************* ** Function name: InitI2C ** ** Descriptions: Initialize the I2C Open Core. The frequency of SCL is set as freq ** Interrupt will be or not be enabled by the IEN ** ** input parameters: base: The base address of I2C Core; ** freq: The frequency of SCL we want ** IEN : When the IEN is 1, interrupt will be enabled; ** When the IEN is NOT 1, interrupt will be disabled. ** ** Returned value: None ** ** Used global variables: None ** Calling modules: None ** ** Created by: Jing.Zhang ** Created Date: 2005/09/30 **------------------------------------------------------------------------------------------------------- ** Modified by: ** Modified date: **------------------------------------------------------------------------------------------------------ ********************************************************************************************************/ void InitI2C(alt_u32 base, alt_u32 freq, alt_u8 IEN) { alt_u32 prescale; // Calculate the prescale value prescale = ALT_CPU_FREQ/((freq<<2) + freq); // Setup prescaler for the freq of SCL with sysclk of ALT_CPU_FREQ IOWR_OC_I2C_PRERLO(base, prescale & 0xff); IOWR_OC_I2C_PRERHI(base,(prescale & 0xff00)>>8); // Enable core if(IEN == 1) // Enable interrupt { IOWR_OC_I2C_CTR(base, 0xC0); } else // Enable core while disable interrupt { IOWR_OC_I2C_CTR(base, 0x80); } }
/********************************************************************************************************* ** Function name: I2CWaitTIP ** ** Descriptions: Wait for the completion of transfer. ** ** input parameters: base: The base address of I2C Core; ** ** ** Returned value: None ** ** Used global variables: None ** Calling modules: None ** ** Created by: Jing.Zhang ** Created Date: 2005/09/30 **------------------------------------------------------------------------------------------------------- ** Modified by: ** Modified date: **------------------------------------------------------------------------------------------------------ ********************************************************************************************************/ void I2CWaitTIP(alt_u32 base) { while (IORD_OC_I2C_SR(base) & I2C_SR_TIP) {} }
/********************************************************************************************************* ** Function name: I2CWrite ** ** Descriptions: Write num bytes data to slave device by I2C bus. ** ** input parameters: base: The base address of I2C Core; ** address: The address of I2C slave device; ** reg: The register of I2C slave device; ** buf: The pointer to data buffer; ** num: The num bytes of data which will be written ** ** Returned value: None ** ** Used global variables: None ** Calling modules: None ** ** Created by: Jing.Zhang ** Created Date: 2005/09/30 **------------------------------------------------------------------------------------------------------- ** Modified by: ** Modified date: **------------------------------------------------------------------------------------------------------ ********************************************************************************************************/ void I2CWrite(alt_u32 base, alt_u8 address, alt_u8 reg, alt_u8 *buf, alt_u16 num) { alt_u16 i,tmp; // Wait for the completion of transfer I2CWaitTIP(base);
// write data with STOP signal IOWR_OC_I2C_TXR(base, *buf); IOWR_OC_I2C_CR(base, I2C_CR_WR | I2C_CR_STO); I2CWaitTIP(base); }
/********************************************************************************************************* ** Function name: I2CRead ** ** Descriptions: Read num bytes data from slave device by I2C bus. ** ** input parameters: base: The base address of I2C Core; ** address: The address of I2C slave device; ** reg: The register of I2C slave device; ** buf: The pointer to data buffer; ** num: The num bytes of data which will be written ** ** Returned value: None ** ** Used global variables: None ** Calling modules: None ** ** Created by: Jing.Zhang ** Created Date: 2005/09/30 **------------------------------------------------------------------------------------------------------- ** Modified by: ** Modified date: **------------------------------------------------------------------------------------------------------ ********************************************************************************************************/ void I2CRead(alt_u32 base, alt_u8 address, alt_u8 reg, alt_u8 *buf, alt_u16 num) { alt_u16 i,tmp; // Wait for the completion of transfer I2CWaitTIP(base);