iic从机模式,时钟延展导致_I2C_通信不可靠导致SCL一直为低电平,造成I2C逻辑异常。
该问题由于CR1的NOSTRETCH 寄存器默认为0导致。
寄存操作如下:
I2C1->CR1 |= I2C_CR1_NOSTRETCH;
程序实现:
void i2c_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_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_Init(&NVIC_InitStructure);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = I2CSLAVE_ADDR<<1;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = I2C_CLOCK_FRQ;
I2C_Cmd(I2C1, ENABLE); // I2C Peripheral Enable
I2C_Init(I2C1, &I2C_InitStructure);
I2C1->CR1 |= I2C_CR1_NOSTRETCH;
I2C_ITConfig(I2C1, I2C_IT_EVT, ENABLE); //Part of the STM32 I2C driver
I2C_ITConfig(I2C1, I2C_IT_BUF, ENABLE);
I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE); //Part of the STM32 I2C driver
}
void I2C1_ClearFlag(void) {
// ADDR-Flag clear
while((I2C1->SR1 & I2C_SR1_ADDR) == I2C_SR1_ADDR) {
I2C1->SR1;
I2C1->SR2;
}
// STOPF Flag clear
while((I2C1->SR1&I2C_SR1_STOPF) == I2C_SR1_STOPF) {
I2C1->SR1;
I2C1->CR1 |= 0x1;
}
}
void I2C1_EV_IRQHandler(void)
{
uint32_t event;
uint8_t i;
// Reading last event
I2cEvent=I2C_GetLastEvent(I2C1);
/*EV1*/
if((I2cEvent &I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED) == I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED)
{
}
else if((I2cEvent &I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED) == I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED)
{
}
/*EV2*/
if( (I2cEvent &I2C_EVENT_SLAVE_BYTE_RECEIVED) == I2C_EVENT_SLAVE_BYTE_RECEIVED)
{
}
/*EV3*/
if( (I2cEvent &I2C_EVENT_SLAVE_BYTE_TRANSMITTED) == I2C_EVENT_SLAVE_BYTE_TRANSMITTED)
{
}
else if((I2cEvent &I2C_EVENT_SLAVE_BYTE_TRANSMITTING) == I2C_EVENT_SLAVE_BYTE_TRANSMITTING)
{
}
/*EV4*/
if((I2cEvent &I2C_EVENT_SLAVE_STOP_DETECTED) == I2C_EVENT_SLAVE_STOP_DETECTED)
{
}
}
void I2C1_ER_IRQHandler(void)
{
if (I2C_GetITStatus(I2C1, I2C_IT_BERR))
{
I2C_ClearITPendingBit(I2C1, I2C_IT_BERR);
}
if (I2C_GetITStatus(I2C1, I2C_IT_AF))
{
I2C_ClearITPendingBit(I2C1, I2C_IT_AF);
}
if (I2C_GetITStatus(I2C1, I2C_IT_ARLO))
{
I2C_ClearITPendingBit(I2C1, I2C_IT_ARLO);
}
if (I2C_GetITStatus(I2C1, I2C_IT_OVR))
{
I2C_ClearITPendingBit(I2C1, I2C_IT_OVR);
}
if (I2C_GetITStatus(I2C1, I2C_IT_PECERR))
{
I2C_ClearITPendingBit(I2C1, I2C_IT_PECERR);
}
if (I2C_GetITStatus(I2C1, I2C_IT_TIMEOUT))
{
I2C_ClearITPendingBit(I2C1, I2C_IT_TIMEOUT);
}
if (I2C_GetITStatus(I2C1, I2C_IT_TIMEOUT))
{
I2C_ClearITPendingBit(I2C1, I2C_IT_TIMEOUT);
}
}