OLED and ETHERNET problem

128x128 pixel 1.5" full colour OLED display with MicroSD card slot. [Product page]
Post Reply
tcornall
Posts: 3
Joined: Fri Oct 04, 2013 12:53 pm

OLED and ETHERNET problem

Post by tcornall » Fri Oct 04, 2013 1:09 pm

Hi.
I have the freetronics ethermega (which has on board Ethernet using SPI and uses d10 as SS pin) and I purchased the 128x128 OLED display, (which also uses SPI but has its own SS pin, D7, different from the Ethernet one) Great display, BTW.

The OLED works fine by itself, and the Ethernet works fine by itself (using Arduino FTOLED and Ethernet libraries and the examples) but when I use the oled.begin and Ethernet.begin in the same sketch, the Ethernet doesn't work. (The oled continues to work fine, as far as I can see)

I looked thru the library code and as far as I can tell, everything that uses the SS pin on the oled also releases it, and similarly for the Ethernet, so I can't see where the problem might be.

Here's the important bits of my code, with a comment about what happens if I call oled.begin before Ethernet.begin. (It fails too if I do them in the other order...)
Thanks in advance

#include <SPI.h>
#include <FTOLED.h>
#include <Ethernet.h>

const byte pin_cs = 7; //these are for the oled.
const byte pin_dc = 2;
const byte pin_reset = 3;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192,168,0,6);
EthernetServer server(80);
OLED oled(pin_cs, pin_dc, pin_reset);

void setup()
{
Serial.begin(115200);
oled.begin(); //this interferes with the ethernet, which is a puzzle
Ethernet.begin(mac, ip); //this fails if oled.begin has been called previously, which is odd....
server.begin(); //because it works fine if I don't call oled.begin
Serial.print("server is at ");
Serial.println(Ethernet.localIP()); //I get 0.0.0.0 if oled.begin has been called
//and I get 192.168.0.6 if it hasn't, as requested.
.....other setup stuff...
}

Jed Hodson
Posts: 71
Joined: Wed Apr 24, 2013 5:14 am
Location: New South Wales, Australia

Re: OLED and ETHERNET problem

Post by Jed Hodson » Sun Oct 06, 2013 4:40 am

the problem may be due to them sharing the SPI a thread to look at may help viewtopic.php?f=37&t=5467

Jed

tcornall
Posts: 3
Joined: Fri Oct 04, 2013 12:53 pm

Re: OLED and ETHERNET problem

Post by tcornall » Sun Oct 06, 2013 6:51 am

Thanks for the suggestion Jed. I'll look into the code to see if I can see where the clocks get set. Maybe the method suggested in that thread, of switching clock rates depending on what device is being used, might work.

tcornall
Posts: 3
Joined: Fri Oct 04, 2013 12:53 pm

Re: OLED and ETHERNET problem

Post by tcornall » Sun Oct 06, 2013 8:38 am

Well, now this is weird! I think I've solved it....
I looked into OLED::begin and started commenting things out to see what was causing the problem, fully expecting to break the oled functionality. After trying clock divider and msbfirst v lsbfirst, I commented out this SPI_MODE3 setting and the Ethernet started working again. What is surprising is that the oled continues to work fine... So why was the data mode3 being set anyway? Beats me...

The following code is in FTOLED.cpp and the //TC is the line I've commented out.

void OLED::begin() {
SPI.begin();
SPI.setBitOrder(MSBFIRST);
//TC SPI.setDataMode(SPI_MODE3);
#ifdef __AVR__
SPI.setClockDivider(SPI_CLOCK_DIV2); // 8MHz on standard Arduino models
#else
SPI.setClockDivider(5); // 16.8MHz on Due
#endif
....more code

So, for now, this is solved. If anyone can explain why, that'd be nice...

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: OLED and ETHERNET problem

Post by angusgr » Tue Oct 08, 2013 4:49 am

tcornall wrote: I looked into OLED::begin and started commenting things out to see what was causing the problem, fully expecting to break the oled functionality. After trying clock divider and msbfirst v lsbfirst, I commented out this SPI_MODE3 setting and the Ethernet started working again. What is surprising is that the oled continues to work fine... So why was the data mode3 being set anyway? Beats me...
Glad to see you got it sorted.

The different SPI modes are documented a bit on the Arduino reference page and there's a bit more explanation on the SPI page.

The common point between Mode 3 and Mode 0 (the default), is that data is sampled on the rising edge. I remember testing that both of these worked on the display, and there was a minor reason I chose mode 3 (clock idles high), I think based on some small note in the OLED datasheet.

Probably the strangest thing is that the W5100 Ethernet chip used on the EtherMega officially supports either SPI mode 0 or mode 3 as well! I can't explain that. I also tested FTOLED with Ethernet, but I must have done the initialisation in some different way when I ran the test. I'll do some more testing soon, but it seems like changing FTOLED back to SPI mode 0 (default mode) is probably be the right option.

- Angus

Scargill
Posts: 8
Joined: Tue Apr 17, 2012 12:26 pm

Re: OLED and ETHERNET problem

Post by Scargill » Sat Feb 08, 2014 1:10 pm

This solved my problem - and I have another type of display with exactly the same issue I'm hoping to resolve thanks to this SUPERB little piece of information.... worth updating the library permanently??

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: OLED and ETHERNET problem

Post by angusgr » Sun Feb 09, 2014 11:18 pm

Hi scargill,

Thanks for reminding me about this one. I checked the OLED controller datasheet again and the timing diagrams do imply that the clock should idle high (ie mode 3), but I did some more tests and it really does seem to work fine in mode 0. Seems the controller really only really cares about the rising edge.

Library updated!

- Angus

Post Reply