Moteino Mega connecting with SDcard Reader/Writer?

Started by hexibot43, June 01, 2019, 06:21:13 PM

hexibot43

     Currently I have been working on a project with an Arduino Mega that I decided I wanted to do some data logging.  Bought a SD card connection board.  And had issues.  Did some reading to find the Arduino Mega has issues with connecting to SD cards.  I have a spare Moteino Mega swap in.  Are there issues with Moteino Mega and reading SD cards?  Any pitfalls to look out for?  Figured I'd better ask before I start moving all my code over to find problems here too. 

Thanks,

MAB

TomWS

There are issues whenever you try to share an SD card with another SPI device. Some work arounds work most of the time, but what works all of the time is to put the SD card on its own SPI bus with dedicated SCK. I've done this with Moteino Mega without any problems.
Alternative is to not use any other SPI device when you're using the SD card. That is, Init SD, open file, do file io, close file, flush SD, and uninit SD. Then do any other SPI  devices.

Felix

Since Moteinos can have a radio module and SPI FLASH-MEM chip, both using SPI with separate SPI_CS pins, why would a third SPI device with another SPI_CS pin not work?
Anything special about SD cards?

hexibot43

     Felix,
    I have a MotenioMega I was going to use as my main Main, and it would need to always be listening.  Radio "On"  And of course that would be the same unit I'm using to Log to the SD card.  Now would that be where it would problematic?  Somewhere around here I use to have a chip dedicated to driving a memory card.  I would just pass it data via serial port.  I just can't find it, or remember where I got it. Ahhh.  Still looking.  Thanks.

Felix

Quote from: hexibot43 on June 03, 2019, 10:30:13 AM
Now would that be where it would problematic?
No, I don't know of any problems.
Maybe Tom can give some details how an SD used with native SPI would be problematic.
BTW - MoteinoMEGA uses the Atmega1284p AVR processor and is not the same as Arduino MEGA.

TomWS

@Felix, SD card protocol is not simple SPI sequence. There are modes where clk and sdi are meaningful WITHOUT CS.  Mixed SPI transactions can mess up SD configuration.  There is at least one thread on this forum discussing it from a couple of years ago.  I'm traveling so can't dig it up easily.

hexibot43

Well, it sounds like at least have a chance.  I'm already realizing I'm enjoying the smaller package size.  And the radios instead of cabling is so much nicer.  Thanks everyone.

chribru

@hexibot, did you have any luck with your SD card project?
I've been trying to initialize SD card with the SD card adapter from lowpower lab and a Moteino Mega with no luck. I was able to configure the SPI pins as they are not standard Arduino Mega pins using this code but no luck. This code works on a Teensy with on board SD card so it's not the card itself.

#include <Arduino.h>
#include <SPI.h>
#include <SD.h>

#define SD_CS  0
#define SD_PWR 1
#define FLASH_CS 23
#define RF_CS 4

#define SPI_MOSI 5
#define SPI_MISO 6
#define SPI_CLK  7

void setup() 
{
  //************ SD Card  ************* 
  pinMode(SS, OUTPUT); 
  pinMode(SD_PWR,OUTPUT);
  pinMode(SD_CS,OUTPUT);
  pinMode(FLASH_CS,OUTPUT);
  pinMode(RF_CS,OUTPUT);
  
  digitalWrite(SD_PWR,LOW);   //turn power to SD module on
  digitalWrite(FLASH_CS,LOW); //pull CS line for on boards SPI flash low
  digitalWrite(RF_CS,LOW);    //pull CS line for on board RF low
  digitalWrite(SD_CS,HIGH);   //pull CS line for SD card module high
  
  Serial.begin(9600);
  delay(1000);

  if (SD.begin(SD_CS,SPI_MOSI,SPI_MISO,SPI_CLK)) {
      Serial.println("SD CARD initialised.....");          
  }
  Serial.println("SD CARD failed.....");
  delay(2000);
}
  
void loop() {
}



Felix

Did you try to use the hardware SPI instead?
Do you power the SD adapter from 3.3V of the MoteinoMEGA?

chribru

#9
I'm not sure what you mean. I'm using the hardware SPI using pin 5-MOSI, 6-MISO and 7-SCK. I got it to work however. Looked at the datasheet of ATmega1284P and saw that the pin 4 on the MotMega is going to SS pin (PB4) which is active low. It's also the CS line for the on board RF but needs to be set HIGH (to make it LOW) if you want to talk to other SPI slaves. Below the working test code.


#include <Arduino.h>
#include <SPI.h>
#include <SD.h>

#define SD_CS  0
#define SD_PWR 1
#define FLASH_CS 23
#define RF_CS 4

void setup() 
{
  //************ SD Card  ************* 
  pinMode(SD_PWR,OUTPUT);
  pinMode(SD_CS,OUTPUT);
  pinMode(FLASH_CS,OUTPUT);
  pinMode(RF_CS,OUTPUT); //setup this pin as output also sets the uC to be SPI master
  
  digitalWrite(SD_PWR,LOW);   //turn power to SD module on
  digitalWrite(FLASH_CS,LOW); //pull CS line for on boards SPI flash low
  digitalWrite(RF_CS,HIGH);   //active LOW therefore pull CS line for on board RF HIGH
  
  Serial.begin(9600);
  delay(1000);

  if (SD.begin(SD_CS)) {  
      Serial.println("SD CARD initialised.....");
  }
  else{
    Serial.println("SD CARD failed.....");
  }
}
  
void loop() {
}

Felix

Great that you got it working.
Note that to cut power off to the SD card completely, you will likely need to set the MOSI pin (possibly SCK and MISO too) to INPUT, then calling your sleep code, then back to OUTPUT, something like this (in addition to setting the SD_PWR to HIGH):

  pinMode(MOSI, INPUT); //ADD THIS BEFORE SLEEPING
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);   // everything is eleeping, just a few uA
  pinMode(MOSI, OUTPUT); //ADD THIS AFTER SLEEPING


Otherwise depending on the SD card, the MOSI pin might drain a few hundred uA.