1 | /* Arduino FAT16 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 SdCard_h
|
---|
21 | #define SdCard_h
|
---|
22 | /**
|
---|
23 | * \file
|
---|
24 | * SdCard class
|
---|
25 | */
|
---|
26 | #include <SdInfo.h>
|
---|
27 | //------------------------------------------------------------------------------
|
---|
28 | // Warning only SD_CHIP_SELECT_PIN, the SD card select pin, may be redefined.
|
---|
29 | // define hardware SPI pins
|
---|
30 | #if defined(__AVR_ATmega168__)\
|
---|
31 | ||defined(__AVR_ATmega168P__)\
|
---|
32 | ||defined(__AVR_ATmega328P__)
|
---|
33 | // 168 and 328 Arduinos
|
---|
34 | /** Slave Select pin */
|
---|
35 | uint8_t const SPI_SS_PIN = 10;
|
---|
36 | /** Master Out Slave In pin */
|
---|
37 | uint8_t const SPI_MOSI_PIN = 11;
|
---|
38 | /** Master In Slave Out pin */
|
---|
39 | uint8_t const SPI_MISO_PIN = 12;
|
---|
40 | /** Serial Clock */
|
---|
41 | uint8_t const SPI_SCK_PIN = 13;
|
---|
42 | //------------------------------------------------------------------------------
|
---|
43 | #elif defined(__AVR_ATmega1280__)\
|
---|
44 | || defined(__AVR_ATmega2560__)
|
---|
45 | // pins for Arduino Mega
|
---|
46 | uint8_t const SPI_SS_PIN = 53;
|
---|
47 | uint8_t const SPI_MOSI_PIN = 51;
|
---|
48 | uint8_t const SPI_MISO_PIN = 50;
|
---|
49 | uint8_t const SPI_SCK_PIN = 52;
|
---|
50 | //------------------------------------------------------------------------------
|
---|
51 | #elif defined(__AVR_ATmega644P__)\
|
---|
52 | || defined(__AVR_ATmega644__)\
|
---|
53 | || defined(__AVR_ATmega1284P__)
|
---|
54 | // pins for Sanguino
|
---|
55 | uint8_t const SPI_SS_PIN = 4;
|
---|
56 | uint8_t const SPI_MOSI_PIN = 5;
|
---|
57 | uint8_t const SPI_MISO_PIN = 6;
|
---|
58 | uint8_t const SPI_SCK_PIN = 7;
|
---|
59 | //------------------------------------------------------------------------------
|
---|
60 | #elif defined(__AVR_ATmega32U4__)
|
---|
61 | // pins for Teensy 2.0
|
---|
62 | uint8_t const SPI_SS_PIN = 8;
|
---|
63 | uint8_t const SPI_MOSI_PIN = 16;
|
---|
64 | uint8_t const SPI_MISO_PIN = 14;
|
---|
65 | uint8_t const SPI_SCK_PIN = 15;
|
---|
66 | //------------------------------------------------------------------------------
|
---|
67 | #elif defined(__AVR_AT90USB646__)\
|
---|
68 | || defined(__AVR_AT90USB1286__)
|
---|
69 | // pins for Teensy++ 1.0 & 2.0
|
---|
70 | uint8_t const SPI_SS_PIN = 20;
|
---|
71 | uint8_t const SPI_MOSI_PIN = 22;
|
---|
72 | uint8_t const SPI_MISO_PIN = 23;
|
---|
73 | uint8_t const SPI_SCK_PIN = 21;
|
---|
74 | //------------------------------------------------------------------------------
|
---|
75 | #else // SPI pins
|
---|
76 | #error unknown CPU
|
---|
77 | #endif // SPI pins
|
---|
78 | //------------------------------------------------------------------------------
|
---|
79 | /**
|
---|
80 | * SD Chip Select pin
|
---|
81 | *
|
---|
82 | * Warning if this pin is redefined the hardware SS pin will be enabled
|
---|
83 | * as an output by init(). An avr processor will not function as an SPI
|
---|
84 | * master unless SS is set to output mode.
|
---|
85 | *
|
---|
86 | * For example to set SD_CHIP_SELECT_PIN to 8 for the SparkFun microSD shield:
|
---|
87 | * uint8_t const SD_CHIP_SELECT_PIN = 8;
|
---|
88 | *
|
---|
89 | * The default chip select pin for the SD card is SS.
|
---|
90 | */
|
---|
91 | uint8_t const SD_CHIP_SELECT_PIN = SPI_SS_PIN;
|
---|
92 | //------------------------------------------------------------------------------
|
---|
93 | /** command timeout ms */
|
---|
94 | uint16_t const SD_COMMAND_TIMEOUT = 300;
|
---|
95 | /** init timeout ms */
|
---|
96 | uint16_t const SD_INIT_TIMEOUT = 2000;
|
---|
97 | /** read timeout ms */
|
---|
98 | uint16_t const SD_READ_TIMEOUT = 300;
|
---|
99 | /** write timeout ms */
|
---|
100 | uint16_t const SD_WRITE_TIMEOUT = 600;
|
---|
101 | //------------------------------------------------------------------------------
|
---|
102 | // error codes
|
---|
103 | /** Card did not go into SPI mode */
|
---|
104 | uint8_t const SD_ERROR_CMD0 = 1;
|
---|
105 | /** Card did not go ready */
|
---|
106 | uint8_t const SD_ERROR_ACMD41 = 2;
|
---|
107 | /** Write command not accepted */
|
---|
108 | uint8_t const SD_ERROR_CMD24 = 3;
|
---|
109 | /** Read command not accepted */
|
---|
110 | uint8_t const SD_ERROR_CMD17 = 4;
|
---|
111 | /** timeout waiting for read data */
|
---|
112 | uint8_t const SD_ERROR_READ_TIMEOUT = 5;
|
---|
113 | /** write error occurred */
|
---|
114 | uint8_t const SD_ERROR_WRITE_RESPONSE = 6;
|
---|
115 | /** timeout waiting for write status */
|
---|
116 | uint8_t const SD_ERROR_WRITE_TIMEOUT = 7;
|
---|
117 | /** attempt to write block zero */
|
---|
118 | uint8_t const SD_ERROR_BLOCK_ZERO_WRITE = 8;
|
---|
119 | /** card returned an error to a CMD13 status check after a write */
|
---|
120 | uint8_t const SD_ERROR_WRITE_PROGRAMMING = 9;
|
---|
121 | /** invalid SPI speed in init() call */
|
---|
122 | uint8_t const SD_ERROR_SPI_SPEED = 10;
|
---|
123 | //------------------------------------------------------------------------------
|
---|
124 | // SD command codes
|
---|
125 | /** SEND OPERATING CONDITIONS */
|
---|
126 | uint8_t const ACMD41 = 0X29;
|
---|
127 | /** GO_IDLE_STATE - init card in spi mode if CS low */
|
---|
128 | uint8_t const CMD0 = 0X00;
|
---|
129 | /** SEND_CSD - Card Specific Data */
|
---|
130 | uint8_t const CMD9 = 0X09;
|
---|
131 | /** SEND_CID - Card IDentification */
|
---|
132 | uint8_t const CMD10 = 0X0A;
|
---|
133 | /** SEND_STATUS - read the card status register */
|
---|
134 | uint8_t const CMD13 = 0X0D;
|
---|
135 | /** READ_BLOCK */
|
---|
136 | uint8_t const CMD17 = 0X11;
|
---|
137 | /** WRITE_BLOCK */
|
---|
138 | uint8_t const CMD24 = 0X18;
|
---|
139 | /** APP_CMD - escape for application specific command */
|
---|
140 | uint8_t const CMD55 = 0X37;
|
---|
141 | //------------------------------------------------------------------------------
|
---|
142 | /**
|
---|
143 | * \class SdCard
|
---|
144 | * \brief Hardware access class for SD flash cards
|
---|
145 | *
|
---|
146 | * Supports raw access to a standard SD flash memory card.
|
---|
147 | *
|
---|
148 | */
|
---|
149 | class SdCard {
|
---|
150 | public:
|
---|
151 | /** Code for a SD error. See SdCard.h for definitions. */
|
---|
152 | uint8_t errorCode;
|
---|
153 | /** Data that may be helpful in determining the cause of an error */
|
---|
154 | uint8_t errorData;
|
---|
155 | uint32_t cardSize(void);
|
---|
156 | /**
|
---|
157 | * Initialize an SD flash memory card with default clock rate and chip
|
---|
158 | * select pin. See SdCard::init(uint8_t sckRateID, uint8_t chipSelectPin).
|
---|
159 | */
|
---|
160 | uint8_t init(void) {
|
---|
161 | return init(0, SD_CHIP_SELECT_PIN);
|
---|
162 | }
|
---|
163 | /**
|
---|
164 | * Initialize an SD flash memory card with the selected SPI clock rate
|
---|
165 | * and the default SD chip select pin.
|
---|
166 | * See SdCard::init(uint8_t slow, uint8_t chipSelectPin).
|
---|
167 | */
|
---|
168 | uint8_t init(uint8_t speed) {
|
---|
169 | return init(speed, SD_CHIP_SELECT_PIN);
|
---|
170 | }
|
---|
171 | uint8_t init(uint8_t speed, uint8_t chipselectPin);
|
---|
172 | uint8_t readBlock(uint32_t block, uint8_t* dst);
|
---|
173 | /** Read the CID register which contains info about the card.
|
---|
174 | * This includes Manufacturer ID, OEM ID, product name, version,
|
---|
175 | * serial number, and manufacturing date. */
|
---|
176 | uint8_t readCID(cid_t* cid) {
|
---|
177 | return readReg(CMD10, cid);
|
---|
178 | }
|
---|
179 | uint8_t writeBlock(uint32_t block, const uint8_t* src);
|
---|
180 | private:
|
---|
181 | uint8_t cardAcmd(uint8_t cmd, uint32_t arg);
|
---|
182 | uint8_t cardCommand(uint8_t cmd, uint32_t arg);
|
---|
183 | uint8_t chipSelectPin_;
|
---|
184 | uint8_t speed_;
|
---|
185 | void chipSelectHigh(void);
|
---|
186 | void chipSelectLow(void);
|
---|
187 | void error(uint8_t code, uint8_t data);
|
---|
188 | void error(uint8_t code);
|
---|
189 | uint8_t readReg(uint8_t cmd, void* buf);
|
---|
190 | uint8_t readTransfer(uint8_t* dst, uint16_t count);
|
---|
191 | };
|
---|
192 | #endif // SdCard_h
|
---|