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


asctec_hl_firmware
Author(s): Markus Achtelik, Michael Achtelik, Stephan Weiss, Laurent Kneip
autogenerated on Tue Jan 7 2014 11:05:19