I2C com problem

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

Moderators: RoccoMarco, barthess

SwatSid
Posts: 1
Joined: Tue Jun 27, 2017 11:25 am

I2C com problem

Postby SwatSid » Tue Jun 27, 2017 11:40 am

Hello,
I am just starting to use stm32 in chibistudio and found the forums and examples very useful. Currently I am trying to set up an I2C communication between the microcontroller and AIS328DQ. I tried using I2Cdev library as well as the native example code for I2c, modifying the device slave address and testing with the WHO_AM_I register of the AIS328DQ. But i am not able to figure out why the communication is not happening.The code i used is this :

Code: Select all

#include <stdlib.h>

#include "ch.h"
#include "hal.h"

static SerialConfig sd2cfg = {
   115200,
   0,
   USART_CR2_STOP1_BITS | USART_CR2_LINEN,
   0
};

static void print(char *p) {

  while (*p) {
    sdPut(&SD2, *p++);
  }
}

static const I2CConfig i2cfg2 = {
    OPMODE_I2C,
    400000,
    FAST_DUTY_CYCLE_2,
};

int main(void) {
  msg_t status = RDY_OK;
  systime_t tmo = MS2ST(4);
 
  halInit();
  chSysInit();

  /*
   * Starts I2C
   */
  palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(0));

  i2cStart(&I2CD1, &i2cfg2);
 
  palSetPadMode(GPIOB, 6, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN);
  palSetPadMode(GPIOB, 7, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN);

 
  /*
   * Prepares the Serial driver 2
   */
  sdStart(&SD2, &sd2cfg);          /* Default is 38400-8-N-1.*/
  palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
  palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));
 
  i2cAcquireBus(&I2CD1);
  txbuf[0] = 0x0F;
  status = i2cMasterTransmitTimeout(&I2CD1, 0x19, txbuf, 1, rxbuf, 1, tmo); //0x19 being address of AIS32DQ and 0x0F is WHO_AM_I
  i2cReleaseBus(&I2CD1);
 
  char msg1;
 
  if (status != RDY_OK){
    errors = i2cGetErrors(&I2CD1);
   print("i2c fault");
  }
 
  while(1){}
}


What here i am doing is, if the i2c communication has an error, it enters the if block and if it does, it should print "i2c fault" and everytime it does print it. I connected the SCL pin to PA6 and SDx(SDA) pin to PA7 and Vdd to 3V and GND t0 GND. Please help me find the problem with this setup. I am only a beginner and hence also please try to keep it simple while explaining. Thank you.

Regards,
Sid

steved
Posts: 823
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: I2C com problem

Postby steved » Tue Jun 27, 2017 1:57 pm

Firstly, I'd suggest a longer timeout; if only to make sure that's not your problem.

Which processor are you using? The STM32F1XX family has problems with 1-byte I2C messages.

Have you got pullups to 3.3V on the SDA and SCL lines? (4k7 or thereabouts suggested)

Are you using a 'standard' board? If so, worth checking that there are no jumper links that need changing so that the signals connect as you expect.

rew
Posts: 380
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 13 times

Re: I2C com problem

Postby rew » Sun Jul 02, 2017 9:12 am

Steve, I really don't think that a hardware I2C device needs more tha n 4ms to react. I've written software I2C slaves on 8MHz processors and the reaction time there is like 2.5 bit times, or 25 microseconds.

There are two ways of looking at I2C addresses. The address is transmitted as a byte with the top 7 bits being the addres, and the lowest bit read/write. I copied the convention from (IIRC) Microchip that you mention the byte that is transmitted on the bus, with one higher being the read instruction.

In your case you are giving the 0x19 to the I2C routine and you're hoping that this is shifted by one bit before being put on the I2C bus as 0x32.

A quick glance at the datasheet does not make it clear to me that the "x" mentioned in the address is a configurable (jumperable: the chip CAN occupy either of two different addresses) address bit and not a dynamic address bit (the chip occupies two addresses and uses the bottom address bit for internal addressing). In the last case, your lowest bit may need to be 0. In the first case, you need to have jumpered the address selection bit high.

steved
Posts: 823
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: I2C com problem

Postby steved » Mon Jul 03, 2017 8:37 am

rew wrote:Steve, I really don't think that a hardware I2C device needs more tha n 4ms to react. I've written software I2C slaves on 8MHz processors and the reaction time there is like 2.5 bit times, or 25 microseconds.

In principle I agree with you; however once you have an RTOS other things can potentially delay processing the I2C task (and I have encountered this), so it's worth making sure this isn't a problem.
And on the slave side, I've had the opposite problem to you - so much processing required to generate the return message that the master timed out!


Return to “STM32 Support”

Who is online

Users browsing this forum: Bing [Bot] and 18 guests