Author Topic: MoteinoMEGA and SD card  (Read 7017 times)

fmark

  • NewMember
  • *
  • Posts: 5
  • Country: ca
MoteinoMEGA and SD card
« on: July 22, 2016, 01:53:44 PM »
Hi,

I'm trying to get SD card (SPI) working with MoteinoMEGA paired with RFM69HW (915Mhz).
My SD card's CS pin is connected to D3 on MoteinoMEGA. Using standard unmodified libraries for both RFM69 and SD.

For some reason I cannot get both(SDCard and RFM69HW) to work together.
Radio fails to initialize when SD Card adapter is connected to MoteinoMEGA even if I comment out SD-related code.

Is there anything specific that I need to do in the code in order to make both play nicely with each other?
Anything specific on the hardware side?

Thanks in advance!

Code: [Select]
#include <RFM69.h>
#include <RFM69_ATC.h>
#include <SD.h>

#define LED                   15 // LED is on D15
#define chipSelect_SD   3

#define NODEID        1    //unique for each node on same network
#define NETWORKID     100  //the same on all nodes that talk to each other
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
#define FREQUENCY     RF69_915MHZ
#define ENCRYPTKEY    "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
#define IS_RFM69HW    //uncomment only for RFM69HW! Leave out if you have RFM69W!
#define ENABLE_ATC    //comment out this line to disable AUTO TRANSMISSION CONTROL
//#define PROMISCOUS_MODE  //uncomment to sniff all packets on the same network

#ifdef ENABLE_ATC
  RFM69_ATC radio;
#else
  RFM69 radio;
#endif

void setup()
{
  //************ SD Card  *************
  pinMode(SS, OUTPUT);
  if (SD.begin(chipSelect_SD)) {

      // Blink 4 times to indicate SD card successful init
      Blink(LED, 200, 4);     
  }
  delay(2000);

  //************ RFM69HW  *************
  delay(10);
  if (radio.initialize(FREQUENCY, NODEID, NETWORKID)) {

    #ifdef IS_RFM69HW
      radio.setHighPower();
    #endif
    radio.encrypt(ENCRYPTKEY);
    #ifdef PROMISCOUS_MODE
      radio.promiscuous(true);
    #else
      radio.promiscuous(false);
    #endif
    //radio.setFrequency(919000000); //set frequency to some custom frequency
 
    //Auto Transmission Control - dials down transmit power to save battery (-100 is the noise floor, -90 is still pretty good)
    //For indoor nodes that are pretty static and at pretty stable temperatures (like a MotionMote) -90dBm is quite safe
    //For more variable nodes that can expect to move or experience larger temp drifts a lower margin like -70 to -80 would probably be better
    //Always test your ATC mote in the edge cases in your own environment to ensure ATC will perform as you expect
    #ifdef ENABLE_ATC
      radio.enableAutoPower(-70);
    #endif

    // Blink 6 times to indicate radio successful init
    Blink(LED, 200, 6); 
  }

  delay(2000);
 
  // Blink to indicate end of Setup
  Blink(LED, 300, 3);
}

void loop()
{
}

void Blink(byte PIN, int DELAY_MS, byte loops)
{
  pinMode(PIN, OUTPUT);
  while (loops--)
  {
    digitalWrite(PIN,HIGH);
    delay(DELAY_MS);
    digitalWrite(PIN,LOW);
    delay(DELAY_MS); 
  }
}


Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: MoteinoMEGA and SD card
« Reply #1 on: July 22, 2016, 03:05:29 PM »
Sounds like a hardware issue. Make sure the SD CS is pulled up or kept up. The SPI transactions between the 2 devices have to be completely separate and clean of any interference from anything else or from each other. Otherwise mayhem like this is expected.

fmark

  • NewMember
  • *
  • Posts: 5
  • Country: ca
Re: MoteinoMEGA and SD card
« Reply #2 on: July 22, 2016, 03:39:12 PM »
Sounds like a hardware issue. Make sure the SD CS is pulled up or kept up. The SPI transactions between the 2 devices have to be completely separate and clean of any interference from anything else or from each other. Otherwise mayhem like this is expected.

Thanks for the advice.
Does it mean that I need to call digitalWrite(chipSelect_SD, HIGH); in the loop() function before any calls to radio functionality?


fmark

  • NewMember
  • *
  • Posts: 5
  • Country: ca
Re: MoteinoMEGA and SD card
« Reply #3 on: July 22, 2016, 06:22:11 PM »
Update:
I added 'delay(1000);' before call to 'SD.begin(chipSelect_SD)' in setup() and got both SD card and the Radio successfully initialized!

Now I have a problem with receiving radio data in the loop()function (with or without 'digitalWrite(chipSelect_SD, HIGH)'  )

Radio receiving works fine when SD card adapter is disconnected.

What can be the problem here?

Does anybody use MoteinoMEGA + SD card + RFM69HW?
If yes, code example would be very appreciated.

Thank you!

Code: [Select]
void loop() 
{
  //digitalWrite(chipSelect_SD, HIGH); 
   
  if (radio.receiveDone()) {

    // Read received data
    String  strReceived = "G:" + String(radio.SENDERID) + " ";
    for (byte i = 0; i < radio.DATALEN; i++) {
      strReceived.concat((char)radio.DATA[i]);
    }

    if (radio.ACKRequested()) {
      radio.sendACK();
    }

    Blink(LED, 3, 1);
  }
}




TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: MoteinoMEGA and SD card
« Reply #4 on: July 22, 2016, 06:46:52 PM »
I use Mega, RFM69HW, SD Card in my Moteino gateways, some of which also have W5100 Ethernet on SPI.  SD cards sharing SPI buses are VERY finicky.  Some work, some don't, but I have found a couple of key ingredients, but, before getting into those, your original setup() set the pin mode of the radio SS but didn't write the output level high.  That would be a problem.

As Felix points out, if the SD card is inserted, you MUST set the SD CS pin high before initializing the radio, even if you're not accessing the SD card.

Key ingredients:
I use the SDFat library, not the standard library.
Your SD adapter must have pullups on key pins (the reserved pins, DI, and probably CS although I'm not sure about that one as I'm not near the computer with my schematics).
You need to be very lucky!  Actually, I'm being facetious here, but I'll never say I've found the magic combination.  I had to fiddle until it worked and, as I said, it doesn't work for all cards, in fact, in my case, I've only had success with 2GB type I cards.  Fortunately, these are fine for my GW needs, but I'll switch processors (and IDE) before I do another SD card project.

Tom

fmark

  • NewMember
  • *
  • Posts: 5
  • Country: ca
Re: MoteinoMEGA and SD card
« Reply #5 on: July 22, 2016, 07:31:37 PM »
I use Mega, RFM69HW, SD Card in my Moteino gateways, some of which also have W5100 Ethernet on SPI.  SD cards sharing SPI buses are VERY finicky.  Some work, some don't, but I have found a couple of key ingredients, but, before getting into those, your original setup() set the pin mode of the radio SS but didn't write the output level high.  That would be a problem.

As Felix points out, if the SD card is inserted, you MUST set the SD CS pin high before initializing the radio, even if you're not accessing the SD card.

Key ingredients:
I use the SDFat library, not the standard library.
Your SD adapter must have pullups on key pins (the reserved pins, DI, and probably CS although I'm not sure about that one as I'm not near the computer with my schematics).
You need to be very lucky!  Actually, I'm being facetious here, but I'll never say I've found the magic combination.  I had to fiddle until it worked and, as I said, it doesn't work for all cards, in fact, in my case, I've only had success with 2GB type I cards.  Fortunately, these are fine for my GW needs, but I'll switch processors (and IDE) before I do another SD card project.

Tom

Thank you very much, Tom!

When you said that you've had different results with different SD cards, I decided to check with another MicroSD cards that I had lying around.
Unfortunately it did not help. Tried different brands and capacities with no success.

However then I remembered that I have another SD Card adaptor (Data Logger like this one http://www.digikey.ca/product-detail/en/adafruit-industries-llc/249/1528-1125-ND/5353585)
To my huge surprise when I connected it to MEGA, everything magically started to work. I can receive radio transmissions and write them right on to the SD card!

Even when I removed all of the code that was pulling 'chipSelect_SD' and 'SS' high, it still works fine!
I guess both libraries(SD and RFM69) do their job correctly. :)

Now question is - what is different between these two SDCard adapters?
One that does not work is actually for MicroSD cards (http://www.ebay.ca/itm/122026207055?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT).
I know they both work, but there is something in the MicroSD one that just does not want to work with MEGA and RFM69HW ..




TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: MoteinoMEGA and SD card
« Reply #6 on: July 22, 2016, 09:22:47 PM »
One that does not work is actually for MicroSD cards (http://www.ebay.ca/itm/122026207055?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT).
I know they both work, but there is something in the MicroSD one that just does not want to work with MEGA and RFM69HW ..
I suppose you could pursue that question and, if you did, you might find some really universal truths that we could all use.   On the other hand, you could use the info and function you've gained and get your project done!

I vote that you dedicate your life to answering questions regarding AVR processors/Multivaried Arduino IDE configurations/SD library variations/SD Interface card variations.   8)

Have fun,
Tom

fmark

  • NewMember
  • *
  • Posts: 5
  • Country: ca
Re: MoteinoMEGA and SD card
« Reply #7 on: July 23, 2016, 12:22:36 AM »
One that does not work is actually for MicroSD cards (http://www.ebay.ca/itm/122026207055?_trksid=p2060353.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT).
I know they both work, but there is something in the MicroSD one that just does not want to work with MEGA and RFM69HW ..
I suppose you could pursue that question and, if you did, you might find some really universal truths that we could all use.   On the other hand, you could use the info and function you've gained and get your project done!

I vote that you dedicate your life to answering questions regarding AVR processors/Multivaried Arduino IDE configurations/SD library variations/SD Interface card variations.   8)

Have fun,
Tom

:)

Actually I already figured out what the problem is. :)
Unfortunately there is a known problem with my specific SD Card adapter (actually it's "Catalex v1.0").
It is described here (post#12): http://forum.arduino.cc/index.php?topic=287671.msg2186760#msg2186760
So it seems like a Hardware limitation of my adapter. Everything else seems to work as intended.

Anyway, thanks again for the help!


TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: MoteinoMEGA and SD card
« Reply #8 on: July 23, 2016, 09:06:08 AM »
Actually I already figured out what the problem is. :)
Unfortunately there is a known problem with my specific SD Card adapter (actually it's "Catalex v1.0").
It is described here (post#12): http://forum.arduino.cc/index.php?topic=287671.msg2186760#msg2186760
So it seems like a Hardware limitation of my adapter. Everything else seems to work as intended.

Anyway, thanks again for the help!
Good find!  I should have noticed the 125 wiring when I looked at the schematic for that adapter.  In any case, it does remind me that I think another key element to getting SD working with other SPI devices is to have an 'isolation' device, typically appearing as a level translator, but even if you don't need voltage translation, being able to disconnect the SD card from the rest of the SPI bus (when CS is false) is probably a cure.  Unfortunately, none of my circuit boards have this so this is pure speculation on my part.

Tom

luisr320

  • Sr. Member
  • ****
  • Posts: 255
  • Country: pt
Re: MoteinoMEGA and SD card
« Reply #9 on: July 24, 2016, 03:26:53 PM »
Also, if you try to use a TFT display that uses SPI, you will also get trouble when you try to write something on the display and the radio interrupt fires. A way around this problem is to disable the interrupts when you try to write to the display, and enabling them again when finished.

Rhood

  • NewMember
  • *
  • Posts: 4
  • Country: us
Re: MoteinoMEGA and SD card
« Reply #10 on: January 18, 2017, 04:41:42 PM »
So I have been having the same problem with (using an Adafruit microSD breakout + MEGA) and I think I have figured out a way to do it.

in the setup I set the chipselects for the flash memory and the radio to "HIGH", then I initialize the the SD card and then set it's chip select to "HIGH" and then initialize everything else. here is the code:
Code: [Select]
pinMode(CS_radio, OUTPUT);
  pinMode(CS_flash, OUTPUT);
  pinMode(CS_SD, OUTPUT);
 
  digitalWrite(CS_flash, HIGH);
  digitalWrite(CS_radio, HIGH);
 
 
  Serial.begin(SERIAL_BAUD);
  delay(10);
  if (!SD.begin(CS_SD)) {
           Serial.println("failed");
           return;
         }
        Serial.println("init OK.");
 
  digitalWrite(CS_SD, HIGH);
 
  delay(100);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);

Then every time I need to log data, I kill the interrupts, set the SD card chip select back to "LOW", do my logging, set the SD card chip select back to "High", and then re enable interrupts. Like so:
Code: [Select]
delay(1000);
    noInterrupts();
    digitalWrite(CS_SD, LOW);
    File dataFile = SD.open("datalog3.txt", FILE_WRITE);
    if (dataFile){
      dataFile.print((char*)radio.DATA);dataFile.print("[");dataFile.print(radio.SENDERID, DEC);dataFile.print("]"); //writes string to datafile
       }
       dataFile.print("\n");
       dataFile.close();
    digitalWrite(CS_SD, HIGH);
    interrupts();

so far it has worked for me

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: MoteinoMEGA and SD card
« Reply #11 on: January 18, 2017, 07:20:25 PM »
...
so far it has worked for me
The other thing you do that helps a lot is that you reopen and close the file each time you write to it.  In a case that I have, I tried to keep the file open and interleave file IO with other operations.  That doesn't work reliably, if at all, unless you have an isolation gate between the SD card's SPI bus and the remainder of devices.

Tom

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: MoteinoMEGA and SD card
« Reply #12 on: January 18, 2017, 07:48:33 PM »
SD card SPI is a bit odd, I've had SD (and MMC) cards that refuse to release MISO to high-Z even after giving dummy clocks with CS# high. The solution to that in my system was to put a 470R resistor from the SD card's MISO to the common MISO of the other devices so they could over-drive it. There's also a strangeness about having to give dummy clocks, it appears the CS# may be synchronously sampled in the SD card.
Mark.

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: MoteinoMEGA and SD card
« Reply #13 on: January 19, 2017, 08:03:41 AM »
There's also a strangeness about having to give dummy clocks, it appears the CS# may be synchronously sampled in the SD card.
I knew that the SD-Fat library did do dummy clocks on the leading edge of a transfer, but I don't know if it does on the trailing edge.  If you're right about the synchronous sampling, this may explain why the MISO isn't released properly.  Interesting.  I'll have to experiment with this on my next SD project.

Tom

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: MoteinoMEGA and SD card
« Reply #14 on: January 19, 2017, 10:30:18 AM »
I knew that the SD-Fat library did do dummy clocks on the leading edge of a transfer, but I don't know if it does on the trailing edge.  If you're right about the synchronous sampling, this may explain why the MISO isn't released properly.  Interesting.  I'll have to experiment with this on my next SD project.

Tom
Unfortunately I tried many dummy clocks with CS# high, that didn't fix the releasing of MISO per se. There's definitely something odd about this that I never got to the bottom of.
Mark.