/*   
*******************************************************************************
COPYRIGHT 2003 STMicroelectronics
Source File Name : main.c
Group            : IPSW, CMG - IPDF
Author           : MCD Application Team
Date First Issued: 22/09/2003
********************************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 when ART is generating PWM pulses.
     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 : 22/09/2003              	Release:  2.0
******************************************************************************/     
#include "ST7lib_config.h"                               /*Configuration File*/ 
#include "main.h"   /* Contains the global variables and functions prototype */

/******************************************************************************
                          MAIN APPLICATION BEGINS
******************************************************************************/
void PWM_Pulse_Change (void);
void LED_FLASH (void);  
void MSG_Display (void); 
void IO_Config (void);
void EI1_IT_Routine (void);
void LT_TB1_IT_Routine (void);
void main (void);
void main (void)                    
{                                    
                     /* Declaration of the variables used in the application */                                                           
    unsigned char i,sub_add;
    unsigned char channel_no = 4;
    
                                                                             
    /**************************************************************************
                           ADC, ART and LT Application
    **************************************************************************/    
    #ifdef TIMERS_ADC_APPLICATION                      
    /********************** All libraries initialized ************************/    
    /* Intialization of the peripherals */  
        LT_Init(LT_DEFAULT);
    Temp_Count  = 0 ;
    IO_Init () ;  
    ITC_Init () ;                                                              
    EEPROM_Init () ;        
    LART_Init (LART_COUNTER_CLK_FCPU) ; 
    

    IO_Write(IO_PORT_A,IO_PIN_3,IO_DATA_HIGH);

    /*********Lite timer is enabled to generate interrupt after 1 ms *********/    
    LT_Init (LT_TB1_IT_ENABLE) ;
    IO_Write (IO_PORT_A,IO_PIN_0,IO_DATA_HIGH);                                       
    
    /* Checking for IO_ByteWrite */
    IO_Output(IO_PUSH_PULL,IO_PORT_A,0xff); 
                                    /* make all Port A pin push-pull         */   
    IO_ByteWrite(IO_PORT_A,(unsigned char)0x12);
    IO_Config();
    
    /* Configure the Port A 6 Pin as EI1 Interrupt as low level or 0*/
    EnableInterrupts ;  
    ITC_ConfigureInterrupt(IT_PortA,(unsigned char)4,IT_EDGE_F_0);
           
    Nop ;
    /* Making Pin 4 of Port A as Input */
    
    IO_Input(IO_PULL_UP,IO_PORT_A,IO_PIN_4);    
    
    /************** Message "LT, ART STARTED" written in EEPROM **************/           
    Temp_Count ++ ;
    MSG_Display () ;        
    IO_Write (IO_PORT_A,IO_PIN_1, IO_DATA_HIGH);                               
    
    /*********** ART is enabled to generate interrupt PWM pulses *************/    
    
    LART_ConfigurePWM(LART_PWM0,(unsigned int)0xFF8,LART_POLARITY_1,(unsigned 
                                                                    int)0xFFE);
    while ( (unsigned int)(counter/4) < 15000)
    {        
        LED_FLASH () ;
    }      
        
    /************************* ADC conversion starts *************************/
    /************ Converted ADC output values stored in Data EEPROM **********/        
    ADC_Init (ADC_SPEED);
    ADC_Select_Channel(channel_no);
    ADC_Enable ();                                     /*Start ADC conversion*/
    Temp_Count ++ ;
    IO_Write (IO_PORT_A,IO_PIN_3,IO_DATA_HIGH);    
    while(1)
    {
    for (i=0;i<10;i++)
        {
            while (!(ADC_Test_Conversn_Complete ()));     /*check for the EOC*/
            Buff3[i]= ADC_Conversn_Read ();   /*store the converted data*/      
        }               
        MSG_Display () ;
        LED_FLASH () ;
        PWM_Pulse_Change ();
    }
    #endif                                                                     

    /**************************************************************************
                              SPI and LT Application
    **************************************************************************/    
    #ifdef SPI_TIMER_APPLICATION                                                   
    /********************** All libraries initialized ************************/    
       
    IO_Init () ;
    LART_Init(LART_COUNTER_CLK_FCPU) ; 
    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)));
    LART_ConfigurePWM(LART_PWM0,(unsigned int)0xFF8,LART_POLARITY_0,(unsigned 
                                                                   int)0xFFE);
						     /* PWM pulse generation */
                                            
    /*************** 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 ((unsigned char)1)
    {
        IO_Write (IO_PORT_A,IO_PIN_4,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));
                                      /*check for the successful transmission*/
        SPI_PutByte((unsigned char)0xF0);       
                                    /*clear all flags in the EEPROM registers*/
        while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
                                      /*check for the successful transmission*/
        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));
                                      /*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));
                                      /*check for the successful transmission*/
            SPI_PutByte(sub_add);            /*load the sub address of EEPROM*/
            while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
                                      /*check for the successful transmission*/
            SPI_PutByte((unsigned char)Buff4[i]);      /*write data on EEPROM*/
            while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
                                      /*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));
                                      /*check for the successful transmission*/
                SPI_PutByte((unsigned char)0x01);        
                                                    /*generate 8 clock pulses*/
                while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));
                                      /*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_4,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));      	 
                                      /*check for the successful transmission*/
            SPI_PutByte(sub_add);                     /*load the read address*/
            while(!(SPI_IsTransmitCompleted()==SPI_TRANSMIT_OK));      	 
                                      /*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));
            Buff5[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 (Buff4[i] != Buff5[i])
            {
                IO_Write (IO_PORT_A,IO_PIN_1,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 PWM_Pulse_Change (void)
{  
    if (((counter/30000)%2)==0)
    {
        LART_ConfigurePWM(LART_PWM0,(unsigned int)0x55A,LART_POLARITY_1,
                                                          (unsigned int)0x803);
    }
    else
    {
        LART_ConfigurePWM(LART_PWM0,(unsigned int)0x55A,LART_POLARITY_0,
                                                          (unsigned int)0x803);        
    }
}                                                                              
                                                              /* Flashes LED */
void LED_FLASH (void) 
{
    if (((unsigned int)(counter/15000)%2)==0)
    {
        IO_Write (IO_PORT_A,IO_PIN_7,IO_DATA_TOGGLE);             
    }    
}
                                          /* Writes message in EEPROM memory */  
void MSG_Display (void) 
{
    if (Temp_Count ==1 )
    {
        EEPROM_Write (Buff1,(unsigned char)16,(unsigned char *)0x1000);             
    }                                                   
    else
    {
        EEPROM_Write (Buff2,(unsigned char)31,(unsigned char *)0x1020);                 
    }                                                
    while (EEPROM_Programming () != EEPROM_PROG_COMPLETE);    
} 
 /* Port registers used directly to avoid overlapping in Lite Program memory */
void IO_Config (void)  
{
    PADDR |= 0xBB ;
    PAOR |= 0xBB ;
}                          
#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 
    WDG_Refresh (CNTR_Value) ;			/* Reads the Counter value   */
    #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_TB1_IT_Routine (void)
{   
    #ifdef TIMERS_ADC_APPLICATION     
    LT_Clear_Flag(LT_FLAG_TB1F);               /* Clears Lite timer interrupt*/
    counter ++ ;	
    #endif
}

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