I2C on OLIMEX-STM32-LCD (STM32F103ZE) Topic is solved

Report here problems in any of ChibiOS components. This forum is NOT for support.
Tuxford
Posts: 26
Joined: Fri Nov 06, 2020 3:28 pm
Has thanked: 4 times
Been thanked: 4 times

I2C on OLIMEX-STM32-LCD (STM32F103ZE)

Post by Tuxford »

I connected to M24C02 eeprom to this board to PB8 (SCL) and PB9 (SDA) as alternate function. But it's always low level on those pins even if palSetLine sets high. The level is about 0.5V. Pullup resistors are present.
Port setup is:

Code: Select all

#define VAL_GPIOBCRL            0x88888888      /*  PB7...PB0 */
#define VAL_GPIOBCRH            0x888888FF      /* PB15...PB8 */
#define VAL_GPIOBODR            0xFFFFFFFF

Config:

Code: Select all

static const I2CConfig i2ccfg = {
    .scl   = PAL_LINE(GPIOB, 8U),
    .sda   = PAL_LINE(GPIOB, 9U),
    .ticks = TIME_US2I(2),
};


Wrong port config or something else?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: I2C on OLIMEX-STM32-LCD (STM32F103ZE)

Post by Giovanni »

Hi,

Is that a customized driver? I don't see those fields in I2CConfig of the I2Cv1 driver.

In general you need to put pins on alternate and open drain mode.

Giovanni
Tuxford
Posts: 26
Joined: Fri Nov 06, 2020 3:28 pm
Has thanked: 4 times
Been thanked: 4 times

Re: I2C on OLIMEX-STM32-LCD (STM32F103ZE)

Post by Tuxford »

Giovanni wrote:Is that a customized driver? I don't see those fields in I2CConfig of the I2Cv1 driver.

No. Driver for eeprom is already implemented for other board. I see this declarion is in the file /os/hal/lib/fallback/I2C/hal_i2c_lld.h

Giovanni wrote:In general you need to put pins on alternate and open drain mode.

I used different modes. In my case i tried A-F modes. It doesn't help.
Tuxford
Posts: 26
Joined: Fri Nov 06, 2020 3:28 pm
Has thanked: 4 times
Been thanked: 4 times

Re: I2C on OLIMEX-STM32-LCD (STM32F103ZE)

Post by Tuxford »

I found there is fallback driver. Is it good? I switched to I2Cv1.

And changed config.

Code: Select all

static const I2CConfig i2ccfg = {
        OPMODE_I2C,
        100000,
        STD_DUTY_CYCLE};


It doesn't work. i2cMasterTransmitTimeout returns -2 (reset) and error and 4.

Code: Select all

I2CD1   I2CDriver   {...}   
   state   i2cstate_t   I2C_READY   
   config   const I2CConfig *   0x801b120 <i2ccfg>   
   errors   i2cflags_t   4   
   mutex   mutex_t   {...}   
   thread   thread_reference_t   0x0   
   addr   i2caddr_t   160   
   rxdmamode   uint32_t   12426   
   txdmamode   uint32_t   12442   
   dmarx   const stm32_dma_stream_t *   0x801b014 <_stm32_dma_streams+96>   
   dmatx   const stm32_dma_stream_t *   0x801b004 <_stm32_dma_streams+80>   
   i2c   I2C_TypeDef *   0x40005400   


Protocol analyzer doesn't catch anything.
Are somewhere I2C examples for Chibios?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: I2C on OLIMEX-STM32-LCD (STM32F103ZE)

Post by Giovanni »

Hi,

There is an examplle for STM32F1 in /testhal/STM32/STM32F1xx/I2C

Giovanni
Tuxford
Posts: 26
Joined: Fri Nov 06, 2020 3:28 pm
Has thanked: 4 times
Been thanked: 4 times

Re: I2C on OLIMEX-STM32-LCD (STM32F103ZE)

Post by Tuxford »

Giovanni,
If STM32_I2C_USE_DMA is FALSE there are many errors in /os/hal/ports/STM32/LLD/I2Cv1/hal_i2c_lld.c due to dma buffer. Bug?
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: I2C on OLIMEX-STM32-LCD (STM32F103ZE)

Post by steved »

The "standard" I2C driver (NOT the "fallback" driver) works for me on PB8/9, using the same configuration data as you. (I am using a driver which also supports I2C slave, but the master part is basically the same).
You definitely need to configure the ports as open drain outputs as Giovanni suggested, and set up the alternate functions correctly.
Make sure that you have not activated SDA1 and SCL1 on more than one pin each.

My configuration pre-dates using the XML configuration file; from board.h I have:

Code: Select all

#define GPIOB_I2C1_SCL              8U
#define GPIOB_I2C1_SDA              9U

#define VAL_GPIOB_MODER    :
                                     PIN_MODE_ALTERNATE(GPIOB_I2C1_SCL) |   \
                                     PIN_MODE_ALTERNATE(GPIOB_I2C1_SDA) |   \

#define VAL_GPIOB_OTYPER  :
                                     PIN_OTYPE_OPENDRAIN(GPIOB_I2C1_SCL) |  \
                                     PIN_OTYPE_OPENDRAIN(GPIOB_I2C1_SDA) |  \

#define VAL_GPIOB_OSPEEDR :
                                     PIN_OSPEED_100M(GPIOB_I2C1_SCL) |      \
                                     PIN_OSPEED_100M(GPIOB_I2C1_SDA) |      \

#define VAL_GPIOB_PUPDR :
                                     PIN_PUPDR_FLOATING(GPIOB_I2C1_SCL) |   \
                                     PIN_PUPDR_FLOATING(GPIOB_I2C1_SDA) |   \

#define VAL_GPIOB_ODR :
                                     PIN_ODR_HIGH(GPIOB_I2C1_SCL) |         \
                                     PIN_ODR_HIGH(GPIOB_I2C1_SDA) |         \

#define VAL_GPIOB_AFRH              (PIN_AFIO_AF(GPIOB_I2C1_SCL, 4) |       \
                                     PIN_AFIO_AF(GPIOB_I2C1_SDA, 4) |       \

Not sure whether that driver works without DMA; pretty certain I had it enabled.

Code: Select all

#define STM32_I2C_I2C1_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 0)
#define STM32_I2C_I2C1_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 6)
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: I2C on OLIMEX-STM32-LCD (STM32F103ZE)

Post by Giovanni »

Tuxford wrote:Giovanni,
If STM32_I2C_USE_DMA is FALSE there are many errors in /os/hal/ports/STM32/LLD/I2Cv1/hal_i2c_lld.c due to dma buffer. Bug?


It is possible, it is a contributed feature that I have not enabled for a while.

Giovanni
Tuxford
Posts: 26
Joined: Fri Nov 06, 2020 3:28 pm
Has thanked: 4 times
Been thanked: 4 times

Re: I2C on OLIMEX-STM32-LCD (STM32F103ZE)

Post by Tuxford »

steved wrote:The "standard" I2C driver (NOT the "fallback" driver) works for me on PB8/9, using the same configuration data as you. (I am using a driver which also supports I2C slave, but the master part is basically the same).
You definitely need to configure the ports as open drain outputs as Giovanni suggested, and set up the alternate functions correctly.
Make sure that you have not activated SDA1 and SCL1 on more than one pin each.

My configuration pre-dates using the XML configuration file; from board.h I have:

Code: Select all

#define GPIOB_I2C1_SCL              8U
#define GPIOB_I2C1_SDA              9U

#define VAL_GPIOB_MODER    :
                                     PIN_MODE_ALTERNATE(GPIOB_I2C1_SCL) |   \
                                     PIN_MODE_ALTERNATE(GPIOB_I2C1_SDA) |   \

#define VAL_GPIOB_OTYPER  :
                                     PIN_OTYPE_OPENDRAIN(GPIOB_I2C1_SCL) |  \
                                     PIN_OTYPE_OPENDRAIN(GPIOB_I2C1_SDA) |  \

#define VAL_GPIOB_OSPEEDR :
                                     PIN_OSPEED_100M(GPIOB_I2C1_SCL) |      \
                                     PIN_OSPEED_100M(GPIOB_I2C1_SDA) |      \

#define VAL_GPIOB_PUPDR :
                                     PIN_PUPDR_FLOATING(GPIOB_I2C1_SCL) |   \
                                     PIN_PUPDR_FLOATING(GPIOB_I2C1_SDA) |   \

#define VAL_GPIOB_ODR :
                                     PIN_ODR_HIGH(GPIOB_I2C1_SCL) |         \
                                     PIN_ODR_HIGH(GPIOB_I2C1_SDA) |         \

#define VAL_GPIOB_AFRH              (PIN_AFIO_AF(GPIOB_I2C1_SCL, 4) |       \
                                     PIN_AFIO_AF(GPIOB_I2C1_SDA, 4) |       \

Not sure whether that driver works without DMA; pretty certain I had it enabled.

Code: Select all

#define STM32_I2C_I2C1_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 0)
#define STM32_I2C_I2C1_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 6)

Possibly your code for STM32F3xx series or higher.
For STM32F103 it's just

Code: Select all

    palSetPadMode(GPIOB, 8U, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
    palSetPadMode(GPIOB, 9U, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
Tuxford
Posts: 26
Joined: Fri Nov 06, 2020 3:28 pm
Has thanked: 4 times
Been thanked: 4 times

Re: I2C on OLIMEX-STM32-LCD (STM32F103ZE)

Post by Tuxford »

Oh... Problem was in

Code: Select all

AFIO->MAPR |= AFIO_MAPR_I2C1_REMAP;

Last flags has to be set if pins PB8 and PB9 are used.

But problems didn't run out.
What is it?

Code: Select all

 
 struct I2CDriver {
   * @brief   Thread waiting for I/O completion.
   */
  thread_reference_t        thread;

Because in my case thread is null and i2cMasterTransmitTimeout hangs when it calls msg = osalThreadSuspendTimeoutS(&i2cp->thread, timeout);
I don't see any initialization of this member in code.
How to use it?
Post Reply