andysworkshop / stm32plus

The C++ library for the STM32 F0, F100, F103, F107 and F4 microcontrollers
http://www.andybrown.me.uk
Other
745 stars 224 forks source link

I2C master with Arduino I2C slave #235

Closed alec-g closed 3 years ago

alec-g commented 3 years ago

Hello all, I am having trouble getting the stm32plus i2c interface talking to an arudino which i have setup as an i2c slave. The arduino i2c slave is setup using the standard arduino Wire library, i have confirmed i am able to communicate to the slave using another arduino and access internal registers, however no matter what i try i cannot get my stm32f4 to read or write registers on this arduino slave. I have setup a very simple i2c master on the stm32:

static const uint8_t SLAVE_ADDRESS = 0x5F;
static const uint8_t CURRENT_SPEED = 0x14;

struct I2CPinPackage
{
  enum
  {
    Port_SCL=GPIOF_BASE,
    Port_SDA=GPIOH_BASE,
    Pin_SCL=GPIO_Pin_1,
    Pin_SDA=GPIO_Pin_5
  };
};

typedef stm32plus::I2C2_Custom<I2CPinPackage, stm32plus::I2CSingleByteMasterPollingFeature> I2C;

int main() {
  stm32plus::Nvic::initialise();
  stm32plus::MillisecondTimer::initialise();

  I2C* i2c_;

  I2C::Parameters i2c_params;
  i2c_params.i2c_clockSpeed = 50000;

  i2c_ = new I2C(i2c_params);
  i2c_->setSlaveAddress(SLAVE_ADDRESS);
  i2c_->enablePeripheral();

  while(1) {
    uint8_t read_buffer;
    if (!(i2c_->readBytes(CURRENT_SPEED, &read_buffer, 1))) {
      // Error!
    }
  }

  // not reached
  return 0;
}

I have tripple checked the pin mapping is correct, i have even tried alternative pin mappings supported by my board, is there something i am doing wrong in the initialisation? Or is there something special about the arduino Wire library and its protocol that the stm32plus library does not accomodate for? Any help is appreciated.

alec-g commented 3 years ago

Solved! I am unsure why, but i had to change the slave address from 0x5F to 0x5F<<1, i guess something to do with 7-bit slave addressing?