hal_i2c.c
来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 160 行
C
160 行
//-----------------------------------------------------------------------------
// HAL_I2C
//-----------------------------------------------------------------------------
#include "hal_i2c.h"
//-----------------------------------------------------------------------------
u8 i2c_buffer[12];
//-----------------------------------------------------------------------------
void I2C_Init (void)
{
I2C->CR = 0x0;
I2C->CCR = 0x0;
I2C->OAR = 0x0;
(void)I2C->SR1;
(void)I2C->SR2;
}
//-----------------------------------------------------------------------------
void I2C_OnOffConfig (functionalstate Condition)
{
if (Condition == ENABLE)
{
// Enable the I2C selected by setting twice the PE bit on the CR register
CONFR->GLOBAL_CONTROL &= ~CONFR_nI2C_ENABLE; //Clear this bit to enable I2C pins
I2C->CR |= CR_PE;
I2C->CR |= CR_PE;
} else{
// Disable the I2C selected
CONFR->GLOBAL_CONTROL |= CONFR_nI2C_ENABLE; //Set this bit to disable I2C pins, switch into GPIO
I2C->CR &= ~CR_PE;
}
}
//-----------------------------------------------------------------------------
void I2C_SpeedConfig (u32 fscl)
{
if (fscl <= 200000) //<=200kHz
{
//Divide by 3 -> Set FM/SM bit
I2C->CCR = (FREG_OSC/fscl/3) - 2;
I2C->CCR |= 0x80;
} else {
//Divide by 2 -> Clear FM/SM bit
I2C->CCR = (FREG_OSC/fscl/2) - 2;
I2C->CCR &= 0x7f;
}
}
//-----------------------------------------------------------------------------
void I2C_AddressConfig (u8 Address)
{
I2C->OAR = Address;
}
//-----------------------------------------------------------------------------
void I2C_Send_Frame (u8 *pBuffer, u8 NoOfBytes)
{
u8 i;
//StartBit
while (I2C->SR1 & SR1_BUSY);
if (!I2C_StartBit())
return;
//SendBytes
for (i=0;i<NoOfBytes;i++)
{
if (!I2C_SendByte(*(pBuffer+i)))
return;
}
//StopBit
I2C->CR |= CR_STOP;
}
//-----------------------------------------------------------------------------
void I2C_Receive_Frame (u8 *pBuffer, u8 NoOfBytes)
{
u8 i;
//StartBit
while (I2C->SR1 & SR1_BUSY);
if (!I2C_StartBit())
return;
//Send bytes: SLAVE_ADDR + WRITE_FLAG, REG_ADDR
if (!I2C_SendByte(*(pBuffer+0)))
return;
if (!I2C_SendByte(*(pBuffer+1)))
return;
//Repeat StartBit
if (!I2C_StartBit())
return;
//Send byte: SLAVE_ADDR + READ_FLAG
I2C->DR = *(pBuffer+2);
while (!(I2C->SR1 & SR1_EVF));
I2C->CR |= CR_ACK;
if (!I2C_TestErr())
return;
while (!(I2C->SR1 & SR1_BTF));
//ReceiveBytes, when finish generate StopBit
for (i=3;i<=NoOfBytes;i++)
{
while (!(I2C->SR1 & SR1_EVF));
if (i < (NoOfBytes-1))
I2C->CR |= CR_ACK;
if (i == (NoOfBytes-1))
I2C->CR &= ~CR_ACK;
if (i > (NoOfBytes-1))
I2C->CR |= CR_STOP;
if (!I2C_TestErr())
return;
while (!(I2C->SR1 & SR1_BTF));
if (i <= (NoOfBytes-1))
*(pBuffer+i) = I2C->DR;
else
(void)I2C->DR;
}
}
//-----------------------------------------------------------------------------
void i2c_write_reg (u8 slave_addr, u8 reg_addr, u8 reg_value)
{
i2c_buffer[0] = slave_addr+kSLAVE_ADDR_WRITE;
i2c_buffer[1] = reg_addr;
i2c_buffer[2] = reg_value;
I2C_Send_Frame (&i2c_buffer[0],3);
}
//-----------------------------------------------------------------------------
void i2c_read_reg (u8 slave_addr, u8 reg_addr, u8 NoOfBytes)
{
i2c_buffer[0] = slave_addr+kSLAVE_ADDR_WRITE;
i2c_buffer[1] = reg_addr;
i2c_buffer[2] = slave_addr+kSLAVE_ADDR_READ;
I2C_Receive_Frame (&i2c_buffer[0],3+NoOfBytes);
}
//-----------------------------------------------------------------------------
u8 i2c_read_buffer (u8 position)
{
return (i2c_buffer[3+position]);
}
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?