Skip to content

Commit a535194

Browse files
committed
Merge ARMmbed#1 and ARMmbed#2
- ARMmbed#1 is based on [JojoS62/sdio-driver/master](JojoS62@c03489c). - ARMmbed#2 is based on [NXPmicro/sdio-driver/Add_SDIO_LPC55S69](nxp-archive@370c51a)
2 parents c03489c + 370c51a commit a535194

29 files changed

+5582
-195
lines changed
Lines changed: 41 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2017 ARM Limited
2+
* Copyright (c) 2019 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
56
* you may not use this file except in compliance with the License.
@@ -14,13 +15,11 @@
1415
* limitations under the License.
1516
*/
1617

17-
#include <errno.h>
18-
#include "platform/mbed_debug.h"
19-
#include "platform/mbed_wait_api.h"
2018
#include "SDIOBlockDevice.h"
19+
#include "platform/mbed_debug.h"
20+
#include "sdio_device.h"
2121

22-
namespace mbed
23-
{
22+
using namespace mbed;
2423

2524
/*
2625
* defines
@@ -63,17 +62,14 @@ SDIOBlockDevice::SDIOBlockDevice(PinName cardDetect) : _cardDetect(cardDetect),
6362
_sectors(0),
6463
_init_ref_count(0)
6564
{
66-
_card_type = SDCARD_NONE;
67-
6865
// Only HC block size is supported.
6966
_block_size = BLOCK_SIZE_HC;
7067
_erase_size = BLOCK_SIZE_HC;
7168
}
7269

7370
SDIOBlockDevice::~SDIOBlockDevice()
7471
{
75-
if (_is_initialized)
76-
{
72+
if (_is_initialized) {
7773
deinit();
7874
}
7975
}
@@ -84,48 +80,39 @@ int SDIOBlockDevice::init()
8480

8581
lock();
8682

87-
if (!_is_initialized)
88-
{
83+
if (!_is_initialized) {
8984
_init_ref_count = 0;
9085
}
9186

9287
_init_ref_count++;
9388

94-
if (_init_ref_count != 1)
95-
{
89+
if (_init_ref_count != 1) {
9690
unlock();
9791
return BD_ERROR_OK;
9892
}
9993

100-
if (isPresent() == false)
101-
{
94+
if (isPresent() == false) {
10295
unlock();
10396
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE;
10497
}
10598

106-
int status = SD_Init();
107-
if (BD_ERROR_OK != status)
108-
{
99+
int status = SDIO_Device_Init();
100+
if (BD_ERROR_OK != status) {
109101
unlock();
110102
return BD_ERROR_DEVICE_ERROR;
111103
}
112104

113-
SD_GetCardInfo(&_cardInfo);
114105
_is_initialized = true;
115-
debug_if(SD_DBG, "SD initialized: type: %d version: %d class: %d\n",
116-
_cardInfo.CardType, _cardInfo.CardVersion, _cardInfo.Class);
117-
debug_if(SD_DBG, "SD size: %d MB\n",
118-
_cardInfo.LogBlockNbr / 2 / 1024);
119106

120107
// get sectors count from cardinfo
121-
_sectors = _cardInfo.LogBlockNbr;
122-
if (BLOCK_SIZE_HC != _cardInfo.BlockSize)
123-
{
108+
_sectors = SDIO_Device_GetBlockCount();
109+
if (BLOCK_SIZE_HC != SDIO_Device_GetBlockSize()) {
124110
unlock();
125111
return SD_BLOCK_DEVICE_ERROR_UNSUPPORTED_BLOCKSIZE;
126112
}
127113

128114
unlock();
115+
129116
return status;
130117
}
131118

@@ -134,22 +121,20 @@ int SDIOBlockDevice::deinit()
134121
debug_if(SD_DBG, "deinit Card...\r\n");
135122
lock();
136123

137-
if (!_is_initialized)
138-
{
124+
if (!_is_initialized) {
139125
_init_ref_count = 0;
140126
unlock();
141127
return BD_ERROR_OK;
142128
}
143129

144130
_init_ref_count--;
145131

146-
if (_init_ref_count)
147-
{
132+
if (_init_ref_count) {
148133
unlock();
149134
return BD_ERROR_OK;
150135
}
151136

152-
int status = SD_DeInit();
137+
int status = SDIO_Device_DeInit();
153138
_is_initialized = false;
154139

155140
_sectors = 0;
@@ -161,19 +146,16 @@ int SDIOBlockDevice::deinit()
161146
int SDIOBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
162147
{
163148
lock();
164-
if (isPresent() == false)
165-
{
149+
if (isPresent() == false) {
166150
unlock();
167151
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE;
168152
}
169-
if (!is_valid_read(addr, size))
170-
{
153+
if (!is_valid_read(addr, size)) {
171154
unlock();
172155
return SD_BLOCK_DEVICE_ERROR_PARAMETER;
173156
}
174157

175-
if (!_is_initialized)
176-
{
158+
if (!_is_initialized) {
177159
unlock();
178160
return SD_BLOCK_DEVICE_ERROR_NO_INIT;
179161
}
@@ -185,76 +167,33 @@ int SDIOBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
185167
bd_addr_t blockCnt = size / _block_size;
186168
addr = addr / _block_size;
187169

188-
// make sure card is ready
189-
{
190-
uint32_t tickstart = HAL_GetTick();
191-
while (SD_GetCardState() != SD_TRANSFER_OK)
192-
{
193-
// wait until SD ready
194-
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
195-
{
196-
unlock();
197-
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
198-
}
199-
}
200-
}
201-
202170
// receive the data : one block/ multiple blocks is handled in ReadBlocks()
203-
int status = SD_ReadBlocks_DMA(_buffer, addr, blockCnt);
171+
int status = SDIO_Device_ReadBlocks(_buffer, addr, blockCnt, MBED_CONF_SD_TIMEOUT);
204172
debug_if(SD_DBG, "ReadBlocks dbgtest addr: %lld blockCnt: %lld \n", addr, blockCnt);
205-
206-
if (status == MSD_OK)
207-
{
208-
// wait until DMA finished
209-
uint32_t tickstart = HAL_GetTick();
210-
while (SD_DMA_ReadPending() != SD_TRANSFER_OK)
211-
{
212-
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
213-
{
214-
unlock();
215-
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
216-
}
217-
}
218-
// make sure card is ready
219-
tickstart = HAL_GetTick();
220-
while (SD_GetCardState() != SD_TRANSFER_OK)
221-
{
222-
// wait until SD ready
223-
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
224-
{
225-
unlock();
226-
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
227-
}
228-
}
229-
}
230-
else
231-
{
173+
if (status != MSD_OK) {
232174
debug_if(SD_DBG, "ReadBlocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
233-
unlock();
234-
return SD_BLOCK_DEVICE_ERROR_READBLOCKS;
175+
status = SD_BLOCK_DEVICE_ERROR_READBLOCKS;
235176
}
236177

237178
unlock();
179+
238180
return status;
239181
}
240182

241183
int SDIOBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
242184
{
243185
lock();
244186

245-
if (isPresent() == false)
246-
{
187+
if (isPresent() == false) {
247188
unlock();
248189
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE;
249190
}
250-
if (!is_valid_program(addr, size))
251-
{
191+
if (!is_valid_program(addr, size)) {
252192
unlock();
253193
return SD_BLOCK_DEVICE_ERROR_PARAMETER;
254194
}
255195

256-
if (!_is_initialized)
257-
{
196+
if (!_is_initialized) {
258197
unlock();
259198
return SD_BLOCK_DEVICE_ERROR_NO_INIT;
260199
}
@@ -266,101 +205,44 @@ int SDIOBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
266205
bd_size_t blockCnt = size / _block_size;
267206
addr = addr / _block_size;
268207

269-
// make sure card is ready
270-
{
271-
uint32_t tickstart = HAL_GetTick();
272-
while (SD_GetCardState() != SD_TRANSFER_OK)
273-
{
274-
// wait until SD ready
275-
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
276-
{
277-
unlock();
278-
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
279-
}
280-
}
281-
}
282-
283-
int status = SD_WriteBlocks_DMA(_buffer, addr, blockCnt);
208+
int status = SDIO_Device_WriteBlocks(_buffer, addr, blockCnt, MBED_CONF_SD_TIMEOUT);
284209
debug_if(SD_DBG, "WriteBlocks dbgtest addr: %lld blockCnt: %lld \n", addr, blockCnt);
285210

286-
if (status == MSD_OK)
287-
{
288-
// wait until DMA finished
289-
uint32_t tickstart = HAL_GetTick();
290-
while (SD_DMA_WritePending() != SD_TRANSFER_OK)
291-
{
292-
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
293-
{
294-
unlock();
295-
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
296-
}
297-
}
298-
// make sure card is ready
299-
tickstart = HAL_GetTick();
300-
while (SD_GetCardState() != SD_TRANSFER_OK)
301-
{
302-
// wait until SD ready
303-
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
304-
{
305-
unlock();
306-
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
307-
}
308-
}
309-
}
310-
else
311-
{
211+
if (status != MSD_OK) {
312212
debug_if(SD_DBG, "WriteBlocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
313-
unlock();
314-
return SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
213+
status = SD_BLOCK_DEVICE_ERROR_WRITEBLOCKS;
315214
}
316215

317216
unlock();
217+
318218
return status;
319219
}
320220

321221
int SDIOBlockDevice::trim(bd_addr_t addr, bd_size_t size)
322222
{
323223
debug_if(SD_DBG, "trim Card...\r\n");
324224
lock();
325-
if (isPresent() == false)
326-
{
225+
if (isPresent() == false) {
327226
unlock();
328227
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE;
329228
}
330-
if (!_is_valid_trim(addr, size))
331-
{
229+
if (!_is_valid_trim(addr, size)) {
332230
unlock();
333231
return SD_BLOCK_DEVICE_ERROR_PARAMETER;
334232
}
335233

336-
if (!_is_initialized)
337-
{
234+
if (!_is_initialized) {
338235
unlock();
339236
return SD_BLOCK_DEVICE_ERROR_NO_INIT;
340237
}
341238

342239
bd_size_t blockCnt = size / _block_size;
343240
addr = addr / _block_size;
344241

345-
int status = SD_Erase(addr, blockCnt);
346-
if (status != 0)
347-
{
242+
int status = SDIO_Device_Erase(addr, blockCnt, MBED_CONF_SD_TIMEOUT);
243+
if (status != 0) {
348244
debug_if(SD_DBG, "Erase blocks failed! addr: %lld blockCnt: %lld \n", addr, blockCnt);
349-
unlock();
350-
return SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS;
351-
}
352-
else
353-
{
354-
uint32_t tickstart = HAL_GetTick();
355-
while (SD_GetCardState() != SD_TRANSFER_OK)
356-
{
357-
// wait until SD ready
358-
if ((HAL_GetTick() - tickstart) >= MBED_CONF_SD_TIMEOUT)
359-
{
360-
unlock();
361-
return SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS;
362-
}
363-
}
245+
status = SD_BLOCK_DEVICE_ERROR_ERASEBLOCKS;
364246
}
365247

366248
unlock();
@@ -404,4 +286,7 @@ bool SDIOBlockDevice::isPresent(void)
404286
}
405287
}
406288

407-
} // namespace mbed
289+
const char *SDIOBlockDevice::get_type() const
290+
{
291+
return "SDIO";
292+
}

0 commit comments

Comments
 (0)