Sd2Card.h

Fri, 17 Nov 2017 10:13:31 +0100

author
mdd
date
Fri, 17 Nov 2017 10:13:31 +0100
changeset 3
770b218a4931
parent 0
2c8ba1964db7
permissions
-rw-r--r--

proper configuration, homing and planner optimization

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

mercurial