Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

sd card access with mbed2148

Status
Not open for further replies.

Ajay_sho

Junior Member level 1
Joined
Oct 19, 2010
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,430
hi all
i m working with mbed with2148,
i have written code

while(1)
{
sdread(int add,int *buffer,int len);
uart0write(char*buffer, int len);
}

but it giving error
sdread function implicite which iis defined in header file as
esint sdread(euint32 address, euint8 *buffer,euint16 len);

can any one help what is meaning ofesint and euint, why it giving implicite error.
rgds
ajay singh
 

The following example demonstrates MMC library test. Upon flashing, insert a MMC/SD card into the module, when you should receive the "Init-OK" message. Then, you can experiment with MMC read and write functions, and observe the results through the Usart Terminal.
==============================================================================================
// MMC module connections
sbit Mmc_Chip_Select at LATC0_bit; // for writing to output pin always use latch (PIC18 family)
sbit Mmc_Chip_Select_Direction at TRISC0_bit;
// eof MMC module connections

const LINE_LEN = 43;
char err_txt[20] = "FAT16 not found";
char file_contents[LINE_LEN] = "XX MMC/SD FAT16 library by Anton Rieckertn";
char filename[14] = "MIKRO00x.TXT"; // File names
unsigned short loop, loop2;
unsigned long i, size;
char Buffer[512];

// UART1 write text and new line (carriage return + line feed)
void UART1_Write_Line(char *uart_text) {
UART1_Write_Text(uart_text);
UART1_Write(13);
UART1_Write(10);
}

// Creates new file and writes some data to it
void M_Create_New_File() {
filename[7] = 'A';
Mmc_Fat_Set_File_Date(2010, 4, 19, 9, 0, 0); // Set file date & time info
Mmc_Fat_Assign(&filename, 0xA0); // Find existing file or create a new one
Mmc_Fat_Rewrite(); // To clear file and start with new data
for(loop = 1; loop <= 99; loop++) {
UART1_Write('.');
file_contents[0] = loop / 10 + 48;
file_contents[1] = loop % 10 + 48;
Mmc_Fat_Write(file_contents, LINE_LEN-1); // write data to the assigned file
}
}

// Creates many new files and writes data to them
void M_Create_Multiple_Files() {
for(loop2 = 'B'; loop2 <= 'Z'; loop2++) {
UART1_Write(loop2); // signal the progress
filename[7] = loop2; // set filename
Mmc_Fat_Set_File_Date(2010, 4, 19, 9, 0, 0); // Set file date & time info
Mmc_Fat_Assign(&filename, 0xA0); // find existing file or create a new one
Mmc_Fat_Rewrite(); // To clear file and start with new data
for(loop = 1; loop <= 44; loop++) {
file_contents[0] = loop / 10 + 48;
file_contents[1] = loop % 10 + 48;
Mmc_Fat_Write(file_contents, LINE_LEN-1); // write data to the assigned file
}
}
}

// Opens an existing file and rewrites it
void M_Open_File_Rewrite() {
filename[7] = 'C';
Mmc_Fat_Assign(&filename, 0);
Mmc_Fat_Rewrite();
for(loop = 1; loop <= 55; loop++) {
file_contents[0] = loop / 10 + 48;
file_contents[1] = loop % 10 + 48;
Mmc_Fat_Write(file_contents, LINE_LEN-1); // write data to the assigned file
}
}

// Opens an existing file and appends data to it
// (and alters the date/time stamp)
void M_Open_File_Append() {
filename[7] = 'B';
Mmc_Fat_Assign(&filename, 0);
Mmc_Fat_Set_File_Date(2010, 4, 19, 9, 20, 0);
Mmc_Fat_Append(); // Prepare file for append
Mmc_Fat_Write(" for mikroElektronika 2010n", 27); // Write data to assigned file
}

// Opens an existing file, reads data from it and puts it to UART
void M_Open_File_Read() {
char character;

filename[7] = 'B';
Mmc_Fat_Assign(&filename, 0);
Mmc_Fat_Reset(&size); // To read file, procedure returns size of file
for (i = 1; i <= size; i++) {
Mmc_Fat_Read(&character);
UART1_Write(character); // Write data to UART
}
}

// Deletes a file. If file doesn't exist, it will first be created
// and then deleted.
void M_Delete_File() {
filename[7] = 'F';
Mmc_Fat_Assign(filename, 0);
Mmc_Fat_Delete();
}

// Tests whether file exists, and if so sends its creation date
// and file size via UART
void M_Test_File_Exist() {
unsigned long fsize;
unsigned int year;
unsigned short month, day, hour, minute;
unsigned char outstr[12];

filename[7] = 'B'; //uncomment this line to search for file that DOES exists
// filename[7] = 'F'; //uncomment this line to search for file that DOES NOT exist
if (Mmc_Fat_Assign(filename, 0)) {
//--- file has been found - get its create date
Mmc_Fat_Get_File_Date(&year, &month, &day, &hour, &minute);
UART1_Write_Text(" created: ");
WordToStr(year, outstr);
UART1_Write_Text(outstr);
ByteToStr(month, outstr);
UART1_Write_Text(outstr);
WordToStr(day, outstr);
UART1_Write_Text(outstr);
WordToStr(hour, outstr);
UART1_Write_Text(outstr);
WordToStr(minute, outstr);
UART1_Write_Text(outstr);

//--- file has been found - get its modified date
Mmc_Fat_Get_File_Date_Modified(&year, &month, &day, &hour, &minute);
UART1_Write_Text(" modified: ");
WordToStr(year, outstr);
UART1_Write_Text(outstr);
ByteToStr(month, outstr);
UART1_Write_Text(outstr);
WordToStr(day, outstr);
UART1_Write_Text(outstr);
WordToStr(hour, outstr);
UART1_Write_Text(outstr);
WordToStr(minute, outstr);
UART1_Write_Text(outstr);

//--- get file size
fsize = Mmc_Fat_Get_File_Size();
LongToStr((signed long)fsize, outstr);
UART1_Write_Line(outstr);
}
else {
//--- file was not found - signal it
UART1_Write(0x55);
Delay_ms(1000);
UART1_Write(0x55);
}
}


// Tries to create a swap file, whose size will be at least 100
// sectors (see Help for details)
void M_Create_Swap_File() {
unsigned int i;

for(i=0; i<512; i++)
Buffer = i;

size = Mmc_Fat_Get_Swap_File(5000, "mikroE.txt", 0x20); // see help on this function for details

if (size) {
LongToStr((signed long)size, err_txt);
UART1_Write_Line(err_txt);

for(i=0; i<5000; i++) {
Mmc_Write_Sector(size++, Buffer);
UART1_Write('.');
}
}
}

// Main. Uncomment the function(s) to test the desired operation(s)
void main() {
#define COMPLETE_EXAMPLE // comment this line to make simpler/smaller example
ADCON1 |= 0x0F; // Configure AN pins as digital
CMCON |= 7; // Turn off comparators

// Initialize UART1 module
UART1_Init(19200);
Delay_ms(10);

UART1_Write_Line("PIC-Started"); // PIC present report

// Initialize SPI1 module
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);

// use fat16 quick format instead of init routine if a formatting is needed
if (Mmc_Fat_Init() == 0) {
// reinitialize spi at higher speed
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
//--- Test start
UART1_Write_Line("Test Start.");
//--- Test routines. Uncomment them one-by-one to test certain features
M_Create_New_File();
#ifdef COMPLETE_EXAMPLE
M_Create_Multiple_Files();
M_Open_File_Rewrite();
M_Open_File_Append();
M_Open_File_Read();
M_Delete_File();
M_Test_File_Exist();
M_Create_Swap_File();
#endif
UART1_Write_Line("Test End.");

}
else {
UART1_Write_Line(err_txt); // Note: Mmc_Fat_Init tries to initialize a card more than once.
// If card is not present, initialization may last longer (depending on clock speed)
}

}
==============================================================================================
This code is written for MikroC compiler
 

thx for your reply
but i m doing interfacing lpc2148 with sd card of capacity 2 gb i have modify some code of dharmani, i m able to read and write data to sd in raw format but when i m using fat32 ,i m not able to access master boot record,pls see cod below


struct BS_Structure{
unsigned char jmpBoot[3]; //default: 0x009000EB
unsigned char OEMName[8];
unsigned int BytsPerSec; //deafault: 512
unsigned char sectorPerCluster;
unsigned int reservedSectorCount;
unsigned char numberofFATs;
unsigned int rootEntryCount;
unsigned int totalSectors_F16; //must be 0 for FAT32
unsigned char mediaType;
unsigned int FATsize_F16; //must be 0 for FAT32
unsigned int sectorsPerTrack;
unsigned int numberofHeads;
unsigned long hiddenSectors;
unsigned long totalSectors_F32;
unsigned long FATsize_F32; //count of sectors occupied by one FAT
unsigned int extFlags;
unsigned int FSversion; //0x0000 (defines version 0.0)
unsigned long rootCluster; //first cluster of root directory (=2)
unsigned int FSinfo; //sector number of FSinfo structure (=1)
unsigned int BackupBootSector;
unsigned char reserved[12];
unsigned char driveNumber;
unsigned char reserved1;
unsigned char bootSignature;
unsigned long volumeID;
unsigned char volumeLabel[11]; //"NO NAME "
unsigned char fileSystemType[8]; //"FAT32"
unsigned char bootData[420];
unsigned int bootEndSignature; //0xaa55
};
unsigned char getBootSectorData(void)
{
struct BS_Structure *BPB; //mapping the buffer onto the structure
struct MBRinfo_Structure *mbr;
struct partitionInfo_Structure *partition;
unsigned long dataSectors;
unusedSectors = 0;
sd_readSector(0,(char*)buffer);
//uart0Write((char*)buffer,sizeof(buffer));


BPB = (struct BS_Structure*)buffer;

if(BPB->jmpBoot[0]!=0x000000E9 && BPB->jmpBoot[0]!=0x000000EB) //check if it is boot sector
{
mbr = (struct MBRinfo_Structure*)buffer; //if it is not boot sector, it must be MBR

if(mbr->signature != 0x0000aa55) return 1; //if it is not even MBR then it's not FAT32

partition = (struct partitionInfo_Structure*)(mbr->partitionData);//first partition
unusedSectors = partition->firstSector; //the unused sectors, hidden to the FAT

sd_readSector(partition->firstSector,(char*)buffer);//read the bpb sector
BPB = (struct BS_Structure*)buffer;
if(BPB->jmpBoot[0]!=0x000000E9 && BPB->jmpBoot[0]!=0x000000EB) return 1;
}
*/
bytesPerSector = BPB->BytsPerSec;
sectorPerCluster = BPB->sectorPerCluster;
reservedSectorCount = BPB->reservedSectorCount;
rootCluster = BPB->rootCluster;// + (sector / sectorPerCluster) +1;
firstDataSector = BPB->hiddenSectors + reservedSectorCount + (BPB->numberofFATs * BPB->FATsize_F32);

dataSectors = BPB->totalSectors_F32 - BPB->reservedSectorCount - ( BPB->numberofFATs * BPB->FATsize_F32);
totalClusters = dataSectors/sectorPerCluster;
//add=500; //for check only
display_int(bytesPerSector);
/*display_int(bpb->sectorPerCluster);
displayMemory (bpb->reservedSectorCount);
displayMemory (dataSectors);
displayMemory (firstDataSector);
displayMemory (totalClusters);
displayMemory (bpb->totalSectors_F32);
displayMemory(bpb->reservedSectorCount);
display_int (bpb->numberofFATs);
displayMemory(bpb->FATsize_F32);
//displayMemory (unsigned long memory)
*/
//uart0Write((char*)buffer,sizeof(buffer));
if((getSetFreeCluster (TOTAL_FREE, GET, 0)) > totalClusters) //check if FSinfo free clusters count is valid
freeClusterCountUpdated = 0;
else
freeClusterCountUpdated = 1;

return 0;
}
int main (void)
{ U8 err,FAT32_active;
U8 ch=0;
test_led();
hardware_init();
test_sd_card();
// test_i2c_at24c256_flash(); // Test I2C EEPROM by wrinting some data at some address and reading the same from same address

// DAC_test();
lcd_clear();

while(1)
{
getBootSectorData();
//if(err)
//tr_string("fat32 not found");
//FAT32_active=0;
// memoryStatistics();
//findFiles(GET_LIST,0);
}
}
}

i m getting bytes per sector 2,492,418 which is not correct ,can any one help me on fat32.
rgds
ajay
 

tahir, The power supply in your schematic may not be inappropriate. Only the LF version of that chip can operate at 3.3V. And the voltage dividers on the bus lines are going to drop the voltage below the operating "spec" voltage of the SD/MMC. That should be a 5.0V regulator.
 

i hav already told that i have access sd card in raw format, reading and writing data from sd card in raw format is o.k. , i want only when i format card in fat32, and after reading first secor ,data which has come from BPB structure is not correct, b'coz byteper sector is coming 2,492,418 bytes which ois not correct for ant fat system.
rgds
ajay
 

i hav already told that i have access sd card in raw format, reading and writing data from sd card in raw format is o.k. , i want only when i format card in fat32, and after reading first secor ,data which has come from BPB structure is not correct, b'coz byteper sector is coming 2,492,418 bytes which ois not correct for ant fat system.
rgds
ajay
Dear all
Sorry for the last reply
Rgds
Ajay
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top