Using SDFat Directly to Access Partitioned SD Cards


Senior Member+
While playing around with another project realized that I pretty much forgot how to use the Teensy SDFat that comes with Teensyduino to access the partitions on a SD card. Pretty always use MTP responder when I wanted to do stuff like that or just use a single partitioned drive. So spent the last couple of days remembering and putting together a sketch that would jog my memory.

Below is a sample run from the sketch:
First that appears is the menu
Menu Options:
    I - Initialize Drive
    1 - List Partitions (Step 1)
    2# - Select Drive # for Logging (Step 2)
    l - List files on selected partition
    L - List files on all partitions
    s - Start Logging data (Restarting logger will append records to existing log)
    x - Stop Logging data
    d - Dump Log
    r - Reset

    'S, or t': Make 2MB file , or remove all 2MB files    h - Menu
Note: didn't implement r, S, t yet.....

First thing you do is select 'I' to initialize the SD Card:
Card size: 63.86 GB (GB = 1E9 bytes)
Card size: 59.48 GiB (GiB = 2^30 bytes)

Card type: SDXC
cardSize: 63864.57 MB (MB = 1,000,000 bytes)
flashEraseSize: 128 blocks
eraseSingleBlock: true

Next think is you have to select '1' to populate and display the partition table so you can tell the sketch what partition you want to use.
SD Partition Table
     part, boot, bgnCHS[3], type, endCHS[3], start, length
(2)FAT32:    1,0,0x21,0x22,0x0,0xB,0xFE,0xFF,0xFF,2112,62377984
(3)exFAT:    2,0,0xFE,0xFF,0xFF,0x7,0xFE,0xFF,0xFF,62382144,62351424
pt_#0:    3,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0
pt_#0:    4,0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0,0

Next up is to tell it what partition you want to use, in the case of the exFat partition I would say '22', first 2 is the option, second 2 is the partition number.
Storage Index 2 Selected of 2[/CODE\
this confirms that you are using the second partition of the 2 partitions available.

If you select 's' you can start the datalogger and 'x' will stop logging data.
[CODE]Logging Data!!!
File opened!!!
.... etc

'd' will dump the data and 'l' will list the directory:
Listing files on storage Index: 2
 Space Used = 20447232
Filesystem Size = 31918686208

2023-08-30 09:38         4004 kv5.txt
2023-09-11 07:03      1032322 1-Ground_SAI2016.pdf
2023-12-03 12:22        32768 New folder/
  2023-05-10 08:24      4810864 47690335362_a9b23dc6c8_o.jpeg
  2097-12-31 23:00         6492
  2097-12-31 23:00         8277
  2097-12-31 23:00         1602
  2021-01-01 10:25         4879 D435 Obs-slam Links.txt
  2020-02-19 20:17        42388 ER-TFTM050-3.png
  2021-04-01 18:13        11286 FlashChipWiring.PNG
  2019-01-01 00:02      4810968 jwst-sideview.jpg
  2023-04-07 17:00         2036 LITF.txt
  2023-04-27 19:20       335766 magn.txt
  2020-01-01 11:06       306892 MPU-9250-Accel-Gyro-and-Mag-Module-Connections.jpg
  2019-01-01 00:13      8393728 SDTEST1.raw
2023-01-01 00:00          245 datalog.txt

'L' would list the directories all partitions including the partition name.



    5.3 KB · Views: 11
Great stuff.

As you mentioned, before we were always doing it using MTP and the like!