/******************************************************************************
COPYRIGHT 2002 STMicroelectronics
Source File Name :  main.c  
Group            :  IPSW,CMG-IPDF
Author           : MCD Application Team
Date First Issued:  04/03/2002
********************************Documentation**********************************
General Purpose - It is application file which calls the functions in i2c.c. 
This example carries out the single byte transmission and continuous 
transmission in pollling mode or in interrupt driven without buffer mode as per 
the selection made in the ST7lib_Config.h. Also it carries out the reception of
a single byte and a buffer of data in polling mode or in interrupt driven 
without buffer mode.The transmission and reception are carried out at a speed
of 200 KHz.
 
Also for transmission and reception in interrupt driven case, interrupt is 
enabled for generating the interrupt.  	
********************************Revision History*******************************
_______________________________________________________________________________
Date :04/03/2002                  Release:1.0
--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**/

#include "ST7lib_config.h" /* List of all ST7 devices and communication mode */
#include "main.h"        
       /* Declaration of prototypes of user defined functions used in main.c */ 
       
void main(void)              
{ 		
     unsigned char Rx_Data;
     unsigned char Buff_Test[size_buff];
     unsigned char Buff[size_buff] = {0, 1, 2, 5, 50, 10, 100, 200, 225, 255};      
     I2C_TxErrCode_t Temp1 ;
     I2C_RxErrCode_t Temp2 ;
     Temp1 = Temp2 = 0x00 ;
     I2C_Init ((unsigned char)I2C_ENABLE_ACK | (unsigned char)I2C_IT_ENABLE); 
                                        /* Enable acknowledge and interrupts */

     EnableInterrupts;     /* Interrupt mask is reset for enabling interrupt */
     I2C_MultiMaster_Config ();   /* Configure I2C as multimaster I2C device */ 
     I2C_Select_Speed (I2C_FASTSPEED, (unsigned int)200);
                                 /* Selects fast speed mode, Speed is 200KHz */
     I2C_Generate_Start ();
     while(!(I2C_IsTransmitCompleted()== I2C_START_OK));     
               /*================================================
                        Transmission through Polling mode
               =================================================*/
              /* Communication mode defined as POLLING_TX in ST7lib_config.h */
     #ifdef I2C_POLLING_TX
     I2C_Load_Address (Addr_Byte_Tx, I2C_TX_MODE);
     Temp1 = I2C_IsTransmitCompleted() ;
     while ((User_Timeout_Function()) && (Temp1 != I2C_ADD_TX_OK)) 
     {
          Temp1 = I2C_IsTransmitCompleted() ;
     }     
     switch (Temp1)                          /* To check transmission status */
     {
          case I2C_TX_AF:
               User_Function();
               break;
          default:
               break;
     }
                                            /* Single byte data transmission */
     I2C_PutByte (My_Data);
     Timeoutcount = 0 ;
     Temp1 = I2C_IsTransmitCompleted() ;
     while ((User_Timeout_Function()) && (Temp1 != I2C_DATA_TX_OK ))
     {
          Temp1 = I2C_IsTransmitCompleted() ;
     }
     switch (Temp1)                          /* To check transmission status */
     {
          case I2C_TX_AF:
          case I2C_TX_ARLO:
          case I2C_TX_BERR:
               User_Function();                          /* Error Management */
               break;
          default:                    /* None of the above condition is true */
               User_Function();
               break;
     }
                           /* Transmission of 10 data bytes from user buffer */
     switch(I2C_PutBuffer (Buff,(unsigned char)10))
     {
          case I2C_TX_AF:
          case I2C_TX_ARLO:
          case I2C_TX_BERR:
               User_Function();                          /* Error Management */
               break;
          case I2C_DATA_TX_OK:
               break;
          default:                    /* None of the above condition is true */
               User_Function();
               break;
     }
     I2C_Generate_Stop ();                           /* Transmission Stopped */
     while (!(I2C_IsStopGen ())) ;     
     #endif
                /*================================================
                          Reception through Polling mode
                ================================================*/
              /* Communication mode defined as POLLING_RX in ST7lib_config.h */
                                               /* Single byte data reception */
     #ifdef I2C_POLLING_RX     
     I2C_Generate_Start ();
     while(!(I2C_IsTransmitCompleted()== I2C_START_OK));     
     I2C_Load_Address (Addr_Byte_Tx, I2C_RX_MODE);
     Timeoutcount = 0 ;
     Temp1 = I2C_IsTransmitCompleted() ;
     while ((User_Timeout_Function()) && ( Temp1 != I2C_ADD_TX_OK)) 
     {
          Temp1 = I2C_IsTransmitCompleted() ;
     }     
     switch (Temp1)                             /* To check reception status */
     {
          case I2C_TX_AF:
               User_Function();
               break;
          default:
               break;
     }
     Timeoutcount = 0 ;
     Temp2 = I2C_IsReceptionCompleted() ;
     while ((User_Timeout_Function()) && (Temp2 == I2C_RX_DATA_EMPTY))
     {
          Temp2 = I2C_IsReceptionCompleted() ;
     }  
                                            /* Waits for data byte reception */
     if (User_Timeout_Function())
     {
          switch (Temp2)
          {
               case I2C_RX_ARLO:
               case I2C_RX_BERR:
                    Rx_Data = I2C_GetByte(); /* Corrupted data byte received */
                    User_Function();                     /* Error Management */
                    break;
               case I2C_DATA_RX_OK: /* Reception successful */
                    Rx_Data = I2C_GetByte();
                    break;
               default:               /* None of the above condition is true */
                    User_Function ();
               break;
          }
     }
     else
     {
           /* Handle time out as Transmitter/Receiver is having some problem */
     }
                                               /* Reception of a set of data */
     switch(I2C_GetBuffer (Buff_Test,(unsigned char)10))
     {
          case I2C_RX_ARLO:
          case I2C_RX_BERR:
               User_Function();                          /* Error Management */
               break;
          case I2C_DATA_RX_OK:
               break;
          default:                     /* None of the above condition is met */
               User_Function ();
               break;
     }
     #endif    
     
            /*==========================================================
             Transmission through Interrupt driven without buffer mode
             ==========================================================*/
     /* Communication mode defined as ITDRV_WITHOUTBUF_TX in 
                                                             ST7lib_config.h */
                                                 /* Single byte transmission */     
     #ifdef I2C_ITDRV_WITHOUTBUF_TX
     if ((I2C_IsTransmitCompleted()) == I2C_DATA_TX_OK)
                                /* Ensure that there are no pending requests */
     {
          I2C_Generate_Start ();  
          while(!((I2C_IsTransmitCompleted())== I2C_START_OK));          
          I2C_Load_Address (Addr_Byte_Tx, I2C_TX_MODE);
          Timeoutcount = 0 ;          
          Temp1 = I2C_IsTransmitCompleted() ;
          while ((User_Timeout_Function()) && ( Temp1 != I2C_ADD_TX_OK)) 
          {
               Temp1 = I2C_IsTransmitCompleted() ;
          }     
          switch (Temp1)                     /* To check transmission status */
          {
               case I2C_TX_AF:
                    User_Function();
                    break;
               default:
                    break;
          }
          I2C_PutByte (My_Data) ;  /* User data is copied to global variable */
          /* Here, user can perform other tasks or operations except 
             transmission till the time transmission is complete, after which 
             user can perform transmission again */
          switch (I2C_IsTransmitCompleted()) /* To check transmission status */
          {
               case I2C_TX_AF:
               case I2C_TX_ARLO:
               case I2C_TX_BERR:
                    User_Function();                     /* Error Management */
                    break;
               case I2C_DATA_TX_OK:
                    break;
               default:            /* If none of the above condition is true */
                    User_Function();
                    break;
          }
                         /* Transmission of set of data from the user buffer */
          I2C_PutBuffer (Buff,(unsigned char)10); 
                                 /* User pointer is copied to global pointer */
          /* Here, user can perform other tasks or operations except 
             transmission till the time transmission is complete, after which 
             user can perform transmission again */
          Timeoutcount = 0 ;          
          Temp1 = I2C_IsTransmitCompleted() ;
          while ( (User_Timeout_Function())&& ((Temp1) == I2C_TX_BUFFER_ONGOING))
          {
               Temp1 = I2C_IsTransmitCompleted() ;
          }                       /* To be sure that the communication by this 
                                                    point has been completed */
          if (User_Timeout_Function())
          {
               switch(Temp1)
               {
                    case I2C_TX_AF:
                    case I2C_TX_BERR:
                         User_Function();                /* Error Management */
                         break;
                    case I2C_DATA_TX_OK:          /* Transmission successful */
                         break;
                    default:       /* If none of the above condition is true */
                         User_Function();
                         break;
               }
          }
          I2C_Generate_Stop ();
          while (!(I2C_IsStopGen ())) ;
     }     
     #endif
          
            /*==========================================================
               Reception through Interrupt driven without buffer mode
             ==========================================================*/
     /* Communication mode defined as ITDRV_WITHOUTBUF_RX in
                                                             ST7lib_config.h */
                                                    /* Single byte reception */
     #ifdef I2C_ITDRV_WITHOUTBUF_RX
     I2C_Generate_Start ();
     while(!((I2C_IsTransmitCompleted())== I2C_START_OK));     
     I2C_Load_Address (Addr_Byte_Tx, I2C_RX_MODE);
     Timeoutcount = 0 ;               
     Temp1 = I2C_IsTransmitCompleted() ;
     while ((User_Timeout_Function()) && ( Temp1 != I2C_ADD_TX_OK)) 
     {
          Temp1 = I2C_IsTransmitCompleted() ;
     }     
     switch (Temp1)                  /* To check address transmission status */
     {
          case I2C_TX_AF:
               User_Function();
               break;
          default:
               break;
     }
     Timeoutcount = 0 ;                    
     Temp2 = I2C_IsReceptionCompleted() ;
     while ((User_Timeout_Function()) && ((Temp2) == I2C_RX_DATA_EMPTY) )
     {
          Temp2 = I2C_IsReceptionCompleted() ;
     } 
                                            /* Waits for data byte reception */
     if (User_Timeout_Function())
     {
          switch (Temp2)
          {
               case I2C_RX_ARLO:
               case I2C_RX_BERR:
                    Rx_Data =I2C_GetByte ();
                    User_Function();
                    break;
               case I2C_DATA_RX_OK:
                    Rx_Data =I2C_GetByte ();
                    break;
               default:               /* None of the above condition is true */
                    User_Function ();
                    break;
          }
     }
                              /* Reception of set of data in the user buffer */
     I2C_GetBuffer (Buff_Test,(unsigned char) 10);    /* Any data received before calling this
                                                         function is ignored */

     /* Here, user can perform other tasks or operations except reception till 
        the time reception is complete, after which user can perform reception 
        again */

     Timeoutcount = 0 ;                  
     Temp2 = I2C_IsReceptionCompleted() ;
     while ((User_Timeout_Function()) && ((Temp2) == I2C_RX_BUFFER_ONGOING))
     {
          Temp2 = I2C_IsReceptionCompleted() ;
     }
       /* To be sure that the communication by this point has been completed */
     if (User_Timeout_Function())
     {
          switch (Temp1)
          {
               case I2C_RX_BERR:
               case I2C_RX_BUFFER_ONGOING:
                    User_Function();                     /* Error Management */
                    break;                  
               case I2C_DATA_RX_OK:                  /* Reception successful */
                    break;
               default:            /* If none of the above condition is true */
                    break;
          }
     }
     #endif          
}

/*-----------------------------------------------------------------------------
ROUTINE NAME : I2C_User_IT_Routine
INPUT        : None
OUTPUT       : None
DESCRIPTION  : Control comes into this routine when an interrupt is generated.
               User can use the I2C interrupt service routine function or he 
               can write his own code inside this routine at his own risk. The
               data transfer syncronisation may be affected if user includes 
               his own code along with I2C ISR function.          
COMMENTS     : None
-----------------------------------------------------------------------------*/
#ifdef _HIWARE_	                                 /* Test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS            /* Additional registers will be saved */
#else
#ifdef _COSMIC_                                  /* Test for Cosmic Compiler */
@interrupt                                      /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!"                 /* Compiler Defines not found! */
#endif
#endif                                                 

void I2C_User_IT_Routine (void)
{
   I2C_IT_Function () ;            /* I2C Interrupt service routine function */
}

/*-----------------------------------------------------------------------------
                                 USER FUNCTIONS                                
-----------------------------------------------------------------------------*/                                 
                                 
BOOL User_Timeout_Function(void)
{    
    while(Timeoutcount < 50000)
    {
        Timeoutcount++ ;
        return (TRUE);                               /* Time-out not elapsed */		
    } 
    return (FALSE)  ;                                    /* Time-out elapsed */
}

void User_Function(void)
{
       I2C_Error_Clear () ;                 /* Clears error and status flags */
}
							
/*** (c) 2002   STMicroelectronics *************************** END OF FILE ***/
