5

我尝试在 stm32f4 上实现 i2c 从接收器中断服务程序。这是我的智能代码。

void I2C2_EV_IRQHandler()
  {
    switch (I2C_GetLastEvent(I2C2))
    {
    //The address sent by the master matches the own address of the peripheral
    case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
        //The slave stretches SCL low until ADDR is
        //cleared and DR filled with the data to be sent
        I2C_ClearFlag(I2C2,I2C_FLAG_ADDR);
        break;

    //The application is expecting a data byte to be received
    case I2C_EVENT_SLAVE_BYTE_RECEIVED:
        I2C_ReceiveData(I2C2);
        break;

    //The application is expecting the end of the communication
    //Make sure that both ADDR and STOPF flags are cleared
    //if both are found set.
    case I2C_EVENT_SLAVE_STOP_DETECTED:
        if(I2C_GetFlagStatus(I2C2,I2C_FLAG_ADDR) == SET)
            I2C_ClearFlag(I2C2,I2C_FLAG_ADDR);
        if(I2C_GetFlagStatus(I2C2,I2C_FLAG_STOPF) == SET)
            I2C_ClearFlag(I2C2,I2C_FLAG_STOPF);
}

}

中断被调用并进入 I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED 情况。SCL 现在很低。参考手册说,如果我清除地址标志,时钟将继续并发送数据(第 579 页 - 从属接收器)。在我看来,如果有任何数据到达并且下一个状态将是 I2C_EVENT_SLAVE_BYTE_RECEIVED,则总是会调用中断。

我无法从 stm 或通过 google 找到任何示例。任何人都可以帮助我或给我一个例子。

4

2 回答 2

3

现在它可以工作了。我的问题是我无法使用参考手册中的给定命令重置 ADDR 和 STOPF 寄存器。但是,如果循环执行它对我来说效果很好。这是我的工作中断例程。

 void I2C3_EV_IRQHandler()
 {
     switch (I2C_GetLastEvent(I2C3))
     {

    case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
        STM_EVAL_LEDOn(LED3);
        STM_EVAL_LEDOff(LED5);
        break;

    case I2C_EVENT_SLAVE_BYTE_RECEIVED:
        STM_EVAL_LEDToggle(LED4);
        STM_EVAL_LEDOff(LED3);
        I2C_InputBuffer[I2C_InputBufferIndex++] = I2C_ReceiveData(I2C3);
        break;

    case I2C_EVENT_SLAVE_STOP_DETECTED:
        STM_EVAL_LEDOn(LED6);
        STM_EVAL_LEDOff(LED4);
        break;
    }

    I2C_CleanADDRandSTOPF();

    if(I2C_InputBufferIndex > MOTOR_PACKAGE_SIZE-1)
    {
      motorHandleEvent(I2C_InputBuffer);
      I2C_InputBufferIndex = 0;
      uint8_t resetIndex;
      for(resetIndex = 0; resetIndex < MOTOR_PACKAGE_SIZE; resetIndex ++)
        I2C_InputBuffer[resetIndex] = 0;
    }
}

inline void I2C_CleanADDRandSTOPF()
{
  while ((I2C3->SR1 & I2C_SR1_ADDR) == I2C_SR1_ADDR)
  {
    volatile uint32_t temp;
    temp=I2C3->SR1;
    temp=I2C3->SR2;
  }
  while ((I2C3->SR1&I2C_SR1_STOPF) == I2C_SR1_STOPF)
  {
    volatile uint32_t temp;
    temp=I2C3->SR1;
    I2C3->CR1 |= 0x1;
  }
}
于 2012-03-29T11:33:23.087 回答
0

硬件进行时钟延长以确保从设备跟上主设备。从机首先等待获得地址匹配。然后你会在 SCL 保持低电平时获得中断。这允许从设备基本上向主设备提供流量控制。主机检测到 SCL 被从机保持为低电平,它将等待它被释放,然后主机发送更多数据。因此,您不会在接收数据时收到额外的中断,因为在您让 SCL 变高之前,主机不会发送更多数据。您可以在此处阅读有关时钟拉伸的信息http://en.wikipedia.org/wiki/I%C2%B2C

于 2012-03-26T17:34:47.700 回答