chipKIT® Development Platform

Inspired by Arduino™

problem of using ChipKit Max32 SD card library

Created Wed, 21 Aug 2013 19:18:52 +0000 by workstation008


workstation008

Wed, 21 Aug 2013 19:18:52 +0000

Hello everyone, I am using the Chipkit Max32 board to write/read a SD card module. The SD card module was bought from ebay. The SPI pin configuration is

SCK2 - 52 pin on ChipKit Max32 board MOSI - 29 pin on chipkit max 32 MISO - 43 pin on chipkit max32 SS2 - 53 pin on chipkit max 32

I use Serial2 as debugging, and directly change the demo code in MPIDE0023-windows-20120903. The SD card is a 32MB microSD with an adapter.

The code is like this

// include the SD library:
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 53;    

void setup()
{
  Serial2.begin(115200);
  Serial2.print("\nInitializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
  pinMode(53, OUTPUT);     // change this to 53 on a mega


  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial2.println("initialization failed. Things to check:");
    Serial2.println("* is a card is inserted?");
    Serial2.println("* Is your wiring correct?");
    Serial2.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
   Serial2.println("Wiring is correct and a card is present."); 
  }

However, the initialization of SD card is always failed.

Can anyone give me any hint what is the reason for this?

Great Thanks!


majenko

Wed, 21 Aug 2013 19:28:33 +0000

You may have your MISO and MOSI lines mixed up.

MISO is Master In Slave Out - the equivalent to SDI. MOSI is Master Out Slave In - the equivalent to SDO.

Pin 29 is SDI2A. Pin 43 is SDO2A.


workstation008

Wed, 21 Aug 2013 19:59:20 +0000

Is this correct?

MCU pin 29 (SDI2) connected to SD card MISO MCU pin 43 (SDO2) connected to SD card MOSI

I switched the MISO and MOSI pins but no success yet.


majenko

Wed, 21 Aug 2013 20:18:01 +0000

This is all very confusing. The chip, and the software, references SPI ports 1 thru 4. The manual references SPI 1A, 1B, 2A and 2B, and I don't think they map in straight order. I have no idea why they chose a different numbering scheme - there must be a logical reason behind it.

Anyway, according to the manual, the pins you should be using are:

SPI: Synchronous serial port. Pin 53 (SS), Pin 51 (MOSI), Pin 50 (MISO), Pin 52 (SCK). These signals also appear on connector J13. Jumpers JP3 and JP4 are used to select whether the Max32 operates as a Master (transmit on MOSI, receive on MISO) or a Slave (transmit on MISO, receive on MOSI) device. The shorting blocks on JP3 and JP4 are normally placed in the Master position for the Max32 to function as an SPI master. This uses SPI2A (SS2A, SDI2A, SDO2A, SCK2A) in the PIC32 microcontroller.

And SPI 2A acually maps to SPI2. Not sure what the others map to.


workstation008

Wed, 21 Aug 2013 20:45:59 +0000

Actually, I don't quite understand the demo code in MPIDE, like this

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 53;

The chipSelect pin number is based on the MCU board, why there is a number based on the SD shield or module?


workstation008

Wed, 21 Aug 2013 21:00:31 +0000

Here is the schematic of using the SD module.


majenko

Wed, 21 Aug 2013 21:15:06 +0000

And are you wiring it up like that? With all those resistors?


workstation008

Wed, 21 Aug 2013 22:07:35 +0000

I changed to another SD shield, and the wiring from Chipkit max32 board is same. Then, I got the following results.

It looks the init of SD is not consistently successful. In addition, even after I format the SD card using PC with a FAT file system, it still can not find the file system on the SD card.


majenko

Wed, 21 Aug 2013 23:07:22 +0000

And are you wiring it up like that? With all those resistors?


workstation008

Thu, 22 Aug 2013 14:14:22 +0000

No. I did not put any resistors.


majenko

Thu, 22 Aug 2013 17:49:16 +0000

Try formatting the SD card using the official SD card formatter: [url]https://www.sdcard.org/downloads/formatter_4/[/url]

It could be that the one in Windows isn't formatting it quite right for the SD library to work with.


mnskll

Thu, 22 Aug 2013 22:34:48 +0000

Hi, I don't have a Max32, so I can't test this, but in the reference manual for the Max32, http://ww1.microchip.com/downloads/en/DeviceDoc/chipKIT%20Max32_rm.pdf it says :

"SPI: Synchronous serial port. Pin 53 (SS), Pin 51 (MOSI), Pin 50 (MISO), Pin 52 (SCK)."

since SS pin and SCK pin are the same, they seem to be talking about the same SPI bus (SPI2?)

I think those are the correct pins. you can also use pin 43 and 29, but then you probably have to use Peripheral Pin Select to remap them to these.

/MÃ¥ns


workstation008

Fri, 23 Aug 2013 14:39:16 +0000

Thanks guys for your replies.

I yesterday tried to debug the code and found a couple of issues.

  1. My 32M SD card will be automatically formatted to FAT12, and the SD library can sometimes correctly initialize it and read its information. It is not consistently successful.

  2. I tried another 8GB SD card, which should be FAT32, still failed. This SD card was bought from ebay. It is readable and writable. I tried the SDformatter, but no success yet.


workstation008

Fri, 23 Aug 2013 19:37:43 +0000

There is some progress on this. The problem is not because of the software, but the hardware. I use the Chipkit network shield together with the Max32 board. Then, I found the SPI signal is really noisy. Finally, I remove the chipkit network shield board temporarily, and everything is good now.

I guess the noise comes from the network shield. But I have no solution yet. Does anybody have suggestion?

Thanks!


workstation008

Fri, 23 Aug 2013 20:13:56 +0000

I guess the noise comes from the 25MHz oscillator, so I remove it for testing. It works!


workstation008

Tue, 27 Aug 2013 15:29:38 +0000

Just to upfate the performance of using the SD card library. write 1.5KByte makes around 70miliseconds.