FatFS working #9

some main functions converted to use it
mp3 converted to use it
This commit is contained in:
Robin Jones 2017-10-14 19:59:49 +01:00
parent 8264d647ba
commit 5442139ff4
5 changed files with 248 additions and 166 deletions

View File

@ -68,7 +68,7 @@
/ Locale and Namespace Configurations / Locale and Namespace Configurations
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#define FF_CODE_PAGE 932 #define FF_CODE_PAGE 437
/* This option specifies the OEM code page to be used on the target system. /* This option specifies the OEM code page to be used on the target system.
/ Incorrect code page setting can cause a file open failure. / Incorrect code page setting can cause a file open failure.
/ /
@ -97,7 +97,7 @@
*/ */
#define FF_USE_LFN 0 #define FF_USE_LFN 1
#define FF_MAX_LFN 255 #define FF_MAX_LFN 255
/* The FF_USE_LFN switches the support for LFN (long file name). /* The FF_USE_LFN switches the support for LFN (long file name).
/ /
@ -210,7 +210,7 @@
/ buffer in the filesystem object (FATFS) is used for the file data transfer. */ / buffer in the filesystem object (FATFS) is used for the file data transfer. */
#define FF_FS_EXFAT 0 #define FF_FS_EXFAT 1
/* This option switches support for exFAT filesystem. (0:Disable or 1:Enable) /* This option switches support for exFAT filesystem. (0:Disable or 1:Enable)
/ When enable exFAT, also LFN needs to be enabled. / When enable exFAT, also LFN needs to be enabled.
/ Note that enabling exFAT discards ANSI C (C89) compatibility. */ / Note that enabling exFAT discards ANSI C (C89) compatibility. */

View File

@ -253,33 +253,48 @@ DRESULT disk_ioctl (
) )
{ {
DRESULT res; DRESULT res;
int result; // int result;
switch (pdrv) { // switch (pdrv) {
case DEV_RAM : // case DEV_RAM :
// Process of the command for the RAM drive // // Process of the command for the RAM drive
return res; // return res;
case DEV_MMC : // case DEV_MMC :
// Process of the command for the MMC/SD card // // Process of the command for the MMC/SD card
return res; // return res;
case DEV_USB : // case DEV_USB :
// Process of the command the USB drive // // Process of the command the USB drive
return res; // return res;
} // }
switch (cmd) {
case CTRL_SYNC:
return RES_OK;
case GET_SECTOR_SIZE:
*(WORD*)buff = 512;
return RES_OK;
case GET_SECTOR_COUNT:
//*(DWORD*)buff = sdGetSectors();
return RES_OK;
case GET_BLOCK_SIZE:
//*(DWORD*)buff = sdGetBlockSize();
return RES_OK;
}
return RES_PARERR; return RES_PARERR;
} }
DWORD get_fattime (void) DWORD get_fattime (void)
{ {
//TODO: can we use the V3 RTC?
return 0; return 0;
} }

View File

@ -21,8 +21,8 @@
//filesystem //filesystem
#include "sd.h" #include "sd.h"
#include "ff.h"
#include "fat_old.h" #include "fat_old.h"
//utils //utils
#include "utils.h" #include "utils.h"
@ -144,6 +144,8 @@ u8 empty = 0;
u8 playing = 0; u8 playing = 0;
u8 gb_load_y = 0; u8 gb_load_y = 0;
FATFS *fs;
//start with filebrowser menu key settings //start with filebrowser menu key settings
enum InputMap enum InputMap
{ {
@ -899,155 +901,175 @@ void clearScreen(display_context_t disp)
void romInfoScreen(display_context_t disp, u8 *buff, int silent) void romInfoScreen(display_context_t disp, u8 *buff, int silent)
{ {
if (silent != 1)
sleep(10);
u8 tmp[32];
u8 filename[64]; u8 filename[64];
u8 ok = 0;
sprintf(filename, "%s", buff); sprintf(filename, "%s", buff);
int swapped = 0; int swapped = 0;
FatRecord rec_tmpf; FRESULT result;
//not needed any longer :>
//file IS there, it's selected at this point
ok = fatFindRecord(filename, &rec_tmpf, 0);
u8 resp = 0;
resp = fatOpenFileByName(filename, 0); //err if not found ^^
int mb = file.sec_available / 2048;
int block_offset = 0;
u32 cart_buff_offset = 0;
u32 begin_sector = file.sector;
//filesize -> readfile / 512
int fsize = 512; //rom-headersize 4096 but the bootcode is not needed int fsize = 512; //rom-headersize 4096 but the bootcode is not needed
unsigned char headerdata[fsize]; //1*512 unsigned char headerdata[fsize]; //1*512
resp = fatReadFile(&headerdata, fsize / 512); //1 cluster FIL file;
UINT bytesread;
result = f_open(&file, filename, FA_READ);
int sw_type = is_valid_rom(headerdata); if (result == FR_OK)
if (sw_type != 0)
{ {
swapped = 1; int fsizeMB = f_size(&file) / 1048576; //Bytes in a MB
swap_header(headerdata, 512);
}
//char 32-51 name result =
unsigned char rom_name[32]; f_read (
&file, /* [IN] File object */
&headerdata, /* [OUT] Buffer to store read data */
fsize, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
for (int u = 0; u < 19; u++) result = f_close(&file);
{
if (u != 0)
sprintf(rom_name, "%s%c", rom_name, headerdata[32 + u]);
else
sprintf(rom_name, "%c", headerdata[32 + u]);
}
//trim right spaces
//romname=trimmed rom name for filename
sprintf(rom_name, "%s", trim(rom_name));
if (silent != 1) int sw_type = is_valid_rom(headerdata);
printText(rom_name, 11, 19, disp);
sprintf(rom_name, "Size: %iMb", mb); if (sw_type != 0)
if (silent != 1)
printText(rom_name, 11, -1, disp);
//unique cart id for gametype
unsigned char cartID_str[12];
sprintf(cartID_str, "ID: %c%c%c%c", headerdata[0x3B], headerdata[0x3C], headerdata[0x3D], headerdata[0x3E]);
if (silent != 1)
printText(cartID_str, 11, -1, disp);
int cic, save;
cic = get_cic(&headerdata[0x40]);
unsigned char cartID_short[4];
sprintf(cartID_short, "%c%c\0", headerdata[0x3C], headerdata[0x3D]);
if (get_cic_save(cartID_short, &cic, &save))
{
if (silent != 1)
printText("found in db", 11, -1, disp);
unsigned char save_type_str[12];
sprintf(save_type_str, "Save: %s", saveTypeToExtension(save, ext_type));
if (silent != 1)
printText(save_type_str, 11, -1, disp);
unsigned char cic_type_str[12];
sprintf(cic_type_str, "CIC: CIC-610%i", cic); //TODO: need to take into account DD and Aleck CIC
if (silent != 1)
printText(cic_type_str, 11, -1, disp);
//thanks for the db :>
//cart was found, use CIC and SaveRAM type
}
if (silent != 1)
{
char box_path[32];
sprite_t *n64cover;
sprintf(box_path, "/ED64/boxart/lowres/%c%c.png", headerdata[0x3C], headerdata[0x3D]);
if (fatFindRecord(box_path, &rec_tmpf, 0) != 0)
{ {
//not found swapped = 1;
sprintf(box_path, "/ED64/boxart/lowres/00.png"); swap_header(headerdata, 512);
} }
n64cover = loadPng(box_path); if (silent != 1)
graphics_draw_sprite(disp, 81, 32, n64cover); {
display_show(disp); //char 32-51 name
} unsigned char rom_name[32];
else
{ for (int u = 0; u < 19; u++)
rom_config[1] = cic - 1; {
rom_config[2] = save; if (u != 0)
rom_config[3] = 0; //tv force off sprintf(rom_name, "%s%c", rom_name, headerdata[32 + u]);
rom_config[4] = 0; //cheat off else
rom_config[5] = 0; //chk_sum off sprintf(rom_name, "%c", headerdata[32 + u]);
rom_config[6] = 0; //rating }
rom_config[7] = 0; //country
rom_config[8] = 0; //reserved //rom name
rom_config[9] = 0; //reserved sprintf(rom_name, "%s", trim(rom_name));
printText(rom_name, 11, 19, disp);
//rom size
sprintf(rom_name, "Size: %iMB", fsizeMB);
printText(rom_name, 11, -1, disp);
//unique cart id for gametype
unsigned char cartID_str[12];
sprintf(cartID_str, "ID: %c%c%c%c", headerdata[0x3B], headerdata[0x3C], headerdata[0x3D], headerdata[0x3E]);
printText(cartID_str, 11, -1, disp);
}
int cic, save;
cic = get_cic(&headerdata[0x40]);
unsigned char cartID_short[4];
sprintf(cartID_short, "%c%c\0", headerdata[0x3C], headerdata[0x3D]);
if (get_cic_save(cartID_short, &cic, &save))
{
if (silent != 1)
{
printText("found in db", 11, -1, disp);
unsigned char save_type_str[12];
sprintf(save_type_str, "Save: %s", saveTypeToExtension(save, ext_type));
printText(save_type_str, 11, -1, disp);
unsigned char cic_type_str[12];
switch (cic)
{
case 4:
sprintf(cic_type_str, "CIC: CIC-5101", cic);
break;
case 7:
sprintf(cic_type_str, "CIC: CIC-5167", cic);
break;
default:
sprintf(cic_type_str, "CIC: CIC-610%i", cic);
break;
}
printText(cic_type_str, 11, -1, disp);
}
//thanks for the db :>
//cart was found, use CIC and SaveRAM type
}
if (silent != 1)
{
char box_path[32];
sprite_t *n64cover;
sprintf(box_path, "/ED64/boxart/lowres/%c%c.png", headerdata[0x3C], headerdata[0x3D]);
FILINFO fnoba;
result = f_stat (box_path, &fnoba);
if (result != FR_OK)
{
//not found
sprintf(box_path, "/ED64/boxart/lowres/00.png");
}
n64cover = loadPng(box_path);
graphics_draw_sprite(disp, 81, 32, n64cover);
display_show(disp);
}
else
{
rom_config[1] = cic - 1;
rom_config[2] = save;
rom_config[3] = 0; //tv force off
rom_config[4] = 0; //cheat off
rom_config[5] = 0; //chk_sum off
rom_config[6] = 0; //rating
rom_config[7] = 0; //country
rom_config[8] = 0; //reserved
rom_config[9] = 0; //reserved
}
} }
} }
sprite_t *loadPng(u8 *png_filename) sprite_t *loadPng(u8 *png_filename)
{ {
u8 *filename; u8 *filename = (u8 *)malloc(slen(png_filename));
u8 ok = 0;
filename = (u8 *)malloc(slen(png_filename));
//config filename
sprintf(filename, "%s", png_filename); sprintf(filename, "%s", png_filename);
FatRecord rec_tmpf;
ok = fatFindRecord(filename, &rec_tmpf, 0);
u8 resp = 0; FRESULT result;
resp = fatOpenFileByName(filename, 0); FIL file;
UINT bytesread;
result = f_open(&file, filename, FA_READ);
//filesize of the opend file -> is the readfile / 512 if (result == FR_OK)
int fsize = file.sec_available * 512; {
int fsize = f_size(&file);
u8 png_rawdata[fsize];
u8 png_rawdata[fsize]; result =
f_read (
&file, /* [IN] File object */
&png_rawdata, /* [OUT] Buffer to store read data */
fsize, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
resp = fatReadFile(&png_rawdata, fsize / 512); //1 cluster result = f_close(&file);
free(filename);
return loadImage32(png_rawdata, fsize);
}
return 0;
return loadImage32(png_rawdata, fsize);
free(filename);
} }
void loadgbrom(display_context_t disp, u8 *buff) void loadgbrom(display_context_t disp, u8 *buff)
@ -1723,6 +1745,13 @@ void initFilesystem(void)
evd_ulockRegs(); evd_ulockRegs();
sleep(1000); sleep(1000);
fs = malloc(sizeof (FATFS)); /* Get work area for the volume */
FRESULT result = f_mount(fs,"",1);
if(result != FR_OK)
{
//printText("mount error", 11, -1, disp);
}
fatInitRam(); fatInitRam();
fatInit(); fatInit();
fat_initialized = 1; fat_initialized = 1;
@ -2158,6 +2187,9 @@ void bootRom(display_context_t disp, int silent)
graphics_fill_screen(disp, 0x000000FF); graphics_fill_screen(disp, 0x000000FF);
display_show(disp); display_show(disp);
f_mount(0, "", 0); /* Unmount the default drive */
free(fs); /* Here the work area can be discarded */
simulate_boot(boot_cic, bios_cic, cheat_lists); // boot_cic simulate_boot(boot_cic, bios_cic, cheat_lists); // boot_cic
} }
} }
@ -4294,6 +4326,8 @@ int main(void)
else else
{ {
printf("Filesystem failed to start!\n"); printf("Filesystem failed to start!\n");
f_mount(0, "", 0); /* Unmount the default drive */
free(fs); /* Here the work area can be discarded */
for ( ;; ) for ( ;; )
; //never leave! ; //never leave!
} }

View File

@ -7,11 +7,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <mad.h>
#include "fat_old.h"
#include "mp3.h"
#include <libdragon.h> #include <libdragon.h>
#include <mad.h>
#include "mp3.h"
#include "ff.h"
static struct mad_stream Stream; static struct mad_stream Stream;
static struct mad_header Header; static struct mad_header Header;
@ -68,13 +68,11 @@ static int mp3_seek(char* fd, int offset, int whence) {
} }
static int mp3_size(char* fd) { static int mp3_size(char* fd) {
FatRecord rec_tmpf; FRESULT result;
u8 resp=0; FILINFO fno;
resp = fatOpenFileByName(fd, 0); //err if not found ^^ //TODO: error
result = f_stat (fd, &fno);
int fsize = file.sec_available*512; //fsize in bytes mp3File_fsize = fno.fsize;
mp3File_fsize = fsize;
//todo filesize
return mp3File_fsize; return mp3File_fsize;
} }
@ -96,6 +94,37 @@ static void _f_read(char* fname, unsigned char *readBuffer, int size){
} }
//dma_write_s(buffer, 0xb0000000, fsize); //dma_write_s(buffer, 0xb0000000, fsize);
*/ */
mp3_size(fname);
FRESULT result;
FIL file;
UINT bytesread;
result = f_open(&file, fname, FA_READ);
if (result == FR_OK)
{
int fsize = f_size(&file);
if ( fsize > size)
{
//todo: set the read pointer
//readBuffer+mp3File_fptr
}
result =
f_read (
&file, /* [IN] File object */
&readBuffer, /* [OUT] Buffer to store read data */
size, /* [IN] Number of bytes to read */
&bytesread /* [OUT] Number of bytes read */
);
result = f_close(&file);
mp3File_fptr+=size;
//dma_write_s(buffer, 0xb0000000, fsize);
}
} }
static int mp3_read(char* fd, unsigned char *ptr, int size) static int mp3_read(char* fd, unsigned char *ptr, int size)

View File

@ -12,28 +12,32 @@
#include "errors.h" #include "errors.h"
#include "sys.h" #include "sys.h"
#define CMD0 0x40 // software reset #define CMD0 (0x40+0) // GO_IDLE_STATE, Software reset.
#define CMD1 0x41 // brings card out of idle state #define CMD1 (0x40+1) // SEND_OP_COND, Initiate initialization process.
#define CMD8 0x48 // Reserved #define CMD2 (0x40+2) //read cid
#define CMD12 0x4C // stop transmission on multiple block read #define CMD3 (0x40+3) //read rca
#define CMD17 0x51 // read single block #define CMD6 (0x40+6)
#define CMD18 0x52 // read multiple block #define CMD7 (0x40+7)
#define CMD58 0x7A // reads the OCR register #define CMD8 (0x40+8) // SEND_IF_COND, For only SDC V2. Check voltage range.
#define CMD55 0x77 #define CMD9 (0x40+9) // SEND_CSD, Read CSD register.
#define CMD41 0x69 #define CMD10 (0x40+10) // SEND_CID, Read CID register.
#define CMD24 0x58 // writes a single block #define CMD12 (0x40+12) // STOP_TRANSMISSION, Stop to read data.
#define CMD25 0x59 // writes a multi block #define ACMD13 (0xC0+13) // SD_STATUS (SDC)
#define ACMD41 0x69 #define CMD16 (0x40+16) // SET_BLOCKLEN, Change R/W block size.
#define ACMD6 0x46 #define CMD17 (0x40+17) // READ_SINGLE_BLOCK, Read a block.
#define CMD18 (0x40+18) // READ_MULTIPLE_BLOCK, Read multiple blocks.
#define ACMD23 (0xC0+23) // SET_WR_BLK_ERASE_COUNT (SDC)
#define CMD24 (0x40+24) // WRITE_BLOCK, Write a block.
#define CMD25 (0x40+25) // WRITE_MULTIPLE_BLOCK, Write multiple blocks.
#define CMD41 (0x40+41) // SEND_OP_COND (ACMD)
#define ACMD41 (0xC0+41) // SEND_OP_COND (SDC)
#define CMD55 (0x40+55) // APP_CMD, Leading command of ACMD<n> command.
#define CMD58 (0x40+58) // READ_OCR, Read OCR.
#define SD_V2 2 #define SD_V2 2
#define SD_HC 1 #define SD_HC 1
#define CMD2 0x42 //read cid
#define CMD3 0x43 //read rca
#define CMD7 0x47
#define CMD9 0x49
#define CMD6 0x46
#define R1 1 #define R1 1
#define R2 2 #define R2 2