uSD API


Testing writing/reading uSD card  using serial API calls.

Toolkit:STM Development System

Location:/bipom/devtools/STM32/examples/usd_api

Code Example


DSCB	uSD;
UBYTE	uSD_Buffer[DATASTORAGEAPI_SECTOR_SIZE];

const char *typeName[8]= 
	{ 
		"STD CAPACITY SD CARD V1.1",
		"STD CAPACITY SD CARD V2.0",
		"HIGH CAPACITY SD CARD",
		"MULTIMEDIA CARD",
		"SECURE DIGITAL IO CARD",
		"HIGH SPEED MULTIMEDIA CARD",
		"SECURE DIGITAL IO COMBO CARD",
		"HIGH CAPACITY MMC CARD"
	};

//********************************************************************************

int main (void)
{
	delayMs(250);
		
	ERRCODE ec;
	tprintf("\n\rMini-Max/STM32F1");
	tprintf("\n\ruSD API REV 1.01");
	//

	uSD.dataBuffer 		= uSD_Buffer;
	uSD.iface			= DATASTORAGEAPI_SDIO_INTERFACE;
	ec = uSD_init(&uSD);
	if(ec)
	{
		tprintf("\r\nERROR: uSD INIT, ec=%d",ec);
		goto func_end;
	}
	else
	{
		tprintf("\r\nuSD init is OK");
  		tprintf("\n\rType                          :  ");
  		(7<SDCardInfo.CardType) ? 
  			tprintf("UNKNOWN CARD"): 
  				tprintf(typeName[SDCardInfo.CardType]);
  		//

  		if(SDIO_HIGH_CAPACITY_SD_CARD== SDCardInfo.CardType)
			tprintf("\n\rCapacity (in 512-byte blocks) :  %u",SDCardInfo.CardCapacity);
		else
			tprintf("\n\rCapacity (in bytes )          :  %u",SDCardInfo.CardCapacity);
		tprintf("\n\rBlock Size                    :  %u",SDCardInfo.CardBlockSize);
		tprintf("\r\nMax Sector                    :  %u", uSD.getMaxSector());
	}
	/* Run the test  */
	tprintf("\r\n>");
	ec = uSDapitest();
	//

func_end:	
	tprintf("\r\nEND, ec =%d",ec);
	for(;;);
	return 0;
}

//********************************************************************************

ERRCODE uSDapitest(void)
{
	ERRCODE ec=SUCCESS;
	int dataInt=-1;
	int escape_cnt=0;
	UINT len;
	UINT uSDSector =0;
	UINT uSDStatus;
	//

	for (;;)
	{
		dataInt = getChar();
		if(dataInt != -1 ) 
		{
			if(dataInt == '?')
			{	
				tprintf("\n\ruSD: rx=%08lu tx=%08lu errors=%08lu", 
						uSD.getRxCnt(),uSD.getTxCnt(),uSD.getErrors());
				continue;		
			}
			#ifdef uSD_WRITE
			// WRITE NEXT PAGE

			for(len=0;len<DATASTORAGEAPI_SECTOR_SIZE;len++)
					uSD.dataBuffer[len]= uSDSector & 0xFF;
			//

			uSD.writeSector(uSDSector);
			uSDStatus = uSD.getStatus();
			tprintf("\n\ruSD:  status %d ",uSDStatus);
			if (SUCCESS == uSDStatus)	tprintf("\n\ruSD: sector %d is written",uSDSector);
			else 						tprintf("\n\ruSD: sector %d is NOT written, ec = %d",uSDSector,uSDStatus);
			#endif
			#ifdef uSD_READ
			// READ NEXT PAGE

			for(len=0;len<DATASTORAGEAPI_SECTOR_SIZE;len++)
					uSD.dataBuffer[len]= 0x00;
			//

			uSD.readSector(uSDSector);
			uSDStatus = uSD.getStatus();
			//

			if(SUCCESS == uSDStatus)
			{
				tprintf("\n\ruSD: sector %d is read",uSDSector);
				for(len=0;len<DATASTORAGEAPI_SECTOR_SIZE;len++)
				{
					if (!(len%16)) tprintf("\n\r%04x",len);
					tprintf(" %02X",uSD.dataBuffer[len]);
				} 
			}
			else
			{
				tprintf("\n\ruSD: page %d is NOT read, ec = %d",uSDSector,uSDStatus);
			}
			#endif
			//

			uSDSector++;
			if(uSD.getMaxSector()<uSDSector)uSDSector=0; 
			// Check 

			if(dataInt == '-')
			{
				if (++escape_cnt == 3) break;
			}
			else escape_cnt =0;
		}
	}
	return ec;
}