i2c.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002  *   i2c.c:  I2C C file for NXP LPC23xx/24xx Family Microprocessors
00003  *
00004  *   Copyright(C) 2006, NXP Semiconductor
00005  *   All rights reserved.
00006  *
00007  *   History
00008  *   2006.07.19  ver 1.00    Prelimnary version, first Release
00009  *
00010 *****************************************************************************/
00011 
00012 #include "i2c.h"
00013 #include "../lpc_config.h"
00014 
00015 #include "../uart/uart.h"
00016 
00017 
00018 volatile int I2CMasterState = I2C_IDLE;
00019 volatile int I2CSlaveState = I2C_IDLE;
00020 
00021 volatile int I2CCmd;
00022 volatile int I2CMode;
00023 
00024 volatile char I2CMasterBuffer[BUFSIZE];
00025 volatile char I2CSlaveBuffer[BUFSIZE];
00026 volatile int I2CCount = 0;
00027 volatile int I2CReadLength;
00028 volatile int I2CWriteLength;
00029 
00030 volatile int RdIndex = 0;
00031 volatile int WrIndex = 0;
00032 
00033 /* 
00034 From device to device, the I2C communication protocol may vary, 
00035 in the example below, the protocol uses repeated start to read data from or 
00036 write to the device:
00037 For master read: the sequence is: STA,Addr(W),offset,RE-STA,Addr(r),data...STO 
00038 for master write: the sequence is: STA,Addr(W),length,RE-STA,Addr(w),data...STO
00039 Thus, in state 8, the address is always WRITE. in state 10, the address could 
00040 be READ or WRITE depending on the I2CCmd.
00041 */   
00042 
00043 
00044 /*****************************************************************************
00045 ** Function name:               I2CStart
00046 **
00047 ** Descriptions:                Create I2C start condition, a timeout
00048 **                              value is set if the I2C never gets started,
00049 **                              and timed out. It's a fatal error. 
00050 **
00051 ** parameters:                  None
00052 ** Returned value:              true or false, return false if timed out
00053 ** 
00054 *****************************************************************************/
00055 int I2CStart( void )
00056 {
00057   int timeout = 0;
00058   int retVal = FALSE;
00059  
00060   /*--- Issue a start condition ---*/
00061   I22CONSET = I2CONSET_STA;     /* Set Start flag */
00062   /*--- Wait until START transmitted ---*/
00063   while( 1 )
00064   {
00065         if ( I2CMasterState == I2C_STARTED )
00066         {
00067           retVal = TRUE;
00068           break;        
00069         }
00070         if ( timeout >= MAX_TIMEOUT )
00071         {
00072           retVal = FALSE;
00073           break;
00074         }
00075         timeout++;
00076   }
00077   return( retVal );
00078 }
00079 
00080 /*****************************************************************************
00081 ** Function name:               I2CStop
00082 **
00083 ** Descriptions:                Set the I2C stop condition, if the routine
00084 **                              never exit, it's a fatal bus error.
00085 **
00086 ** parameters:                  None
00087 ** Returned value:              true or never return
00088 ** 
00089 *****************************************************************************/
00090 int I2CStop( void )
00091 {
00092   I22CONSET = I2CONSET_STO;      /* Set Stop flag */
00093   I22CONCLR = I2CONCLR_SIC;  /* Clear SI flag */
00094   /*--- Wait for STOP detected ---*/
00095   while( I22CONSET & I2CONSET_STO );
00096   return TRUE;
00097 }
00098 
00099 /*****************************************************************************
00100 ** Function name:               I2CInit
00101 **
00102 ** Descriptions:                Initialize I2C controller
00103 **
00104 ** parameters:                  I2c mode is either MASTER or SLAVE
00105 ** Returned value:              true or false, return false if the I2C
00106 **                              interrupt handler was not installed correctly
00107 ** 
00108 *****************************************************************************/
00109 int I2CInit( int I2cMode )
00110 {
00111   //I2C2
00112   PCONP |=   0x04000000;
00113   PINSEL0 &= 0xffafffff;
00114   PINSEL0 |= 0x00a00000;
00115 
00116   //I2C0
00117 //  PCONP |=  0x00000080
00118 //  PINSEL1 &= ~0x03C00000;
00119 //  PINSEL1 |= 0x01400000;      /* set PIO0.27 and PIO0.28 to I2C0 SDA and SCK */
00120                                                         /* function to 01 on both SDA and SCK. */
00121   /*--- Clear flags ---*/
00122   I22CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC;
00123 
00124 
00125   /*--- Reset registers ---*/
00126   I22SCLL   = I2SCLL_SCLL;
00127   I22SCLH   = I2SCLH_SCLH;
00128   if ( I2cMode == I2CSLAVE )
00129   {
00130         I22ADR = LM75_ADDR;
00131   }    
00132 
00133   extern void I2C0MasterHandler( void ) __attribute__ ((interrupt("IRQ")));
00134 
00135   disableIRQ();
00136 //  PINSEL0 &= 0xffafffff;
00137 //  PINSEL0 |= 0x00a00000;
00138   VICIntSelect &= ~0x40000000;  /* i2c2=bit 30 como IRQ */
00139   VICIntEnable = 0x40000000;    /* Habilita int do i2c2 no VIC*/
00140   VICVectAddr30 = (int)I2C0MasterHandler;       /* Vetor para atendimento do I2C2 */
00141 
00142 //  PINSEL1 &= 0xfd7fffff;
00143 //  PINSEL1 |= 0x01400000;
00144 //  VICIntSelect &= ~0x200;     /* i2c0=bit 9 como IRQ  */
00145 //  VICIntEnable = 0x200;       /* Habilita int do i2c0 no VIC*/
00146 //  VICVectAddr9 = (int)I2C0MasterHandler;      /* Vetor 9 para atendimento do I2C0 */
00147   enableIRQ();
00148 
00149   I22CONSET = I2CONSET_I2EN;
00150   return( TRUE );
00151 }
00152 
00153 
00154 /*****************************************************************************
00155 ** Function name:               I2CEngine
00156 **
00157 ** Descriptions:                The routine to complete a I2C transaction
00158 **                              from start to stop. All the intermitten
00159 **                              steps are handled in the interrupt handler.
00160 **                              Before this routine is called, the read
00161 **                              length, write length, I2C master buffer,
00162 **                              and I2C command fields need to be filled.
00163 **                              see i2cmst.c for more details. 
00164 **
00165 ** parameters:                  None
00166 ** Returned value:              true or false, return false only if the
00167 **                              start condition can never be generated and
00168 **                              timed out. 
00169 ** 
00170 *****************************************************************************/
00171 int I2CEngine( void )
00172 {
00173   I2CMasterState = I2C_IDLE;
00174   RdIndex = 0;
00175   WrIndex = 0;
00176   if ( I2CStart() != TRUE )
00177   {
00178         I2CStop();
00179         return ( FALSE );
00180   }
00181 
00182   while ( 1 )
00183   {
00184         if ( I2CMasterState == DATA_NACK )
00185         {
00186           I2CStop();
00187           break;
00188         }
00189   }    
00190   return ( TRUE );      
00191 }
00192 
00193 /******************************************************************************
00194 **                            End Of File
00195 ******************************************************************************/
00196