打印
[STM32F0]

ST STM32F042C6 I2C Slave 模式 调试问题

[复制链接]
1286|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
jason小鸟|  楼主 | 2017-6-21 15:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
各位领导,我是用ST STM32F042C6 调试 I2c Slave 模式 无**常进入中断 请各位大神帮我看下哪里有问题

void STM32_config_iic_slavemode()
{
   uint32_t tmpreg = 0;
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    // Disable I2C1 Peripheral
    I2C1->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_PE);
    //---------------------------- Deinitialize I2C1 clock ------------------
    // Reset I2C1 device clock in order to avoid non-cleared error flags
    RCC_APB1PeriphResetCmd((RCC_APB1Periph_I2C1),ENABLE);
    RCC_APB1PeriphResetCmd((RCC_APB1Periph_I2C1),DISABLE);
    // Disable I2C1 device clock
    RCC_APB1PeriphClockCmd((RCC_APB1Periph_I2C1),DISABLE);
    //---------------------------- GPIO pins configuration ------------------
    // Initialize I2C1 GPIO
    // Enable I2C1 SCL and SDA Pin Clock
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
    // Connect PXx to I2C_SCL
    GPIO_PinAFConfig((GPIO_TypeDef*)GPIOB,GPIO_PinSource6,GPIO_AF_1);
    // Connect PXx to I2C_SDA
    GPIO_PinAFConfig((GPIO_TypeDef*)GPIOB,GPIO_PinSource7,GPIO_AF_1);
    // Set GPIO frequency to 50MHz
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    // Select Alternate function mode
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    //Select output Open Drain type
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    // Disable internal Pull-up
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    // Initialize I2C1 SCL Pin
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_Init((GPIO_TypeDef*)GPIOB, &GPIO_InitStructure);
    // Initialize I2C1 SDA Pin
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_Init((GPIO_TypeDef*)GPIOB, &GPIO_InitStructure);
   
    // Initialize I2C1 clock
    // Reset I2C1 device clock in order to avoid non-cleared error flags
    //__I2C_RCC_RESET(CPAL_I2C_CLK [Device]);
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1,ENABLE);
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1,DISABLE);
    // Enable I2C1 device clock
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
   
    //I2C1 FILTERS Configuration
    // Get the I2C1 CR1 value
    tmpreg = I2C1->CR1;
    // Clear I2C1 CR1 register
    tmpreg &= CR1_CLEAR_MASK;
    // Configure I2C1: analog and digital filter
    // Set ANFOFF bit according to I2C_AnalogFilter value
    // Set DFN bits according to I2C_DigitalFilter value
    tmpreg |= (uint32_t)0x00000000 | (0 << 8); // Analog noise filter enabled, Digital filter disabled
       
    //tmpreg |= I2C_CR1_NOSTRETCH; // disable clcok stretching  
  
    // Write to I2C1 CR1
    I2C1->CR1 = tmpreg;
  
    //I2C1 TIMING Configuration
    // Configure I2C1: Timing
    // Set TIMINGR bits according to I2C_Timing
    // Write to I2C1 TIMING
    //I2C1->TIMINGR = 0x00000000 & TIMING_CLEAR_MASK;
    I2C1->TIMINGR = I2C_TIMING;
  
    // Enable I2C1 Peripheral
    I2C1->CR1 |= I2C_CR1_PE;
  
    //I2C1 OAR1 Configuration
    // Clear tmpreg local variable
    tmpreg = 0;
    // Clear OAR1 register
    I2C1->OAR1 = (uint32_t)tmpreg;
    // Clear OAR2 register
    I2C1->OAR2 = (uint32_t)tmpreg;
       
    // Configure I2C1: Own Address1 and acknowledged address
    // Set OA1MODE bit according to I2C_AcknowledgedAddress value
    // Set OA1 bits according to I2C_OwnAddress1 value
    tmpreg = (uint32_t)((uint32_t)0x00000000 | \
                         (uint32_t)OWN_ADDRESS1);
       
    // Write to I2C1 OAR1
    I2C1->OAR1 = tmpreg;
                       
    // Enable Own Address1 acknowledgement
    I2C1->OAR1 |= I2C_OAR1_OA1EN;
  
    //I2C1 MODE Configuration
    // Configure I2C1: mode
    // Set SMBDEN and SMBHEN bits according to I2C_Mode value
    tmpreg = 0;
    // Write to I2C1 CR1
    I2C1->CR1 |= tmpreg;
  
    //I2C1 ACK Configuration
    //Get the I2C1 CR2 value
    tmpreg = I2C1->CR2;
    //Clear I2C1 CR2 register
    tmpreg &= CR2_CLEAR_MASK;
    //Configure I2C1: acknowledgement
    //Set NACK bit according to I2C_Ack value
    tmpreg |= (uint32_t)0x00008000;
    // Write to I2C1 CR2
    I2C1->CR2 = tmpreg;
            
    //I2C1->OAR2 |= I2C_OAR2_OA2EN;
       
        //I2C1->CR1 |= I2C_CR1_GCEN;
        // Enable slave byte control
    //I2C1->CR1 |= I2C_CR1_SBC;
    I2C1->CR1 |= (I2C_CR1_ADDRIE | I2C_CR1_STOPIE | I2C_CR1_TXIE | I2C_CR1_RXIE);
    //---------------------------- Peripheral and DMA interrupts Initialization ------------------
    // Initialize I2C1 interrupts
    /* Enable the IRQ channel */
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   
    /* Configure NVIC for I2C1 Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = I2C1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
    NVIC_Init(&NVIC_InitStructure);
   
    // If I2C ERR Interrupt Option Bit not selected
    //NULL
   
    // Initialize Timeout procedure
    //NULL
}

通过这样的设置,调试的结果是,I2C无**常进入中断,我的中断设置是地址中断,接收中断,发送中断,请帮帮忙,帮我看下问题在哪里
沙发
jason小鸟|  楼主 | 2017-6-21 15:12 | 只看该作者
希望有高手帮我指点一下,分享下您的可用i2c驱动资源,这样有助于大家日后使用st的各种资源  我已经是照着reference manual 操作了,但是无**常进入中断,黔驴技穷,只得在此请教各位的指点

使用特权

评论回复
板凳
jason小鸟|  楼主 | 2017-6-21 15:13 | 只看该作者
自己帮顶起来,希望版主 等 高手看到这一条 请教

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

3

帖子

0

粉丝