STM32F7 I2C

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

Moderator: RoccoMarco

Post Reply
Koen
Posts: 35
Joined: Mon Dec 21, 2015 12:15 am
Been thanked: 5 times

STM32F7 I2C

Post by Koen »

Hello,

    as anyone successfully used I2C with an STM32F7 ? I haven't found tests or demos of it and have tried to debug my try for hours now. Here's a condensed version of it :

    Code: Select all

    int main(void) {

       halInit();

       chSysInit();

       palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(4) | PAL_MODE_OUTPUT_OPENDRAIN);
       palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(4) | PAL_MODE_OUTPUT_OPENDRAIN);

       i2cInit();

       const I2CConfig i2cd1_cfg = {
          .timingr    = 0x40912732,
          .cr1       = 0,
          .cr2      = 0
       };

       i2cStart(&I2CD1, &i2cd1_cfg);

       msg_t       i2c_result;
       i2cflags_t   i2c_errors;
       uint8_t    i2c_rxbuf[129],
                i2c_txbuf[129];

       memset(i2c_txbuf, 0, sizeof(i2c_txbuf));

       i2cAcquireBus(&I2CD1);

       i2c_result = i2cMasterTransmitTimeout(&I2CD1, 0x37, i2c_txbuf, sizeof(i2c_txbuf), i2c_rxbuf, sizeof(i2c_rxbuf), MS2ST(1000));

       if(i2c_result != MSG_OK)
          i2c_errors = i2cGetErrors(&I2CD1);

       (void) i2c_errors;

       i2cReleaseBus(&I2CD1);

       i2cStop(&I2CD1);

       while (true) {

          chThdSleepMilliseconds(500);

       }

    }


    Defines modified from defaults :

    Code: Select all

    #define HAL_USE_I2C            TRUE
    #define STM32_I2C_USE_I2C1 TRUE
    #define STM32_I2C_USE_DMA FALSE


    Results :

    Code: Select all

    i2c_result = -1 / MSG_RESET,
    i2c_errors = 0


    Is there anything obviously wrong ? I wrote the equivalent with STM32CubeHAL to make sure the hardware isn't at fault and it works.
Thank you,
Koen
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F7 I2C

Post by Giovanni »

Hi,

I haven't tested that driver on the F7 yet, there could be an error (likely in the registry if there is an error).

Giovanni
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: STM32F7 I2C

Post by steved »

I've successfully used the I2C on the F7 Disco in master mode (and, I think, in slave mode - can't remember for sure)

Some of my code is here

And I've just uploaded a specific example which worked for me

(Note that I was working with my modified driver which also handles slave mode, but I don't think I had to change any of the master-related code, except that there was a bug in the master code for transfers >255 bytes which is fixed in SVN)
Attachments
F746_I2C_Master_Test.zip
F7 Disco I2C test
(51.22 KiB) Downloaded 395 times
Koen
Posts: 35
Joined: Mon Dec 21, 2015 12:15 am
Been thanked: 5 times

Re: STM32F7 I2C

Post by Koen »

Well, sorry to have wasted your time. I2C does work. I had commented

Code: Select all

#if (HAL_USE_PAL == TRUE) || defined(__DOXYGEN__)
  //palInit(&pal_default_config);
#endif

to reduce the test-case and concentrate on the two I2C pins only. I only discovered much later that palInit was also responsible for enabling the GPIO clocks.
Post Reply