forked from ARMmbed/sdio-driver
-
Notifications
You must be signed in to change notification settings - Fork 8
/
SDIOBlockDevice.cpp
418 lines (359 loc) · 10.9 KB
/
SDIOBlockDevice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* mbed Microcontroller Library
* Copyright (c) 2017 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include <errno.h>
#include "platform/mbed_debug.h"
#include "platform/mbed_wait_api.h"
#include "SDIOBlockDevice.h"
namespace mbed
{
/*
* defines
*/
#define SD_DBG 0 /*!< 1 - Enable debugging */
#define SD_CMD_TRACE 0 /*!< 1 - Enable SD command tracing */
#define SD_BLOCK_DEVICE_ERROR_WOULD_BLOCK -5001 /*!< operation would block */
#define SD_BLOCK_DEVICE_ERROR_UNSUPPORTED -5002 /*!< unsupported operation */
#define SD_BLOCK_DEVICE_ERROR_PARAMETER -5003 /*!< invalid parameter */
#define SD_BLOCK_DEVICE_ERROR_NO_INIT -5004 /*!< uninitialized */
#define SD_BLOCK_DEVICE_ERROR_NO_DEVICE -5005 /*!< device is missing or not connected */
#define SD_BLOCK_DEVICE_ERROR_WRITE_PROTECTED -5006 /*!< write protected */
#define SD_BLOCK_DEVICE_ERROR_UNUSABLE -5007 /*!< unusable card */
#define SD_BLOCK_DEVICE_ERROR_NO_RESPONSE -5008 /*!< No response from device */
#define SD_BLOCK_DEVICE_ERROR_CRC -5009 /*!< CRC error */
#define SD_BLOCK_DEVICE_ERROR_ERASE -5010 /*!< Erase error: reset/sequence */
#define SD_BLOCK_DEVICE_ERROR_WRITE -5011 /*!< SPI Write error: !SPI_DATA_ACCEPTED */
#define SD_BLOCK_DEVICE_ERROR_UNSUPPORTED_BLOCKSIZE -5012 /*!< unsupported blocksize, only 512 byte supported */
#define SD_BLOCK_DEVICE_ERROR_READBLOCKS -5013 /*!< read data blocks from SD failed */
#define SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS -5014 /*!< write data blocks to SD failed */
#define SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS -5015 /*!< erase data blocks to SD failed */
#define BLOCK_SIZE_HC 512 /*!< Block size supported for SD card is 512 bytes */
// Types
#define SDCARD_NONE 0 /**< No card is present */
#define SDCARD_V1 1 /**< v1.x Standard Capacity */
#define SDCARD_V2 2 /**< v2.x Standard capacity SD card */
#define SDCARD_V2HC 3 /**< v2.x High capacity SD card */
#define CARD_UNKNOWN 4 /**< Unknown or unsupported card */
#ifndef MBED_CONF_SD_TIMEOUT
#define MBED_CONF_SD_TIMEOUT (30 * 1000) /* ms */
#endif
constexpr auto delayTime = 0ms;
SDIOBlockDevice::SDIOBlockDevice(PinName cardDetect) : _cardDetect(cardDetect),
_is_initialized(0),
_sectors(0),
_init_ref_count(0)
{
_card_type = SDCARD_NONE;
// Only HC block size is supported.
_block_size = BLOCK_SIZE_HC;
_erase_size = BLOCK_SIZE_HC;
}
SDIOBlockDevice::~SDIOBlockDevice()
{
if (_is_initialized)
{
deinit();
}
}
int SDIOBlockDevice::init()
{
debug_if(SD_DBG, "init Card...\r\n");
lock();
if (!_is_initialized)
{
_init_ref_count = 0;
}
_init_ref_count++;
if (_init_ref_count != 1)
{
unlock();
return BD_ERROR_OK;
}
if (isPresent() == false)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE;
}
int status = SD_Init();
if (BD_ERROR_OK != status)
{
unlock();
return BD_ERROR_DEVICE_ERROR;
}
SD_GetCardInfo(&_cardInfo);
_is_initialized = true;
debug_if(SD_DBG, "SD initialized: type: %ld version: %ld class: %ld\n",
_cardInfo.CardType, _cardInfo.CardVersion, _cardInfo.Class);
debug_if(SD_DBG, "SD size: %ld MB\n",
_cardInfo.LogBlockNbr / 2 / 1024);
// get sectors count from cardinfo
_sectors = _cardInfo.LogBlockNbr;
if (BLOCK_SIZE_HC != _cardInfo.BlockSize)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_UNSUPPORTED_BLOCKSIZE;
}
unlock();
return status;
}
int SDIOBlockDevice::deinit()
{
debug_if(SD_DBG, "deinit Card...\r\n");
lock();
if (!_is_initialized)
{
_init_ref_count = 0;
unlock();
return BD_ERROR_OK;
}
_init_ref_count--;
if (_init_ref_count)
{
unlock();
return BD_ERROR_OK;
}
int status = SD_DeInit();
_is_initialized = false;
_sectors = 0;
unlock();
return status;
}
int SDIOBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
{
lock();
if (isPresent() == false)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE;
}
if (!is_valid_read(addr, size))
{
unlock();
return SD_BLOCK_DEVICE_ERROR_PARAMETER;
}
if (!_is_initialized)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_NO_INIT;
}
uint32_t *_buffer = static_cast<uint32_t *>(buffer);
// ReadBlocks uses byte unit address
// SDHC and SDXC Cards different addressing is handled in ReadBlocks()
bd_addr_t blockCnt = size / _block_size;
addr = addr / _block_size;
// make sure card is ready
{
uint32_t tickstart = HAL_GetTick();
while (SD_GetCardState() != SD_TRANSFER_OK)
{
// wait until SD ready
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
}
ThisThread::sleep_for(delayTime);
}
}
// receive the data : one block/ multiple blocks is handled in ReadBlocks()
int status = SD_ReadBlocks_DMA(_buffer, addr, blockCnt);
debug_if(SD_DBG, "ReadBlocks dbgtest addr: %lld blockCnt: %lld \n", addr, blockCnt);
if (status == MSD_OK)
{
// wait until DMA finished
uint32_t tickstart = HAL_GetTick();
while (SD_DMA_ReadPending() != SD_TRANSFER_OK)
{
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
}
ThisThread::sleep_for(delayTime);
}
// make sure card is ready
tickstart = HAL_GetTick();
while (SD_GetCardState() != SD_TRANSFER_OK)
{
// wait until SD ready
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
}
ThisThread::sleep_for(delayTime);
}
}
else
{
debug_if(SD_DBG, "ReadBlocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
unlock();
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
}
unlock();
return status;
}
int SDIOBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
{
lock();
if (isPresent() == false)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE;
}
if (!is_valid_program(addr, size))
{
unlock();
return SD_BLOCK_DEVICE_ERROR_PARAMETER;
}
if (!_is_initialized)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_NO_INIT;
}
// HAL layer uses uint32_t for addr/size
uint32_t *_buffer = (uint32_t *)(buffer);
// Get block count
bd_size_t blockCnt = size / _block_size;
addr = addr / _block_size;
// make sure card is ready
{
uint32_t tickstart = HAL_GetTick();
while (SD_GetCardState() != SD_TRANSFER_OK)
{
// wait until SD ready
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
}
}
}
int status = SD_WriteBlocks_DMA(_buffer, addr, blockCnt);
debug_if(SD_DBG, "WriteBlocks dbgtest addr: %lld blockCnt: %lld \n", addr, blockCnt);
if (status == MSD_OK)
{
// wait until DMA finished
uint32_t tickstart = HAL_GetTick();
while (SD_DMA_WritePending() != SD_TRANSFER_OK)
{
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
}
}
// make sure card is ready
tickstart = HAL_GetTick();
while (SD_GetCardState() != SD_TRANSFER_OK)
{
// wait until SD ready
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
}
}
}
else
{
debug_if(SD_DBG, "WriteBlocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
unlock();
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
}
unlock();
return status;
}
int SDIOBlockDevice::trim(bd_addr_t addr, bd_size_t size)
{
debug_if(SD_DBG, "trim Card...\r\n");
lock();
if (isPresent() == false)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE;
}
if (!_is_valid_trim(addr, size))
{
unlock();
return SD_BLOCK_DEVICE_ERROR_PARAMETER;
}
if (!_is_initialized)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_NO_INIT;
}
bd_size_t blockCnt = size / _block_size;
addr = addr / _block_size;
int status = SD_Erase(addr, blockCnt);
if (status != 0)
{
debug_if(SD_DBG, "Erase blocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
unlock();
return SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS;
}
else
{
uint32_t tickstart = HAL_GetTick();
while (SD_GetCardState() != SD_TRANSFER_OK)
{
// wait until SD ready
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
{
unlock();
return SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS;
}
}
}
unlock();
return status;
}
bd_size_t SDIOBlockDevice::get_read_size() const
{
return _block_size;
}
bd_size_t SDIOBlockDevice::get_program_size() const
{
return _block_size;
}
bd_size_t SDIOBlockDevice::size() const
{
return _block_size * _sectors;
}
void SDIOBlockDevice::debug(bool dbg)
{
}
bool SDIOBlockDevice::_is_valid_trim(bd_addr_t addr, bd_size_t size)
{
return (
addr % _erase_size == 0 &&
size % _erase_size == 0 &&
addr + size <= this->size());
}
bool SDIOBlockDevice::isPresent(void)
{
if (_cardDetect.is_connected()) {
return (_cardDetect.read() == 0);
}
else {
return true;
}
}
const char *SDIOBlockDevice::get_type() const
{
return "SDIO";
}
} // namespace mbed