source: rtems/c/src/lib/libbsp/arm/nds/libfat/source/disc_io/io_sd_common.c @ 311dfa6

4.104.114.95
Last change on this file since 311dfa6 was 311dfa6, checked in by Joel Sherrill <joel.sherrill@…>, on 04/16/08 at 18:37:33

2008-04-16 Matthieu Bucchianeri <mbucchia@…>

  • ChangeLog?, Makefile.am, README, bsp_specs, configure.ac, mk_libnds.sh, patch.libnds, preinstall.am, block/block.c, clock/clock.c, console/console.c, coproc/coproc.S, coproc/coproc.c, coproc/coproc.ld, dswifi/dswifi_license.txt, dswifi/makefile, dswifi/arm7/makefile, dswifi/arm7/source/wifi_arm7.c, dswifi/arm7/source/wifi_arm7.h, dswifi/arm9/makefile, dswifi/arm9/source/sgIP.c, dswifi/arm9/source/sgIP.h, dswifi/arm9/source/sgIP_ARP.c, dswifi/arm9/source/sgIP_ARP.h, dswifi/arm9/source/sgIP_Config.h, dswifi/arm9/source/sgIP_DHCP.c, dswifi/arm9/source/sgIP_DHCP.h, dswifi/arm9/source/sgIP_DNS.c, dswifi/arm9/source/sgIP_DNS.h, dswifi/arm9/source/sgIP_Hub.c, dswifi/arm9/source/sgIP_Hub.h, dswifi/arm9/source/sgIP_ICMP.c, dswifi/arm9/source/sgIP_ICMP.h, dswifi/arm9/source/sgIP_IP.c, dswifi/arm9/source/sgIP_IP.h, dswifi/arm9/source/sgIP_TCP.c, dswifi/arm9/source/sgIP_TCP.h, dswifi/arm9/source/sgIP_UDP.c, dswifi/arm9/source/sgIP_UDP.h, dswifi/arm9/source/sgIP_memblock.c, dswifi/arm9/source/sgIP_memblock.h, dswifi/arm9/source/sgIP_sockets.c, dswifi/arm9/source/sgIP_sockets.h, dswifi/arm9/source/wifi_arm9.c, dswifi/arm9/source/wifi_arm9.h, dswifi/common/source/dsregs.h, dswifi/common/source/spinlock.h, dswifi/common/source/spinlock.s, dswifi/common/source/wifi_shared.h, dswifi/include/dswifi7.h, dswifi/include/dswifi9.h, dswifi/include/dswifi_version.h, dswifi/include/netdb.h, dswifi/include/sgIP_errno.h, dswifi/include/netinet/in.h, fb/fb.c, fb/fb.h, include/bsp.h, include/my_ipc.h, include/tm27.h, include/types.h, include/sys/iosupport.h, irq/irq.c, irq/irq.h, libfat/gba/include/fat.h, libfat/include/fat.h, libfat/nds/include/fat.h, libfat/source/bit_ops.h, libfat/source/cache.c, libfat/source/cache.h, libfat/source/common.h, libfat/source/directory.c, libfat/source/directory.h, libfat/source/fatdir.c, libfat/source/fatdir.h, libfat/source/fatfile.c, libfat/source/fatfile.h, libfat/source/file_allocation_table.c, libfat/source/file_allocation_table.h, libfat/source/filetime.c, libfat/source/filetime.h, libfat/source/libfat.c, libfat/source/mem_allocate.h, libfat/source/partition.c, libfat/source/partition.h, libfat/source/disc_io/disc.c, libfat/source/disc_io/disc.h, libfat/source/disc_io/disc_io.h, libfat/source/disc_io/io_cf_common.c, libfat/source/disc_io/io_cf_common.h, libfat/source/disc_io/io_dldi.h, libfat/source/disc_io/io_dldi.s, libfat/source/disc_io/io_efa2.c, libfat/source/disc_io/io_efa2.h, libfat/source/disc_io/io_fcsr.c, libfat/source/disc_io/io_fcsr.h, libfat/source/disc_io/io_m3_common.c, libfat/source/disc_io/io_m3_common.h, libfat/source/disc_io/io_m3cf.c, libfat/source/disc_io/io_m3cf.h, libfat/source/disc_io/io_m3sd.c, libfat/source/disc_io/io_m3sd.h, libfat/source/disc_io/io_mpcf.c, libfat/source/disc_io/io_mpcf.h, libfat/source/disc_io/io_njsd.c, libfat/source/disc_io/io_njsd.h, libfat/source/disc_io/io_nmmc.c, libfat/source/disc_io/io_nmmc.h, libfat/source/disc_io/io_sc_common.c, libfat/source/disc_io/io_sc_common.h, libfat/source/disc_io/io_sccf.c, libfat/source/disc_io/io_sccf.h, libfat/source/disc_io/io_scsd.c, libfat/source/disc_io/io_scsd.h, libfat/source/disc_io/io_scsd_s.s, libfat/source/disc_io/io_sd_common.c, libfat/source/disc_io/io_sd_common.h, libnds/Makefile.arm7, libnds/Makefile.arm9, libnds/libnds_license.txt, libnds/basicARM7/source/defaultARM7.c, libnds/include/default_font_bin.h, libnds/include/gbfs.h, libnds/include/nds.h, libnds/include/nds/bios.h, libnds/include/nds/card.h, libnds/include/nds/dma.h, libnds/include/nds/interrupts.h, libnds/include/nds/ipc.h, libnds/include/nds/jtypes.h, libnds/include/nds/memory.h, libnds/include/nds/registers_alt.h, libnds/include/nds/reload.h, libnds/include/nds/system.h, libnds/include/nds/timers.h, libnds/include/nds/arm7/audio.h, libnds/include/nds/arm7/clock.h, libnds/include/nds/arm7/serial.h, libnds/include/nds/arm7/touch.h, libnds/include/nds/arm9/background.h, libnds/include/nds/arm9/boxtest.h, libnds/include/nds/arm9/cache.h, libnds/include/nds/arm9/console.h, libnds/include/nds/arm9/exceptions.h, libnds/include/nds/arm9/image.h, libnds/include/nds/arm9/input.h, libnds/include/nds/arm9/math.h, libnds/include/nds/arm9/ndsmotion.h, libnds/include/nds/arm9/pcx.h, libnds/include/nds/arm9/postest.h, libnds/include/nds/arm9/rumble.h, libnds/include/nds/arm9/sound.h, libnds/include/nds/arm9/sprite.h, libnds/include/nds/arm9/trig_lut.h, libnds/include/nds/arm9/video.h, libnds/include/nds/arm9/videoGL.h, libnds/source/arm7/audio.c, libnds/source/arm7/clock.c, libnds/source/arm7/microphone.c, libnds/source/arm7/spi.c, libnds/source/arm7/touch.c, libnds/source/arm7/userSettings.c, libnds/source/arm9/COS.bin, libnds/source/arm9/COS.s, libnds/source/arm9/SIN.bin, libnds/source/arm9/SIN.s, libnds/source/arm9/TAN.bin, libnds/source/arm9/TAN.s, libnds/source/arm9/boxtest.c, libnds/source/arm9/console.c, libnds/source/arm9/dcache.s, libnds/source/arm9/default_font.bin, libnds/source/arm9/default_font.s, libnds/source/arm9/exceptionHandler.S, libnds/source/arm9/exceptionHandler.s, libnds/source/arm9/exceptions.c, libnds/source/arm9/gurumeditation.c, libnds/source/arm9/icache.s, libnds/source/arm9/image.c, libnds/source/arm9/initSystem.c, libnds/source/arm9/keys.c, libnds/source/arm9/ndsmotion.c, libnds/source/arm9/pcx.c, libnds/source/arm9/rumble.c, libnds/source/arm9/sound.c, libnds/source/arm9/system.c, libnds/source/arm9/touch.c, libnds/source/arm9/video.c, libnds/source/arm9/videoGL.c, libnds/source/common/biosCalls.s, libnds/source/common/card.c, libnds/source/common/gbfs.c, libnds/source/common/interruptDispatcher.s, libnds/source/common/interrupts.c, rtc/rtc.c, sound/sound.c, sound/sound.h, start/start.S, startup/linkcmds, startup/start.c, timer/timer.c, tools/Makefile.am, tools/bin2s, tools/bin2s.c, tools/configure.ac, tools/runtest, tools/ndstool/include/arm7_sha1_homebrew.h, tools/ndstool/include/arm7_sha1_nintendo.h, tools/ndstool/include/banner.h, tools/ndstool/include/bigint.h, tools/ndstool/include/crc.h, tools/ndstool/include/default_icon.h, tools/ndstool/include/encryption.h, tools/ndstool/include/header.h, tools/ndstool/include/hook.h, tools/ndstool/include/little.h, tools/ndstool/include/loadme.h, tools/ndstool/include/logo.h, tools/ndstool/include/ndscreate.h, tools/ndstool/include/ndsextract.h, tools/ndstool/include/ndstool.h, tools/ndstool/include/ndstree.h, tools/ndstool/include/overlay.h, tools/ndstool/include/passme.h, tools/ndstool/include/passme_sram.h, tools/ndstool/include/passme_vhd1.h, tools/ndstool/include/passme_vhd2.h, tools/ndstool/include/raster.h, tools/ndstool/include/sha1.h, tools/ndstool/include/types.h, tools/ndstool/source/arm7_sha1_homebrew.c, tools/ndstool/source/arm7_sha1_nintendo.c, tools/ndstool/source/banner.cpp, tools/ndstool/source/bigint.cpp, tools/ndstool/source/compile_date.c, tools/ndstool/source/crc.cpp, tools/ndstool/source/default_icon.c, tools/ndstool/source/encryption.cpp, tools/ndstool/source/header.cpp, tools/ndstool/source/hook.cpp, tools/ndstool/source/loadme.c, tools/ndstool/source/logo.cpp, tools/ndstool/source/ndscodes.cpp, tools/ndstool/source/ndscreate.cpp, tools/ndstool/source/ndsextract.cpp, tools/ndstool/source/ndstool.cpp, tools/ndstool/source/ndstree.cpp, tools/ndstool/source/passme.cpp, tools/ndstool/source/passme_sram.c, tools/ndstool/source/raster.cpp, tools/ndstool/source/sha1.cpp, touchscreen/README.reco, touchscreen/parser.c, touchscreen/reco.c, touchscreen/reco.h, touchscreen/touchscreen.c, touchscreen/touchscreen.h, wifi/compat.c, wifi/compat.h, wifi/wifi.c: New files.
  • Property mode set to 100644
File size: 6.1 KB
Line 
1/*
2        io_sd_common.c
3
4        By chishm (Michael Chisholm)
5
6        Common SD card routines
7
8        SD routines partially based on sd.s by Romman
9
10 Copyright (c) 2006 Michael "Chishm" Chisholm
11       
12 Redistribution and use in source and binary forms, with or without modification,
13 are permitted provided that the following conditions are met:
14
15  1. Redistributions of source code must retain the above copyright notice,
16     this list of conditions and the following disclaimer.
17  2. Redistributions in binary form must reproduce the above copyright notice,
18     this list of conditions and the following disclaimer in the documentation and/or
19     other materials provided with the distribution.
20  3. The name of the author may not be used to endorse or promote products derived
21     from this software without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
26 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32               
33        2006-08-07 - Chishm
34                * Moved the SD initialization to a common function
35                * Increased timeouts for slower cards
36               
37        2006-08-08 - Chishm
38                * Init aborts when it doesn't get a valid response to APP_CMD - speeds up detection when no card is inserted
39*/
40
41#include "io_sd_common.h"
42
43#define MAX_STARTUP_TRIES 1000  // Arbitrary value, check if the card is ready 20 times before giving up
44#define RESPONSE_TIMEOUT 256    // Number of clocks sent to the SD card before giving up
45
46/*
47Improved CRC7 function provided by cory1492
48Calculates the CRC of an SD command, and includes the end bit in the byte
49*/
50u8 _SD_CRC7(u8* data, int cnt) {
51    int i, a;
52    u8 crc, temp;
53
54    crc = 0;
55    for (a = 0; a < cnt; a++)
56    {
57        temp = data[a];
58        for (i = 0; i < 8; i++)
59        {
60            crc <<= 1;
61            if ((temp & 0x80) ^ (crc & 0x80)) crc ^= 0x09;
62            temp <<= 1;
63        }
64    }
65    crc = (crc << 1) | 1;
66    return(crc);
67}
68
69/*
70Calculates the CRC16 for a sector of data. Calculates it
71as 4 separate lots, merged into one buffer. This is used
72for 4 SD data lines, not for 1 data line alone.
73*/
74void _SD_CRC16 (u8* buff, int buffLength, u8* crc16buff) {
75        u32 a, b, c, d;
76        int count;
77        u32 bitPattern = 0x80808080;    // r7
78        u32 crcConst = 0x1021;  // r8
79        u32 dataByte = 0;       // r2
80
81        a = 0;  // r3
82        b = 0;  // r4
83        c = 0;  // r5
84        d = 0;  // r6
85       
86        buffLength = buffLength * 8;
87       
88       
89        do {
90                if (bitPattern & 0x80) dataByte = *buff++;
91               
92                a = a << 1;
93                if ( a & 0x10000) a ^= crcConst;
94                if (dataByte & (bitPattern >> 24)) a ^= crcConst;
95               
96                b = b << 1;
97                if (b & 0x10000) b ^= crcConst;
98                if (dataByte & (bitPattern >> 25)) b ^= crcConst;
99       
100                c = c << 1;
101                if (c & 0x10000) c ^= crcConst;
102                if (dataByte & (bitPattern >> 26)) c ^= crcConst;
103               
104                d = d << 1;
105                if (d & 0x10000) d ^= crcConst;
106                if (dataByte & (bitPattern >> 27)) d ^= crcConst;
107               
108                bitPattern = (bitPattern >> 4) | (bitPattern << 28);
109        } while (buffLength-=4);
110       
111        count = 16;     // r8
112       
113        do {
114                bitPattern = bitPattern << 4;
115                if (a & 0x8000) bitPattern |= 8;
116                if (b & 0x8000) bitPattern |= 4;
117                if (c & 0x8000) bitPattern |= 2;
118                if (d & 0x8000) bitPattern |= 1;
119       
120                a = a << 1;
121                b = b << 1;
122                c = c << 1;
123                d = d << 1;
124               
125                count--;
126               
127                if (!(count & 0x01)) {
128                        *crc16buff++ = (u8)(bitPattern & 0xff);
129                }
130        } while (count != 0);
131       
132        return;
133}
134
135/*
136Initialise the SD card, after it has been sent into an Idle state
137cmd_6byte_response: a pointer to a function that sends the SD card a command and gets a 6 byte response
138cmd_17byte_response: a pointer to a function that sends the SD card a command and gets a 17 byte response
139use4bitBus: initialise card to use a 4 bit data bus when communicating with the card
140RCA: a pointer to the location to store the card's Relative Card Address, preshifted up by 16 bits.
141*/
142bool _SD_InitCard (_SD_FN_CMD_6BYTE_RESPONSE cmd_6byte_response,
143                                        _SD_FN_CMD_17BYTE_RESPONSE cmd_17byte_response,
144                                        bool use4bitBus,
145                                        u32 *RCA)
146{
147        u8 responseBuffer[17] = {0};
148        int i;
149       
150        for (i = 0; i < MAX_STARTUP_TRIES ; i++) {
151                cmd_6byte_response (responseBuffer, APP_CMD, 0);
152                // Check that the card gave the correct response
153                if (responseBuffer[0] != APP_CMD) {
154                        return false;
155                }
156                if (
157                        cmd_6byte_response (responseBuffer, SD_APP_OP_COND, SD_OCR_VALUE) &&
158                        ((responseBuffer[1] & 0x80) != 0))
159                {       
160                        // Card is ready to receive commands now
161                        break;
162                }
163        }
164        if (i >= MAX_STARTUP_TRIES) {
165                return false;
166        }
167 
168        // The card's name, as assigned by the manufacturer
169        cmd_17byte_response (responseBuffer, ALL_SEND_CID, 0);
170 
171        // Get a new address
172        for (i = 0; i < MAX_STARTUP_TRIES ; i++) {
173                cmd_6byte_response (responseBuffer, SEND_RELATIVE_ADDR, 0);
174                *RCA = (responseBuffer[1] << 24) | (responseBuffer[2] << 16);
175                if ((responseBuffer[3] & 0x1e) != (SD_STATE_STBY << 1)) {
176                        break;
177                }
178        }
179        if (i >= MAX_STARTUP_TRIES) {
180                return false;
181        }
182
183        // Some cards won't go to higher speeds unless they think you checked their capabilities
184        cmd_17byte_response (responseBuffer, SEND_CSD, *RCA);
185 
186        // Only this card should respond to all future commands
187        cmd_6byte_response (responseBuffer, SELECT_CARD, *RCA);
188 
189        if (use4bitBus) {
190                // Set a 4 bit data bus
191                cmd_6byte_response (responseBuffer, APP_CMD, *RCA);
192                cmd_6byte_response (responseBuffer, SET_BUS_WIDTH, 2); // 4-bit mode.
193        }
194
195        // Use 512 byte blocks
196        cmd_6byte_response (responseBuffer, SET_BLOCKLEN, 512); // 512 byte blocks
197       
198        // Wait until card is ready for data
199        i = 0;
200        do {
201                if (i >= RESPONSE_TIMEOUT) {
202                        return false;
203                }
204                i++;
205        } while (!cmd_6byte_response (responseBuffer, SEND_STATUS, *RCA) && ((responseBuffer[3] & 0x1f) != ((SD_STATE_TRAN << 1) | READY_FOR_DATA)));
206 
207        return true;
208}
209
210
Note: See TracBrowser for help on using the repository browser.