Try SdFat forTeensy 3.5/3.6

Status
Not open for further replies.

Bill Greiman

Well-known member
SdFat-beta now supports SDHC for the Teensy 3.5/3.6 on-board microSD.

Try the TeensySdioDemo example.

The bench, SdFormatter, and SdInfo examples can be run on Teensy 3.6 by defining the symbol USE_SDIO to be non-zero.

Many other examples will work if you use the SdFatSdio class in place of the Sdfat class and call begin() with no parameters.

Code:
// Replace SdFat with SdFatSdio.
// Sdfat sd;
SdFatSdio sd;

  // Remove parameters from begin().
  //  if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  if (!sd.begin()) {
    sd.initErrorHalt();
  }
 
Great..works!

Code:
pre-production Teensy 3.5 @ 120MHz

size,write,read
bytes,KB/sec,KB/sec
512,407.27,2004.32
1024,517.30,2579.62
2048,1375.49,4580.52
4096,2939.62,7467.61
8192,4595.34,11019.66    
16384,11838.28,14052.92
32768,14085.65,16246.76

totalMicros  61161203
yieldMicros  60484683
yieldCalls   81352
yieldMaxUsec 579390
Done

pre-production Teensy 3.6 @ 240MHz

size,write,read
bytes,KB/sec,KB/sec
512,407.80,2071.13
1024,530.48,2680.34
2048,1406.73,4771.54
4096,3474.70,7942.13
8192,8722.41,11949.21
16384,13145.07,15928.06
32768,15977.66,18875.02

totalMicros  58553159
yieldMicros  58200508
yieldCalls   81365
yieldMaxUsec 20438
Done
 
@Bill, i have a question,

I had to run the tests above with a brand-new card (sandisk-ultra 8GB), because my old card (sandisk-ultra 16GB), which i use often for arduino-experiments or audio with teensy shows ("2048,1044.90,error: read failed").

I've never experienced problems with the 16-GB Card. Is it possible, that the card is broken, and I've not noticed it before ?
Or is it a bug ?
The card was fresh-fromatted with the "official" SDFORMAT-tool.
 
@Bill, i have a question,

I had to run the tests above with a brand-new card (sandisk-ultra 8GB), because my old card (sandisk-ultra 16GB), which i use often for arduino-experiments or audio with teensy shows ("2048,1044.90,error: read failed").

I've never experienced problems with the 16-GB Card. Is it possible, that the card is broken, and I've not noticed it before ?
Or is it a bug ?
The card was fresh-fromatted with the "official" SDFORMAT-tool.

A bug is certainly possible.

I have a 16GB and 32GB SanDisk ultra and the demo seems to run at 240 MHz. I added a SDCLK frequency print to the demo and updated GitHub.

Code:
16 GB ultra at 240 MHz.

size,write,read
bytes,KB/sec,KB/sec
512,351.74,2386.14
1024,362.84,2908.13
2048,931.30,5130.56
4096,2118.74,7931.93
8192,4379.89,12542.66
16384,7416.31,16387.65
32768,9031.42,18966.99

totalMicros  74625679
yieldMicros  74279767
yieldCalls   80858
yieldMaxUsec 78799
kHzSdClk     48000

Code:
32GB ultra at 240 MHz.

size,write,read
bytes,KB/sec,KB/sec
512,402.25,1968.08
1024,509.35,2581.77
2048,1341.43,4619.01
4096,3194.47,7783.48
8192,8097.63,11792.35
16384,12312.45,15664.71
32768,15017.98,18703.20

totalMicros  60579274
yieldMicros  60237195
yieldCalls   81452
yieldMaxUsec 27222
kHzSdClk     48000

One thing to check is if the card CID look OK. Fake SanDisk cards are common.

A PC or other device never writes a big file as many single block writes at high speed. It really punishes the card since it must copy and reprogram a big block of flash for every write. The internal flash blocks are 16K or 32K. I am running the card at 48MHz. The uSDFS library probably ran at 24 MHz.

Run the SdIno example on the card. The result should look like this:
Code:
Manufacturer ID: 0X3
OEM ID: SD
Product: SL16G
Version: 8.0
Serial number: 0X91203A25
Manufacturing date: 4/2014

cardSize: 15931.54 MB (MB = 1,000,000 bytes)
flashEraseSize: 128 blocks
eraseSingleBlock: true
OCR: 0XC0FF8000

SD Partition Table
part,boot,type,start,length
1,0X0,0XC,8192,31108096
2,0X0,0X0,0,0
3,0X0,0X0,0,0
4,0X0,0X0,0,0

Volume is FAT32
blocksPerCluster: 64
clusterCount: 485936
freeClusters: 485677
freeSpace: 15914.66 MB (MB = 1,000,000 bytes)
fatStartBlock: 8790
fatCount: 2
blocksPerFat: 3797
rootDirStart: 2
dataStartBlock: 16384
The Manufacturer ID, OEM ID, Product, and version should be the same.

Did you format it with SD Formatter?

There is only one standard format for an SD card. The size and position of everything is specified by the SD Association.

Edit: Here is a Samsung 32GB Pro+. It handles small writes better.
Code:
Samsung Pro+ 240 MHz.

size,write,read
bytes,KB/sec,KB/sec
512,598.73,2204.46
1024,1141.81,3212.32
2048,2210.54,5370.53
4096,3760.99,8140.06
8192,4266.42,9312.60
16384,7670.57,13767.16
32768,12962.17,15782.66

totalMicros  42140441
yieldMicros  41846388
yieldCalls   78584
yieldMaxUsec 53812
kHzSdClk     48000
 
Last edited:
Hi Bill, yes i used the "SD FORMATTER" tool.

Code:
SdFat version: 20160905 

 Assuming the SD is the only SPI device.

 Edit DISABLE_CHIP_SELECT to disable another device.

 

 Assuming the SD chip select pin is: 10

 Edit SD_CHIP_SELECT to change the SD chip select pin.

 

 type any character to start

 

 init time: 88 ms

 

 Card type: SDHC

 

 Manufacturer ID: 0X3

 OEM ID: SD

 Product: SU16G

 Version: 8.0

 Serial number: 0XE51C7A05

 Manufacturing date: 2/2013
 

 cardSize: 15931.54 MB (MB = 1,000,000 bytes)
 flashEraseSize: 128 blocks
 eraseSingleBlock: true

 OCR: 0XC0FF8000
 

 SD Partition Table
 part,boot,type,start,length
 1,0X0,0XC,8192,31093648

 2,0X0,0X0,0,0
 3,0X0,0X0,0,0
 4,0X0,0X0,0,0
 

 Volume is FAT32

 blocksPerCluster: 64
 clusterCount: 485710
 freeClusters: 484128
 freeSpace: 15863.91 MB (MB = 1,000,000 bytes)
 fatStartBlock: 8794

 fatCount: 2
 blocksPerFat: 3795
 rootDirStart: 2
 dataStartBlock: 16384
 
I ran on 3.5 at 120mhz Transcend 4GB card that I formated with your formater

Code:
size,write,read
bytes,KB/sec,KB/sec
512,288.51,1506.30
1024,473.70,2129.27
2048,910.38,3737.27
4096,1585.79,5906.54
8192,2358.80,8297.50
16384,3893.78,11527.31
32768,7707.44,14256.45

totalMicros  83588189
yieldMicros  83091897
yieldCalls   69354
yieldMaxUsec 389826
Done

SDInfo...
Code:
nit time: 160 ms

Card type: SDHC

Manufacturer ID: 0X74
OEM ID: JE
Product: USD  
Version: 1.0
Serial number: 0XC21B974E
Manufacturing date: 6/2013

cardSize: 4035.97 MB (MB = 1,000,000 bytes)
flashEraseSize: 128 blocks
eraseSingleBlock: true
OCR: 0XC0FF8000

SD Partition Table
part,boot,type,start,length
1,0X0,0XB,8192,7874560
2,0X0,0X0,0,0
3,0X0,0X0,0,0
4,0X0,0X0,0,0

Volume is FAT32
blocksPerCluster: 64
clusterCount: 122912
freeClusters: 122655
freeSpace: 4019.16 MB (MB = 1,000,000 bytes)
fatStartBlock: 14462
fatCount: 2
blocksPerFat: 961
rootDirStart: 2
dataStartBlock: 16384
 
Last edited:
Hi Bill, yes i used the "SD FORMATTER" tool.

That looks like a real SanDisk card but is internally different than my 4/2014 card. Different number of blocks.

SanDisk used a product: SU16G for a full size 5/2013 card that I have.

Did the failure have this print out?
Code:
size,write,read
bytes,KB/sec,KB/sec
512,397.21,error: read failed
SD errorCode: 0X31,0X20002B

The last line would help.
 
Hi Bill,

i have different outputs. The first time, I got this:
Code:
size,write,read bytes,KB/sec,KB/sec

 512,353.42,1165.17
 1024,638.93,2371.80
 2048,1095.24,error: read failed
 SD errorCode: 0X31,0X100021]

now, the output is:
Code:
size,write,read 
bytes,KB/sec,KB/sec
 512,345.43,1300.57
 1024,645.00,error: data check

And it's slow.
But i tried to copy a big file to the card (from PC) - this worked. And the file seems to be ok.

Hope this helps :)
 
FYI I pulled out another micro SD card (Sandisk ultra 32) Ran formatter, now Teensy test. 120mhz T3.5
Code:
Type any character to begin

size,write,read
bytes,KB/sec,KB/sec
512,470.59,2055.11
1024,1300.00,2573.00
2048,2687.51,4620.25
4096,4854.93,7748.13
8192,6580.27,11362.69
16384,7692.58,14468.85
32768,11093.02,16577.95

totalMicros  44313493
yieldMicros  43742474
yieldCalls   66930
yieldMaxUsec 55530
Done
SDInfo:
Code:
SdFat version: 20160905

Assuming the SD is the only SPI device.
Edit DISABLE_CHIP_SELECT to disable another device.

Assuming the SD chip select pin is: 10
Edit SD_CHIP_SELECT to change the SD chip select pin.

type any character to start

init time: 10 ms

Card type: SDHC

Manufacturer ID: 0X3
OEM ID: SD
Product: SE32G
Version: 8.0
Serial number: 0XBD036D00
Manufacturing date: 3/2015

cardSize: 31914.98 MB (MB = 1,000,000 bytes)
flashEraseSize: 128 blocks
eraseSingleBlock: true
OCR: 0XC0FF8000

SD Partition Table
part,boot,type,start,length
1,0X0,0XC,8192,62325760
2,0X0,0X0,0,0
3,0X0,0X0,0,0
4,0X0,0X0,0,0

Volume is FAT32
blocksPerCluster: 64
clusterCount: 973584
freeClusters: 973327
freeSpace: 31893.98 MB (MB = 1,000,000 bytes)
fatStartBlock: 9362
fatCount: 2
blocksPerFat: 7607
rootDirStart: 2
dataStartBlock: 24576

type any character to start

Ran same test on T3.6 180mhz and:
Code:
Type any character to begin

size,write,read
bytes,KB/sec,KB/sec
512,473.47,2029.61
1024,1326.97,2556.80
2048,2708.84,4700.57
4096,4992.95,8555.75
8192,6967.93,12123.68
16384,8028.14,15671.06
32768,11759.08,18317.06

totalMicros  43642294
yieldMicros  43337856
yieldCalls   66941
yieldMaxUsec 52259
Done
Now back to some other stuff unless you see something else you would like run.
 
Frank,

The first error is a "Read Data time-out", the 0X100000 bit. The command was read-multiple blocks. The card responded that it accepted the command. There is no code during this phase, it's all done by the SDHC controller.

I have the data timeout set to the highest possible value, 2^27 SD clocks. The manual states:

Time-out clock frequency will be generated by dividing the base clock SDCLK value by this value.

2^27 = 134,217,728 so with SDCLK of 48 MHz the time-out should be 2-3 seconds?

The second case is another puzzle. Read data didn't match write data. The data is CRC protected so I suspect a bug.

If it is a bug, it will bite more people.

Hope I get something I can reproduce with my Teensy.

I don't think the success with the PC helps. The PC reads/writes the file with huge multi-block transfers. That's way cards are designed to work.
 
Works for me - Thanks. Got a NEW TYQT - going to try that integrated and the SDFAT code on T_3.5 and T_3.6 - a fresh 64GB SanDisk Ultra (up to 80MB/s) card.
tyqt_multiT.PNG
TYQT is better than ever (feature wise - and getting up to speed on T_3.5 and T_3.6 - another picture on that thread

T_3.5 at 120MHz before formatting
init time: 8 ms

Card type: SDXC

Manufacturer ID: 0X3
OEM ID: PT
Product: SL64G
Version: 3.0
Serial number: 0X45813CDA
Manufacturing date: 4/2010

cardSize: 62587.40 MB (MB = 1,000,000 bytes)
flashEraseSize: 128 blocks
eraseSingleBlock: true
OCR: 0XC0FF8000

SD Partition Table
part,boot,type,start,length
1,0X0,0X7,32768,122208256
2,0X0,0X0,0,0
3,0X0,0X0,0,0
4,0X0,0X0,0,0
error:
File System initialization failed.

SdFormatter.ino with "F" full erase and format:
Card Size: 62587 MB, (MB = 1,000,000 bytes)

Erasing
................................
{ ... }

All data set to 0xff
Erase done

Formatting
Blocks/Cluster: 128
FAT32
...........................................................
Format done

And now instead of 'File System initialization failed'::
Volume is FAT32
blocksPerCluster: 128
clusterCount: 954816
freeClusters: 954815
freeSpace: 62574.75 MB (MB = 1,000,000 bytes)
fatStartBlock: 9656
fatCount: 2
blocksPerFat: 7460
rootDirStart: 2
dataStartBlock: 24576

Slower then Kurt's - and 5 years older - but NEW?::
SANDISK 64GB
size,write,read
bytes,KB/sec,KB/sec
512,234.90,1007.21
1024,447.24,1856.83
2048,836.20,3050.88
4096,1540.81,5861.62
8192,2863.72,9075.67
16384,5365.59,12488.38
32768,7069.82,15069.10

totalMicros 94802082
yieldMicros 94269851
yieldCalls 65170
yieldMaxUsec 152149

16GB ADATA
size,write,read
bytes,KB/sec,KB/sec
512,438.24,2322.88
1024,717.08,2929.71
2048,1604.94,4752.64
4096,4006.46,8589.21
8192,5526.66,11855.99
16384,7538.85,14834.39
32768,10189.03,16829.93

totalMicros 52602064
yieldMicros 51918565
yieldCalls 81388
yieldMaxUsec 54592

And the ADATA SdInfo on T_3.5::
init time: 20 ms

Card type: SDHC

Manufacturer ID: 0X3
OEM ID: SD
Product: SL16G
Version: 8.0
Serial number: 0X74F3B993
Manufacturing date: 10/2015

cardSize: 15931.54 MB (MB = 1,000,000 bytes)
flashEraseSize: 128 blocks
eraseSingleBlock: true
OCR: 0XC0FF8000

SD Partition Table
part,boot,type,start,length
1,0X0,0XC,8192,31108096
2,0X0,0X0,0,0
3,0X0,0X0,0,0
4,0X0,0X0,0,0

Volume is FAT32
blocksPerCluster: 64
clusterCount: 485936
freeClusters: 483447
freeSpace: 15841.59 MB (MB = 1,000,000 bytes)
fatStartBlock: 8790
fatCount: 2
blocksPerFat: 3797
rootDirStart: 2
dataStartBlock: 16384

NOW in the T_3.6 at 180MHz first a 16GB ADATA card then the SanDisk Ultra::
16GB ADATA
size,write,read
bytes,KB/sec,KB/sec
512,356.95,1962.57
1024,623.52,2767.51
2048,1259.50,4965.47
4096,3751.84,8830.67
8192,5948.75,12874.18
16384,8237.51,16366.39
32768,9409.79,18374.23

totalMicros 60736158
yieldMicros 60317961
yieldCalls 81376
yieldMaxUsec 98508

SANDISK 64GB
size,write,read
bytes,KB/sec,KB/sec
512,235.11,1019.75
1024,447.70,1863.53
2048,854.03,3050.03
4096,1518.74,6181.29
8192,2891.15,9593.66
16384,5682.16,13461.99
32768,7555.32,16508.43

totalMicros 94091637
yieldMicros 94026352
yieldCalls 65177
yieldMaxUsec 143522
 
Reading above I'm wondering is my slow "SanDisk Ultra" - in nice retail packaging is fake? IIRC this card was new on Amazon Prime Day:
Card type: SDXC

Manufacturer ID: 0X3
OEM ID: PT
Product: SL64G
Version: 3.0
Serial number: 0X45813CDA
Manufacturing date: 4/2010

The ADATA card shows this SdInfo:
Card type: SDHC

Manufacturer ID: 0X3
OEM ID: SD
Product: SL16G
Version: 8.0
Serial number: 0X74F3B993
Manufacturing date: 10/2015
 
Frank,

The first error is a "Read Data time-out", the 0X100000 bit. The command was read-multiple blocks. The card responded that it accepted the command. There is no code during this phase, it's all done by the SDHC controller.

I have the data timeout set to the highest possible value, 2^27 SD clocks. The manual states:



2^27 = 134,217,728 so with SDCLK of 48 MHz the time-out should be 2-3 seconds?

The second case is another puzzle. Read data didn't match write data. The data is CRC protected so I suspect a bug.

If it is a bug, it will bite more people.

Hope I get something I can reproduce with my Teensy.

I don't think the success with the PC helps. The PC reads/writes the file with huge multi-block transfers. That's way cards are designed to work.

Bill, would it help to send you the card ?
btw, i made a quick'n dirty video-player with the help of your library: https://forum.pjrc.com/threads/36751-Teensy-3-6-plays-video-from-SD-Card?p=114396#post114396
 
Slowest Card Ever!

Here are the results from a 2 Gb MicroSD card that Verizon sold my daughter for $24.00! when she went to pick up her phone upgrade.

Code:
Teensy 3.6 @ 240 MHz

[B]TeensySdioDemo:[/B]

size,write,read
bytes,KB/sec,KB/sec
512,80.21,391.42
1024,151.88,499.64
2048,280.97,990.58
4096,490.71,1946.40
8192,2523.27,3297.18
16384,3522.41,5357.96
32768,4538.04,7980.36

totalMicros  270475259
yieldMicros  270288346
yieldCalls   80228
yieldMaxUsec 193358
kHzSdClk     48000
Done

[B]SdInfo[/B]:

init time: 9 ms

Card type: SD2

Manufacturer ID: 0X1B
OEM ID: SM
Product: 00000
Version: 1.0
Serial number: 0XAD133826
Manufacturing date: 11/2010

cardSize: 2002.78 MB (MB = 1,000,000 bytes)
flashEraseSize: 64 blocks
eraseSingleBlock: true
OCR: 0X80FF8000

SD Partition Table
part,boot,type,start,length
1,0X0,0X6,129,3911551
2,0X0,0X0,0,0
3,0X0,0X0,0,0
4,0X0,0X0,0,0

Volume is FAT16
blocksPerCluster: 64
clusterCount: 61110
freeClusters: 60852
freeSpace: 1994.00 MB (MB = 1,000,000 bytes)
fatStartBlock: 130
fatCount: 2
blocksPerFat: 239
rootDirStart: 608
dataStartBlock: 640
 
Last edited:
Bill, would it help to send you the card ?
btw, i made a quick'n dirty video-player with the help of your library: https://forum.pjrc.com/threads/36751-Teensy-3-6-plays-video-from-SD-Card?p=114396#post114396

I did some more research and both failures can happen with a bus error. The CRC for read is checked in the SDHC controller so a bus error can cause the data compare error after the CRC is checked.

The reference manual also says a bus error can cause the time-out.

I would like you to try the card at 180 MHz. The overclocking at 240 MHz plus a marginal card may cause the problem.
 
Reading above I'm wondering is my slow "SanDisk Ultra" - in nice retail packaging is fake? IIRC this card was new on Amazon Prime Day:

The ADATA card shows this SdInfo:

The first card is not a real SanDisk card. I have seen this CID before.

The second card appear to be manufactured by SanDisk.

In the future, probably in a few months, I am going to add exFAT support to SdFat. Then you won't need to format SDXC cards as FAT32. I will be very busy until mid November and then I need to tidy up SdFat before a major new development.
 
Tried SdFat on K64 and K66 with 8GB SanDisk uSD. Sdinfo good. directory listing good.;)
Code:
size,write,read
bytes,KB/sec,KB/sec
512,264.69,1656.10
1024,253.67,2452.38
2048,655.89,3185.37
4096,1498.75,5516.23
8192,3177.63,9383.29
16384,5595.39,13001.24
32768,6270.75,15747.96

totalMicros  103336131
yieldMicros  102946810
yieldCalls   80610
yieldMaxUsec 32989
kHzSdClk     45000
Done
 
Does the sd.h wrapper work with this version sdfat ?
I'd like to try some audio..

I am not sure what features you need.

You should be able to run an SD.h program with SDIO on Teensy 3.5/3.6 by making the following changes.

Code:
// replace SD.h inlclude like this.
// #include SD.h"
#include "SdFat.h"
SdFatSdio SD;

 ...

  // remove chip select pin parameter from begin()
  // if (!SD.begin(CD_PIN)) {
  if (!SD.begin() {
    // handle error here.
  }

The main feature missing from the SDIO driver vs the SPI driver is extended multi-block read/write.

For your failing card, hang on to it. If someone else has a similar problem, I will need to decide whether to have you send it to me or have you run some test.

I would like to have more than one case before I spend a lot of time. I have tested about a dozen cards without a problem and quite a variety have now been tested by other users.
 
Hi Bill,

no big problem with card. Others work, so everything is ok.
Thank you for the hint with sd.h

Another question: It seems to me, that your lib does not read large files ? Is that correct or my fault :) ?
It stops after reading 256MB..
 
sorry!!!! it was a problem with the file..or better a problem with "processing" which created the file.
 
FYI, I have updated a K66 beta post comparing uSDFS with SdFat SDIO and with SPI SD, see
https://forum.pjrc.com/threads/34808-K66-Beta-Test?p=108170&viewfull=1#post108170

The best performance for small, 512 byte, transfers on K66 is with the SdFatEX class using SPI. You must edit SdFatConfig.h to enable SdFatEX. SdFat default configuration is for the most popular Uno.

https://forum.pjrc.com/threads/36331-Teensy-3-6-microSD-pins

The read and write rates are both about 3,200 KB/sec.

I hope to do a similar SDIO driver to get higher rate for small SDIO transfers.

For very large transfers the current SdFat will be faster than uSDFS. SdFat use high speed mode SDIO at 45 MHz on a 180 MHz K66. uSDFS is limited to 22.5 MHz.

For small transfers the high speed mode actually seem to be slower.
 
Last edited:
Status
Not open for further replies.
Back
Top