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

📄 91x_i2c.c

📁 STR912 arm9实现的以太网通信程序
💻 C
📖 第 1 页 / 共 2 页
字号:
* Output         : None
* Return         : None.
*******************************************************************************/
void I2C_ITConfig(I2C_TypeDef *I2Cx, FunctionalState NewState)
{
  if (NewState == ENABLE)
  {
    /* Enable the I2C interrupt */
    I2Cx->CR |= I2C_IT_Enable;
  }
  else
  {
    /* Disable the I2C interrupt */
    I2Cx->CR &= I2C_IT_Disable;
  }
}

/*******************************************************************************
* Function Name  : I2C_ReadRegister
* Description    : Reads any I2C register and returns its value.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1     
*                 - I2C_Register: the I2C register to be read. This parameter
*                    can be one of the following values:
*                         - I2C_CR:   CR register.
*                         - I2C_SR1:  SR1 register.
*                         - I2C_SR2:  SR2 register.
*                         - I2C_CCR:  CCR register.
*                         - I2C_OAR1: OAR1 register.
*                         - I2C_OAR2: OAR2 register.
*                         - I2C_DR:   DR register.
*                         - I2C_ECCR: ECCR register.
* Output         : None
* Return         : The value of the register passed as parameter
*******************************************************************************/
u8 I2C_ReadRegister(I2C_TypeDef* I2Cx, u8 I2C_Register)
{
  /* Return the selected register value */
  if (I2Cx == I2C0)
  {
  return (*(u8 *)(I2C0_BASE + I2C_Register));
  }
  if (I2Cx == I2C1)
  {
  return (*(u8 *)(I2C1_BASE + I2C_Register));
  }
 return 0;
}

/*******************************************************************************
* Function Name  : I2C_GetFlagStatus
* Description    : Checks whether the specified I2C flag is set or not.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*                 - I2C_FLAG: flag to check. This parameter can be one of the
*                    following values:
*                         - I2C_FLAG_SB: Start bit flag
*                         - I2C_FLAG_M_SL: Master/Slave flag
*                         - I2C_FLAG_ADSL: Adress matched flag
*                         - I2C_FLAG_BTF: Byte transfer finished flag
*                         - I2C_FLAG_BUSY: Bus busy flag
*                         - I2C_FLAG_TRA: Transmitter/Receiver flag
*                         - I2C_FLAG_ADD10: 10-bit addressing in Master mode flag
*                         - I2C_FLAG_EVF: Event flag
*                         - I2C_FLAG_GCAL: General call flag
*                         - I2C_FLAG_BERR: Bus error flag
*                         - I2C_FLAG_ARLO: Arbitration lost flag
*                         - I2C_FLAG_STOPF: Stop detection flag
*                         - I2C_FLAG_AF: Acknowledge failure flag
*                         - I2C_FLAG_ENDAD: End of address transmission flag
*                         - I2C_FLAG_ACK: Acknowledge enable flag
* Output         : None	
* Return         : The NewState of the I2C_Flag (SET or RESET).
*******************************************************************************/
FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, u16 I2C_FLAG)
{
  u16 wFlag1=0, wFlag2=0, wTmp=0;

  wFlag1 = I2Cx->SR2;
  wFlag1 = wFlag1<<8;
  wFlag2 = I2Cx->CR & 0x04;

  /* Get all the I2C flags in a unique register*/
  wTmp = (((I2Cx->SR1 | (wFlag1)) & I2C_Event_Mask) | (wFlag2<<12));

  /* Check the status of the specified I2C flag */
  if((wTmp & I2C_FLAG) != RESET)
  {
    /* Return SET if I2C_FLAG is set */
    return SET;
  }
  else
  {
    /* Return RESET if I2C_FLAG is reset */
    return RESET;
  }
}

/*******************************************************************************
* Function Name  : I2C_ClearFlag
* Description    : Clears the I2C Flag passed as a parameter 
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1
*                 - I2C_FLAG: flag to check. This parameter can be one of the
*                    following values:
*                         - I2C_FLAG_SB: Start bit flag
*                         - I2C_FLAG_M_SL: Master/Slave flag
*                         - I2C_FLAG_ADSL: Adress matched flag
*                         - I2C_FLAG_BTF: Byte transfer finished flag
*                         - I2C_FLAG_BUSY: Bus busy flag
*                         - I2C_FLAG_TRA: Transmitter/Receiver flag
*                         - I2C_FLAG_ADD10: 10-bit addressing in Master mode flag
*                         - I2C_FLAG_EVF: Event flag
*                         - I2C_FLAG_GCAL: General call flag
*                         - I2C_FLAG_BERR: Bus error flag
*                         - I2C_FLAG_ARLO: Arbitration lost flag
*                         - I2C_FLAG_STOPF: Stop detection flag
*                         - I2C_FLAG_AF: Acknowledge failure flag
*                         - I2C_FLAG_ENDAD: End of address transmission flag
*                         - I2C_FLAG_ACK: Acknowledge enable flag
*                  - parameter needed in the case that the flag to be cleared
*                    need a write in one register
* Output         : None	
* Return         : None.
*******************************************************************************/
void I2C_ClearFlag(I2C_TypeDef* I2Cx, u16 I2C_FLAG, ...)
{
  u8 bTmp = (u8)*((u32 *) & I2C_FLAG + sizeof(I2C_FLAG));

  /* flags that need a read of the SR2 register to be cleared */
  if ((I2C_FLAG==I2C_FLAG_ADD10) || (I2C_FLAG==I2C_FLAG_EVF) || (I2C_FLAG==I2C_FLAG_BERR) || (I2C_FLAG==I2C_FLAG_ARLO) |
      (I2C_FLAG==I2C_FLAG_STOPF) ||(I2C_FLAG==I2C_FLAG_AF)  || (I2C_FLAG==I2C_FLAG_ENDAD))
  {
    /* Read the SR2 register */
    I2Cx->SR2;

    /* Two flags need a second step to be cleared */
    switch (I2C_FLAG)
    {
       case  I2C_FLAG_ADD10:
         /* Send the MSB 10bit address passed as second parameter */
         I2Cx->DR = bTmp;
         break;
       case  I2C_FLAG_ENDAD:
         /* Write to the I2C_CR register by setting PE bit */
         I2Cx->CR |= I2C_PE_Set;
         break;
    }
  }

  /* flags that need a read of the SR1 register to be cleared */
  else if (I2C_FLAG==I2C_FLAG_SB || I2C_FLAG==I2C_FLAG_ADSL || I2C_FLAG==I2C_FLAG_BTF || I2C_FLAG==I2C_FLAG_TRA)
  {
    /* Read the SR1 register */
    (void)I2Cx->SR1;

    /* three flags need a second step to be cleared */
    if (I2C_FLAG == I2C_FLAG_SB)
    {
      /* Send the address byte passed as second parameter */
      I2Cx->DR = bTmp;
    }
    else if (I2C_FLAG==I2C_FLAG_BTF || I2C_FLAG==I2C_FLAG_TRA)
    {
      /* return the received byte in the variable passed as second parameter  */
      bTmp=I2Cx->DR;
    }
  }

  /* flags that need to disable the I2C interface */
  else if ( I2C_FLAG==I2C_FLAG_M_SL || I2C_FLAG==I2C_FLAG_GCAL)
  {
    I2C_Cmd(I2Cx, DISABLE);
    I2C_Cmd(I2Cx, ENABLE);
  }
}

/*******************************************************************************
* Function Name  : I2C_Send7bitAddress                                             
* Description    : Transmits the address byte to select the slave device.      
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1
*                 - Address: specifies the slave address which will be transmitted    
*                 - Direction: specifies whether the I2C device will be a 
*                    Transmitter or a Receiver. This parameter can be one of the 
*                    following values
*                         - I2C_MODE_TRANSMITTER: Transmitter mode
*                         - I2C_MODE_RECEIVER: Receiver mode  
* Output         : None	
* Return         : None.                                                       
*******************************************************************************/
void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, u8 Address, u8 Direction)
{
  /* Test on the direction to define the read/write bit */
  if (Direction == I2C_MODE_RECEIVER)
  {
    /* Set the address bit0 for read */
    Address |= I2C_ADD0_Set;
  }
  else
  {
    /* Reset the address bit0 for write */
    Address &= I2C_ADD0_Reset;
  }
  /* Send the address */
  I2Cx->DR = Address;
}

/*******************************************************************************
* Function Name  : I2C_SendData
* Description    : Send a data byte.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
*                   - bData : the byte to be sent
* Output         : None	
* Return         : None.
*******************************************************************************/
void I2C_SendData(I2C_TypeDef* I2Cx, u8 bData)
{
  /* Write in the DR register the byte to be sent */
  I2Cx->DR = bData;
}

/*******************************************************************************
* Function Name  : I2C_ReceiveData
* Description    : Read the received byte.
* Input          : - I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1
* Output         : None	
* Return         : The received byte
*******************************************************************************/
u8 I2C_ReceiveData(I2C_TypeDef* I2Cx)
{
  /* Return from the DR register the received byte */
  return I2Cx->DR;
}

/*******************************************************************************
* Function Name  : I2C_GetLastEvent
* Description    : Get the Last happened I2C Event.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1  
* Output         : None	
* Return         : The Last happened Event.
*******************************************************************************/
u16 I2C_GetLastEvent(I2C_TypeDef* I2Cx)
{
  u16 wFlag1=0, wFlag2 =0, wLastEvent=0;
  wFlag2 = I2Cx->SR1;
  wFlag1 = I2Cx->SR2;
  wFlag1 = wFlag1<<8;
  /* Get the last event value from I2C status register */
  wLastEvent = ((( wFlag2 | (wFlag1)) & I2C_Event_Mask));
  /* Return the last event */
  return wLastEvent;
}

/*******************************************************************************
* Function Name  : I2C_CheckEvent
* Description    : Checks whether the Last I2C Event is equal to the one passed
*                  as parameter.
* Input          :- I2Cx: I2C peripheral can be:
*                    - I2C0
*					 - I2C1   
*                  - I2C_EVENT: the event to check. This parameter can be one of
*                    the following values:
*                         - I2C_EVENT_SLAVE_ADDRESS_MATCHED
*                         - I2C_EVENT_SLAVE_BYTE_RECEIVED
*                         - I2C_EVENT_SLAVE_BYTE_TRANSMITTED
*                         - I2C_EVENT_MASTER_MODE_SELECT
*                         - I2C_EVENT_MASTER_MODE_SELECTED
*                         - I2C_EVENT_MASTER_BYTE_RECEIVED
*                         - I2C_EVENT_MASTER_BYTE_TRANSMITTED
*                         - I2C_EVENT_MASTER_MODE_ADDRESS10
*                         - I2C_EVENT_SLAVE_STOP_DETECTED
*                         - I2C_EVENT_SLAVE_ACK_FAILURE
                          - I2C_EV31 
* Output         : None	
* Return         : An ErrorStatus enumuration value:
*                         - SUCCESS: Last event is equal to the I2C_Event
*                         - ERROR: Last event is different from the I2C_Event
*******************************************************************************/
ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx,u16 I2C_EVENT)
{
  u16  wLastEvent = I2C_GetLastEvent(I2Cx);

  /* Check whther the last event is equal to I2C_EVENT */
  if (wLastEvent == I2C_EVENT)
  {
    /* Return SUCCESS when last event is equal to I2C_EVENT */
    return SUCCESS;
  }
  else
  {
    /* Return ERROR when last event is different from I2C_EVENT */
    return ERROR;
  }
}

/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

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