i2c.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002  *   i2c.c:  I2C C file for Philips LPC214x Family Microprocessors
00003  *
00004  *   Copyright(C) 2006, Philips Semiconductor
00005  *   All rights reserved.
00006  *
00007  *   History
00008  *   2005.10.01  ver 1.00    Prelimnary version, first Release
00009  *
00010 *****************************************************************************/
00011 #include "LPC214x.h"                        /* LPC21xx definitions */
00012 #include "type.h"
00013 #include "irq.h"
00014 #include "i2c.h"
00015 
00016 DWORD I2CMasterState = I2C_IDLE;
00017 DWORD I2CSlaveState = I2C_IDLE;
00018 
00019 DWORD I2CCmd;
00020 DWORD I2CMode;
00021 
00022 BYTE I2CMasterBuffer[BUFSIZE];
00023 BYTE I2CSlaveBuffer[BUFSIZE];
00024 DWORD I2CCount = 0;
00025 DWORD I2CReadLength;
00026 DWORD I2CWriteLength;
00027 
00028 DWORD RdIndex = 0;
00029 DWORD WrIndex = 0;
00030 
00031 /* 
00032 From device to device, the I2C communication protocol may vary, 
00033 in the example below, the protocol uses repeated start to read data from or 
00034 write to the device:
00035 For master read: the sequence is: STA,Addr(W),offset,RE-STA,Addr(w),data...STO 
00036 for master write: the sequence is: STA,Addr(W),length,RE-STA,Addr(r),data...STO
00037 Thus, in state 8, the address is always WRITE. in state 10, the address could 
00038 be READ or WRITE depending on the I2CCmd.
00039 */   
00040 
00041 /*****************************************************************************
00042 ** Function name:               I2C0MasterHandler
00043 **
00044 ** Descriptions:                I2C0 interrupt handler, deal with master mode
00045 **                              only.
00046 **
00047 ** parameters:                  None
00048 ** Returned value:              None
00049 ** 
00050 *****************************************************************************/
00051 void I2C0MasterHandler (void) __irq 
00052 {
00053     BYTE StatValue;
00054 
00055     /* this handler deals with master read and master write only */
00056     StatValue = I20STAT;
00057     
00058     IENABLE;   
00059     switch ( StatValue )
00060     {
00061         case 0x08:                      /* A Start condition is issued. */
00062         I20DAT = I2CMasterBuffer[0];
00063         I20CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
00064         I2CMasterState = I2C_STARTED;
00065         break;
00066         
00067         case 0x10:                      /* A repeated started is issued */
00068         if (  I2CCmd == GET_DEVICE_ID || I2CCmd == GET_TEMPERATURE )
00069         {
00070             I20DAT = I2CMasterBuffer[2];
00071         }
00072         I20CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
00073         I2CMasterState = I2C_RESTARTED;
00074         break;
00075         
00076         case 0x18:                      /* Regardless, it's a ACK */
00077         if ( I2CMasterState == I2C_STARTED )
00078         {
00079             I20DAT = I2CMasterBuffer[1+WrIndex];
00080             WrIndex++;
00081             I2CMasterState = DATA_ACK;
00082         }
00083         I20CONCLR = I2CONCLR_SIC;
00084         break;
00085         
00086         case 0x28:      /* Data byte has been transmitted, regardless ACK or NACK */
00087         case 0x30:
00088         if ( WrIndex != I2CWriteLength )
00089         {   
00090             I20DAT = I2CMasterBuffer[1+WrIndex]; /* this should be the last one */
00091             WrIndex++;
00092             if ( WrIndex != I2CWriteLength )
00093             {   
00094                 I2CMasterState = DATA_ACK;
00095             }
00096             else
00097             {
00098                 I2CMasterState = DATA_NACK;
00099                 if ( I2CReadLength != 0 )
00100                 {
00101                     I20CONSET = I2CONSET_STA;   /* Set Repeated-start flag */
00102                     I2CMasterState = I2C_REPEATED_START;
00103                 }
00104             }
00105         }
00106         else
00107         {
00108             if ( I2CReadLength != 0 )
00109             {
00110                 I20CONSET = I2CONSET_STA;       /* Set Repeated-start flag */
00111                 I2CMasterState = I2C_REPEATED_START;
00112             }
00113             else
00114             {
00115                 I2CMasterState = DATA_NACK;
00116             }
00117         }
00118         I20CONCLR = I2CONCLR_SIC;
00119         break;
00120         
00121         case 0x40:      /* Master Receive, SLA_R has been sent */
00122         I20CONCLR = I2CONCLR_SIC;
00123         break;
00124         
00125         case 0x50:      /* Data byte has been received, regardless following ACK or NACK */
00126         case 0x58:
00127         I2CMasterBuffer[3+RdIndex] = I20DAT;
00128         RdIndex++;
00129         if ( RdIndex != I2CReadLength )
00130         {   
00131             I2CMasterState = DATA_ACK;
00132         }
00133         else
00134         {
00135             RdIndex = 0;
00136             I2CMasterState = DATA_NACK;
00137         }
00138         I20CONSET = I2CONSET_AA;        /* assert ACK after data is received */
00139         I20CONCLR = I2CONCLR_SIC;
00140         break;
00141         
00142         case 0x20:                      /* regardless, it's a NACK */
00143         case 0x48:
00144         I20CONCLR = I2CONCLR_SIC;
00145         I2CMasterState = DATA_NACK;
00146         break;
00147         
00148         case 0x38:                      /* Arbitration lost, in this example, we don't
00149                                         deal with multiple master situation */
00150         default:
00151         I20CONCLR = I2CONCLR_SIC;       
00152         break;
00153     }
00154     
00155     IDISABLE;
00156     VICVectAddr = 0;            /* Acknowledge Interrupt */
00157 
00158 }
00159 
00160 
00161 void I2C0_send_motordata(void)
00162 {
00163         WrIndex=0;
00164         RdIndex=0;
00165         I2CWriteLength = 5;
00166         I2CReadLength = 0;
00167     I2CMasterBuffer[0] = 0x02;
00168     I2CMasterBuffer[1] = 100;
00169     I2CMasterBuffer[2] = 100;
00170     I2CMasterBuffer[3] = 100;
00171     I2CMasterBuffer[4] = 1;
00172 
00173     //I20CONSET = I2CONSET_STA; /* Set Start flag */
00174     //if ( !I2CStart() ) I2CStop();
00175     I2CCmd = GET_TEMPERATURE;
00176         I2CEngine();
00177 }
00178 
00179 /*****************************************************************************
00180 ** Function name:               I2CStart
00181 **
00182 ** Descriptions:                Create I2C start condition, a timeout
00183 **                              value is set if the I2C never gets started,
00184 **                              and timed out. It's a fatal error. 
00185 **
00186 ** parameters:                  None
00187 ** Returned value:              true or false, return false if timed out
00188 ** 
00189 *****************************************************************************/
00190 unsigned int I2CStart( void )
00191 {
00192     unsigned int timeout = 0;
00193     unsigned int returnValue = FALSE;
00194  
00195     /*--- Issue a start condition ---*/
00196     I20CONSET = I2CONSET_STA;   /* Set Start flag */
00197     
00198     /*--- Wait until START transmitted ---*/
00199     while( 1 )
00200     {
00201         if ( I2CMasterState == I2C_STARTED )
00202         {
00203             returnValue = TRUE;
00204             break;      
00205         }
00206         if ( timeout >= MAX_TIMEOUT )
00207         {
00208             returnValue = FALSE;
00209             break;
00210         }
00211         timeout++;
00212     }
00213     return( returnValue );
00214 }
00215 
00216 /*****************************************************************************
00217 ** Function name:               I2CStop
00218 **
00219 ** Descriptions:                Set the I2C stop condition, if the routine
00220 **                              never exit, it's a fatal bus error.
00221 **
00222 ** parameters:                  None
00223 ** Returned value:              true or never return
00224 ** 
00225 *****************************************************************************/
00226 unsigned int I2CStop( void )
00227 {
00228     I20CONSET = I2CONSET_STO;      /* Set Stop flag */ 
00229     I20CONCLR = I2CONCLR_SIC;  /* Clear SI flag */ 
00230             
00231     /*--- Wait for STOP detected ---*/
00232     while( I20CONSET & I2CONSET_STO );
00233     return TRUE;
00234 }
00235 
00236 /*****************************************************************************
00237 ** Function name:               I2CInit
00238 **
00239 ** Descriptions:                Initialize I2C controller
00240 **
00241 ** parameters:                  I2c mode is either MASTER or SLAVE
00242 ** Returned value:              true or false, return false if the I2C
00243 **                              interrupt handler was not installed correctly
00244 ** 
00245 *****************************************************************************/
00246 void I2CInit( unsigned int I2cMode ) 
00247 {
00248     IODIR0|= 0x0C;      /* set port 0.2 and port 0.3 to output, high */
00249     IOSET0 = 0x0C;
00250 
00251     /*--- Clear flags ---*/
00252     I20CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC;    
00253 
00254     /*--- Reset registers ---*/
00255     I20SCLL   = I2SCLL_SCLL;
00256     I20SCLH   = I2SCLH_SCLH;
00257 }
00258 
00259 /*****************************************************************************
00260 ** Function name:               I2CEngine
00261 **
00262 ** Descriptions:                The routine to complete a I2C transaction
00263 **                              from start to stop. All the intermitten
00264 **                              steps are handled in the interrupt handler.
00265 **                              Before this routine is called, the read
00266 **                              length, write length, I2C master buffer,
00267 **                              and I2C command fields need to be filled.
00268 **                              see i2cmst.c for more details. 
00269 **
00270 ** parameters:                  None
00271 ** Returned value:              true or false, return false only if the
00272 **                              start condition can never be generated and
00273 **                              timed out. 
00274 ** 
00275 *****************************************************************************/
00276 unsigned int I2CEngine( void ) 
00277 {
00278     I2CMasterState = I2C_IDLE;
00279     RdIndex = 0;
00280     WrIndex = 0;
00281     if ( I2CStart() != TRUE )
00282     {
00283                 I2CStop();
00284         return ( FALSE );
00285     }
00286     while ( 1 )
00287     {
00288         if ( I2CMasterState == DATA_NACK )
00289         {
00290             I2CStop();
00291             break;
00292         }
00293     }    
00294     return ( TRUE );      
00295 }
00296 
00297 /******************************************************************************
00298 **                            End Of File
00299 ******************************************************************************/
00300 


ccny_asctec_firmware_2
Author(s): Ivan Dryanovski, Roberto G. Valenti
autogenerated on Tue Jan 7 2014 11:04:17