11/*
22 * This program attempts to initialize an SD card and analyze its structure.
3+ * The CID and CSD registers are also printed in HEX for use in online
4+ * decoders like these.
5+ *
6+ * https://gurumeditation.org/1342/sd-memory-card-register-decoder/
7+ * https://archive.goughlui.com/static/multicid.htm
38 */
49#include " SdFat.h"
510#include " sdios.h"
@@ -55,7 +60,8 @@ void cidDmp() {
5560 cout << F (" Serial number: " ) << hex << cid.psn () << dec << endl;
5661 cout << F (" Manufacturing date: " );
5762 cout << cid.mdtMonth () << ' /' << cid.mdtYear () << endl;
58- cout << endl;
63+ cout << F (" CID HEX: " );
64+ hexDmp (&cid, sizeof (cid));
5965}
6066// ------------------------------------------------------------------------------
6167void clearSerialInput () {
@@ -69,7 +75,7 @@ void clearSerialInput() {
6975// ------------------------------------------------------------------------------
7076void csdDmp () {
7177 eraseSize = csd.eraseSize ();
72- cout << F (" cardSize : " ) << 0.000512 * csd.capacity ();
78+ cout << F (" \n cardSize : " ) << 0.000512 * csd.capacity ();
7379 cout << F (" MB (MB = 1,000,000 bytes)\n " );
7480
7581 cout << F (" flashEraseSize: " ) << int (eraseSize) << F (" blocks\n " );
@@ -85,6 +91,8 @@ void csdDmp() {
8591 } else {
8692 cout << F (" zeros\n " );
8793 }
94+ cout << F (" CSD HEX: " );
95+ hexDmp (&csd, sizeof (csd));
8896}
8997// ------------------------------------------------------------------------------
9098void errorPrint () {
@@ -96,18 +104,27 @@ void errorPrint() {
96104 }
97105}
98106// ------------------------------------------------------------------------------
107+ void hexDmp (void * reg, uint8_t size) {
108+ uint8_t * u8 = reinterpret_cast <uint8_t *>(reg);
109+ cout << hex << noshowbase;
110+ for (size_t i = 0 ; i < size; i++) {
111+ cout << setw (2 ) << setfill (' 0' ) << int (u8 [i]);
112+ }
113+ cout << dec << endl;
114+ }
115+ // ------------------------------------------------------------------------------
99116bool mbrDmp () {
100117 MbrSector_t mbr;
101118 bool valid = true ;
102- if (!sd.card ()->readSector (0 , (uint8_t *)&mbr)) {
119+ if (!sd.card ()->readSector (0 , (uint8_t *)&mbr)) {
103120 cout << F (" \n read MBR failed.\n " );
104121 errorPrint ();
105122 return false ;
106123 }
107124 cout << F (" \n SD Partition Table\n " );
108125 cout << F (" part,boot,bgnCHS[3],type,endCHS[3],start,length\n " );
109126 for (uint8_t ip = 1 ; ip < 5 ; ip++) {
110- MbrPart_t * pt = &mbr.part [ip - 1 ];
127+ MbrPart_t* pt = &mbr.part [ip - 1 ];
111128 if ((pt->boot != 0 && pt->boot != 0X80 ) ||
112129 getLe32 (pt->relativeSectors ) > csd.capacity ()) {
113130 valid = false ;
@@ -242,7 +259,7 @@ void loop() {
242259 printCardType ();
243260 cout << F (" sdSpecVer: " ) << 0.01 * scr.sdSpecVer () << endl;
244261 cout << F (" HighSpeedMode: " );
245- if (scr.sdSpecVer () && sd.card ()->cardCMD6 (0X00FFFFFF , cmd6Data) &&
262+ if (scr.sdSpecVer () > 101 && sd.card ()->cardCMD6 (0X00FFFFFF , cmd6Data) &&
246263 (2 & cmd6Data[13 ])) {
247264 cout << F (" true\n " );
248265 } else {
0 commit comments