chipKIT® Development Platform

Inspired by Arduino™

Troubles with home built PIC32MX250F128 (PDIP) and SD card

Created Fri, 28 Nov 2014 22:52:22 +0000 by madias


madias

Fri, 28 Nov 2014 22:52:22 +0000

Edit: case closed -> see the last two replies!

Hello, I'm pretty new to chipkit and built successfully my first PIC32MX250F128 board (with the standard microchip bootloader). Everything is running fine, all pins tested, Nokia Display, SPI DAC's (MCP49x2), ILI9341 LCD with self rewritten UTFT library and so on. But I'm struggling total with the SD-card library (both with MPIDE and UECIDE, they appear to have different SD card libraries). As board, I choose "chipkit DP32". The problem is, that I can't get any connection to the sd-card. I've tried many adapters and SD-cards, all of them work with arduino and stellaris Launchpad Tiva123 (as an 3.3V example). The sd-card library with MPIDE doesn't compile, error: /Applications/mpide.app/Contents/Resources/Java/./hardware/pic32/libraries/SD/utility/Sd2PinMap.h:165:6: error: #error Boards with PPS must be specifically defined What does that mean?!?! How to specifically my board?!?! So I used UECIDE My SPI should work (tested with DSPI and a SPI DAC). My connections should be: MOSI: RA4/18 MISO: RA1/10 SCK: RB14/7 as CS Pin I choose Pin RA0/9 and other ones tested with the basic sketch (all of the sd-lib demos) won't work. I also used a sketch, found in a forum (chipkit/UECIDE?) from majenko (In this place: MANY, MANY thank for all your work!!!)

edit: removed useless code But also without success. I tested with an oscilloscope the 4 pins (mosi/miso,sck,CS) and I see some flicker on starting. (strange, that the SCK pin isn''t giving signal all the time) The SD-card library is very confused and obscure to me (especially the pin configuration, is hardware SPI the default??). So maybe someone of you can give me some hints.

Thank you and regards Matthias


majenko

Sat, 29 Nov 2014 11:29:52 +0000

Ok, first step is to confirm that the SDCARD SPI port has mapped itself correctly.

Connect the MOSI and MISO pins together, and add a little bit to send out stuff through the SPI and print what it receives. Check the SCK pin is clocking while it's doing it. Something like:

while (1) {
    for (int i = 0; i < 256; i++) {
        byte ret = SDCARD.transfer(i);
        Serial.print("Sent: ");
        Serial.print(i, DEC);
        Serial.print(" Received: ");
        Serial.println(ret, DEC);
    }
}

just after the SDCARD.setMode(...) line.

You should, of course, see:

Sent 0 Received 0
Sent 1 Received 1
Sent 2 Received 2
... etc ...

and your oscilloscope should show you lots and lots of activity on the SPI bus.


madias

Sat, 29 Nov 2014 23:26:31 +0000

dear majenko, I tried following code with success:

#include <DSPI.h>
DSPI0 spi;
void setup() {
spi.begin();
Serial.begin(9600);
}
void loop() {
    for (int i = 0; i < 256; i++) {
        byte ret = spi.transfer(i);
        Serial.print("Sent: ");
        Serial.print(i, DEC);
        Serial.print(" Received: ");
        Serial.println(ret, DEC);
    }
}

I built in this code also in the sd-card example above, with same results: 1 equal 1, 2 equal 2 and so on, so (D)SPI is working correct. All pins are working (MOSI,MISO,SCK) BUT when I put the while() code into the sd-example, there is no SCK signal. I've localized the problem: Serial.begin(9600); When I insert the (USB) serial communication, SCK signal is gone :/ Serial0.begin(9600); should work (I've the SCK line back) but now I've to debug with my ttl-usb converter, or - more easier - I try to use DSPI1 instead of DSPI0. But the "reason why?" is not clear to me.


Jacob Christ

Sun, 30 Nov 2014 01:39:15 +0000

Your problems may be related to this:

https://github.com/chipKIT32/chipKIT32-MAX/issues/532

Jacob


madias

Sun, 30 Nov 2014 10:17:49 +0000

I think the github issue is about the MPIDE sd-card-library. This library isn't working, because it uses software SPI without PIC32MX1xx and PXC32MX2xx support, see Sd2PinMap.h line 164:

#elif defined(__PIC32MX1XX__) || defined(__PIC32MX2XX__) || defined(__PIC32MZXX__)
    #error Boards with PPS must be specifically defined

I use the UECIDE sd-card library with reenabled hardware spi. But it seems that the DSPI OR the sd-card library cut off the standard SCK DSPI0 pin (RB14/ 7), when using USB serial. I've to research if the problem is caused by the DSPI li and/or the standard SPI lib.

So the problem is not only the SD-card lib, but also every DSPI/SPI combination with Serial.begin(); and the PIC32MX250xx


madias

Sun, 30 Nov 2014 19:19:40 +0000

edit: removed useless post, see next posting!


madias

Wed, 03 Dec 2014 22:21:12 +0000

now got 100% success! I don't really know, what was really the matter, uploaded via pickit3 the newest bootloader for DP32, updated UECIDE to beta 0.8.7z25. Sometimes it is difficult to get the right connection, but maybe it's caused by my hardware layout. Is worth mentioning that only one of my adapters work correctly (a cheap LC-Soft breakout card holder) Even DPI0 works for now. Tested with my homebuilt board and my breadboard version.

Thanks for all of your (brain)work!

code:

//Main program
//Uses majenko's modified DSPI from UECIDE
//#include <plib.h>
#include <SPI.h>
#include <SD.h>
#include <DSPI.h>
#define ssselect 9 // Chip select
DSPI0 SDCARD;
File myFile;
Sd2Card card(&SDCARD,ssselect);

SdVolume volume;
SdFile root;
const int chipSelect_SD = ssselect;

void setup() {
   Serial.begin(115200);
   delay(2000);
   Serial.println("TEST");

   pinMode(chipSelect_SD, OUTPUT);
   digitalWrite(chipSelect_SD, HIGH);
   
   SDCARD.begin(ssselect); // MISO, MOSI and Chip Select (Must be mappable)
   //delay(2000);
   pinMode(ssselect, OUTPUT);
   SDCARD.setMode(DSPI_MODE0);
   SD.begin(ssselect);
   if(!card.init(SPI_FULL_SPEED, chipSelect_SD)) {
      Serial.println("initialization failed. Things to check:");
      Serial.println("* is a card is inserted?");
      Serial.println("* Is your wiring correct?");
      Serial.println("* did you change the chipSelect pin to match your shield or module?");
      Serial.println(card.errorCode(),HEX);
      Serial.println(card.errorData(),HEX);
      return;
   }
   else{
      Serial.println("Wiring is correct and a card is present.");
   }


 // print the type of card
  Serial.print("\nCard type: ");
  switch(card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  // print the type and size of the first FAT-type volume
  long volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();
  
  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
  
  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);

//*********read/write example
 myFile = SD.open("test.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
    
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
        Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
}

madias

Fri, 05 Dec 2014 10:36:02 +0000

Ok, now I found out the real problem: It was the prebuilt bootloader, shipped with the TCHIP-USB-MX250F128B

For additional infos see my thread: "Some notes to TCHIP-USB-MX250F128B" http://chipkit.net/forum/viewtopic.php?f=17&t=3114

case closed


majenko

Fri, 05 Dec 2014 10:51:48 +0000

Great spot of detective work there!

Maybe I should roll a new bootloader specially for breadboards and start selling pre-programmed chips myself...?


jmlynesjr

Fri, 05 Dec 2014 18:35:32 +0000

Good idea!

Would be kept more up to date that way. Big company not very agile.

James


madias

Sat, 06 Dec 2014 22:02:49 +0000

selling pre-programmed chips WITH documentation should be the key :) maybe in combination with a completely stripped down dev board. Maybe better idea: selling a board with pre-programmed pic32mx150xx, a cheap ftdi (or similar) as programming unit, so people get rid of the strange usb-uart serial-monitor behavior (insert delay in sketch..) and we got back RB6 for a "real" complete 8-bit port (MC killed it on the mx250 the RB6 with "VBUS") but seriously: the DP32 bootloader works fine!