⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drvsdcard.c

📁 cortex-m0 LCD1602程序
💻 C
📖 第 1 页 / 共 3 页
字号:
    	while(counter <= 3) 
		{
    		SingleWrite(0x00);
			counter++;
		}
		SingleWrite(current_command.CRC);
  	}
	
											// The command table entry will indicate
                                       		// what type of response to expect for
                                       		// a given command;  The following 
                                       		// conditional handles the MMC response;
	if(current_command.response == R1)  	// Read the R1 response from the card;
	{
  		loopguard=0;
    	do{
			card_response.b[0] = SingleWrite(0xFF);
      		if(!++loopguard) break;
    	}while((card_response.b[0] & BUSY_BIT));
		DBG_PRINTF("R1:0x%x, counter:%d\n",card_response.b[0],loopguard);
		if(!loopguard){BACK_FROM_ERROR;}
		*response=card_response.b[0];
    }                                     
    else if(current_command.response == R1b)// Read the R1b response; 
	{
    	loopguard = 0;	
      	do {
			card_response.b[0] =  SingleWrite(0xFF);
			if(!++loopguard) break;
      	}while((card_response.b[0] & BUSY_BIT));
		while((SingleWrite(0xFF)&0xFF) == 0x00);
	}
	else if(current_command.response == R2) 
	{
    	loopguard=0;
      	do{
		    card_response.b[0] = SingleWrite(0xFF);
        	if(!++loopguard) break;
      	}while((card_response.b[0] & BUSY_BIT));
	    card_response.b[1] = SingleWrite(0xFF);
		DBG_PRINTF("R2:0x%x, counter:%d\n",card_response.i,loopguard);
		if(!loopguard) { BACK_FROM_ERROR; }
		*response=card_response.i;		
    }else if(current_command.response == R3)  
	{                               // Read R3 response;
    	loopguard=0;
      	do {
        	card_response.b[0] = SingleWrite(0xFF);
		    if(!++loopguard) break;
      	} while((card_response.b[0] & BUSY_BIT));
  		DBG_PRINTF("R3:0x%x, counter:%d\n",card_response.b[0],loopguard);
  	  	if(!loopguard) { BACK_FROM_ERROR; }
      	counter = 0;
      	while(counter <= 3)              // Read next three bytes and store them
      	{                                // in local memory;  These bytes make up
        	counter++;                    // the Operating Conditions Register
	      	*pchar++ = SingleWrite(0xFF);
	    }
		*response=card_response.b[0];
    }else  
	{                               // Read R7 response;
    	loopguard=0;
      	do {
        	card_response.b[0] = SingleWrite(0xFF);
		    if(!++loopguard) break;
      	} while((card_response.b[0] & BUSY_BIT));
  		DBG_PRINTF("R7:0x%x, counter:%d\n",card_response.b[0],loopguard);
  	  	if(!loopguard) { BACK_FROM_ERROR; }
      	counter = 0;
      	while(counter <= 3)              // Read next three bytes and store them
      	{                                // in local memory;  These bytes make up
        	counter++;                    // the Operating Conditions Register
	      	*pchar++ = SingleWrite(0xFF);
	    }
		*response=card_response.b[0];
    }


	switch(current_command.trans_type)  // This conditional handles all data 
    {                                   // operations;  The command entry
                                       // determines what type, if any, data
                                       // operations need to occur;
		case RDB:                         // Read data from the MMC;
  			loopguard = 0;

	     	while((SingleWrite(0xFF)&0xFF)!=START_SBR) 
			{
    	  		if(!++loopguard) {BACK_FROM_ERROR;}
				DrvSYS_Delay(1);
	      	}		
          	counter = 0;                  	// Reset byte counter;
                                       		// Read <current_blklen> bytes;

			SPI1->TX[0] = 0xFFFFFFFF;
			if(pchar)
			{
				/*Set pchar+counter is a multiple of 4*/
				while(((uint32_t)pchar+counter)&0x03)
				{
					SPI1->CNTRL.GO_BUSY = 1;
					while(SPI1->CNTRL.GO_BUSY);
					*(pchar+counter++)=SPI1->RX[0];
				}
				/*Read data by word*/
				SPI1->CNTRL.TX_BIT_LEN=0; 	
				SPI1->CNTRL.REORDER=2;
				SPI1->CNTRL.TX_NUM=1;				
				for (; counter<current_blklen-7; )
				{

					SPI1->CNTRL.GO_BUSY = 1;
					while(SPI1->CNTRL.GO_BUSY);
					*((uint32_t*)(pchar+counter))=SPI1->RX[0];
					counter+=4;
					*((uint32_t*)(pchar+counter))=SPI1->RX[1];
					counter+=4;				
				}
				SPI1->CNTRL.TX_NUM=0;
				SPI1->CNTRL.REORDER=0;
				SPI1->CNTRL.TX_BIT_LEN=8;
				/*Read data by byte*/
				for (; counter<current_blklen; counter++)
				{
					SPI1->CNTRL.GO_BUSY = 1;
					while(SPI1->CNTRL.GO_BUSY);
					*(pchar+counter)=SPI1->RX[0];				
				}

			}else
			{
				for (; counter<current_blklen; counter++)
				{ 
					SPI1->CNTRL.GO_BUSY = 1;
					while(SPI1->CNTRL.GO_BUSY);									
				}
			}
           	dummy_CRC.b[1] = SingleWrite(0xFF);	// After all data is read, read the two
           	dummy_CRC.b[0] = SingleWrite(0xFF);	// CRC bytes;  These bytes are not used
                               					// in this mode, but the placeholders 
                   								// must be read anyway;			      
          	break;
		case RD:                         // Read data from the MMC;
  			loopguard = 0;

	     	while((SingleWrite(0xFF)&0xFF)!=START_SBR) 
			{
    	  		if(!++loopguard) {BACK_FROM_ERROR;}
	      	}		
          	counter = 0;                  	// Reset byte counter;
                                       		// Read <current_blklen> bytes;
			if(pchar)
			{
				for (counter=0; counter<current_blklen; counter++)
				{ 
					SPI1->TX[0] = 0xFF;
					SPI1->CNTRL.GO_BUSY = 1;
					while(SPI1->CNTRL.GO_BUSY);
					*(pchar+counter)=SPI1->RX[0];										
				}
			}else
			{
				for (counter=0; counter<current_blklen; counter++)
				{ 
					SPI1->TX[0] = 0xFF;
					SPI1->CNTRL.GO_BUSY = 1;
					while(SPI1->CNTRL.GO_BUSY);									
				}
			}
           	dummy_CRC.b[1] = SingleWrite(0xFF);	// After all data is read, read the two
           	dummy_CRC.b[0] = SingleWrite(0xFF);	// CRC bytes;  These bytes are not used
                               					// in this mode, but the placeholders 
                   								// must be read anyway;			      
          	break;

		case WR: 			
			SingleWrite(0xFF);
			SingleWrite(START_SBW);
          
			for (counter=0; counter<current_blklen; counter++)
			{
				SPI1->TX[0] = *(pchar+counter);
				SPI1->CNTRL.GO_BUSY = 1;
				dummy_CRC.i = GenerateCRC(*(pchar+counter), 0x1021, dummy_CRC.i);				
				while(SPI1->CNTRL.GO_BUSY);
			}
			SingleWrite(dummy_CRC.b[1]);
			SingleWrite(dummy_CRC.b[0]);
	
        	loopguard = 0;
	        do                            // Read Data Response from card;
	        {  
	          	data_resp = SingleWrite(0xFF);
	          	if(!++loopguard) break;
	        }while((data_resp & DATA_RESP_MASK) != 0x01);	// When bit 0 of the MMC response
	                                       					// is clear, a valid data response
	                                       					// has been received;
	        
	        if(!loopguard) { BACK_FROM_ERROR; }


	        while((SingleWrite(0xFF)&0xFF)!=0xFF);//Wait for Busy
			SingleWrite(0xFF);	        
	        break;
		default: break;
	}
    DrvSPI_ClrSS(eDRVSPI_PORT1, eDRVSPI_SS0);	// CS = 1
    if((current_command.command_byte == 9)||(current_command.command_byte == 10)) {
    	current_blklen = old_blklen;    
	}
	DBG_PRINTF("True\n");
    return TRUE;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: 	 MMC_FLASH_Init                                                                            */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*               None                                                                                      */
/*                                                                                                         */
/* Returns:                                                                                                */
/*               None                                                                                      */
/*                                                                                                         */
/* Side effects:                                                                                           */
/* Description:                                                                                            */
/*               This function is used to initialize the flash card                                        */
/*---------------------------------------------------------------------------------------------------------*/
void MMC_FLASH_Init(void)
{
	uint32_t response;
	uint8_t loopguard;
  	uint32_t i;
  	uint8_t counter = 0;    		
  	uint8_t pchar[16];         		// Data pointer for storing MMC 
  	uint32_t c_size,bl_len;
  	uint8_t c_mult;


	Is_Initialized = 0;


	DrvSPI_ClrSS(eDRVSPI_PORT1, eDRVSPI_SS0);	// CS = 1
	DrvSYS_Delay(1000);
	//--------------------------------------------------------
	//	Send 74 SD clcok in SD mode for Toshiba SD Card
	//--------------------------------------------------------	
  	for(counter = 0; counter < 10; counter++) {
    	SingleWrite(0xFF);
  	}
	DrvSYS_Delay(1000);

  	DrvSPI_SetSS(eDRVSPI_PORT1, eDRVSPI_SS0);  // CS = 0		
	while(MMC_Command_Exec(GO_IDLE_STATE,EMPTY,EMPTY,&response)==FALSE);
  	if(response!=0x01)
		return;

	if(MMC_Command_Exec(SEND_IF_COND,0x15A,pchar,&response) && response==1)
	{/* SDC ver 2.00 */
		if (pchar[2] == 0x01 && pchar[3] == 0x5A) 
		{	/* The card can work at vdd range of 2.7-3.6V */
			loopguard=0;
			do
			{
				MMC_Command_Exec(SD_SEND_OP_COND,0x40000000,EMPTY,&response);//Enable HCS(OCR[30])
				if(!++loopguard) break;
				DrvSYS_Delay(50);
			}while(response!=0);
			if(!loopguard) return;

			MMC_Command_Exec(READ_OCR,EMPTY,pchar,&response);
			SDtype=(pchar[0]&0x40)?SDv2|SDBlock:SDv2;
			DBG_PRINTF("SDv2\n");
		}
	}else
	{/* SDv1 or MMCv3 */

⌨️ 快捷键说明

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