<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>LPC1768 + AT24C08 - Timeout problem</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/28942/lpc1768-at24c08---timeout-problem</link><description> 
Hello, I am using a AT24C08 for store some datas and I&amp;#39;m having
trouble. I am using code sample for I2C. The data is being written
and read perfectly, but the function I2CStart() return always
&amp;quot;timeout&amp;quot;. This &amp;quot;problem&amp;quot; causes a delay in my routines</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: LPC1768 + AT24C08 - Timeout problem</title><link>https://community.arm.com/thread/136821?ContentTypeID=1</link><pubDate>Mon, 23 Jul 2012 17:56:02 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:94c0d34a-24bd-47d1-8eae-5f73ce9f2235</guid><dc:creator>John Linq</dc:creator><description>&lt;p&gt;&lt;p&gt;
=&amp;gt; I tried using the code below, but don&amp;#39;t work&lt;/p&gt;

&lt;p&gt;
What do you mean by [but don't work] in this case?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC1768 + AT24C08 - Timeout problem</title><link>https://community.arm.com/thread/127634?ContentTypeID=1</link><pubDate>Mon, 23 Jul 2012 04:41:49 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:a10ca031-6cf7-4b88-9c5a-5d5e605e1ed2</guid><dc:creator>ALEXANDRE PERUSSI</dc:creator><description>&lt;p&gt;&lt;p&gt;
Yes, the I2CStart() returns FALSE. This code is a driver sample of
NXP for PCA8581, but don&amp;#39;t work with my 24C08. I tried using the code
below, but don&amp;#39;t work. There is some error in I2C0_IRQHandler() that
I can&amp;#39;t find.&lt;/p&gt;

&lt;pre&gt;
void I2C0_IRQHandler(void)
{
  uint8_t StatValue;

  /* this handler deals with master read and master write only */
  StatValue = LPC_I2C0-&amp;gt;I2STAT;

  switch ( StatValue )
  {
        case 0x08:                      /* A Start condition is issued. */
        LPC_I2C0-&amp;gt;I2DAT = I2CMasterBuffer[0];
        LPC_I2C0-&amp;gt;I2CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
        I2CMasterState = I2C_STARTED;
        break;

        case 0x10:                      /* A repeated started is issued */
        LPC_I2C0-&amp;gt;I2CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
        I2CMasterState = I2C_RESTARTED;
        break;

        case 0x18:                      /* Regardless, it&amp;#39;s a ACK */
        if ( I2CMasterState == I2C_STARTED )
        {
          LPC_I2C0-&amp;gt;I2DAT = I2CMasterBuffer[1+WrIndex];
          WrIndex++;
          I2CMasterState = DATA_ACK;
        }
        LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;
        break;

        case 0x28:      /* Data byte has been transmitted, regardless ACK or NACK */
        case 0x30:
        if ( WrIndex != I2CWriteLength )
        {
          LPC_I2C0-&amp;gt;I2DAT = I2CMasterBuffer[1+WrIndex]; /* this should be the last one */
          WrIndex++;
          if ( WrIndex != I2CWriteLength )
          {
                I2CMasterState = DATA_ACK;
          }
          else
          {
                I2CMasterState = DATA_NACK;
                if ( I2CReadLength != 0 )
                {
                  LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_STA; /* Set Repeated-start flag */
                  I2CMasterState = I2C_REPEATED_START;
                }
          }
        }
        else
        {
          if ( I2CReadLength != 0 )
          {
                LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_STA;   /* Set Repeated-start flag */
                I2CMasterState = I2C_REPEATED_START;
          }
          else
          {
                I2CMasterState = DATA_NACK;
                LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_STO;      /* Set Stop flag */
          }
        }
        LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;
        break;

        case 0x40:      /* Master Receive, SLA_R has been sent */
        LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_AA;    /* assert ACK after data is received */
        LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;
        break;

        case 0x50:      /* Data byte has been received, regardless following ACK or NACK */
        case 0x58:
        I2CMasterBuffer[3+RdIndex] = LPC_I2C0-&amp;gt;I2DAT;
        RdIndex++;
        if ( RdIndex != I2CReadLength )
        {
          I2CMasterState = DATA_ACK;
        }
        else
        {
          RdIndex = 0;
          I2CMasterState = DATA_NACK;
        }
        LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_AA;    /* assert ACK after data is received */
        LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;
        break;

        case 0x20:              /* regardless, it&amp;#39;s a NACK */

        case 0x48:
        LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;
        I2CMasterState = DATA_NACK;
        break;

        case 0x38:              /* Arbitration lost, in this example, we don&amp;#39;t
                                        deal with multiple master situation */
        default:
        LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;
        break;
  }
}
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC1768 + AT24C08 - Timeout problem</title><link>https://community.arm.com/thread/127628?ContentTypeID=1</link><pubDate>Sun, 22 Jul 2012 23:08:22 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e18c9189-813f-4825-a110-7c5217b41585</guid><dc:creator>John Linq</dc:creator><description>&lt;p&gt;&lt;p&gt;
An old thread may be relevant:&lt;br /&gt;
&lt;a href="http://www.keil.com/forum/19195/"&gt;http://www.keil.com/forum/19195/&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC1768 + AT24C08 - Timeout problem</title><link>https://community.arm.com/thread/119759?ContentTypeID=1</link><pubDate>Sun, 22 Jul 2012 23:01:58 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:d48a4957-bb24-4429-9487-3e739d6c910b</guid><dc:creator>John Linq</dc:creator><description>&lt;p&gt;&lt;p&gt;
=&amp;gt; but the function I2CStart() return always &amp;quot;timeout&amp;quot;.&lt;br /&gt;
Do you mean that I2CStart() returns FALSE due to timeout?&lt;/p&gt;

&lt;pre&gt;
uint32_t I2CStart( void )
{
  uint32_t timeout = 0;
  uint32_t retVal = FALSE;

  /*--- Issue a start condition ---*/
  LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_STA; /* Set Start flag */



  /*--- Wait until START transmitted ---*/
  while( 1 )
  {
        &lt;b&gt;if ( I2CMasterState == I2C_STARTED )
        {
          retVal = TRUE;
          break;&lt;/b&gt;
        }
&lt;/pre&gt;

&lt;p&gt;
But it seems you never assign an I2C_STARTED to
I2CMasterState.&lt;/p&gt;

&lt;pre&gt;
Search &amp;quot;I2CMasterState&amp;quot; (10 hits in 1 files)
  new  1 (10 hits)
        Line 6: volatile uint32_t I2CMasterState = I2C_IDLE;
        Line 48:           I2CMasterState = I2C_NO_DATA;
        Line 71:                 I2CMasterState = I2C_OK;
        Line 79:         I2CMasterState = I2C_NACK_ON_DATA;
        Line 112:         I2CMasterState = I2C_OK;
        Line 120:         I2CMasterState = I2C_NACK_ON_ADDRESS;
        Line 127:         I2CMasterState = I2C_ARBITRATION_LOST;
        Line 146:         if ( I2CMasterState == I2C_STARTED )
        Line 201:   I2CMasterState = I2C_IDLE;
        Line 213:         if ( I2CMasterState == DATA_NACK )
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC1768 + AT24C08 - Timeout problem</title><link>https://community.arm.com/thread/106217?ContentTypeID=1</link><pubDate>Sun, 22 Jul 2012 16:06:29 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b40d879d-fa7b-4536-97c1-4b9cbd1e768c</guid><dc:creator>ALEXANDRE PERUSSI</dc:creator><description>&lt;p&gt;&lt;p&gt;
i2c.h&lt;/p&gt;

&lt;pre&gt;
#define I2C_STARTED           1
#define I2C_RESTARTED         2
#define I2C_REPEATED_START    3
#define DATA_ACK              4
#define DATA_NACK             5
#define I2C_BUSY              6
#define I2C_NO_DATA           7
#define I2C_NACK_ON_ADDRESS   8
#define I2C_NACK_ON_DATA      9
#define I2C_ARBITRATION_LOST  10
#define I2C_TIME_OUT          11
#define I2C_OK                12

#define I2CONSET_I2EN       (0x1&amp;lt;&amp;lt;6)  /* I2C Control Set Register */
#define I2CONSET_AA         (0x1&amp;lt;&amp;lt;2)
#define I2CONSET_SI         (0x1&amp;lt;&amp;lt;3)
#define I2CONSET_STO        (0x1&amp;lt;&amp;lt;4)
#define I2CONSET_STA        (0x1&amp;lt;&amp;lt;5)

#define I2CONCLR_AAC        (0x1&amp;lt;&amp;lt;2)  /* I2C Control clear Register */
#define I2CONCLR_SIC        (0x1&amp;lt;&amp;lt;3)
#define I2CONCLR_STAC       (0x1&amp;lt;&amp;lt;5)
#define I2CONCLR_I2ENC      (0x1&amp;lt;&amp;lt;6)

#define I2DAT_I2C                       0x00000000  /* I2C Data Reg */
#define I2ADR_I2C                       0x00000000  /* I2C Slave Address Reg */
#define I2SCLH_SCLH                     0x00000080  /* I2C SCL Duty Cycle High Reg */
#define I2SCLL_SCLL                     0x00000080  /* I2C SCL Duty Cycle Low Reg */
#define I2SCLH_HS_SCLH          0x00000008  /* Fast Plus I2C SCL Duty Cycle High Reg */
#define I2SCLL_HS_SCLL          0x00000008  /* Fast Plus I2C SCL Duty Cycle Low Reg */

extern void I2C0_IRQHandler( void );
extern uint32_t I2CInit( uint32_t I2cMode );
extern uint32_t I2CStart( void );
extern uint32_t I2CStop( void );
extern uint32_t I2CEngine( void );

#endif /* end __I2C_H */
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC1768 + AT24C08 - Timeout problem</title><link>https://community.arm.com/thread/80415?ContentTypeID=1</link><pubDate>Sun, 22 Jul 2012 16:05:47 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:de9bee8d-9450-4fc9-965b-76fc48db3d4b</guid><dc:creator>ALEXANDRE PERUSSI</dc:creator><description>&lt;p&gt;&lt;p&gt;
i2c.c&lt;/p&gt;

&lt;pre&gt;
#include &amp;quot;lpc17xx.h&amp;quot;
#include &amp;quot;type.h&amp;quot;
#include &amp;quot;i2c.h&amp;quot;
#include &amp;quot;lcd.h&amp;quot;

volatile uint32_t I2CMasterState = I2C_IDLE;
volatile uint32_t I2CSlaveState = I2C_IDLE;

volatile uint32_t I2CCmd;
volatile uint32_t I2CMode;

volatile uint8_t I2CMasterBuffer[BUFSIZE];
volatile uint8_t I2CSlaveBuffer[BUFSIZE];
volatile uint32_t I2CCount = 0;
volatile uint32_t I2CReadLength;
volatile uint32_t I2CWriteLength;

volatile uint32_t RdIndex = 0;
volatile uint32_t WrIndex = 0;


void I2C0_IRQHandler(void)
{
  uint8_t StatValue;

  /* this handler deals with master read and master write only */
  StatValue = LPC_I2C0-&amp;gt;I2STAT;

        switch ( StatValue )
  {
        case 0x08:                      /* A Start condition is issued. */
        WrIndex = 0;
        LPC_I2C0-&amp;gt;I2DAT = I2CMasterBuffer[WrIndex++];
        LPC_I2C0-&amp;gt;I2CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
        break;

        case 0x10:                      /* A repeated started is issued */
        RdIndex = 0;
        /* Send SLA with R bit set, */
        LPC_I2C0-&amp;gt;I2DAT = I2CMasterBuffer[WrIndex++];
        LPC_I2C0-&amp;gt;I2CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
        break;

        case 0x18:                      /* Regardless, it&amp;#39;s a ACK */
        if ( I2CWriteLength == 1 )
        {
          LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_STO;      /* Set Stop flag */
          I2CMasterState = I2C_NO_DATA;
        }
        else
        {
          LPC_I2C0-&amp;gt;I2DAT = I2CMasterBuffer[WrIndex++];
        }
        LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;
        break;

        case 0x28:      /* Data byte has been transmitted, regardless ACK or NACK */
        if ( WrIndex &amp;lt; I2CWriteLength )
        {
          LPC_I2C0-&amp;gt;I2DAT = I2CMasterBuffer[WrIndex++]; /* this should be the last one */
        }
        else
        {
          if ( I2CReadLength != 0 )
          {
                LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_STA;   /* Set Repeated-start flag */
          }
          else
          {
                LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_STO;      /* Set Stop flag */
                I2CMasterState = I2C_OK;
          }
        }
        LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;
        break;

        case 0x30:
        LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_STO;      /* Set Stop flag */
        I2CMasterState = I2C_NACK_ON_DATA;
        LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;
        break;

        case 0x40:      /* Master Receive, SLA_R has been sent */
        if ( (RdIndex + 1) &amp;lt; I2CReadLength )
        {
          /* Will go to State 0x50 */
          LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_AA;  /* assert ACK after data is received */
        }
        else
        {
          /* Will go to State 0x58 */
          LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_AAC; /* assert NACK after data is received */
        }
        LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;
        break;

        case 0x50:      /* Data byte has been received, regardless following ACK or NACK */
        I2CSlaveBuffer[RdIndex++] = LPC_I2C0-&amp;gt;I2DAT;
        if ( (RdIndex + 1) &amp;lt; I2CReadLength )
        {
          LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_AA;  /* assert ACK after data is received */
        }
        else
        {
          LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_AAC; /* assert NACK on last byte */
        }
        LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;
        break;

        case 0x58:
        I2CSlaveBuffer[RdIndex++] = LPC_I2C0-&amp;gt;I2DAT;
        I2CMasterState = I2C_OK;
        LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_STO;   /* Set Stop flag */
        LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;   /* Clear SI flag */
        break;

        case 0x20:              /* regardless, it&amp;#39;s a NACK */
        case 0x48:
        LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_STO;      /* Set Stop flag */
        I2CMasterState = I2C_NACK_ON_ADDRESS;
        LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;
        break;

        case 0x38:              /* Arbitration lost, in this example, we don&amp;#39;t
                                        deal with multiple master situation */
        default:
        I2CMasterState = I2C_ARBITRATION_LOST;
        LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;
        break;
  }
}

uint32_t I2CStart( void )
{
  uint32_t timeout = 0;
  uint32_t retVal = FALSE;

  /*--- Issue a start condition ---*/
  LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_STA; /* Set Start flag */



  /*--- Wait until START transmitted ---*/
  while( 1 )
  {
        if ( I2CMasterState == I2C_STARTED )
        {
          retVal = TRUE;
          break;
        }
        if ( timeout &amp;gt;= MAX_TIMEOUT )
        {
                lcd_gotoxy(1,0);
                lcd_print(&amp;quot;MAX_TIMEOUT&amp;quot;);
          retVal = FALSE;
          break;
        }
        timeout++;
  }
  return( retVal );
}


uint32_t I2CStop( void )
{
  LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_STO;  /* Set Stop flag */
  LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_SIC;  /* Clear SI flag */

  /*--- Wait for STOP detected ---*/
  while( LPC_I2C0-&amp;gt;I2CONSET &amp;amp; I2CONSET_STO );
  return TRUE;
}


uint32_t I2CInit( uint32_t I2cMode )
{
  LPC_SC-&amp;gt;PCONP |= (1 &amp;lt;&amp;lt; 7);

  /* set PIO0.27 and PIO0.28 to I2C0 SDA and SCL */
  /* function to 01 on both SDA and SCL. */
  LPC_PINCON-&amp;gt;PINSEL1 &amp;amp;= ~((0x03&amp;lt;&amp;lt;22)|(0x03&amp;lt;&amp;lt;24));
  LPC_PINCON-&amp;gt;PINSEL1 |= ((0x01&amp;lt;&amp;lt;22)|(0x01&amp;lt;&amp;lt;24));

  /*--- Clear flags ---*/
  LPC_I2C0-&amp;gt;I2CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC;

  LPC_PINCON-&amp;gt;I2CPADCFG &amp;amp;= ~((0x1&amp;lt;&amp;lt;0)|(0x1&amp;lt;&amp;lt;2));
  LPC_I2C0-&amp;gt;I2SCLL   = I2SCLL_SCLL;
  LPC_I2C0-&amp;gt;I2SCLH   = I2SCLH_SCLH;

  /* Install interrupt handler */
  NVIC_EnableIRQ(I2C0_IRQn);

  LPC_I2C0-&amp;gt;I2CONSET = I2CONSET_I2EN;
  return( TRUE );
}


uint32_t I2CEngine( void )
{
  I2CMasterState = I2C_IDLE;
  RdIndex = 0;
  WrIndex = 0;
  lcd_clear();
  if ( I2CStart() != TRUE )
  {
        I2CStop();
        return ( FALSE );
  }

  while ( 1 )
  {
        if ( I2CMasterState == DATA_NACK )
        {
          I2CStop();
          break;
        }
  }

  return ( TRUE );
}
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LPC1768 + AT24C08 - Timeout problem</title><link>https://community.arm.com/thread/60947?ContentTypeID=1</link><pubDate>Sun, 22 Jul 2012 16:05:03 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:d4cb6933-cff2-4f23-abac-4276bcf307f5</guid><dc:creator>ALEXANDRE PERUSSI</dc:creator><description>&lt;p&gt;&lt;p&gt;
main.c&lt;/p&gt;

&lt;pre&gt;
int main (void)
{
        char temp[16];
        unsigned char c = 0;
        unsigned int i = 0;

        SystemInit();
        SysTick_Config(SystemFrequency/100);  /* Generate interrupt each 10 ms      */

        if ( I2CInit( (uint32_t)I2CMASTER ) == FALSE )  /* initialize I2c */
        {
                while ( 1 );                            /* Fatal error */
        }

        /* Initialize RTC with RTCCAL and RTCDIR values */
        InitRTC();
        /* start RTC */
        StartRTC();

        init_KEYPAD();
        lcd_clear();

        while(1)
        {
                c = get_KEYPAD();
                if(c == &amp;#39;1&amp;#39;)
                {
                        for ( i = 0; i &amp;lt; BUFSIZE; i++ )      /* clear buffer */
                        {
                                I2CMasterBuffer[i] = 0;
                        }

                        I2CWriteLength = 5;
                        I2CReadLength = 0;
                        I2CMasterBuffer[0] = AT24C08_ADDR;
                        I2CMasterBuffer[1] = 0x00;              /* address offset for user seq. */

                        I2CMasterBuffer[2] = LPC_RTC-&amp;gt;HOUR;
                        I2CMasterBuffer[3] = LPC_RTC-&amp;gt;MIN;
                        I2CMasterBuffer[4] = LPC_RTC-&amp;gt;SEC;
                        I2CEngine();
                }
                if(c == &amp;#39;2&amp;#39;)
                {
                        for ( i = 0; i &amp;lt; BUFSIZE; i++ )
                        {
                                I2CSlaveBuffer[i] = 0x00;
                        }
                        /* Write SLA(W), address, SLA(R), and read one byte back. */
                        I2CWriteLength = 2;
                        I2CReadLength = 20;
                        I2CMasterBuffer[0] = AT24C08_ADDR;
                        I2CMasterBuffer[1] = 0x00;              /* address */
                        I2CMasterBuffer[2] = AT24C08_ADDR | RD_BIT;
                        I2CEngine();

                }
                lcd_gotoxy(0,0);
                sprintf(temp,&amp;quot;%d:%d:%d&amp;quot;,I2CSlaveBuffer[0],I2CSlaveBuffer[1],I2CSlaveBuffer[2]);
                lcd_print(temp);
        }

        return 0;
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>