summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/nds/libfat/source/disc_io/io_m3sd.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--c/src/lib/libbsp/arm/nds/libfat/source/disc_io/io_m3sd.c78
1 files changed, 39 insertions, 39 deletions
diff --git a/c/src/lib/libbsp/arm/nds/libfat/source/disc_io/io_m3sd.c b/c/src/lib/libbsp/arm/nds/libfat/source/disc_io/io_m3sd.c
index 322ad007f3..88f205a249 100644
--- a/c/src/lib/libbsp/arm/nds/libfat/source/disc_io/io_m3sd.c
+++ b/c/src/lib/libbsp/arm/nds/libfat/source/disc_io/io_m3sd.c
@@ -1,14 +1,14 @@
/*
- io_m3sd.c
+ io_m3sd.c
Hardware Routines for reading a Secure Digital card
using the M3 SD
-
+
Some code based on M3 SD drivers supplied by M3Adapter.
Some code written by SaTa may have been unknowingly used.
Copyright (c) 2006 Michael "Chishm" Chisholm
-
+
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
@@ -29,7 +29,7 @@
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
+
2006-07-25 - Chishm
* Improved startup function that doesn't delay hundreds of seconds
before reporting no card inserted.
@@ -38,7 +38,7 @@
2006-08-05 - Chishm
* Tries multiple times to get a Relative Card Address at startup
-
+
2006-08-07 - Chishm
* Moved the SD initialization to a common function
*/
@@ -104,7 +104,7 @@ static bool _M3SD_sendCommand (u16 command, u32 argument) {
REG_M3SD_ARGH = argument >> 16;
REG_M3SD_ARGL = argument;
// The CRC7 of the command is calculated by the M3
-
+
REG_M3SD_DIR=0x29;
if (!_M3SD_waitOnBusy()) {
REG_M3SD_DIR=0x09;
@@ -156,10 +156,10 @@ static u8 _M3SD_getByte (void) {
// Returns the response from the SD card to a previous command.
static bool _M3SD_getResponse (u8* dest, u32 length) {
- u32 i;
+ u32 i;
u8 dataByte;
int shiftAmount;
-
+
// Wait for the card to be non-busy
for (i = 0; i < RESPONSE_TIMEOUT; i++) {
dataByte = _M3SD_getByte();
@@ -167,27 +167,27 @@ static bool _M3SD_getResponse (u8* dest, u32 length) {
break;
}
}
-
+
if (dest == NULL) {
return true;
}
-
+
// Still busy after the timeout has passed
if (dataByte == 0xff) {
return false;
}
-
+
// Read response into buffer
for ( i = 0; i < length; i++) {
dest[i] = dataByte;
dataByte = _M3SD_getByte();
}
// dataByte will contain the last piece of the response
-
+
// Send 16 more clocks, 8 more than the delay required between a response and the next command
i = _M3SD_getByte();
i = _M3SD_getByte();
-
+
// Shift response so that the bytes are correctly aligned
// The register may not contain properly aligned data
for (shiftAmount = 0; ((dest[0] << shiftAmount) & 0x80) != 0x00; shiftAmount++) {
@@ -195,7 +195,7 @@ static bool _M3SD_getResponse (u8* dest, u32 length) {
return false;
}
}
-
+
for (i = 0; i < length - 1; i++) {
dest[i] = (dest[i] << shiftAmount) | (dest[i+1] >> (8-shiftAmount));
}
@@ -251,7 +251,7 @@ bool _M3SD_cmd_17byte_response (u8* responseBuffer, u8 command, u32 data) {
static bool _M3SD_initCard (void) {
// Give the card time to stabilise
_M3SD_sendClocks (NUM_STARTUP_CLOCKS);
-
+
// Reset the card
if (!_M3SD_sendCommand (GO_IDLE_STATE, 0)) {
return false;
@@ -261,9 +261,9 @@ static bool _M3SD_initCard (void) {
// Card is now reset, including it's address
_M3SD_relativeCardAddress = 0;
-
+
// Init the card
- return _SD_InitCard (_M3SD_cmd_6byte_response,
+ return _SD_InitCard (_M3SD_cmd_6byte_response,
_M3SD_cmd_17byte_response,
true,
&_M3SD_relativeCardAddress);
@@ -281,10 +281,10 @@ static bool _M3SD_readData (void* buffer) {
return false;
}
REG_M3SD_DIR = 0x09;
-
+
REG_M3SD_DIR = 0x8;
REG_M3SD_STS = 0x4;
-
+
i = REG_M3SD_DIR;
// Read data
i=256;
@@ -297,11 +297,11 @@ static bool _M3SD_readData (void* buffer) {
}
} else {
while(i--)
- *buff++ = REG_M3SD_DIR;
+ *buff++ = REG_M3SD_DIR;
}
// Read end checksum
i = REG_M3SD_DIR + REG_M3SD_DIR + REG_M3SD_DIR + REG_M3SD_DIR;
-
+
return true;
}
@@ -309,7 +309,7 @@ static void _M3SD_clkout (void) {
REG_M3SD_DIR = 0x4;
REG_M3SD_DIR = 0xc;
/* __asm volatile (
- "ldr r1, =0x08800000 \n"
+ "ldr r1, =0x08800000 \n"
"mov r0, #0x04 \n"
"strh r0, [r1] \n"
"mov r0, r0 \n"
@@ -326,7 +326,7 @@ static void _M3SD_clkin (void) {
REG_M3SD_DIR = 0x0;
REG_M3SD_DIR = 0x8;
/* __asm volatile (
- "ldr r1, =0x08800000 \n"
+ "ldr r1, =0x08800000 \n"
"mov r0, #0x00 \n"
"strh r0, [r1] \n"
"mov r0, r0 \n"
@@ -346,11 +346,11 @@ static bool _M3SD_writeData (u8* data, u8* crc) {
do {
_M3SD_clkin();
} while ((REG_M3SD_DAT & 0x100) == 0);
-
+
REG_M3SD_DAT = 0; // Start bit
-
+
_M3SD_clkout();
-
+
for (i = 0; i < BYTES_PER_READ; i++) {
temp = (*data++);
REG_M3SD_DAT = temp >> 4;
@@ -358,7 +358,7 @@ static bool _M3SD_writeData (u8* data, u8* crc) {
REG_M3SD_DAT = temp;
_M3SD_clkout();
}
-
+
if (crc != NULL) {
for (i = 0; i < 8; i++) {
temp = (*crc++);
@@ -373,16 +373,16 @@ static bool _M3SD_writeData (u8* data, u8* crc) {
while (i--) {
temp += 2; // a NOP to stop the compiler optimising out the loop
}
-
+
for (i = 0; i < 32; i++) {
REG_M3SD_DAT = 0xff;
_M3SD_clkout();
}
-
+
do {
_M3SD_clkin();
} while ((REG_M3SD_DAT & 0x100) == 0);
-
+
return true;
}
@@ -415,7 +415,7 @@ bool _M3SD_readSectors (u32 sector, u32 numSectors, void* buffer) {
u32 i;
u8* dest = (u8*) buffer;
u8 responseBuffer[6];
-
+
if (numSectors == 1) {
// If it's only reading one sector, use the (slightly faster) READ_SINGLE_BLOCK
if (!_M3SD_sendCommand (READ_SINGLE_BLOCK, sector * BYTES_PER_READ)) {
@@ -431,14 +431,14 @@ bool _M3SD_readSectors (u32 sector, u32 numSectors, void* buffer) {
if (!_M3SD_sendCommand (READ_MULTIPLE_BLOCK, sector * BYTES_PER_READ)) {
return false;
}
-
+
for(i=0; i < numSectors; i++, dest+=BYTES_PER_READ) {
if (!_M3SD_readData(dest)) {
return false;
}
REG_M3SD_STS = 0x8;
}
-
+
// Stop the streaming
_M3SD_sendCommand (STOP_TRANSMISSION, 0);
_M3SD_getResponse_R1b (responseBuffer);
@@ -455,29 +455,29 @@ bool _M3SD_writeSectors (u32 sector, u32 numSectors, const void* buffer) {
int i;
// Precalculate the data CRC
_SD_CRC16 ( data, BYTES_PER_READ, crc);
-
+
while (numSectors--) {
// Send a single sector write command
_M3SD_sendCommand (WRITE_BLOCK, offset);
if (!_M3SD_getResponse_R1 (responseBuffer)) {
return false;
}
-
+
REG_M3SD_DIR = 0x4;
REG_M3SD_STS = 0x0;
-
+
// Send the data
if (! _M3SD_writeData( data, crc)) {
return false;
}
-
+
if (numSectors > 0) {
offset += BYTES_PER_READ;
data += BYTES_PER_READ;
// Calculate the next CRC while waiting for the card to finish writing
_SD_CRC16 ( data, BYTES_PER_READ, crc);
}
-
+
// Wait for the card to be ready for the next transfer
i = WRITE_TIMEOUT;
responseBuffer[3] = 0;
@@ -490,7 +490,7 @@ bool _M3SD_writeSectors (u32 sector, u32 numSectors, const void* buffer) {
}
} while (((responseBuffer[3] & 0x1f) != ((SD_STATE_TRAN << 1) | READY_FOR_DATA)));
}
-
+
return true;
}