hal_i2c.h

来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C头文件 代码 · 共 100 行

H
100
字号
//-----------------------------------------------------------------------------
// HAL_I2C
//-----------------------------------------------------------------------------
#ifndef HAL_I2C_H
#define HAL_I2C_H

//-----------------------------------------------------------------------------
#include "hal_map.h"

//-----------------------------------------------------------------------------
//CONFR
//-----------------------------------------------------------------------------
#define CONFR_nI2C_ENABLE 0x00000001UL /*enable means clear this bit*/

//-----------------------------------------------------------------------------
//---according i2c protocol read/write---
#define kSLAVE_ADDR_WRITE        0x00
#define kSLAVE_ADDR_READ         0x01

//-----------------------------------------------------------------------------
//---CR---
#define CR_PE     0x20
#define CR_ENGC   0x10
#define CR_START  0x08
#define CR_ACK    0x04
#define CR_STOP   0x02
#define CR_ITE    0x01

//---SR1---
#define SR1_EVF   0x80
#define SR1_TRA   0x20
#define SR1_BUSY  0x10
#define SR1_BTF   0x08
#define SR1_ADSL  0x04
#define SR1_MSL   0x02
#define SR1_SB    0x01

//---SR2---
#define SR2_AF    0x10
#define SR2_STOPF 0x08
#define SR2_ARLO  0x04
#define SR2_BERR  0x02
#define SR2_GCAL  0x01

//-----------------------------------------------------------------------------
inline u8 I2C_TestErr (void)
{
  if (I2C->SR2 & (SR2_AF|SR2_ARLO|SR2_BERR))
  {
    I2C->CR |= CR_STOP;
    return (0);
  } else
    return (1);
}

//-----------------------------------------------------------------------------
inline u8 I2C_StartBit (void)
{
  I2C->CR |= CR_START;
  while (!(I2C->SR1 & SR1_EVF));

  if (!(I2C->SR1 & SR1_SB))
    return (0);

  if (!I2C_TestErr())
    return (0);

  return (1);
}

//-----------------------------------------------------------------------------
inline u8 I2C_SendByte (u8 dato)
{
  I2C->DR = dato;
  while (!(I2C->SR1 & SR1_EVF));

  I2C->CR = I2C->CR;

  if (!I2C_TestErr())
    return (0);

  while (!(I2C->SR1 & SR1_BTF));

  return (1);
}

//-----------------------------------------------------------------------------
void          I2C_Init               (void);
void          I2C_OnOffConfig        (functionalstate Condition);
void          I2C_SpeedConfig        (u32 Clock);
void          I2C_AddressConfig      (u8 Address);
void          I2C_Send_Frame         (u8 *pBuffer, u8 NoOfBytes);
void          I2C_Receive_Frame      (u8 *pBuffer, u8 NoOfBytes);
void          i2c_write_reg          (u8 slave_addr, u8 reg_addr, u8 reg_value);
void          i2c_read_reg           (u8 slave_addr, u8 reg_addr, u8 NoOfBytes);
u8            i2c_read_buffer        (u8 position);

//-----------------------------------------------------------------------------
#endif

⌨️ 快捷键说明

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