Page 1 of 1

Issue with I2C in STM32F1

Posted: Mon Mar 27, 2017 11:55 am
by BinZh
Hello,
I am trying to run a very simple demo of I2C with ChibiOS at STM32F1.
I have looked lots of articles, and very confused.
I want to know how ChibiOS build a connection with GPIO and I2C mode. It means that I do not know how to initial a GPIO (palSetPadMode at which mode) and how to build a connection with the Pins and I2C.
Is anyone can help me? I will be very grateful. :cry:

Re: Issue with I2C in STM32F1

Posted: Mon Mar 27, 2017 12:19 pm
by BinZh
here is my simple code in main.c
I connect GOPIB Pin6 or Pin7 to Oscilloscope without slave device. But only 0 voltage at the screen.

Code: Select all

#include "ch.h"
#include "hal.h"
#define STM32F103C8
#define addr 0b1001100
//#include "test.cpp"

/*
 * I2C1 config.
 */
static const I2CConfig i2cfg1 = {
    OPMODE_I2C,
    100000,
    FAST_DUTY_CYCLE_2,
};
static uint8_t accel_tx_data[8];

I2CDriver I2CD1;

int main(void) {

  halInit();
  chSysInit();
  i2cInit();
  palSetPadMode(GPIOB, GPIOB_I2C1_SCL, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
  palSetPadMode(GPIOB, GPIOB_I2C1_SDA, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
  i2cStart(&I2CD1, &i2cfg1);

  msg_t status = MSG_OK;
  systime_t tmo = MS2ST(4);

  accel_tx_data[0] = 0b01000001;
  accel_tx_data[1] = 0b11100111;
  accel_tx_data[2] = 0b01000001;
  accel_tx_data[3] = 0b00000000;
  while(1){
      i2cAcquireBus(&I2CD1);
      status = i2cMasterTransmitTimeout(&I2CD1, addr,
                          accel_tx_data, 4, NULL, 0, tmo);
      i2cReleaseBus(&I2CD1);
  }


}

Re: Issue with I2C in STM32F1

Posted: Mon Mar 27, 2017 12:43 pm
by BinZh
Thanks every one!

I understand that my code is able to use, but I need a external pull-up res!

Thank you~

Re: Issue with I2C in STM32F1

Posted: Mon Mar 27, 2017 1:11 pm
by tfAteba
Hi BinZh,

To understand how to use GPIO in I2C mode with ChibiOS, you can use test project located at: /testhal/STM32/STM32F1xx/I2C/
It shows how to read data from Accelerometer by using gpio pins configured in I2C mode.

Re: Issue with I2C in STM32F1

Posted: Mon Mar 27, 2017 2:21 pm
by Giovanni
Hi,

There are some notes about I2C usage in this article: http://wiki.chibios.org/dokuwiki/doku.p ... e_shooting

Giovanni