Quantcast
Channel: Altera Forums
Viewing all articles
Browse latest Browse all 19390

opencoreI2C for cyclone3

$
0
0
Hi I am implementing opencoreI2C, can some one double check my code. I am new to opencoreso I am having problems in figuring out scl and sda.

Here is my code

#include <stdio.h>
#include "alt_types.h"
#include "opencores_i2c_regs.h"
#include "opencores_i2c.h"


int main()
{
/* IOWR_OPENCORES_I2C_CTR(0x0, 0xA); /* turn off the core*/
//IORD_OPENCORES_I2C_CTR(0x0); /* turn off the core*/
/*
printf("IORD: %x",IORD_OPENCORES_I2C_CTR(0x0));


return 0;
}
*/
#define I2C_BASE 0x0
#define clk ALT_CPU_FREQ
#define speed 100000






//I2C_init(alt_u32 base, alt_u32 clk, alt_u32 speed);
// ++++++++++++++++++++++++ initialize ++++++++++++++++++++++++++++++++++++


//IOWR_OPENCORES_I2C_CTR(I2C_BASE,0); // turn off the core
//int i = 100;
//while (i!=0){
//printf("CTR %x at base %x \n",IORD_OPENCORES_I2C_CTR(I2C_BASE),I2C_BASE);
//--i;
//}
#define I2C_DEBUG
//int I2C_init(alt_u32 base,alt_u32 clk, alt_u32 speed)
//int I2C_start(alt_u32 base, alt_u32 add, alt_u32 write);
//alt_u32 I2C_read(alt_u32 base);
//int I2C_write(alt_u32 base, alt_u8 data);
//int I2C_stop(alt_u32 base);


/* these functions are polled only. */
/* all functions wait until the I2C is done before exiting */




/************************************************** **************
int I2C_init
This function inititlizes the prescalor for the scl
and then enables the core. This must be run before
any other i2c code is executed
inputs
base = the base address of the component
clk = freuqency of the clock driving this component ( in Hz)
speed = SCL speed ie 100K, 400K ... (in Hz)
15-OCT-07 initial release
************************************************** ***************/






IOWR_OPENCORES_I2C_CR(I2C_BASE, OPENCORES_I2C_CR_IACK_MSK); // clearn any pening IRQ
IOWR_OPENCORES_I2C_PRERLO(I2C_BASE,0x31); // load low presacle bit
IOWR_OPENCORES_I2C_PRERHI(I2C_BASE,0x00); // load upper prescale bit


IOWR_OPENCORES_I2C_CTR(I2C_BASE,0x80); // turn on the core
printf("CTR %x \n",IORD_OPENCORES_I2C_CTR(I2C_BASE));


//++++++++++++++++++++++++ send slave address and verify ++++++++++++++++++


IOWR_OPENCORES_I2C_TXR (I2C_BASE, 0x3A);
IOWR_OPENCORES_I2C_CR(I2C_BASE, 0x90);






while( IORD_OPENCORES_I2C_SR(I2C_BASE) & OPENCORES_I2C_SR_TIP_MSK)




{printf( "TIP 1\n");
//while(( IORD_OPENCORES_I2C_SR(I2C_BASE) & 0x02) == 0x02)
//{printf( "TIP 1 & SR = %x \n",IORD_OPENCORES_I2C_SR(I2C_BASE));}


printf( "SR = %x \n",IORD_OPENCORES_I2C_SR(I2C_BASE));


while((IORD_OPENCORES_I2C_SR(I2C_BASE) & 0x80) == 0x80)
{printf("NO ACK 1\n");
IOWR_OPENCORES_I2C_TXR (I2C_BASE, 0x3A);
IOWR_OPENCORES_I2C_CR(I2C_BASE, 0x80);
while(( IORD_OPENCORES_I2C_SR(I2C_BASE) & 0x02) == 0x02)
printf( "TIP 1 & SR = %x \n",IORD_OPENCORES_I2C_SR(I2C_BASE));




printf("hi");


//else //++++++++++++++ send slave register address to be read from ++++++++++
printf("ACK 1\n");
printf( "SR = %x \n",IORD_OPENCORES_I2C_SR(I2C_BASE));
IOWR_OPENCORES_I2C_TXR(I2C_BASE, 0x00);


IOWR_OPENCORES_I2C_CR(I2C_BASE, 0x90);


while( (IORD_OPENCORES_I2C_SR(I2C_BASE) & 0x02) == 0x02 )
{printf( "TIP 2 \n");}


if((IORD_OPENCORES_I2C_SR(I2C_BASE) & 0x80) == 0x80)
printf("\t NO ACK 2\n");
else{
IOWR_OPENCORES_I2C_TXR(I2C_BASE, 0x3B);


IOWR_OPENCORES_I2C_CR(I2C_BASE, 0x90);


{printf( "TIP 3 \n");}


if((IORD_OPENCORES_I2C_SR(I2C_BASE) & 0x80) == 0x80)
printf("\t NO ACK 3\n");
else{
alt_u32 x = IORD_OPENCORES_I2C_RXR(I2C_BASE);
printf("Received Byte %x \n",x);
IOWR_OPENCORES_I2C_CR(I2C_BASE, 0x08);
IOWR_OPENCORES_I2C_CR(I2C_BASE, 0x08);
IOWR_OPENCORES_I2C_CTR(I2C_BASE,0x00);
}
}




}
return 0;
}


}

Viewing all articles
Browse latest Browse all 19390

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>