cancel
Showing results for 
Search instead for 
Did you mean: 

STM32WB cannot connect to adafruit feather through I2C

QQian.1
Associate

Hi there,

I tried to set STM32 as the master and adafruit feather ESP8266 as the slave. They are going to communicate through I2C. Below is the configuration of the i2c3 because it is the only port for external sensors:0693W00000aJWCfQAO.pngThen I write below code for master STM32:

HAL_StatusTypeDef ret;
uint8_t counter;
uint8_t i2c_addr = 0;
 
MX_I2C3_Init();
 
while (1) {
	for (counter = 0; counter < 255; counter++) {
		ret = HAL_I2C_IsDeviceReady(&hi2c3, (uint16_t)(counter<<1), 3, HAL_MAX_DELAY);
		if(ret == HAL_OK) {
			i2c_addr = counter;
			HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
		}
	}
	HAL_Delay(500);
}

And code in Arduino for slave feather:

#include <Wire.h>
 
#if defined(ARDUINO_SAMD_ZERO) && defined(SERIAL_PORT_USBVIRTUAL)
  // Required for Serial on Zero based boards
  #define Serial SERIAL_PORT_USBVIRTUAL
#endif
 
int cnt = 0;
 
void receiveEvent(int howMany)
{
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c); // print the character
  }
  int x = Wire.read(); // receive byte as an integer
  Serial.println(x); // print the integer
}
 
void setup() {
  // put your setup code here, to run once:
  Wire.begin(2);
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);
}
 
void loop() {
  // put your main code here, to run repeatedly:
  delay(100);
}

All I got is `HAL_ERROR`. But I can receive something on SDA (so as SCL):

0693W00000aJWDnQAO.jpgFor hardware connection, I connected two 10k pull-up resistors to SDA and SCL. Did I do something wrong? I really appreciate any help to this problem !!

3 REPLIES 3
Foued_KH
ST Employee

Hello @QQian.1​ ,

Please check the error flags set in the status register and try to get the error (HAL_I2C_GetError).

Make sure you are using correct parameters for this function HAL_I2C_IsDeviceReady().

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Thanks for the reply. I tested and got:

HAL_I2C_STATE_READY in hi2c3 state

and HAL_I2C_ERROR_TIMEOUT

Any ideas about this?

Please try to put 4.7k resistor to SDA and SDC each,

GPIO_InitStruct.Pull = GPIO_PULLUP, is often not enough.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.