image/svg+xml
Brteve's API for EveApps
Version 0.0.1
The reference document for common folder of EveApps project.
 
Loading...
Searching...
No Matches
sdcard Class Reference

#include <sdcard.h>

Public Member Functions

void sel ()
 
void desel ()
 
void sd_delay (byte n)
 
void cmd (byte cmd, uint32_t lba=0, uint8_t crc=0x95)
 
byte R1 ()
 
byte sdR3 (uint32_t &ocr)
 
byte sdR7 ()
 
void appcmd (byte cc, uint32_t lba=0)
 
uint8_t begin (byte p)
 
void cmd17 (uint32_t off)
 
void rdn (byte *d, uint32_t off, uint16_t n)
 
uint32_t rd4 (uint32_t off)
 
uint16_t rd2 (uint32_t off)
 
byte rd (uint32_t off)
 

Data Fields

byte pin
 
byte ccs
 
byte type
 
uint16_t sectors_per_cluster
 
uint16_t reserved_sectors
 
uint16_t max_root_dir_entries
 
uint16_t sectors_per_fat
 
uint16_t cluster_size
 
uint32_t root_dir_first_cluster
 
uint32_t o_partition
 
uint32_t o_fat
 
uint32_t o_root
 
uint32_t o_data
 

Detailed Description

Definition at line 57 of file sdcard.h.

Member Function Documentation

◆ appcmd()

void sdcard::appcmd ( byte  cc,
uint32_t  lba = 0 
)
inline

Definition at line 123 of file sdcard.h.

123 {
124 cmd(55); R1();
125 cmd(cc, lba);
126 }
static ft_void_t ft_uint32_t * cmd
Definition FT_Gpu_Hal.h:184
byte R1()
Definition sdcard.h:89

◆ begin()

uint8_t sdcard::begin ( byte  p)
inline

Definition at line 128 of file sdcard.h.

129 {
130 uint16_t cts=2000;
131 pin = p;
132
133 pinMode(pin, OUTPUT);
134 SPI.begin();
135 SPI.setClockDivider(SPI_CLOCK_DIV64);
136 desel();
137
138 delay(10); // wait for boot
139 sd_delay(10); // deselected, 80 pulses
140
141 // Tty.printf("Attempting card reset... ");
142 // attempt reset
143 byte r1,r;
144 do
145 { // reset, enter idle
146 cmd(0);
147 do
148 {
149 if(cts>0) cts--; else
150 {
151 desel();
152 return 0;
153 }
154 }while ((r = SPI.transfer(0xff)) & 0x80);
155 desel();
156 SPI.transfer(0xff); // trailing byte
157 r1 = r;
158 }while(r1!=1);
159 // Tty.printf("reset ok\n");
160
161 byte sdhc = 0;
162 cmd(8, 0x1aa, 0x87);
163 r1 = sdR7();
164 sdhc = (r1 == 1);
165
166 // Tty.printf("card %s SDHC\n", sdhc ? "is" : "is not");
167
168 // Tty.printf("Sending card init command... ");
169 while (1) {
170 appcmd(41, sdhc ? (1UL << 30) : 0); // card init
171 r1 = R1();
172 if ((r1 & 1) == 0)
173 break;
174 delay(100);
175 }
176 // Tty.printf("OK\n");
177
178 if (sdhc) {
179 cmd(58);
180 uint32_t OCR;
181 sdR3(OCR);
182 ccs = 1UL & (OCR >> 30);
183 // Tty.printf("OCR register is %#010lx\n", long(OCR));
184 } else {
185 ccs = 0;
186 }
187 // Tty.printf("ccs = %d\n", ccs);
188
189 byte type_code = rd(0x1be + 0x4);
190 switch (type_code) {
191 default:
192 type = FAT16;
193 break;
194 case 0x0b:
195 case 0x0c:
196 type = FAT32;
197 break;
198 }
199 // Tty.printf("Type code %#02x means FAT%d\n", type_code, (type == FAT16) ? 16 : 32);
200
201 o_partition = 512L * rd4(0x1be + 0x8);
205
206 // Tty.printf("Bytes per sector: %d\n", rd2(o_partition + 0xb));
207 // Tty.printf("Sectors per cluster: %d\n", sectors_per_cluster);
208
209 if (type == FAT16) {
213 o_root = o_fat + (2 * 512L * sectors_per_fat);
214 // data area starts with cluster 2, so offset it here
215 o_data = o_root + (max_root_dir_entries * 32L) - (2L * cluster_size);
216 } else {
219 uint32_t fat_begin_lba = (o_partition >> 9) + reserved_sectors;
220 uint32_t cluster_begin_lba = (o_partition >> 9) + reserved_sectors + (2 * sectors_per_fat);
221
222 o_fat = 512L * fat_begin_lba;
223 o_root = (512L * (cluster_begin_lba + (root_dir_first_cluster - 2) * sectors_per_cluster));
224 o_data = (512L * (cluster_begin_lba - 2 * sectors_per_cluster));
225 }
226 return 1;
227 }
unsigned short uint16_t
unsigned int uint32_t
#define delay(x)
Definition Gpu_Hal.h:468
uint16_t sectors_per_fat
Definition sdcard.h:275
byte sdR7()
Definition sdcard.h:111
byte pin
Definition sdcard.h:268
uint32_t rd4(uint32_t off)
Definition sdcard.h:251
void desel()
Definition sdcard.h:60
byte type
Definition sdcard.h:271
uint16_t rd2(uint32_t off)
Definition sdcard.h:257
byte ccs
Definition sdcard.h:269
uint16_t cluster_size
Definition sdcard.h:276
uint16_t reserved_sectors
Definition sdcard.h:273
uint16_t max_root_dir_entries
Definition sdcard.h:274
uint32_t o_data
Definition sdcard.h:283
uint32_t o_root
Definition sdcard.h:282
uint32_t o_fat
Definition sdcard.h:281
void sd_delay(byte n)
Definition sdcard.h:64
void appcmd(byte cc, uint32_t lba=0)
Definition sdcard.h:123
uint32_t o_partition
Definition sdcard.h:280
uint32_t root_dir_first_cluster
Definition sdcard.h:277
byte rd(uint32_t off)
Definition sdcard.h:263
uint16_t sectors_per_cluster
Definition sdcard.h:272
byte sdR3(uint32_t &ocr)
Definition sdcard.h:99
#define FAT32
Definition sdcard.h:55
#define FAT16
Definition sdcard.h:54

◆ cmd()

void sdcard::cmd ( byte  cmd,
uint32_t  lba = 0,
uint8_t  crc = 0x95 
)
inline

Definition at line 69 of file sdcard.h.

69 {
70#if VERBOSE
71 Serial.print("cmd ");
72 Serial.print(cmd, DEC);
73 Serial.print(" ");
74 Serial.print(lba, HEX);
75 Serial.println();
76#endif
77
78 sel();
79 SPI.transfer(0xff);
80 SPI.transfer(0x40 | cmd);
81 SPI.transfer(0xff & (lba >> 24));
82 SPI.transfer(0xff & (lba >> 16));
83 SPI.transfer(0xff & (lba >> 8));
84 SPI.transfer(0xff & (lba));
85 SPI.transfer(crc);
86 SPI.transfer(0xff);
87 }
void sel()
Definition sdcard.h:59

◆ cmd17()

void sdcard::cmd17 ( uint32_t  off)
inline

Definition at line 228 of file sdcard.h.

228 {
229 if (ccs)
230 cmd(17, off >> 9);
231 else
232 cmd(17, off & ~511L);
233 R1();
234 sel();
235 while (SPI.transfer(0xff) != 0xfe)
236 ;
237 }

◆ desel()

void sdcard::desel ( )
inline

Definition at line 60 of file sdcard.h.

60 {
61 digitalWrite(pin, HIGH);
62 SPI.transfer(0xff); // force DO release
63 }

◆ R1()

byte sdcard::R1 ( )
inline

Definition at line 89 of file sdcard.h.

90 { // read response R1
91 byte r;
92
93 while ((r = SPI.transfer(0xff)) & 0x80);
94 desel();
95 SPI.transfer(0xff);
96 return r;
97 }

◆ rd()

byte sdcard::rd ( uint32_t  off)
inline

Definition at line 263 of file sdcard.h.

263 {
264 byte r;
265 rdn((byte*)&r, off, sizeof(r));
266 return r;
267 }
void rdn(byte *d, uint32_t off, uint16_t n)
Definition sdcard.h:238

◆ rd2()

uint16_t sdcard::rd2 ( uint32_t  off)
inline

Definition at line 257 of file sdcard.h.

257 {
258 uint16_t r;
259 rdn((byte*)&r, off, sizeof(r));
260 return r;
261 }

◆ rd4()

uint32_t sdcard::rd4 ( uint32_t  off)
inline

Definition at line 251 of file sdcard.h.

251 {
252 uint32_t r;
253 rdn((byte*)&r, off, sizeof(r));
254 return r;
255 }

◆ rdn()

void sdcard::rdn ( byte *  d,
uint32_t  off,
uint16_t  n 
)
inline

Definition at line 238 of file sdcard.h.

238 {
239 cmd17(off);
240 uint16_t i;
241 uint16_t bo = (off & 511);
242 for (i = 0; i < bo; i++)
243 SPI.transfer(0xff);
244 for (i = 0; i < n; i++)
245 *d++ = SPI.transfer(0xff);
246 for (i = 0; i < (514 - bo - n); i++)
247 SPI.transfer(0xff);
248 desel();
249 }
static uint32_t d
Definition Common.c:39
void cmd17(uint32_t off)
Definition sdcard.h:228

◆ sd_delay()

void sdcard::sd_delay ( byte  n)
inline

Definition at line 64 of file sdcard.h.

64 {
65 while (n--)
66 SPI.transfer(0xff);
67 }

◆ sdR3()

byte sdcard::sdR3 ( uint32_t ocr)
inline

Definition at line 99 of file sdcard.h.

99 { // read response R3
100 uint32_t r;
101 while ((r = SPI.transfer(0xff)) & 0x80)
102 ;
103 for (byte i = 4; i; i--)
104 ocr = (ocr << 8) | SPI.transfer(0xff);
105 SPI.transfer(0xff); // trailing byte
106
107 desel();
108 return r;
109 }

◆ sdR7()

byte sdcard::sdR7 ( )
inline

Definition at line 111 of file sdcard.h.

111 { // read response R3
112 uint32_t r;
113 while ((r = SPI.transfer(0xff)) & 0x80)
114 ;
115 for (byte i = 4; i; i--)
116 // Serial.println(SPI.transfer(0xff), HEX);
117 SPI.transfer(0xff);
118 desel();
119
120 return r;
121 }

◆ sel()

void sdcard::sel ( )
inline

Definition at line 59 of file sdcard.h.

59{ digitalWrite(pin, LOW); }

Field Documentation

◆ ccs

byte sdcard::ccs

Definition at line 269 of file sdcard.h.

◆ cluster_size

uint16_t sdcard::cluster_size

Definition at line 276 of file sdcard.h.

◆ max_root_dir_entries

uint16_t sdcard::max_root_dir_entries

Definition at line 274 of file sdcard.h.

◆ o_data

uint32_t sdcard::o_data

Definition at line 283 of file sdcard.h.

◆ o_fat

uint32_t sdcard::o_fat

Definition at line 281 of file sdcard.h.

◆ o_partition

uint32_t sdcard::o_partition

Definition at line 280 of file sdcard.h.

◆ o_root

uint32_t sdcard::o_root

Definition at line 282 of file sdcard.h.

◆ pin

byte sdcard::pin

Definition at line 268 of file sdcard.h.

◆ reserved_sectors

uint16_t sdcard::reserved_sectors

Definition at line 273 of file sdcard.h.

◆ root_dir_first_cluster

uint32_t sdcard::root_dir_first_cluster

Definition at line 277 of file sdcard.h.

◆ sectors_per_cluster

uint16_t sdcard::sectors_per_cluster

Definition at line 272 of file sdcard.h.

◆ sectors_per_fat

uint16_t sdcard::sectors_per_fat

Definition at line 275 of file sdcard.h.

◆ type

byte sdcard::type

Definition at line 271 of file sdcard.h.


The documentation for this class was generated from the following file: