STM32F103C6T6 Arduino I2C remapping pins issue ?  - Page 1
Forum section: electronics / general

author: snoopy
author_rank: Frequent Contributor
timestamp: July 03, 2020, 06:47:34 am
content: By default the Arduino environment seems to support the two I2C TWI hardware controllers in this chip on PB6/PB7 and PB10/PB11 respectively. How do I go about remapping the first controller from PB6/PB7 to PB8/PB9 pair ?? I've been pulling my hair out trying to do this in the software but all to no avail. As a work around I may have to physical bridge these pins until I can find a software solutionDoes anyone have a work around ?cheersdavid

author: garethw
author_rank: Regular Contributor
timestamp: July 03, 2020, 08:41:39 am
content: I’ve only ever used the Arduino IDE for Arduino hardware such as the UNO so I don’t have any solutions to hand.However, you might be able to interact directly with the registers to enable the remap. When I get a chance to get to my laptop I will have a bash at a possible solution.

author: newbrain
author_rank: Super Contributor
timestamp: July 03, 2020, 01:50:06 pm
content: 1st Note: I do not do Arduino.2nd Note: Just a nitpick. TWI is the AVR name, the peripheral in the STM32F103 is called, unsurprisingly, I2C.The pin assignment in STM32F103 is controlled in a slightly different way than in other STM32 families, remapping is handled by having a default alternate function on the pins and a remap bit to use an alternative set of pins.I2C1, as you noticed, defaults to PB6 and PB7.In order to remap I2C1 to PB8 (SCL) and PB9 (SDA), you need to set bit 1 (I2C1_REMAP) of AFIO_MAPR (alternate function remap register).PB8 and PB9 pins must be defined Open-drain, alternate function output in GPIOB_CRH, output speed 2MHz (enough for the max 400kHz I2C).Note that AFIO registers needs their clock enabled for the write to succeed. Bit 0 (AFIO_EN) in RCC_APB2ENR must be set to 1 before using them.If there are no native STM32 Arduino library functions to do that, you can always directly write to the registers as described above:Set up the GPIOB pins.Enable the AFIO clock.Remap the I2C.after the I2C has been initialized.In thereference manualsee:Chapter 9 for general discussion of the GPIO and AFIOChapter 9.2.2 and 9.4.2 for GPIO and AFIO register descriptionsChapter 7.3.7 for the clock enable register.

author: profdc9
author_rank: Frequent Contributor
timestamp: July 03, 2020, 02:16:47 pm
content: It depends on which core you are using.I usually use Roger's stm32duino core. There is a function called afio_remap() for this purpose. There is probably something similar in the STM32 HAL.I would check the STM32F1 errata closely. Unfortunately, besides a buggy I2C peripheral in the first place, there are problems with remapping the peripherals. For this reason, you may want to consider avoiding the remapping. One of the main problems is a conflict between the RCC clocks of remapped SPI and I2C.Alternatively, bitbang I2C is sometimes preferred if you don't mind the extra overhead. The benefit is that you can use whatever pins you want and it is very reliable.

author: snoopy
author_rank: Frequent Contributor
timestamp: July 04, 2020, 02:55:33 am
content: Quote from: profdc9 on July 03, 2020, 02:16:47 pmI found a simple way of remapping the pins that is part of the stm32duino library but when compiling says these functions are not in the Wire class ?? What am I not including ??Code:Wire.setSDA(PB9);Wire.setSCL(PB8);Wire.begin();https://github.com/stm32duino/wiki/wiki/API#redefine-i2c-pins

author: ozcar
author_rank: Frequent Contributor
timestamp: July 04, 2020, 06:54:30 am
content: Quote from: snoopy on July 04, 2020, 02:55:33 amWorks for me - at least, it compiles without complaint.Are you really using the STM core?If I turn on verbose in preferences, it tells me:Using library Wire at version 1.0 in folder: C:\Users\user\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.9.0\libraries\WirePresumably, if you call those functions with pins that it is not possible to use, it will fail in some way when you try to run it – whether you will easily work out why, I'm not sure.

author: snoopy
author_rank: Frequent Contributor
timestamp: July 04, 2020, 02:38:27 pm
content: Quote from: ozcar on July 04, 2020, 06:54:30 amI had been using the stm32duino STM32F1xx board support package which doesn't seem to have these extensions to the Wire class. However when I installed the "STM32 Cores" board support package by STMicroelectronics version 1.9.0 and select the BluePill board it now supports these extensions to the Wire class but now I can't upload to my board using a USB to serial converter even after installing STM32CubeProgrammer and setting the correct path !! This is what it is spitting out at me. Perhaps I need to use the STLink adaptor ?Code:Sketch uses 17664 bytes (26%) of program storage space. Maximum is 65536 bytes.Global variables use 1052 bytes (5%) of dynamic memory, leaving 19428 bytes for local variables. Maximum is 20480 bytes.'where' is not recognized as an internal or external command,operable program or batch file.'where' is not recognized as an internal or external command,operable program or batch file.STM32_Programmer_CLI.exe not found.Please install it or add <STM32CubeProgrammer path>\bin' to your PATH environment:https://www.st.com/en/development-tools/stm32cubeprog.htmlAborting!An error occurred while uploading the sketch

author: ozcar
author_rank: Frequent Contributor
timestamp: July 04, 2020, 11:29:55 pm
content: Quote from: snoopy on July 04, 2020, 02:38:27 pmWay back I have used USB bootloader, but you'd have to program that into your device first.These days I always use STLINK (cheap clone ones or the ones on Nucleo boards). However, that also uses STM32CubeProgrammer, so you would still have to sort out the path problem.It seems to use a batch file to do the programming, which for me is in C:\Users\user\AppData\Local\Arduino15\packages\STM32\tools\STM32Tools\1.4.0/tools/win/stm32CubeProg.bat , which starts like this:Code:@echo offset ERROR=0set STM32CP_CLI=STM32_Programmer_CLI.exeset ADDRESS=0x8000000set ERASE=set MODE=set PORT=set OPTS=:: Check toolwhere /Q %STM32CP_CLI%if %errorlevel%==0 goto :param::Check with default pathset STM32CP=%ProgramW6432%\STMicroelectronics\STM32Cube\STM32CubeProgrammer\binset STM32CP86=%ProgramFiles(X86)%\STMicroelectronics\STM32Cube\STM32CubeProgrammer\binset PATH=%PATH%;%STM32CP%;%STM32CP86%where /Q %STM32CP_CLI%if %errorlevel%==0 goto :paramecho %STM32CP_CLI% not found.echo Please install it or add ^<STM32CubeProgrammer path^>\bin' to your PATH environment:echo https://www.st.com/en/development-tools/stm32cubeprog.htmlecho Aborting!Exit 1…So, if the STM32CubeProgrammer is where it would be installed by default, it should work without you having to do anything. I don't have it in the default location, and I can see that I have added it to the path. But for you it could not even find the windows 'where' command, so somehow your path must be completely messed up. Or maybe you only have the STM32CubeProgrammer path there now???

author: profdc9
author_rank: Frequent Contributor
timestamp: July 05, 2020, 12:32:29 am
content: You may have to use the SoftWire class if you want to set SDA and SCL arbitrarily. It is in Roger's stm32duino core, though it may be in the official core too, I don't know. At any rate, it just needs open drain outputs so it can use almost any GPIO pin.

author: snoopy
author_rank: Frequent Contributor
timestamp: July 05, 2020, 03:00:45 am
content: Quote from: ozcar on July 04, 2020, 11:29:55 pmI didn't know what 'where' was until you pointed it out and then I tried to run it from the command line and sure enough it was nowhere to be found. When I added c:\Windows\system32 into the system path the whole thing now workscheersdavid

author: snoopy
author_rank: Frequent Contributor
timestamp: July 05, 2020, 03:02:46 am
content: Quote from: profdc9 on July 05, 2020, 12:32:29 amYes the SoftyWire class did indeed work the first time I tried it. However because the chip has the i2C hardware built in I thought it would be more prudent to use it to free up the CPU for other thingscheersdavid