Question
I2C communication between 2 STM32 MCUs
Hello I am trying to learn I2C communication between 2 STM32F103C8 MCUs. MCU1 acts as the master and MCU2 acts as the slave. I am using CubeMX to configure the settings however I could not establish a link between the 2 MCUs. What I wanted to happen is that once I toggle a button connected to MCU1, the LED controlled by MCU2 should be enabled.
May I ask for your advice on the code below.
Master MCU1 code:
//Enable Button_LED1 and transmit 0xFF data. 0x5A (0b01011010) Slave + Write (0)
if(HAL_GPIO_ReadPin(BUTTON_LED1_GPIO_Port, BUTTON_LED1_Pin))
{
Buffer_I2C[0] = 0xFF;
HAL_I2C_Master_Transmit(&hi2c1, 0x5A, Buffer_I2C, 1, 10);
}
//Disable Button_LED1 and transmit 0x00 data
else
{
Buffer_I2C[0] = 0x00;
HAL_I2C_Master_Transmit(&hi2c1, 0x5A, Buffer_I2C, 1, 10);
}
Slave MCU2 code:
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0x5B; //Slave + Read (1)
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
HAL_I2C_Slave_Receive(&hi2c1, Buffer_I2C, 1, 10);
uint8_t data = Buffer_I2C[0];
switch(data)
{
case 0xFF:
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET);
break;
case 0x00:
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
break;
default:
break;
}
}:
