AnsweredAssumed Answered

I2C HAL library generating hard fualt on stm32f302

Question asked by conrad c on May 1, 2018
Latest reply on May 1, 2018 by Clive One

Hi ,

 

I am migrating from STM32f101 I2C to Stm32f302, the migration document says this is a complete re write of the code . So i used the STM cube to generate the startup code for the I2C for 100khz. I know the board is working and the external component is correct as it is on the same pins. to access the data on the external port expander pca9554 i need to write the slave address +command then read the data by sending the slave address and 8 extra clocks 

 

I am assuming the Hal library is taking care of the start stop and acknowledgements on the i2c bus

 

I2C acording to cube is setup as follows for 7 bit adress mode

/* I2C1 init function */
void MX_I2C1_Init2(void)
{

hi2c2.Instance = I2C1;
hi2c2.Init.Timing = 0x00901D23;
hi2c2.Init.OwnAddress1 = 0;
hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c2.Init.OwnAddress2 = 0;
hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}

 

 

My reading of the data is a follows slave adress is 0x40 ,ReadAddr=command byte =0x00

//========================================================================
//
// pBuffer: is pointer for data BYTE read back
// ReadAddr: contains Command byte of device for reading
//
//============================================================================

void read_9554(uint8_t* pBuffer,uint8_t ReadAddr)
{

u16 NumByteToRead =0x0001;
u8 slave_adressr=0x00;
u8 rxdata=0x00;

slave_adressr=DEV_ADDRESS|0x01;//sets last bit in device address byte for reading

 

while(HAL_I2C_Master_Transmit(&hi2c2, (uint16_t)slave_adressr, (uint8_t*)ReadAddr, 1, 10000)!= HAL_OK)
{
/* Error_Handler() function is called when Timeout error occurs.
When Acknowledge failure occurs (Slave don't acknowledge its address)
Master restarts communication */
if (HAL_I2C_GetError(&hi2c2) != HAL_I2C_ERROR_AF)
{
Error_Handler();
}
}


while(HAL_I2C_Master_Receive(&hi2c2, (uint16_t)slave_adressr, (uint8_t *)pBuffer, 1, 10000) != HAL_OK)
{
/* Error_Handler() function is called when Timeout error occurs.
When Acknowledge failure occurs (Slave don't acknowledge it's address)
Master restarts communication */
if (HAL_I2C_GetError(&hi2c2) != HAL_I2C_ERROR_AF)
{
Error_Handler();
}
}

 

i2c_delay(10);
}

 

 

Every time it get to the wrtie or read function it goes to the hard error function , i can figure out why .

setup is taken from the I2C/I2C_TwoBoards_ComPolling/Src/main.c  in the cube repository so it should work but it doesn't.

 

Could someone please help.

 

thanks

Outcomes