GD32f303 iic从机问题请求帮忙解决

[复制链接]
1491|2
 楼主| 金戈骑士 发表于 2022-5-23 16:04 | 显示全部楼层 |阅读模式
iic从机中断中stop事件中断移植没法触发到,波形用逻辑分析仪抓取了看起来是没问题的。请问是哪里出问题了?

  1. static int slaveI2c1Enable(struct slaveI2cBus *bus, unsigned int slaveAddr, int bEnable)
  2. {
  3.     if (!bEnable)
  4.     {
  5.         i2c_disable(I2C1);
  6.         rcu_periph_clock_disable(RCU_I2C1);
  7.     }
  8.     else
  9.     {
  10.         /* enable GPIOB clock */
  11.         rcu_periph_clock_enable(RCU_GPIOB);
  12.         /* enable I2C1 clock */
  13.         rcu_periph_clock_enable(RCU_I2C1);

  14.         /* connect PB10 to I2C1_SCL */
  15.         /* connect PB11 to I2C1_SDA */
  16.         gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_10 | GPIO_PIN_11);

  17.         /* I2C clock configure */
  18.         i2c_clock_config(I2C1, 100000, I2C_DTCY_2);
  19.         /* I2C address configure */
  20.         i2c_mode_addr_config(I2C1, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, slaveAddr);
  21.         /* enable I2C1 */
  22.         i2c_enable(I2C1);
  23.         /* enable acknowledge */
  24.         i2c_ack_config(I2C1, I2C_ACK_ENABLE);

  25.         /* enable the I2C1 interrupt */
  26.         nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
  27.         nvic_irq_enable(I2C1_EV_IRQn, 0, 1);
  28.         nvic_irq_enable(I2C1_ER_IRQn, 0, 2);

  29.         i2c_interrupt_enable(I2C1, I2C_INT_ERR);
  30.         i2c_interrupt_enable(I2C1, I2C_INT_BUF);
  31.         i2c_interrupt_enable(I2C1, I2C_INT_EV);
  32.     }

  33.     return 0;
  34. }

  35. void I2C1_EV_IRQHandler(void)
  36. {

  37.     if (i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_ADDSEND))
  38.     {
  39.         /* clear the ADDSEND bit */
  40.         i2c_interrupt_flag_clear(I2C1, I2C_INT_FLAG_ADDSEND);

  41.         if (i2c1.start)
  42.             i2c1.start(&i2c1);
  43.     }
  44.     else if (i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_RBNE))
  45.     {
  46.         /* if reception data register is not empty ,I2C1 will read a data from I2C_DATA */
  47.         if (i2c1.rx)
  48.             i2c1.rx(&i2c1, i2c_data_receive(I2C1));
  49.     }
  50.     else if ((i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_TBE)) && (!i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_AERR)))
  51.     {
  52.         /* send a data byte */
  53.         unsigned char txValue = i2c1.txData ? (unsigned char)i2c1.txData(&i2c1) : 0x00;
  54.         i2c_data_transmit(I2C1, txValue);
  55.     }
  56.     else if (i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_STPDET))
  57.     {
  58.         /* clear the STPDET bit */
  59.         i2c_enable(I2C1);

  60.         if (i2c1.stop)
  61.             i2c1.stop(&i2c1);
  62.     }
  63. }

  64. void I2C1_ER_IRQHandler(void)
  65. {
  66.     /* no acknowledge received */
  67.     if (i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_AERR))
  68.         i2c_interrupt_flag_clear(I2C1, I2C_INT_FLAG_AERR);

  69.     /* SMBus alert */
  70.     if (i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_SMBALT))
  71.         i2c_interrupt_flag_clear(I2C1, I2C_INT_FLAG_SMBALT);

  72.     /* bus timeout in SMBus mode */
  73.     if (i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_SMBTO))
  74.         i2c_interrupt_flag_clear(I2C1, I2C_INT_FLAG_SMBTO);

  75.     /* over-run or under-run when SCL stretch is disabled */
  76.     if (i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_OUERR))
  77.         i2c_interrupt_flag_clear(I2C1, I2C_INT_FLAG_OUERR);

  78.     /* arbitration lost */
  79.     if (i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_LOSTARB))
  80.         i2c_interrupt_flag_clear(I2C1, I2C_INT_FLAG_LOSTARB);

  81.     /* bus error */
  82.     if (i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_BERR))
  83.         i2c_interrupt_flag_clear(I2C1, I2C_INT_FLAG_BERR);

  84.     /* CRC value doesn't match */
  85.     if (i2c_interrupt_flag_get(I2C1, I2C_INT_FLAG_PECERR))
  86.         i2c_interrupt_flag_clear(I2C1, I2C_INT_FLAG_PECERR);
  87. }
 楼主| 金戈骑士 发表于 2022-5-23 16:08 | 显示全部楼层
项目是使用iic从里来进行数据的收发,以stop事件来判断一次i2c读取流程结束,可是现在其余中断事件进入正常,就只有stop事件中断没有进入过,各位大佬帮忙看看
lvben5d 发表于 2022-5-24 10:34 | 显示全部楼层
本帖最后由 lvben5d 于 2022-5-24 12:29 编辑

官网例程 I2C做slave 看看 是否可以找到相关信息,stop中断标志位 估计就没触发吧, 如果开了stop中断的前提下, 从机模式下监测到STOP结束位 STPDET  可以看看硬件波形出来了,是否你程序硬防 没有置位!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

11

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部