/******************************************************************************
COPYRIGHT 2002 STMicroelectronics
Source File Name :  main.c  
Group            :  IPSW,CMG-IPDF
Author           : MCD Application Team
Date First Issued:  07/03/2002
********************************Documentation**********************************
General Purpose - It is application file which calls the functions in spi.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 with 
baudrate of fcpu/4.
 
Also for transmission and reception in interrupt driven case, interrupt is 
enabled for generating the interrupt.  	
********************************Revision History*******************************
_______________________________________________________________________________
Date :07/03/2002                  Release:1.0
Date :28/04/04                    Release:2.1
               1.Changed for MISRA compliance
--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**/

#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);

void main(void)
{ 
    unsigned char NbOfBytes_get = 10;
    unsigned char NbOfBytes_put = 10;		
    unsigned char Rx_Data;
    unsigned char Buff_Test[buf_size];
    unsigned char Buff[buf_size] ={0x00,0x55,0xAA,0xFF,0x00,0x55,0xAA,0xFF,0x00,
                               0x55}; 					
    SPI_TxErrCode_t Temp1= 0x00 ;
    SPI_RxErrCode_t Temp2= 0x00 ; 

    SPI_Init((((unsigned char)SPI_DEFAULT | ((unsigned char)SPI_ENABLE|
                   ((unsigned char)SPI_ENABLE_IT|((unsigned char)SPI_BAUDRATE_4|
                              ((unsigned char)SPI_CLK_PP_0)))))), SPI_MSTR_SW );   
           /* SPI Initialised in master software mode,Serial peripheral output 
             enabled, Interrupt enabled, CPOL=0, CPHA=1 and baudrate is 2MHz */
										
    EnableInterrupts      /* Interrupt mask is reset for enabling interrupt */
    
    /*=========================================================================
                        Transmission through Polling mode
    =========================================================================*/
    #ifdef SPI_POLLING_TX
                                            /* Single byte data transmission */
    SPI_PutByte (My_Data) ;                      
    Temp1 = SPI_IsTransmitCompleted();
    while ((!(User_Timeout_Function())) && (Temp1 != SPI_TRANSMIT_OK) )
    {
            Temp1 = SPI_IsTransmitCompleted() ;
    }
    if (!(User_Timeout_Function()))
    {
        switch (Temp1)                       
        {
           case (SPI_TX_MODF + SPI_TX_WCOL):       
           case (SPI_TX_MODF):                      
           case (SPI_TX_WCOL):       
                User_Function();                         /* Error Management */
                break;        
           default:                 /* If none of the above condition is met */
                User_Function();                         
                break;      
        }
    }                                                                                    
                           /* Transmission of 10 data bytes from user buffer */
    switch(SPI_PutBuffer(Buff, (unsigned char)10))
                                             
    {
       case (SPI_TX_MODF + SPI_TX_WCOL):     
       case (SPI_TX_MODF):                      
       case (SPI_TX_WCOL):           
            User_Function();                             /* Error Management */
            break;   
       case SPI_TRANSMIT_OK:                   /* Transmission is successful */
            break;             
        default:                    /* If none of the above condition is met */
            User_Function();                             
            break;   
    }                             
    #endif
    /*=========================================================================
                        Reception through Polling mode
    =========================================================================*/     
    #ifdef SPI_POLLING_RX
                                               /* Single byte data reception */
    Temp2 = SPI_IsReceptionCompleted() ;
    while ((User_Timeout_Function()) && (Temp2 == SPI_RX_DATA_EMPTY)) 
    {                                       /* Waits for data byte reception */
         Temp2 = SPI_IsReceptionCompleted() ;
    }
                                          /* Waiting for data byte reception */
    if (User_Timeout_Function())
    {
        switch (Temp2)                          
        {
             case (SPI_RX_MODF + SPI_RX_OVR):         
             case (SPI_RX_MODF):              
             case (SPI_RX_OVR):                   
                  User_Function();                       /* Error Management */
                  Rx_Data = SPI_GetByte () ; /* Corrupted data byte received */                  
                  break;
             case SPI_RECEIVE_OK:                    /* Reception successful */
                  Rx_Data = SPI_GetByte () ; 
                  break;              
             default:               /* If none of the above condition is met */
                  User_Function();                       
                  break;
        }
    }
    else
    {
          while (1) ;
           /* Handle time out as Transmitter/Receiver is having some problem */
    }                        	       
                                                 /* Reception of set of data */
    switch ((SPI_GetBuffer(Buff_Test,(unsigned char)10)))
    {
        case (SPI_RX_MODF + SPI_RX_OVR):                       
        case (SPI_RX_MODF):                   
        case (SPI_RX_OVR):                        
             User_Function();                            /* Error Management */
             break; 
        case SPI_RECEIVE_OK:                      /* Reception is successful */
             break;             
        default:                    /* If none of the above condition is met */
             User_Function();                           
             break; 
    }                     
    #endif
    /*=========================================================================
             Transmission through 'Interrupt Driven wihtout Buffer Mode'
    =========================================================================*/     
    #ifdef SPI_ITDRV_WITHOUTBUF_TX               
                                                 /* Single byte transmission */
    SPI_PutByte (My_Data) ;
            
    /* Here, user can perform other tasks or operations except transmission 
    till the time transmission is complete, after which user can perform 
    transmission again */       
               
    Temp1 = SPI_IsTransmitCompleted() ;
    while((Temp1 == SPI_TX_BUFFER_ONGOING) && (Temp1 == SPI_TX_STRING_ONGOING))
                                         /* Wait for transmission completion */
    {
         Temp1 = SPI_IsTransmitCompleted() ;
    } 
    switch (Temp1)                           
    {
        case (SPI_TX_MODF + SPI_TX_WCOL):         
        case (SPI_TX_MODF):                   
        case (SPI_TX_WCOL):          
             User_Function();                            /* Error Management */
             break;   
        case SPI_TRANSMIT_OK:                  /* Transmission is successful */           
             break;             
        default:                    /* If none of the above condition is met */
             User_Function();                           
             break;   
     }                       
                           /* Transmission of 10 data bytes from user buffer */
                             /* User pointer is copied to the global pointer */
     SPI_PutBuffer(Buff, NbOfBytes_put);
                             
     /* Here, user can perform other tasks or operations except transmission 
     till the time transmission is complete, after which user can perform 
     transmission again */                             
     
     Temp1 = SPI_IsTransmitCompleted() ;
     while ((User_Timeout_Function())&&(Temp1 == SPI_TX_BUFFER_ONGOING))
     {
          Temp1 = SPI_IsTransmitCompleted() ;           
     } /* To be sure that the communication by this point has been completed */
     if (User_Timeout_Function())
     {                            
         switch (Temp1)                      
         {
             case (SPI_TX_MODF + SPI_TX_WCOL):
                                                        
             case (SPI_TX_MODF):              
             case (SPI_TX_WCOL):     
                  User_Function();                       /* Error Management */
                  break;
             case SPI_TRANSMIT_OK:                /* Transmission successful */
                  break;             
             default:               /* If none of the above condition is met */
                  User_Function();                      
                  break;
         }                        
     }
     else
     {
           while (1) ;
           /* Time-Out elapsed without transmission completion. Error in
              communication and user should handle the case */
     }         
     #endif
    /*=========================================================================
               Reception through 'Interrupt Driven wihtout Buffer Mode'
    =========================================================================*/
    #ifdef SPI_ITDRV_WITHOUTBUF_RX
                                                    /* Single byte reception */
    Temp2 = SPI_IsReceptionCompleted() ;
    while ((User_Timeout_Function())&&(Temp2 == SPI_RX_DATA_EMPTY)) 
                                            /* Waits for data byte reception */                                          
    {
        Temp2 = SPI_IsReceptionCompleted() ;    
    }                                      
    if (User_Timeout_Function())
    {
        switch (Temp2)                          
        {
             case (SPI_RX_MODF + SPI_RX_OVR):         
             case (SPI_RX_MODF):              
             case (SPI_RX_OVR):                   
                  User_Function();                       /* Error Management */
                  Rx_Data = SPI_GetByte () ; 
                                         /* User will get the corrupted data */                  
                  break;
             case SPI_RECEIVE_OK:                    /* Reception successful */
                  Rx_Data = SPI_GetByte () ; 
                  break;              
             default:               /* If none of the above condition is met */
                  User_Function();                       
                  break;
         }
    }
    else
    {
            while (1) ;
            /* Time-Out elapsed without reception completion. Error in
               communication and user should handle the case */
    }
                                 /* Reception of the data in the user buffer */
    SPI_GetBuffer(Buff_Test,NbOfBytes_get); 
                /* Any data received before calling this function is ignored */
                
    /* Here, user can perform other tasks or operations except reception till 
       the time re-ception is complete, after which user can perform reception 
       again */ 
       
    Temp2 = SPI_IsReceptionCompleted();
    while ((User_Timeout_Function()) && (Temp2 == SPI_RX_BUFFER_ONGOING))

    {
         Temp2 = SPI_IsReceptionCompleted();        
    }  /* To be sure that the communication by this point has been completed */

    if (User_Timeout_Function())
    {
         switch (Temp2)                         
         {
             case (SPI_RX_MODF + SPI_RX_OVR):                    
             case (SPI_RX_MODF):              
             case (SPI_RX_OVR):                   
                  User_Function();                       /* Error Management */
                  break;
             case SPI_RECEIVE_OK:                    /* Reception successful */
                  break;             
             default:               /* If none of the above condition is met */
                  User_Function();                       
                  break;
         }                   
    }
    else
    {
            while (1) ;
            /* Time-Out elapsed without reception completion. Error in
               communication and user should handle the case */
    }    
    #endif
}

/*-----------------------------------------------------------------------------
ROUTINE NAME : SPI_User_IT_Routine
INPUT        : None
OUTPUT       : None
DESCRIPTION  : Control comes into this routine when an interrupt is generated.
               User can use the SPI 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 SPI 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 SPI_User_IT_Routine (void)
{
   SPI_IT_Function () ;            /* SPI 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)
{
       SPI_Clear_Flags ();                  /* Clears error and status flags */
}
							
/*** (c) 2002   STMicroelectronics *************************** END OF FILE ***/
