Issue with I2C in STM32F1

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
BinZh
Posts: 3
Joined: Mon Mar 27, 2017 9:46 am

Issue with I2C in STM32F1

Post 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:
BinZh
Posts: 3
Joined: Mon Mar 27, 2017 9:46 am

Re: Issue with I2C in STM32F1

Post 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);
  }


}
BinZh
Posts: 3
Joined: Mon Mar 27, 2017 9:46 am

Re: Issue with I2C in STM32F1

Post by BinZh »

Thanks every one!

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

Thank you~
User avatar
tfAteba
Posts: 551
Joined: Fri Oct 16, 2015 11:03 pm
Has thanked: 91 times
Been thanked: 50 times

Re: Issue with I2C in STM32F1

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Issue with I2C in STM32F1

Post by Giovanni »

Hi,

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

Giovanni
Post Reply