Я бы хотел запрограммировать STM32F407VG как I2 C Slave. Поэтому я хочу отправить данные между STM32F407VG и PIC18F.
, но у меня есть проблема со связью между ведомым и ведущим во время отправки ACK после события «SLAVE_RECEIVER_ADDRESS_MATCHED»: линия SDA должна оставаться в низком состоянии, а не в высоком состоянии что вызывает отключение часов. Элемент в конфигурации Init или конфигурации прерываний должен провоцировать эту ошибку.
Мой код примерно такой:
void I2C1_Init (void) {
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
NVIC_InitTypeDef NVIC_InitStruct;
//peripheral clock enable register in low power
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
//peripheral clock enable register in low power
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);
//Configure pins: SCL and SDA ------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Enable the I2C event Interrupt */
NVIC_InitStruct.NVIC_IRQChannel = I2C1_EV_IRQn;
/* Set priority */
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
/* Set sub priority */
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
/* Enable interrupt */
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
/* Add to NVIC */
NVIC_Init(&NVIC_InitStruct);
/* Enable the I2C event Interrupt */
NVIC_InitStruct.NVIC_IRQChannel = I2C1_ER_IRQn;
/* Set priority */
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 3;
/* Set sub priority */
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
/* Enable interrupt */
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
/* Add to NVIC */
NVIC_Init(&NVIC_InitStruct);
// Configure I2C1
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0b0100000000001100; //address 110
I2C1->OAR2 = 0b00000000; //only OwnAddress1 is take in account
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
// Enable interrupts on event or error
I2C_ITConfig(I2C1, I2C_IT_EVT, ENABLE);
I2C_ITConfig(I2C1, I2C_IT_BUF, ENABLE);
I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE);
}
и подобное прерывание:
void I2C1_ER_IRQHandler (void)
{
if ((I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0xFF00) != 0x00)
{
I2C1-> SR1 &= 0x00FF;//!<>
}
}
/* I2C Interrupt */
void I2C1_EV_IRQHandler(void)
{
volatile uint32_t temp;
//Clear AF from slave-transmission end
//if (I2C_GetITStatus(I2C1, I2C_IT_AF) != RESET)
//{
// I2C_ClearITPendingBit(I2C1, I2C_IT_AF);
//}
switch (I2C_GetLastEvent(I2C1))
{
case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:// If ADDR = 1: EV1
/* Clear ADDR by reading SR1, then SR2 */
temp = I2C1->SR1;
temp = I2C1->SR2;
break;
case (I2C_EVENT_SLAVE_BYTE_RECEIVED):// If RXNE = 1: EV2
I2Crecpbuff[I2Cindex++] = I2C1->DR;
I2Crecpbuff[I2Cindex++]=I2C_ReceiveData(I2C1);
break;
case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:// If ADDR = 1: EV1
/* Clear ADDR by reading SR1, then SR2 */
temp = I2C1->SR1;
temp = I2C1->SR2;
I2C_SendData(I2C1, I2Crecpbuff[I2Cindex++]);
// Enable I2C event interrupt
break;
case I2C_EVENT_SLAVE_STOP_DETECTED:// If STOPF =1: EV4 (Slave has detected a STOP condition on the bus
/* Clear STOPF by reading SR1, then writing CR1 */
temp = I2C1->SR1;
I2C1->CR1 |= 0x1;
FLAG_I2C_RECEIVE=1;
I2C_ClearFlag(I2C1,I2C_FLAG_STOPF);
I2C_Cmd(I2C1, ENABLE);
I2Cindex=0;
break;
default:
I2C_ClearITPendingBit(I2C1, I2C_IT_EVT);
break;
}
I2C_CleanADDRandSTOPF();
}
void I2C_CleanADDRandSTOPF(void)
{
while ((I2C1->SR1 & I2C_SR1_ADDR) == I2C_SR1_ADDR)
{
volatile uint32_t temp;
temp=I2C1->SR1;
temp=I2C1->SR2;
}
while ((I2C1->SR1&I2C_SR1_STOPF) == I2C_SR1_STOPF)
{
volatile uint32_t temp;
temp=I2C1->SR1;
I2C1->CR1 |= 0x1;
}
}