‎2020-11-09 08:40 AM
Hello,
I cannot use the I2C bus pins to do I2C. That is, I can't use I2C1 or I2C2 or any of the alternate pins. I need to use PB5 as SDA and PB4 as SCK. I haven't found a way to do this in cubeMX, so I am wondering if I can bit-bang I2C on those pins. I found the following library for STM32 F4: https://github.com/tobajer/i2cbitbang
But did not find much for STM32F103. Are there any libraries or examples showing how to bit-bang I2C on the STM32F103. Can the library I mentioned be easily tweaked to work on the STM32F103.
Regards,
Mawaba.
‎2020-11-09 09:37 AM
Hello @MDao.1​
Referring to the STM32F103x8 datasheet DocID13587 Rev 17 and check the specific alternate function assignments for each pin are detailed in the Table 5 .Medium-density STM32F103xx pin definitions. For example you can use PB8 as SCL and PB9 as SDA.
Bouraoui
‎2020-11-09 09:56 AM
Please have a look at AN4655, which describes the steps to do I2C by SW.
When your question is answered, please close this topic by choosing Select as Best.
Good luck!
/Peter
P.S. and please close the other topic too when finished. :grinning_face:
‎2020-11-09 12:19 PM
@Peter BENSCH​
Thanks! what is I2C_DR in that document (page 16) ? and how are I2C_SDA_PIN and I2C_SCL_PIN defined? As in what GPIO pins are used?
Regards,
Mawaba.
‎2020-11-09 11:06 PM
Bit-banging is low level programming, so you have to be somewhat experienced using it. The library you mentioned could be customized to suit your needs and other STM32s if you either use it as a CPP (and prepare your environment to use it) or convert to ANSI C.
The product web site of the STM32F103CB can be found here, where you will also find the reference manual RM0008 describing all family-related things, e.g. I2C_DR - the I2C Data Register.
The aforementioned AN4655 describes in section 2.4.3 (p. 12) also some aspects for I2C in general, as well as for the GPIO, which should function as SDA and SCL:
The output stages of the bidirectional SDA and SCL signals should have an open-drain or open-collector to perform the wired-AND function. The standard allows to connect sources with different supply levels.
Please remember what the first paragraph of this section says: SW emulation of I2C channel is the most complex one as wide range of aspects has to be considered.
BTW: I2C_DR is defined in the CMSIS header file of the specific device. For for the STM32F103CB you will find it in <repository>/STM32Cube_FW_F1_Vxyz/Drivers\CMSIS\Device\ST\STM32F1xx\Include\stm32f103xb.h
Regards
/Peter
P.S. AN4655 also mentions AN4457 (STSW-STM32156), but this relates to an implementation of USART using bit-banging.
‎2020-12-02 09:44 AM
Thanks for the resources. I am still working on it, but I wrote a C library to bitbang I2C for STM32's. Hopefully, it's helpful to someone in the future. https://github.com/PascalPolygon/stm32_bitbang_i2c
Regards,
Mawaba.
‎2020-12-14 03:20 PM
I am very new to the STM32 but this is one of the things I have working. I ported code that I've carried around from 8031 and 68HC11 days. It is capable of Standard Speed and Fast Mode, although there are a few magic numbers involved so far (I would prefer to calculate my bit timing from the RCC rather than just a number). Otherwise it works quite well.
My desire it to make this code "look" like a new I2C peripheral (call it I2C0 or something), but I don't understand to do it yet. This is my present hangup. Any pointers here would be great, then it would be shareable.
‎2020-12-15 08:10 AM
Hello @Grant Bt​
Are you referring to the repo I shared in my previous post?
‎2020-12-15 08:26 AM
No I did one myself. I think the one you posted ( https://github.com/tobajer/i2cbitbang ) is very interesting though. It reminds me of the one from AN4655. I wish I had seen those, but I wanted to learn GPIO and had done bit-bang I2C before, so it was (is) a learning experience. I need to work on it more, but it's functional on the F103.