I have 2 MCU, one is the STM32F401RE acting as master and the other is STM32G474RE acting as slave, I have kept both the controllers in open drain and pulled up, the slave address is 0x30<<1, I think the data is being transferred because on pressing the reset button on the master, the slave displays this over UART "SLAVE HAL I2C NOT READY" , live expression is not working for me so that was my other option.
My master code is this
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART2_UART_Init();
char outputBuffer[200];
char errorBuffer[200];
uint8_t errorTxBuffer[] ="Master Transmission Not Complete";
uint8_t errorTxBuffer1[] ="HAL";
do
{
/*##-2- Start the transmission process #####################################*/
/* While the I2C in reception process, user can transmit data through
"aTxBuffer" buffer */
if(HAL_I2C_Master_Transmit_IT(&hi2c1, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer, sizeof(aTxBuffer))!= HAL_OK)
{
/* Error_Handler() function is called in case of error. */
Error_Handler();
}
/*##-3- Wait for the end of the transfer ###################################*/
/* Before starting a new communication transfer, you need to check the current
state of the peripheral; if it�s busy you need to wait for the end of current
transfer before starting a new one.
For simplicity reasons, this example is just waiting till the end of the
transfer, but application may perform other tasks while transfer operation
is ongoing. */
while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
{
sprintf(errorBuffer, " State: %s \r\n",errorTxBuffer);
HAL_UART_Transmit(&huart2,errorTxBuffer,strlen((char*)errorTxBuffer),HAL_MAX_DELAY);
HAL_Delay(500);
}
/* When Acknowledge failure occurs (Slave don't acknowledge its address)
Master restarts communication */
}
while(HAL_I2C_GetError(&hi2c1) == HAL_I2C_ERROR_AF);
sprintf(outputBuffer, "DATA: %s \r", aTxBuffer);
HAL_UART_Transmit(&huart2,aTxBuffer,strlen((char*)aTxBuffer),HAL_MAX_DELAY);
HAL_Delay(500);
/*##-4- Put I2C peripheral in reception process ############################*/
// do
// {
// if(HAL_I2C_Master_Receive_IT(&hi2c1, (uint16_t)I2C_ADDRESS, (uint8_t *)aRxBuffer, sizeof(aRxBuffer)) != HAL_OK)
// {
// /* Error_Handler() function is called in case of error. */
// Error_Handler();
// }
//
// /* When Acknowledge failure occurs (Slave don't acknowledge its address)
// Master restarts communication */
// }
// while(HAL_I2C_GetError(&hi2c1) == HAL_I2C_ERROR_AF);
// sprintf(errorBuffer, "Error: %s \r\n",errorTxBuffer1);
// HAL_UART_Transmit(&huart2,errorTxBuffer1,strlen((char*)errorTxBuffer1),HAL_MAX_DELAY);
// HAL_Delay(500);
}
My slave code is this
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
MX_LPUART1_UART_Init();
char outputBuffer[200];
char errorBuffer[200];
uint8_t errorRxBuffer[] ="SLAVE HAL I2C NOT READY";
uint8_t errorRxBuffer1[] ="SLAVE HAL NOT OK";
uint8_t errorRxBuffer2[] ="SLAVE RUN";
while (1)
{
if(HAL_I2C_Slave_Receive_IT(&hi2c1, (uint8_t *)aRxBuffer, sizeof(aRxBuffer)) != HAL_OK)
{
/* Transfer error in reception process */
sprintf(errorBuffer, "Error: %s \r\n",errorRxBuffer1);
HAL_UART_Transmit(&hlpuart1,errorRxBuffer1,strlen((char*)errorRxBuffer1),HAL_MAX_DELAY);
HAL_Delay(500);
Error_Handler();
}
/*##-3- Wait for the end of the transfer ###################################*/
/* Before starting a new communication transfer, you need to check the current
state of the peripheral; if it�s busy you need to wait for the end of current
transfer before starting a new one.
For simplicity reasons, this example is just waiting till the end of the
transfer, but application may perform other tasks while transfer operation
is ongoing. */
while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
{
sprintf(errorBuffer, "Error: %s \r\n",errorRxBuffer);
HAL_UART_Transmit(&hlpuart1,errorRxBuffer,strlen((char*)errorRxBuffer),HAL_MAX_DELAY);
HAL_Delay(500);
}
//aRxBuffer[sizeof(aRxBuffer) - 1] = '\0';
/////////////////////////////////////////////////////////////////////////////////////
sprintf(errorBuffer, "Error: %s \r\n",errorRxBuffer2);
HAL_UART_Transmit(&hlpuart1,errorRxBuffer2,strlen((char*)errorRxBuffer2),HAL_MAX_DELAY);
HAL_Delay(500);
///////////////////////////////////////////////////////////////////////////////////
sprintf(outputBuffer, "DATA: %s \r", aRxBuffer);
HAL_UART_Transmit(&hlpuart1,aRxBuffer,strlen((char*)aRxBuffer),HAL_MAX_DELAY);
HAL_Delay(500);
// /*##-4- Start the transmission process #####################################*/
// /* While the I2C in reception process, user can transmit data through
// "aTxBuffer" buffer */
// if(HAL_I2C_Slave_Transmit_IT(&I2cHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK)
// {
// /* Transfer error in transmission process */
// Error_Handler();
// }
}
}
I have attached both codes. Please help me out since i am trying to figure this out for the past week