LowPowerLab Forum

Hardware support => Moteino => Topic started by: sketchy on November 18, 2013, 04:43:28 PM

Title: Moteino and SPI
Post by: sketchy on November 18, 2013, 04:43:28 PM
I have been testing JeeLab's RFM12B board wired to an Arduino Uno and an Adafruit cc3000 board.  There is a conflict between the two over the SPI bus (can't get data when both have been initialized then sends/receives data to a node).

I would like to try the Moteino in replacement of the RFM12B board.  However,  I am concerned there will still be SPI conflicts.

What advice do you have about using the Moteino with Adafruit's cc3000 board?

Thank you.
Title: Re: Moteino and SPI
Post by: Felix on November 18, 2013, 05:10:42 PM
What are the SPI requirements of the cc3000?
Title: Re: Moteino and SPI
Post by: sketchy on November 18, 2013, 05:18:25 PM
What requirements do you mean?  I can set the SS/CS to any pin... I can give you more specifics - I apologize.  I just don't know which requirements.  (sorry!)
Title: Re: Moteino and SPI
Post by: Felix on November 18, 2013, 05:35:26 PM
As long as CS is different than D10 you should be all set.
D2 is used by the RFM interrupt.
D8 is also used by FLASH MEM if you get that option.
Otherwise there should not be any issue, you can use any other pin for your CC3000 SPI CS line.

The CC3000 might draw lots of power so you might want to watch that.
Title: Re: Moteino and SPI
Post by: sketchy on November 18, 2013, 06:36:09 PM
Thanks for the response.  My concern is I am finding a conflict between cc3000 and rfm12b - isn't the rfm12b very similar?  So if I'm having a conflict with the rfm12b - wouldn't i see the same thing?
Title: Re: Moteino and SPI
Post by: Felix on November 18, 2013, 06:54:15 PM
What kind of conflict?
Title: Re: Moteino and SPI
Post by: priority on November 20, 2013, 10:44:51 PM
Are you using the Adafruit CC3000 breakout or the shield?  I will hazard a guess that you are using the breakout and have run into a "feature" of the CC3000 that I find extremely annoying.  Seems that TI's engineers forgot to test what happens with more than 1 device on a SPI bus and the CC3000 does not tri-state MISO (data out) from the CC3000 when its CS is low.  The second schematic on http://learn.adafruit.com/adafruit-cc3000-wifi/downloads (http://learn.adafruit.com/adafruit-cc3000-wifi/downloads) shows a work-around to deal with this "feature" using a 74AHC1G125 (which I must admit is a rather cute little chip in its own right).
Title: Re: Moteino and SPI
Post by: Felix on November 21, 2013, 07:48:24 AM
Oh .... could that be it? I've seen that happen on cheap chinese crap SPI LCDs. Not on expensive brand devices. I would just use large value resistors to pull down MISO.
Title: Re: Moteino and SPI
Post by: sketchy on December 03, 2013, 07:30:31 AM
Yes. the challenge is after a call to cc3000.begin(), the cc3000 leaves MISO high instead of floating. (using breakout) I found a software workaround (I am better at software then electronics).  Just ordered a level shifter to see if I put that in the RFM12B can pull down the line.

I document this challenge on this post:  http://bitknitting.wordpress.com/2013/12/02/debugging-the-spi-bus-when-components-wont-share/ (http://bitknitting.wordpress.com/2013/12/02/debugging-the-spi-bus-when-components-wont-share/)

Isn't this something TI should fix?

FELIX: regarding using large value resistors (I am new to electronics).  I thought using a 74LVC245 might be a better choice?  I thought this because I read somewhere that using resistors would cause too much fluctuation? What are the tradeoffs?  ( a big goal of my efforts is to learn as much as I can about electronics).

I want to use the RFM69.  This might fine at the nodes with the Moteino.  It is at the base system - the one that reads in the sensor data then sends the data to a cloud service.  On the base system I need more than 2K of SRAM...the
Title: Re: Moteino and SPI
Post by: Felix on December 03, 2013, 08:43:58 AM
I was referring to pullups, not level shifting. I solved such SPI conflicts with pullups or pulldowns depending on situation. Some SPI devices leave MISO floating and cause conflicts when other SPI devices are trying to communicate with the master. So the pulldown/up resistors (10k or more is fine) would make sure that line won't float and garble SPI communications from other devices. 10K resistor pull resistors are enough to keep that from floating, but weaker than a normal SPI device that will pull the data line up/down for the communication.
Title: Re: Moteino and SPI
Post by: sketchy on December 03, 2013, 11:08:04 AM
But isn't correct behavior for an SPI device to put CS to high and MISO to floating when not using?
e.g.:
SS taken high to indicate end of the sequence of data. At this stage the slave should release the MISO line (configure it as an input, or "high impedance"). Also the slave should ignore any clock pulses now (they may be for a different peripheral).

From: http://www.gammon.com.au/forum/?id=10892 (http://www.gammon.com.au/forum/?id=10892)

(Just trying to make sure I understand.  I don't feel confident in my knowledge of electronics.)
Title: Re: Moteino and SPI
Post by: Felix on December 03, 2013, 12:29:18 PM
I would just say that a SPI device should not interfere or garble MISO when another device is talking (ie when the CS line is not asserted).
Title: Re: Moteino and SPI
Post by: scott216 on December 13, 2014, 04:46:22 PM
Does the RFM69 module in the Moteino tri-state the MISO line?  I'm using a moteino with an Ethernet module, so I've got two devices on the SPI buffer: the radio in the Moteino and the RFM69 module.  I came across this article (http://www.dorkbotpdx.org/blog/paul/better_spi_bus_design_in_3_steps) that suggests adding a buffer chip to make sure MISO is tri-stated.
Title: Re: Moteino and SPI
Post by: Felix on December 13, 2014, 06:23:55 PM
I am not sure. It will work with other SPI devices as long as separate CS pins are used, and the SPI comm doesn't step on each other.
Title: Re: Moteino and SPI
Post by: scott216 on December 13, 2014, 10:36:27 PM
I'm using my Moteno with a Wiznet 5100 Ethernet module and the Ethernet module SS is on pin D9, so It's separate from the RFM69 SS pin which is pin D10.  It does work ok, but it will hang every now and then. 
Title: Re: Moteino and SPI
Post by: Felix on December 13, 2014, 10:40:50 PM
Keep away from D9, it's the onboard LED.
moteino.com/#pinout
Wiznet is a module that I would also avoid, from experience - it will hang and the libraries are very heavy for the atmega328p.
Title: Re: Moteino and SPI
Post by: scott216 on December 14, 2014, 06:16:50 AM
Thanks for the feedback.  I didn't realize D9 was the LED.  What would you recommend for an Ethernet board?
Title: Re: Moteino and SPI
Post by: Felix on December 15, 2014, 10:36:50 AM
I think the WIZ library assumes CS=D10 but that's used by the RFM69 transceiver.
You can use any pin that is not used, please study the pinout to avoid confusion: moteino.com/#pinout
Title: Re: Moteino and SPI
Post by: scott216 on December 15, 2014, 10:50:36 AM
I think the WIZ library assumes CS=D10 but that's used by the RFM69 transceiver.
You can use any pin that is not used, please study the pinout to avoid confusion: moteino.com/#pinout

That's right, the Arduino Ethernet library does use D10, but I have some modified files that let you select a different CS pin:
See: http://forum.arduino.cc/index.php?topic=217423.msg1881620#msg1881620
Title: Re: Moteino and SPI
Post by: scott216 on December 15, 2014, 05:20:23 PM
Keep away from D9, it's the onboard LED.
moteino.com/#pinout
Wiznet is a module that I would also avoid, from experience - it will hang and the libraries are very heavy for the atmega328p.

I decided to upgrade my project and use the Moteino Mega (it arrived today).  Looking at the Moteino Mega PCB I see that CS is on pin 4, MOSI is pin 5, MISO pin 6 and SCK is pin 7.  Is that right?  On the regular Arduino MEGA the SPI pins are 51-53.  Will the standard Ethernet libraries work with the Moteino Mega and this SPI pin configuration?  Which Moteino Mega pin is used for CS for the radio module, is it pin 4?
Title: Re: Moteino and SPI
Post by: Felix on December 15, 2014, 06:30:40 PM
www.moteino.com/#pinoutMEGA
Title: Re: Moteino and SPI
Post by: scott216 on December 16, 2014, 09:08:41 AM
www.moteino.com/#pinoutMEGA

I just want to confirm - If I use the Arduino IDE and choose MoteinoMega (I haven't installed the Moteino Mega Core (https://github.com/LowPowerLab/Moteino/tree/master/MEGA/Core) yet) for the board and use the standard Arduino Ethernet library, then the Moteino will use the MISO, MOSI and SCK pins as shown on the Moteino pinout (https://farm4.staticflickr.com/3871/14677033948_a1a5d108c8_o.png) for SPI communication.  I'm not worried about CS pin at this point.
Title: Re: Moteino and SPI
Post by: Felix on December 16, 2014, 09:57:22 AM
That is library dependent not arduino variant/core dependent. You will have to check with the library.
The Motieno has the same pin assignments as the Arduino UNO. There is no arduino based on the atmega1284 so I don't know if the ethernet library will work with it.
Title: Re: Moteino and SPI
Post by: scott216 on January 06, 2015, 07:35:29 AM
That is library dependent not arduino variant/core dependent. You will have to check with the library.
The Motieno has the same pin assignments as the Arduino UNO. There is no arduino based on the atmega1284 so I don't know if the ethernet library will work with it.

I tried the MoteinoMega with my Ethernet shield and it worked fine.  I did use modified Ethernet libraries (http://forum.arduino.cc/index.php?topic=217423.msg1881620#msg1881620) that let me change the slave select pin to something other than pin 10.

Here's the sketch I used.  It makes an API request to Weather Underground and then prints the wind speed to the serial monitor.
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>     // modified to support alternate SS pin.  See http://bit.ly/1xNy2rP
#include <TextFinder.h>  // http://playground.arduino.cc/Code/TextFinder

// Slave Select Pins
const byte SS_PIN_ETHERNET = 12; 
const byte SS_PIN_MICROSD =  13; 
const byte SS_PIN_RADIO =    10; 

char server[] = "api.wunderground.com";   

byte g_mac[] = { 0xDE, 0xAD, 0xBD, 0xAA, 0xAB, 0xA5 };
byte g_ip[] = { 192, 168, 216, 40 }; 

EthernetClient client;

void setup()
{
  pinMode(SS_PIN_ETHERNET,    OUTPUT);
  pinMode(SS_PIN_RADIO,       OUTPUT);
  pinMode(SS_PIN_MICROSD,     OUTPUT);
 
  digitalWrite(SS_PIN_ETHERNET, HIGH);
  digitalWrite(SS_PIN_RADIO,    HIGH);
  digitalWrite(SS_PIN_MICROSD,  HIGH);
 
  Serial.begin(9600);
  delay(6000);
  Serial.println("MoteniMega Ethernet Test");
   
  // start the Ethernet connection:
  Ethernet.select(SS_PIN_ETHERNET);  // Set slave select pin - requires modified Ethernet.h library
  Ethernet.begin(g_mac, g_ip); 
  Serial.println(Ethernet.localIP());

  // give the Ethernet shield a second to initialize:
  delay(2000);
 
}


void loop()
{

  Serial.print(F("Wind speed: "));         
  Serial.print(getWind());
  Serial.println();         
  delay(10000);
 
}

byte getWind()
{
  TextFinder finder( client );
  float windDir = 0;

  if (client.connect(server, 80))
  {
    // Make a HTTP request:
    client.println(F("GET /api/cb0578a32efb0c50/conditions/q/pws:KVTWESTD3.json HTTP/1.1"));
    client.println(F("Host: api.wunderground.com"));
    client.println(F("Connection: close"));
    client.println();
  }
  else
  { Serial.println(F("connection failed"));  }

  if (client.connected())
  {
    if(finder.find("wind_mph"))
    {  windDir = finder.getFloat(','); }
  }
  else
  { Serial.println(F("not connected")); }
 
  client.stop();
 
  return windDir;
 
}
Title: Re: Moteino and SPI
Post by: kiwisincebirth on January 07, 2015, 06:34:09 AM
Hi Scott, I am interested to know, have you gotten the W5100 ethernet and RFM69 working on a single Moteino. I am planning on trying to build a Gateway running on a single Moteino.

I have also been doing some SPI investigation, and there are software issues with multiple devices on the SPI bus, especially where interrupts are involved. This is described here

http://dorkbotpdx.org/blog/paul/spi_transactions_in_arduino

This new SPI capability has been released in Arduino 1.5.8 (not 1.0.6), and the ethernet libraries updated to support this. The RFM69 libraries have not had such attention.
Title: Re: Moteino and SPI
Post by: scott216 on January 07, 2015, 06:54:47 AM
Hi Scott, I am interested to know, have you gotten the W5100 ethernet and RFM69 working on a single Moteino. I am planning on trying to build a Gateway running on a single Moteino.

I have also been doing some SPI investigation, and there are software issues with multiple devices on the SPI bus, especially where interrupts are involved. This is described here

http://dorkbotpdx.org/blog/paul/spi_transactions_in_arduino

This new SPI capability has been released in Arduino 1.5.8 (not 1.0.6), and the ethernet libraries updated to support this. The RFM69 libraries have not had such attention.

Well, I wouldn't say me setup is super stable, it's okay though.  I do have my sketch reboot if it doesn't upload anything to weather underground for a while and I want to add a watch dog timer.  I'm using a modified Ethernet.h/cpp and w5100.h/cpp files which lets me  set the slave select pin to something other then D10.  This modification seems to work very well and I've used it on other projects.   All my code is posted on github here: https://github.com/Scott216/Weather_Station_Data.  You can also read about it on the associated GitHub Wiki page (https://github.com/Scott216/Weather_Station_Data/wiki):

Modified libraries can be found here:
Arduino 1.0.5/6: http://forum.arduino.cc/index.php?topic=217423.msg1881620#msg1881620
Arduino 1.5.8:     http://forum.arduino.cc/index.php?topic=217423.msg1962182#msg1962182

Title: Re: Moteino and SPI
Post by: TomWS on January 07, 2015, 08:54:09 AM
@Scott216, can you tell us how much memory you're consuming with this application using the Ethernet library?

Kiwi and I are both interested, but I'm concerned about consuming all of RAM to support Ethernet on a AtMega328P.

Tom
By the way, I looked at the code, the TextFinder library looks pretty sweet for HTTP clients...
Title: Re: Moteino and SPI
Post by: kiwisincebirth on January 07, 2015, 06:26:48 PM
Hi Tom

It is encouraging that the basic configuration does work. I myself am interested in MQTT communication over ethernet.

Compiling my Sketch with RFM69 Ethernet and the MQTT PubSubClient client, indicated 68% Flash consumed, and 66% SRAM consumed, this is with other stuff as well so there is some headroom

My investigation has shown that 1.5.8 has the new SPI libraries included, but also that the W5100 ethernet drivers have also been enhanced to use these features.

I am thinking that IF the new SPI library transaction support could be incorporated into the RFM69 library, then this might be the best outcome to deliver a stable solution.

Kiwi
Title: Re: Moteino and SPI
Post by: TomWS on January 07, 2015, 06:45:02 PM
Kiwi,
do you know if the two versions of IDE can peacefully coexist (ie, can I install the 1.5.8 version and it won't break my 1.5.7 setup)?

Seems like they could since everything is installed into a separate arduino-<version> folder, but I'd like to know that it will be ok before I install it.

Tom
Title: Re: Moteino and SPI
Post by: scott216 on January 07, 2015, 07:24:05 PM
@Scott216, can you tell us how much memory you're consuming with this application using the Ethernet library?

Kiwi and I are both interested, but I'm concerned about consuming all of RAM to support Ethernet on a AtMega328P.

Tom
By the way, I looked at the code, the TextFinder library looks pretty sweet for HTTP clients...

I'm compiling with IDE 1.0.5, but I want to try it with 1.5.8.  Compiling my sketch (https://github.com/Scott216/Weather_Station_Data/blob/48640de1702af291b076fdea496a46ace528d126/Weather_Station/Weather_Station.ino) uses 29,780 bytes (of a 32,256 byte maximum).  At the end of my setup function, I get the free RAM which is 445 bytes. 

I did purchase a MoteinoMega and want to try that and see if it's more stable. 
Title: Re: Moteino and SPI
Post by: TomWS on January 07, 2015, 09:14:23 PM
@Scott216, can you tell us how much memory you're consuming with this application using the Ethernet library?

Kiwi and I are both interested, but I'm concerned about consuming all of RAM to support Ethernet on a AtMega328P.

Tom
By the way, I looked at the code, the TextFinder library looks pretty sweet for HTTP clients...

I'm compiling with IDE 1.0.5, but I want to try it with 1.5.8.  Compiling my sketch (https://github.com/Scott216/Weather_Station_Data/blob/48640de1702af291b076fdea496a46ace528d126/Weather_Station/Weather_Station.ino) uses 29,780 bytes (of a 32,256 byte maximum).  At the end of my setup function, I get the free RAM which is 445 bytes. 

I did purchase a MoteinoMega and want to try that and see if it's more stable.
Since you're down to that 'risky' level of available RAM, have you already moved your debug strings to Flash memory?  Look on adafruit for F() macro - this helps a lot!   I suppose I shouldn't be surprised that you're consuming 29K of memory, but, wow... I would have thought that the Ethernet library would over consume RAM rather than Flash storage.  The good news is that using up Flash doesn't create 'instabilities' while not having quite enough RAM certainly does!

Thanks for the info.  The MoteinoMega might not be a bad idea.  ;)
I'd use that too if I wasn't so invested in squeezing every possible bit of function out of the smallest possible device.  :D

Tom
Title: Re: Moteino and SPI
Post by: pedro.albuquerque on June 13, 2016, 07:02:17 PM
Hello,

I'm trying to use a TFT display on Motion MEGA Lora, using SPI .
When I configure the TFT (Adafruit 2.2" ILI9340 or ITEAD ITDB02) to use hardware SPI it is way faster, but as soon as I get Motion to receive info from another Motion, the display and sketch "hangs".

All work fine as long as :
1- no packets are sent form remote motion
2- or use software SPI on receiving Moteino

I have two question if anyone can help:

a) reading this thread I understand this is not uncommon, and eventually a resistor on MISO may solve the problem. is this applicable i this case? any experience with OLED displays/SPI based displays?

b) how does one library know that the other one is not using the SPI lines ? how does it know that SS on the other divide is not active? Moteino is using interrupts for the library to be aware that data is coming and initiate a SPI read right ? If I'm sending data to the display will it not interrupt the display communication ?
How is this potencial conflict avoided ?