I²C HardWire operation freezing Serial (and probably the board)

Naguissa
Mon May 04, 2015 8:26 pm
Hello,

I’m porting an RTC over I²C mini library from Arduino nano to Maple mini: https://github.com/Naguissa/RTCLib

I’m trying to use HardWare I²C ports and added a private variable handling correct Wire option:

#ifdef _VARIANT_ARDUINO_STM32_
HardWire RTCLIB_WIRE = HardWire(2);
#else
Wire RTCLIB_WIRE = Wire;
#endif


madias
Mon May 04, 2015 9:09 pm
Hm, ok, this are my I2c HW code lines from my adapted SSD1306 library, maybe you missed something….
*.h:
#define WIRE_WRITE HWIRE.write

madias
Mon May 04, 2015 9:28 pm
Ok, I tried something out for you (on my nucleo board):
Setting up my I2c OLED on I2c2
Setting up Serial (is HW on nucleo), Serial1 and Serial2
looping:
draw some things on OLED
serial.print on all three ports.

code is running and running….
Results for maple mini:
Using I2c1 (because I2c2 shares USART3)
Everything works as expected, so HW wire and serial(1-3) together is working for me


Naguissa
Tue May 05, 2015 3:21 am
Maybe pin operation on declaration, as explained here: viewtopic.php?f=15&t=52&p=222

I’ll check later.


RogerClark
Tue May 05, 2015 3:30 am
If the issue is use of GPIO in the constructor, we are looking at a change that may fix this, by declaring the PIN_MAP in __FLASH__ and const, so that the compiler will re-populate the array and it should then be available to all functions eg. pinMode etc

However we need to do some testing first to see if this change has any unintended side effects


RogerClark
Thu May 07, 2015 9:05 pm
This is hopefully fixed in the PIM_MAP-in-FLASH branch of the repo, which should me merged into the master after its had enough community testing

See the announcement section


Naguissa
Thu May 07, 2015 9:51 pm
I’m still investigating inside original maple headers. It seems to fail on i2c reads, bet writes doesn’t hangs (to check if it works). Maybe 400KHz i2c is not supported by my rtc (400KHz is standard on HardWire). Also I’m bad-programing Wire variable, I have to change it to a pointer, pointing Wire on Avr or new HardWire(1 or 2) in STM32.

RogerClark
Thu May 07, 2015 9:53 pm
Have you tried the branch I pushed yesterday, with the change to PIN MAP?

BTW the new code also doesn’t flash the LED in an ASSERT
However the number of places that use ASSERT is very small, so it seems unlikely that the code has asserted

If it is an assert try connecting a USB to serial adapter to hardware serial, it may be writing out an error message


Naguissa
Fri May 08, 2015 5:07 pm
Code reviewed –> No luck
Serial –> Nothing shown. It’s RX & TX between USB and SPI2 pins, right?

Naguissa
Fri May 08, 2015 5:30 pm
Just following the code I’ve found the error is here:

FILE: hardware/Arduino_STM32/STM32F1/cores/maple/libmaple/i2c.c
FUNCTION: int32 i2c_master_xfer
LINE: ASSERT(dev->state == I2C_STATE_IDLE);
LINE #: 228

Seems I have latest version as mini’s led is not flashing.


Naguissa
Fri May 08, 2015 7:42 pm
I created a branch and added current changes to see if you can help me: https://github.com/Naguissa/RTCLib/tree/STM32-dev

As test I’m using this:

#include "Arduino.h"

#ifdef _VARIANT_ARDUINO_STM32_
#include "HardWire.h"
#else
#include "Wire.h"
#endif

#include "RTCLib.h"

RTCLib rtc;

unsigned int pos;

void setup() {
delay (2000);
Serial.begin(9600);
Serial.println("Serial OK");
// Max position: 32767

for(pos = 0; pos < 100; pos++) {
rtc.eeprom_write(pos, (unsigned char) pos % 256);
}

// Only used once, then disabled
rtc.set(0, 42, 16, 6, 2, 5, 15);
// RTCLib::set(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)

pos = 0;

#ifdef _VARIANT_ARDUINO_STM32_
Serial.println("Board: Maple");
#else
Serial.println("Board: Other");
#endif

}

void loop() {
Serial.println();
Serial.println("Loop!");
rtc.refresh();
Serial.print("RTC DateTime: ");
Serial.print(rtc.year());
Serial.print('/');
Serial.print(rtc.month());
Serial.print('/');
Serial.print(rtc.day());
Serial.print(' ');
Serial.print(rtc.hour());
Serial.print(':');
Serial.print(rtc.minute());
Serial.print(':');
Serial.print(rtc.second());
Serial.print(" DOW: ");
Serial.print(rtc.dayOfWeek());
Serial.print(" ---- ");
Serial.print(pos);
Serial.print(": ");
Serial.print(rtc.eeprom_read(pos));

Serial.println();
pos++;
pos %= 1000;
delay(99);
}


RogerClark
Fri May 08, 2015 10:10 pm
I’m it sure why you are using hardware instead of wire for i2c

Especially for a simple device like a Rtc

Did you try the i2c scanner sketch from the Arduino site, it normally works and shows what devices are connected.

Can you try that first and let me know if it detects your clock


Naguissa
Sat May 09, 2015 10:47 am
I’ve tested I2C scanner and works well.

After that, I modified it to use “Wire” or “HardWire” depending on a define (as my library, using a pointer). Wire works OK but HardWire hangs Maple.

As I understand, Maple’s Wire was software Wire, but I also thought that it was using other pins -19 and 20- (I tested ‘as is’, without changing connections, and it worked).

I’ll change to use Wire and that’s all….


Naguissa
Sat May 09, 2015 11:09 am
Thanks to all.

Finally I’ve used standard Wire, that works on I2C1.

It’s tested and merged to master: https://github.com/Naguissa/RTCLib

Now, let’s continue with Wifi, SDCard, Soil sensor… some with common libs, other with self-made ones. I had wifi and sol sensor working in my nano (not in libraries) just before my hard disk died…


RogerClark
Sat May 09, 2015 10:59 pm
Can I repackage the repo and add it to the libraries section ?

Do you have an example code we could include in an examples folder

Thanks


Naguissa
Sun May 10, 2015 10:38 am
No problem, it’s public.

About example, it’s basically the one posted before:

#include "Arduino.h"
#include "Wire.h"
#include "RTCLib.h"

RTCLib rtc;

unsigned int pos;

void setup() {
delay (2000);
Serial.begin(9600);
Serial.println("Serial OK");
// Max position: 32767

for(pos = 0; pos < 1000; pos++) {
rtc.eeprom_write(pos, (unsigned char) pos % 256);
}

// Only used once, then disabled
// rtc.set(0, 42, 16, 6, 2, 5, 15);
// RTCLib::set(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)

pos = 0;

#ifdef _VARIANT_ARDUINO_STM32_
Serial.println("Board: Maple");
#else
Serial.println("Board: Other");
#endif

}

void loop() {
rtc.refresh();
Serial.print("RTC DateTime: ");
Serial.print(rtc.year());
Serial.print('/');
Serial.print(rtc.month());
Serial.print('/');
Serial.print(rtc.day());
Serial.print(' ');
Serial.print(rtc.hour());
Serial.print(':');
Serial.print(rtc.minute());
Serial.print(':');
Serial.print(rtc.second());
Serial.print(" DOW: ");
Serial.print(rtc.dayOfWeek());
Serial.print(" ---- ");
Serial.print(pos);
Serial.print(": ");
Serial.print(rtc.eeprom_read(pos));
Serial.println();
pos++;
pos %= 1000;
delay(100);
}


RogerClark
Sun May 10, 2015 10:48 am
Thanks

I was just going to make it into the new library structure, where the cpp and h are in a sub folder and there is a descrition file etc

Otheriwse the IDE starts to give warning messages for some people (especially linux)

Thanks

Roger


Naguissa
Mon May 11, 2015 4:58 am
I have repackaged the library with new library format and added example program. In repo (master) and new v2.0.1 release.

RogerClark
Mon May 11, 2015 5:12 am
Thanks

I will see if I can add it as a git submodule

Thanks


RogerClark
Mon May 11, 2015 10:37 am
Thanks

Its been added as a sub module i.e the repo references your repo for the files.


Leave a Reply

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