cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407 I2C1 EV and ER Interrupt Problem

hakand
Associate

Hello Everyone,

I have dealing with STM32F407 I2C peripheral in order to implement Non-blocking communication.

I have made many trials but I2C1 event and error interrupts never occur. I checked the SB bits during the debug mode it becomes 1,the other register seems okay but interrupt is never created. Also I have made different trials, changed interrupt priorities, enabled  ev and er interrupts before enabling the periheral, after enabling periheral and after start condition is generated.

I have made other trials with cubemx and hal library for my board. After SB bit is set interrupt occured. However  I couldn’t see the start bit  from logic analyzer.

What am I missing here ?

I am using IAR 8.32, Here is my code ;

I2C.c
void I2C1_SystemInit(void ){
   I2C1->CR1    &=  ~I2C_CR1_PE;                                                     // I2C peripheral disable 
  // I2C1 Initialization  PB8 (SCL) ve PB9 (SDA)  
  RCC->AHB1ENR  |= RCC_AHB1ENR_GPIOBEN;                       // Enable clock for GPIOB
  GPIOB->AFR[1] |= (4);                                                                 // PB8 AF4 for I2C1
  GPIOB->AFR[1] |= (4 << 4);                                                       // PB9 AF4 for I2C1 
  GPIOB->MODER  &= ~(3 << 16);	                              // Clear bits 12 & 13 (PB8)
  GPIOB->MODER  |= GPIO_MODER_MODER8_1;	   // Set GPIOB8 to alternate function 
  GPIOB->MODER  &= ~(3 << 18);	                              // Clear bits 14 & 15 (PB9)
  GPIOB->MODER  |=  GPIO_MODER_MODER9_1;	   // Set GPIOB9 to alternate function 
  GPIOB->OTYPER |=  GPIO_OTYPER_OT_8;	                //  Open Drain
  GPIOB->OTYPER |=  GPIO_OTYPER_OT_9;                        //  Open Drain
  GPIOB->PUPDR  |=  GPIO_PUPDR_PUPDR8_0;	                      //  Pull-Up
  GPIOB->PUPDR  |=  GPIO_PUPDR_PUPDR9_0;	                      //  Pull-Up
  GPIOB->OSPEEDR |=  (3<<16);
  GPIOB->OSPEEDR |=  (3<<18);
  RCC->APB1ENR  |= RCC_APB1ENR_I2C1EN;                        // Enable clock for I2C1
  I2C1->CR2      |=   0x14;                                   
  I2C1->CCR      |=   208;                                 
  I2C1->TRISE    |=   0x5;                                    // 100 kHz
//  I2C1->CR1      |=   I2C_CR1_NOSTRETCH;
//  I2C1->CR1      |=   I2C_CR1_ACK;                            // Acknowledge enable 
//  I2C1->CR1      &=   ~I2C_CR1_POS;                             // Disable Pos 
 // I2C1->CR2      |=   I2C_CR2_ITEVTEN;                        //<Event Interrupt Enable 
 // I2C1->CR2     |=   I2C_CR2_ITBUFEN;                         //<Buffer Interrupt Enable 
 // I2C1->CR2     |=   I2C_CR2_ITERREN;
  I2C1->CR1      |=  I2C_CR1_PE;
}
void  I2C1_EV_IRQHandler(void){ 
  flag=1;  uint32_t sr2itflags   = (I2C1->SR2);
  uint32_t sr1itflags   = (I2C1->SR1);
  uint32_t itsources    = (I2C1->CR2);                    }
void  I2C1_ER_IRQHandler(void){  flag=2;  uint32_t sr2itflags   = (I2C1->SR2);
  uint32_t sr1itflags   = (I2C1->SR1);
  uint32_t itsources    = (I2C1->CR2);                    }
 
Main.c
void Init_Priorities(void){
  SCB->AIRCR = ((uint32_t)0x05FA0000) | NVIC_PriorityGroup_4; // NVIC_SetPriorityGrouping(4)
  NVIC_SetPriority(TIM1_UP_TIM10_IRQn,                   NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));  
  NVIC_SetPriority(ADC_IRQn,                                          NVIC_EncodePriority(NVIC_GetPriorityGrouping(),1, 0));  
  NVIC_SetPriority(TIM1_BRK_TIM9_IRQn,                   NVIC_EncodePriority(NVIC_GetPriorityGrouping(),2, 0));  
  NVIC_SetPriority(TIM1_TRG_COM_TIM11_IRQn,     NVIC_EncodePriority(NVIC_GetPriorityGrouping(),3, 0));  
  NVIC_SetPriority(TIM8_BRK_TIM12_IRQn,                NVIC_EncodePriority(NVIC_GetPriorityGrouping(),4, 0));  
  NVIC_SetPriority(TIM8_TRG_COM_TIM14_IRQn,     NVIC_EncodePriority(NVIC_GetPriorityGrouping(),5, 0));  
  NVIC_SetPriority(I2C1_EV_IRQn,                                  NVIC_EncodePriority(NVIC_GetPriorityGrouping(),6, 0)); 
  NVIC_SetPriority(I2C1_ER_IRQn,                                   NVIC_EncodePriority(NVIC_GetPriorityGrouping(),7, 0)); 
  NVIC_SetPriority(I2C2_EV_IRQn,                                 NVIC_EncodePriority(NVIC_GetPriorityGrouping(),8, 0)); 
  NVIC_SetPriority(I2C2_ER_IRQn,                               NVIC_EncodePriority(NVIC_GetPriorityGrouping(),9, 0)); 
}
 
void TIM8_TRG_COM_TIM14_IRQHandler(void)  {   RD_EEP1();  TIM14->SR   &= ~(TIM_SR_UIF);    }  
void RD_EEP1(void){  
  if((I2C1->CR1 & I2C_CR1_PE) != I2C_CR1_PE){
    I2C1->CR1   |=   I2C_CR1_PE;    }                         // I2C peripheral enable
  
  I2C1->CR1 &= ~I2C_CR1_POS;
    I2C1->CR1   |= I2C_CR1_START;   asm("nop"); asm("nop"); asm("nop");
  I2C1->CR2      |=   I2C_CR2_ITEVTEN;                        //<Event Interrupt Enable 
  I2C1->CR2     |=   I2C_CR2_ITBUFEN;                         //<Buffer Interrupt Enable 
  I2C1->CR2     |=   I2C_CR2_ITERREN;
  GREEN_LED_ON;
}
 
  int main()
  { for(uint16_t i=0;i<10000;i++);          //  wait for a while K
  Fan_Control();                                       //  Fan pins initilization
  init_inj_adc_interrupt();               //  ADC Injected Channel 
  I2C1_SystemInit();
  DAC1_Init();
  TIM9_Init();                            //  1mS    Timer Interrupt Init 
  TIM11_Init();                           //  10mS   Timer Interrupt Init 
  TIM12_Init();                           //  100mS  Timer Interrupt Init 
  TIM14_Init();                           //  1000mS Timer Interrupt Init 
  Init_Priorities();
  while(1){ 
  }   }
 
 

1 ACCEPTED SOLUTION

Accepted Solutions
hakand
Associate

Hello again, I found the problem guys,

change the code like this ;

void I2C1_SystemInit(void ){
  // I2C1 Initialization  PB8 (SCL) ve PB9 (SDA)  
  RCC->AHB1ENR  |= RCC_AHB1ENR_GPIOBEN;                       // Enable clock for GPIOB
  GPIOB->AFR[1] |= (4);                                       // PB8 AF4 for I2C1
  GPIOB->AFR[1] |= (4 << 4);                                  // PB9 AF4 for I2C1 
  GPIOB->MODER  &= ~(3 << 16);	                              // Clear bits 12 & 13 (PB8)
  GPIOB->MODER  |= GPIO_MODER_MODER8_1;	                      // Set GPIOB8 to alternate  
  GPIOB->MODER  &= ~(3 << 18);	                              // Clear bits 14 & 15 (PB9)
  GPIOB->MODER  |=  GPIO_MODER_MODER9_1;	                    // Set GPIOB9 to alternate
  GPIOB->OTYPER |=  GPIO_OTYPER_OT_8;	
  GPIOB->OTYPER |=  GPIO_OTYPER_OT_9; 
  GPIOB->PUPDR  |=  GPIO_PUPDR_PUPDR8_0;	                      //  pull-up,  
  GPIOB->PUPDR  |=  GPIO_PUPDR_PUPDR9_0;	                      //  pull-up, 
  GPIOB->OSPEEDR |=  (3<<16);
  GPIOB->OSPEEDR |=  (3<<18);
  RCC->APB1ENR  |= RCC_APB1ENR_I2C1EN;                         // Enable clock for I2C1
  while(!(RCC->APB1ENR & RCC_APB1ENR_I2C1EN) ){} 
  for(uint16_t i=0;i<1000;i++); 
     NVIC_SetPriority(I2C1_EV_IRQn,                      6);
     NVIC_EnableIRQ(I2C1_EV_IRQn);
     NVIC_SetPriority(I2C1_ER_IRQn,                      7);
     NVIC_EnableIRQ(I2C1_ER_IRQn);
    I2C1->CR1    &=  ~I2C_CR1_PE;                             // I2C peripheral disable 
  I2C1->CR2      |=   0x2A;                                         //0x14;              
  I2C1->CCR      |=   0X0D2;                                   //208;      
  I2C1->TRISE    |=   0x2B;                                    //0x5;                                    
  I2C1->CR1      |=  I2C_CR1_PE;
  for(uint16_t i=0;i<1000;i++); 
} 

View solution in original post

1 REPLY 1
hakand
Associate

Hello again, I found the problem guys,

change the code like this ;

void I2C1_SystemInit(void ){
  // I2C1 Initialization  PB8 (SCL) ve PB9 (SDA)  
  RCC->AHB1ENR  |= RCC_AHB1ENR_GPIOBEN;                       // Enable clock for GPIOB
  GPIOB->AFR[1] |= (4);                                       // PB8 AF4 for I2C1
  GPIOB->AFR[1] |= (4 << 4);                                  // PB9 AF4 for I2C1 
  GPIOB->MODER  &= ~(3 << 16);	                              // Clear bits 12 & 13 (PB8)
  GPIOB->MODER  |= GPIO_MODER_MODER8_1;	                      // Set GPIOB8 to alternate  
  GPIOB->MODER  &= ~(3 << 18);	                              // Clear bits 14 & 15 (PB9)
  GPIOB->MODER  |=  GPIO_MODER_MODER9_1;	                    // Set GPIOB9 to alternate
  GPIOB->OTYPER |=  GPIO_OTYPER_OT_8;	
  GPIOB->OTYPER |=  GPIO_OTYPER_OT_9; 
  GPIOB->PUPDR  |=  GPIO_PUPDR_PUPDR8_0;	                      //  pull-up,  
  GPIOB->PUPDR  |=  GPIO_PUPDR_PUPDR9_0;	                      //  pull-up, 
  GPIOB->OSPEEDR |=  (3<<16);
  GPIOB->OSPEEDR |=  (3<<18);
  RCC->APB1ENR  |= RCC_APB1ENR_I2C1EN;                         // Enable clock for I2C1
  while(!(RCC->APB1ENR & RCC_APB1ENR_I2C1EN) ){} 
  for(uint16_t i=0;i<1000;i++); 
     NVIC_SetPriority(I2C1_EV_IRQn,                      6);
     NVIC_EnableIRQ(I2C1_EV_IRQn);
     NVIC_SetPriority(I2C1_ER_IRQn,                      7);
     NVIC_EnableIRQ(I2C1_ER_IRQn);
    I2C1->CR1    &=  ~I2C_CR1_PE;                             // I2C peripheral disable 
  I2C1->CR2      |=   0x2A;                                         //0x14;              
  I2C1->CCR      |=   0X0D2;                                   //208;      
  I2C1->TRISE    |=   0x2B;                                    //0x5;                                    
  I2C1->CR1      |=  I2C_CR1_PE;
  for(uint16_t i=0;i<1000;i++); 
}