cancel
Showing results for 
Search instead for 
Did you mean: 

I2C2 configuration

gil23
Associate III
Posted on March 03, 2015 at 09:12

Hi I'm working on STM32F401,

I'm trying to configure I2C2 port as an I2C Master,

when i Generate START condition i don't get an interrupt. 

What I'm doing wrong?

  //Configure PB10 I2C2 Master, SCL pin PB3 I2C2 Master SDA pin

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_10;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_Init(GPIOB, &GPIO_InitStructure); // init GPIOB

  //Configure the NVIC for I2C2 MASTER   

  NVIC_InitStructure.NVIC_IRQChannel = I2C2_EV_IRQn;// Configure the I2C event priority

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  

  NVIC_InitStructure.NVIC_IRQChannel = I2C2_ER_IRQn;// Configure I2C error interrupt to have the higher priority

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 7;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  

  NVIC_SetPriority(I2C2_EV_IRQn ,6 );//I2C2 MASTER

  NVIC_SetPriority(I2C2_ER_IRQn ,7 );//I2C2 MASTER  

  I2C_InitTypeDef I2C_InitStruct;

  

  I2C_DeInit(I2C2);

  // enable APB1 peripheral clock for I2C2

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);/*SCL GPIO clock enable */

  RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);/* Reset I2Cx IP */

  

  // Connect I2C2 pins to AF  

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF9_I2C2);       // SCL

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2);      // SDA

  

  // configure I2C2 

  I2C_InitStruct.I2C_ClockSpeed = 400000; // 400kHz

  I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C mode

  I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard

  I2C_InitStruct.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode

  I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; // disable acknowledge when reading (can be changed later on)

  I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses

  I2C_Init(I2C2, &I2C_InitStruct); // init I2C3

  // enable I2C2

  I2C_Cmd(I2C2, ENABLE);

    I2C_ITConfig(I2C2, (I2C_IT_EVT | I2C_IT_BUF), ENABLE);// Enable Event Interrupts

    I2C_AcknowledgeConfig(I2C2, ENABLE);// Enable Acknowledge

    I2C_GenerateSTART(I2C2, ENABLE);// Generate the Start condition

4 REPLIES 4
Posted on March 03, 2015 at 13:11

This needs to be enabled BEFORE GPIO_Init()

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gil23
Associate III
Posted on March 03, 2015 at 13:38

Thanks,

I did this & it's steel doesn't  works, I get on SR2 register BUSY=1. Both lines SDA and SCL are HIGH. 

Posted on March 03, 2015 at 13:59

// enable I2C2
I2C_Cmd(I2C2, ENABLE);
// configure I2C2 
I2C_InitStruct.I2C_ClockSpeed = 400000; // 400kHz
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C mode
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard
I2C_InitStruct.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode
I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; // disable acknowledge when reading (can be changed later on)
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses
I2C_Init(I2C2, &I2C_InitStruct); // init I2C2

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gil23
Associate III
Posted on March 03, 2015 at 14:36

Thanks,

I did this & it's steel doesn't  works, I get on SR2 register BUSY=1. Both lines SDA and SCL are HIGH.