/*****************************************************************************
COPYRIGHT 2002 STMicroelectronics
Source File Name   : IO.c 
Group              : IPSW,CMG-IPDF
Author             : MCD Application Team
Date First Issued  : 07/03/2002
Date Upgrade Issued: 16/09/2003
********************************Documentation**********************************
General Purpose - Contains source code for all the functions of IO

********************************Revision History*******************************
_______________________________________________________________________________
Date :07/03/2002    Release:1.0       
Date :16/09/2003    Release:2.0 
                    1.Moved all register defintions to single file
                    2.Used indirect offset method to remove switch cases
Date :26/05/2004    Release:2.1
                    1.Changed for MISRA compliance
Date :16/03/2006    Release:BETA
                    1.Add IO_LITEUS. Only PORT A available for UltraLite
******************************************************************************/

#include "ST7lib_config.h"                            /* Selection of device */
#include "IO_hr.h"                           /* IO registers bit definitions */
#include "IO.h"             /* Prototype definitions of IO library functions */

/* Port Register addresses                                                   */
#ifdef IO_72F521  

#ifdef _HIWARE_
#pragma INTO_ROM
static volatile unsigned char * const PortAddr[]={&PADR,&PBDR,&PCDR,&PDDR,&PEDR,
                                                        &PFDR,&PGDR,&PHDR};    
#endif 

#ifdef _COSMIC_
@tiny static unsigned char * const PortAddr[]={&PADR,&PBDR,&PCDR,&PDDR,
                                                      &PEDR,&PFDR,&PGDR,&PHDR};
#endif

#ifdef _RAISONANCE_
static hreg * code PortAddr[]={&PADR,&PBDR,&PCDR,&PDDR,
                                                      &PEDR,&PFDR,&PGDR,&PHDR};
#endif

#endif

#if (defined IO_72F65||defined IO_72F561||defined IO_72F34x) 

#ifdef _HIWARE_
#pragma INTO_ROM
static volatile unsigned char * const PortAddr[]={&PADR,&PBDR,&PCDR,&PDDR,&PEDR,
                                                                         &PFDR};
#endif

#ifdef _COSMIC_
@tiny static unsigned char * const PortAddr[]={&PADR,&PBDR,&PCDR,&PDDR,
                                                     &PEDR,&PFDR};    
#endif

#ifdef _RAISONANCE_
static hreg * code PortAddr[]={&PADR,&PBDR,&PCDR,&PDDR,&PEDR,
                                                                         &PFDR};
#endif

#endif

#ifdef IO_72F63 

#ifdef _HIWARE_
#pragma INTO_ROM
static volatile unsigned char * const PortAddr[]={&PADR,&PBDR,&PCDR};    
#endif                                   

#ifdef _COSMIC_
@tiny static unsigned char * const PortAddr[]={&PADR,&PBDR,&PCDR};    
#endif

#ifdef _RAISONANCE_
static hreg * code PortAddr[]={&PADR,&PBDR,&PCDR,&PDDR};    
#endif

#endif

#if (defined IO_72F264) || (defined IO_72F60)

#ifdef _HIWARE_
#pragma INTO_ROM
static volatile unsigned char * const PortAddr[]={&PCDR,&PBDR,&PADR};    
#endif          

#ifdef _COSMIC_
@tiny const static unsigned char * const PortAddr[]={&PCDR,&PBDR,&PADR};    
#endif

#ifdef _RAISONANCE_
static hreg * code PortAddr[]={&PCDR,&PBDR,&PADR};    
#endif 

#endif

#if (defined IO_DALI||\
defined IO_LITE0|| defined IO_SUPERLITE ||defined IO_LITE1||defined IO_LITE2||defined IO_LITE3) 

#ifdef _HIWARE_
#pragma INTO_ROM
static volatile unsigned char * const PortAddr[]={&PADR,&PBDR};    
#endif

#ifdef _COSMIC_
@tiny static unsigned char * const PortAddr[]={&PADR,&PBDR};
#endif  

#ifdef _RAISONANCE_
static hreg * code PortAddr[]={&PADR,&PBDR};    
#endif

#endif


#if (defined IO_LITEUS) 
#ifdef _HIWARE_
#pragma INTO_ROM
static volatile unsigned char * const PortAddr[]={&PADR};    
#endif

#ifdef _COSMIC_
@tiny static unsigned char * const PortAddr[]={&PADR};
#endif    

#ifdef _RAISONANCE_
static hreg * code PortAddr[]={&PADR};    
#endif

#endif


#ifdef IO_72F62 

#ifdef _HIWARE_
#pragma INTO_ROM
static volatile unsigned char * const PortAddr[]={&PADR,&PBDR,&PCDR,&PDDR};    
#endif 

#ifdef _COSMIC_
@tiny static unsigned char * const PortAddr[]={&PADR,&PBDR,&PCDR,&PDDR};    
#endif

#ifdef _RAISONANCE_
static hreg * code PortAddr[]={&PADR,&PBDR,&PCDR,&PDDR};    
#endif 

#endif


#ifdef IO_LITE49M

#ifdef _HIWARE_
#pragma INTO_ROM
static volatile unsigned char * const PortAddr[]={&PADR,&PBDR,&PCDR};    
#endif 

#ifdef _COSMIC_
@tiny static unsigned char * const PortAddr[]={&PADR,&PBDR,&PCDR};    
#endif

#ifdef _RAISONANCE_
static hreg * code PortAddr[]={&PADR,&PBDR,&PCDR};    
#endif 

#endif

/*-----------------------------------------------------------------------------
ROUTINE NAME : IO_Init
INPUT        : None
OUTPUT       : None	       
DESCRIPTION  : Initialize all port registers.
COMMENTS     : Must be called before starting any IO operation.
-----------------------------------------------------------------------------*/ 
void IO_Init (void)
{
#ifdef IO_72F521  /* Initialisation of all port registers of ST72F521 device */
    PADDR = 0x00 ;
    PBDDR = 0x00 ;
    PCDDR = 0x00 ;
    PDDDR = 0x00 ;
    PEDDR = 0x00 ;
    PFDDR = 0x00 ;
    PAOR = 0x00 ;  
    PBOR = 0x00 ;  
    PCOR = 0x00 ;  
    PDOR = 0x00 ;  
    PEOR = 0x00 ;  
    PFOR = 0x00 ;  
#ifdef IO_521_80PIN 
    PGDDR = 0x00 ;
    PHDDR = 0x00 ;
    PGOR = 0x00 ;
    PHOR = 0x00 ;
#endif    
#endif          

#ifdef IO_72F62    /* Initialisation of all port registers of ST72F62 device */
    PADDR = 0x00;
    PBDDR = 0x00;
#ifdef IO_62_32PIN
    PCDDR = 0x00 ;    
#endif
#ifdef IO_62_42PIN
    PCDDR = 0x00 ; 
    PDDDR = 0x00 ;    
#endif
#endif

#ifdef IO_72F65    /* Initialisation of all port registers of ST72F65 device */
    PADDR = 0x00;  
    PBDDR = 0x00;  
    PCDDR = 0x00;  
    PDDDR = 0x00;  
    PEDDR = 0x00;  
    PFDDR = 0x00;  
    PAOR = 0x00;
    PCOR = 0x00;
    PDOR = 0x00;
    PEOR = 0x00;
#endif          

#ifdef IO_72F561  /* Initialisation of all port registers of ST72F561 device */
    PADDR = 0x00;  
    PBDDR = 0x00;  
    PCDDR = 0x00;  
    PDDDR = 0x00;  
    PEDDR = 0x00;  
    PFDDR = 0x00;  
    PAOR = 0x00;
    PBOR = 0x00;
    PCOR = 0x00;
    PDOR = 0x00;
    PEOR = 0x00;
    PFOR = 0x00;
#endif     
     
#ifdef IO_72F34x /* Initialisation of all port registers of ST72F34x devices */
    PADDR = 0x00;  
    PBDDR = 0x00;  
    PCDDR = 0x00;  
    PDDDR = 0x00;  
    PEDDR = 0x00;  
    PFDDR = 0x00;  
    PAOR = 0x00;
    PBOR = 0x00;
    PCOR = 0x00;
    PDOR = 0x00;
    PEOR = 0x00;
    PFOR = 0x00;
#endif     

#ifdef IO_72F63    /* Initialisation of all port registers of ST72F63 device */ 
    PADDR = 0x00  ;
    PBDDR = 0x00  ;
    PCDDR = 0x00  ;
    PDDDR = 0x00  ;
#endif          

#if(defined IO_72F264||defined IO_LITE49M) /* Initialisation of all port registers of ST72F264 device */
    PADDR = 0x00;  
    PBDDR = 0x00;  
    PCDDR = 0x00;  
    PAOR = 0x00;
    PBOR = 0x00;
    PCOR = 0x00;
#endif 


#if (defined IO_DALI||\
defined IO_LITE0|| defined IO_SUPERLITE || defined IO_LITE1||defined IO_LITE2||defined IO_LITE3)
    PADDR = 0x00 ;
    PBDDR = 0x00 ;
    PAOR  = 0x00 ;  
    PBOR  = 0x00 ;  
#endif  

#if (defined IO_LITEUS)
    PADDR = 0x00 ;
    PAOR  = 0x00 ;  
#endif                    
}                                                        
                                                        
/*-----------------------------------------------------------------------------
ROUTINE NAME : IO_Input
INPUT        : Input port mode, Port name, Port pin number.
OUTPUT       : None.	       
DESCRIPTION  : Configures the selected port pin in the selected input mode. 
COMMENTS     : None
-----------------------------------------------------------------------------*/
void IO_Input (IO_Input_Mode Input_Val, IO_Port Port_Val1, IO_Pin Pin_Val1)
{                  
    volatile unsigned char *PortDDR, *PortOR;

    PortDDR = (PortAddr[Port_Val1]+1) ;
    
    *PortDDR &= (unsigned char)(~(unsigned char)( Pin_Val1)) ;
    
#if (defined IO_72F521|| defined IO_LITE0||defined IO_SUPERLITE ||defined IO_LITE1||defined IO_LITE2||\
defined IO_LITE3||defined IO_DALI||\
defined IO_72F264||defined IO_72F561||defined IO_LITEUS||defined IO_72F34x||defined IO_LITE49M)
    

    PortOR = (PortAddr[Port_Val1]+2);

    
    if (Input_Val == IO_FLOATING )
    {                                   /* Configured as floating input mode */
        *PortOR &= (unsigned char)(~(unsigned char)( Pin_Val1 )) ;
    }
    else         
    {                                    /* Configured as pull-up input mode */ 
        *PortOR |= (unsigned char)(Pin_Val1) ;           
    }    
#endif    

#ifdef IO_72F65 
    
    if(((Port_Val1==IO_PORT_A)||(Port_Val1==IO_PORT_C))||\
          ((Port_Val1==IO_PORT_D)||(Port_Val1==IO_PORT_E)))
    {
        PortOR = (PortAddr[Port_Val1]+2);
    
        if (Input_Val == IO_FLOATING )
        {                               /* Configured as floating input mode */
            *PortOR &= (unsigned char)(~(unsigned char)( Pin_Val1 )) ;                         
        }
        else         
        {                                /* Configured as pull-up input mode */ 
            *PortOR |= (unsigned char)(Pin_Val1) ;           
        } 
    }
       
#endif                 
} 

/*-----------------------------------------------------------------------------
ROUTINE NAME : IO_Output
INPUT        : Output port mode, Port name, Port pin number. 
OUTPUT       : None.	       
DESCRIPTION  : Configures the selected port pin in the selected output mode. 
COMMENTS     : None.
-----------------------------------------------------------------------------*/
void IO_Output (IO_Output_Mode Output_Val,IO_Port Port_Val2, IO_Pin Pin_Val2)
{                   

    volatile unsigned char *PortDDR, *PortOR;
    
    PortDDR = PortAddr[Port_Val2]+1;
    *PortDDR |= (unsigned char)Pin_Val2 ;  /* Port configured in output mode */
    
#if (defined IO_72F521|| defined IO_LITE0||defined IO_SUPERLITE ||defined IO_LITE1||defined IO_LITE2||\
defined IO_LITE3||defined IO_DALI||\
defined IO_72F264||defined IO_72F561||defined IO_LITEUS||defined IO_72F34x||defined IO_LITE49M)

    PortOR = PortAddr[Port_Val2]+2;
    if (Output_Val == IO_OPEN_DRAIN )
    {                                /* Configured as open-drain output mode */
        *PortOR &= (unsigned char)(~(unsigned char)( Pin_Val2 )) ;
    }
    else
    {                                 /* Configured as push-pull output mode */
        *PortOR |= (unsigned char)(Pin_Val2 ) ;           
    }    
#endif

#ifdef IO_72F65 

    if(((Port_Val2==IO_PORT_A)||(Port_Val2==IO_PORT_C))||\
          ((Port_Val2==IO_PORT_D)||(Port_Val2==IO_PORT_E)))
    {
        PortOR = (PortAddr[Port_Val2]+2);
        if (Output_Val == IO_OPEN_DRAIN )
        {                            /* Configured as open-drain output mode */
            *PortOR &= (unsigned char)(~(unsigned char)( Pin_Val2 )) ;                         
        }
        else
        {                             /* Configured as push-pull output mode */
            *PortOR |= (unsigned char)(Pin_Val2 ) ;           
        }        
    } 
    
#endif

}    
 
/*-----------------------------------------------------------------------------
ROUTINE NAME : IO_Read
INPUT        : Port name
OUTPUT       : Returns the selected port data register status.
DESCRIPTION  : Reads the port and returns the value to user. 
COMMENTS     : None
-----------------------------------------------------------------------------*/
unsigned char IO_Read (IO_Port Read_Val)
{   
    volatile unsigned char *PortDR;

    PortDR = PortAddr[Read_Val];
    return *PortDR;
}

/*-----------------------------------------------------------------------------
ROUTINE NAME : IO_Write
INPUT        : Port name, Port pin number, data to be written (High/Low)
OUTPUT       : None
DESCRIPTION  : Writes the data into the port register.. 
COMMENTS     : None
-----------------------------------------------------------------------------*/
void IO_Write (IO_Port Port_Val3,IO_Pin Pin_Val3, IO_Write_Data Data_Val)
{
    volatile unsigned char *PortDR;
    
    PortDR = PortAddr[Port_Val3];

    if (Data_Val == IO_DATA_HIGH )
    {                            /* Writes Logical high in selected port pin */
        *PortDR |= (unsigned char)Pin_Val3 ; 
    } 
    else if (Data_Val == IO_DATA_TOGGLE )
    {                                
        *PortDR ^= (unsigned char)(Pin_Val3) ;  /* Toggles selected port pin */                                   
    }
    else
    {                             /* Writes Logical low in selected port pin */
        *PortDR &= (unsigned char)(~(unsigned char)(Pin_Val3)) ;
    }
}  

/*-----------------------------------------------------------------------------
ROUTINE NAME : IO_ByteWrite
INPUT        : Port name, data byte to be written 
OUTPUT       : None
DESCRIPTION  : Writes the data into the port register.. 
COMMENTS     : None
-----------------------------------------------------------------------------*/
void IO_ByteWrite (IO_Port Port_Val4,unsigned char IO_ByteData)
{   
    volatile unsigned char *PortDR;

    PortDR = PortAddr[Port_Val4];
    *PortDR = IO_ByteData;
	

}
/*** (c) 2002  ST Microelectronics ****************** END OF FILE ************/
