Reading the capacity of SDHC ?

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Guys, I've been trying to read the capacity of sd card, but always fail, anything that I can work around ?
thanks for reading and helping,

Here's the function :

uint32 MMC_SD_ReadCapacity()
{
	uint8 r1;
	uint16 i;
	uint16 temp;
	uint8 buffer[16];
	uint32 Capacity;
	uint16 retry =0;
	//uint8 retry=0;

	r1 = MMC_SD_SendCommand(9, 0);//дÃüÁî	//send command  //READ CSD
	if(r1 != 0x00)
		return r1;

	SPI_CS_Assert();
	while(SPI_WriteByte(0xff) != 0xfe)if(retry++ > 0xfffe){SPI_CS_Deassert();return 1;}

	
	for(i=0;i<16;i++)
	{
		buffer[i]=SPI_WriteByte(0xff);
	}	

	SPI_WriteByte(0xff);
	SPI_WriteByte(0xff);
	
	SPI_WriteByte(0xff);
	
	SPI_CS_Deassert();

	SPI_WriteByte(0xff);// extra 8 CLK

/*********************************/
//	C_SIZE
	i = buffer[6]&0x03;
	i<<=8;
	i += buffer[7];
	i<<=2;
	i += ((buffer[8]&0xc0)>>6);

/**********************************/
//  C_SIZE_MULT

	r1 = buffer[9]&0x03;
	r1<<=1;
	r1 += ((buffer[10]&0x80)>>7);


/**********************************/
// BLOCKNR

	r1+=2;

	temp = 1;
	while(r1)
	{
		temp*=2;
		r1--;
	}
	
	Capacity = ((uint32)(i+1))*((uint32)temp);

/////////////////////////
// READ_BL_LEN

	i = buffer[5]&0x0f;

/*************************/
//BLOCK_LEN

	temp = 1;
	while(i)
	{
		temp*=2;
		i--;
	}
/************************/


/************** formula of the capacity ******************/
//
//  memory capacity = BLOCKNR * BLOCK_LEN
//	
//	BLOCKNR = (C_SIZE + 1)* MULT
//
//           C_SIZE_MULT+2
//	MULT = 2
//
//               READ_BL_LEN
//	BLOCK_LEN = 2
/**********************************************/

//The final result
	
	Capacity *= (uint32)temp;	 
	return Capacity;		
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Guess you are going to also use the card in the end to read/write data.

why not make it easy on yourself and use FatFs?
it has all the stuff already implemented.
you could also download that to see how it is done by the writer of that code, if you are on a learning trip....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't understand on using FatFS for AVR, try to learning it but ...still don't get it..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok,

what do you not understand?

you did check the tutorials on the use of FatFs in the tutorials section?
I know cliff recently has made an update on one of the latest examples from FatFs.

you could read that to see how you can use FatFs.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I don't understand on using FatFS for AVR, try to learning it but ...still don't get it..

But it's about 50 times simpler than trying to interact with an SD card direct?!?

There are only three (disk_*()) functions in FatFs that actually interact with the card. Once you have made those work the rest of the thing "just works".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

meslomp wrote:
Ok,

what do you not understand?

you did check the tutorials on the use of FatFs in the tutorials section?
I know cliff recently has made an update on one of the latest examples from FatFs.

you could read that to see how you can use FatFs.

I don't understand why on main.c,
sdmm.c is not included on it....
I need that file for my port...

on main.c

#include 	/* Device specific declarations */
#include "ff.h"		/* Declarations of FatFs API */

I got it from ffsample.zip and avr_foolproof directory...

Anyone has any experiences about it ?

thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

on sdmm.c

#include "diskio.h"		/* Common include file for FatFs and disk I/O layer */


/*-------------------------------------------------------------------------*/
/* Platform dependent macros and functions needed to be modified           */
/*-------------------------------------------------------------------------*/

#include 			/* Include device specific declareation file here */


#define DO_INIT()					/* Initialize port for MMC DO as input */
#define DO			(PINB &	0x01)	/* Test for MMC DO ('H':true, 'L':false) */

#define DI_INIT()	DDRB  |= 0x02	/* Initialize port for MMC DI as output */
#define DI_H()		PORTB |= 0x02	/* Set MMC DI "high" */
#define DI_L()		PORTB &= 0xFD	/* Set MMC DI "low" */