Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
14 years ago

Problem with i2c and fpga

Hi,

I'm using an Altera Cyclone ll as fpga with a nios to communicate with a sfr08 sensor.

I downloaded the opencore IP found in the wiki but i have a problem with it.

I integrated it in the SOPC builder and generated the processor without problem, also the bdf file compiles without errors.

I read the documentation but i'm not able to communicate with the internal registers of the module, probably due to a wrong memory address.

Here is the code i'm using:


//the i2c module in the sopc has the address 0x00101000;
volatile int * red_LED_ptr = (int *) 0x00101040; // red LED address
int * Control = (int *) 0x00101002;
int * Send = (int *) 0x00101003;
int * Command = (int *) 0x00101004;
volatile int * Receive = (int *)0x00101003;
volatile int * Status = (int *)0x00101004;
int main(void)
{
  int stato;
  *(Control) = 0x80;                 //enable the i2c core 
  * (Command) = 0x90;             //set STA bit and WR bit
  * (Send) = 0xa0;                   //device address with last bit 0 (write)
  do{
     stato = * (Status);}            //check status register for the TIP bit
  while ((stato & 0x02 )!= 0x00); // TIP == 0 --> trasimssion complete
  if ((stato & 0x80) == 0x00){     // if ack send register address
     * (Send) = 0x00;                 // register address
     * (Command) = 0x10;           //set STA bit and WR bit
  }
  do{                                     
     stato = * (Status);}            // TIP control as before
  while ((stato & 0x02 )!= 0x00) ;
  if ((stato & 0x80) == 0x00){    //if ack  send data
     * (Send) = 0x51;                //data to write
     * (Command) = 0x50;           //set STA bit and WR bit
  }
  do{
     stato = * (Status);}           // TIP control as before
  while ((stato & 0x02 )!= 0x00) ;
  *(red_LED_ptr)= stato;          // put the status register (with ack bit) on led
}
Any suggestion will be appreciate, thanks

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    You don't need to set pointers to component addresses. Those addresses are defined in system.h file, so use them instead. Check the core examples, they're very informative.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thanks for the suggestion, i should think to it by myself.

    Even if i can't find the file system.h i was able compile the code without it and now it works.

    Thanks again.