SD card: which library?

Status
Not open for further replies.

XFer

Well-known member
Hello,

I have a Teensy 3.2 hooked up to a Adafruit Data Logging Shield (I'm migrating an existing project from Arduino Mega to Teensy)
Teensy is powered via USB.

Wiring:
Apart from the obvious +5V (splitted from USB +5V power) and +3.3V (wired to "3.3V 250mA Max"), I have:
IORef on 3.3V
ChipSelect on Pin10
MOSI on Pin11
MISO on Pin12
Clock on Pin13

I have Arduino IDE 1.6.5r5 and Teensyduino 1.2.6b1; host OS is Windows7-64.

On Arduino Mega, I used to load the Adafruit SD library (SD.h).

My initialization call was:

Code:
SD.begin(10, 11, 12, 13)

and I used to access files like that:

Code:
File myFile = SD.open("myfile.dat", FILE_READ);

and all was fine and dandy. :)

Now, the project won't even compile:

Code:
Sd2Card.cpp:30:17: error: 'RwReg' does not name a type
:static volatile RwReg *mosiport, *clkport, *misoport
Sd2Card.cpp:In function 'void spiSend(uint8_t)
Sd2Card.cpp:60:10: error: 'clkport' was not declared in this scope
:*clkport &= ~clkpinmask
Sd2Card.cpp:62:12: error: 'mosiport' was not declared in this scope
:*mosiport |= mosipinmask
Sd2Card.cpp:64:12: error: 'mosiport' was not declared in this scope
:*mosiport &= ~mosipinmask
Sd2Card.cpp:69:8: error: 'clkport' was not declared in this scope
:*clkport &= ~clkpinmask
Sd2Card.cpp:In function 'uint8_t spiRec()
Sd2Card.cpp:88:6: error: 'mosiport' was not declared in this scope
:*mosiport |= mosipinmask
Sd2Card.cpp:91:8: error: 'clkport' was not declared in this scope
:*clkport |=  clkpinmask
Sd2Card.cpp:95:13: error: 'misoport' was not declared in this scope
:if ((*misoport) & misopinmask)  data |= 1
Sd2Card.cpp:In member function 'uint8_t Sd2Card::init(uint8_t, uint8_t, int8_t, int8_t, int8_t)
Sd2Card.cpp:302:5: error: 'clkport' was not declared in this scope
:clkport     = portOutputRegister(digitalPinToPort(clockPin_))
Sd2Card.cpp:304:5: error: 'mosiport' was not declared in this scope
:mosiport    = portOutputRegister(digitalPinToPort(mosiPin_))
Sd2Card.cpp:306:5: error: 'misoport' was not declared in this scope
:misoport    = portInputRegister(digitalPinToPort(misoPin_))

I thought "Too bad, Adafruit's SD library is not compatibile with Teensyduino :( ", so I tried to switch to "SdFat" library:

Code:
#include <SdFat.h>
SdFat SD;

and changed the initialization code to:

Code:
SD.begin(SD_CHIPSELECT)

but now, while SD.begin apparently works (result != 0), I can't open files (SD.open always fails).

I wonder, if I need to use some other library?

*** Update: I tried the "standard" SD.h too, same result: SD.begin ok but SD.open fails on any file on the SD. Tried different SD cards as well

Is there an example of working SD code for Teensyduino 1.2.6 and Arduino IDE 1.6.5?

Or maybe I did the wiring wrong? I think using pin 10 to 13 should be OK actually...

Thanks for any help.
Honestly I was hoping to switch to Teensy more easily; it's my fault for sure, but maybe a Arduino->Teensy sketch migration guide for beginners could be useful...?

Fernando
 
Last edited:
sdfat has a bunch of example sketches and utility sketches. Try those.
Try formatting with the sdfat sketch, since occasionally it helps.

If you only need reading, then you should try Paul's (creator of teensy) sd library, because it uses SPI transactions correctly.

Switching to teensy is fairly easy, and when there's a problem, it's almost always straightforward. It will be even easier when the wiki is done.
 
Hi, thanks for replying

sdfat has a bunch of example sketches and utility sketches. Try those.
Try formatting with the sdfat sketch, since occasionally it helps.

In the meantime I tried the "bench.ino" from the SdFat package; it kind of works (recognizes the card, shows correct infos about capacity etc.), but it takes forever to perform the tests, and ends up with "9 KB/s" speed, which sounds wrong. :(

If you only need reading, then you should try Paul's (creator of teensy) sd library, because it uses SPI transactions correctly.

Unfortunately I have to write, too. It's a logging application: read configuration, write data.

Switching to teensy is fairly easy, and when there's a problem, it's almost always straightforward. It will be even easier when the wiki is done.

From what I'm seeing, is mostly an issue of properly wiring existing shields and using the appropriate libraries (ported to Teensy 3). But those tasks can be daunting without a beginner's guide.
Indeed a wiki would be welcome! In the meantime I'll keep digging the forum.

Thank you

Fernando
 
When something is "just barely" working (obviously, I/O bitrate to the card should be higher!) I wonder about marginal voltages. The old-style Arduino is +5V logic and the Teensy is 3.3V, and SD card I/O must be 3.3V. Is the 5->3V level translator that I presume is on the SD card shield not working as well when the controller signal is 3.3V instead of 5V?
 
The default SD library that ships with Teensyduino should work fine. It does support writing to the card. It also properly supports SPI transactions.

There is an experimental optimization, which is disabled by default. If you enable that, by editing SD_t3.h, reads become optimized but you lose writing. The only way to get that optimization is editing SD_t3.h. Someday the optimized version will gain writing support, and eventually it'll become the default for Teensy. But for now, it's not used unless you edit that file.

Teensyduino's SD library supports a single pin for the begin. Just use this:

Code:
SD.begin(10);

By default, 11, 12, 13 are used for MOSI, MISO, SCK. If you want to use the alternate pins, see the audio library examples for how to reconfigure SPI before SD.begin().

Teensyduino's SD library does not support choosing arbitrary pins. You have to use the SPI signals for data and clock.

The big caveat is whether you're really using the SD library from Teensyduino. It's in hardware/teensy/avr/libraries/SD. If you turn on verbose info, Arduino tells you the full pathname of every library it used. If you have multiple conflicting versions, it should alert you to the conflict and tell you which one it actually used. You really should use the one Teensyduino installs into hardware/teensy/avr/libraries/SD.
 
Thank you Paul; indeed there could have been some kind of library conflict.

I've reinstalled everyting from scratch, so now I have a (hopefully) clean build system; I decided to stick to the SdFat library, since so many users praise it.
Now I can read and write files; writes are still very slow, only a few KB/s (the card itself measures 3 MB/s on the PC; it's an oldish card, but not terrible), but it's doable since I read from XBee, not so high a bitrate.

I'm really loving Teensy 3.2. I think it will be my platform of choice from now on, so I'll better gain as much knowledge as I can. :)
 
Same issue!

Hello,

I have a Teensy 3.2 hooked up to a Adafruit Data Logging Shield (I'm migrating an existing project from Arduino Mega to Teensy)
Teensy is powered via USB.

Wiring:
Apart from the obvious +5V (splitted from USB +5V power) and +3.3V (wired to "3.3V 250mA Max"), I have:
IORef on 3.3V
ChipSelect on Pin10
MOSI on Pin11
MISO on Pin12
Clock on Pin13

I have Arduino IDE 1.6.5r5 and Teensyduino 1.2.6b1; host OS is Windows7-64.

On Arduino Mega, I used to load the Adafruit SD library (SD.h).

My initialization call was:

Code:
SD.begin(10, 11, 12, 13)

and I used to access files like that:

Code:
File myFile = SD.open("myfile.dat", FILE_READ);

and all was fine and dandy. :)

Now, the project won't even compile:

Code:
Sd2Card.cpp:30:17: error: 'RwReg' does not name a type
:static volatile RwReg *mosiport, *clkport, *misoport
Sd2Card.cpp:In function 'void spiSend(uint8_t)
Sd2Card.cpp:60:10: error: 'clkport' was not declared in this scope
:*clkport &= ~clkpinmask
Sd2Card.cpp:62:12: error: 'mosiport' was not declared in this scope
:*mosiport |= mosipinmask
Sd2Card.cpp:64:12: error: 'mosiport' was not declared in this scope
:*mosiport &= ~mosipinmask
Sd2Card.cpp:69:8: error: 'clkport' was not declared in this scope
:*clkport &= ~clkpinmask
Sd2Card.cpp:In function 'uint8_t spiRec()
Sd2Card.cpp:88:6: error: 'mosiport' was not declared in this scope
:*mosiport |= mosipinmask
Sd2Card.cpp:91:8: error: 'clkport' was not declared in this scope
:*clkport |=  clkpinmask
Sd2Card.cpp:95:13: error: 'misoport' was not declared in this scope
:if ((*misoport) & misopinmask)  data |= 1
Sd2Card.cpp:In member function 'uint8_t Sd2Card::init(uint8_t, uint8_t, int8_t, int8_t, int8_t)
Sd2Card.cpp:302:5: error: 'clkport' was not declared in this scope
:clkport     = portOutputRegister(digitalPinToPort(clockPin_))
Sd2Card.cpp:304:5: error: 'mosiport' was not declared in this scope
:mosiport    = portOutputRegister(digitalPinToPort(mosiPin_))
Sd2Card.cpp:306:5: error: 'misoport' was not declared in this scope
:misoport    = portInputRegister(digitalPinToPort(misoPin_))

I thought "Too bad, Adafruit's SD library is not compatibile with Teensyduino :( ", so I tried to switch to "SdFat" library:

Code:
#include <SdFat.h>
SdFat SD;

and changed the initialization code to:

Code:
SD.begin(SD_CHIPSELECT)

but now, while SD.begin apparently works (result != 0), I can't open files (SD.open always fails).

I wonder, if I need to use some other library?

*** Update: I tried the "standard" SD.h too, same result: SD.begin ok but SD.open fails on any file on the SD. Tried different SD cards as well

Is there an example of working SD code for Teensyduino 1.2.6 and Arduino IDE 1.6.5?

Or maybe I did the wiring wrong? I think using pin 10 to 13 should be OK actually...

Thanks for any help.
Honestly I was hoping to switch to Teensy more easily; it's my fault for sure, but maybe a Arduino->Teensy sketch migration guide for beginners could be useful...?

Fernando

I'm having the same problem and generated the same errors with the standard SD library. Was there a fix other than switching to the SdFat library? It sounds like the SD library should work. I'm lost on this one, so any help would be appreciated!
 
Same problem? That would be fixed the same way as above. If you read the thread and posts it suggested doing some of the alternate samples and reformatting in post 2.

Did you try verbose and get added info? Re-install? Post the specifics of your findings - IDE compile errors or sketch tried and problems encountered.
 
Same problem? That would be fixed the same way as above. If you read the thread and posts it suggested doing some of the alternate samples and reformatting in post 2.

Did you try verbose and get added info? Re-install? Post the specifics of your findings - IDE compile errors or sketch tried and problems encountered.

Thanks - believe it or not I got almost the same errors. I was using the standard CardInfo demo file from the SD library.
Here's the dump:

Arduino: 1.6.5 (Mac OS X), TD: 1.29, Board: "Teensy 3.2 / 3.1, Serial, 96 MHz optimized (overclock), US English"

/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:30:17: error: 'RwReg' does not name a type
static volatile RwReg *mosiport, *clkport, *misoport;
^
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp: In function 'void spiSend(uint8_t)':
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:60:10: error: 'clkport' was not declared in this scope
*clkport &= ~clkpinmask;
^
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:62:12: error: 'mosiport' was not declared in this scope
*mosiport |= mosipinmask;
^
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:64:12: error: 'mosiport' was not declared in this scope
*mosiport &= ~mosipinmask;
^
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:69:8: error: 'clkport' was not declared in this scope
*clkport &= ~clkpinmask;
^
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp: In function 'uint8_t spiRec()':
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:88:6: error: 'mosiport' was not declared in this scope
*mosiport |= mosipinmask;
^
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:91:8: error: 'clkport' was not declared in this scope
*clkport |= clkpinmask;
^
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:95:13: error: 'misoport' was not declared in this scope
if ((*misoport) & misopinmask) data |= 1;
^
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp: In member function 'uint8_t Sd2Card::init(uint8_t, uint8_t, int8_t, int8_t, int8_t)':
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:302:5: error: 'clkport' was not declared in this scope
clkport = portOutputRegister(digitalPinToPort(clockPin_));
^
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:304:5: error: 'mosiport' was not declared in this scope
mosiport = portOutputRegister(digitalPinToPort(mosiPin_));
^
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:306:5: error: 'misoport' was not declared in this scope
misoport = portInputRegister(digitalPinToPort(misoPin_));
^
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp: In member function 'uint8_t Sd2Card::readData(uint32_t, uint16_t, uint16_t, uint8_t*)':
^
/Users/test/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:438:12: warning: unused variable 'n' [-Wunused-variable]
uint16_t n;
^
Error compiling.

I'm wondering if I'm not actually getting the correct library. I've been working on Arduino Unos and Dues, but this is my first foray into Teensy and I'd love to get it to work like my Due code.
 
At a glance those errors suggest the library or sketch in use is incomplete or counting on other system defines that are not present. My system only shows those SPI variables in Adafruit/AVR libraries - none are from the SD library. The posts above mention that the TD SD library should be used. That would be "teensy\avr\libraries\SD\examples\CardInfo" from Teensy - but I don't know how the files are stored under MAC.

I got the path above hitting "Ctrl+K" in the IDE on the open EXAMPLES sketch - it points into my TD install.

First best guess, that helped the OP - would be to do a fresh install (1.6.11) and get the latest TeensyDuino 1.30b3, unless you have need to stay on the older 1.6.5 version. And check for conflicting libraries in 'sketchbook'\libraries. Newer IDE versions better display the libraries in use and point out conflicts. Also if there is a problem in TD - it can be addressed in the newer version.

Have you tried other samples with the same errors showing up?
 
Status
Not open for further replies.
Back
Top