Getting I2c on Nucleo F103-RB to work

STM32F103 Nucleo boards e.g. STM Nucleo F103RB
madias
Posts: 812
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Getting I2c on Nucleo F103-RB to work

Postby madias » Wed Apr 29, 2015 7:32 am

Hello,
I*ve splitted up follwing thread: viewtopic.php?f=8&t=19&start=10
last post by me:
Getting I2c working should not be a big problem.
Maybe this is new to you:
There are two different versions of I2c in the library/wire folder:
"Hardwire.h" this is the hardware version of I2c, I got it successfully running on the maple.
"Wire.h" is only a ugly software implementation (with a lot of digitalWrites and /Reads) it's set as "standard". Only advantage: You can set every pin you want. I didn't play with it and I'm not going to use it. As in wire.h there are following defaults:
#define SDA 19
#define SCL 20

#define SOFT_STANDARD 16
#define SOFT_FAST 0
(don't know what the last two entries are... maybe you only need to change SDA and SCL or just try them out)
I'll try to get my I2C OLED working with "hardwire.h" this week.

IOTrav
Posts: 11
Joined: Mon Apr 27, 2015 6:26 pm

Re: Getting I2c on Nucleo F103-RB to work

Postby IOTrav » Fri May 01, 2015 4:22 pm

Hi @madias,

Sorry, I was out for a couple of days and have been playing catch up. I will be working to get I2C working on the Nucleo today.

I did see the #defines for SCL and SDA were set to 19 and 20 in Arduino_STM32-master/STM32F1/libraries/Wire/Wire.h

What do 19 and 20 refer to exactly? Looking at board.cpp I see reference to 19 and 20 under boardPWMPins[] and boardADCPins[] I guess all this alternate labeling for the pins is making me confused. I know on the board the SCL and SDA lines are labeled:
SCL: D15/PB8
SDA: D14/PB9

In the board.cpp I see a comment down towards the bottom that says this:
Bit 1 I2C1_REMAP: I2C1 remapping
This bit is set and cleared by software. It controls the mapping of I2C1 SCL and SDA
alternate functions on the GPIO ports.
0: No remap (SCL/PB6, SDA/PB7)
1: Remap (SCL/PB8, SDA/PB9)

So if the pins are not remaped then SCL will be connected to PB6 and SDA will be connected to PB7? How can I tell if they are remapped or not? I see this remapping is set by bit one in a byte but what byte is this bit set in? Should I just use PB6 and PB7 and see if it works?

Sorry for being all over the place :)

IOTrav
Posts: 11
Joined: Mon Apr 27, 2015 6:26 pm

Re: Getting I2c on Nucleo F103-RB to work

Postby IOTrav » Fri May 01, 2015 4:40 pm

Hey @all,

Great news! I2C tested and working with my MCP23007 chip and Nucleo . I did have to connect the I2C lines like this:

SCL from MCP23007: PB6 Nucleo
SDA from MCP23007: PB7 Nucleo

Remapping would be good though so I can actually connect to the points on the Nucleo labeled SDA(PB9) and SCL(PB8). I know this is done by setting Bit 1 in the byte to on but what byte? It is referenced in the board.cpp file.

madias
Posts: 812
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: Getting I2c on Nucleo F103-RB to work

Postby madias » Fri May 01, 2015 5:09 pm

do you use the wire.h library? then it's only Software and you can use every pin you want, defined in the wire.h (?) for using HW you must use the hardwire.h and the Pins are hard wired you cannot change them. look at the stm32f103rb data sheet for more information. Never eher change the board. CPP file unless you know exactly what you are doing. for HW i2c I need time, there are many factory to observe. there are also Solder jumpers on nucleo to mind

IOTrav
Posts: 11
Joined: Mon Apr 27, 2015 6:26 pm

Re: Getting I2c on Nucleo F103-RB to work

Postby IOTrav » Fri May 01, 2015 8:18 pm

Hi @madias,

Yes, I am using Wire.h library. Here is the sample code working with MCP23007(also used to test serial coms and LED pin):

#include <Wire.h>

int ledPin = PA5;
void setup() {
//Setup GPIO for LED control
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
Wire.begin();
Wire.beginTransmission(0x20);
Wire.write(0);
Wire.write(0);
Wire.write(0);
Wire.endTransmission();

}

void loop() {
digitalWrite(ledPin, HIGH);
Serial.println("Serial print");
Serial1.println("Serial1 print");
Serial2.println("Serial2 print");
Wire.beginTransmission(0x20);
Wire.write(18);
Wire.write(255);
Wire.write(255);
Wire.endTransmission();
delay(1000);
Wire.beginTransmission(0x20);
Wire.write(18);
Wire.write(0);
Wire.write(0);
Wire.endTransmission();
delay(1000);
}

Example just turns all 16 channels on and off every second. Also prints to all three serial ports and turns the on board user LED on and off. Everything is working perfectly.

madias
Posts: 812
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: Getting I2c on Nucleo F103-RB to work

Postby madias » Sun May 03, 2015 6:07 am

Some update:
I've tested software I2c on any pin and HW I2c on I2c Port2 (port 1 not tested yet, should be working) successfully. Now I'm thinking how we manage a common solution for the board (There must be defines in the I2c library and serial-library) In consultation with Roger)

madias
Posts: 812
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: Getting I2c on Nucleo F103-RB to work

Postby madias » Sun May 03, 2015 9:50 pm

Ok, nucleo passed my testing phase and it's now 100% working:
SPI (both) working.
I2c (both) & software I2c working.
USART (all 3 channels) working.
But I cannot get those ugly "arduino pin headers" 100% compatible (caused by mad hardware wiring of STM, so it's not possible to use every ADC pins, I2c standard mapping (except software mode) and different SPI mappings), but for "basic shields" (digital in/out) it should be ok (whoever needs...)
Even there is an option to get "Serial" as standard connection with the ST-Link and some more intelligent pin mappings, but this involves core files of STM32-arduino, so this would be only in a repo with arrangement with roger, further details: viewtopic.php?f=17&t=64


Return to “STM Nucleo boards”

Who is online

Users browsing this forum: No registered users and 1 guest