mcitest.c

来自「nxp的LPC2888处理器的示例代码.」· C语言 代码 · 共 197 行

C
197
字号
/*****************************************************************************
 *   mcitest.c:  main C entry file for NXP LPC2xxx Family Microprocessors
 *
 *   Copyright(C) 2006, NXP Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2006.07.20  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/
#include "LPC288x.h"                       /* LPC2xxx definitions */
#include "type.h"
#include "target.h"
#include "irq.h"
#include "mci.h"
#if MCI_DMA_ENABLED
#include "ex_sdram.h"
#include "dma.h"
#endif

extern volatile DWORD CardType;

/* Below buffer allocation could be simplier, if the buffer used for DMA and non-DMA 
are shared. Future clear up will be needed. For now, leave as it's. */
#if MCI_DMA_ENABLED
BYTE *src_addr; 
BYTE *dest_addr;
extern volatile DWORD DMACompCount[DMA_CHANNELS];
#else
volatile BYTE WriteBlock[BLOCK_LENGTH], ReadBlock[BLOCK_LENGTH];
volatile DWORD TXBlockCounter, RXBlockCounter;
#endif

/******************************************************************************
**   Main Function  main()
******************************************************************************/
int main (void)
{
  DWORD i, j;

  TargetResetInit();
  
  /* Fill block data pattern in write buffer and clear everything 
  in read buffer. */
#if MCI_DMA_ENABLED
  SDRAMInit();
  DMA_MCI_Init();

  /* on DMA channel 0, source is memory, destination is MCI FIFO. */
  /* On DMA channel 1, source is MCI FIFO, destination is memory. */
  src_addr = (BYTE *)SDRAM_WR_ADDR;
  dest_addr = (BYTE *)SDRAM_RD_ADDR;
  for ( i = 0; i < BLOCK_LENGTH; i++ )
  {
	*src_addr++ = i;
	*dest_addr++ = 0;
  }
#else
  for ( i = 0; i < BLOCK_LENGTH; i++ )
  {
	WriteBlock[i] = i;
	ReadBlock[i] = 0;
  }
#endif
	
  /* For the SD card I tested, the minimum required block length is 512 */
  /* For MMC, the restriction is loose, due to the variety of SD and MMC
  card support, ideally, the driver should read CSD register to find the 
  right speed and block length for the card, and set them accordingly.
  In this driver example, it will support both MMC and SD cards, and it
  does read the information by send SEND_CSD to poll the card status,
  however, to simplify the example, it doesn't configure them accordingly
  based on the CSD register value. This is not intended to support all 
  the SD and MMC cards. */
  if ( MCI_Init() != TRUE )
  {
	while( 1 );			/* fatal error */
  }

  if ( (CardType = MCI_CardInit()) == CARD_UNKNOWN )
  {
	while ( 1 );		/* fatal error */
  }

  if ( MCI_Check_CID() != TRUE )
  {
	while ( 1 );		/* fatal error */
  }

  if ( MCI_Set_Address() != TRUE )
  {
	while ( 1 );		/* fatal error */
  }

  if ( MCI_Send_CSD() != TRUE )
  {
	while ( 1 );		/* fatal error */
  }

  if ( MCI_Select_Card() != TRUE )
  {
	while ( 1 );		/* fatal error */
  }

  if ( CardType == SD_CARD )
  {
	MCI_CLOCK |= (1 << 11);		/* Use wide bus for SD */
	for ( i = 0; i < 0x20; i++ );
	if ( MCI_Send_ACMD_Bus_Width( BUS_WIDTH_4BITS ) == FALSE )
	{
	  while ( 1 );	/* fatal error */
	}
  }

  if ( MCI_Set_BlockLen( BLOCK_LENGTH ) != TRUE )
  {
	while ( 1 );		/* fatal error */
  }

#if MCI_DMA_ENABLED
  for ( i = 0; i < BLOCK_NUM; i++ )
  {
  	if ( MCI_Write_Block( i ) != TRUE )
	{
	  while ( 1 );		/* Fatal error */
	}
	while ( !DMACompCount[0] );	/* Wait until DMA TX is done, channel 0 */
	DMACompCount[1] = 0;		/* DMA is disabled inside DMA ISR */

	if ( MCI_Read_Block( i ) != TRUE )
	{
	  while ( 1 );		/* Fatal error */
	}
	while ( !DMACompCount[1] );	/* Wait until DMA RX is done, channel 1 */
	DMACompCount[1] = 0;		/* DMA is disabled inside DMA ISR */

	src_addr = (BYTE *)SDRAM_WR_ADDR;
	dest_addr = (BYTE *)SDRAM_RD_ADDR;	
	for ( j = 0; j < BLOCK_LENGTH; j++ )
	{
	  if ( *src_addr++ != *dest_addr++ )
	  {
		while ( 1 );	/* Data comparison failure, fatal error */
	  }
	}
	/* clear read buffer for next comparison */
	for ( j = 0; j < BLOCK_LENGTH; j++ )
	{
	  *dest_addr++ = 0;
	}
  }
#else
  for ( i = 0; i < BLOCK_NUM; i++ )
  {
	TXBlockCounter = 0;
	RXBlockCounter = 0;
	if ( MCI_Write_Block( i ) != TRUE )
	{
	  while ( 1 );		/* Fatal error */
	}
	/* When TX_ACTIVE is not set, it indicates TX is done from 
	Interrupt_Write_Block, now, Interrupt_Read_Block can start. */
	while ( MCI_STATUS & MCI_TX_ACTIVE );
	/* Note TXEnable() is called in the Interrupt_Write_Block(). */
	MCI_TXDisable();

	if ( MCI_Read_Block( i ) != TRUE )
	{
	  while ( 1 );		/* Fatal error */
	}
	/* When RX_ACTIVE is not set, it indicates RX is done from 
	Interrupt_Read_Block, now, validation of RX and TX buffer can start. */
	while ( MCI_STATUS & MCI_RX_ACTIVE );
	/* Note RXEnable() is called in the Interrupt_Read_Block(). */
	MCI_RXDisable();

	for ( j = 0; j < BLOCK_LENGTH; j++ )
	{
	  if ( WriteBlock[j] != ReadBlock[j] )
	  {
		while ( 1 );	/* Data comparison failure, fatal error */
	  }
	}
	/* clear read buffer for next comparison */
	for ( j = 0; j < BLOCK_LENGTH; j++ )
	{
	  ReadBlock[j] = 0;
	}			 
  }
#endif
  return 0;
}

/******************************************************************************
**                            End Of File
******************************************************************************/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?