Skip to main content
MC.4
Associate II
June 28, 2022
Question

Connecting two nucleo boards using I2C.

  • June 28, 2022
  • 2 replies
  • 2141 views

Hello, I just started using the Nucleo board and currently I am trying to connect two Nucleo G071RB by using I2C, using one as a Master and another one as a Slave.

I, however, am not able to even get a clock signal coming from the Master (measured using a logic analyzer) and I am not sure why as I'm pretty sure I have set the Pins right in MX. I try using the HAL_I2C_IsDeviceReady function, before the loop, but that already returns a HAL_ERROR as expected.

My code for the Master board can be seen below, if anyone has any tips or ideas please let me know.

int main(void)

{

 uint8_t buffer[3];

 HAL_StatusTypeDef ret;

 HAL_Init();

 /* Configure the system clock */

 SystemClock_Config();

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_I2C1_Init();

 MX_USART2_UART_Init();

 HAL_I2C_MspInit(&hi2c1);

  

 ret = HAL_I2C_IsDeviceReady(&hi2c1, (peripheralNucleoAddress << 1), 3, 5000);

 if (ret != HAL_OK)

 {

 uint8_t message[27];

  strcpy((char*) message, "Connection to board lost\n");

  HAL_UART_Transmit(&huart2, message, strlen((char*) message), 5000);

  //Error_Handler();

 }

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

 ret = HAL_I2C_Master_Transmit(&hi2c1, peripheralNucleoAddress << 1, buffer, 1, 10000);

 if (ret != HAL_OK)

 {

 strcpy((char*) buffer, "Tx");

 HAL_UART_Transmit(&huart2, buffer, strlen((char*) buffer), 1000);

 //Error_Handler();

 }

 ret = HAL_I2C_Master_Receive(&hi2c1, peripheralNucleoAddress << 1 | 1, buffer, 2, 1000);

 if (ret != HAL_OK)

 {

 strcpy((char*) buffer, "Rx");

 HAL_UART_Transmit(&huart2, buffer, strlen((char*) buffer), 1000);

 //Error_Handler();

 }

 if (buffer[1] == 0xFF || buffer[1] == 0x01)

 {

 uint8_t message[9];

 strcpy((char*) message, "Success!");

 HAL_UART_Transmit(&huart2, message, strlen((char*) message), 1000);

 }

 }

}

    This topic has been closed for replies.

    2 replies

    Andrew Neil
    Super User
    June 29, 2022

    I would strongly recommend that you don't try to implement both Master and Slave at the same time!

    https://www.avrfreaks.net/comment/2320631#comment-2320631

    0693W000008xsqBQAQ.png

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Andrew Neil
    Super User
    June 29, 2022
    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.