Page 1 of 2

I2C on the STM32F103C8T6

Posted: Wed May 17, 2017 1:49 pm
by Fingolin
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 ... 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!

Re: I2C on the STM32F103C8T6

Posted: Wed May 17, 2017 2:25 pm
by zmemw16
this is Arduino_STM32
helpful information on wiki
mbed != Arduino_STM32; more correctly imho mbed <<<<< Arduino_STM32
speed, no idea other than maybe mbed
pb6 pb7 are i2c1.
pb8 pb9 are i2c2.

Re: I2C on the STM32F103C8T6

Posted: Wed May 17, 2017 2:39 pm
by Fingolin
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

Re: I2C on the STM32F103C8T6

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

Re: I2C on the STM32F103C8T6

Posted: Sun May 21, 2017 3:01 pm
by zmemw16

Code: Select all

not something that normally needs editing, please clarify why / what you're thinking about doing

Re: I2C on the STM32F103C8T6

Posted: Sun May 21, 2017 3:33 pm
by ag123
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
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' ... -debugging
i'm not too sure if that means certain things would only be distributed as binary blobs on the devices

Re: I2C on the STM32F103C8T6

Posted: Sun May 21, 2017 10:33 pm
by RogerClark
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.

Re: I2C on the STM32F103C8T6

Posted: Mon May 22, 2017 9:41 am
by Fingolin
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:

Code: Select all

         //Reading Raw:
          Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
          Wire.requestFrom(this->Adress,14);  // request a total of 14 registers
          this-><<8|;  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
          this-><<8|;  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
          this-><<8|;  // 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];
Here I read the raw Gyro Data from the Gyros registers and then scale it (correction[] is 1,1,1 and Bias 0,0,0).
The "Problem" is that I only get values >0 but on the arduino I got the range from +- 9.81, while on the stm board I get +-19.
Is that different behaviour or am I missing something?
Its not the biggest problem since I could just substract the value but I am curious.

Thanks for all the help so far!

Re: I2C on the STM32F103C8T6

Posted: Mon May 22, 2017 10:05 am
by RogerClark
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


as the data type in that struct

Re: I2C on the STM32F103C8T6

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