I2C on the STM32F103C8T6

Generic boards that are not Maple or Maple mini clones, and don't contain the additional USB reset hardware
Fingolin
Posts: 15
Joined: Wed May 17, 2017 1:28 pm

I2C on the STM32F103C8T6

Post by 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
Posts: 1449
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: I2C on the STM32F103C8T6

Post by 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
Posts: 15
Joined: Wed May 17, 2017 1:28 pm

Re: I2C on the STM32F103C8T6

Post by 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
Posts: 15
Joined: Wed May 17, 2017 1:28 pm

Re: I2C on the STM32F103C8T6

Post by 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
Posts: 1449
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: I2C on the STM32F103C8T6

Post by zmemw16 » Sun May 21, 2017 3:01 pm

Code: Select all

~/sketchbook/hardware/Arduino_STM32/STM32F1/libraries/Wire
:twisted:
not something that normally needs editing, please clarify why / what you're thinking about doing
stephen

ag123
Posts: 767
Joined: Thu Jul 21, 2016 4:24 pm

Re: I2C on the STM32F103C8T6

Post by 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

User avatar
RogerClark
Posts: 7145
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: I2C on the STM32F103C8T6

Post by 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
Posts: 15
Joined: Wed May 17, 2017 1:28 pm

Re: I2C on the STM32F103C8T6

Post by 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:

Code: Select all

         //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];
     
     
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!

User avatar
RogerClark
Posts: 7145
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: I2C on the STM32F103C8T6

Post by 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
Posts: 15
Joined: Wed May 17, 2017 1:28 pm

Re: I2C on the STM32F103C8T6

Post by 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

Post Reply