I2CD1 is not working

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
Post Reply
KalRal
Posts: 6
Joined: Tue Feb 12, 2019 9:08 pm
Has thanked: 2 times
Been thanked: 1 time

I2CD1 is not working

Post by KalRal »

First of all, thank you all very much for this amazing RTOS and making it OpenSource.
I have been trying for the last day to interface ADXL345 to STM32F4 discovery and has been unsuccessful before the last moment. The code was simple, as I was just testing it and it includes just to read device ID stored on ADXL345. I came to know that I2CD1 driver was not working but for the same exact code I2CD2 is working. I just want to bring this problem to your attention and ask whether I have made any mistakes in the process.

I have used two 4k7 pull-ups for SDA and SCL lines and the wave form has been observed using a Logic analyzer.

Code: Select all

#define ADXL345_REG_DEVID               (0x00)    // Device ID
#define ADXL345_DEFAULT_ADDRESS     (0x53)


The I2C driver configuration can be seen below.

Code: Select all

/*
 * I2C Driver Configuration
 */
static const I2CConfig i2cConfig = {
      OPMODE_I2C,
      100000,
      STD_DUTY_CYCLE,
};


A Simple while loop which reads the device address can be seen below

Code: Select all

while (true) {
     bool value = I2CreadBytes(ADXL345_DEFAULT_ADDRESS, ADXL345_REG_DEVID, 1, returnData);
   if(value == TRUE)
   {
      chprintf((BaseSequentialStream*)&SD2, "Sucessfully transmitted\n");
   }
   else
   {
      chprintf((BaseSequentialStream*)&SD2, "Failure\n");
   }
    chThdSleepMilliseconds(1000);


The function for reading can be seen below.

Code: Select all

int8_t I2CreadBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data) {
   msg_t rdymsg;
   if(length > I2CDEV_BUFFER_LENGTH) {
      return FALSE;
   }
   i2cAcquireBus(&I2CD1);
   rdymsg = i2cMasterTransmitTimeout(&I2CD2, devAddr, &regAddr, 1, data, length, TIME_INFINITE);
   i2cReleaseBus(&I2CD1);
   if(rdymsg == MSG_TIMEOUT || rdymsg == MSG_RESET) {
      chprintf((BaseSequentialStream*)&SD2, "\n%d\n", rdymsg);
      chprintf((BaseSequentialStream*)&SD2,"I2C ERROR: %d\n", i2cGetErrors(&I2CD2));
      chprintf((BaseSequentialStream*)&SD2, "Received : 0x%02X", data[0]);
      return FALSE;
   }
   return TRUE;
}


I have tested with the same connections(but with different example code) on NodeMCU as well and it seems to be working well. All the pictures were taken while testing will be attached below. The strange thing is that STM32F4 doesn't transmit a "Read" message, even though it has received ACK from the slave.

STM32F4 - ChibiOS I2CD1
[img]
Chibios_I2CD1.png

[/img]

STM32F4 - Chibios I2CD2
[img]
Chibios_I2CD2.png

[/img]

NodeMCU
Arduino.png
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: I2CD1 is not working

Post by Giovanni »

Hi,

You should verify some things:
1) Do you have DMA conflicts on I2C1? it could happen if you have other peripherals in use sharing the same DMAs.
2) Have you enabled debug options in chconf.h? assertions, state checker mainly.
3) Are GPIO pins programmed correctly in the appropriate alternate mode?

Giovanni
KalRal
Posts: 6
Joined: Tue Feb 12, 2019 9:08 pm
Has thanked: 2 times
Been thanked: 1 time

Re: I2CD1 is not working

Post by KalRal »

Hi Giovanni,

I am not using any other peripheral except I2C and Serial UART. Yeah, the assertions are kept as defaults in chconf.h.
CH_DBG_SYSTEM_STATE_CHECK

and
CH_DBG_ENABLE_STACK_CHECK
are also TRUE. I also had the same doubt initially and triple checked whether the GPIOs are properly configured. I even followed the I2C debug guide which I found while going through a couple of forums.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: I2CD1 is not working

Post by Giovanni »

The I2Cdriver code is the same for all I2C units, what is different are: clock enable/disable macros, DMA settings, IRQs numbers, GPIO settings.

The problem should be not in the driver itself but in one of the above places.

Giovanni
Post Reply