Skip to content

An error occurred in the communication between the stm32f103c8t6 and python smbus #1436

@AITCK

Description

@AITCK

The code I tested is below.
python code:

import smbus
bus = smbus.SMBus(1)
# Read a block of 6 bytes from address 0x18, offset 0
data = bus.read_i2c_block_data(0x18, 0, 6) 

arduino code:

#include <Wire.h>

#define I2C_ADDR  0x18

void setup()
{
  Wire.begin(I2C_ADDR);         // join i2c bus with address #0x18
  Wire.onRequest(requestEvent); // register event
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(115200);           // start serial for output
}

void loop()
{
  //empty loop
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(0 < 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
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
  Wire.write("hello\n");  // respond with message of 6 bytes
                          // as expected by master
}

Test Results:
The code was successfully uploaded to stm32 without errors. When I run the python smbus code, 6 bytes of data were successfully read, but I did not see any information on the serial monitor.

I changed my bluepill to arduino nano for testing and the code runs successfully. The difference is that I saw the print data from the serial monitor. The printed data is exactly the offset parameter in the smbus code.

bus.read_i2c_block_data(address, offset, data size) 

In other words, the problem with stm32 i2c is that the offset parameter of smbus cannot be obtained.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions