Project

General

Profile

Statistics
| Branch: | Revision:

root / arduino-1.0 / libraries / SD / utility / Sd2Card.cpp @ 58d82c77

History | View | Annotate | Download (19.2 KB)

1
/* Arduino Sd2Card Library
2
 * Copyright (C) 2009 by William Greiman
3
 *
4
 * This file is part of the Arduino Sd2Card Library
5
 *
6
 * This Library is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This Library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with the Arduino Sd2Card Library.  If not, see
18
 * <http://www.gnu.org/licenses/>.
19
 */
20
#include <Arduino.h>
21
#include "Sd2Card.h"
22
//------------------------------------------------------------------------------
23
#ifndef SOFTWARE_SPI
24
// functions for hardware SPI
25
/** Send a byte to the card */
26
static void spiSend(uint8_t b) {
27
  SPDR = b;
28
  while (!(SPSR & (1 << SPIF)));
29
}
30
/** Receive a byte from the card */
31
static  uint8_t spiRec(void) {
32
  spiSend(0XFF);
33
  return SPDR;
34
}
35
#else  // SOFTWARE_SPI
36
//------------------------------------------------------------------------------
37
/** nop to tune soft SPI timing */
38
#define nop asm volatile ("nop\n\t")
39
//------------------------------------------------------------------------------
40
/** Soft SPI receive */
41
uint8_t spiRec(void) {
42
  uint8_t data = 0;
43
  // no interrupts during byte receive - about 8 us
44
  cli();
45
  // output pin high - like sending 0XFF
46
  fastDigitalWrite(SPI_MOSI_PIN, HIGH);
47

    
48
  for (uint8_t i = 0; i < 8; i++) {
49
    fastDigitalWrite(SPI_SCK_PIN, HIGH);
50

    
51
    // adjust so SCK is nice
52
    nop;
53
    nop;
54

    
55
    data <<= 1;
56

    
57
    if (fastDigitalRead(SPI_MISO_PIN)) data |= 1;
58

    
59
    fastDigitalWrite(SPI_SCK_PIN, LOW);
60
  }
61
  // enable interrupts
62
  sei();
63
  return data;
64
}
65
//------------------------------------------------------------------------------
66
/** Soft SPI send */
67
void spiSend(uint8_t data) {
68
  // no interrupts during byte send - about 8 us
69
  cli();
70
  for (uint8_t i = 0; i < 8; i++) {
71
    fastDigitalWrite(SPI_SCK_PIN, LOW);
72

    
73
    fastDigitalWrite(SPI_MOSI_PIN, data & 0X80);
74

    
75
    data <<= 1;
76

    
77
    fastDigitalWrite(SPI_SCK_PIN, HIGH);
78
  }
79
  // hold SCK high for a few ns
80
  nop;
81
  nop;
82
  nop;
83
  nop;
84

    
85
  fastDigitalWrite(SPI_SCK_PIN, LOW);
86
  // enable interrupts
87
  sei();
88
}
89
#endif  // SOFTWARE_SPI
90
//------------------------------------------------------------------------------
91
// send command and return error code.  Return zero for OK
92
uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
93
  // end read if in partialBlockRead mode
94
  readEnd();
95

    
96
  // select card
97
  chipSelectLow();
98

    
99
  // wait up to 300 ms if busy
100
  waitNotBusy(300);
101

    
102
  // send command
103
  spiSend(cmd | 0x40);
104

    
105
  // send argument
106
  for (int8_t s = 24; s >= 0; s -= 8) spiSend(arg >> s);
107

    
108
  // send CRC
109
  uint8_t crc = 0XFF;
110
  if (cmd == CMD0) crc = 0X95;  // correct crc for CMD0 with arg 0
111
  if (cmd == CMD8) crc = 0X87;  // correct crc for CMD8 with arg 0X1AA
112
  spiSend(crc);
113

    
114
  // wait for response
115
  for (uint8_t i = 0; ((status_ = spiRec()) & 0X80) && i != 0XFF; i++);
116
  return status_;
117
}
118
//------------------------------------------------------------------------------
119
/**
120
 * Determine the size of an SD flash memory card.
121
 *
122
 * \return The number of 512 byte data blocks in the card
123
 *         or zero if an error occurs.
124
 */
125
uint32_t Sd2Card::cardSize(void) {
126
  csd_t csd;
127
  if (!readCSD(&csd)) return 0;
128
  if (csd.v1.csd_ver == 0) {
129
    uint8_t read_bl_len = csd.v1.read_bl_len;
130
    uint16_t c_size = (csd.v1.c_size_high << 10)
131
                      | (csd.v1.c_size_mid << 2) | csd.v1.c_size_low;
132
    uint8_t c_size_mult = (csd.v1.c_size_mult_high << 1)
133
                          | csd.v1.c_size_mult_low;
134
    return (uint32_t)(c_size + 1) << (c_size_mult + read_bl_len - 7);
135
  } else if (csd.v2.csd_ver == 1) {
136
    uint32_t c_size = ((uint32_t)csd.v2.c_size_high << 16)
137
                      | (csd.v2.c_size_mid << 8) | csd.v2.c_size_low;
138
    return (c_size + 1) << 10;
139
  } else {
140
    error(SD_CARD_ERROR_BAD_CSD);
141
    return 0;
142
  }
143
}
144
//------------------------------------------------------------------------------
145
void Sd2Card::chipSelectHigh(void) {
146
  digitalWrite(chipSelectPin_, HIGH);
147
}
148
//------------------------------------------------------------------------------
149
void Sd2Card::chipSelectLow(void) {
150
  digitalWrite(chipSelectPin_, LOW);
151
}
152
//------------------------------------------------------------------------------
153
/** Erase a range of blocks.
154
 *
155
 * \param[in] firstBlock The address of the first block in the range.
156
 * \param[in] lastBlock The address of the last block in the range.
157
 *
158
 * \note This function requests the SD card to do a flash erase for a
159
 * range of blocks.  The data on the card after an erase operation is
160
 * either 0 or 1, depends on the card vendor.  The card must support
161
 * single block erase.
162
 *
163
 * \return The value one, true, is returned for success and
164
 * the value zero, false, is returned for failure.
165
 */
166
uint8_t Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) {
167
  if (!eraseSingleBlockEnable()) {
168
    error(SD_CARD_ERROR_ERASE_SINGLE_BLOCK);
169
    goto fail;
170
  }
171
  if (type_ != SD_CARD_TYPE_SDHC) {
172
    firstBlock <<= 9;
173
    lastBlock <<= 9;
174
  }
175
  if (cardCommand(CMD32, firstBlock)
176
    || cardCommand(CMD33, lastBlock)
177
    || cardCommand(CMD38, 0)) {
178
      error(SD_CARD_ERROR_ERASE);
179
      goto fail;
180
  }
181
  if (!waitNotBusy(SD_ERASE_TIMEOUT)) {
182
    error(SD_CARD_ERROR_ERASE_TIMEOUT);
183
    goto fail;
184
  }
185
  chipSelectHigh();
186
  return true;
187

    
188
 fail:
189
  chipSelectHigh();
190
  return false;
191
}
192
//------------------------------------------------------------------------------
193
/** Determine if card supports single block erase.
194
 *
195
 * \return The value one, true, is returned if single block erase is supported.
196
 * The value zero, false, is returned if single block erase is not supported.
197
 */
198
uint8_t Sd2Card::eraseSingleBlockEnable(void) {
199
  csd_t csd;
200
  return readCSD(&csd) ? csd.v1.erase_blk_en : 0;
201
}
202
//------------------------------------------------------------------------------
203
/**
204
 * Initialize an SD flash memory card.
205
 *
206
 * \param[in] sckRateID SPI clock rate selector. See setSckRate().
207
 * \param[in] chipSelectPin SD chip select pin number.
208
 *
209
 * \return The value one, true, is returned for success and
210
 * the value zero, false, is returned for failure.  The reason for failure
211
 * can be determined by calling errorCode() and errorData().
212
 */
213
uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
214
  errorCode_ = inBlock_ = partialBlockRead_ = type_ = 0;
215
  chipSelectPin_ = chipSelectPin;
216
  // 16-bit init start time allows over a minute
217
  uint16_t t0 = (uint16_t)millis();
218
  uint32_t arg;
219

    
220
  // set pin modes
221
  pinMode(chipSelectPin_, OUTPUT);
222
  chipSelectHigh();
223
  pinMode(SPI_MISO_PIN, INPUT);
224
  pinMode(SPI_MOSI_PIN, OUTPUT);
225
  pinMode(SPI_SCK_PIN, OUTPUT);
226

    
227
#ifndef SOFTWARE_SPI
228
  // SS must be in output mode even it is not chip select
229
  pinMode(SS_PIN, OUTPUT);
230
  digitalWrite(SS_PIN, HIGH); // disable any SPI device using hardware SS pin
231
  // Enable SPI, Master, clock rate f_osc/128
232
  SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1) | (1 << SPR0);
233
  // clear double speed
234
  SPSR &= ~(1 << SPI2X);
235
#endif  // SOFTWARE_SPI
236

    
237
  // must supply min of 74 clock cycles with CS high.
238
  for (uint8_t i = 0; i < 10; i++) spiSend(0XFF);
239

    
240
  chipSelectLow();
241

    
242
  // command to go idle in SPI mode
243
  while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
244
    if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
245
      error(SD_CARD_ERROR_CMD0);
246
      goto fail;
247
    }
248
  }
249
  // check SD version
250
  if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) {
251
    type(SD_CARD_TYPE_SD1);
252
  } else {
253
    // only need last byte of r7 response
254
    for (uint8_t i = 0; i < 4; i++) status_ = spiRec();
255
    if (status_ != 0XAA) {
256
      error(SD_CARD_ERROR_CMD8);
257
      goto fail;
258
    }
259
    type(SD_CARD_TYPE_SD2);
260
  }
261
  // initialize card and send host supports SDHC if SD2
262
  arg = type() == SD_CARD_TYPE_SD2 ? 0X40000000 : 0;
263

    
264
  while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) {
265
    // check for timeout
266
    if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
267
      error(SD_CARD_ERROR_ACMD41);
268
      goto fail;
269
    }
270
  }
271
  // if SD2 read OCR register to check for SDHC card
272
  if (type() == SD_CARD_TYPE_SD2) {
273
    if (cardCommand(CMD58, 0)) {
274
      error(SD_CARD_ERROR_CMD58);
275
      goto fail;
276
    }
277
    if ((spiRec() & 0XC0) == 0XC0) type(SD_CARD_TYPE_SDHC);
278
    // discard rest of ocr - contains allowed voltage range
279
    for (uint8_t i = 0; i < 3; i++) spiRec();
280
  }
281
  chipSelectHigh();
282

    
283
#ifndef SOFTWARE_SPI
284
  return setSckRate(sckRateID);
285
#else  // SOFTWARE_SPI
286
  return true;
287
#endif  // SOFTWARE_SPI
288

    
289
 fail:
290
  chipSelectHigh();
291
  return false;
292
}
293
//------------------------------------------------------------------------------
294
/**
295
 * Enable or disable partial block reads.
296
 *
297
 * Enabling partial block reads improves performance by allowing a block
298
 * to be read over the SPI bus as several sub-blocks.  Errors may occur
299
 * if the time between reads is too long since the SD card may timeout.
300
 * The SPI SS line will be held low until the entire block is read or
301
 * readEnd() is called.
302
 *
303
 * Use this for applications like the Adafruit Wave Shield.
304
 *
305
 * \param[in] value The value TRUE (non-zero) or FALSE (zero).)
306
 */
307
void Sd2Card::partialBlockRead(uint8_t value) {
308
  readEnd();
309
  partialBlockRead_ = value;
310
}
311
//------------------------------------------------------------------------------
312
/**
313
 * Read a 512 byte block from an SD card device.
314
 *
315
 * \param[in] block Logical block to be read.
316
 * \param[out] dst Pointer to the location that will receive the data.
317

318
 * \return The value one, true, is returned for success and
319
 * the value zero, false, is returned for failure.
320
 */
321
uint8_t Sd2Card::readBlock(uint32_t block, uint8_t* dst) {
322
  return readData(block, 0, 512, dst);
323
}
324
//------------------------------------------------------------------------------
325
/**
326
 * Read part of a 512 byte block from an SD card.
327
 *
328
 * \param[in] block Logical block to be read.
329
 * \param[in] offset Number of bytes to skip at start of block
330
 * \param[out] dst Pointer to the location that will receive the data.
331
 * \param[in] count Number of bytes to read
332
 * \return The value one, true, is returned for success and
333
 * the value zero, false, is returned for failure.
334
 */
335
uint8_t Sd2Card::readData(uint32_t block,
336
        uint16_t offset, uint16_t count, uint8_t* dst) {
337
  uint16_t n;
338
  if (count == 0) return true;
339
  if ((count + offset) > 512) {
340
    goto fail;
341
  }
342
  if (!inBlock_ || block != block_ || offset < offset_) {
343
    block_ = block;
344
    // use address if not SDHC card
345
    if (type()!= SD_CARD_TYPE_SDHC) block <<= 9;
346
    if (cardCommand(CMD17, block)) {
347
      error(SD_CARD_ERROR_CMD17);
348
      goto fail;
349
    }
350
    if (!waitStartBlock()) {
351
      goto fail;
352
    }
353
    offset_ = 0;
354
    inBlock_ = 1;
355
  }
356

    
357
#ifdef OPTIMIZE_HARDWARE_SPI
358
  // start first spi transfer
359
  SPDR = 0XFF;
360

    
361
  // skip data before offset
362
  for (;offset_ < offset; offset_++) {
363
    while (!(SPSR & (1 << SPIF)));
364
    SPDR = 0XFF;
365
  }
366
  // transfer data
367
  n = count - 1;
368
  for (uint16_t i = 0; i < n; i++) {
369
    while (!(SPSR & (1 << SPIF)));
370
    dst[i] = SPDR;
371
    SPDR = 0XFF;
372
  }
373
  // wait for last byte
374
  while (!(SPSR & (1 << SPIF)));
375
  dst[n] = SPDR;
376

    
377
#else  // OPTIMIZE_HARDWARE_SPI
378

    
379
  // skip data before offset
380
  for (;offset_ < offset; offset_++) {
381
    spiRec();
382
  }
383
  // transfer data
384
  for (uint16_t i = 0; i < count; i++) {
385
    dst[i] = spiRec();
386
  }
387
#endif  // OPTIMIZE_HARDWARE_SPI
388

    
389
  offset_ += count;
390
  if (!partialBlockRead_ || offset_ >= 512) {
391
    // read rest of data, checksum and set chip select high
392
    readEnd();
393
  }
394
  return true;
395

    
396
 fail:
397
  chipSelectHigh();
398
  return false;
399
}
400
//------------------------------------------------------------------------------
401
/** Skip remaining data in a block when in partial block read mode. */
402
void Sd2Card::readEnd(void) {
403
  if (inBlock_) {
404
      // skip data and crc
405
#ifdef OPTIMIZE_HARDWARE_SPI
406
    // optimize skip for hardware
407
    SPDR = 0XFF;
408
    while (offset_++ < 513) {
409
      while (!(SPSR & (1 << SPIF)));
410
      SPDR = 0XFF;
411
    }
412
    // wait for last crc byte
413
    while (!(SPSR & (1 << SPIF)));
414
#else  // OPTIMIZE_HARDWARE_SPI
415
    while (offset_++ < 514) spiRec();
416
#endif  // OPTIMIZE_HARDWARE_SPI
417
    chipSelectHigh();
418
    inBlock_ = 0;
419
  }
420
}
421
//------------------------------------------------------------------------------
422
/** read CID or CSR register */
423
uint8_t Sd2Card::readRegister(uint8_t cmd, void* buf) {
424
  uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
425
  if (cardCommand(cmd, 0)) {
426
    error(SD_CARD_ERROR_READ_REG);
427
    goto fail;
428
  }
429
  if (!waitStartBlock()) goto fail;
430
  // transfer data
431
  for (uint16_t i = 0; i < 16; i++) dst[i] = spiRec();
432
  spiRec();  // get first crc byte
433
  spiRec();  // get second crc byte
434
  chipSelectHigh();
435
  return true;
436

    
437
 fail:
438
  chipSelectHigh();
439
  return false;
440
}
441
//------------------------------------------------------------------------------
442
/**
443
 * Set the SPI clock rate.
444
 *
445
 * \param[in] sckRateID A value in the range [0, 6].
446
 *
447
 * The SPI clock will be set to F_CPU/pow(2, 1 + sckRateID). The maximum
448
 * SPI rate is F_CPU/2 for \a sckRateID = 0 and the minimum rate is F_CPU/128
449
 * for \a scsRateID = 6.
450
 *
451
 * \return The value one, true, is returned for success and the value zero,
452
 * false, is returned for an invalid value of \a sckRateID.
453
 */
454
uint8_t Sd2Card::setSckRate(uint8_t sckRateID) {
455
  if (sckRateID > 6) {
456
    error(SD_CARD_ERROR_SCK_RATE);
457
    return false;
458
  }
459
  // see avr processor datasheet for SPI register bit definitions
460
  if ((sckRateID & 1) || sckRateID == 6) {
461
    SPSR &= ~(1 << SPI2X);
462
  } else {
463
    SPSR |= (1 << SPI2X);
464
  }
465
  SPCR &= ~((1 <<SPR1) | (1 << SPR0));
466
  SPCR |= (sckRateID & 4 ? (1 << SPR1) : 0)
467
    | (sckRateID & 2 ? (1 << SPR0) : 0);
468
  return true;
469
}
470
//------------------------------------------------------------------------------
471
// wait for card to go not busy
472
uint8_t Sd2Card::waitNotBusy(uint16_t timeoutMillis) {
473
  uint16_t t0 = millis();
474
  do {
475
    if (spiRec() == 0XFF) return true;
476
  }
477
  while (((uint16_t)millis() - t0) < timeoutMillis);
478
  return false;
479
}
480
//------------------------------------------------------------------------------
481
/** Wait for start block token */
482
uint8_t Sd2Card::waitStartBlock(void) {
483
  uint16_t t0 = millis();
484
  while ((status_ = spiRec()) == 0XFF) {
485
    if (((uint16_t)millis() - t0) > SD_READ_TIMEOUT) {
486
      error(SD_CARD_ERROR_READ_TIMEOUT);
487
      goto fail;
488
    }
489
  }
490
  if (status_ != DATA_START_BLOCK) {
491
    error(SD_CARD_ERROR_READ);
492
    goto fail;
493
  }
494
  return true;
495

    
496
 fail:
497
  chipSelectHigh();
498
  return false;
499
}
500
//------------------------------------------------------------------------------
501
/**
502
 * Writes a 512 byte block to an SD card.
503
 *
504
 * \param[in] blockNumber Logical block to be written.
505
 * \param[in] src Pointer to the location of the data to be written.
506
 * \return The value one, true, is returned for success and
507
 * the value zero, false, is returned for failure.
508
 */
509
uint8_t Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
510
#if SD_PROTECT_BLOCK_ZERO
511
  // don't allow write to first block
512
  if (blockNumber == 0) {
513
    error(SD_CARD_ERROR_WRITE_BLOCK_ZERO);
514
    goto fail;
515
  }
516
#endif  // SD_PROTECT_BLOCK_ZERO
517

    
518
  // use address if not SDHC card
519
  if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
520
  if (cardCommand(CMD24, blockNumber)) {
521
    error(SD_CARD_ERROR_CMD24);
522
    goto fail;
523
  }
524
  if (!writeData(DATA_START_BLOCK, src)) goto fail;
525

    
526
  // wait for flash programming to complete
527
  if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
528
    error(SD_CARD_ERROR_WRITE_TIMEOUT);
529
    goto fail;
530
  }
531
  // response is r2 so get and check two bytes for nonzero
532
  if (cardCommand(CMD13, 0) || spiRec()) {
533
    error(SD_CARD_ERROR_WRITE_PROGRAMMING);
534
    goto fail;
535
  }
536
  chipSelectHigh();
537
  return true;
538

    
539
 fail:
540
  chipSelectHigh();
541
  return false;
542
}
543
//------------------------------------------------------------------------------
544
/** Write one data block in a multiple block write sequence */
545
uint8_t Sd2Card::writeData(const uint8_t* src) {
546
  // wait for previous write to finish
547
  if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
548
    error(SD_CARD_ERROR_WRITE_MULTIPLE);
549
    chipSelectHigh();
550
    return false;
551
  }
552
  return writeData(WRITE_MULTIPLE_TOKEN, src);
553
}
554
//------------------------------------------------------------------------------
555
// send one block of data for write block or write multiple blocks
556
uint8_t Sd2Card::writeData(uint8_t token, const uint8_t* src) {
557
#ifdef OPTIMIZE_HARDWARE_SPI
558

    
559
  // send data - optimized loop
560
  SPDR = token;
561

    
562
  // send two byte per iteration
563
  for (uint16_t i = 0; i < 512; i += 2) {
564
    while (!(SPSR & (1 << SPIF)));
565
    SPDR = src[i];
566
    while (!(SPSR & (1 << SPIF)));
567
    SPDR = src[i+1];
568
  }
569

    
570
  // wait for last data byte
571
  while (!(SPSR & (1 << SPIF)));
572

    
573
#else  // OPTIMIZE_HARDWARE_SPI
574
  spiSend(token);
575
  for (uint16_t i = 0; i < 512; i++) {
576
    spiSend(src[i]);
577
  }
578
#endif  // OPTIMIZE_HARDWARE_SPI
579
  spiSend(0xff);  // dummy crc
580
  spiSend(0xff);  // dummy crc
581

    
582
  status_ = spiRec();
583
  if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
584
    error(SD_CARD_ERROR_WRITE);
585
    chipSelectHigh();
586
    return false;
587
  }
588
  return true;
589
}
590
//------------------------------------------------------------------------------
591
/** Start a write multiple blocks sequence.
592
 *
593
 * \param[in] blockNumber Address of first block in sequence.
594
 * \param[in] eraseCount The number of blocks to be pre-erased.
595
 *
596
 * \note This function is used with writeData() and writeStop()
597
 * for optimized multiple block writes.
598
 *
599
 * \return The value one, true, is returned for success and
600
 * the value zero, false, is returned for failure.
601
 */
602
uint8_t Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
603
#if SD_PROTECT_BLOCK_ZERO
604
  // don't allow write to first block
605
  if (blockNumber == 0) {
606
    error(SD_CARD_ERROR_WRITE_BLOCK_ZERO);
607
    goto fail;
608
  }
609
#endif  // SD_PROTECT_BLOCK_ZERO
610
  // send pre-erase count
611
  if (cardAcmd(ACMD23, eraseCount)) {
612
    error(SD_CARD_ERROR_ACMD23);
613
    goto fail;
614
  }
615
  // use address if not SDHC card
616
  if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
617
  if (cardCommand(CMD25, blockNumber)) {
618
    error(SD_CARD_ERROR_CMD25);
619
    goto fail;
620
  }
621
  return true;
622

    
623
 fail:
624
  chipSelectHigh();
625
  return false;
626
}
627
//------------------------------------------------------------------------------
628
/** End a write multiple blocks sequence.
629
 *
630
* \return The value one, true, is returned for success and
631
 * the value zero, false, is returned for failure.
632
 */
633
uint8_t Sd2Card::writeStop(void) {
634
  if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
635
  spiSend(STOP_TRAN_TOKEN);
636
  if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
637
  chipSelectHigh();
638
  return true;
639

    
640
 fail:
641
  error(SD_CARD_ERROR_STOP_TRAN);
642
  chipSelectHigh();
643
  return false;
644
}