I2C on B-G431B-ESC1 problem

Hi all!

I am trying to get I2C working on B-G431B-ESC1 but it still remains a struggle. Looking at the datasheets of the chip, pins PB7 (B+/H2) and PB8(Z+/H3) have I2C support. I have a working board setup using platform.io and the files from @Owen_Williams. Directly using these files, when the command Wire.begin(); is started, the board stops functioning (I verified this with a simple blink example). So after doing some research I think that the board files need to be changed.

Following the guide from STM32duino wiki on how to add a new board, I think that I need to change the following in every existing file:

  1. PeripheralPins.C: Unchecked the pins PB7 and PB8
    I2C

  2. PinNamesVar: Not changed

  3. Variant.h: Doubting to add #define HAL_I2C_MODULE_ENABLED here, but looking at the wiki, stm32duino should automatically disable or enable this. So this should not be added.

  4. ldscript.ld: Not changed

  5. Variant.cpp: I think that this is the crucial file. Looking add the guide from STM32duino you need to enable there if some pins use I2C etc. My plan was to only enable the PB7 and PB8 pin for I2C and look add the differences between the files and than add the code for I2C. I configured these pins in STM32Cube:
    STM32CUBE
    Only thing that I am not totally sure is that I can not select the SW4STM32 toolchain here. The only extra code that it generates is the following part: PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1; PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); }
    The weird thing is that this piece of code is non existed on the same file of the nucleo board. So I am not really sure that this part needs to be added.

So in conclusion: Did some people figure out how to do I2C support on this chip? Or are there people who have experience enable certain functions on other chips and have tips for me? Or maybe have some tips in general?

Thanks in advance and greetings,

Wittecactus

Hi @Wittecactus I would try to use these compile options in platformio.ini where PBYYY and PBXXX and i2C capable pins. I did something similar with a generic board based on STM332F405RTG6 for SPI and it worked.

  -D PIN_WIRE_SDA=PBYYY
  -D PIN_WIRE_SCL=PBXXX

I would try defining this. Often when optional modules are not enabled - the code goes into a ‘not implemented’ loop.

Stepping back a little, I’d try to debug this. But that requires you to get debugging working!! I’ve got debugging working in platformio for this board. Assuming you can get debug working - you’ll be able to put a breakpoint on Wire.begin(400000); and step into the Wire implementation.

The peripheral pins functions can be put at the top of your main.cpp. Might be worth trying to do that too.

Hi @JorgeMaker and @Owen_Williams!

Thanks for your replies!

Good news after changing the Variant.cpp file, I finally can run some code! Now when I start Wire.begin(); the simple blink example will work! The only thing is that I do not get any data from the sensor… So that is something to figure out coming week!

I added the compile options but unfortunately no correct sensor data.

I really need to figure debugging out! So that is also a task for met for the coming week!

Thanks for the help and if you have further suggestions on to get correct data working let me know!

Greetings,

Wittecactus

Progress!


The circuitry around those pins is shown in the diagram. Not sure if those series resistors are helping (i really have no idea!) and the 10k pullups are a bit weak for i2c. Do you have external pullups?

Most likely, you already have one of these but if this is not the case, get one on AliExpress / Ebay / Bannagood. They are very cheap, about € 6 and it works with the Saleale software . It can save you many hours and tons of frustration.

Captura de pantalla 2021-02-09 a las 23.20.30

Hi @Owen_Williams and @JorgeMaker,

Thanks for your reply.

I am using external pull-ups.

I do not have a logic analyzer here so I am going to order one to read the signals! Thanks for the suggestion.

I am going to look at a simple way to check that there is some data comming in via the I2C bus or not. I have a feeling that the sensor is working and code is running on the chip, but that the I2C port is still closed. Is there a simple way to check this? Otherwise I still need to get the debugger working because maybe it can reveal some more information.

Greetings,

Wittecactus

Without a debugger, logic analyzer, or oscilloscope, it is very difficult for you to figure out where the error is, you go blindly in midle of the fog trying to guess the source of the problem.

If you have something that you know that surely the I2C works for it, I would try to put it to rule out that what is not working is the encoder, that is, the I2C slave part.

Hi @JorgeMaker,

You are right! I figured out that now Wire.begin() works but when you ask Wire.requestFrom(X, X); then it already goes wrong… So I think I still need to work on chip level. I will keep you up to date!

Greetings,

Wittecactus

1 Like

The “hello world” test for i2c is to scan for devices. It sweeps the range of device ids (0-127) and tells you what’s connected. Have you tried that?

Hi @Owen_Williams,

Great suggestion! This brought me a step further because it gave errors first. After trying different things (I am going to add the steps to the guide once it works fully) it finally gives that a device is connected on address 0x36, which is after checking the datasheet the AS5600! :slight_smile:. Can I conclude now that I2C is working? :face_with_monocle:

Still when running the Simplefoc code, there looks to be no data input. So that remains strange. So I am first going to manually try to read the angle and if that works, I know for sure that it is related to SimpoleFOC or not.

Thank you and greetings,

Wittecactus

Oke that went faster than expected! With the Magnetic_sensor_I2C exmaple correct data is coming in!. I do not have acces to a power supply now so I can’t test full FOC so I will give an update after the weekend!

3 Likes

Hi all!

Good news: I have run the FOC code on the board and it worked on the board in combination with the magnetic sensor.

Bad news: There was a bit more noice so I changed the value of the filter on the board and uploaded the code, afterwards in stopped working. Uploaded the original code and it still did not work.

The problem has something to do that I restarted my computer this weekend. Because the only thing I can upload and get working now is a simple blink example without serial. Also when I revert all the changes that I made back to the variant files of @Owen_Williams, still serial will not work. I also tested it on an other discovery board.

Did someone also experience this issue? Does someone have a suggestion what to do? Maybe reinstall platform.io?

#Update: After pressing the clean button, and sometimes removing certain flags or adding the flags, it is working again. Now with a bad working sensor… but this method is a bit inconsistent (hopefully next time the computer goes off that it is working faster again). Sometimes it is also board dependant. So I think sometimes the board and platformio need to reset some files? Does someone have some suggestions or explanation for this?

Greetings,

Wittecactus

When you have problems, try removing power from board entirely. I’ve seen something similar when i2c gets ‘stuck’. If that fixes it, I can try to dig out some code to clear a stuck i2c bus.
Regarding noise. How often are you calling move()? Probably ‘as often as possible’. You want to call loopFOC as often as possible but I’d try calling move() less, e.g. in the range of 500-2000Hz (no faster). When you try to work out instantaneous speed and are sampling at 10,000+ times a second it gets noisy. Also try moving the Tf on low pass filter from the default of 0.005 to 0.05 (50ms).

Hi @Owen_Williams,

Thanks for your reply.

I have managed to get the board working again. The motor ran perfectly and no noice from the sensor. Again, I changed the variable and uploaded (while not removing power) and the board did not work. After that even if I remove the power from the board it will not work, even with a simple blink code with serial. It looks like the board needs to be resetted or something? But then again when connecting an other board it will also not work.

Even when restarting the pc and somewhat trying to repeat the steps or try other codes that worked the other day, it will not work. It is just really incosistent. It ia weird that sometimes using the exact same code, it works but then it blocks and then it is really difficult to figure out how to unblock it.

Greetings,

Wittecactus

Small update:

Reinstalled visual studio and platform.IO and the basics are working again. Hopefully this remains the case. The sensor is working again. When only testing the sensor, velocity and angle data are correct. After that tested closed loop velocity, then everything is working perfectly. When implemented in angle control closed loop, only angle is working and velocity is stuck at 1/-1.

Update 2: Is working again, I will update the guide.

Update 3: Guide: Beginner guide for B-G431B-ESC1 - Hardware support - SimpleFOC Community