Mbed OS Reference
Loading...
Searching...
No Matches
SDBlockDevice.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef MBED_SD_BLOCK_DEVICE_H
19#define MBED_SD_BLOCK_DEVICE_H
20
21/* If the target has no SPI support, then SD Card is not supported. */
22#if DEVICE_SPI
23
24#include "blockdevice/BlockDevice.h"
25#include "drivers/SPI.h"
26#include "drivers/Timer.h"
27#include "drivers/MbedCRC.h"
28#include "drivers/DigitalOut.h"
29#include "platform/platform.h"
30#include "rtos/Mutex.h"
31#include "hal/static_pinmap.h"
32
33#ifndef MBED_CONF_SD_SPI_MOSI
34#define MBED_CONF_SD_SPI_MOSI NC
35#endif
36#ifndef MBED_CONF_SD_SPI_MISO
37#define MBED_CONF_SD_SPI_MISO NC
38#endif
39#ifndef MBED_CONF_SD_SPI_CLK
40#define MBED_CONF_SD_SPI_CLK NC
41#endif
42#ifndef MBED_CONF_SD_SPI_CS
43#define MBED_CONF_SD_SPI_CS NC
44#endif
45#ifndef MBED_CONF_SD_INIT_FREQUENCY
46#define MBED_CONF_SD_INIT_FREQUENCY 100000
47#endif
48#ifndef MBED_CONF_SD_TRX_FREQUENCY
49#define MBED_CONF_SD_TRX_FREQUENCY 1000000
50#endif
51#ifndef MBED_CONF_SD_CRC_ENABLED
52#define MBED_CONF_SD_CRC_ENABLED 0
53#endif
54
55/** SDBlockDevice class
56 *
57 * Access an SD Card using SPI bus
58 */
60public:
61 /** Creates an SDBlockDevice on a SPI bus specified by pins (using dynamic pin-map)
62 *
63 * @param mosi SPI master out, slave in pin
64 * @param miso SPI master in, slave out pin
65 * @param sclk SPI clock pin
66 * @param cs SPI chip select pin. Currently, GPIO chip selects are always used.
67 * @param hz Clock speed of the SPI bus (defaults to 1MHz)
68 * @param crc_on Enable cyclic redundancy check (defaults to disabled)
69 */
70 SDBlockDevice(PinName mosi = MBED_CONF_SD_SPI_MOSI,
71 PinName miso = MBED_CONF_SD_SPI_MISO,
72 PinName sclk = MBED_CONF_SD_SPI_CLK,
73 PinName cs = MBED_CONF_SD_SPI_CS,
74 uint64_t hz = MBED_CONF_SD_TRX_FREQUENCY,
75 bool crc_on = MBED_CONF_SD_CRC_ENABLED);
76
77 /** Creates an SDBlockDevice on a SPI bus specified by pins (using static pin-map)
78 *
79 * @param spi_pinmap Static SPI pin-map
80 * @param cs Chip select pin (can be any GPIO)
81 * @param hz Clock speed of the SPI bus (defaults to 1MHz)
82 * @param crc_on Enable cyclic redundancy check (defaults to disabled)
83 */
84 SDBlockDevice(const spi_pinmap_t &spi_pinmap,
85 PinName cs = MBED_CONF_SD_SPI_CS,
86 uint64_t hz = MBED_CONF_SD_TRX_FREQUENCY,
87 bool crc_on = MBED_CONF_SD_CRC_ENABLED);
88
89 virtual ~SDBlockDevice();
90
91 /** Initialize a block device
92 *
93 * @return BD_ERROR_OK(0) - success
94 * BD_ERROR_DEVICE_ERROR - device driver transaction failed
95 * SD_BLOCK_DEVICE_ERROR_NO_DEVICE - device (SD card) is missing or not connected
96 * SD_BLOCK_DEVICE_ERROR_UNUSABLE - unusable card
97 * SD_BLOCK_DEVICE_ERROR_CRC - crc error
98 */
99 virtual int init();
100
101#if DEVICE_SPI_ASYNCH
102 /**
103 * @brief Configure the usage of asynchronous %SPI by this class.
104 *
105 * By default, async %SPI is not enabled, so this class will simply busy-wait while
106 * communicating with the card. When async %SPI is enabled, %SPI operations
107 * will be done in blocking asynchronous mode, so other threads may execute
108 * in the background while data is going to and from the card.
109 *
110 * @param enabled Whether usage of async %SPI is enabled.
111 * @param dma_usage_hint DMA usage hint to pass to the underlying #SPI instance.
112 */
113 void set_async_spi_mode(bool enabled, DMAUsage dma_usage_hint = DMAUsage::DMA_USAGE_NEVER);
114#endif
115
116 /** Deinitialize a block device
117 *
118 * @return BD_ERROR_OK(0) - success
119 */
120 virtual int deinit();
121
122 /** Read blocks from a block device
123 *
124 * @param buffer Buffer to write blocks to
125 * @param addr Address of block to begin reading from
126 * @param size Size to read in bytes, must be a multiple of read block size
127 * @return BD_ERROR_OK(0) - success
128 * SD_BLOCK_DEVICE_ERROR_NO_DEVICE - device (SD card) is missing or not connected
129 * SD_BLOCK_DEVICE_ERROR_CRC - crc error
130 * SD_BLOCK_DEVICE_ERROR_PARAMETER - invalid parameter
131 * SD_BLOCK_DEVICE_ERROR_NO_RESPONSE - no response from device
132 * SD_BLOCK_DEVICE_ERROR_UNSUPPORTED - unsupported command
133 */
134 virtual int read(void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
135
136 /** Program blocks to a block device
137 *
138 * @note The blocks must be erased prior to programming
139 *
140 * @param buffer Buffer of data to write to blocks
141 * @param addr Address of block to begin writing to
142 * @param size Size to write in bytes. Must be a multiple of program block size
143 * @return BD_ERROR_OK(0) - success
144 * SD_BLOCK_DEVICE_ERROR_NO_DEVICE - device (SD card) is missing or not connected
145 * SD_BLOCK_DEVICE_ERROR_CRC - crc error
146 * SD_BLOCK_DEVICE_ERROR_PARAMETER - invalid parameter
147 * SD_BLOCK_DEVICE_ERROR_UNSUPPORTED - unsupported command
148 * SD_BLOCK_DEVICE_ERROR_NO_INIT - device is not initialized
149 * SD_BLOCK_DEVICE_ERROR_WRITE - SPI write error
150 * SD_BLOCK_DEVICE_ERROR_ERASE - erase error
151 */
152 virtual int program(const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size);
153
154 /** Mark blocks as no longer in use
155 *
156 * This function provides a hint to the underlying block device that a region of blocks
157 * is no longer in use and may be erased without side effects. Erase must still be called
158 * before programming, but trimming allows flash-translation-layers to schedule erases when
159 * the device is not busy.
160 *
161 * @param addr Address of block to mark as unused
162 * @param size Size to mark as unused in bytes, must be a multiple of erase block size
163 * @return BD_ERROR_OK(0) - success
164 * SD_BLOCK_DEVICE_ERROR_NO_DEVICE - device (SD card) is missing or not connected
165 * SD_BLOCK_DEVICE_ERROR_CRC - crc error
166 * SD_BLOCK_DEVICE_ERROR_PARAMETER - invalid parameter
167 * SD_BLOCK_DEVICE_ERROR_UNSUPPORTED - unsupported command
168 * SD_BLOCK_DEVICE_ERROR_NO_INIT - device is not initialized
169 * SD_BLOCK_DEVICE_ERROR_ERASE - erase error
170 */
172
173 /** Get the size of a readable block
174 *
175 * @return Size of a readable block in bytes
176 */
178
179 /** Get the size of a programmable block
180 *
181 * @return Size of a programmable block in bytes
182 * @note Must be a multiple of the read size
183 */
185
186 /** Get the total size of the underlying device
187 *
188 * @return Size of the underlying device in bytes
189 */
190 virtual mbed::bd_size_t size() const;
191
192 /** Enable or disable debugging
193 *
194 * @param dbg State of debugging
195 */
196 virtual void debug(bool dbg);
197
198 /** Set the transfer frequency
199 *
200 * @param freq Transfer frequency
201 * @note Max frequency supported is 25MHZ
202 */
203 virtual int frequency(uint64_t freq);
204
205 /** Get the BlockDevice class type.
206 *
207 * @return A string representation of the BlockDevice class type.
208 */
209 virtual const char *get_type() const;
210
211private:
212 /* Commands : Listed below are commands supported
213 * in SPI mode for SD card : Only Mandatory ones
214 */
215 enum cmdSupported {
216 CMD_NOT_SUPPORTED = -1, /**< Command not supported error */
217 CMD0_GO_IDLE_STATE = 0, /**< Resets the SD Memory Card */
218 CMD1_SEND_OP_COND = 1, /**< Sends host capacity support */
219 CMD6_SWITCH_FUNC = 6, /**< Check and Switches card function */
220 CMD8_SEND_IF_COND = 8, /**< Supply voltage info */
221 CMD9_SEND_CSD = 9, /**< Provides Card Specific data */
222 CMD10_SEND_CID = 10, /**< Provides Card Identification */
223 CMD12_STOP_TRANSMISSION = 12, /**< Forces the card to stop transmission */
224 CMD13_SEND_STATUS = 13, /**< Card responds with status */
225 CMD16_SET_BLOCKLEN = 16, /**< Length for SC card is set */
226 CMD17_READ_SINGLE_BLOCK = 17, /**< Read single block of data */
227 CMD18_READ_MULTIPLE_BLOCK = 18, /**< Card transfers data blocks to host until interrupted
228 by a STOP_TRANSMISSION command */
229 CMD24_WRITE_BLOCK = 24, /**< Write single block of data */
230 CMD25_WRITE_MULTIPLE_BLOCK = 25, /**< Continuously writes blocks of data until
231 'Stop Tran' token is sent */
232 CMD27_PROGRAM_CSD = 27, /**< Programming bits of CSD */
233 CMD32_ERASE_WR_BLK_START_ADDR = 32, /**< Sets the address of the first write
234 block to be erased. */
235 CMD33_ERASE_WR_BLK_END_ADDR = 33, /**< Sets the address of the last write
236 block of the continuous range to be erased.*/
237 CMD38_ERASE = 38, /**< Erases all previously selected write blocks */
238 CMD55_APP_CMD = 55, /**< Extend to Applications specific commands */
239 CMD56_GEN_CMD = 56, /**< General Purpose Command */
240 CMD58_READ_OCR = 58, /**< Read OCR register of card */
241 CMD59_CRC_ON_OFF = 59, /**< Turns the CRC option on or off*/
242 // App Commands
243 ACMD6_SET_BUS_WIDTH = 6,
244 ACMD13_SD_STATUS = 13,
245 ACMD22_SEND_NUM_WR_BLOCKS = 22,
246 ACMD23_SET_WR_BLK_ERASE_COUNT = 23,
247 ACMD41_SD_SEND_OP_COND = 41,
248 ACMD42_SET_CLR_CARD_DETECT = 42,
249 ACMD51_SEND_SCR = 51,
250 };
251
252 uint8_t _card_type;
253 int _cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAcmd = 0, uint32_t *resp = NULL);
254 int _cmd8();
255
256 /* Move the SD Card into the SPI Mode idle state
257 *
258 * The card is transitioned from SD Card mode to SPI mode by sending the
259 * CMD0 (GO_IDLE_STATE) command with CS asserted. See the notes in the
260 * "SPI Startup" section of the comments at the head of the
261 * implementation file for further details and specification references.
262 *
263 * @return Response form the card. R1_IDLE_STATE (0x1), the successful
264 * response from CMD0. R1_XXX_XXX for more response
265 */
266 uint32_t _go_idle_state();
267 int _initialise_card();
268
269 mbed::bd_size_t _sectors;
270 mbed::bd_size_t _sd_sectors();
271
272 bool _is_valid_trim(mbed::bd_addr_t addr, mbed::bd_size_t size);
273
274 /* SPI functions */
275 mbed::Timer _spi_timer; /**< Timer Class object used for busy wait */
276 uint32_t _init_sck; /**< Initial SPI frequency */
277 uint32_t _transfer_sck; /**< SPI frequency during data transfer/after initialization */
278 mbed::SPI _spi; /**< SPI Class object */
279
280 /* SPI initialization function */
281 void _spi_init();
282 uint8_t _cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg);
283 void _spi_wait(uint8_t count);
284
285 bool _wait_token(uint8_t token); /**< Wait for token */
286 bool _wait_ready(std::chrono::duration<uint32_t, std::milli> timeout = std::chrono::milliseconds{300}); /**< 300ms default wait for card to be ready */
287 int _read(uint8_t *buffer, uint32_t length);
288 int _read_bytes(uint8_t *buffer, uint32_t length);
289 uint8_t _write(const uint8_t *buffer, uint8_t token, uint32_t length);
290 int _freq(void);
291 void _preclock_then_select();
292 void _postclock_then_deselect();
293
294 virtual void lock()
295 {
296 _mutex.lock();
297 }
298
299 virtual void unlock()
300 {
301 _mutex.unlock();
302 }
303
304 rtos::Mutex _mutex;
305 static const uint32_t _block_size;
306 uint32_t _erase_size;
307 bool _is_initialized;
308 bool _dbg;
309 uint32_t _init_ref_count;
310
311#if MBED_CONF_SD_CRC_ENABLED
312 bool _crc_on;
313#endif
314
315#if DEVICE_SPI_ASYNCH
316 bool _async_spi_enabled = false;
317#endif
318};
319
320#endif /* DEVICE_SPI */
321
322#endif /* MBED_SD_BLOCK_DEVICE_H */
SDBlockDevice class.
Definition: SDBlockDevice.h:59
virtual const char * get_type() const
Get the BlockDevice class type.
virtual mbed::bd_size_t get_read_size() const
Get the size of a readable block.
virtual int read(void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size)
Read blocks from a block device.
virtual int program(const void *buffer, mbed::bd_addr_t addr, mbed::bd_size_t size)
Program blocks to a block device.
virtual void debug(bool dbg)
Enable or disable debugging.
virtual mbed::bd_size_t get_program_size() const
Get the size of a programmable block.
virtual mbed::bd_size_t size() const
Get the total size of the underlying device.
virtual int deinit()
Deinitialize a block device.
SDBlockDevice(const spi_pinmap_t &spi_pinmap, PinName cs=NC, uint64_t hz=1000000, bool crc_on=0)
Creates an SDBlockDevice on a SPI bus specified by pins (using static pin-map)
virtual int frequency(uint64_t freq)
Set the transfer frequency.
virtual int trim(mbed::bd_addr_t addr, mbed::bd_size_t size)
Mark blocks as no longer in use.
virtual int init()
Initialize a block device.
SDBlockDevice(PinName mosi=NC, PinName miso=NC, PinName sclk=NC, PinName cs=NC, uint64_t hz=1000000, bool crc_on=0)
Creates an SDBlockDevice on a SPI bus specified by pins (using dynamic pin-map)
void set_async_spi_mode(bool enabled, DMAUsage dma_usage_hint=DMAUsage::DMA_USAGE_NEVER)
Configure the usage of asynchronous SPI by this class.
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:53
An SPI Master, used for communicating with SPI slave devices.
Definition: SPI.h:257
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
void unlock()
Unlock the mutex that has previously been locked by the same thread.
void lock()
Wait until a Mutex becomes available.
DMAUsage
Enumeration of possible DMA usage hints.
Definition: dma_api.h:32
uint64_t bd_size_t
Type representing a quantity of 8-bit bytes.
Definition: BlockDevice.h:48
uint64_t bd_addr_t
Type representing the address of a specific block.
Definition: BlockDevice.h:44