Changed all IO_INTERFACEs to const. M3SD and SCSD drivers no longer delay card detection when it is not inserted.

This commit is contained in:
Michael Chisholm 2006-07-25 10:36:42 +00:00
parent 58996ae0d2
commit eb2b6d1e20
26 changed files with 97 additions and 60 deletions

View File

@ -87,7 +87,7 @@ bool fatMountNormalInterface (PARTITION_INTERFACE partitionNumber, u32 cacheSize
/*
Mount a partition on a custom device
*/
bool fatMountCustomInterface (struct IO_INTERFACE_STRUCT* device, u32 cacheSize);
bool fatMountCustomInterface (const struct IO_INTERFACE_STRUCT* device, u32 cacheSize);
/*
Unmount the partition specified by partitionNumber

View File

@ -43,7 +43,7 @@
#define CACHE_FREE 0xFFFFFFFF
CACHE* _FAT_cache_constructor (u32 numberOfPages, IO_INTERFACE* discInterface) {
CACHE* _FAT_cache_constructor (u32 numberOfPages, const IO_INTERFACE* discInterface) {
CACHE* cache;
u32 i;
CACHE_ENTRY* cacheEntries;

View File

@ -51,7 +51,7 @@ typedef struct {
} CACHE_ENTRY;
typedef struct {
IO_INTERFACE* disc;
const IO_INTERFACE* disc;
u32 numberOfPages;
CACHE_ENTRY* cacheEntries;
u8* pages;
@ -111,7 +111,7 @@ Clear out the contents of the cache without writing any dirty sectors first
*/
void _FAT_cache_invalidate (CACHE* cache);
CACHE* _FAT_cache_constructor (u32 numberOfPages, IO_INTERFACE* discInterface);
CACHE* _FAT_cache_constructor (u32 numberOfPages, const IO_INTERFACE* discInterface);
void _FAT_cache_destructor (CACHE* cache);

View File

@ -59,7 +59,8 @@
* Rewrote device detection functions
* First libfat release
2006-07-25 - Chishm
* Changed IO_INTERFACEs to const
*/
#include "disc.h"
@ -81,7 +82,7 @@
#include "io_efa2.h"
#include "io_mmcf.h"
IO_INTERFACE* ioInterfaces[] = {
const IO_INTERFACE* ioInterfaces[] = {
#ifdef NDS
// Place Slot 1 (DS Card) interfaces here
&_io_nmmc,
@ -98,7 +99,7 @@ IO_INTERFACE* ioInterfaces[] = {
*/
IO_INTERFACE* _FAT_disc_gbaSlotFindInterface (void)
const IO_INTERFACE* _FAT_disc_gbaSlotFindInterface (void)
{
// If running on an NDS, make sure the correct CPU can access
// the GBA cart. First implemented by SaTa.
@ -128,7 +129,7 @@ IO_INTERFACE* _FAT_disc_gbaSlotFindInterface (void)
* and it returns true. Otherwise the default interface is left
* untouched and it returns false.
*/
IO_INTERFACE* _FAT_disc_dsSlotFindInterface (void)
const IO_INTERFACE* _FAT_disc_dsSlotFindInterface (void)
{
#ifdef ARM9
WAIT_CR &= ~ARM9_OWNS_CARD;
@ -157,7 +158,7 @@ IO_INTERFACE* _FAT_disc_dsSlotFindInterface (void)
* untouched and it returns false.
*/
#ifdef NDS
IO_INTERFACE* _FAT_disc_findInterface (void)
const IO_INTERFACE* _FAT_disc_findInterface (void)
{
#ifdef ARM9
WAIT_CR &= ~(ARM9_OWNS_CARD | ARM9_OWNS_ROM);
@ -177,7 +178,7 @@ IO_INTERFACE* _FAT_disc_findInterface (void)
return NULL;
}
#else
IO_INTERFACE* _FAT_disc_findInterface (void)
const IO_INTERFACE* _FAT_disc_findInterface (void)
{
return _FAT_disc_gbaSlotFindInterface();
}

View File

@ -41,7 +41,7 @@ Search for a block based device in the GBA slot.
Return a pointer to a usable interface if one is found,
NULL if not.
*/
extern IO_INTERFACE* _FAT_disc_gbaSlotFindInterface (void);
extern const IO_INTERFACE* _FAT_disc_gbaSlotFindInterface (void);
/*
Search for a block based device in the DS slot.
@ -49,7 +49,7 @@ Return a pointer to a usable interface if one is found,
NULL if not.
*/
#ifdef NDS
extern IO_INTERFACE* _FAT_disc_dsSlotFindInterface (void);
extern const IO_INTERFACE* _FAT_disc_dsSlotFindInterface (void);
#endif
/*
@ -57,13 +57,13 @@ Search for a block based device in the both slots.
Return a pointer to a usable interface if one is found,
NULL if not.
*/
extern IO_INTERFACE* _FAT_disc_findInterface (void);
extern const IO_INTERFACE* _FAT_disc_findInterface (void);
/*
Check if a disc is inserted
Return true if a disc is inserted and ready, false otherwise
*/
static inline bool _FAT_disc_isInserted (IO_INTERFACE* disc) {
static inline bool _FAT_disc_isInserted (const IO_INTERFACE* disc) {
return disc->fn_isInserted();
}
@ -73,7 +73,7 @@ numSectors is between 1 and 256
sector is from 0 to 2^28
buffer is a pointer to the memory to fill
*/
static inline bool _FAT_disc_readSectors (IO_INTERFACE* disc, u32 sector, u32 numSectors, void* buffer) {
static inline bool _FAT_disc_readSectors (const IO_INTERFACE* disc, u32 sector, u32 numSectors, void* buffer) {
return disc->fn_readSectors (sector, numSectors, buffer);
}
@ -83,21 +83,21 @@ numSectors is between 1 and 256
sector is from 0 to 2^28
buffer is a pointer to the memory to read from
*/
static inline bool _FAT_disc_writeSectors (IO_INTERFACE* disc, u32 sector, u32 numSectors, const void* buffer) {
static inline bool _FAT_disc_writeSectors (const IO_INTERFACE* disc, u32 sector, u32 numSectors, const void* buffer) {
return disc->fn_writeSectors (sector, numSectors, buffer);
}
/*
Reset the card back to a ready state
*/
static inline bool _FAT_disc_clearStatus (IO_INTERFACE* disc) {
static inline bool _FAT_disc_clearStatus (const IO_INTERFACE* disc) {
return disc->fn_clearStatus();
}
/*
Initialise the disc to a state ready for data reading or writing
*/
static inline bool _FAT_disc_startup (IO_INTERFACE* disc) {
static inline bool _FAT_disc_startup (const IO_INTERFACE* disc) {
return disc->fn_startup();
}
@ -105,21 +105,21 @@ static inline bool _FAT_disc_startup (IO_INTERFACE* disc) {
Put the disc in a state ready for power down.
Complete any pending writes and disable the disc if necessary
*/
static inline bool _FAT_disc_shutdown (IO_INTERFACE* disc) {
static inline bool _FAT_disc_shutdown (const IO_INTERFACE* disc) {
return disc->fn_shutdown();
}
/*
Return a 32 bit value unique to each type of interface
*/
static inline u32 _FAT_disc_hostType (IO_INTERFACE* disc) {
static inline u32 _FAT_disc_hostType (const IO_INTERFACE* disc) {
return disc->ioType;
}
/*
Return a 32 bit value that specifies the capabilities of the disc
*/
static inline u32 _FAT_disc_features (IO_INTERFACE* disc) {
static inline u32 _FAT_disc_features (const IO_INTERFACE* disc) {
return disc->features;
}

View File

@ -295,7 +295,7 @@ bool _EFA2_startUp(void)
/*-----------------------------------------------------------------
the actual interface structure
-----------------------------------------------------------------*/
IO_INTERFACE _io_efa2 = {
const IO_INTERFACE _io_efa2 = {
DEVICE_TYPE_EFA2,
FEATURE_MEDIUM_CANREAD | FEATURE_SLOT_GBA,
(FN_MEDIUM_STARTUP)&_EFA2_startUp,

View File

@ -18,6 +18,6 @@
#include "disc_io.h"
// export interface
extern IO_INTERFACE _io_efa2;
extern const IO_INTERFACE _io_efa2;
#endif // define IO_EFA2_H

View File

@ -322,7 +322,7 @@ bool _FCSR_startUp(void)
/*-----------------------------------------------------------------
the actual interface structure
-----------------------------------------------------------------*/
IO_INTERFACE _io_fcsr = {
const IO_INTERFACE _io_fcsr = {
DEVICE_TYPE_FCSR, // 'FCSR'
FEATURE_MEDIUM_CANREAD | FEATURE_MEDIUM_CANWRITE | FEATURE_SLOT_GBA,
(FN_MEDIUM_STARTUP)&_FCSR_startUp,

View File

@ -39,6 +39,6 @@
#include "disc_io.h"
// export interface
extern IO_INTERFACE _io_fcsr ;
extern const IO_INTERFACE _io_fcsr ;
#endif // define IO_FCSR_H

View File

@ -84,7 +84,7 @@ bool _M3CF_startup(void) {
}
IO_INTERFACE _io_m3cf = {
const IO_INTERFACE _io_m3cf = {
DEVICE_TYPE_M3CF,
FEATURE_MEDIUM_CANREAD | FEATURE_MEDIUM_CANWRITE | FEATURE_SLOT_GBA,
(FN_MEDIUM_STARTUP)&_M3CF_startup,

View File

@ -40,6 +40,6 @@
#include "disc_io.h"
// export interface
extern IO_INTERFACE _io_m3cf ;
extern const IO_INTERFACE _io_m3cf ;
#endif // define IO_M3CF_H

View File

@ -29,6 +29,12 @@
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.
* Fixed writeData function to timeout on error
* writeSectors function now wait until the card is ready before continuing with a transfer
*/
#include "io_m3sd.h"
@ -47,10 +53,11 @@
//---------------------------------------------------------------
// Send / receive timeouts, to stop infinite wait loops
#define MAX_STARTUP_TRIES 10 // Arbitrary value, check if the card is ready 10 times before giving up
#define MAX_STARTUP_TRIES 20 // Arbitrary value, check if the card is ready 20 times before giving up
#define NUM_STARTUP_CLOCKS 100 // Number of empty (0xFF when sending) bytes to send/receive to/from the card
#define TRANSMIT_TIMEOUT 2000 // Time to wait for the M3 to respond to transmit or receive requests
#define RESPONSE_TIMEOUT 256 // Number of clocks sent to the SD card before giving up
#define WRITE_TIMEOUT 300 // Time to wait for the card to finish writing
//---------------------------------------------------------------
// Variables required for tracking SD state
@ -245,7 +252,9 @@ static bool _M3SD_initCard (void) {
for (i = 0; i < MAX_STARTUP_TRIES ; i++) {
_M3SD_sendCommand (APP_CMD, 0);
_M3SD_getResponse_R1 (responseBuffer);
if (!_M3SD_getResponse_R1 (responseBuffer)) {
return false;
}
_M3SD_sendCommand (SD_APP_OP_COND, 3<<16);
if ((_M3SD_getResponse_R3 (responseBuffer)) && ((responseBuffer[1] & 0x80) != 0)) {
@ -372,9 +381,9 @@ static bool _M3SD_writeData (u8* data, u8* crc) {
int i;
u8 temp;
do {
_M3SD_clkin();
while ((REG_M3SD_DAT & 0x100) == 0);
} while ((REG_M3SD_DAT & 0x100) == 0);
REG_M3SD_DAT = 0; // Start bit
@ -481,8 +490,12 @@ bool _M3SD_writeSectors (u32 sector, u32 numSectors, const void* buffer) {
u8 responseBuffer[6];
u32 offset = sector * BYTES_PER_READ;
u8* data = (u8*) 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;
@ -491,12 +504,29 @@ bool _M3SD_writeSectors (u32 sector, u32 numSectors, const void* buffer) {
REG_M3SD_DIR = 0x4;
REG_M3SD_STS = 0x0;
_SD_CRC16 ( data, BYTES_PER_READ, crc);
// 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;
do {
_M3SD_sendCommand (SEND_STATUS, _M3SD_relativeCardAddress);
_M3SD_getResponse_R1 (responseBuffer);
i--;
if (i <= 0) {
return false;
}
} while (((responseBuffer[3] & 0x1f) != ((SD_STATE_TRAN << 1) | READY_FOR_DATA)));
}
return true;
@ -512,7 +542,7 @@ bool _M3SD_shutdown (void) {
return true;
}
IO_INTERFACE _io_m3sd = {
const IO_INTERFACE _io_m3sd = {
DEVICE_TYPE_M3SD,
FEATURE_MEDIUM_CANREAD | FEATURE_MEDIUM_CANWRITE | FEATURE_SLOT_GBA,
(FN_MEDIUM_STARTUP)&_M3SD_startUp,

View File

@ -43,6 +43,6 @@
#include "disc_io.h"
// export interface
extern IO_INTERFACE _io_m3sd ;
extern const IO_INTERFACE _io_m3sd ;
#endif // define IO_M3SD_H

View File

@ -89,7 +89,7 @@ bool _MMCF_startup(void) {
/*-----------------------------------------------------------------
the actual interface structure
-----------------------------------------------------------------*/
IO_INTERFACE _io_mmcf = {
const IO_INTERFACE _io_mmcf = {
DEVICE_TYPE_MMCF,
FEATURE_MEDIUM_CANREAD | FEATURE_MEDIUM_CANWRITE | FEATURE_SLOT_GBA,
(FN_MEDIUM_STARTUP)&_MMCF_startup,

View File

@ -40,6 +40,6 @@
#include "disc_io.h"
// export interface
extern IO_INTERFACE _io_mmcf ;
extern const IO_INTERFACE _io_mmcf ;
#endif // define IO_MMCF_H

View File

@ -88,7 +88,7 @@ bool _MPCF_startup(void) {
/*-----------------------------------------------------------------
the actual interface structure
-----------------------------------------------------------------*/
IO_INTERFACE _io_mpcf = {
const IO_INTERFACE _io_mpcf = {
DEVICE_TYPE_MPCF,
FEATURE_MEDIUM_CANREAD | FEATURE_MEDIUM_CANWRITE | FEATURE_SLOT_GBA,
(FN_MEDIUM_STARTUP)&_MPCF_startup,

View File

@ -40,6 +40,6 @@
#include "disc_io.h"
// export interface
extern IO_INTERFACE _io_mpcf ;
extern const IO_INTERFACE _io_mpcf ;
#endif // define IO_MPCF_H

View File

@ -334,7 +334,7 @@ bool _NMMC_readSectors (u32 sector, u32 totalSecs, void* buffer)
}
IO_INTERFACE _io_nmmc = {
const IO_INTERFACE _io_nmmc = {
DEVICE_TYPE_NMMC,
FEATURE_MEDIUM_CANREAD | FEATURE_MEDIUM_CANWRITE | FEATURE_SLOT_NDS,
(FN_MEDIUM_STARTUP)&_NMMC_startUp,

View File

@ -46,7 +46,7 @@
// export interface
extern IO_INTERFACE _io_nmmc;
extern const IO_INTERFACE _io_nmmc;
#endif // defined NDS

View File

@ -71,7 +71,7 @@ bool _SCCF_startup(void) {
}
IO_INTERFACE _io_sccf = {
const IO_INTERFACE _io_sccf = {
DEVICE_TYPE_SCCF,
FEATURE_MEDIUM_CANREAD | FEATURE_MEDIUM_CANWRITE | FEATURE_SLOT_GBA,
(FN_MEDIUM_STARTUP)&_SCCF_startup,

View File

@ -40,6 +40,6 @@
#include "disc_io.h"
// export interface
extern IO_INTERFACE _io_sccf;
extern const IO_INTERFACE _io_sccf;
#endif // define IO_SCCF_H

View File

@ -32,6 +32,10 @@
2006-07-22 - Chishm
* First release of stable code
2006-07-25 - Chishm
* Improved startup function that doesn't delay hundreds of seconds
before reporting no card inserted.
*/
#include "io_scsd.h"
@ -59,12 +63,12 @@
//---------------------------------------------------------------
// Send / receive timeouts, to stop infinite wait loops
#define MAX_STARTUP_TRIES 100 // Arbitrary value, check if the card is ready 100 times before giving up
#define MAX_STARTUP_TRIES 20 // Arbitrary value, check if the card is ready 20 times before giving up
#define NUM_STARTUP_CLOCKS 100 // Number of empty (0xFF when sending) bytes to send/receive to/from the card
#define TRANSMIT_TIMEOUT 10000 // Time to wait for the SC to respond to transmit or receive requests
#define RESPONSE_TIMEOUT 256 // Number of clocks sent to the SD card before giving up
#define BUSY_WAIT_TIMEOUT 500000
#define WRITE_TIMEOUT 10000 // Time to wait for the card to finish writing
#define WRITE_TIMEOUT 300 // Time to wait for the card to finish writing
//---------------------------------------------------------------
// Variables required for tracking SD state
static u32 _SCSD_relativeCardAddress = 0; // Preshifted Relative Card Address
@ -200,7 +204,9 @@ static bool _SCSD_initCard (void) {
for (i = 0; i < MAX_STARTUP_TRIES ; i++) {
_SCSD_sendCommand (APP_CMD, 0);
_SCSD_getResponse_R1 (responseBuffer);
if (!_SCSD_getResponse_R1 (responseBuffer)) {
return false;
}
_SCSD_sendCommand (SD_APP_OP_COND, 3<<16);
if ((_SCSD_getResponse_R3 (responseBuffer)) && ((responseBuffer[1] & 0x80) != 0)) {
@ -406,7 +412,7 @@ bool _SCSD_shutdown (void) {
return true;
}
IO_INTERFACE _io_scsd = {
const IO_INTERFACE _io_scsd = {
DEVICE_TYPE_SCSD,
FEATURE_MEDIUM_CANREAD | FEATURE_MEDIUM_CANWRITE | FEATURE_SLOT_GBA,
(FN_MEDIUM_STARTUP)&_SCSD_startUp,

View File

@ -43,6 +43,6 @@
#include "disc_io.h"
// export interface
extern IO_INTERFACE _io_scsd ;
extern const IO_INTERFACE _io_scsd ;
#endif // define IO_SCSD_H

View File

@ -105,7 +105,7 @@ bool fatMountNormalInterface (PARTITION_INTERFACE partitionNumber, u32 cacheSize
return _FAT_partition_mount (partitionNumber, cacheSize);
}
bool fatMountCustomInterface (IO_INTERFACE* device, u32 cacheSize) {
bool fatMountCustomInterface (const IO_INTERFACE* device, u32 cacheSize) {
return _FAT_partition_mountCustomInterface (device, cacheSize);
}

View File

@ -108,7 +108,7 @@ PARTITION* _partitions[MAXIMUM_PARTITIONS] = {NULL};
// Use a single static buffer for the partitions
static PARTITION* _FAT_partition_constructor ( IO_INTERFACE* disc, u32 cacheSize) {
static PARTITION* _FAT_partition_constructor ( const IO_INTERFACE* disc, u32 cacheSize) {
PARTITION* partition;
int i;
u32 bootSector;
@ -231,7 +231,7 @@ static void _FAT_partition_destructor (PARTITION* partition) {
bool _FAT_partition_mount (PARTITION_INTERFACE partitionNumber, u32 cacheSize) {
#ifdef NDS
int i;
IO_INTERFACE* disc = NULL;
const IO_INTERFACE* disc = NULL;
if (_partitions[partitionNumber] != NULL) {
return false;
@ -273,7 +273,7 @@ bool _FAT_partition_mount (PARTITION_INTERFACE partitionNumber, u32 cacheSize) {
}
#else // not defined NDS
IO_INTERFACE* disc = NULL;
const IO_INTERFACE* disc = NULL;
if (_partitions[partitionNumber] != NULL) {
return false;
@ -288,7 +288,7 @@ bool _FAT_partition_mount (PARTITION_INTERFACE partitionNumber, u32 cacheSize) {
return true;
}
bool _FAT_partition_mountCustomInterface (IO_INTERFACE* device, u32 cacheSize) {
bool _FAT_partition_mountCustomInterface (const IO_INTERFACE* device, u32 cacheSize) {
#ifdef NDS
int i;

View File

@ -58,7 +58,7 @@ typedef struct {
} FAT;
typedef struct {
IO_INTERFACE* disc;
const IO_INTERFACE* disc;
CACHE* cache;
// Info about the partition
bool readOnly; // If this is set, then do not try writing to the disc
@ -87,7 +87,7 @@ bool _FAT_partition_mount (PARTITION_INTERFACE partitionNumber, u32 cacheSize);
/*
Mount a partition on a custom device
*/
bool _FAT_partition_mountCustomInterface (IO_INTERFACE* device, u32 cacheSize);
bool _FAT_partition_mountCustomInterface (const IO_INTERFACE* device, u32 cacheSize);
/*
Unmount the partition specified by partitionNumber