本文主要总结STM32F030_I2C的相关功能与源代码分享。
I2C(Inter-Integrated Circuit)总线是由PHILIPS公司开发的两线式串行总线,用于连接微控制器及其外围设备。是微电子通信控制领域广泛采用的一种总线标准。它是同步通信的一种特殊形式,具有接口线少,控制方式简单,器件封装形式小,通信速率较高等优点。I2C 总线支持任何IC 生产工艺(CMOS、双极型)。通过串行数据(SDA)线和串行时钟 (SCL)线在连接到总线的器件间传递信息。每个器件都有一个唯一的地址识别(无论是微控制器——MCU、LCD 驱动器、存储器或键盘接口),而且都可以作为一个发送器或接收器(由器件的功能决定)。除了发送器和接收器外,器件在执行数据传输时也可以被看作是主机或从机(见表1)。主机是初始化总线的数据传输并产生允许传输的时钟信号的器件。此时,任何被寻址的器件都被认为是从机。
STM32F030当然也内置了I2C模块,通过I2C进行与EEPROM进行通讯,快速存储数据。
在进行I2C模块学习前,我们应该先了解AT24Cxx的串行CMOS E2PROM,STM32F030开发板上的EEPROM主要是AT24C02。
AT24C16的存储容量为16K bit,内容分成256页,每页8Byte,共2048Byte。
AT24C16支持I2C,总线数据传送协议I2C,总线协议规定任何将数据传送到总线的器件作为发送器。任何从总线接收数据的器件为接收器。数据传送是由产生串行时钟和所有起始停止信号的主器件控制的。主器件和从器件都可以作为发送器或接收器,但由主器件控制传送数据(发送或接收)的模式,由于A0、A1和A2可以组成000~111八种情况,即通过器件地址输入端A0、A1和A2可以实现将最多8个AT24C16器件连接到总线上,通过进行不同的配置进行选择器件。
● I2C 总线规范 rev03 兼容性:
- 从机模式和主机模式
- 多主机功能
- 标准模式(高达 100kHz)
- 快速模式(高达 400kHz)
- 超快速模式(高达 1 MHz)
- 7 位和 10 位地址模式
- 多个 7 位从地址(2 个地址, 其中一个可屏蔽)
- 所有 7 位地址应答模式
- 广播呼叫
- 可编程建立和保持时间
- 易用的事件管理
- 可选的时钟延长
- 软件复位
● 1 字节缓冲带 DMA 功能
● 可编程的模拟和数字噪声滤波器
以下附加功能根据产品具体配备(见 23.3 节: I2C 具体功能配备) :
● SMBus 规范 2.0 版的兼容性:
- 硬件 PEC(包错误检查) 的生成和验证, 带 ACK 控制
- 命令和数据的应答控制
- 地址解析协议(ARP) 的支持
- 主机和设备支持
- SMBus 报警
- 超时和空闲状态检测
● 与 PMBus 版本 1.1 标准兼容
● 独立的时钟: 允许 I2C 选择一个独立的时钟源通信速度相对于 PCLK 可独立调整
● 根据地址匹配事件从 STOP 模式唤醒。
控制寄存器 1( I2Cx_CR1)
控制寄存器 2( I2Cx_CR2)
本机地址 1 寄存器( I2Cx_OAR1)
本机地址 2 寄存器( I2Cx_OAR2)
时序寄存器( I2Cx_TIMINGR)
超时寄存器( I2Cx_TIMEOUTR)
中断和状态寄存器( I2Cx_ISR)
中断清除寄存器( I2Cx_ICR)
接收数据寄存器( I2Cx_RXDR)
发送数据寄存器( I2Cx_TXDR)
代码分析
void IIC_Config(void)
{
I2C_InitTypeDef I2C_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
/*
主机模式
普通模式 100kHZ
I2C时钟频率32000KHz
使用模拟滤波器
不使用数字滤波器
上升时间100ns
下降时间10ns
*/
I2C_InitStruct.I2C_Ack=I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress=I2C_AcknowledgedAddress_7bit;
I2C_InitStruct.I2C_AnalogFilter=I2C_AnalogFilter_Enable;
I2C_InitStruct.I2C_DigitalFilter=0x00;
I2C_InitStruct.I2C_Mode=I2C_Mode_I2C;
I2C_InitStruct.I2C_OwnAddress1=0x00;
I2C_InitStruct.I2C_Timing=0x20D22E37;
I2C_Init(I2C1,&I2C_InitStruct);
}
void IIC_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
/*PB9-I2C_SDA PB8-I2C_SCK */
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType=GPIO_OType_OD;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Connect PXx to I2C_SCL*/
GPIO_PinAFConfig( GPIOB , GPIO_PinSource8, GPIO_AF_1);
/* Connect PXx to I2C_SDA*/
GPIO_PinAFConfig( GPIOB ,GPIO_PinSource9, GPIO_AF_1);
}
void IIC_Init(void)
{
IIC_GPIO_Config();
IIC_Config();
I2C_Cmd(I2C1,ENABLE);
}
1)宏定义
#define AT24Cxx_FLAG_TIMEOUT ((uint32_t)0x1000)
#define AT24Cxx_LONG_TIMEOUT ((uint32_t)(10 * AT24Cxx_FLAG_TIMEOUT))
#define AT24Cxx_MAX_TRIALS_NUMBER 300
#define AT24Cxx_OK 0
#define AT24Cxx_FAIL 1
#define AT24Cxx_I2C I2C1
#define AT24Cxx_PAGESIZE 16
#define AT24Cxx_HW_Address 0xA0
2)AT24C02初始化
void AT24CXX_Init(void)
{
IIC_Init();
AT24Cxx_Address = AT24Cxx_HW_Address;
}
3)等待操作完成
uint32_t AT24Cxx_WaitEepromStandbyState(void)
{
__IO uint32_t sEETrials = 0;
/* Configure CR2 register : set Slave Address and end mode */
I2C_TransferHandling(AT24Cxx_I2C,
AT24Cxx_Address,
0,
I2C_AutoEnd_Mode,
I2C_No_StartStop);
do
{
/* Initialize sEETimeout */
AT24Cxx_Timeout = AT24Cxx_FLAG_TIMEOUT;
/* Clear NACKF */
I2C_ClearFlag(AT24Cxx_I2C,
I2C_ICR_NACKCF | I2C_ICR_STOPCF);
/* Generate start */
I2C_GenerateSTART(AT24Cxx_I2C, ENABLE);
/* Wait until timeout elapsed */
while (AT24Cxx_Timeout-- != 0);
/* Check if the maximum allowed numbe of trials has bee reached */
if (sEETrials++ == AT24Cxx_MAX_TRIALS_NUMBER)
{
/* If the maximum number of trials has been reached, exit the function */
return AT24Cxx_TIMEOUT_UserCallback();
}
}
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_NACKF) != RESET);
/* Clear STOPF */
I2C_ClearFlag(AT24Cxx_I2C, I2C_ICR_STOPCF);
/* Return sEE_OK if device is ready */
return AT24Cxx_OK;
}
4)AT24C02页写数据
uint32_t AT24Cxx_WritePage(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t* NumByteToWrite)
{
uint32_t DataNum = 0;
I2C_TransferHandling(AT24Cxx_I2C, AT24Cxx_Address, 2, I2C_Reload_Mode, I2C_Generate_Start_Write);
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_TXIS) == RESET)
{
if((AT24Cxx_Timeout--) == 0)
{
return AT24Cxx_TIMEOUT_UserCallback();
}
}
/* Send MSB of memory address */
I2C_SendData(AT24Cxx_I2C, (uint8_t)((WriteAddr & 0xFF00) >> 8));
/* Wait until TXIS flag is set */
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_TXIS) == RESET)
{
if((AT24Cxx_Timeout--) == 0)
{
return AT24Cxx_TIMEOUT_UserCallback();
}
}
/* Send LSB of memory address */
I2C_SendData(AT24Cxx_I2C, (uint8_t)(WriteAddr & 0x00FF));
/* Wait until TCR flag is set */
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_TCR) == RESET)
{
if((AT24Cxx_Timeout--) == 0)
{
return AT24Cxx_TIMEOUT_UserCallback();
}
}
/* Update CR2 : set Slave Address , set write request, generate Start and set end mode */
I2C_TransferHandling(AT24Cxx_I2C, AT24Cxx_Address, (uint8_t)(*NumByteToWrite), I2C_AutoEnd_Mode, I2C_No_StartStop);
while (DataNum != (*NumByteToWrite))
{
/* Wait until TXIS flag is set */
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_TXIS) == RESET)
{
if((AT24Cxx_Timeout--) == 0)
{
return AT24Cxx_TIMEOUT_UserCallback();
}
}
/* Write data to TXDR */
I2C_SendData(AT24Cxx_I2C, (uint8_t)(pBuffer[DataNum]));
/* Update number of transmitted data */
DataNum++;
}
/* Wait until STOPF flag is set */
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_STOPF) == RESET)
{
if((AT24Cxx_Timeout--) == 0)
{
return AT24Cxx_TIMEOUT_UserCallback();
}
}
/* Clear STOPF flag */
I2C_ClearFlag(AT24Cxx_I2C, I2C_ICR_STOPCF);
return AT24Cxx_OK;
}
4)AT24C02写任意长度数据
void AT24Cxx_WriteBuffer(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
{
uint16_t NumOfPage = 0, NumOfSingle = 0, count = 0;
uint16_t Addr = 0;
Addr = WriteAddr % AT24Cxx_PAGESIZE;
count = AT24Cxx_PAGESIZE - Addr;
NumOfPage = NumByteToWrite / AT24Cxx_PAGESIZE;
NumOfSingle = NumByteToWrite % AT24Cxx_PAGESIZE;
/*!< If WriteAddr is sEE_PAGESIZE aligned */
if(Addr == 0)
{
/*!< If NumByteToWrite < sEE_PAGESIZE */
if(NumOfPage == 0)
{
/* Store the number of data to be written */
AT24Cxx_DataNum = NumOfSingle;
/* Start writing data */
AT24Cxx_WritePage(pBuffer, WriteAddr, (uint8_t*)(&AT24Cxx_DataNum));
AT24Cxx_WaitEepromStandbyState();
}
/*!< If NumByteToWrite > sEE_PAGESIZE */
else
{
while(NumOfPage--)
{
/* Store the number of data to be written */
AT24Cxx_DataNum = AT24Cxx_PAGESIZE;
AT24Cxx_WritePage(pBuffer, WriteAddr, (uint8_t*)(&AT24Cxx_DataNum));
AT24Cxx_WaitEepromStandbyState();
WriteAddr += AT24Cxx_PAGESIZE;
pBuffer += AT24Cxx_PAGESIZE;
}
if(NumOfSingle!=0)
{
/* Store the number of data to be written */
AT24Cxx_DataNum = NumOfSingle;
AT24Cxx_WritePage(pBuffer, WriteAddr, (uint8_t*)(&AT24Cxx_DataNum));
AT24Cxx_WaitEepromStandbyState();
}
}
}
/*!< If WriteAddr is not sEE_PAGESIZE aligned */
else
{
/*!< If NumByteToWrite < sEE_PAGESIZE */
if(NumOfPage== 0)
{
/*!< If the number of data to be written is more than the remaining space
in the current page: */
if (NumByteToWrite > count)
{
/* Store the number of data to be written */
AT24Cxx_DataNum = count;
/*!< Write the data conained in same page */
AT24Cxx_WritePage(pBuffer, WriteAddr, (uint8_t*)(&AT24Cxx_DataNum));
AT24Cxx_WaitEepromStandbyState();
/* Store the number of data to be written */
AT24Cxx_DataNum = (NumByteToWrite - count);
/*!< Write the remaining data in the following page */
AT24Cxx_WritePage((uint8_t*)(pBuffer + count), (WriteAddr + count), (uint8_t*)(&AT24Cxx_DataNum));
AT24Cxx_WaitEepromStandbyState();
}
else
{
/* Store the number of data to be written */
AT24Cxx_DataNum = NumOfSingle;
AT24Cxx_WritePage(pBuffer, WriteAddr, (uint8_t*)(&AT24Cxx_DataNum));
AT24Cxx_WaitEepromStandbyState();
}
}
/*!< If NumByteToWrite > sEE_PAGESIZE */
else
{
NumByteToWrite -= count;
NumOfPage = NumByteToWrite / AT24Cxx_PAGESIZE;
NumOfSingle = NumByteToWrite % AT24Cxx_PAGESIZE;
if(count != 0)
{
/* Store the number of data to be written */
AT24Cxx_DataNum = count;
AT24Cxx_WritePage(pBuffer, WriteAddr, (uint8_t*)(&AT24Cxx_DataNum));
AT24Cxx_WaitEepromStandbyState();
WriteAddr += count;
pBuffer += count;
}
while(NumOfPage--)
{
/* Store the number of data to be written */
AT24Cxx_DataNum = AT24Cxx_PAGESIZE;
AT24Cxx_WritePage(pBuffer, WriteAddr, (uint8_t*)(&AT24Cxx_DataNum));
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
AT24Cxx_WaitEepromStandbyState();
WriteAddr += AT24Cxx_PAGESIZE;
pBuffer += AT24Cxx_PAGESIZE;
}
if(NumOfSingle != 0)
{
/* Store the number of data to be written */
AT24Cxx_DataNum = NumOfSingle;
AT24Cxx_WritePage(pBuffer, WriteAddr, (uint8_t*)(&AT24Cxx_DataNum));
AT24Cxx_WaitEepromStandbyState();
}
}
}
}
5)AT24C02读任意数据
uint32_t AT24Cxx_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t* NumByteToRead)
{
uint32_t NumbOfSingle = 0, Count = 0, DataNum = 0, StartCom = 0;
/* Get number of reload cycles */
Count = (*NumByteToRead) / 255;
NumbOfSingle = (*NumByteToRead) % 255;
/* Configure slave address, nbytes, reload and generate start */
I2C_TransferHandling(AT24Cxx_I2C, AT24Cxx_Address, 2, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
/* Wait until TXIS flag is set */
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_TXIS) == RESET)
{
if((AT24Cxx_Timeout--) == 0)
{
return AT24Cxx_TIMEOUT_UserCallback();
}
}
/* Send MSB of memory address */
I2C_SendData(AT24Cxx_I2C, (uint8_t)((ReadAddr & 0xFF00) >> 8));
/* Wait until TXIS flag is set */
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_TXIS) == RESET)
{
if((AT24Cxx_Timeout--) == 0) return AT24Cxx_TIMEOUT_UserCallback();
}
/* Send LSB of memory address */
I2C_SendData(AT24Cxx_I2C, (uint8_t)(ReadAddr & 0x00FF));
/* Wait until TC flag is set */
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_TC) == RESET)
{
if((AT24Cxx_Timeout--) == 0) return AT24Cxx_TIMEOUT_UserCallback();
}
/* If number of Reload cycles is not equal to 0 */
if (Count != 0)
{
/* Starting communication */
StartCom = 1;
/* Wait until all reload cycles are performed */
while( Count != 0)
{
/* If a read transfer is performed */
if (StartCom == 0)
{
/* Wait until TCR flag is set */
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_TCR) == RESET)
{
if((AT24Cxx_Timeout--) == 0) return AT24Cxx_TIMEOUT_UserCallback();
}
}
/* if remains one read cycle */
if ((Count == 1) && (NumbOfSingle == 0))
{
/* if starting communication */
if (StartCom != 0)
{
/* Configure slave address, end mode and start condition */
I2C_TransferHandling(AT24Cxx_I2C, AT24Cxx_Address, 255, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
}
else
{
/* Configure slave address, end mode */
I2C_TransferHandling(AT24Cxx_I2C, AT24Cxx_Address, 255, I2C_AutoEnd_Mode, I2C_No_StartStop);
}
}
else
{
/* if starting communication */
if (StartCom != 0)
{
/* Configure slave address, end mode and start condition */
I2C_TransferHandling(AT24Cxx_I2C, AT24Cxx_Address, 255, I2C_Reload_Mode, I2C_Generate_Start_Read);
}
else
{
/* Configure slave address, end mode */
I2C_TransferHandling(AT24Cxx_I2C, AT24Cxx_Address, 255, I2C_Reload_Mode, I2C_No_StartStop);
}
}
/* Update local variable */
StartCom = 0;
DataNum = 0;
/* Wait until all data are received */
while (DataNum != 255)
{
/* Wait until RXNE flag is set */
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_RXNE) == RESET)
{
if((AT24Cxx_Timeout--) == 0) return AT24Cxx_TIMEOUT_UserCallback();
}
/* Read data from RXDR */
pBuffer[DataNum]= I2C_ReceiveData(AT24Cxx_I2C);
/* Update number of received data */
DataNum++;
(*NumByteToRead)--;
}
/* Update Pointer of received buffer */
pBuffer += DataNum;
/* update number of reload cycle */
Count--;
}
/* If number of single data is not equal to 0 */
if (NumbOfSingle != 0)
{
/* Wait until TCR flag is set */
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_TCR) == RESET)
{
if((AT24Cxx_Timeout--) == 0) return AT24Cxx_TIMEOUT_UserCallback();
}
/* Update CR2 : set Nbytes and end mode */
I2C_TransferHandling(AT24Cxx_I2C, AT24Cxx_Address, (uint8_t)(NumbOfSingle), I2C_AutoEnd_Mode, I2C_No_StartStop);
/* Reset local variable */
DataNum = 0;
/* Wait until all data are received */
while (DataNum != NumbOfSingle)
{
/* Wait until RXNE flag is set */
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_RXNE) == RESET)
{
if((AT24Cxx_Timeout--) == 0) return AT24Cxx_TIMEOUT_UserCallback();
}
/* Read data from RXDR */
pBuffer[DataNum]= I2C_ReceiveData(AT24Cxx_I2C);
/* Update number of received data */
DataNum++;
(*NumByteToRead)--;
}
}
}
else
{
/* Update CR2 : set Slave Address , set read request, generate Start and set end mode */
I2C_TransferHandling(AT24Cxx_I2C, AT24Cxx_Address, (uint32_t)(NumbOfSingle), I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
/* Reset local variable */
DataNum = 0;
/* Wait until all data are received */
while (DataNum != NumbOfSingle)
{
/* Wait until RXNE flag is set */
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_RXNE) == RESET)
{
if((AT24Cxx_Timeout--) == 0) return AT24Cxx_TIMEOUT_UserCallback();
}
/* Read data from RXDR */
pBuffer[DataNum]= I2C_ReceiveData(AT24Cxx_I2C);
/* Update number of received data */
DataNum++;
(*NumByteToRead)--;
}
}
/* Wait until STOPF flag is set */
AT24Cxx_Timeout = AT24Cxx_LONG_TIMEOUT;
while(I2C_GetFlagStatus(AT24Cxx_I2C, I2C_ISR_STOPF) == RESET)
{
if((AT24Cxx_Timeout--) == 0) return AT24Cxx_TIMEOUT_UserCallback();
}
/* Clear STOPF flag */
I2C_ClearFlag(AT24Cxx_I2C, I2C_ICR_STOPCF);
/* If all operations OK, return sEE_OK (0) */
return AT24Cxx_OK;
}
6)超时函数
uint32_t AT24Cxx_TIMEOUT_UserCallback(void)
{
/* Block communication and all processes */
while (1)
{
}
}