AnsweredAssumed Answered

STM32 HAL I2C Problem

Question asked by BesenSelqnin on Nov 29, 2016
Latest reply on Nov 30, 2016 by FTITI.Walid
Hello everyone,

for my current project I am trying to communicate with my STM32F407 Discovery board via I2C. I want to poll different data from it using an Arduino board, similar to a sensor I have. The sensor polling already works and is as follows: I send send the address with a write bit, then a register I want the value from, then I send the slave address once more, only this time with a read bit. I then expect a float.

So far I have managed to detect my STM using a scanner I implemented. I have trouble moving on from there though mainly because I do not understand the driver description of the HAL library I suppose.

My code:


//I2C Initialization

I2C_HandleTypeDef hi2c1;
void InitializeI2C1()
{
     __I2C1_CLK_ENABLE();
     hi2c1.Instance             = I2C1;
     hi2c1.Init.AddressingMode  = I2C_ADDRESSINGMODE_7BIT;
     hi2c1.Init.ClockSpeed      = 100000;
     hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
     hi2c1.Init.DutyCycle       = I2C_DUTYCYCLE_2;
     hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
     hi2c1.Init.NoStretchMode   = I2C_NOSTRETCH_DISABLED;
     hi2c1.Init.OwnAddress1     = 0x25<<1;
     if (HAL_I2C_Init(&hi2c1) != HAL_OK)
     {
          Error_Handler();
     }


     HAL_NVIC_SetPriority(I2C1_EV_IRQn,0,1);
     HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);


     __GPIOB_CLK_ENABLE();
     GPIO_InitTypeDef GPIO_InitStructure;
     GPIO_InitStructure.Pin       = GPIO_PIN_6|GPIO_PIN_7;
     GPIO_InitStructure.Mode      = GPIO_MODE_AF_OD; //
     GPIO_InitStructure.Speed     = GPIO_SPEED_HIGH;
     GPIO_InitStructure.Pull      = GPIO_PULLUP;
     GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
     HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);


     HAL_I2C_EnableListen_IT(&hi2c1);
}

extern "C" void I2C1_EV_IRQHandler()
{
     HAL_I2C_EV_IRQHandler(&hi2c1);
}

// My code for the (already functioning) detect:
void HAL_I2C_AddrCallback(I2C_HandleTypeDef * hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
{


}

void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef * hi2c)
{
     HAL_I2C_EnableListen_IT(&hi2c1);
}

void main()
{
     InitializeI2C1();


     while(1)
     {
     }
}


For the next step I am just sending the 1 byte register address. So, on the STM board the question is: what do I do? I assume I still have to have the HAL_I2C_EnableListen_IT, otherwise no transmission will take place, correct? Where in the code should I implement the HAL_I2C_Slave_Receive_IT because if I do it in the HAL_I2C_AddrCallback, the I2C line gets help up and nothing works anymore. The same applies if I put it in my main...

Thank you for your time.

Outcomes