/*   
*******************************************************************************
COPYRIGHT 2003 STMicroelectronics
Source File Name : main.c
Group            : IPSW, CMG - IPDF
Author           : MCD Application Team
Date First Issued: 03/10/2004
********************************Documentation**********************************
General Purpose -Top Level file.This file calls the functions, written in 
     different source directories of ST7 Library. In SPI-Timers application,
     SPI communicates with M95040 EEPROM.
     In ADC-Timers application ADC converts analog signal to digital 
     when lite timer is generating interrupts after every 1ms and LART is 
     generating PWM pulses.

*******************************************************************************     
********************************RevisionHistory********************************
_______________________________________________________________________________
Date : 03/10/2004              	Release:  2.1
******************************************************************************/     
#include "ST7lib_config.h"                               /*Configuration File*/ 
#include "main.h"  

#pragma DATA_SEG SHORT DEFAULT_RAM                     

        

/******************************************************************************
                          MAIN APPLICATION BEGINS
******************************************************************************/

void main (void);        //Prototype for main
void main (void)                    
{                                    
                     /* Declaration of the variables used in the application */                                                           
    unsigned char i ; 
    
                                                      
    /**************************************************************************
                           ADC, ART and LT Application
    **************************************************************************/    
    #ifdef TIMERS_ADC_APPLICATION                                
    
    /********************** All libraries initialized ************************/    
    LT_Init(LT_DEFAULT);
    EnableInterrupts          
    IO_Init () ;  
    ITC_Init () ;                                                              
    LART_Init (LART_COUNTER_CLK_FCPU) ;    
    IO_Config () ;   
    
    /*********Lite timer is enabled to generate interrupt after 1 ms *********/    
    LT_Init (LT_TB_IT_ENABLE) ;
    IO_Write (IO_PORT_A,IO_PIN_3,IO_DATA_HIGH);                                       
    IO_Write (IO_PORT_A,IO_PIN_1, IO_DATA_HIGH);                               
    
    /*********** ART is enabled to generate interrupt PWM pulses *************/    
    LART_PWM_Mode(0xCE0, LART_POLARITY_0, 0xDD0);
         while ( (counter/4) < 15000)
    {        
        LED_FLASH () ;
    }     
      
       /************************* ADC conversion starts *************************/
    /************ Converted ADC output values stored in Buff2**********/        
    IO_Input(IO_FLOATING,IO_PORT_B,IO_PIN_3);
    ADC_Init (ADC_SPEED);
    ADC_Select_Channel(3);
    ADC_Enable ();      /*Start ADC conversion*/
    IO_Write (IO_PORT_A,IO_PIN_4,IO_DATA_HIGH);    
    while (1)
    {
        for (i=0;i<10;i++)
        {
            while (!(ADC_Test_Conversn_Complete ()));     /*check for the EOC*/
            Buff2[i]= ADC_Conversn_Read ();   /*store the converted data*/      
        }               
          LED_FLASH () ;
    }
    #endif
    /**************************************************************************
                              SPI and LT Application
    **************************************************************************/    

    #ifdef SPI_TIMER_APPLICATION                                                   
    /********************** All libraries initialized ************************/    
    unsigned char sub_add ;
    IO_Init ();
    MCC_Init (MCC_DEFAULT);                 
    IO_Output (IO_PUSH_PULL, IO_PORT_A, ((unsigned char)IO_PIN_1 |((unsigned char)IO_PIN_3|((unsigned char)IO_PIN_4 |((unsigned char)IO_PIN_7)))));
                                                

    /*************** SPI communication with 95040 EEPROM started *************/
    /************* Message "SPI STARTED" is written in data EEPROM ***********/
    SPI_Init (((unsigned char)SPI_ENABLE | ((unsigned char)SPI_BAUDRATE_16 | ((unsigned char)SPI_CLK_PP_0))), SPI_MSTR_SW) ;
    while (1)
    {
        IO_Write (IO_PORT_A,IO_PIN_1,IO_DATA_HIGH);
        IO_Write (IO_PORT_A, IO_PIN_7, IO_DATA_LOW);
        SPI_PutByte((unsigned char)0x01);                       /*access the EEPROM register*/
        while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
        SPI_Clear_Flags();   /*check transmission complete and clear flag set*/
        SPI_PutByte((unsigned char)0xF0);          /*clear all flags in the EEPROM registers*/
        while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
        SPI_Clear_Flags();                                       
        IO_Write (IO_PORT_A, IO_PIN_7, IO_DATA_HIGH);          /*set PA7 High*/
       
        /**************** SPI to EEPROM (95040) WRITE OPERATION ******************/        
        sub_add = 0x00 ;
        for (i=0;i<10;i++)
        { 
            IO_Write (IO_PORT_A, IO_PIN_7, IO_DATA_LOW);        /*set PA7 low*/
            SPI_PutByte((unsigned char)0x06);                /*access the register of EEPROM*/
            while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
            SPI_Clear_Flags();        /*check for the successful transmission*/                          
            IO_Write (IO_PORT_A, IO_PIN_7, IO_DATA_HIGH);      /*set PA7 high*/
      
            IO_Write (IO_PORT_A, IO_PIN_7, IO_DATA_LOW);        /*set PA7 low*/   	     
            SPI_PutByte((unsigned char)0x02);           /*write in the data memory of EEPROM*/
            while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
            SPI_Clear_Flags();        /*check for the successful transmission*/
            SPI_PutByte(sub_add);            /*load the sub address of EEPROM*/
            while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
            SPI_Clear_Flags();        /*check for the successful transmission*/
            SPI_PutByte((unsigned char)Buff3[i]);      /*write data on EEPROM*/
            while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
            SPI_Clear_Flags();        /*check for the successful transmission*/
            IO_Write (IO_PORT_A, IO_PIN_7, IO_DATA_HIGH);      /*set PA7 high*/
            sub_add++;
    
          
          do
            {
                IO_Write (IO_PORT_A, IO_PIN_7, IO_DATA_LOW);   	     
                SPI_PutByte((unsigned char)0x05);      /* Read the status register of EEPROM*/
                while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
                SPI_Clear_Flags();    /*check for the successful transmission*/
                SPI_PutByte((unsigned char)0x01);                  /*generate 8 clock pulses*/
                while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
                SPI_Clear_Flags();    /*check for the successful transmission*/
                IO_Write (IO_PORT_A, IO_PIN_7, IO_DATA_HIGH);  /*set PA7 high*/
            } while (SPI_GetByte() & 0x01);  /*check write complete in EEPROM*/
         
        }
        IO_Write (IO_PORT_A,IO_PIN_1,IO_DATA_LOW);                                      
        
    /************** SPI to EEPROM (95040) READ OPERATION *********************/     
                
        sub_add=0x00 ;               
        IO_Write (IO_PORT_A,IO_PIN_3,IO_DATA_HIGH);
        for (i=0;i<10;i++)
        {
            IO_Write (IO_PORT_A, IO_PIN_7, IO_DATA_LOW);   	    /*set PA7 low*/
            SPI_PutByte((unsigned char)0x03);            /*Set the EEPROM for read operation*/
            while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));      	 
            SPI_Clear_Flags();        /*check for the successful transmission*/
            SPI_PutByte(sub_add);                     /*load the read address*/
            while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));      	 
            SPI_Clear_Flags();        /*check for the successful transmission*/                         
            SPI_PutByte((unsigned char)0x01);                      /*generate 8 clock pulses*/
            while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
            while(!(SPI_IsReceptionCompleted()==SPI_RECEIVE_OK));
            Buff4[i] = SPI_GetByte();              /*get the data from EEPROM*/
            IO_Write (IO_PORT_A, IO_PIN_7, IO_DATA_HIGH);      /*set PA7 high*/
            sub_add++;  
            
            if (Buff3[i] != Buff4[i])
            {
                IO_Write (IO_PORT_A,IO_PIN_4,IO_DATA_HIGH);
                        /* LED to indicate that problem in SPI communication */
                while (1);
            }
           }                   
        IO_Write (IO_PORT_A,IO_PIN_3,IO_DATA_LOW);  
            
                        /* LED to indicate that SPI communication successful */
    }

    #endif        
}

/********************************END OF MAIN**********************************/

/******************************************************************************
                              USER DEFINED FUNCTIONS
******************************************************************************/
#ifdef TIMERS_ADC_APPLICATION
                                          /* After 60 sec PWM pulses changed */
void LED_FLASH () 
{
    if (((counter/15000)%2)==0)
    {
        IO_Write (IO_PORT_A,IO_PIN_3,IO_DATA_TOGGLE);             
    }    
}
                                          
 /* Port registers used directly to avoid overlapping in Lite Program memory */
void IO_Config ()  
{
    PADDR &= 0x7E ;
    PADDR |= 0x1A ;
    PAOR |= 0x9A ;
}                          
#endif
/*---------------------------------------------------------------------------*/
/**************************Interrupt Service Routines*************************/
/******************************************************************************
                          Interrupt Subroutine for EI0
******************************************************************************/
 
#ifdef _HIWARE_                                  /* test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS            /* additional registers will be saved */
#else
#ifdef _COSMIC_                                  /* test for Cosmic Compiler */
@interrupt @nostack                              /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!"                 /* Compiler Defines not found! */
#endif
#endif

void EI1_IT_Routine (void)
{
    #ifdef TIMERS_ADC_APPLICATION 
    LT_WDG_Enable () ;                                        /* Enables WDG */
    #endif
}

/******************************************************************************
                          Interrupt Subroutine for Lite Timer
******************************************************************************/
#ifdef _HIWARE_                                  /* test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS            /* additional registers will be saved */
#else
#ifdef _COSMIC_                                  /* test for Cosmic Compiler */
@interrupt @nostack                             /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!"                 /* Compiler Defines not found! */
#endif
#endif

void LT_TB_IT_Routine (void)
{   
    #ifdef TIMERS_ADC_APPLICATION     
    LT_Clear_Flag(LT_FLAG_TBF);               /* Clears Lite timer interrupt */           
    counter ++ ;	
    #endif
}

/**** (c) 2003   STMicroelectronics *************************** END OF FILE **/









