Przeglądaj źródła

Converted all files on src to Unix file format

João Brázio 8 lat temu
rodzic
commit
32d798fcc7
No account linked to committer's email address

+ 718
- 718
Marlin/Sd2Card.cpp
Plik diff jest za duży
Wyświetl plik


+ 252
- 252
Marlin/Sd2Card.h Wyświetl plik

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

+ 453
- 453
Marlin/Sd2PinMap.h Wyświetl plik

@@ -1,453 +1,453 @@
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
- * Arduino SdFat Library
25
- * Copyright (C) 2010 by William Greiman
26
- *
27
- * This file is part of the Arduino Sd2Card Library
28
- */
29
-// Warning this file was generated by a program.
30
-#include "Marlin.h"
31
-#include "macros.h"
32
-
33
-#if ENABLED(SDSUPPORT)
34
-
35
-#ifndef Sd2PinMap_h
36
-#define Sd2PinMap_h
37
-#include <avr/io.h>
38
-//------------------------------------------------------------------------------
39
-/** struct for mapping digital pins */
40
-struct pin_map_t {
41
-  volatile uint8_t* ddr;
42
-  volatile uint8_t* pin;
43
-  volatile uint8_t* port;
44
-  uint8_t bit;
45
-};
46
-//------------------------------------------------------------------------------
47
-#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) // Mega
48
-
49
-// Two Wire (aka I2C) ports
50
-uint8_t const SDA_PIN = 20;  // D1
51
-uint8_t const SCL_PIN = 21;  // D0
52
-
53
-#undef MOSI_PIN
54
-#undef MISO_PIN
55
-#undef SCK_PIN
56
-// SPI port
57
-uint8_t const SS_PIN = 53;    // B0
58
-uint8_t const MOSI_PIN = 51;  // B2
59
-uint8_t const MISO_PIN = 50;  // B3
60
-uint8_t const SCK_PIN = 52;   // B1
61
-
62
-static const pin_map_t digitalPinMap[] = {
63
-  {&DDRE, &PINE, &PORTE, 0},  // E0  0
64
-  {&DDRE, &PINE, &PORTE, 1},  // E1  1
65
-  {&DDRE, &PINE, &PORTE, 4},  // E4  2
66
-  {&DDRE, &PINE, &PORTE, 5},  // E5  3
67
-  {&DDRG, &PING, &PORTG, 5},  // G5  4
68
-  {&DDRE, &PINE, &PORTE, 3},  // E3  5
69
-  {&DDRH, &PINH, &PORTH, 3},  // H3  6
70
-  {&DDRH, &PINH, &PORTH, 4},  // H4  7
71
-  {&DDRH, &PINH, &PORTH, 5},  // H5  8
72
-  {&DDRH, &PINH, &PORTH, 6},  // H6  9
73
-  {&DDRB, &PINB, &PORTB, 4},  // B4 10
74
-  {&DDRB, &PINB, &PORTB, 5},  // B5 11
75
-  {&DDRB, &PINB, &PORTB, 6},  // B6 12
76
-  {&DDRB, &PINB, &PORTB, 7},  // B7 13
77
-  {&DDRJ, &PINJ, &PORTJ, 1},  // J1 14
78
-  {&DDRJ, &PINJ, &PORTJ, 0},  // J0 15
79
-  {&DDRH, &PINH, &PORTH, 1},  // H1 16
80
-  {&DDRH, &PINH, &PORTH, 0},  // H0 17
81
-  {&DDRD, &PIND, &PORTD, 3},  // D3 18
82
-  {&DDRD, &PIND, &PORTD, 2},  // D2 19
83
-  {&DDRD, &PIND, &PORTD, 1},  // D1 20
84
-  {&DDRD, &PIND, &PORTD, 0},  // D0 21
85
-  {&DDRA, &PINA, &PORTA, 0},  // A0 22
86
-  {&DDRA, &PINA, &PORTA, 1},  // A1 23
87
-  {&DDRA, &PINA, &PORTA, 2},  // A2 24
88
-  {&DDRA, &PINA, &PORTA, 3},  // A3 25
89
-  {&DDRA, &PINA, &PORTA, 4},  // A4 26
90
-  {&DDRA, &PINA, &PORTA, 5},  // A5 27
91
-  {&DDRA, &PINA, &PORTA, 6},  // A6 28
92
-  {&DDRA, &PINA, &PORTA, 7},  // A7 29
93
-  {&DDRC, &PINC, &PORTC, 7},  // C7 30
94
-  {&DDRC, &PINC, &PORTC, 6},  // C6 31
95
-  {&DDRC, &PINC, &PORTC, 5},  // C5 32
96
-  {&DDRC, &PINC, &PORTC, 4},  // C4 33
97
-  {&DDRC, &PINC, &PORTC, 3},  // C3 34
98
-  {&DDRC, &PINC, &PORTC, 2},  // C2 35
99
-  {&DDRC, &PINC, &PORTC, 1},  // C1 36
100
-  {&DDRC, &PINC, &PORTC, 0},  // C0 37
101
-  {&DDRD, &PIND, &PORTD, 7},  // D7 38
102
-  {&DDRG, &PING, &PORTG, 2},  // G2 39
103
-  {&DDRG, &PING, &PORTG, 1},  // G1 40
104
-  {&DDRG, &PING, &PORTG, 0},  // G0 41
105
-  {&DDRL, &PINL, &PORTL, 7},  // L7 42
106
-  {&DDRL, &PINL, &PORTL, 6},  // L6 43
107
-  {&DDRL, &PINL, &PORTL, 5},  // L5 44
108
-  {&DDRL, &PINL, &PORTL, 4},  // L4 45
109
-  {&DDRL, &PINL, &PORTL, 3},  // L3 46
110
-  {&DDRL, &PINL, &PORTL, 2},  // L2 47
111
-  {&DDRL, &PINL, &PORTL, 1},  // L1 48
112
-  {&DDRL, &PINL, &PORTL, 0},  // L0 49
113
-  {&DDRB, &PINB, &PORTB, 3},  // B3 50
114
-  {&DDRB, &PINB, &PORTB, 2},  // B2 51
115
-  {&DDRB, &PINB, &PORTB, 1},  // B1 52
116
-  {&DDRB, &PINB, &PORTB, 0},  // B0 53
117
-  {&DDRF, &PINF, &PORTF, 0},  // F0 54
118
-  {&DDRF, &PINF, &PORTF, 1},  // F1 55
119
-  {&DDRF, &PINF, &PORTF, 2},  // F2 56
120
-  {&DDRF, &PINF, &PORTF, 3},  // F3 57
121
-  {&DDRF, &PINF, &PORTF, 4},  // F4 58
122
-  {&DDRF, &PINF, &PORTF, 5},  // F5 59
123
-  {&DDRF, &PINF, &PORTF, 6},  // F6 60
124
-  {&DDRF, &PINF, &PORTF, 7},  // F7 61
125
-  {&DDRK, &PINK, &PORTK, 0},  // K0 62
126
-  {&DDRK, &PINK, &PORTK, 1},  // K1 63
127
-  {&DDRK, &PINK, &PORTK, 2},  // K2 64
128
-  {&DDRK, &PINK, &PORTK, 3},  // K3 65
129
-  {&DDRK, &PINK, &PORTK, 4},  // K4 66
130
-  {&DDRK, &PINK, &PORTK, 5},  // K5 67
131
-  {&DDRK, &PINK, &PORTK, 6},  // K6 68
132
-  {&DDRK, &PINK, &PORTK, 7}   // K7 69
133
-};
134
-//------------------------------------------------------------------------------
135
-#elif defined(__AVR_ATmega644P__)\
136
-|| defined(__AVR_ATmega644__)\
137
-|| defined(__AVR_ATmega1284P__)
138
-// Sanguino
139
-
140
-// Two Wire (aka I2C) ports
141
-uint8_t const SDA_PIN = 17;  // C1
142
-uint8_t const SCL_PIN = 18;  // C2
143
-
144
-// SPI port
145
-uint8_t const SS_PIN = 4;    // B4
146
-uint8_t const MOSI_PIN = 5;  // B5
147
-uint8_t const MISO_PIN = 6;  // B6
148
-uint8_t const SCK_PIN = 7;   // B7
149
-
150
-static const pin_map_t digitalPinMap[] = {
151
-  {&DDRB, &PINB, &PORTB, 0},  // B0  0
152
-  {&DDRB, &PINB, &PORTB, 1},  // B1  1
153
-  {&DDRB, &PINB, &PORTB, 2},  // B2  2
154
-  {&DDRB, &PINB, &PORTB, 3},  // B3  3
155
-  {&DDRB, &PINB, &PORTB, 4},  // B4  4
156
-  {&DDRB, &PINB, &PORTB, 5},  // B5  5
157
-  {&DDRB, &PINB, &PORTB, 6},  // B6  6
158
-  {&DDRB, &PINB, &PORTB, 7},  // B7  7
159
-  {&DDRD, &PIND, &PORTD, 0},  // D0  8
160
-  {&DDRD, &PIND, &PORTD, 1},  // D1  9
161
-  {&DDRD, &PIND, &PORTD, 2},  // D2 10
162
-  {&DDRD, &PIND, &PORTD, 3},  // D3 11
163
-  {&DDRD, &PIND, &PORTD, 4},  // D4 12
164
-  {&DDRD, &PIND, &PORTD, 5},  // D5 13
165
-  {&DDRD, &PIND, &PORTD, 6},  // D6 14
166
-  {&DDRD, &PIND, &PORTD, 7},  // D7 15
167
-  {&DDRC, &PINC, &PORTC, 0},  // C0 16
168
-  {&DDRC, &PINC, &PORTC, 1},  // C1 17
169
-  {&DDRC, &PINC, &PORTC, 2},  // C2 18
170
-  {&DDRC, &PINC, &PORTC, 3},  // C3 19
171
-  {&DDRC, &PINC, &PORTC, 4},  // C4 20
172
-  {&DDRC, &PINC, &PORTC, 5},  // C5 21
173
-  {&DDRC, &PINC, &PORTC, 6},  // C6 22
174
-  {&DDRC, &PINC, &PORTC, 7},  // C7 23
175
-  {&DDRA, &PINA, &PORTA, 7},  // A7 24
176
-  {&DDRA, &PINA, &PORTA, 6},  // A6 25
177
-  {&DDRA, &PINA, &PORTA, 5},  // A5 26
178
-  {&DDRA, &PINA, &PORTA, 4},  // A4 27
179
-  {&DDRA, &PINA, &PORTA, 3},  // A3 28
180
-  {&DDRA, &PINA, &PORTA, 2},  // A2 29
181
-  {&DDRA, &PINA, &PORTA, 1},  // A1 30
182
-  {&DDRA, &PINA, &PORTA, 0}   // A0 31
183
-};
184
-//------------------------------------------------------------------------------
185
-#elif defined(__AVR_ATmega32U4__)
186
-// Teensy 2.0
187
-
188
-// Two Wire (aka I2C) ports
189
-uint8_t const SDA_PIN = 6;  // D1
190
-uint8_t const SCL_PIN = 5;  // D0
191
-
192
-// SPI port
193
-uint8_t const SS_PIN = 0;    // B0
194
-uint8_t const MOSI_PIN = 2;  // B2
195
-uint8_t const MISO_PIN = 3;  // B3
196
-uint8_t const SCK_PIN = 1;   // B1
197
-
198
-static const pin_map_t digitalPinMap[] = {
199
-  {&DDRB, &PINB, &PORTB, 0},  // B0  0
200
-  {&DDRB, &PINB, &PORTB, 1},  // B1  1
201
-  {&DDRB, &PINB, &PORTB, 2},  // B2  2
202
-  {&DDRB, &PINB, &PORTB, 3},  // B3  3
203
-  {&DDRB, &PINB, &PORTB, 7},  // B7  4
204
-  {&DDRD, &PIND, &PORTD, 0},  // D0  5
205
-  {&DDRD, &PIND, &PORTD, 1},  // D1  6
206
-  {&DDRD, &PIND, &PORTD, 2},  // D2  7
207
-  {&DDRD, &PIND, &PORTD, 3},  // D3  8
208
-  {&DDRC, &PINC, &PORTC, 6},  // C6  9
209
-  {&DDRC, &PINC, &PORTC, 7},  // C7 10
210
-  {&DDRD, &PIND, &PORTD, 6},  // D6 11
211
-  {&DDRD, &PIND, &PORTD, 7},  // D7 12
212
-  {&DDRB, &PINB, &PORTB, 4},  // B4 13
213
-  {&DDRB, &PINB, &PORTB, 5},  // B5 14
214
-  {&DDRB, &PINB, &PORTB, 6},  // B6 15
215
-  {&DDRF, &PINF, &PORTF, 7},  // F7 16
216
-  {&DDRF, &PINF, &PORTF, 6},  // F6 17
217
-  {&DDRF, &PINF, &PORTF, 5},  // F5 18
218
-  {&DDRF, &PINF, &PORTF, 4},  // F4 19
219
-  {&DDRF, &PINF, &PORTF, 1},  // F1 20
220
-  {&DDRF, &PINF, &PORTF, 0},  // F0 21
221
-  {&DDRD, &PIND, &PORTD, 4},  // D4 22
222
-  {&DDRD, &PIND, &PORTD, 5},  // D5 23
223
-  {&DDRE, &PINE, &PORTE, 6}   // E6 24
224
-};
225
-//------------------------------------------------------------------------------
226
-#elif defined(__AVR_AT90USB646__)\
227
-|| defined(__AVR_AT90USB1286__)
228
-// Teensy++ 1.0 & 2.0
229
-
230
-// Two Wire (aka I2C) ports
231
-uint8_t const SDA_PIN = 1;  // D1
232
-uint8_t const SCL_PIN = 0;  // D0
233
-
234
-// SPI port
235
-uint8_t const SS_PIN    = 20;    // B0
236
-uint8_t const MOSI_PIN  = 22;    // B2
237
-uint8_t const MISO_PIN  = 23;    // B3
238
-uint8_t const SCK_PIN   = 21;    // B1
239
-
240
-static const pin_map_t digitalPinMap[] = {
241
-  {&DDRD, &PIND, &PORTD, 0},  // D0  0
242
-  {&DDRD, &PIND, &PORTD, 1},  // D1  1
243
-  {&DDRD, &PIND, &PORTD, 2},  // D2  2
244
-  {&DDRD, &PIND, &PORTD, 3},  // D3  3
245
-  {&DDRD, &PIND, &PORTD, 4},  // D4  4
246
-  {&DDRD, &PIND, &PORTD, 5},  // D5  5
247
-  {&DDRD, &PIND, &PORTD, 6},  // D6  6
248
-  {&DDRD, &PIND, &PORTD, 7},  // D7  7
249
-  {&DDRE, &PINE, &PORTE, 0},  // E0  8
250
-  {&DDRE, &PINE, &PORTE, 1},  // E1  9
251
-  {&DDRC, &PINC, &PORTC, 0},  // C0 10
252
-  {&DDRC, &PINC, &PORTC, 1},  // C1 11
253
-  {&DDRC, &PINC, &PORTC, 2},  // C2 12
254
-  {&DDRC, &PINC, &PORTC, 3},  // C3 13
255
-  {&DDRC, &PINC, &PORTC, 4},  // C4 14
256
-  {&DDRC, &PINC, &PORTC, 5},  // C5 15
257
-  {&DDRC, &PINC, &PORTC, 6},  // C6 16
258
-  {&DDRC, &PINC, &PORTC, 7},  // C7 17
259
-  {&DDRE, &PINE, &PORTE, 6},  // E6 18
260
-  {&DDRE, &PINE, &PORTE, 7},  // E7 19
261
-  {&DDRB, &PINB, &PORTB, 0},  // B0 20
262
-  {&DDRB, &PINB, &PORTB, 1},  // B1 21
263
-  {&DDRB, &PINB, &PORTB, 2},  // B2 22
264
-  {&DDRB, &PINB, &PORTB, 3},  // B3 23
265
-  {&DDRB, &PINB, &PORTB, 4},  // B4 24
266
-  {&DDRB, &PINB, &PORTB, 5},  // B5 25
267
-  {&DDRB, &PINB, &PORTB, 6},  // B6 26
268
-  {&DDRB, &PINB, &PORTB, 7},  // B7 27
269
-  {&DDRA, &PINA, &PORTA, 0},  // A0 28
270
-  {&DDRA, &PINA, &PORTA, 1},  // A1 29
271
-  {&DDRA, &PINA, &PORTA, 2},  // A2 30
272
-  {&DDRA, &PINA, &PORTA, 3},  // A3 31
273
-  {&DDRA, &PINA, &PORTA, 4},  // A4 32
274
-  {&DDRA, &PINA, &PORTA, 5},  // A5 33
275
-  {&DDRA, &PINA, &PORTA, 6},  // A6 34
276
-  {&DDRA, &PINA, &PORTA, 7},  // A7 35
277
-  {&DDRE, &PINE, &PORTE, 4},  // E4 36
278
-  {&DDRE, &PINE, &PORTE, 5},  // E5 37
279
-  {&DDRF, &PINF, &PORTF, 0},  // F0 38
280
-  {&DDRF, &PINF, &PORTF, 1},  // F1 39
281
-  {&DDRF, &PINF, &PORTF, 2},  // F2 40
282
-  {&DDRF, &PINF, &PORTF, 3},  // F3 41
283
-  {&DDRF, &PINF, &PORTF, 4},  // F4 42
284
-  {&DDRF, &PINF, &PORTF, 5},  // F5 43
285
-  {&DDRF, &PINF, &PORTF, 6},  // F6 44
286
-  {&DDRF, &PINF, &PORTF, 7}   // F7 45
287
-};
288
-//------------------------------------------------------------------------------
289
-#elif defined(__AVR_ATmega168__)\
290
-||defined(__AVR_ATmega168P__)\
291
-||defined(__AVR_ATmega328P__)
292
-// 168 and 328 Arduinos
293
-
294
-// Two Wire (aka I2C) ports
295
-uint8_t const SDA_PIN = 18;  // C4
296
-uint8_t const SCL_PIN = 19;  // C5
297
-
298
-// SPI port
299
-uint8_t const SS_PIN = 10;    // B2
300
-uint8_t const MOSI_PIN = 11;  // B3
301
-uint8_t const MISO_PIN = 12;  // B4
302
-uint8_t const SCK_PIN = 13;   // B5
303
-
304
-static const pin_map_t digitalPinMap[] = {
305
-  {&DDRD, &PIND, &PORTD, 0},  // D0  0
306
-  {&DDRD, &PIND, &PORTD, 1},  // D1  1
307
-  {&DDRD, &PIND, &PORTD, 2},  // D2  2
308
-  {&DDRD, &PIND, &PORTD, 3},  // D3  3
309
-  {&DDRD, &PIND, &PORTD, 4},  // D4  4
310
-  {&DDRD, &PIND, &PORTD, 5},  // D5  5
311
-  {&DDRD, &PIND, &PORTD, 6},  // D6  6
312
-  {&DDRD, &PIND, &PORTD, 7},  // D7  7
313
-  {&DDRB, &PINB, &PORTB, 0},  // B0  8
314
-  {&DDRB, &PINB, &PORTB, 1},  // B1  9
315
-  {&DDRB, &PINB, &PORTB, 2},  // B2 10
316
-  {&DDRB, &PINB, &PORTB, 3},  // B3 11
317
-  {&DDRB, &PINB, &PORTB, 4},  // B4 12
318
-  {&DDRB, &PINB, &PORTB, 5},  // B5 13
319
-  {&DDRC, &PINC, &PORTC, 0},  // C0 14
320
-  {&DDRC, &PINC, &PORTC, 1},  // C1 15
321
-  {&DDRC, &PINC, &PORTC, 2},  // C2 16
322
-  {&DDRC, &PINC, &PORTC, 3},  // C3 17
323
-  {&DDRC, &PINC, &PORTC, 4},  // C4 18
324
-  {&DDRC, &PINC, &PORTC, 5}   // C5 19
325
-};
326
-#elif defined(__AVR_ATmega1281__)
327
-// Waspmote
328
-
329
-// Two Wire (aka I2C) ports
330
-uint8_t const SDA_PIN = 41;
331
-uint8_t const SCL_PIN = 40;
332
-
333
-
334
-#undef MOSI_PIN
335
-#undef MISO_PIN
336
-// SPI port
337
-uint8_t const SS_PIN = 16;    // B0
338
-uint8_t const MOSI_PIN = 11;  // B2
339
-uint8_t const MISO_PIN = 12;  // B3
340
-uint8_t const SCK_PIN = 10;   // B1
341
-
342
-static const pin_map_t digitalPinMap[] = {
343
-  {&DDRE, &PINE, &PORTE, 0}, // E0 0
344
-  {&DDRE, &PINE, &PORTE, 1}, // E1 1
345
-  {&DDRE, &PINE, &PORTE, 3}, // E3 2
346
-  {&DDRE, &PINE, &PORTE, 4}, // E4 3
347
-  {&DDRC, &PINC, &PORTC, 4}, // C4 4
348
-  {&DDRC, &PINC, &PORTC, 5}, // C5 5
349
-  {&DDRC, &PINC, &PORTC, 6}, // C6 6
350
-  {&DDRC, &PINC, &PORTC, 7}, // C7 7
351
-  {&DDRA, &PINA, &PORTA, 2}, // A2 8
352
-  {&DDRA, &PINA, &PORTA, 3}, // A3 9
353
-  {&DDRA, &PINA, &PORTA, 4}, // A4 10
354
-  {&DDRD, &PIND, &PORTD, 5}, // D5 11
355
-  {&DDRD, &PIND, &PORTD, 6}, // D6 12
356
-  {&DDRC, &PINC, &PORTC, 1}, // C1 13
357
-  {&DDRF, &PINF, &PORTF, 1}, // F1 14
358
-  {&DDRF, &PINF, &PORTF, 2}, // F2 15
359
-  {&DDRF, &PINF, &PORTF, 3}, // F3 16
360
-  {&DDRF, &PINF, &PORTF, 4}, // F4 17
361
-  {&DDRF, &PINF, &PORTF, 5}, // F5 18
362
-  {&DDRF, &PINF, &PORTF, 6}, // F6 19
363
-  {&DDRF, &PINF, &PORTF, 7}, // F7 20
364
-  {&DDRF, &PINF, &PORTF, 0}, // F0 21
365
-  {&DDRA, &PINA, &PORTA, 1}, // A1 22
366
-  {&DDRD, &PIND, &PORTD, 7}, // D7 23
367
-  {&DDRE, &PINE, &PORTE, 5}, // E5 24
368
-  {&DDRA, &PINA, &PORTA, 6}, // A6 25
369
-  {&DDRE, &PINE, &PORTE, 2}, // E2 26
370
-  {&DDRA, &PINA, &PORTA, 5}, // A5 27
371
-  {&DDRC, &PINC, &PORTC, 0}, // C0 28
372
-  {&DDRB, &PINB, &PORTB, 0}, // B0 29
373
-  {&DDRB, &PINB, &PORTB, 1}, // B1 30
374
-  {&DDRB, &PINB, &PORTB, 2}, // B2 31
375
-  {&DDRB, &PINB, &PORTB, 3}, // B3 32
376
-  {&DDRB, &PINB, &PORTB, 4}, // B4 33
377
-  {&DDRB, &PINB, &PORTB, 5}, // B5 34
378
-  {&DDRA, &PINA, &PORTA, 0}, // A0 35
379
-  {&DDRB, &PINB, &PORTB, 6}, // B6 36
380
-  {&DDRB, &PINB, &PORTB, 7}, // B7 37
381
-  {&DDRE, &PINE, &PORTE, 6}, // E6 38
382
-  {&DDRE, &PINE, &PORTE, 7}, // E7 39
383
-  {&DDRD, &PIND, &PORTD, 0}, // D0 40
384
-  {&DDRD, &PIND, &PORTD, 1}, // D1 41
385
-  {&DDRC, &PINC, &PORTC, 3}, // C3 42
386
-  {&DDRD, &PIND, &PORTD, 2}, // D2 43
387
-  {&DDRD, &PIND, &PORTD, 3}, // D3 44
388
-  {&DDRA, &PINA, &PORTA, 7}, // A7 45
389
-  {&DDRC, &PINC, &PORTC, 2}, // C2 46
390
-  {&DDRD, &PIND, &PORTD, 4}, // D4 47
391
-  {&DDRG, &PING, &PORTG, 2}, // G2 48
392
-  {&DDRG, &PING, &PORTG, 1}, // G1 49
393
-  {&DDRG, &PING, &PORTG, 0}, // G0 50
394
-};
395
-#else  // defined(__AVR_ATmega1280__)
396
-#error unknown chip
397
-#endif  // defined(__AVR_ATmega1280__)
398
-//------------------------------------------------------------------------------
399
-static const uint8_t digitalPinCount = COUNT(digitalPinMap);
400
-
401
-uint8_t badPinNumber(void)
402
-  __attribute__((error("Pin number is too large or not a constant")));
403
-
404
-static inline __attribute__((always_inline))
405
-  bool getPinMode(uint8_t pin) {
406
-  if (__builtin_constant_p(pin) && pin < digitalPinCount) {
407
-    return (*digitalPinMap[pin].ddr >> digitalPinMap[pin].bit) & 1;
408
-  }
409
-  else {
410
-    return badPinNumber();
411
-  }
412
-}
413
-static inline __attribute__((always_inline))
414
-  void setPinMode(uint8_t pin, uint8_t mode) {
415
-  if (__builtin_constant_p(pin) && pin < digitalPinCount) {
416
-    if (mode) {
417
-      SBI(*digitalPinMap[pin].ddr, digitalPinMap[pin].bit);
418
-    }
419
-    else {
420
-      CBI(*digitalPinMap[pin].ddr, digitalPinMap[pin].bit);
421
-    }
422
-  }
423
-  else {
424
-    badPinNumber();
425
-  }
426
-}
427
-static inline __attribute__((always_inline))
428
-  bool fastDigitalRead(uint8_t pin) {
429
-  if (__builtin_constant_p(pin) && pin < digitalPinCount) {
430
-    return (*digitalPinMap[pin].pin >> digitalPinMap[pin].bit) & 1;
431
-  }
432
-  else {
433
-    return badPinNumber();
434
-  }
435
-}
436
-static inline __attribute__((always_inline))
437
-  void fastDigitalWrite(uint8_t pin, uint8_t value) {
438
-  if (__builtin_constant_p(pin) && pin < digitalPinCount) {
439
-    if (value) {
440
-      SBI(*digitalPinMap[pin].port, digitalPinMap[pin].bit);
441
-    }
442
-    else {
443
-      CBI(*digitalPinMap[pin].port, digitalPinMap[pin].bit);
444
-    }
445
-  }
446
-  else {
447
-    badPinNumber();
448
-  }
449
-}
450
-#endif  // Sd2PinMap_h
451
-
452
-
453
-#endif
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
+ * Arduino SdFat Library
25
+ * Copyright (C) 2010 by William Greiman
26
+ *
27
+ * This file is part of the Arduino Sd2Card Library
28
+ */
29
+// Warning this file was generated by a program.
30
+#include "Marlin.h"
31
+#include "macros.h"
32
+
33
+#if ENABLED(SDSUPPORT)
34
+
35
+#ifndef Sd2PinMap_h
36
+#define Sd2PinMap_h
37
+#include <avr/io.h>
38
+//------------------------------------------------------------------------------
39
+/** struct for mapping digital pins */
40
+struct pin_map_t {
41
+  volatile uint8_t* ddr;
42
+  volatile uint8_t* pin;
43
+  volatile uint8_t* port;
44
+  uint8_t bit;
45
+};
46
+//------------------------------------------------------------------------------
47
+#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) // Mega
48
+
49
+// Two Wire (aka I2C) ports
50
+uint8_t const SDA_PIN = 20;  // D1
51
+uint8_t const SCL_PIN = 21;  // D0
52
+
53
+#undef MOSI_PIN
54
+#undef MISO_PIN
55
+#undef SCK_PIN
56
+// SPI port
57
+uint8_t const SS_PIN = 53;    // B0
58
+uint8_t const MOSI_PIN = 51;  // B2
59
+uint8_t const MISO_PIN = 50;  // B3
60
+uint8_t const SCK_PIN = 52;   // B1
61
+
62
+static const pin_map_t digitalPinMap[] = {
63
+  {&DDRE, &PINE, &PORTE, 0},  // E0  0
64
+  {&DDRE, &PINE, &PORTE, 1},  // E1  1
65
+  {&DDRE, &PINE, &PORTE, 4},  // E4  2
66
+  {&DDRE, &PINE, &PORTE, 5},  // E5  3
67
+  {&DDRG, &PING, &PORTG, 5},  // G5  4
68
+  {&DDRE, &PINE, &PORTE, 3},  // E3  5
69
+  {&DDRH, &PINH, &PORTH, 3},  // H3  6
70
+  {&DDRH, &PINH, &PORTH, 4},  // H4  7
71
+  {&DDRH, &PINH, &PORTH, 5},  // H5  8
72
+  {&DDRH, &PINH, &PORTH, 6},  // H6  9
73
+  {&DDRB, &PINB, &PORTB, 4},  // B4 10
74
+  {&DDRB, &PINB, &PORTB, 5},  // B5 11
75
+  {&DDRB, &PINB, &PORTB, 6},  // B6 12
76
+  {&DDRB, &PINB, &PORTB, 7},  // B7 13
77
+  {&DDRJ, &PINJ, &PORTJ, 1},  // J1 14
78
+  {&DDRJ, &PINJ, &PORTJ, 0},  // J0 15
79
+  {&DDRH, &PINH, &PORTH, 1},  // H1 16
80
+  {&DDRH, &PINH, &PORTH, 0},  // H0 17
81
+  {&DDRD, &PIND, &PORTD, 3},  // D3 18
82
+  {&DDRD, &PIND, &PORTD, 2},  // D2 19
83
+  {&DDRD, &PIND, &PORTD, 1},  // D1 20
84
+  {&DDRD, &PIND, &PORTD, 0},  // D0 21
85
+  {&DDRA, &PINA, &PORTA, 0},  // A0 22
86
+  {&DDRA, &PINA, &PORTA, 1},  // A1 23
87
+  {&DDRA, &PINA, &PORTA, 2},  // A2 24
88
+  {&DDRA, &PINA, &PORTA, 3},  // A3 25
89
+  {&DDRA, &PINA, &PORTA, 4},  // A4 26
90
+  {&DDRA, &PINA, &PORTA, 5},  // A5 27
91
+  {&DDRA, &PINA, &PORTA, 6},  // A6 28
92
+  {&DDRA, &PINA, &PORTA, 7},  // A7 29
93
+  {&DDRC, &PINC, &PORTC, 7},  // C7 30
94
+  {&DDRC, &PINC, &PORTC, 6},  // C6 31
95
+  {&DDRC, &PINC, &PORTC, 5},  // C5 32
96
+  {&DDRC, &PINC, &PORTC, 4},  // C4 33
97
+  {&DDRC, &PINC, &PORTC, 3},  // C3 34
98
+  {&DDRC, &PINC, &PORTC, 2},  // C2 35
99
+  {&DDRC, &PINC, &PORTC, 1},  // C1 36
100
+  {&DDRC, &PINC, &PORTC, 0},  // C0 37
101
+  {&DDRD, &PIND, &PORTD, 7},  // D7 38
102
+  {&DDRG, &PING, &PORTG, 2},  // G2 39
103
+  {&DDRG, &PING, &PORTG, 1},  // G1 40
104
+  {&DDRG, &PING, &PORTG, 0},  // G0 41
105
+  {&DDRL, &PINL, &PORTL, 7},  // L7 42
106
+  {&DDRL, &PINL, &PORTL, 6},  // L6 43
107
+  {&DDRL, &PINL, &PORTL, 5},  // L5 44
108
+  {&DDRL, &PINL, &PORTL, 4},  // L4 45
109
+  {&DDRL, &PINL, &PORTL, 3},  // L3 46
110
+  {&DDRL, &PINL, &PORTL, 2},  // L2 47
111
+  {&DDRL, &PINL, &PORTL, 1},  // L1 48
112
+  {&DDRL, &PINL, &PORTL, 0},  // L0 49
113
+  {&DDRB, &PINB, &PORTB, 3},  // B3 50
114
+  {&DDRB, &PINB, &PORTB, 2},  // B2 51
115
+  {&DDRB, &PINB, &PORTB, 1},  // B1 52
116
+  {&DDRB, &PINB, &PORTB, 0},  // B0 53
117
+  {&DDRF, &PINF, &PORTF, 0},  // F0 54
118
+  {&DDRF, &PINF, &PORTF, 1},  // F1 55
119
+  {&DDRF, &PINF, &PORTF, 2},  // F2 56
120
+  {&DDRF, &PINF, &PORTF, 3},  // F3 57
121
+  {&DDRF, &PINF, &PORTF, 4},  // F4 58
122
+  {&DDRF, &PINF, &PORTF, 5},  // F5 59
123
+  {&DDRF, &PINF, &PORTF, 6},  // F6 60
124
+  {&DDRF, &PINF, &PORTF, 7},  // F7 61
125
+  {&DDRK, &PINK, &PORTK, 0},  // K0 62
126
+  {&DDRK, &PINK, &PORTK, 1},  // K1 63
127
+  {&DDRK, &PINK, &PORTK, 2},  // K2 64
128
+  {&DDRK, &PINK, &PORTK, 3},  // K3 65
129
+  {&DDRK, &PINK, &PORTK, 4},  // K4 66
130
+  {&DDRK, &PINK, &PORTK, 5},  // K5 67
131
+  {&DDRK, &PINK, &PORTK, 6},  // K6 68
132
+  {&DDRK, &PINK, &PORTK, 7}   // K7 69
133
+};
134
+//------------------------------------------------------------------------------
135
+#elif defined(__AVR_ATmega644P__)\
136
+|| defined(__AVR_ATmega644__)\
137
+|| defined(__AVR_ATmega1284P__)
138
+// Sanguino
139
+
140
+// Two Wire (aka I2C) ports
141
+uint8_t const SDA_PIN = 17;  // C1
142
+uint8_t const SCL_PIN = 18;  // C2
143
+
144
+// SPI port
145
+uint8_t const SS_PIN = 4;    // B4
146
+uint8_t const MOSI_PIN = 5;  // B5
147
+uint8_t const MISO_PIN = 6;  // B6
148
+uint8_t const SCK_PIN = 7;   // B7
149
+
150
+static const pin_map_t digitalPinMap[] = {
151
+  {&DDRB, &PINB, &PORTB, 0},  // B0  0
152
+  {&DDRB, &PINB, &PORTB, 1},  // B1  1
153
+  {&DDRB, &PINB, &PORTB, 2},  // B2  2
154
+  {&DDRB, &PINB, &PORTB, 3},  // B3  3
155
+  {&DDRB, &PINB, &PORTB, 4},  // B4  4
156
+  {&DDRB, &PINB, &PORTB, 5},  // B5  5
157
+  {&DDRB, &PINB, &PORTB, 6},  // B6  6
158
+  {&DDRB, &PINB, &PORTB, 7},  // B7  7
159
+  {&DDRD, &PIND, &PORTD, 0},  // D0  8
160
+  {&DDRD, &PIND, &PORTD, 1},  // D1  9
161
+  {&DDRD, &PIND, &PORTD, 2},  // D2 10
162
+  {&DDRD, &PIND, &PORTD, 3},  // D3 11
163
+  {&DDRD, &PIND, &PORTD, 4},  // D4 12
164
+  {&DDRD, &PIND, &PORTD, 5},  // D5 13
165
+  {&DDRD, &PIND, &PORTD, 6},  // D6 14
166
+  {&DDRD, &PIND, &PORTD, 7},  // D7 15
167
+  {&DDRC, &PINC, &PORTC, 0},  // C0 16
168
+  {&DDRC, &PINC, &PORTC, 1},  // C1 17
169
+  {&DDRC, &PINC, &PORTC, 2},  // C2 18
170
+  {&DDRC, &PINC, &PORTC, 3},  // C3 19
171
+  {&DDRC, &PINC, &PORTC, 4},  // C4 20
172
+  {&DDRC, &PINC, &PORTC, 5},  // C5 21
173
+  {&DDRC, &PINC, &PORTC, 6},  // C6 22
174
+  {&DDRC, &PINC, &PORTC, 7},  // C7 23
175
+  {&DDRA, &PINA, &PORTA, 7},  // A7 24
176
+  {&DDRA, &PINA, &PORTA, 6},  // A6 25
177
+  {&DDRA, &PINA, &PORTA, 5},  // A5 26
178
+  {&DDRA, &PINA, &PORTA, 4},  // A4 27
179
+  {&DDRA, &PINA, &PORTA, 3},  // A3 28
180
+  {&DDRA, &PINA, &PORTA, 2},  // A2 29
181
+  {&DDRA, &PINA, &PORTA, 1},  // A1 30
182
+  {&DDRA, &PINA, &PORTA, 0}   // A0 31
183
+};
184
+//------------------------------------------------------------------------------
185
+#elif defined(__AVR_ATmega32U4__)
186
+// Teensy 2.0
187
+
188
+// Two Wire (aka I2C) ports
189
+uint8_t const SDA_PIN = 6;  // D1
190
+uint8_t const SCL_PIN = 5;  // D0
191
+
192
+// SPI port
193
+uint8_t const SS_PIN = 0;    // B0
194
+uint8_t const MOSI_PIN = 2;  // B2
195
+uint8_t const MISO_PIN = 3;  // B3
196
+uint8_t const SCK_PIN = 1;   // B1
197
+
198
+static const pin_map_t digitalPinMap[] = {
199
+  {&DDRB, &PINB, &PORTB, 0},  // B0  0
200
+  {&DDRB, &PINB, &PORTB, 1},  // B1  1
201
+  {&DDRB, &PINB, &PORTB, 2},  // B2  2
202
+  {&DDRB, &PINB, &PORTB, 3},  // B3  3
203
+  {&DDRB, &PINB, &PORTB, 7},  // B7  4
204
+  {&DDRD, &PIND, &PORTD, 0},  // D0  5
205
+  {&DDRD, &PIND, &PORTD, 1},  // D1  6
206
+  {&DDRD, &PIND, &PORTD, 2},  // D2  7
207
+  {&DDRD, &PIND, &PORTD, 3},  // D3  8
208
+  {&DDRC, &PINC, &PORTC, 6},  // C6  9
209
+  {&DDRC, &PINC, &PORTC, 7},  // C7 10
210
+  {&DDRD, &PIND, &PORTD, 6},  // D6 11
211
+  {&DDRD, &PIND, &PORTD, 7},  // D7 12
212
+  {&DDRB, &PINB, &PORTB, 4},  // B4 13
213
+  {&DDRB, &PINB, &PORTB, 5},  // B5 14
214
+  {&DDRB, &PINB, &PORTB, 6},  // B6 15
215
+  {&DDRF, &PINF, &PORTF, 7},  // F7 16
216
+  {&DDRF, &PINF, &PORTF, 6},  // F6 17
217
+  {&DDRF, &PINF, &PORTF, 5},  // F5 18
218
+  {&DDRF, &PINF, &PORTF, 4},  // F4 19
219
+  {&DDRF, &PINF, &PORTF, 1},  // F1 20
220
+  {&DDRF, &PINF, &PORTF, 0},  // F0 21
221
+  {&DDRD, &PIND, &PORTD, 4},  // D4 22
222
+  {&DDRD, &PIND, &PORTD, 5},  // D5 23
223
+  {&DDRE, &PINE, &PORTE, 6}   // E6 24
224
+};
225
+//------------------------------------------------------------------------------
226
+#elif defined(__AVR_AT90USB646__)\
227
+|| defined(__AVR_AT90USB1286__)
228
+// Teensy++ 1.0 & 2.0
229
+
230
+// Two Wire (aka I2C) ports
231
+uint8_t const SDA_PIN = 1;  // D1
232
+uint8_t const SCL_PIN = 0;  // D0
233
+
234
+// SPI port
235
+uint8_t const SS_PIN    = 20;    // B0
236
+uint8_t const MOSI_PIN  = 22;    // B2
237
+uint8_t const MISO_PIN  = 23;    // B3
238
+uint8_t const SCK_PIN   = 21;    // B1
239
+
240
+static const pin_map_t digitalPinMap[] = {
241
+  {&DDRD, &PIND, &PORTD, 0},  // D0  0
242
+  {&DDRD, &PIND, &PORTD, 1},  // D1  1
243
+  {&DDRD, &PIND, &PORTD, 2},  // D2  2
244
+  {&DDRD, &PIND, &PORTD, 3},  // D3  3
245
+  {&DDRD, &PIND, &PORTD, 4},  // D4  4
246
+  {&DDRD, &PIND, &PORTD, 5},  // D5  5
247
+  {&DDRD, &PIND, &PORTD, 6},  // D6  6
248
+  {&DDRD, &PIND, &PORTD, 7},  // D7  7
249
+  {&DDRE, &PINE, &PORTE, 0},  // E0  8
250
+  {&DDRE, &PINE, &PORTE, 1},  // E1  9
251
+  {&DDRC, &PINC, &PORTC, 0},  // C0 10
252
+  {&DDRC, &PINC, &PORTC, 1},  // C1 11
253
+  {&DDRC, &PINC, &PORTC, 2},  // C2 12
254
+  {&DDRC, &PINC, &PORTC, 3},  // C3 13
255
+  {&DDRC, &PINC, &PORTC, 4},  // C4 14
256
+  {&DDRC, &PINC, &PORTC, 5},  // C5 15
257
+  {&DDRC, &PINC, &PORTC, 6},  // C6 16
258
+  {&DDRC, &PINC, &PORTC, 7},  // C7 17
259
+  {&DDRE, &PINE, &PORTE, 6},  // E6 18
260
+  {&DDRE, &PINE, &PORTE, 7},  // E7 19
261
+  {&DDRB, &PINB, &PORTB, 0},  // B0 20
262
+  {&DDRB, &PINB, &PORTB, 1},  // B1 21
263
+  {&DDRB, &PINB, &PORTB, 2},  // B2 22
264
+  {&DDRB, &PINB, &PORTB, 3},  // B3 23
265
+  {&DDRB, &PINB, &PORTB, 4},  // B4 24
266
+  {&DDRB, &PINB, &PORTB, 5},  // B5 25
267
+  {&DDRB, &PINB, &PORTB, 6},  // B6 26
268
+  {&DDRB, &PINB, &PORTB, 7},  // B7 27
269
+  {&DDRA, &PINA, &PORTA, 0},  // A0 28
270
+  {&DDRA, &PINA, &PORTA, 1},  // A1 29
271
+  {&DDRA, &PINA, &PORTA, 2},  // A2 30
272
+  {&DDRA, &PINA, &PORTA, 3},  // A3 31
273
+  {&DDRA, &PINA, &PORTA, 4},  // A4 32
274
+  {&DDRA, &PINA, &PORTA, 5},  // A5 33
275
+  {&DDRA, &PINA, &PORTA, 6},  // A6 34
276
+  {&DDRA, &PINA, &PORTA, 7},  // A7 35
277
+  {&DDRE, &PINE, &PORTE, 4},  // E4 36
278
+  {&DDRE, &PINE, &PORTE, 5},  // E5 37
279
+  {&DDRF, &PINF, &PORTF, 0},  // F0 38
280
+  {&DDRF, &PINF, &PORTF, 1},  // F1 39
281
+  {&DDRF, &PINF, &PORTF, 2},  // F2 40
282
+  {&DDRF, &PINF, &PORTF, 3},  // F3 41
283
+  {&DDRF, &PINF, &PORTF, 4},  // F4 42
284
+  {&DDRF, &PINF, &PORTF, 5},  // F5 43
285
+  {&DDRF, &PINF, &PORTF, 6},  // F6 44
286
+  {&DDRF, &PINF, &PORTF, 7}   // F7 45
287
+};
288
+//------------------------------------------------------------------------------
289
+#elif defined(__AVR_ATmega168__)\
290
+||defined(__AVR_ATmega168P__)\
291
+||defined(__AVR_ATmega328P__)
292
+// 168 and 328 Arduinos
293
+
294
+// Two Wire (aka I2C) ports
295
+uint8_t const SDA_PIN = 18;  // C4
296
+uint8_t const SCL_PIN = 19;  // C5
297
+
298
+// SPI port
299
+uint8_t const SS_PIN = 10;    // B2
300
+uint8_t const MOSI_PIN = 11;  // B3
301
+uint8_t const MISO_PIN = 12;  // B4
302
+uint8_t const SCK_PIN = 13;   // B5
303
+
304
+static const pin_map_t digitalPinMap[] = {
305
+  {&DDRD, &PIND, &PORTD, 0},  // D0  0
306
+  {&DDRD, &PIND, &PORTD, 1},  // D1  1
307
+  {&DDRD, &PIND, &PORTD, 2},  // D2  2
308
+  {&DDRD, &PIND, &PORTD, 3},  // D3  3
309
+  {&DDRD, &PIND, &PORTD, 4},  // D4  4
310
+  {&DDRD, &PIND, &PORTD, 5},  // D5  5
311
+  {&DDRD, &PIND, &PORTD, 6},  // D6  6
312
+  {&DDRD, &PIND, &PORTD, 7},  // D7  7
313
+  {&DDRB, &PINB, &PORTB, 0},  // B0  8
314
+  {&DDRB, &PINB, &PORTB, 1},  // B1  9
315
+  {&DDRB, &PINB, &PORTB, 2},  // B2 10
316
+  {&DDRB, &PINB, &PORTB, 3},  // B3 11
317
+  {&DDRB, &PINB, &PORTB, 4},  // B4 12
318
+  {&DDRB, &PINB, &PORTB, 5},  // B5 13
319
+  {&DDRC, &PINC, &PORTC, 0},  // C0 14
320
+  {&DDRC, &PINC, &PORTC, 1},  // C1 15
321
+  {&DDRC, &PINC, &PORTC, 2},  // C2 16
322
+  {&DDRC, &PINC, &PORTC, 3},  // C3 17
323
+  {&DDRC, &PINC, &PORTC, 4},  // C4 18
324
+  {&DDRC, &PINC, &PORTC, 5}   // C5 19
325
+};
326
+#elif defined(__AVR_ATmega1281__)
327
+// Waspmote
328
+
329
+// Two Wire (aka I2C) ports
330
+uint8_t const SDA_PIN = 41;
331
+uint8_t const SCL_PIN = 40;
332
+
333
+
334
+#undef MOSI_PIN
335
+#undef MISO_PIN
336
+// SPI port
337
+uint8_t const SS_PIN = 16;    // B0
338
+uint8_t const MOSI_PIN = 11;  // B2
339
+uint8_t const MISO_PIN = 12;  // B3
340
+uint8_t const SCK_PIN = 10;   // B1
341
+
342
+static const pin_map_t digitalPinMap[] = {
343
+  {&DDRE, &PINE, &PORTE, 0}, // E0 0
344
+  {&DDRE, &PINE, &PORTE, 1}, // E1 1
345
+  {&DDRE, &PINE, &PORTE, 3}, // E3 2
346
+  {&DDRE, &PINE, &PORTE, 4}, // E4 3
347
+  {&DDRC, &PINC, &PORTC, 4}, // C4 4
348
+  {&DDRC, &PINC, &PORTC, 5}, // C5 5
349
+  {&DDRC, &PINC, &PORTC, 6}, // C6 6
350
+  {&DDRC, &PINC, &PORTC, 7}, // C7 7
351
+  {&DDRA, &PINA, &PORTA, 2}, // A2 8
352
+  {&DDRA, &PINA, &PORTA, 3}, // A3 9
353
+  {&DDRA, &PINA, &PORTA, 4}, // A4 10
354
+  {&DDRD, &PIND, &PORTD, 5}, // D5 11
355
+  {&DDRD, &PIND, &PORTD, 6}, // D6 12
356
+  {&DDRC, &PINC, &PORTC, 1}, // C1 13
357
+  {&DDRF, &PINF, &PORTF, 1}, // F1 14
358
+  {&DDRF, &PINF, &PORTF, 2}, // F2 15
359
+  {&DDRF, &PINF, &PORTF, 3}, // F3 16
360
+  {&DDRF, &PINF, &PORTF, 4}, // F4 17
361
+  {&DDRF, &PINF, &PORTF, 5}, // F5 18
362
+  {&DDRF, &PINF, &PORTF, 6}, // F6 19
363
+  {&DDRF, &PINF, &PORTF, 7}, // F7 20
364
+  {&DDRF, &PINF, &PORTF, 0}, // F0 21
365
+  {&DDRA, &PINA, &PORTA, 1}, // A1 22
366
+  {&DDRD, &PIND, &PORTD, 7}, // D7 23
367
+  {&DDRE, &PINE, &PORTE, 5}, // E5 24
368
+  {&DDRA, &PINA, &PORTA, 6}, // A6 25
369
+  {&DDRE, &PINE, &PORTE, 2}, // E2 26
370
+  {&DDRA, &PINA, &PORTA, 5}, // A5 27
371
+  {&DDRC, &PINC, &PORTC, 0}, // C0 28
372
+  {&DDRB, &PINB, &PORTB, 0}, // B0 29
373
+  {&DDRB, &PINB, &PORTB, 1}, // B1 30
374
+  {&DDRB, &PINB, &PORTB, 2}, // B2 31
375
+  {&DDRB, &PINB, &PORTB, 3}, // B3 32
376
+  {&DDRB, &PINB, &PORTB, 4}, // B4 33
377
+  {&DDRB, &PINB, &PORTB, 5}, // B5 34
378
+  {&DDRA, &PINA, &PORTA, 0}, // A0 35
379
+  {&DDRB, &PINB, &PORTB, 6}, // B6 36
380
+  {&DDRB, &PINB, &PORTB, 7}, // B7 37
381
+  {&DDRE, &PINE, &PORTE, 6}, // E6 38
382
+  {&DDRE, &PINE, &PORTE, 7}, // E7 39
383
+  {&DDRD, &PIND, &PORTD, 0}, // D0 40
384
+  {&DDRD, &PIND, &PORTD, 1}, // D1 41
385
+  {&DDRC, &PINC, &PORTC, 3}, // C3 42
386
+  {&DDRD, &PIND, &PORTD, 2}, // D2 43
387
+  {&DDRD, &PIND, &PORTD, 3}, // D3 44
388
+  {&DDRA, &PINA, &PORTA, 7}, // A7 45
389
+  {&DDRC, &PINC, &PORTC, 2}, // C2 46
390
+  {&DDRD, &PIND, &PORTD, 4}, // D4 47
391
+  {&DDRG, &PING, &PORTG, 2}, // G2 48
392
+  {&DDRG, &PING, &PORTG, 1}, // G1 49
393
+  {&DDRG, &PING, &PORTG, 0}, // G0 50
394
+};
395
+#else  // defined(__AVR_ATmega1280__)
396
+#error unknown chip
397
+#endif  // defined(__AVR_ATmega1280__)
398
+//------------------------------------------------------------------------------
399
+static const uint8_t digitalPinCount = COUNT(digitalPinMap);
400
+
401
+uint8_t badPinNumber(void)
402
+  __attribute__((error("Pin number is too large or not a constant")));
403
+
404
+static inline __attribute__((always_inline))
405
+  bool getPinMode(uint8_t pin) {
406
+  if (__builtin_constant_p(pin) && pin < digitalPinCount) {
407
+    return (*digitalPinMap[pin].ddr >> digitalPinMap[pin].bit) & 1;
408
+  }
409
+  else {
410
+    return badPinNumber();
411
+  }
412
+}
413
+static inline __attribute__((always_inline))
414
+  void setPinMode(uint8_t pin, uint8_t mode) {
415
+  if (__builtin_constant_p(pin) && pin < digitalPinCount) {
416
+    if (mode) {
417
+      SBI(*digitalPinMap[pin].ddr, digitalPinMap[pin].bit);
418
+    }
419
+    else {
420
+      CBI(*digitalPinMap[pin].ddr, digitalPinMap[pin].bit);
421
+    }
422
+  }
423
+  else {
424
+    badPinNumber();
425
+  }
426
+}
427
+static inline __attribute__((always_inline))
428
+  bool fastDigitalRead(uint8_t pin) {
429
+  if (__builtin_constant_p(pin) && pin < digitalPinCount) {
430
+    return (*digitalPinMap[pin].pin >> digitalPinMap[pin].bit) & 1;
431
+  }
432
+  else {
433
+    return badPinNumber();
434
+  }
435
+}
436
+static inline __attribute__((always_inline))
437
+  void fastDigitalWrite(uint8_t pin, uint8_t value) {
438
+  if (__builtin_constant_p(pin) && pin < digitalPinCount) {
439
+    if (value) {
440
+      SBI(*digitalPinMap[pin].port, digitalPinMap[pin].bit);
441
+    }
442
+    else {
443
+      CBI(*digitalPinMap[pin].port, digitalPinMap[pin].bit);
444
+    }
445
+  }
446
+  else {
447
+    badPinNumber();
448
+  }
449
+}
450
+#endif  // Sd2PinMap_h
451
+
452
+
453
+#endif

+ 57
- 57
Marlin/SdFatUtil.h Wyświetl plik

@@ -1,57 +1,57 @@
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
- * Arduino SdFat Library
25
- * Copyright (C) 2008 by William Greiman
26
- *
27
- * This file is part of the Arduino Sd2Card Library
28
- */
29
-#include "Marlin.h"
30
-#if ENABLED(SDSUPPORT)
31
-
32
-#ifndef SdFatUtil_h
33
-#define SdFatUtil_h
34
-/**
35
- * \file
36
- * \brief Useful utility functions.
37
- */
38
-#include "Marlin.h"
39
-#include "MarlinSerial.h"
40
-/** Store and print a string in flash memory.*/
41
-#define PgmPrint(x) SerialPrint_P(PSTR(x))
42
-/** Store and print a string in flash memory followed by a CR/LF.*/
43
-#define PgmPrintln(x) SerialPrintln_P(PSTR(x))
44
-
45
-namespace SdFatUtil {
46
-  int FreeRam();
47
-  void print_P(PGM_P str);
48
-  void println_P(PGM_P str);
49
-  void SerialPrint_P(PGM_P str);
50
-  void SerialPrintln_P(PGM_P str);
51
-}
52
-
53
-using namespace SdFatUtil;  // NOLINT
54
-#endif  //#define SdFatUtil_h
55
-
56
-
57
-#endif
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
+ * Arduino SdFat Library
25
+ * Copyright (C) 2008 by William Greiman
26
+ *
27
+ * This file is part of the Arduino Sd2Card Library
28
+ */
29
+#include "Marlin.h"
30
+#if ENABLED(SDSUPPORT)
31
+
32
+#ifndef SdFatUtil_h
33
+#define SdFatUtil_h
34
+/**
35
+ * \file
36
+ * \brief Useful utility functions.
37
+ */
38
+#include "Marlin.h"
39
+#include "MarlinSerial.h"
40
+/** Store and print a string in flash memory.*/
41
+#define PgmPrint(x) SerialPrint_P(PSTR(x))
42
+/** Store and print a string in flash memory followed by a CR/LF.*/
43
+#define PgmPrintln(x) SerialPrintln_P(PSTR(x))
44
+
45
+namespace SdFatUtil {
46
+  int FreeRam();
47
+  void print_P(PGM_P str);
48
+  void println_P(PGM_P str);
49
+  void SerialPrint_P(PGM_P str);
50
+  void SerialPrintln_P(PGM_P str);
51
+}
52
+
53
+using namespace SdFatUtil;  // NOLINT
54
+#endif  //#define SdFatUtil_h
55
+
56
+
57
+#endif

+ 102
- 102
Marlin/SdFile.cpp Wyświetl plik

@@ -1,102 +1,102 @@
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
- * Arduino SdFat Library
25
- * Copyright (C) 2009 by William Greiman
26
- *
27
- * This file is part of the Arduino Sd2Card Library
28
- */
29
-#include "Marlin.h"
30
-
31
-#if ENABLED(SDSUPPORT)
32
-#include "SdFile.h"
33
-/**  Create a file object and open it in the current working directory.
34
- *
35
- * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
36
- *
37
- * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
38
- * OR of open flags. see SdBaseFile::open(SdBaseFile*, const char*, uint8_t).
39
- */
40
-SdFile::SdFile(const char* path, uint8_t oflag) : SdBaseFile(path, oflag) {
41
-}
42
-//------------------------------------------------------------------------------
43
-/** Write data to an open file.
44
- *
45
- * \note Data is moved to the cache but may not be written to the
46
- * storage device until sync() is called.
47
- *
48
- * \param[in] buf Pointer to the location of the data to be written.
49
- *
50
- * \param[in] nbyte Number of bytes to write.
51
- *
52
- * \return For success write() returns the number of bytes written, always
53
- * \a nbyte.  If an error occurs, write() returns -1.  Possible errors
54
- * include write() is called before a file has been opened, write is called
55
- * for a read-only file, device is full, a corrupt file system or an I/O error.
56
- *
57
- */
58
-int16_t SdFile::write(const void* buf, uint16_t nbyte) {
59
-  return SdBaseFile::write(buf, nbyte);
60
-}
61
-//------------------------------------------------------------------------------
62
-/** Write a byte to a file. Required by the Arduino Print class.
63
- * \param[in] b the byte to be written.
64
- * Use writeError to check for errors.
65
- */
66
-#if ARDUINO >= 100
67
-  size_t SdFile::write(uint8_t b) {
68
-    return SdBaseFile::write(&b, 1);
69
-  }
70
-#else
71
-  void SdFile::write(uint8_t b) {
72
-    SdBaseFile::write(&b, 1);
73
-  }
74
-#endif
75
-//------------------------------------------------------------------------------
76
-/** Write a string to a file. Used by the Arduino Print class.
77
- * \param[in] str Pointer to the string.
78
- * Use writeError to check for errors.
79
- */
80
-void SdFile::write(const char* str) {
81
-  SdBaseFile::write(str, strlen(str));
82
-}
83
-//------------------------------------------------------------------------------
84
-/** Write a PROGMEM string to a file.
85
- * \param[in] str Pointer to the PROGMEM string.
86
- * Use writeError to check for errors.
87
- */
88
-void SdFile::write_P(PGM_P str) {
89
-  for (uint8_t c; (c = pgm_read_byte(str)); str++) write(c);
90
-}
91
-//------------------------------------------------------------------------------
92
-/** Write a PROGMEM string followed by CR/LF to a file.
93
- * \param[in] str Pointer to the PROGMEM string.
94
- * Use writeError to check for errors.
95
- */
96
-void SdFile::writeln_P(PGM_P str) {
97
-  write_P(str);
98
-  write_P(PSTR("\r\n"));
99
-}
100
-
101
-
102
-#endif
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
+ * Arduino SdFat Library
25
+ * Copyright (C) 2009 by William Greiman
26
+ *
27
+ * This file is part of the Arduino Sd2Card Library
28
+ */
29
+#include "Marlin.h"
30
+
31
+#if ENABLED(SDSUPPORT)
32
+#include "SdFile.h"
33
+/**  Create a file object and open it in the current working directory.
34
+ *
35
+ * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
36
+ *
37
+ * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
38
+ * OR of open flags. see SdBaseFile::open(SdBaseFile*, const char*, uint8_t).
39
+ */
40
+SdFile::SdFile(const char* path, uint8_t oflag) : SdBaseFile(path, oflag) {
41
+}
42
+//------------------------------------------------------------------------------
43
+/** Write data to an open file.
44
+ *
45
+ * \note Data is moved to the cache but may not be written to the
46
+ * storage device until sync() is called.
47
+ *
48
+ * \param[in] buf Pointer to the location of the data to be written.
49
+ *
50
+ * \param[in] nbyte Number of bytes to write.
51
+ *
52
+ * \return For success write() returns the number of bytes written, always
53
+ * \a nbyte.  If an error occurs, write() returns -1.  Possible errors
54
+ * include write() is called before a file has been opened, write is called
55
+ * for a read-only file, device is full, a corrupt file system or an I/O error.
56
+ *
57
+ */
58
+int16_t SdFile::write(const void* buf, uint16_t nbyte) {
59
+  return SdBaseFile::write(buf, nbyte);
60
+}
61
+//------------------------------------------------------------------------------
62
+/** Write a byte to a file. Required by the Arduino Print class.
63
+ * \param[in] b the byte to be written.
64
+ * Use writeError to check for errors.
65
+ */
66
+#if ARDUINO >= 100
67
+  size_t SdFile::write(uint8_t b) {
68
+    return SdBaseFile::write(&b, 1);
69
+  }
70
+#else
71
+  void SdFile::write(uint8_t b) {
72
+    SdBaseFile::write(&b, 1);
73
+  }
74
+#endif
75
+//------------------------------------------------------------------------------
76
+/** Write a string to a file. Used by the Arduino Print class.
77
+ * \param[in] str Pointer to the string.
78
+ * Use writeError to check for errors.
79
+ */
80
+void SdFile::write(const char* str) {
81
+  SdBaseFile::write(str, strlen(str));
82
+}
83
+//------------------------------------------------------------------------------
84
+/** Write a PROGMEM string to a file.
85
+ * \param[in] str Pointer to the PROGMEM string.
86
+ * Use writeError to check for errors.
87
+ */
88
+void SdFile::write_P(PGM_P str) {
89
+  for (uint8_t c; (c = pgm_read_byte(str)); str++) write(c);
90
+}
91
+//------------------------------------------------------------------------------
92
+/** Write a PROGMEM string followed by CR/LF to a file.
93
+ * \param[in] str Pointer to the PROGMEM string.
94
+ * Use writeError to check for errors.
95
+ */
96
+void SdFile::writeln_P(PGM_P str) {
97
+  write_P(str);
98
+  write_P(PSTR("\r\n"));
99
+}
100
+
101
+
102
+#endif

+ 289
- 289
Marlin/SdInfo.h Wyświetl plik

@@ -1,289 +1,289 @@
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
- * Arduino Sd2Card Library
25
- * Copyright (C) 2009 by William Greiman
26
- *
27
- * This file is part of the Arduino Sd2Card Library
28
- */
29
-#include "Marlin.h"
30
-#if ENABLED(SDSUPPORT)
31
-
32
-#ifndef SdInfo_h
33
-#define SdInfo_h
34
-#include <stdint.h>
35
-// Based on the document:
36
-//
37
-// SD Specifications
38
-// Part 1
39
-// Physical Layer
40
-// Simplified Specification
41
-// Version 3.01
42
-// May 18, 2010
43
-//
44
-// http://www.sdcard.org/developers/tech/sdcard/pls/simplified_specs
45
-//------------------------------------------------------------------------------
46
-// SD card commands
47
-/** GO_IDLE_STATE - init card in spi mode if CS low */
48
-uint8_t const CMD0 = 0X00;
49
-/** SEND_IF_COND - verify SD Memory Card interface operating condition.*/
50
-uint8_t const CMD8 = 0X08;
51
-/** SEND_CSD - read the Card Specific Data (CSD register) */
52
-uint8_t const CMD9 = 0X09;
53
-/** SEND_CID - read the card identification information (CID register) */
54
-uint8_t const CMD10 = 0X0A;
55
-/** STOP_TRANSMISSION - end multiple block read sequence */
56
-uint8_t const CMD12 = 0X0C;
57
-/** SEND_STATUS - read the card status register */
58
-uint8_t const CMD13 = 0X0D;
59
-/** READ_SINGLE_BLOCK - read a single data block from the card */
60
-uint8_t const CMD17 = 0X11;
61
-/** READ_MULTIPLE_BLOCK - read a multiple data blocks from the card */
62
-uint8_t const CMD18 = 0X12;
63
-/** WRITE_BLOCK - write a single data block to the card */
64
-uint8_t const CMD24 = 0X18;
65
-/** WRITE_MULTIPLE_BLOCK - write blocks of data until a STOP_TRANSMISSION */
66
-uint8_t const CMD25 = 0X19;
67
-/** ERASE_WR_BLK_START - sets the address of the first block to be erased */
68
-uint8_t const CMD32 = 0X20;
69
-/** ERASE_WR_BLK_END - sets the address of the last block of the continuous
70
-    range to be erased*/
71
-uint8_t const CMD33 = 0X21;
72
-/** ERASE - erase all previously selected blocks */
73
-uint8_t const CMD38 = 0X26;
74
-/** APP_CMD - escape for application specific command */
75
-uint8_t const CMD55 = 0X37;
76
-/** READ_OCR - read the OCR register of a card */
77
-uint8_t const CMD58 = 0X3A;
78
-/** SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be
79
-     pre-erased before writing */
80
-uint8_t const ACMD23 = 0X17;
81
-/** SD_SEND_OP_COMD - Sends host capacity support information and
82
-    activates the card's initialization process */
83
-uint8_t const ACMD41 = 0X29;
84
-//------------------------------------------------------------------------------
85
-/** status for card in the ready state */
86
-uint8_t const R1_READY_STATE = 0X00;
87
-/** status for card in the idle state */
88
-uint8_t const R1_IDLE_STATE = 0X01;
89
-/** status bit for illegal command */
90
-uint8_t const R1_ILLEGAL_COMMAND = 0X04;
91
-/** start data token for read or write single block*/
92
-uint8_t const DATA_START_BLOCK = 0XFE;
93
-/** stop token for write multiple blocks*/
94
-uint8_t const STOP_TRAN_TOKEN = 0XFD;
95
-/** start data token for write multiple blocks*/
96
-uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
97
-/** mask for data response tokens after a write block operation */
98
-uint8_t const DATA_RES_MASK = 0X1F;
99
-/** write data accepted token */
100
-uint8_t const DATA_RES_ACCEPTED = 0X05;
101
-//------------------------------------------------------------------------------
102
-/** Card IDentification (CID) register */
103
-typedef struct CID {
104
-  // byte 0
105
-  /** Manufacturer ID */
106
-  unsigned char mid;
107
-  // byte 1-2
108
-  /** OEM/Application ID */
109
-  char oid[2];
110
-  // byte 3-7
111
-  /** Product name */
112
-  char pnm[5];
113
-  // byte 8
114
-  /** Product revision least significant digit */
115
-  unsigned char prv_m : 4;
116
-  /** Product revision most significant digit */
117
-  unsigned char prv_n : 4;
118
-  // byte 9-12
119
-  /** Product serial number */
120
-  uint32_t psn;
121
-  // byte 13
122
-  /** Manufacturing date year low digit */
123
-  unsigned char mdt_year_high : 4;
124
-  /** not used */
125
-  unsigned char reserved : 4;
126
-  // byte 14
127
-  /** Manufacturing date month */
128
-  unsigned char mdt_month : 4;
129
-  /** Manufacturing date year low digit */
130
-  unsigned char mdt_year_low : 4;
131
-  // byte 15
132
-  /** not used always 1 */
133
-  unsigned char always1 : 1;
134
-  /** CRC7 checksum */
135
-  unsigned char crc : 7;
136
-} cid_t;
137
-//------------------------------------------------------------------------------
138
-/** CSD for version 1.00 cards */
139
-typedef struct CSDV1 {
140
-  // byte 0
141
-  unsigned char reserved1 : 6;
142
-  unsigned char csd_ver : 2;
143
-  // byte 1
144
-  unsigned char taac;
145
-  // byte 2
146
-  unsigned char nsac;
147
-  // byte 3
148
-  unsigned char tran_speed;
149
-  // byte 4
150
-  unsigned char ccc_high;
151
-  // byte 5
152
-  unsigned char read_bl_len : 4;
153
-  unsigned char ccc_low : 4;
154
-  // byte 6
155
-  unsigned char c_size_high : 2;
156
-  unsigned char reserved2 : 2;
157
-  unsigned char dsr_imp : 1;
158
-  unsigned char read_blk_misalign : 1;
159
-  unsigned char write_blk_misalign : 1;
160
-  unsigned char read_bl_partial : 1;
161
-  // byte 7
162
-  unsigned char c_size_mid;
163
-  // byte 8
164
-  unsigned char vdd_r_curr_max : 3;
165
-  unsigned char vdd_r_curr_min : 3;
166
-  unsigned char c_size_low : 2;
167
-  // byte 9
168
-  unsigned char c_size_mult_high : 2;
169
-  unsigned char vdd_w_cur_max : 3;
170
-  unsigned char vdd_w_curr_min : 3;
171
-  // byte 10
172
-  unsigned char sector_size_high : 6;
173
-  unsigned char erase_blk_en : 1;
174
-  unsigned char c_size_mult_low : 1;
175
-  // byte 11
176
-  unsigned char wp_grp_size : 7;
177
-  unsigned char sector_size_low : 1;
178
-  // byte 12
179
-  unsigned char write_bl_len_high : 2;
180
-  unsigned char r2w_factor : 3;
181
-  unsigned char reserved3 : 2;
182
-  unsigned char wp_grp_enable : 1;
183
-  // byte 13
184
-  unsigned char reserved4 : 5;
185
-  unsigned char write_partial : 1;
186
-  unsigned char write_bl_len_low : 2;
187
-  // byte 14
188
-  unsigned char reserved5: 2;
189
-  unsigned char file_format : 2;
190
-  unsigned char tmp_write_protect : 1;
191
-  unsigned char perm_write_protect : 1;
192
-  unsigned char copy : 1;
193
-  /** Indicates the file format on the card */
194
-  unsigned char file_format_grp : 1;
195
-  // byte 15
196
-  unsigned char always1 : 1;
197
-  unsigned char crc : 7;
198
-} csd1_t;
199
-//------------------------------------------------------------------------------
200
-/** CSD for version 2.00 cards */
201
-typedef struct CSDV2 {
202
-  // byte 0
203
-  unsigned char reserved1 : 6;
204
-  unsigned char csd_ver : 2;
205
-  // byte 1
206
-  /** fixed to 0X0E */
207
-  unsigned char taac;
208
-  // byte 2
209
-  /** fixed to 0 */
210
-  unsigned char nsac;
211
-  // byte 3
212
-  unsigned char tran_speed;
213
-  // byte 4
214
-  unsigned char ccc_high;
215
-  // byte 5
216
-  /** This field is fixed to 9h, which indicates READ_BL_LEN=512 Byte */
217
-  unsigned char read_bl_len : 4;
218
-  unsigned char ccc_low : 4;
219
-  // byte 6
220
-  /** not used */
221
-  unsigned char reserved2 : 4;
222
-  unsigned char dsr_imp : 1;
223
-  /** fixed to 0 */
224
-  unsigned char read_blk_misalign : 1;
225
-  /** fixed to 0 */
226
-  unsigned char write_blk_misalign : 1;
227
-  /** fixed to 0 - no partial read */
228
-  unsigned char read_bl_partial : 1;
229
-  // byte 7
230
-  /** not used */
231
-  unsigned char reserved3 : 2;
232
-  /** high part of card size */
233
-  unsigned char c_size_high : 6;
234
-  // byte 8
235
-  /** middle part of card size */
236
-  unsigned char c_size_mid;
237
-  // byte 9
238
-  /** low part of card size */
239
-  unsigned char c_size_low;
240
-  // byte 10
241
-  /** sector size is fixed at 64 KB */
242
-  unsigned char sector_size_high : 6;
243
-  /** fixed to 1 - erase single is supported */
244
-  unsigned char erase_blk_en : 1;
245
-  /** not used */
246
-  unsigned char reserved4 : 1;
247
-  // byte 11
248
-  unsigned char wp_grp_size : 7;
249
-  /** sector size is fixed at 64 KB */
250
-  unsigned char sector_size_low : 1;
251
-  // byte 12
252
-  /** write_bl_len fixed for 512 byte blocks */
253
-  unsigned char write_bl_len_high : 2;
254
-  /** fixed value of 2 */
255
-  unsigned char r2w_factor : 3;
256
-  /** not used */
257
-  unsigned char reserved5 : 2;
258
-  /** fixed value of 0 - no write protect groups */
259
-  unsigned char wp_grp_enable : 1;
260
-  // byte 13
261
-  unsigned char reserved6 : 5;
262
-  /** always zero - no partial block read*/
263
-  unsigned char write_partial : 1;
264
-  /** write_bl_len fixed for 512 byte blocks */
265
-  unsigned char write_bl_len_low : 2;
266
-  // byte 14
267
-  unsigned char reserved7: 2;
268
-  /** Do not use always 0 */
269
-  unsigned char file_format : 2;
270
-  unsigned char tmp_write_protect : 1;
271
-  unsigned char perm_write_protect : 1;
272
-  unsigned char copy : 1;
273
-  /** Do not use always 0 */
274
-  unsigned char file_format_grp : 1;
275
-  // byte 15
276
-  /** not used always 1 */
277
-  unsigned char always1 : 1;
278
-  /** checksum */
279
-  unsigned char crc : 7;
280
-} csd2_t;
281
-//------------------------------------------------------------------------------
282
-/** union of old and new style CSD register */
283
-union csd_t {
284
-  csd1_t v1;
285
-  csd2_t v2;
286
-};
287
-#endif  // SdInfo_h
288
-
289
-#endif
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
+ * Arduino Sd2Card Library
25
+ * Copyright (C) 2009 by William Greiman
26
+ *
27
+ * This file is part of the Arduino Sd2Card Library
28
+ */
29
+#include "Marlin.h"
30
+#if ENABLED(SDSUPPORT)
31
+
32
+#ifndef SdInfo_h
33
+#define SdInfo_h
34
+#include <stdint.h>
35
+// Based on the document:
36
+//
37
+// SD Specifications
38
+// Part 1
39
+// Physical Layer
40
+// Simplified Specification
41
+// Version 3.01
42
+// May 18, 2010
43
+//
44
+// http://www.sdcard.org/developers/tech/sdcard/pls/simplified_specs
45
+//------------------------------------------------------------------------------
46
+// SD card commands
47
+/** GO_IDLE_STATE - init card in spi mode if CS low */
48
+uint8_t const CMD0 = 0X00;
49
+/** SEND_IF_COND - verify SD Memory Card interface operating condition.*/
50
+uint8_t const CMD8 = 0X08;
51
+/** SEND_CSD - read the Card Specific Data (CSD register) */
52
+uint8_t const CMD9 = 0X09;
53
+/** SEND_CID - read the card identification information (CID register) */
54
+uint8_t const CMD10 = 0X0A;
55
+/** STOP_TRANSMISSION - end multiple block read sequence */
56
+uint8_t const CMD12 = 0X0C;
57
+/** SEND_STATUS - read the card status register */
58
+uint8_t const CMD13 = 0X0D;
59
+/** READ_SINGLE_BLOCK - read a single data block from the card */
60
+uint8_t const CMD17 = 0X11;
61
+/** READ_MULTIPLE_BLOCK - read a multiple data blocks from the card */
62
+uint8_t const CMD18 = 0X12;
63
+/** WRITE_BLOCK - write a single data block to the card */
64
+uint8_t const CMD24 = 0X18;
65
+/** WRITE_MULTIPLE_BLOCK - write blocks of data until a STOP_TRANSMISSION */
66
+uint8_t const CMD25 = 0X19;
67
+/** ERASE_WR_BLK_START - sets the address of the first block to be erased */
68
+uint8_t const CMD32 = 0X20;
69
+/** ERASE_WR_BLK_END - sets the address of the last block of the continuous
70
+    range to be erased*/
71
+uint8_t const CMD33 = 0X21;
72
+/** ERASE - erase all previously selected blocks */
73
+uint8_t const CMD38 = 0X26;
74
+/** APP_CMD - escape for application specific command */
75
+uint8_t const CMD55 = 0X37;
76
+/** READ_OCR - read the OCR register of a card */
77
+uint8_t const CMD58 = 0X3A;
78
+/** SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be
79
+     pre-erased before writing */
80
+uint8_t const ACMD23 = 0X17;
81
+/** SD_SEND_OP_COMD - Sends host capacity support information and
82
+    activates the card's initialization process */
83
+uint8_t const ACMD41 = 0X29;
84
+//------------------------------------------------------------------------------
85
+/** status for card in the ready state */
86
+uint8_t const R1_READY_STATE = 0X00;
87
+/** status for card in the idle state */
88
+uint8_t const R1_IDLE_STATE = 0X01;
89
+/** status bit for illegal command */
90
+uint8_t const R1_ILLEGAL_COMMAND = 0X04;
91
+/** start data token for read or write single block*/
92
+uint8_t const DATA_START_BLOCK = 0XFE;
93
+/** stop token for write multiple blocks*/
94
+uint8_t const STOP_TRAN_TOKEN = 0XFD;
95
+/** start data token for write multiple blocks*/
96
+uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
97
+/** mask for data response tokens after a write block operation */
98
+uint8_t const DATA_RES_MASK = 0X1F;
99
+/** write data accepted token */
100
+uint8_t const DATA_RES_ACCEPTED = 0X05;
101
+//------------------------------------------------------------------------------
102
+/** Card IDentification (CID) register */
103
+typedef struct CID {
104
+  // byte 0
105
+  /** Manufacturer ID */
106
+  unsigned char mid;
107
+  // byte 1-2
108
+  /** OEM/Application ID */
109
+  char oid[2];
110
+  // byte 3-7
111
+  /** Product name */
112
+  char pnm[5];
113
+  // byte 8
114
+  /** Product revision least significant digit */
115
+  unsigned char prv_m : 4;
116
+  /** Product revision most significant digit */
117
+  unsigned char prv_n : 4;
118
+  // byte 9-12
119
+  /** Product serial number */
120
+  uint32_t psn;
121
+  // byte 13
122
+  /** Manufacturing date year low digit */
123
+  unsigned char mdt_year_high : 4;
124
+  /** not used */
125
+  unsigned char reserved : 4;
126
+  // byte 14
127
+  /** Manufacturing date month */
128
+  unsigned char mdt_month : 4;
129
+  /** Manufacturing date year low digit */
130
+  unsigned char mdt_year_low : 4;
131
+  // byte 15
132
+  /** not used always 1 */
133
+  unsigned char always1 : 1;
134
+  /** CRC7 checksum */
135
+  unsigned char crc : 7;
136
+} cid_t;
137
+//------------------------------------------------------------------------------
138
+/** CSD for version 1.00 cards */
139
+typedef struct CSDV1 {
140
+  // byte 0
141
+  unsigned char reserved1 : 6;
142
+  unsigned char csd_ver : 2;
143
+  // byte 1
144
+  unsigned char taac;
145
+  // byte 2
146
+  unsigned char nsac;
147
+  // byte 3
148
+  unsigned char tran_speed;
149
+  // byte 4
150
+  unsigned char ccc_high;
151
+  // byte 5
152
+  unsigned char read_bl_len : 4;
153
+  unsigned char ccc_low : 4;
154
+  // byte 6
155
+  unsigned char c_size_high : 2;
156
+  unsigned char reserved2 : 2;
157
+  unsigned char dsr_imp : 1;
158
+  unsigned char read_blk_misalign : 1;
159
+  unsigned char write_blk_misalign : 1;
160
+  unsigned char read_bl_partial : 1;
161
+  // byte 7
162
+  unsigned char c_size_mid;
163
+  // byte 8
164
+  unsigned char vdd_r_curr_max : 3;
165
+  unsigned char vdd_r_curr_min : 3;
166
+  unsigned char c_size_low : 2;
167
+  // byte 9
168
+  unsigned char c_size_mult_high : 2;
169
+  unsigned char vdd_w_cur_max : 3;
170
+  unsigned char vdd_w_curr_min : 3;
171
+  // byte 10
172
+  unsigned char sector_size_high : 6;
173
+  unsigned char erase_blk_en : 1;
174
+  unsigned char c_size_mult_low : 1;
175
+  // byte 11
176
+  unsigned char wp_grp_size : 7;
177
+  unsigned char sector_size_low : 1;
178
+  // byte 12
179
+  unsigned char write_bl_len_high : 2;
180
+  unsigned char r2w_factor : 3;
181
+  unsigned char reserved3 : 2;
182
+  unsigned char wp_grp_enable : 1;
183
+  // byte 13
184
+  unsigned char reserved4 : 5;
185
+  unsigned char write_partial : 1;
186
+  unsigned char write_bl_len_low : 2;
187
+  // byte 14
188
+  unsigned char reserved5: 2;
189
+  unsigned char file_format : 2;
190
+  unsigned char tmp_write_protect : 1;
191
+  unsigned char perm_write_protect : 1;
192
+  unsigned char copy : 1;
193
+  /** Indicates the file format on the card */
194
+  unsigned char file_format_grp : 1;
195
+  // byte 15
196
+  unsigned char always1 : 1;
197
+  unsigned char crc : 7;
198
+} csd1_t;
199
+//------------------------------------------------------------------------------
200
+/** CSD for version 2.00 cards */
201
+typedef struct CSDV2 {
202
+  // byte 0
203
+  unsigned char reserved1 : 6;
204
+  unsigned char csd_ver : 2;
205
+  // byte 1
206
+  /** fixed to 0X0E */
207
+  unsigned char taac;
208
+  // byte 2
209
+  /** fixed to 0 */
210
+  unsigned char nsac;
211
+  // byte 3
212
+  unsigned char tran_speed;
213
+  // byte 4
214
+  unsigned char ccc_high;
215
+  // byte 5
216
+  /** This field is fixed to 9h, which indicates READ_BL_LEN=512 Byte */
217
+  unsigned char read_bl_len : 4;
218
+  unsigned char ccc_low : 4;
219
+  // byte 6
220
+  /** not used */
221
+  unsigned char reserved2 : 4;
222
+  unsigned char dsr_imp : 1;
223
+  /** fixed to 0 */
224
+  unsigned char read_blk_misalign : 1;
225
+  /** fixed to 0 */
226
+  unsigned char write_blk_misalign : 1;
227
+  /** fixed to 0 - no partial read */
228
+  unsigned char read_bl_partial : 1;
229
+  // byte 7
230
+  /** not used */
231
+  unsigned char reserved3 : 2;
232
+  /** high part of card size */
233
+  unsigned char c_size_high : 6;
234
+  // byte 8
235
+  /** middle part of card size */
236
+  unsigned char c_size_mid;
237
+  // byte 9
238
+  /** low part of card size */
239
+  unsigned char c_size_low;
240
+  // byte 10
241
+  /** sector size is fixed at 64 KB */
242
+  unsigned char sector_size_high : 6;
243
+  /** fixed to 1 - erase single is supported */
244
+  unsigned char erase_blk_en : 1;
245
+  /** not used */
246
+  unsigned char reserved4 : 1;
247
+  // byte 11
248
+  unsigned char wp_grp_size : 7;
249
+  /** sector size is fixed at 64 KB */
250
+  unsigned char sector_size_low : 1;
251
+  // byte 12
252
+  /** write_bl_len fixed for 512 byte blocks */
253
+  unsigned char write_bl_len_high : 2;
254
+  /** fixed value of 2 */
255
+  unsigned char r2w_factor : 3;
256
+  /** not used */
257
+  unsigned char reserved5 : 2;
258
+  /** fixed value of 0 - no write protect groups */
259
+  unsigned char wp_grp_enable : 1;
260
+  // byte 13
261
+  unsigned char reserved6 : 5;
262
+  /** always zero - no partial block read*/
263
+  unsigned char write_partial : 1;
264
+  /** write_bl_len fixed for 512 byte blocks */
265
+  unsigned char write_bl_len_low : 2;
266
+  // byte 14
267
+  unsigned char reserved7: 2;
268
+  /** Do not use always 0 */
269
+  unsigned char file_format : 2;
270
+  unsigned char tmp_write_protect : 1;
271
+  unsigned char perm_write_protect : 1;
272
+  unsigned char copy : 1;
273
+  /** Do not use always 0 */
274
+  unsigned char file_format_grp : 1;
275
+  // byte 15
276
+  /** not used always 1 */
277
+  unsigned char always1 : 1;
278
+  /** checksum */
279
+  unsigned char crc : 7;
280
+} csd2_t;
281
+//------------------------------------------------------------------------------
282
+/** union of old and new style CSD register */
283
+union csd_t {
284
+  csd1_t v1;
285
+  csd2_t v2;
286
+};
287
+#endif  // SdInfo_h
288
+
289
+#endif

+ 420
- 420
Marlin/SdVolume.cpp Wyświetl plik

@@ -1,420 +1,420 @@
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
- * Arduino SdFat Library
25
- * Copyright (C) 2009 by William Greiman
26
- *
27
- * This file is part of the Arduino Sd2Card Library
28
- */
29
-#include "Marlin.h"
30
-#if ENABLED(SDSUPPORT)
31
-
32
-#include "SdVolume.h"
33
-//------------------------------------------------------------------------------
34
-#if !USE_MULTIPLE_CARDS
35
-  // raw block cache
36
-  uint32_t SdVolume::cacheBlockNumber_;  // current block number
37
-  cache_t  SdVolume::cacheBuffer_;       // 512 byte cache for Sd2Card
38
-  Sd2Card* SdVolume::sdCard_;            // pointer to SD card object
39
-  bool     SdVolume::cacheDirty_;        // cacheFlush() will write block if true
40
-  uint32_t SdVolume::cacheMirrorBlock_;  // mirror  block for second FAT
41
-#endif  // USE_MULTIPLE_CARDS
42
-//------------------------------------------------------------------------------
43
-// find a contiguous group of clusters
44
-bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
45
-  // start of group
46
-  uint32_t bgnCluster;
47
-  // end of group
48
-  uint32_t endCluster;
49
-  // last cluster of FAT
50
-  uint32_t fatEnd = clusterCount_ + 1;
51
-
52
-  // flag to save place to start next search
53
-  bool setStart;
54
-
55
-  // set search start cluster
56
-  if (*curCluster) {
57
-    // try to make file contiguous
58
-    bgnCluster = *curCluster + 1;
59
-
60
-    // don't save new start location
61
-    setStart = false;
62
-  }
63
-  else {
64
-    // start at likely place for free cluster
65
-    bgnCluster = allocSearchStart_;
66
-
67
-    // save next search start if one cluster
68
-    setStart = count == 1;
69
-  }
70
-  // end of group
71
-  endCluster = bgnCluster;
72
-
73
-  // search the FAT for free clusters
74
-  for (uint32_t n = 0;; n++, endCluster++) {
75
-    // can't find space checked all clusters
76
-    if (n >= clusterCount_) goto fail;
77
-
78
-    // past end - start from beginning of FAT
79
-    if (endCluster > fatEnd) {
80
-      bgnCluster = endCluster = 2;
81
-    }
82
-    uint32_t f;
83
-    if (!fatGet(endCluster, &f)) goto fail;
84
-
85
-    if (f != 0) {
86
-      // cluster in use try next cluster as bgnCluster
87
-      bgnCluster = endCluster + 1;
88
-    }
89
-    else if ((endCluster - bgnCluster + 1) == count) {
90
-      // done - found space
91
-      break;
92
-    }
93
-  }
94
-  // mark end of chain
95
-  if (!fatPutEOC(endCluster)) goto fail;
96
-
97
-  // link clusters
98
-  while (endCluster > bgnCluster) {
99
-    if (!fatPut(endCluster - 1, endCluster)) goto fail;
100
-    endCluster--;
101
-  }
102
-  if (*curCluster != 0) {
103
-    // connect chains
104
-    if (!fatPut(*curCluster, bgnCluster)) goto fail;
105
-  }
106
-  // return first cluster number to caller
107
-  *curCluster = bgnCluster;
108
-
109
-  // remember possible next free cluster
110
-  if (setStart) allocSearchStart_ = bgnCluster + 1;
111
-
112
-  return true;
113
-fail:
114
-  return false;
115
-}
116
-//------------------------------------------------------------------------------
117
-bool SdVolume::cacheFlush() {
118
-  if (cacheDirty_) {
119
-    if (!sdCard_->writeBlock(cacheBlockNumber_, cacheBuffer_.data)) {
120
-      goto fail;
121
-    }
122
-    // mirror FAT tables
123
-    if (cacheMirrorBlock_) {
124
-      if (!sdCard_->writeBlock(cacheMirrorBlock_, cacheBuffer_.data)) {
125
-        goto fail;
126
-      }
127
-      cacheMirrorBlock_ = 0;
128
-    }
129
-    cacheDirty_ = 0;
130
-  }
131
-  return true;
132
-fail:
133
-  return false;
134
-}
135
-//------------------------------------------------------------------------------
136
-bool SdVolume::cacheRawBlock(uint32_t blockNumber, bool dirty) {
137
-  if (cacheBlockNumber_ != blockNumber) {
138
-    if (!cacheFlush()) goto fail;
139
-    if (!sdCard_->readBlock(blockNumber, cacheBuffer_.data)) goto fail;
140
-    cacheBlockNumber_ = blockNumber;
141
-  }
142
-  if (dirty) cacheDirty_ = true;
143
-  return true;
144
-fail:
145
-  return false;
146
-}
147
-//------------------------------------------------------------------------------
148
-// return the size in bytes of a cluster chain
149
-bool SdVolume::chainSize(uint32_t cluster, uint32_t* size) {
150
-  uint32_t s = 0;
151
-  do {
152
-    if (!fatGet(cluster, &cluster)) goto fail;
153
-    s += 512UL << clusterSizeShift_;
154
-  } while (!isEOC(cluster));
155
-  *size = s;
156
-  return true;
157
-fail:
158
-  return false;
159
-}
160
-//------------------------------------------------------------------------------
161
-// Fetch a FAT entry
162
-bool SdVolume::fatGet(uint32_t cluster, uint32_t* value) {
163
-  uint32_t lba;
164
-  if (cluster > (clusterCount_ + 1)) goto fail;
165
-  if (FAT12_SUPPORT && fatType_ == 12) {
166
-    uint16_t index = cluster;
167
-    index += index >> 1;
168
-    lba = fatStartBlock_ + (index >> 9);
169
-    if (!cacheRawBlock(lba, CACHE_FOR_READ)) goto fail;
170
-    index &= 0X1FF;
171
-    uint16_t tmp = cacheBuffer_.data[index];
172
-    index++;
173
-    if (index == 512) {
174
-      if (!cacheRawBlock(lba + 1, CACHE_FOR_READ)) goto fail;
175
-      index = 0;
176
-    }
177
-    tmp |= cacheBuffer_.data[index] << 8;
178
-    *value = cluster & 1 ? tmp >> 4 : tmp & 0XFFF;
179
-    return true;
180
-  }
181
-  if (fatType_ == 16) {
182
-    lba = fatStartBlock_ + (cluster >> 8);
183
-  }
184
-  else if (fatType_ == 32) {
185
-    lba = fatStartBlock_ + (cluster >> 7);
186
-  }
187
-  else {
188
-    goto fail;
189
-  }
190
-  if (lba != cacheBlockNumber_) {
191
-    if (!cacheRawBlock(lba, CACHE_FOR_READ)) goto fail;
192
-  }
193
-  if (fatType_ == 16) {
194
-    *value = cacheBuffer_.fat16[cluster & 0XFF];
195
-  }
196
-  else {
197
-    *value = cacheBuffer_.fat32[cluster & 0X7F] & FAT32MASK;
198
-  }
199
-  return true;
200
-fail:
201
-  return false;
202
-}
203
-//------------------------------------------------------------------------------
204
-// Store a FAT entry
205
-bool SdVolume::fatPut(uint32_t cluster, uint32_t value) {
206
-  uint32_t lba;
207
-  // error if reserved cluster
208
-  if (cluster < 2) goto fail;
209
-
210
-  // error if not in FAT
211
-  if (cluster > (clusterCount_ + 1)) goto fail;
212
-
213
-  if (FAT12_SUPPORT && fatType_ == 12) {
214
-    uint16_t index = cluster;
215
-    index += index >> 1;
216
-    lba = fatStartBlock_ + (index >> 9);
217
-    if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail;
218
-    // mirror second FAT
219
-    if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
220
-    index &= 0X1FF;
221
-    uint8_t tmp = value;
222
-    if (cluster & 1) {
223
-      tmp = (cacheBuffer_.data[index] & 0XF) | tmp << 4;
224
-    }
225
-    cacheBuffer_.data[index] = tmp;
226
-    index++;
227
-    if (index == 512) {
228
-      lba++;
229
-      index = 0;
230
-      if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail;
231
-      // mirror second FAT
232
-      if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
233
-    }
234
-    tmp = value >> 4;
235
-    if (!(cluster & 1)) {
236
-      tmp = ((cacheBuffer_.data[index] & 0XF0)) | tmp >> 4;
237
-    }
238
-    cacheBuffer_.data[index] = tmp;
239
-    return true;
240
-  }
241
-  if (fatType_ == 16) {
242
-    lba = fatStartBlock_ + (cluster >> 8);
243
-  }
244
-  else if (fatType_ == 32) {
245
-    lba = fatStartBlock_ + (cluster >> 7);
246
-  }
247
-  else {
248
-    goto fail;
249
-  }
250
-  if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail;
251
-  // store entry
252
-  if (fatType_ == 16) {
253
-    cacheBuffer_.fat16[cluster & 0XFF] = value;
254
-  }
255
-  else {
256
-    cacheBuffer_.fat32[cluster & 0X7F] = value;
257
-  }
258
-  // mirror second FAT
259
-  if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
260
-  return true;
261
-fail:
262
-  return false;
263
-}
264
-//------------------------------------------------------------------------------
265
-// free a cluster chain
266
-bool SdVolume::freeChain(uint32_t cluster) {
267
-  uint32_t next;
268
-
269
-  // clear free cluster location
270
-  allocSearchStart_ = 2;
271
-
272
-  do {
273
-    if (!fatGet(cluster, &next)) goto fail;
274
-
275
-    // free cluster
276
-    if (!fatPut(cluster, 0)) goto fail;
277
-
278
-    cluster = next;
279
-  } while (!isEOC(cluster));
280
-
281
-  return true;
282
-fail:
283
-  return false;
284
-}
285
-//------------------------------------------------------------------------------
286
-/** Volume free space in clusters.
287
- *
288
- * \return Count of free clusters for success or -1 if an error occurs.
289
- */
290
-int32_t SdVolume::freeClusterCount() {
291
-  uint32_t free = 0;
292
-  uint16_t n;
293
-  uint32_t todo = clusterCount_ + 2;
294
-
295
-  if (fatType_ == 16) {
296
-    n = 256;
297
-  }
298
-  else if (fatType_ == 32) {
299
-    n = 128;
300
-  }
301
-  else {
302
-    // put FAT12 here
303
-    return -1;
304
-  }
305
-
306
-  for (uint32_t lba = fatStartBlock_; todo; todo -= n, lba++) {
307
-    if (!cacheRawBlock(lba, CACHE_FOR_READ)) return -1;
308
-    NOMORE(n, todo);
309
-    if (fatType_ == 16) {
310
-      for (uint16_t i = 0; i < n; i++) {
311
-        if (cacheBuffer_.fat16[i] == 0) free++;
312
-      }
313
-    }
314
-    else {
315
-      for (uint16_t i = 0; i < n; i++) {
316
-        if (cacheBuffer_.fat32[i] == 0) free++;
317
-      }
318
-    }
319
-  }
320
-  return free;
321
-}
322
-//------------------------------------------------------------------------------
323
-/** Initialize a FAT volume.
324
- *
325
- * \param[in] dev The SD card where the volume is located.
326
- *
327
- * \param[in] part The partition to be used.  Legal values for \a part are
328
- * 1-4 to use the corresponding partition on a device formatted with
329
- * a MBR, Master Boot Record, or zero if the device is formatted as
330
- * a super floppy with the FAT boot sector in block zero.
331
- *
332
- * \return The value one, true, is returned for success and
333
- * the value zero, false, is returned for failure.  Reasons for
334
- * failure include not finding a valid partition, not finding a valid
335
- * FAT file system in the specified partition or an I/O error.
336
- */
337
-bool SdVolume::init(Sd2Card* dev, uint8_t part) {
338
-  uint32_t totalBlocks;
339
-  uint32_t volumeStartBlock = 0;
340
-  fat32_boot_t* fbs;
341
-
342
-  sdCard_ = dev;
343
-  fatType_ = 0;
344
-  allocSearchStart_ = 2;
345
-  cacheDirty_ = 0;  // cacheFlush() will write block if true
346
-  cacheMirrorBlock_ = 0;
347
-  cacheBlockNumber_ = 0XFFFFFFFF;
348
-
349
-  // if part == 0 assume super floppy with FAT boot sector in block zero
350
-  // if part > 0 assume mbr volume with partition table
351
-  if (part) {
352
-    if (part > 4)goto fail;
353
-    if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) goto fail;
354
-    part_t* p = &cacheBuffer_.mbr.part[part - 1];
355
-    if ((p->boot & 0X7F) != 0  ||
356
-        p->totalSectors < 100 ||
357
-        p->firstSector == 0) {
358
-      // not a valid partition
359
-      goto fail;
360
-    }
361
-    volumeStartBlock = p->firstSector;
362
-  }
363
-  if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) goto fail;
364
-  fbs = &cacheBuffer_.fbs32;
365
-  if (fbs->bytesPerSector != 512 ||
366
-      fbs->fatCount == 0 ||
367
-      fbs->reservedSectorCount == 0 ||
368
-      fbs->sectorsPerCluster == 0) {
369
-    // not valid FAT volume
370
-    goto fail;
371
-  }
372
-  fatCount_ = fbs->fatCount;
373
-  blocksPerCluster_ = fbs->sectorsPerCluster;
374
-  // determine shift that is same as multiply by blocksPerCluster_
375
-  clusterSizeShift_ = 0;
376
-  while (blocksPerCluster_ != _BV(clusterSizeShift_)) {
377
-    // error if not power of 2
378
-    if (clusterSizeShift_++ > 7) goto fail;
379
-  }
380
-  blocksPerFat_ = fbs->sectorsPerFat16 ?
381
-                  fbs->sectorsPerFat16 : fbs->sectorsPerFat32;
382
-
383
-  fatStartBlock_ = volumeStartBlock + fbs->reservedSectorCount;
384
-
385
-  // count for FAT16 zero for FAT32
386
-  rootDirEntryCount_ = fbs->rootDirEntryCount;
387
-
388
-  // directory start for FAT16 dataStart for FAT32
389
-  rootDirStart_ = fatStartBlock_ + fbs->fatCount * blocksPerFat_;
390
-
391
-  // data start for FAT16 and FAT32
392
-  dataStartBlock_ = rootDirStart_ + ((32 * fbs->rootDirEntryCount + 511) / 512);
393
-
394
-  // total blocks for FAT16 or FAT32
395
-  totalBlocks = fbs->totalSectors16 ?
396
-                fbs->totalSectors16 : fbs->totalSectors32;
397
-
398
-  // total data blocks
399
-  clusterCount_ = totalBlocks - (dataStartBlock_ - volumeStartBlock);
400
-
401
-  // divide by cluster size to get cluster count
402
-  clusterCount_ >>= clusterSizeShift_;
403
-
404
-  // FAT type is determined by cluster count
405
-  if (clusterCount_ < 4085) {
406
-    fatType_ = 12;
407
-    if (!FAT12_SUPPORT) goto fail;
408
-  }
409
-  else if (clusterCount_ < 65525) {
410
-    fatType_ = 16;
411
-  }
412
-  else {
413
-    rootDirStart_ = fbs->fat32RootCluster;
414
-    fatType_ = 32;
415
-  }
416
-  return true;
417
-fail:
418
-  return false;
419
-}
420
-#endif
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
+ * Arduino SdFat Library
25
+ * Copyright (C) 2009 by William Greiman
26
+ *
27
+ * This file is part of the Arduino Sd2Card Library
28
+ */
29
+#include "Marlin.h"
30
+#if ENABLED(SDSUPPORT)
31
+
32
+#include "SdVolume.h"
33
+//------------------------------------------------------------------------------
34
+#if !USE_MULTIPLE_CARDS
35
+  // raw block cache
36
+  uint32_t SdVolume::cacheBlockNumber_;  // current block number
37
+  cache_t  SdVolume::cacheBuffer_;       // 512 byte cache for Sd2Card
38
+  Sd2Card* SdVolume::sdCard_;            // pointer to SD card object
39
+  bool     SdVolume::cacheDirty_;        // cacheFlush() will write block if true
40
+  uint32_t SdVolume::cacheMirrorBlock_;  // mirror  block for second FAT
41
+#endif  // USE_MULTIPLE_CARDS
42
+//------------------------------------------------------------------------------
43
+// find a contiguous group of clusters
44
+bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
45
+  // start of group
46
+  uint32_t bgnCluster;
47
+  // end of group
48
+  uint32_t endCluster;
49
+  // last cluster of FAT
50
+  uint32_t fatEnd = clusterCount_ + 1;
51
+
52
+  // flag to save place to start next search
53
+  bool setStart;
54
+
55
+  // set search start cluster
56
+  if (*curCluster) {
57
+    // try to make file contiguous
58
+    bgnCluster = *curCluster + 1;
59
+
60
+    // don't save new start location
61
+    setStart = false;
62
+  }
63
+  else {
64
+    // start at likely place for free cluster
65
+    bgnCluster = allocSearchStart_;
66
+
67
+    // save next search start if one cluster
68
+    setStart = count == 1;
69
+  }
70
+  // end of group
71
+  endCluster = bgnCluster;
72
+
73
+  // search the FAT for free clusters
74
+  for (uint32_t n = 0;; n++, endCluster++) {
75
+    // can't find space checked all clusters
76
+    if (n >= clusterCount_) goto fail;
77
+
78
+    // past end - start from beginning of FAT
79
+    if (endCluster > fatEnd) {
80
+      bgnCluster = endCluster = 2;
81
+    }
82
+    uint32_t f;
83
+    if (!fatGet(endCluster, &f)) goto fail;
84
+
85
+    if (f != 0) {
86
+      // cluster in use try next cluster as bgnCluster
87
+      bgnCluster = endCluster + 1;
88
+    }
89
+    else if ((endCluster - bgnCluster + 1) == count) {
90
+      // done - found space
91
+      break;
92
+    }
93
+  }
94
+  // mark end of chain
95
+  if (!fatPutEOC(endCluster)) goto fail;
96
+
97
+  // link clusters
98
+  while (endCluster > bgnCluster) {
99
+    if (!fatPut(endCluster - 1, endCluster)) goto fail;
100
+    endCluster--;
101
+  }
102
+  if (*curCluster != 0) {
103
+    // connect chains
104
+    if (!fatPut(*curCluster, bgnCluster)) goto fail;
105
+  }
106
+  // return first cluster number to caller
107
+  *curCluster = bgnCluster;
108
+
109
+  // remember possible next free cluster
110
+  if (setStart) allocSearchStart_ = bgnCluster + 1;
111
+
112
+  return true;
113
+fail:
114
+  return false;
115
+}
116
+//------------------------------------------------------------------------------
117
+bool SdVolume::cacheFlush() {
118
+  if (cacheDirty_) {
119
+    if (!sdCard_->writeBlock(cacheBlockNumber_, cacheBuffer_.data)) {
120
+      goto fail;
121
+    }
122
+    // mirror FAT tables
123
+    if (cacheMirrorBlock_) {
124
+      if (!sdCard_->writeBlock(cacheMirrorBlock_, cacheBuffer_.data)) {
125
+        goto fail;
126
+      }
127
+      cacheMirrorBlock_ = 0;
128
+    }
129
+    cacheDirty_ = 0;
130
+  }
131
+  return true;
132
+fail:
133
+  return false;
134
+}
135
+//------------------------------------------------------------------------------
136
+bool SdVolume::cacheRawBlock(uint32_t blockNumber, bool dirty) {
137
+  if (cacheBlockNumber_ != blockNumber) {
138
+    if (!cacheFlush()) goto fail;
139
+    if (!sdCard_->readBlock(blockNumber, cacheBuffer_.data)) goto fail;
140
+    cacheBlockNumber_ = blockNumber;
141
+  }
142
+  if (dirty) cacheDirty_ = true;
143
+  return true;
144
+fail:
145
+  return false;
146
+}
147
+//------------------------------------------------------------------------------
148
+// return the size in bytes of a cluster chain
149
+bool SdVolume::chainSize(uint32_t cluster, uint32_t* size) {
150
+  uint32_t s = 0;
151
+  do {
152
+    if (!fatGet(cluster, &cluster)) goto fail;
153
+    s += 512UL << clusterSizeShift_;
154
+  } while (!isEOC(cluster));
155
+  *size = s;
156
+  return true;
157
+fail:
158
+  return false;
159
+}
160
+//------------------------------------------------------------------------------
161
+// Fetch a FAT entry
162
+bool SdVolume::fatGet(uint32_t cluster, uint32_t* value) {
163
+  uint32_t lba;
164
+  if (cluster > (clusterCount_ + 1)) goto fail;
165
+  if (FAT12_SUPPORT && fatType_ == 12) {
166
+    uint16_t index = cluster;
167
+    index += index >> 1;
168
+    lba = fatStartBlock_ + (index >> 9);
169
+    if (!cacheRawBlock(lba, CACHE_FOR_READ)) goto fail;
170
+    index &= 0X1FF;
171
+    uint16_t tmp = cacheBuffer_.data[index];
172
+    index++;
173
+    if (index == 512) {
174
+      if (!cacheRawBlock(lba + 1, CACHE_FOR_READ)) goto fail;
175
+      index = 0;
176
+    }
177
+    tmp |= cacheBuffer_.data[index] << 8;
178
+    *value = cluster & 1 ? tmp >> 4 : tmp & 0XFFF;
179
+    return true;
180
+  }
181
+  if (fatType_ == 16) {
182
+    lba = fatStartBlock_ + (cluster >> 8);
183
+  }
184
+  else if (fatType_ == 32) {
185
+    lba = fatStartBlock_ + (cluster >> 7);
186
+  }
187
+  else {
188
+    goto fail;
189
+  }
190
+  if (lba != cacheBlockNumber_) {
191
+    if (!cacheRawBlock(lba, CACHE_FOR_READ)) goto fail;
192
+  }
193
+  if (fatType_ == 16) {
194
+    *value = cacheBuffer_.fat16[cluster & 0XFF];
195
+  }
196
+  else {
197
+    *value = cacheBuffer_.fat32[cluster & 0X7F] & FAT32MASK;
198
+  }
199
+  return true;
200
+fail:
201
+  return false;
202
+}
203
+//------------------------------------------------------------------------------
204
+// Store a FAT entry
205
+bool SdVolume::fatPut(uint32_t cluster, uint32_t value) {
206
+  uint32_t lba;
207
+  // error if reserved cluster
208
+  if (cluster < 2) goto fail;
209
+
210
+  // error if not in FAT
211
+  if (cluster > (clusterCount_ + 1)) goto fail;
212
+
213
+  if (FAT12_SUPPORT && fatType_ == 12) {
214
+    uint16_t index = cluster;
215
+    index += index >> 1;
216
+    lba = fatStartBlock_ + (index >> 9);
217
+    if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail;
218
+    // mirror second FAT
219
+    if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
220
+    index &= 0X1FF;
221
+    uint8_t tmp = value;
222
+    if (cluster & 1) {
223
+      tmp = (cacheBuffer_.data[index] & 0XF) | tmp << 4;
224
+    }
225
+    cacheBuffer_.data[index] = tmp;
226
+    index++;
227
+    if (index == 512) {
228
+      lba++;
229
+      index = 0;
230
+      if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail;
231
+      // mirror second FAT
232
+      if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
233
+    }
234
+    tmp = value >> 4;
235
+    if (!(cluster & 1)) {
236
+      tmp = ((cacheBuffer_.data[index] & 0XF0)) | tmp >> 4;
237
+    }
238
+    cacheBuffer_.data[index] = tmp;
239
+    return true;
240
+  }
241
+  if (fatType_ == 16) {
242
+    lba = fatStartBlock_ + (cluster >> 8);
243
+  }
244
+  else if (fatType_ == 32) {
245
+    lba = fatStartBlock_ + (cluster >> 7);
246
+  }
247
+  else {
248
+    goto fail;
249
+  }
250
+  if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto fail;
251
+  // store entry
252
+  if (fatType_ == 16) {
253
+    cacheBuffer_.fat16[cluster & 0XFF] = value;
254
+  }
255
+  else {
256
+    cacheBuffer_.fat32[cluster & 0X7F] = value;
257
+  }
258
+  // mirror second FAT
259
+  if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
260
+  return true;
261
+fail:
262
+  return false;
263
+}
264
+//------------------------------------------------------------------------------
265
+// free a cluster chain
266
+bool SdVolume::freeChain(uint32_t cluster) {
267
+  uint32_t next;
268
+
269
+  // clear free cluster location
270
+  allocSearchStart_ = 2;
271
+
272
+  do {
273
+    if (!fatGet(cluster, &next)) goto fail;
274
+
275
+    // free cluster
276
+    if (!fatPut(cluster, 0)) goto fail;
277
+
278
+    cluster = next;
279
+  } while (!isEOC(cluster));
280
+
281
+  return true;
282
+fail:
283
+  return false;
284
+}
285
+//------------------------------------------------------------------------------
286
+/** Volume free space in clusters.
287
+ *
288
+ * \return Count of free clusters for success or -1 if an error occurs.
289
+ */
290
+int32_t SdVolume::freeClusterCount() {
291
+  uint32_t free = 0;
292
+  uint16_t n;
293
+  uint32_t todo = clusterCount_ + 2;
294
+
295
+  if (fatType_ == 16) {
296
+    n = 256;
297
+  }
298
+  else if (fatType_ == 32) {
299
+    n = 128;
300
+  }
301
+  else {
302
+    // put FAT12 here
303
+    return -1;
304
+  }
305
+
306
+  for (uint32_t lba = fatStartBlock_; todo; todo -= n, lba++) {
307
+    if (!cacheRawBlock(lba, CACHE_FOR_READ)) return -1;
308
+    NOMORE(n, todo);
309
+    if (fatType_ == 16) {
310
+      for (uint16_t i = 0; i < n; i++) {
311
+        if (cacheBuffer_.fat16[i] == 0) free++;
312
+      }
313
+    }
314
+    else {
315
+      for (uint16_t i = 0; i < n; i++) {
316
+        if (cacheBuffer_.fat32[i] == 0) free++;
317
+      }
318
+    }
319
+  }
320
+  return free;
321
+}
322
+//------------------------------------------------------------------------------
323
+/** Initialize a FAT volume.
324
+ *
325
+ * \param[in] dev The SD card where the volume is located.
326
+ *
327
+ * \param[in] part The partition to be used.  Legal values for \a part are
328
+ * 1-4 to use the corresponding partition on a device formatted with
329
+ * a MBR, Master Boot Record, or zero if the device is formatted as
330
+ * a super floppy with the FAT boot sector in block zero.
331
+ *
332
+ * \return The value one, true, is returned for success and
333
+ * the value zero, false, is returned for failure.  Reasons for
334
+ * failure include not finding a valid partition, not finding a valid
335
+ * FAT file system in the specified partition or an I/O error.
336
+ */
337
+bool SdVolume::init(Sd2Card* dev, uint8_t part) {
338
+  uint32_t totalBlocks;
339
+  uint32_t volumeStartBlock = 0;
340
+  fat32_boot_t* fbs;
341
+
342
+  sdCard_ = dev;
343
+  fatType_ = 0;
344
+  allocSearchStart_ = 2;
345
+  cacheDirty_ = 0;  // cacheFlush() will write block if true
346
+  cacheMirrorBlock_ = 0;
347
+  cacheBlockNumber_ = 0XFFFFFFFF;
348
+
349
+  // if part == 0 assume super floppy with FAT boot sector in block zero
350
+  // if part > 0 assume mbr volume with partition table
351
+  if (part) {
352
+    if (part > 4)goto fail;
353
+    if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) goto fail;
354
+    part_t* p = &cacheBuffer_.mbr.part[part - 1];
355
+    if ((p->boot & 0X7F) != 0  ||
356
+        p->totalSectors < 100 ||
357
+        p->firstSector == 0) {
358
+      // not a valid partition
359
+      goto fail;
360
+    }
361
+    volumeStartBlock = p->firstSector;
362
+  }
363
+  if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) goto fail;
364
+  fbs = &cacheBuffer_.fbs32;
365
+  if (fbs->bytesPerSector != 512 ||
366
+      fbs->fatCount == 0 ||
367
+      fbs->reservedSectorCount == 0 ||
368
+      fbs->sectorsPerCluster == 0) {
369
+    // not valid FAT volume
370
+    goto fail;
371
+  }
372
+  fatCount_ = fbs->fatCount;
373
+  blocksPerCluster_ = fbs->sectorsPerCluster;
374
+  // determine shift that is same as multiply by blocksPerCluster_
375
+  clusterSizeShift_ = 0;
376
+  while (blocksPerCluster_ != _BV(clusterSizeShift_)) {
377
+    // error if not power of 2
378
+    if (clusterSizeShift_++ > 7) goto fail;
379
+  }
380
+  blocksPerFat_ = fbs->sectorsPerFat16 ?
381
+                  fbs->sectorsPerFat16 : fbs->sectorsPerFat32;
382
+
383
+  fatStartBlock_ = volumeStartBlock + fbs->reservedSectorCount;
384
+
385
+  // count for FAT16 zero for FAT32
386
+  rootDirEntryCount_ = fbs->rootDirEntryCount;
387
+
388
+  // directory start for FAT16 dataStart for FAT32
389
+  rootDirStart_ = fatStartBlock_ + fbs->fatCount * blocksPerFat_;
390
+
391
+  // data start for FAT16 and FAT32
392
+  dataStartBlock_ = rootDirStart_ + ((32 * fbs->rootDirEntryCount + 511) / 512);
393
+
394
+  // total blocks for FAT16 or FAT32
395
+  totalBlocks = fbs->totalSectors16 ?
396
+                fbs->totalSectors16 : fbs->totalSectors32;
397
+
398
+  // total data blocks
399
+  clusterCount_ = totalBlocks - (dataStartBlock_ - volumeStartBlock);
400
+
401
+  // divide by cluster size to get cluster count
402
+  clusterCount_ >>= clusterSizeShift_;
403
+
404
+  // FAT type is determined by cluster count
405
+  if (clusterCount_ < 4085) {
406
+    fatType_ = 12;
407
+    if (!FAT12_SUPPORT) goto fail;
408
+  }
409
+  else if (clusterCount_ < 65525) {
410
+    fatType_ = 16;
411
+  }
412
+  else {
413
+    rootDirStart_ = fbs->fat32RootCluster;
414
+    fatType_ = 32;
415
+  }
416
+  return true;
417
+fail:
418
+  return false;
419
+}
420
+#endif

+ 1
- 1
Marlin/example_configurations/K8200/Configuration.h Wyświetl plik

@@ -1,4 +1,4 @@
1
-/**
1
+/**
2 2
  * Marlin 3D Printer Firmware
3 3
  * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4 4
  *

+ 1
- 1
Marlin/language_bg.h Wyświetl plik

@@ -1,4 +1,4 @@
1
-/**
1
+/**
2 2
  * Marlin 3D Printer Firmware
3 3
  * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4 4
  *

+ 1
- 1
Marlin/language_ru.h Wyświetl plik

@@ -1,4 +1,4 @@
1
-/**
1
+/**
2 2
  * Marlin 3D Printer Firmware
3 3
  * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4 4
  *

+ 94
- 94
README.md Wyświetl plik

@@ -1,94 +1,94 @@
1
-# Marlin 3D Printer Firmware
2
-<img align="top" width=175 src="Documentation/Logo/Marlin%20Logo%20GitHub.png" />
3
- Additional documentation can be found in [The Marlin Wiki](https://github.com/MarlinFirmware/Marlin/wiki/Main-Page).
4
-
5
-## Release Candidate -- Marlin 1.1.0-RC5 - 01 April 2016
6
-
7
-__Not for production use – use with caution!__
8
-
9
-You can download earlier versions of Marlin on the [Releases page](https://github.com/MarlinFirmware/Marlin/releases). (The latest "stable" release of Marlin is 1.0.2-1.)
10
-
11
-You'll always find the latest Release Candidate in the ["RC" branch](https://github.com/MarlinFirmware/Marlin/tree/RC). Bugs that we find in the current Release Candidate are patched in the ["RCBugFix" branch](https://github.com/MarlinFirmware/Marlin/tree/RC), so during beta testing this is where you can always find the latest code on its way towards release.
12
-
13
-Future development (Marlin 1.2 and beyond) takes place in the [MarlinDev repository](https://github.com/MarlinFirmware/MarlinDev/).
14
-
15
-## Recent Changes
16
-- RCBugFix
17
-  - Throw error if compiling with older versions (<1.60) of Arduino due to serios problems with outdated Arduino versions
18
-  - Please upgrade your IDE at least to Arduino 1.6.0. Thanks.
19
-
20
-- RC5 - 01 Apr 2016
21
-  - Warn if compiling with older versions (<1.50) of Arduino
22
-  - Fix various LCD menu issues
23
-  - Add formal support for MKSv1.3 and Sainsmart (RAMPS variants)
24
-  - Fix bugs in M104, M109, and M190
25
-  - Fix broken M404 command
26
-  - Fix issues with M23 and "Start SD Print"
27
-  - More output for M111
28
-  - Rename FILAMENT_SENSOR to FILAMENT_WIDTH_SENSOR
29
-  - Fix SD card bugs
30
-  - and a lot more
31
-  - see https://github.com/MarlinFirmware/Marlin/releases/tag/1.1.0-RC5 for details
32
-
33
-- RC4 - 24 Mar 2016
34
-  - Many lingering bugs and nagging issues addressed
35
-  - Improvements to LCD menus, CoreXY/CoreXZ, Delta, Bed Leveling, and more…
36
-
37
-- RC3 - 01 Dec 2015
38
-  - A number of language sensitive strings have been revised
39
-  - Formatting of the LCD display has been improved to handle negative coordinates better
40
-  - Various compiler-related issues have been corrected
41
-
42
-- RC2 - 29 Sep 2015
43
-  - File styling reverted
44
-  - LCD update frequency reduced
45
-
46
-- RC1 - 19 Sep 2015
47
-  - Published for testing
48
-
49
-## Submitting Patches
50
-Proposed patches should be submitted as a Pull Request against the [RCBugFix](https://github.com/MarlinFirmware/Marlin/tree/RCBugFix) branch.
51
-
52
-- Don't submit new feature proposals. The RCBugFix branch is for fixing bugs in existing features.
53
-- Do submit questions and concerns. The "naive" question is often the one we forget to ask.
54
-- Follow the proper coding style. Pull requests with styling errors will be delayed. See our [Coding Standards](https://github.com/MarlinFirmware/Marlin/wiki/DNE-Coding-Standards) page for more information.
55
-
56
-## Current Status: Testing
57
-
58
-Please test this firmware and inform us if it misbehaves in any way. Volunteers are standing by!
59
-
60
-[![Coverity Scan Build Status](https://scan.coverity.com/projects/2224/badge.svg)](https://scan.coverity.com/projects/2224)
61
-[![Travis Build Status](https://travis-ci.org/MarlinFirmware/MarlinDev.svg)](https://travis-ci.org/MarlinFirmware/MarlinDev)
62
-
63
-##### [RepRap.org Wiki Page](http://reprap.org/wiki/Marlin)
64
-
65
-## Credits
66
-
67
-The current Marlin dev team consists of:
68
-
69
- - Scott Lahteine [@thinkyhead] - English
70
- - [@Wurstnase] - Deutsch, English
71
- - F. Malpartida [@fmalpartida] - English, Spanish
72
- - Jochen Groppe [@CONSULitAS] - Deutsch, English
73
- - [@maverikou]
74
- - Chris Palmer [@nophead]
75
- - [@paclema]
76
- - Edward Patel [@epatel] - Swedish, English
77
- - Erik van der Zalm [@ErikZalm]
78
- - David Braam [@daid]
79
- - Bernhard Kubicek [@bkubicek]
80
- - Roxanne Neufeld [@Roxy-3DPrintBoard] - English
81
-
82
-More features have been added by:
83
-  - Alberto Cotronei [@MagoKimbra]
84
-  - Lampmaker,
85
-  - Bradley Feldman,
86
-  - and others...
87
-
88
-## License
89
-
90
-Marlin is published under the [GPL license](/LICENSE) because we believe in open development. The GPL comes with both rights and obligations. Whether you use Marlin firmware as the driver for your open or closed-source product, you must keep Marlin open, and you must provide your compatible Marlin source code to end users upon request. The most straightforward way to comply with the Marlin license is to make a fork of Marlin on Github, perform your modifications, and direct users to your modified fork.
91
-
92
-While we can't prevent the use of this code in products (3D printers, CNC, etc.) that are closed source or crippled by a patent, we would prefer that you choose another firmware or, better yet, make your own.
93
-
94
-[![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=ErikZalm&url=https://github.com/MarlinFirmware/Marlin&title=Marlin&language=&tags=github&category=software)
1
+# Marlin 3D Printer Firmware
2
+<img align="top" width=175 src="Documentation/Logo/Marlin%20Logo%20GitHub.png" />
3
+ Additional documentation can be found in [The Marlin Wiki](https://github.com/MarlinFirmware/Marlin/wiki/Main-Page).
4
+
5
+## Release Candidate -- Marlin 1.1.0-RC5 - 01 April 2016
6
+
7
+__Not for production use – use with caution!__
8
+
9
+You can download earlier versions of Marlin on the [Releases page](https://github.com/MarlinFirmware/Marlin/releases). (The latest "stable" release of Marlin is 1.0.2-1.)
10
+
11
+You'll always find the latest Release Candidate in the ["RC" branch](https://github.com/MarlinFirmware/Marlin/tree/RC). Bugs that we find in the current Release Candidate are patched in the ["RCBugFix" branch](https://github.com/MarlinFirmware/Marlin/tree/RC), so during beta testing this is where you can always find the latest code on its way towards release.
12
+
13
+Future development (Marlin 1.2 and beyond) takes place in the [MarlinDev repository](https://github.com/MarlinFirmware/MarlinDev/).
14
+
15
+## Recent Changes
16
+- RCBugFix
17
+  - Throw error if compiling with older versions (<1.60) of Arduino due to serios problems with outdated Arduino versions
18
+  - Please upgrade your IDE at least to Arduino 1.6.0. Thanks.
19
+
20
+- RC5 - 01 Apr 2016
21
+  - Warn if compiling with older versions (<1.50) of Arduino
22
+  - Fix various LCD menu issues
23
+  - Add formal support for MKSv1.3 and Sainsmart (RAMPS variants)
24
+  - Fix bugs in M104, M109, and M190
25
+  - Fix broken M404 command
26
+  - Fix issues with M23 and "Start SD Print"
27
+  - More output for M111
28
+  - Rename FILAMENT_SENSOR to FILAMENT_WIDTH_SENSOR
29
+  - Fix SD card bugs
30
+  - and a lot more
31
+  - see https://github.com/MarlinFirmware/Marlin/releases/tag/1.1.0-RC5 for details
32
+
33
+- RC4 - 24 Mar 2016
34
+  - Many lingering bugs and nagging issues addressed
35
+  - Improvements to LCD menus, CoreXY/CoreXZ, Delta, Bed Leveling, and more…
36
+
37
+- RC3 - 01 Dec 2015
38
+  - A number of language sensitive strings have been revised
39
+  - Formatting of the LCD display has been improved to handle negative coordinates better
40
+  - Various compiler-related issues have been corrected
41
+
42
+- RC2 - 29 Sep 2015
43
+  - File styling reverted
44
+  - LCD update frequency reduced
45
+
46
+- RC1 - 19 Sep 2015
47
+  - Published for testing
48
+
49
+## Submitting Patches
50
+Proposed patches should be submitted as a Pull Request against the [RCBugFix](https://github.com/MarlinFirmware/Marlin/tree/RCBugFix) branch.
51
+
52
+- Don't submit new feature proposals. The RCBugFix branch is for fixing bugs in existing features.
53
+- Do submit questions and concerns. The "naive" question is often the one we forget to ask.
54
+- Follow the proper coding style. Pull requests with styling errors will be delayed. See our [Coding Standards](https://github.com/MarlinFirmware/Marlin/wiki/DNE-Coding-Standards) page for more information.
55
+
56
+## Current Status: Testing
57
+
58
+Please test this firmware and inform us if it misbehaves in any way. Volunteers are standing by!
59
+
60
+[![Coverity Scan Build Status](https://scan.coverity.com/projects/2224/badge.svg)](https://scan.coverity.com/projects/2224)
61
+[![Travis Build Status](https://travis-ci.org/MarlinFirmware/MarlinDev.svg)](https://travis-ci.org/MarlinFirmware/MarlinDev)
62
+
63
+##### [RepRap.org Wiki Page](http://reprap.org/wiki/Marlin)
64
+
65
+## Credits
66
+
67
+The current Marlin dev team consists of:
68
+
69
+ - Scott Lahteine [@thinkyhead] - English
70
+ - [@Wurstnase] - Deutsch, English
71
+ - F. Malpartida [@fmalpartida] - English, Spanish
72
+ - Jochen Groppe [@CONSULitAS] - Deutsch, English
73
+ - [@maverikou]
74
+ - Chris Palmer [@nophead]
75
+ - [@paclema]
76
+ - Edward Patel [@epatel] - Swedish, English
77
+ - Erik van der Zalm [@ErikZalm]
78
+ - David Braam [@daid]
79
+ - Bernhard Kubicek [@bkubicek]
80
+ - Roxanne Neufeld [@Roxy-3DPrintBoard] - English
81
+
82
+More features have been added by:
83
+  - Alberto Cotronei [@MagoKimbra]
84
+  - Lampmaker,
85
+  - Bradley Feldman,
86
+  - and others...
87
+
88
+## License
89
+
90
+Marlin is published under the [GPL license](/LICENSE) because we believe in open development. The GPL comes with both rights and obligations. Whether you use Marlin firmware as the driver for your open or closed-source product, you must keep Marlin open, and you must provide your compatible Marlin source code to end users upon request. The most straightforward way to comply with the Marlin license is to make a fork of Marlin on Github, perform your modifications, and direct users to your modified fork.
91
+
92
+While we can't prevent the use of this code in products (3D printers, CNC, etc.) that are closed source or crippled by a patent, we would prefer that you choose another firmware or, better yet, make your own.
93
+
94
+[![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=ErikZalm&url=https://github.com/MarlinFirmware/Marlin&title=Marlin&language=&tags=github&category=software)

Ładowanie…
Anuluj
Zapisz