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 !!

4 REPLIES 4
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.

jeonwookyu
Associate

//#include <Wire.h>
//#include <Wire_slave.h>
#define I2C_MASTERSLAVE 1  // 1이면 마스터, 0이면 슬레이브
#define SLAVE_ADDRESS 0x08
#if I2C_MASTERSLAVE == 1  // 마스터
#include <Wire.h>
#endif

#if I2C_MASTERSLAVE == 0  // 슬레이브 모드일 때 사용
#include <Wire_slave.h>

void requestEvent();
void receiveEvent(int numBytes);
 String receivedData = "";
bool stringComplete = false;
#endif
void setup(){
   Wire.begin(SLAVE_ADDRESS);                // join i2c bus with address #4
   Serial.begin(9600);
          // start serial for output
  #if I2C_MASTERSLAVE == 0  // 슬레이브 모드일 때 사용
  Wire.onReceive(receiveEvent); // register event
  Wire.onRequest(requestEvent);  // 요청 이벤트 등록
  receivedData.reserve(200);
  #endif
}

void loop(){
 #if I2C_MASTERSLAVE == 1
 Wire.beginTransmission(SLAVE_ADDRESS);

 Wire.setClock(40000L); // Set speed at 40kHz
  Wire.write("1234567890abcd\n");//종료문자추가
  Wire.endTransmission();

  Serial.println("Sent data to STM32");  // 전송한 데이터 시리얼로 출력
  delay(3000);  // 1초 대기
  #endif
}

#if I2C_MASTERSLAVE == 0
void receiveEvent(int howMany){

 

 while(Wire.available()){ // loop through all but the last

   char c = (char)Wire.read(); // receive byte as a character
   receivedData += c;
   //Serial.print(c);
    if(c=='\n'){
     stringComplete = true;
    }
     if (stringComplete) {
    Serial.println(receivedData);
    // clear the string:
    receivedData = "";
    stringComplete = false;
  }
 }

}
 
void requestEvent() {
   
}
#endif