Adafruit WaveHC Arduino Library
SdReader.h
Go to the documentation of this file.
1 /* Arduino WaveHC Library
2  * Copyright (C) 2008 by William Greiman
3  *
4  * This file is part of the Arduino FAT16 Library
5  *
6  * This Library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This Library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15 
16  * You should have received a copy of the GNU General Public License
17  * along with the Arduino Fat16 Library. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 #ifndef SdReader_h
21 #define SdReader_h
22 #include <SdInfo.h>
34 #define SPI_INIT_SLOW 0
35 
39 #define SPI_DEFAULT_HALF_SPEED false
40 
42 #define SD_READ_TIMEOUT 300
43 
44 // SD card errors
46 #define SD_CARD_ERROR_CMD0 0X1
47 
48 #define SD_CARD_ERROR_CMD8 0X2
49 
50 #define SD_CARD_ERROR_CMD17 0X3
51 
52 #define SD_CARD_ERROR_CMD24 0X4
53 
54 #define SD_CARD_ERROR_CMD58 0X5
55 
56 #define SD_CARD_ERROR_ACMD41 0X6
57 
58 #define SD_CARD_ERROR_BAD_CSD 0X7
59 
60 #define SD_CARD_ERROR_READ_REG 0X8
61 
62 #define SD_CARD_ERROR_CMD8_ECHO 0X09
63 
64 #define SD_CARD_ERROR_READ_TIMEOUT 0XD
65 
66 #define SD_CARD_ERROR_READ 0X10
67 //
68 // card types
70 #define SD_CARD_TYPE_SD1 1
71 
72 #define SD_CARD_TYPE_SD2 2
73 
74 #define SD_CARD_TYPE_SDHC 3
75 //------------------------------------------------------------------------------
83 class SdReader {
84  uint32_t block_;
85  uint8_t errorCode_;
86  uint8_t errorData_;
87  uint8_t inBlock_;
88  uint16_t offset_;
89  uint8_t partialBlockRead_;
90  uint8_t response_;
91  uint8_t type_;
92  uint8_t cardCommand(uint8_t cmd, uint32_t arg);
93  void error(uint8_t code) { errorCode_ = code; }
94  void error(uint8_t code, uint8_t data) {
95  errorCode_ = code;
96  errorData_ = data;
97  }
98  uint8_t readRegister(uint8_t cmd, uint8_t *dst);
99  void type(uint8_t value) { type_ = value; }
100  uint8_t waitNotBusy(uint16_t timeoutMillis);
101  uint8_t waitStartBlock(void);
102 
103 public:
105  SdReader(void) : errorCode_(0), inBlock_(0), partialBlockRead_(0), type_(0){};
106  uint32_t cardSize(void);
108  uint8_t errorCode(void) { return errorCode_; }
110  uint8_t errorData(void) { return errorData_; }
111  uint8_t init(uint8_t slow = SPI_DEFAULT_HALF_SPEED);
123  void partialBlockRead(uint8_t value) {
124  readEnd();
125  partialBlockRead_ = value;
126  }
136  uint8_t readBlock(uint32_t block, uint8_t *dst) {
137  return readData(block, 0, dst, 512);
138  }
139  uint8_t readData(uint32_t block, uint16_t offset, uint8_t *dst,
140  uint16_t count);
153  uint8_t readCID(cid_t &cid) { return readRegister(CMD10, (uint8_t *)&cid); }
154 
161  uint8_t readCSD(csd_t &csd) { return readRegister(CMD9, (uint8_t *)&csd); }
162  void readEnd(void);
167  uint8_t type() { return type_; }
168 };
169 #endif // SdReader_h
void readEnd(void)
Definition: SdReader.cpp:289
uint8_t readBlock(uint32_t block, uint8_t *dst)
Definition: SdReader.h:136
uint8_t readCSD(csd_t &csd)
Read a cards CSD register. The CSD contains Card-Specific Data that provides information regarding ac...
Definition: SdReader.h:161
Hardware access class for SD flash cards.
Definition: SdReader.h:83
uint8_t init(uint8_t slow=SPI_DEFAULT_HALF_SPEED)
Definition: SdReader.cpp:137
void partialBlockRead(uint8_t value)
Definition: SdReader.h:123
uint8_t readData(uint32_t block, uint16_t offset, uint8_t *dst, uint16_t count)
Definition: SdReader.cpp:235
SdReader(void)
Definition: SdReader.h:105
uint32_t cardSize(void)
Definition: SdReader.cpp:104
< union of old and new style CSD register
Definition: SdInfo.h:282
uint8_t readCID(cid_t &cid)
Read a cards CID register. The CID contains card identification information such as Manufacturer ID...
Definition: SdReader.h:153
uint8_t type()
Return the card type: SD V1, SD V2 or SDHC.
Definition: SdReader.h:167
#define SPI_DEFAULT_HALF_SPEED
Definition: SdReader.h:39
uint8_t errorData(void)
Definition: SdReader.h:110
< CID typedef struct
Definition: SdInfo.h:88
uint8_t errorCode(void)
Definition: SdReader.h:108