I2C on the STM32F103C8T6

Fingolin
Wed May 17, 2017 1:49 pm
Hi,
I am currently working on a quadrocopter project. I started it with and Arduino Board, but since of the higher speed I moved to this board.
I managed to uplaoad sketches and everything seems to work pretty solid so far.
Now I want to import my sketch to this board starting with the IMU (6050) which is controlled by i2c.
I used this https://developer.mbed.org/users/hudakz … 8T6_Hello/ as a reference for the pinouts.
There I got the first problems:
– There are two I2c1 ports? working with the first one (PB_9 and 8 ) was pretty slow.
Why is this? :D and how do I use I2C in generall on this device (say if I want to use 3 ports)
-Can I use I2C the same way I do on an Arduino board or do I have to consider certain things?

Thank you for your support!


zmemw16
Wed May 17, 2017 2:25 pm
this is Arduino_STM32
helpful information on wiki http://wiki.stm32duino.com/index.php?title=Main_Page
mbed != Arduino_STM32; more correctly imho mbed <<<<< Arduino_STM32
speed, no idea other than maybe mbed
btw
pb6 pb7 are i2c1.
pb8 pb9 are i2c2.
srp

Fingolin
Wed May 17, 2017 2:39 pm
Thank you for your quick answer and the clarification, I was not aware of that :D
I will click thorugh the wiki and propably come back with a few more questions :D

Fingolin
Sun May 21, 2017 2:48 pm
This may be a really stupid question, but can anyone tell me where the Wire.h for the stm32 is located?

zmemw16
Sun May 21, 2017 3:01 pm
~/sketchbook/hardware/Arduino_STM32/STM32F1/libraries/Wire

ag123
Sun May 21, 2017 3:33 pm
not familar with mbed, but i think mbed is simply another ‘arduino like’ implementation just that arm drives it and does it probably somewhat differently
it seemed mbed bundles a keil rtos
https://github.com/ARMmbed/mbed-os
taking a look at the api codes, i think it probably may not be ‘arduino compatible’
i’m not too sure if it is possible to simply build mbed using the arm-gcc toolchain. apparently mbed readme specifically states
Getting Started for Developers
You need mbed CLI to build mbed OS. For more details, read the mbed OS Handbook.

there are some specific mentions of an ‘mbed firmware’
https://developer.mbed.org/cookbook/ecl … -debugging
i’m not too sure if that means certain things would only be distributed as binary blobs on the devices


RogerClark
Sun May 21, 2017 10:33 pm
by default I2C is “software” driven, and speed is at least 100k

Hardware I2C should now be working Ok, and will be faster, but you will need to instantiate an instance of the hardware I2C class to use it.

Search the forum, there are many posts about using hardware I2C

Note. The reason that I have not switched to the core using Hardware I2C by default, is that the speed of software i2C is enough for most people and I think is at least equivalent to the AVR boards.

Software I2C can run on any pin.

Software I2C has been tested and used by loads of people and is stable.

Hardware I2C now works, but is not extensively tested.


Fingolin
Mon May 22, 2017 9:41 am
Thank you for all of your support, it turned out I2C did not work for me since on of my cables was broken :oops: .
I got one more question: is it possible, that conversion from bytes to float/int is different on the stm board?
This is the code I am talking about:
//Reading Raw:
Wire.beginTransmission(this->Adress);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission();
Wire.requestFrom(this->Adress,14); // request a total of 14 registers

this->x_accel=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
this->y_accel=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
this->z_accel=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)

Serial.print(" ax: "); Serial.print(this->x_accel);
Serial.print(" ay: "); Serial.print(this->y_accel);
Serial.print(" az: "); Serial.print(this->z_accel);

this->x_accel=(float)this->x_accel*this->AccelMultipl/32767*this->correction[0] + this->bias[0];
this->y_accel=(float)this->y_accel*this->AccelMultipl/32767*this->correction[1] + this->bias[1];
this->z_accel=(float)this->z_accel*this->AccelMultipl/32767*this->correction[2] + this->bias[2];


RogerClark
Mon May 22, 2017 10:05 am
What data types are you using to store the initial X Y and Z values?

The “int” data type on STM32 is 32 bits, but on AVR its only 16 bits

So your maths that has the value 32768 will not work on the STM32 unless you change the data type of your X Y and Z struct values to be 16 bit ints rather than 32 bit.

try using

int16_t

as the data type in that struct


Fingolin
Mon May 22, 2017 10:21 am
Ah I see!
Edit: found a workaround thank you so much! Woul have taken me ages to find all that :D

Fingolin
Tue May 23, 2017 2:48 pm
Software I2C can run on any pin.
How do I change the pins used by the standard Wire library?

Fingolin
Wed May 24, 2017 9:49 am
Can someone help me? I am not able to get i2c get to work on pins PB4 and PB5 (SCL,SDA).
I tried:
Wire=TwoWire(PB5,PB4);

this->Adress=Adress;
Wire.begin();
Wire.beginTransmission(Adress);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission();

Wire.beginTransmission(Adress);
Wire.write(0x1A); // CONFIG register
byte val=B00000110; //
Wire.write(val); // Set the LowPassFilter
Wire.endTransmission();


stevestrong
Wed May 24, 2017 1:16 pm
Be careful, PB4 can be alternatively reserved by SWD interface when you upload over serial (PA9/10) or STLink or any debug probe.
Only by uploading over STM32duino bootloader, PB4 can be used as GPIO, and thus for I2C, too.
Otherwise you have to manually edit boards.txt and remove “-DCONFIG_MAPLE_MINI_NO_DISABLE_DEBUG=1” from the respective lines.
Search the forum for PB4…

Fingolin
Wed May 24, 2017 1:27 pm
Woa thanks!
I just found this viewtopic.php?f=3&t=2084&p=27945&hilit=PB4#p27945
and the line disableDebugPorts(); resolves my issue!
Damn thanks!!

Leave a Reply

Your email address will not be published. Required fields are marked *