i2c slave interrupt mode

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

i2c slave interrupt mode

1,227 Views
K60EC
Contributor I

Hi all,

Lots of great info here on the furums so thank you to begin.

I have a problem where slave i2c (i2c1 on K60 tower) task hangs application and also does not recieve any data from external master.

 

I was able to verify connections and also able to be a MASTER (tower K60  as master ) on the very same i2c (I2c1)  and send valid messages to an external i2c slave (which worked very well).

 

So, getting back to this case, I am trying to get i2c working as a slave interrupt mode and have the K60 respond as slave to external master.The only output is from the iitialization ("Leaving initialize_i2c1_slave()") and I never get to print any recieved i2c data from external master and further, the rest of the application is hung (there is also the "shell" running on serial port d but is becomes unresponsive/hung)/

 

Any ideas very appreciated!

Thanks!

 

 

This is what I have so far.....

 

#define I2C_RUC_ADDRESS 0x10
#define I2C_TEST_DESTINATION_ADDRESS 0x20
#define I2C1_DATA_SIZE 5



void InitializeI2C_1();
void initialize_i2c1_slave(void);


/* Global Variables */

FILE_PTR fd1;
FILE_PTR fd_i2c_slave;
uchar recv_buffer1[I2C1_DATA_SIZE];

/*
 * Task for Test_i2c1
 */
void Test_i2c1_Task()
{
     uint_32 result;
    pointer fh_ptr;
    fh_ptr = (pointer)fopen("ttyd:", BSP_DEFAULT_IO_OPEN_MODE);
    _io_set_handle(IO_STDOUT, fh_ptr);
    _io_set_handle(IO_STDIN, fh_ptr);
    fflush(stdout);    
  printf("Starting Test_i2c1 Task FOR SLAVE INTERRUPT interface on PORTC i2c#1 JMK\n");

  initialize_i2c1_slave(); //From forum code
 
 
  //while (TRUE)
  //{
      
      // read
      result = 0;
      do
      {
         result += fread (&recv_buffer1, 1, 1, fd_i2c_slave);
      } while (result < 1);

      printf ("....ArdVark test: Slave I2C port C 1 Interrupt read data recv_buffer1[0-4]: %x, %x, %x, %x, %x ....\n",
                 recv_buffer1[0],recv_buffer1[1],recv_buffer1[2],recv_buffer1[3],recv_buffer1[4]);        

    _time_delay(300);
 // }
}


void initialize_i2c1_slave(void)
{
    uint_32 param1;
   fd_i2c_slave = fopen ("ii2c1:", NULL); // interrupt i2c1  version
      
   
   if (fd_i2c_slave == NULL)
   {
      printf ("Failed to open the I2C1 interrupt slave driver!\n");
      _time_delay (200L);
      _mqx_exit (1L);
   }
   //Configure i2c1 as alt2 pins of portC  -> copy from previous master Demo  i2c1
   SIM_SCGC4 |= 0x80;
   SIM_SCGC5 |= 0x800;
   printf ("For the i2c1 task we use K60 Tower and the alt2 portC functions to locate the i2c1 \n");
   printf ("For the i2c1 task we intend to connect an external i2c tool (ardvark) to  \n");
   printf ("the tower's elevator  expansion ports. Lack of connection can hang demo at present  \n");
   PORTC_PCR10 = 0x223;
   PORTC_PCR11 = 0x223;
   printf ("....post-Init PORTC_PCR10 = 0x%02lX, PORTC_PCR11 = 0x%02lX  \n",PORTC_PCR10,PORTC_PCR11);
   // Done configuring port pins on port2

       // Set I2C into slave mode & set the slave address
   if( I2C_OK == ioctl (fd_i2c_slave, IO_IOCTL_I2C_SET_SLAVE_MODE, NULL) )
   { printf("I2C1 int. slave mode GOOD\n"); }
   else
   { printf("I2C1 int. slave mode ERROR\n"); }
   
   
   param1 = I2C_RUC_ADDRESS;
   if( I2C_OK == ioctl(fd_i2c_slave, IO_IOCTL_I2C_SET_STATION_ADDRESS, &param1) )
   { printf("I2C1 int. slave mode SET STATION ADDRESS= %x   is GOOD\n",I2C_RUC_ADDRESS); }
   else
   { printf("I2C1 int. slave mode SET STATION ADDRESS ERROR\n"); }
         
   if( I2C_OK == ioctl (fd_i2c_slave,IO_IOCTL_I2C_ENABLE_DEVICE , NULL) )
   { printf("I2C1 int. slave mode ENABLE DEVICE is GOOD\n"); }
   else
   { printf("I2C1 int. slave mode ENABLE DEVICE ERROR\n"); }
   
   printf ( "Leaving initialize_i2c1_slave().....\n");
}


 

0 Kudos
2 Replies

439 Views
sergeEWU
Contributor I

Hey man, we are have pretty much the same problem with our K60. 

We are using I2C protocol to talk to a DPOL (voltage regulator) on the DPOL there are registers we are trying to read. 

Below is our C code that we are using, the problem we are getting is that the i2c_wait() function is hang up our program. I was reading somewhere it has to could do with the master (MCU) not sending ACK bit to slave chip.

Plus let us know if you figure it out or find any information. And we will do the same.

 

 

 

 

 

 

 

void Read_Iout(void)

{

  unsigned char byte1,byte2,byte3,read_I2C0_S;

  byte1=0;

  byte2=0;

  byte3=0;

  i2c_Start();

  // DPOL Address with Write bit

  I2C0_D = 0x38;

  read_I2C0_S=I2C0_S;

  printf("After DPOL address sent I2C0_S: %3d\n",read_I2C0_S);

  i2c_Wait();

  printf(" DPOL Address Sent (W)\n");

  /* Write Register Address */

  I2C0_D = 0x8C;

  i2c_Wait();

  printf(" Register Address Sent\n");

  /* Do a repeated start */

  I2C0_C1 |= I2C_C1_RSTA_MASK;

  /* Send Slave Address */

  I2C0_D = 0x39; //read address

  i2c_Wait();

  printf(" DPOL Address Sent (R)\n");

  /* Put in Rx Mode */

  I2C0_C1 &= (~I2C_C1_TX_MASK);

  /* Ensure TXAK bit is 0 */

  I2C0_C1 &= ~I2C_C1_TXAK_MASK;

  /* Dummy read */

  byte1 = I2C0_D ;

  printf("Dummy Read: %3d\n",byte1);

  i2c_Wait();

  /* Read first byte */

  byte1 = I2C0_D;

  i2c_Wait();

  /* Turn off ACK since this is second to last read*/

  I2C0_C1 |= I2C_C1_TXAK_MASK;

  /* Read second byte */

  byte2 = I2C0_D;

  i2c_Wait();

  /* Send stop */

  i2c_Stop();

  /* Read third byte */

  byte3 = I2C0_D;

  printf("%3d    %3d     %3d\n",byte1,byte2,byte3);

}

 

//Here is the wait function definition:

#define i2c_Wait()              while((I2C0_S & I2C_S_IICIF_MASK)==0) {} \

                                I2C0_S |= I2C_S_IICIF_MASK;

// I2C_S_IICIF_MASK= 0x2u this checks the IICIF field which is defined as follows:

0 Kudos

439 Views
Monica
Senior Contributor III

any updates on this guys? @sergey, I have the impression that your response was incomplete, can you continue the idea so the Tech experts would give some follow up on this? Thank you very much! And sorry for the delay, we're working on it! ;)

0 Kudos