[SOLVED] I2C2 on STM32F103C8T6 and HWire

Generic boards that are not Maple or Maple mini clones, and don't contain the additional USB reset hardware
luca_stm32
Posts: 18
Joined: Wed Apr 26, 2017 12:39 pm

Re: I2C2 on STM32F103C8T6 and HWire

Post by luca_stm32 » Wed Jun 21, 2017 8:07 am

Hi everybody.
With a friend of mine we found the problem.
It is located if boards.h (under the directory STM32F1\vaiants\generic_stm32f103c\board).

The difference respect board.h of Maple Mini is the line where is defined pin that control USB disconnection via external transistor.

Code: Select all

#define BOARD_USB_DISC_DEV        GPIOB
#define BOARD_USB_DISC_BIT        10
If I understood well the code, this means that PB10 is used to control external transistor, but PB10 is used also by I2C2 :(
Changing the BOARD_USB_DISC_BIT to 9 (like Maple Mini), the code works :D

I tried to comment this two defines, but they are used by USB initialization.

Code: Select all

usb_cdcacm_enable(BOARD_USB_DISC_DEV, BOARD_USB_DISC_BIT);
....
usb_cdcacm_disable(BOARD_USB_DISC_DEV, BOARD_USB_DISC_BIT);
Roger, can you keep this type of initialization only for Maple and delete the line for those who do not use the external transistor? Maybe with a #ifdef..

Luca

User avatar
Pito
Posts: 1593
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: I2C2 on STM32F103C8T6 and HWire

Post by Pito » Wed Jun 21, 2017 8:12 am

The usb d+ pullup transistor is used during the init in bootloader, isn't it?
The i2c init should set it as "open_drain" then..
Pukao Hats Cleaning Services Ltd.

luca_stm32
Posts: 18
Joined: Wed Apr 26, 2017 12:39 pm

Re: I2C2 on STM32F103C8T6 and HWire

Post by luca_stm32 » Wed Jun 21, 2017 8:19 am

I found also this tread about I2C initialization:

https://community.st.com/thread/24199

In this tread, I found the following sentence:
I solved this issue by changing the initialization order from that shown in your code. First initialize the I2C port pins as I2C function pins( open collector) and enable their RCC module, then start the I2C RCC module running.
So I think it could be better to modify also i2c_master_enable function in i2c.c in this way:

Code: Select all

void i2c_master_enable(i2c_dev *dev, uint32 flags) {
    /* PE must be disabled to configure the device */
    ASSERT(!(dev->regs->CR1 & I2C_CR1_PE));

    /* Ugh */
    _i2c_handle_remap(dev, flags);
    
     i2c_config_gpios(dev); 			// first, initialize gpio. Or call it before _i2c_handle_remap(dev, flags);?

    /* Reset the bus. Clock out any hung slaves. */
    if (flags & I2C_BUS_RESET) {
        i2c_bus_reset(dev);
    }

    /* Turn on clock and set GPIO modes */
    i2c_init(dev);
@Roger: What do think about?

luca_stm32
Posts: 18
Joined: Wed Apr 26, 2017 12:39 pm

Re: I2C2 on STM32F103C8T6 and HWire

Post by luca_stm32 » Wed Jun 21, 2017 9:21 am

I think my last post is not correct.
Infact the function i2c_master_enable calls i2c_bus_reset that calls i2c_master_release_bus. In this last function, gpio configuration is performed.

So it seems that gpio configuration is made before i2c is enabled.

luca_stm32
Posts: 18
Joined: Wed Apr 26, 2017 12:39 pm

Re: I2C2 on STM32F103C8T6 and HWire

Post by luca_stm32 » Wed Jun 21, 2017 9:30 am

@Pito. Yes, I think your are right.
I noticed this event. With the original repository files, selecting generic_stm32, printing the I2C2 registers, I saw that after I2C2initialization SR2 was 2 (I2C busy). Selecting MM (that use PB9), after I2C2 initialization SR2 was 0 (not busy).
Maybe that when USB drive external transistor on PB10, it generate a condition to I2C pheripheral.

So resetting I2C pheripheral could be sufficient to make it work. But more in general, on BP, you could use PB10 as input. In this case, USB initialization set PB10 as output and drive it high and low. This can potentially damage the external circuit of BP connected to PB10.

Luca.

User avatar
Pito
Posts: 1593
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: I2C2 on STM32F103C8T6 and HWire

Post by Pito » Wed Jun 21, 2017 9:51 am

The I2C init comes after the BP plays with USB transistor (in the bootloader or in the serialUSB). So my current understanding is the I2C init (with HWire) is using the P10 "as-is" - that means when set in the bootloader or serialUSB as "output" it will mess the I2C.
With sw wire I2C init() sets the P10 as an "open-drain" and it works.
Pukao Hats Cleaning Services Ltd.

User avatar
RogerClark
Posts: 7165
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: I2C2 on STM32F103C8T6 and HWire

Post by RogerClark » Wed Jun 21, 2017 10:08 am

Only the MM should mess with the pin it uses to enumerate the USB.

If the generic boards are doing this, its a bug, and the MM code needs to be #if def'ed out

User avatar
RogerClark
Posts: 7165
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: I2C2 on STM32F103C8T6 and HWire

Post by RogerClark » Sun Jun 25, 2017 3:26 am

Fixed for the BP and a few other boards in this commit

https://github.com/rogerclarkmelbourne/ ... a2f6c76051

luca_stm32
Posts: 18
Joined: Wed Apr 26, 2017 12:39 pm

Re: Solved: I2C2 on STM32F103C8T6 and HWire

Post by luca_stm32 » Mon Jun 26, 2017 7:35 pm

Hi Roger.
Today I tested your last repository: it works! :-)

I made also a comparison between sofware implementaion and hardware implementation of I2C. The difference is 880 bytes (software implementaion takes less memory than hardware implementation).

Thanks again!
Congratulations (all of you) again for your good work.

Luca.

gc9n
Posts: 5
Joined: Wed Jan 04, 2017 1:09 pm

Re: Solved: I2C2 on STM32F103C8T6 and HWire

Post by gc9n » Sat Aug 05, 2017 10:27 am

i am trying to write and read into an EEPROM(ATMLH712-02CM) also . but in vain , the code cant read or write from EEPROM . its not hanging (stacks) it works and executes the commands but with no result
i checked to see if the address of eeprom that i m giving is correct with an I2C scanner and i get 0x50 . So its there and suposed that it works
my EEPROM is connected into HardWire HWire(2, I2C_FAST_MODE);

ATMLH712-02CM
Port Label
PCB-GND 1 A0 0
PCB-GND 2 A1 0
PCB-GND 3 A2 0
PCB-GND 4 GND
MCU – 22(PB11) 5 SDA RES-1K to VCC
MCU – 21(PB10) 6 SCL RES-1K to VCC
PCB-GND 7 WP Read/Write
PCB-1 8 VCC

My libraries are the latest . with the latest wire.h enhancement of hardwire, but also with RogerClark bug fixes
I tried both SoftWire and HArdWire

i m using st-link to upload the code into the STM32F103C8T6

Post Reply