Subversion Repositories MK-Marlin

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ron 1
/**
2
 * Marlin 3D Printer Firmware
3
 * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
 *
5
 * Based on Sprinter and grbl.
6
 * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
 
23
/**
24
 * \file
25
 * \brief Sd2Card class for V2 SD/SDHC cards
26
 */
27
 
28
/**
29
 * Arduino Sd2Card Library
30
 * Copyright (C) 2009 by William Greiman
31
 *
32
 * This file is part of the Arduino Sd2Card Library
33
 */
34
#ifndef _SD2CARD_H_
35
#define _SD2CARD_H_
36
 
37
#include "SdFatConfig.h"
38
#include "SdInfo.h"
39
 
40
// SPI speed is F_CPU/2^(1 + index), 0 <= index <= 6
41
uint8_t const SPI_FULL_SPEED = 0,         // Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate().
42
              SPI_HALF_SPEED = 1,         // Set SCK rate to F_CPU/4. See Sd2Card::setSckRate().
43
              SPI_QUARTER_SPEED = 2,      // Set SCK rate to F_CPU/8. See Sd2Card::setSckRate().
44
              SPI_EIGHTH_SPEED = 3,       // Set SCK rate to F_CPU/16. See Sd2Card::setSckRate().
45
              SPI_SIXTEENTH_SPEED = 4;    // Set SCK rate to F_CPU/32. See Sd2Card::setSckRate().
46
 
47
uint16_t const SD_INIT_TIMEOUT = 2000,    // init timeout ms
48
               SD_ERASE_TIMEOUT = 10000,  // erase timeout ms
49
               SD_READ_TIMEOUT = 300,     // read timeout ms
50
               SD_WRITE_TIMEOUT = 600;    // write time out ms
51
 
52
// SD card errors
53
uint8_t const SD_CARD_ERROR_CMD0 = 0X1,                 // timeout error for command CMD0 (initialize card in SPI mode)
54
              SD_CARD_ERROR_CMD8 = 0X2,                 // CMD8 was not accepted - not a valid SD card
55
              SD_CARD_ERROR_CMD12 = 0X3,                // card returned an error response for CMD12 (write stop)
56
              SD_CARD_ERROR_CMD17 = 0X4,                // card returned an error response for CMD17 (read block)
57
              SD_CARD_ERROR_CMD18 = 0X5,                // card returned an error response for CMD18 (read multiple block)
58
              SD_CARD_ERROR_CMD24 = 0X6,                // card returned an error response for CMD24 (write block)
59
              SD_CARD_ERROR_CMD25 = 0X7,                // WRITE_MULTIPLE_BLOCKS command failed
60
              SD_CARD_ERROR_CMD58 = 0X8,                // card returned an error response for CMD58 (read OCR)
61
              SD_CARD_ERROR_ACMD23 = 0X9,               // SET_WR_BLK_ERASE_COUNT failed
62
              SD_CARD_ERROR_ACMD41 = 0XA,               // ACMD41 initialization process timeout
63
              SD_CARD_ERROR_BAD_CSD = 0XB,              // card returned a bad CSR version field
64
              SD_CARD_ERROR_ERASE = 0XC,                // erase block group command failed
65
              SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD,   // card not capable of single block erase
66
              SD_CARD_ERROR_ERASE_TIMEOUT = 0XE,        // Erase sequence timed out
67
              SD_CARD_ERROR_READ = 0XF,                 // card returned an error token instead of read data
68
              SD_CARD_ERROR_READ_REG = 0x10,            // read CID or CSD failed
69
              SD_CARD_ERROR_READ_TIMEOUT = 0x11,        // timeout while waiting for start of read data
70
              SD_CARD_ERROR_STOP_TRAN = 0x12,           // card did not accept STOP_TRAN_TOKEN
71
              SD_CARD_ERROR_WRITE = 0x13,               // card returned an error token as a response to a write operation
72
              SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0x14,    // REMOVE - not used ... attempt to write protected block zero
73
              SD_CARD_ERROR_WRITE_MULTIPLE = 0x15,      // card did not go ready for a multiple block write
74
              SD_CARD_ERROR_WRITE_PROGRAMMING = 0x16,   // card returned an error to a CMD13 status check after a write
75
              SD_CARD_ERROR_WRITE_TIMEOUT = 0x17,       // timeout occurred during write programming
76
              SD_CARD_ERROR_SCK_RATE = 0x18,            // incorrect rate selected
77
              SD_CARD_ERROR_INIT_NOT_CALLED = 0x19,     // init() not called
78
              SD_CARD_ERROR_CRC = 0x20;                 // crc check error
79
 
80
// card types
81
uint8_t const SD_CARD_TYPE_SD1  = 1,                    // Standard capacity V1 SD card
82
              SD_CARD_TYPE_SD2  = 2,                    // Standard capacity V2 SD card
83
              SD_CARD_TYPE_SDHC = 3;                    // High Capacity SD card
84
 
85
/**
86
 * define SOFTWARE_SPI to use bit-bang SPI
87
 */
88
#if MEGA_SOFT_SPI
89
  #define SOFTWARE_SPI
90
#elif USE_SOFTWARE_SPI
91
  #define SOFTWARE_SPI
92
#endif
93
 
94
// SPI pin definitions - do not edit here - change in SdFatConfig.h
95
#if DISABLED(SOFTWARE_SPI)
96
  // hardware pin defs
97
  #define SD_CHIP_SELECT_PIN SS_PIN   // The default chip select pin for the SD card is SS.
98
  // The following three pins must not be redefined for hardware SPI.
99
  #define SPI_MOSI_PIN MOSI_PIN       // SPI Master Out Slave In pin
100
  #define SPI_MISO_PIN MISO_PIN       // SPI Master In Slave Out pin
101
  #define SPI_SCK_PIN SCK_PIN         // SPI Clock pin
102
#else  // SOFTWARE_SPI
103
  #define SD_CHIP_SELECT_PIN SOFT_SPI_CS_PIN  // SPI chip select pin
104
  #define SPI_MOSI_PIN SOFT_SPI_MOSI_PIN      // SPI Master Out Slave In pin
105
  #define SPI_MISO_PIN SOFT_SPI_MISO_PIN      // SPI Master In Slave Out pin
106
  #define SPI_SCK_PIN SOFT_SPI_SCK_PIN        // SPI Clock pin
107
#endif  // SOFTWARE_SPI
108
 
109
/**
110
 * \class Sd2Card
111
 * \brief Raw access to SD and SDHC flash memory cards.
112
 */
113
class Sd2Card {
114
  public:
115
 
116
  Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0) {}
117
 
118
  uint32_t cardSize();
119
  bool erase(uint32_t firstBlock, uint32_t lastBlock);
120
  bool eraseSingleBlockEnable();
121
 
122
  /**
123
   *  Set SD error code.
124
   *  \param[in] code value for error code.
125
   */
126
  void error(uint8_t code) {errorCode_ = code;}
127
 
128
  /**
129
   * \return error code for last error. See Sd2Card.h for a list of error codes.
130
   */
131
  int errorCode() const {return errorCode_;}
132
 
133
  /** \return error data for last error. */
134
  int errorData() const {return status_;}
135
 
136
  /**
137
   * Initialize an SD flash memory card with default clock rate and chip
138
   * select pin.  See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
139
   *
140
   * \return true for success or false for failure.
141
   */
142
  bool init(uint8_t sckRateID = SPI_FULL_SPEED,
143
            pin_t chipSelectPin = SD_CHIP_SELECT_PIN);
144
  bool readBlock(uint32_t block, uint8_t* dst);
145
 
146
  /**
147
   * Read a card's CID register. The CID contains card identification
148
   * information such as Manufacturer ID, Product name, Product serial
149
   * number and Manufacturing date.
150
   *
151
   * \param[out] cid pointer to area for returned data.
152
   *
153
   * \return true for success or false for failure.
154
   */
155
  bool readCID(cid_t* cid) { return readRegister(CMD10, cid); }
156
 
157
  /**
158
   * Read a card's CSD register. The CSD contains Card-Specific Data that
159
   * provides information regarding access to the card's contents.
160
   *
161
   * \param[out] csd pointer to area for returned data.
162
   *
163
   * \return true for success or false for failure.
164
   */
165
  bool readCSD(csd_t* csd) { return readRegister(CMD9, csd); }
166
 
167
  bool readData(uint8_t* dst);
168
  bool readStart(uint32_t blockNumber);
169
  bool readStop();
170
  bool setSckRate(uint8_t sckRateID);
171
  /**
172
   * Return the card type: SD V1, SD V2 or SDHC
173
   * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC.
174
   */
175
  int type() const {return type_;}
176
  bool writeBlock(uint32_t blockNumber, const uint8_t* src);
177
  bool writeData(const uint8_t* src);
178
  bool writeStart(uint32_t blockNumber, uint32_t eraseCount);
179
  bool writeStop();
180
 
181
  private:
182
  uint8_t chipSelectPin_,
183
          errorCode_,
184
          spiRate_,
185
          status_,
186
          type_;
187
 
188
  // private functions
189
  uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
190
    cardCommand(CMD55, 0);
191
    return cardCommand(cmd, arg);
192
  }
193
  uint8_t cardCommand(uint8_t cmd, uint32_t arg);
194
 
195
  bool readData(uint8_t* dst, uint16_t count);
196
  bool readRegister(uint8_t cmd, void* buf);
197
  void chipSelectHigh();
198
  void chipSelectLow();
199
  void type(uint8_t value) { type_ = value; }
200
  bool waitNotBusy(uint16_t timeoutMillis);
201
  bool writeData(uint8_t token, const uint8_t* src);
202
};
203
 
204
#endif  // _SD2CARD_H_