前言:
根据网上的资料,大部分网友表示STM32自带的硬件IIC存在bug,读写时很容易卡死。自己在调试的时候也出现卡死的情况,最后一点一点调试,也还是调通了。本文将记录自己调试STM32硬件IIC从机的一些心得体会。硬件IIC主机通信见另一篇文章:传送门 。
硬件平台:STM32F205
软件平台:keil v5
函数库:标准库
硬件IIC从机初始化
下面看下STM32中IIC的相应设置。
首先是IIC的管脚配置。
/*---------IIC1---------------*/
uint8_t Buffer_Rx_IIC1[40];//接收缓存
uint8_t Rx_Idx_IIC1=0;//接收计数
uint8_t Flag_RcvOK_IIC1 = 0;// 接收完成标志
uint8_t Tx_Idx_IIC1=0;//发送计数
u8 Response_Message[40];//发送缓存
void I2C1_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// SCL PB6
// SDA PB7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;//必须设置为开漏输出,实现iic的线与逻辑
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_I2C1);
}
注意上述管脚配置中GPIO_InitStructure.GPIO_PuPd必须配置为GPIO_PuPd_NOPULL,若配置为 GPIO_PuPd_UP或GPIO_PuPd_DOWN,IIC总线会一直繁忙,导致总线出错,检测不到IIC从机。
IIC工作参数配置
void I2C1_Configuration(void)
{
I2C_InitTypeDef I2C_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
I2C_DeInit(I2C1);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = I2C1_Slave_Address; //从机地址,一定要设置正确
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress= I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Init(I2C1, &I2C_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;//事件中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn;//错误中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
I2C_ITConfig(I2C1, I2C_IT_BUF | I2C_IT_EVT |I2C_IT_ERR, ENABLE);
I2C_Cmd(I2C1, ENABLE);
}
IIC初始化函数
void I2C1_Init(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
I2C1_GPIO_Configuration();
I2C1_Configuration();
}
IIC从机中断中接收和发送数据
从机中断函数中发送的流程:EV1—>EV3-1—>EV3—>EV3-2。
从机中断函数中接收的流程:EV1—>EV2—>EV4。
详细的中断处理程序如下:
void I2C1_EV_IRQHandler(void)
{
__IO uint32_t SR1Register =0;
__IO uint32_t SR2Register =0;
SR1Register = I2C1->SR1;
SR2Register = I2C1->SR2;
/* I2C1是从机(MSL = 0) */
if((SR2Register &0x0001) != 0x0001)
{
/* 主机已发送地址,地址为被置位·(ADDR = 1: EV1(包括发送和接收)) */
if((SR1Register & 0x0002) == 0x0002)
{
/* 清除标志位 */
SR1Register = 0;
SR2Register = 0;
Rx_Idx_IIC1=0;
Tx_Idx_IIC1=0;
}
/* 接收数据(RXNE = 1: EV2) */
if((SR1Register & 0x0040) == 0x0040)
{
Buffer_Rx_IIC1[Rx_Idx_IIC1++] = I2C1->DR;
SR1Register = 0;
SR2Register = 0;
}
/* 检测到停止条件(STOPF =1: EV4) */
if(( SR1Register & 0x0010) == 0x0010)
{
I2C1->CR1 |= 0x0001;
SR1Register = 0;
SR2Register = 0;
Flag_RcvOK_IIC1 = 1;
}
/* 发送数据(TxE = 1: EV3、EV3-1) */
if((SR1Register & 0x0080) == 0x0080)
{
I2C1->DR = Response_Message[Tx_Idx_IIC1++];
SR1Register = 0;
SR2Register = 0;
}
/* 检测到非应答(AF =1: EV3-2) */
if(( SR1Register & 0x0400) == 0x0400)
{
I2C1->SR1 &= 0xFDFF;
SR1Register = 0;
SR2Register = 0;
}
}
}
上述中断程序中,当主机读取和写入数据时,都会引起地址位被置位,即发生EV1事件(即本中断处理程序中将发送和接收的EV1合并了);
当主机写入数据时,中断的执行顺序是EV1—>EV2—>EV4,其中有多个数据EV2会多次执行;
当主机读取数据时,中断的执行顺序是EV1—>EV3—>EV3-2,本中断程序中将EV3和EV3-1合并了,若有多个数据,EV3将多次执行。
错误中断处理函数如下:
void I2C1_ER_IRQHandler(void) {
__IO uint32_t SR1Register =0;
__IO uint32_t SR2Register =0;
SR1Register = I2C1->SR1;
SR2Register = I2C1->SR2;
if(I2C_GetITStatus(I2C1, I2C_IT_SMBALERT)) {
}
else if(I2C_GetITStatus(I2C1, I2C_IT_TIMEOUT)) {
}
else if(I2C_GetITStatus(I2C1, I2C_IT_PECERR)) {
}
else if(I2C_GetITStatus(I2C1, I2C_IT_OVR)) {
}
else if(I2C_GetITStatus(I2C1, I2C_IT_AF)) {
I2C_ClearITPendingBit(I2C1, I2C_IT_AF);
}
else if(I2C_GetITStatus(I2C1, I2C_IT_ARLO)) {
}
else if(I2C_GetITStatus(I2C1, I2C_IT_BERR)) {
}
I2C1->CR1 |= 0x0001;
SR1Register = 0;
SR2Register = 0;
}
发送各种错误进行错误中断不做对应的处理,最后只进行清除寄存器(SR1和SR2)操作。
上一篇:【stm32f407】硬件介绍
下一篇:STM32——硬件IIC主机通信
推荐阅读最新更新时间:2024-03-16 16:23