⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2cs.c

📁 ST7 Mcu I2C slave mode code for learning
💻 C
📖 第 1 页 / 共 3 页
字号:
/*****************************************************************************
COPYRIGHT 2003 STMicroelectronics
Source File Name :  i2cs.c 
Group            : IPSW,CMG
Author           : MCD Application Team
Date First Issued: 14/10/2003
********************************Documentation**********************************
General Purpose - Contains source code for all the functions of i2cs

********************************Revision History*******************************
_______________________________________________________________________________
Date :07/03/2002                 Release:1.0        	   	   	      	       		        
******************************************************************************/

#include "ST7lib_config.h"                            /* Selection of device */
#include "i2cs_hr.h"                     /* Declaration of i2cs HW registers */                        
#include "i2cs.h"         /* Prototype definitions of i2cs library functions */

/*---------------------------------------------------------------------------*/
                                          /* Declaration of Global Variables */
#if (defined I2C_ITDRV_WITHOUTBUF_RX ||defined I2C_ITDRV_WITHOUTBUF_TX )
/* Global address variable for Interrupt Mode                                */
static unsigned char* I2Cs_GlobalBuff ;  
    #endif    

/* Transmitter buffer count                                                  */
static unsigned char I2Cs_IntrCounter;      

/* Error variable for transmission and reception                             */
static unsigned char I2CST1;    
static unsigned char I2CST2; 

/* Stores the maximum size of the buffer                                     */
static unsigned char I2Cs_MaxSize;
                            
/* Global variable for storing the communication mode                        */ 
static I2Cs_Mode I2Cs_Com_Mode = I2Cs_DEFAULT; 

static BOOL I2Cs_StartStatus = FALSE;    

static BOOL I2Cs_Reception = FALSE;                                   

static BOOL I2Cs_Transmission = FALSE;

static BOOL FirstByte = FALSE;

/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_Init
INPUT 1      : I2Cs_DEFAULT_PARAM - Default value
               I2Cs_ENABLE_ACK - Enable the Ack bit
               I2Cs_ENABLE_ENGC - Enable the ENGC bit
INPUT 2      : I2Cs_OAR1Value - Lower address byte 
INPUT 3      : I2Cs_OAR2Value - Higher address byte                
OUTPUT       : None.	       
DESCRIPTION  : Initialize the control register and status register. Configure 
               polarity,phase and baudrate of i2cs clock. Master/ slave in 
               software/ hardware mode can be selected and Interrupt and i2cs 
               can be enabled. 
COMMENTS     : Must be called before starting any i2cs operation.
-----------------------------------------------------------------------------*/ 
void I2Cs_Init( I2Cs_Control_Param InitParam,unsigned char I2Cs_OAR1Value,
               unsigned char I2Cs_OAR2Value)
{	
    /* Clear past request and clear all register                             */
    I2CDR = I2Cs_DEFAULT_PARAM; 
    I2CCR = I2Cs_DEFAULT_PARAM; 

    #if (defined I2C_72F63)
    I2COAR = I2Cs_OAR1Value;
    #endif        

    #if (defined I2C_72F521 || defined I2C_72F264 )
    I2COAR1 = I2Cs_OAR1Value;         
    if ( Fcpu >= 6000000)
        I2COAR2 = 0x40;
    else
        I2COAR2 = 0x00;                                                           
    /* Higher byte and FRi bits                                              */
    I2COAR2 |= (unsigned char)(I2Cs_OAR2Value & 0x3F);
    #endif                                                                                        
    /* Enable peripheral                                                     */    
    I2CCR |= I2Cs_PE;   
      /* Sets the control bits depending on the settings                     */
    //I2CCR |= (unsigned char)( InitParam | I2Cs_PE );
    I2CCR |= (unsigned char)(((unsigned char)InitParam |(unsigned char)I2Cs_PE));
                                               //InitParam & I2Cs_Param typecasted 
    I2Cs_MaxSize = 0;
    I2Cs_Com_Mode = I2Cs_DEFAULT;
    #if (defined I2C_ITDRV_WITHOUTBUF_RX ||defined I2C_ITDRV_WITHOUTBUF_TX )
    I2Cs_IntrCounter = 0;
    I2CST1 = 0;
    I2CST2 = 0;
    I2Cs_StartStatus = FALSE;
    /* Enable Interrupt for Interrupt mode I2C Slave                         */               
    I2CCR |= I2Cs_ITE; 
    #endif
                       
}                                                                              


/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_GetCommMode
INPUT        : None
OUTPUT       : I2Cs_Mode
DESCRIPTION  : Data transmission and reception takes place in interrupt 
               subroutine for Interrupt driven without buffer mode. 
COMMENTS     : None
-----------------------------------------------------------------------------*/
I2Cs_Mode I2Cs_GetCommMode(void)
{
    #if (defined I2C_POLLING_TX || defined I2C_POLLING_RX )
    unsigned char Temp, Temp2;  
    #endif  
    /* For Interrupt Mode                                                    */
    #if (defined I2C_ITDRV_WITHOUTBUF_TX || defined I2C_ITDRV_WITHOUTBUF_RX )
    if( I2Cs_StartStatus )
    {
        I2Cs_StartStatus = FALSE;
        I2CCR |= I2Cs_ITE;
    }                     
    else if ( I2CST2 & I2Cs_ERR )
    {
        I2CCR |= I2Cs_ITE;
    }    
    return I2Cs_Com_Mode;    
    
    #endif    
    
    /* For Polling Mode                                                      */
    #if (defined I2C_POLLING_TX || defined I2C_POLLING_RX )
    Temp = I2CSR1;
    I2CST2 = I2CSR2;
    Temp2 = (unsigned char)(I2CST2 & I2Cs_ERR);
    //if ( (Temp & I2Cs_BTF) && !(Temp2 & I2CSR2))
    if ( (!(Temp2 & I2CSR2)) && (Temp & I2Cs_BTF))//newly added,previous stmt commented above
    {
        if( Temp & I2Cs_TRA) 
        {   
            /* Transmitter                                                */
            return I2Cs_TX_MODE;  
        }    
        else 
        {                                 
             /* Receicver                                                    */ 
            return I2Cs_RX_MODE;  
        }    
    }
    else  
	{//add brace      
        return I2Cs_DEFAULT;
	}
    #endif     
    
}                                                                              
                                                                               

/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_PutByte
INPUT        : None
OUTPUT       : Received data byte 
DESCRIPTION  : Reads the data from i2csDR register and return the received 
               data byte.
COMMENTS     : I2Cs_IsReceptionCompleted is called to check whether data byte is 
               received.
-----------------------------------------------------------------------------*/
void I2Cs_PutByte(unsigned char Tx_Byte)
{
    I2CDR = Tx_Byte;
    /* For Interrupt Mode                                                    */ 
    I2Cs_MaxSize = 1;
    #ifdef I2C_ITDRV_WITHOUTBUF_TX
     /* No meaning in single byte case                                       */
    I2Cs_IntrCounter = 0;  
     /* Enable i2c interrupt                                                 */  
    I2CCR |= I2Cs_ITE; 
    #endif
}



#ifdef I2C_POLLING_TX	   
/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_PutBuffer
INPUT 1      : *PtrToBuffer - Starting address of the user buffer.
INPUT 2      : NbOfBytes - Number of data bytes to be transmitted.
OUTPUT       : Transmission status
DESCRIPTION  : In polling mode, Transmits data bytes continuously from user 
               buffer and returns the transmission status 
COMMENTS     : None
-----------------------------------------------------------------------------*/ 
I2Cs_ErrCode_t I2Cs_PutBuffer(unsigned char *PtrToBuffer, unsigned char MaxSize)
{
    unsigned char Temp1, Temp2;
    unsigned char counter = 0;
    volatile unsigned char temp =0;  

     /* Check whether BTF is set or not                                      */
    while (!(I2CSR1 & I2Cs_BTF));         

    /* Write handling for  MaxSize = 0                                       */
    if ( MaxSize )
	{//add brace
        I2CDR = *(PtrToBuffer + counter);
	}
    else
	{//add brace
        I2CDR = 0xFF; /* Dummy Bytes   */
	}                                          
    counter++;               
    /* Communication routine                                                 */
    while(1)
    {                
        Temp1 = I2CSR1;                                                        
        /* Wait for an event to occured                                      */
        while (!( Temp1 & I2Cs_EVF) ) 
        {
            Temp1 = I2CSR1;
        }
        /* Check for errors                                                  */ 
        Temp2 = I2CSR2; 
        if( Temp2 & I2Cs_ERR)
        {                                                                      
            if ( Temp2 & I2Cs_BRR)
            {
                return I2Cs_BERR;
            }        
            else if (Temp2 & I2Cs_AF)
            {
                while(!(I2CSR1 & I2Cs_BTF));
                /* Release the SCL and SDA lines                             */ 
                I2CDR = 0xFF;
            }    
            else if ( Temp2 & I2Cs_STOPF ) 
            { 
                if ( counter <= MaxSize)
		    {//add brace
                    return I2Cs_TX_DATA_OK;
		    }
                else
		    {//add brace
                    return I2Cs_OVERFLOW_TX;  
		    }  
            } 
        }
        else
        {
            if ( Temp1 & I2Cs_ADSL ) 
            {   /* Start condition need to check whether Tx or Rx.           */    
                if ( Temp2 & I2Cs_GCAL )  
		    {//add brace                          
                    return I2Cs_GENERAL_CALL;
		    }
                else 
		    {//add brace   
                    return I2Cs_ADDRESS_DETECTED; 
		    }             
            }
            else if ((Temp1 & I2Cs_BTF) && ( Temp1 & I2Cs_TRA)) 
            {
                if ( counter < MaxSize)
		    {//add brace
                    I2CDR = *(PtrToBuffer + counter);
		    }
                else 
		    {//add brace
                    I2CDR = 0xFF; /* Dummy bytes due to Overflow comm        */
		    }
                counter++;                        
            }
        
        }  /* End of  'Temp2' if loop                                        */
        
    }  /* End of while loop                                                  */           

} /* End of the function                                                     */     
#endif


#ifdef I2C_ITDRV_WITHOUTBUF_TX
/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cs_PutBuffer
INPUT 1      : *PtrToBuffer - Starting address of the user buffer
INPUT 2      : NbOfBytes - Number of data bytes to be transmitted
OUTPUT       : None
DESCRIPTION  : In interrupt driven mode, starts continuous data bytes 
               transmission in ISR
COMMENTS     : None
-----------------------------------------------------------------------------*/ 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -