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

📄 mspro.c

📁 8032底层驱动部分。因为可以移植 所以单独来拿出来
💻 C
📖 第 1 页 / 共 3 页
字号:
	}
	else
	{
		cmd = CMD_READ_DATA;
		tpc = TPC_READ_LONG_DATA;	
	}
	
//#define USE_SET_CMD
#ifdef USE_SET_CMD	
	{
		kal_uint8 data[8] = {0};
		// set parameter register
		data[0] = (kal_uint8)(count >> 8);
		data[1] = (kal_uint8)(count);
		data[2] = (kal_uint8)(adrs >> 24);
		data[3] = (kal_uint8)(adrs >> 16);
		data[4] = (kal_uint8)(adrs >> 8);
		data[5] = (kal_uint8)(adrs);
		if((status = MSP_TPC_SetRWAdrs(0,8,MSP_DATCNT1_REG,6)) != MSP_NOERROR)
			return status;
		if((status = MSP_TPC_WriteReg((kal_uint32*)data,6)) != MSP_NOERROR)
			return status;	
		
		// send SET_CMD [READ_DATA] or [WRITE_DATA]
		if((status = MSP_TPC_SetCmd(cmd,&msc_sta)) != MSP_NOERROR)
			return status;	
	}
#else	// use Ex_Set_Cmd
	if((status = MSP_TPC_ExSetCmd(adrs,count,cmd,&msc_sta)) != MSP_NOERROR)
		return status;		
#endif	
	if(!(msc_sta & MSC_STA_BREQ))
	{
		status = MSP_ERR_NOBREQ;
		goto error_exit;
	}

	// check INT status
	while(msc_sta & MSC_STA_BREQ)
	{
		// use TPC_xxx_LONG_DATA to transfer data
		if((status = MSP_TPCs(buffer,MSP_LONGDATA_SIZE,tpc)) != MSP_NOERROR)
         goto error_exit;
         
		(*sectors)++;
		buffer += MSP_LONGDATA_SIZE/sizeof(kal_uint32);
		
		MSP_WaitCard_INT(&msc_sta);
		if(msc_sta & MSC_STA_ERR)
		{
			status = MSP_ERR_INTERR;
			goto error_exit;
		}
		if(msc_sta & MSC_STA_CED)
			break;				
	}
	
	return MSP_NOERROR;	
	
error_exit:
   MSP_CMD_Stop();
   return status;	
}
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#ifdef USE_SET_CMD	
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#else	// use Ex_Set_Cmd
/* under construction !*/
/* under construction !*/
#endif	
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
/*************************************************************************
* FUNCTION
*  MSP_CMD_ReadAtrb
*
* DESCRIPTION
*	TTPC for read 1 byte INT register
*
* PARAMETERS
*	1. adrs: start address.
*	2. count: number of of sectors
*	3. buffer: data buffer 
*	4. sectors: actually sectors accessed 
*	5. IsWrite: indicating read or write data
*
* RETURNS
*	MSP_STATUS
*
* GLOBALS AFFECTED
*	
*************************************************************************/
MSP_STATUS MSP_CMD_ReadAtrb(kal_uint32 adrs, kal_uint16 count, kal_uint32* buffer, kal_uint16* sectors)
{
	MSP_STATUS status;
	kal_uint16 msc_sta;

	// check if count and adrs are valid
	if(count == 0 || count > 0x40 || adrs > 0x3F)
		return MSP_ERR_CMDFAIL;
	
	if((status = MSP_TPC_ExSetCmd(adrs,count,CMD_READ_ATRB,&msc_sta)) != MSP_NOERROR)
		return status;	
	if(!(msc_sta & MSC_STA_BREQ))
	{
		status = MSP_ERR_NOBREQ;
		goto error_exit;
	}	
	while(msc_sta & MSC_STA_BREQ)
	{
		// use TPC_xxx_LONG_DATA to transfer data
		if((status = MSP_TPC_ReadLongData(buffer)) != MSP_NOERROR)
		   goto error_exit;
		   
		(*sectors)++;
		buffer += MSP_LONGDATA_SIZE/sizeof(kal_uint32);
		
		MSP_WaitCard_INT(&msc_sta);
		if(msc_sta & MSC_STA_ERR)
		{		   
			status = MSP_ERR_INTERR;
			goto error_exit;
		}
		if(msc_sta & MSC_STA_CED)
			return MSP_NOERROR;				
	}			
	return MSP_NOERROR;
	
error_exit:
   MSP_CMD_Stop();
   return status;		
}
/*************************************************************************
* FUNCTION
*  MSP_CMD_Erase
*
* DESCRIPTION
*	Erase data from the assigned address in USER AREA
*
* PARAMETERS
*	adrs: start address 
*	count: sectors to be erased 
*	callback: callback function to indicate the progress status
*
* RETURNS
*	MSP_STATUS
*
* GLOBALS AFFECTED
*	
*************************************************************************/
MSP_STATUS MSP_CMD_Erase(kal_uint32 adrs, kal_uint16 count,MSP_CallBack callback)
{
	MSP_STATUS status;
	kal_uint16 msc_sta;
	kal_uint8 data[8] = {0};
	
	// set parameter registers for Erase
	data[0] = (kal_uint8)(count >> 8);
	data[1] = (kal_uint8)(count);
	data[2] = (kal_uint8)(adrs >> 24);
	data[3] = (kal_uint8)(adrs >> 16);
	data[4] = (kal_uint8)(adrs >> 8);
	data[5] = (kal_uint8)(adrs);
	data[6] = 0; // set TPC parameter to 0 => 32 bytes for READ_SHORT_DATAs
	if((status = MSP_TPC_SetRWAdrs(MSP_INT_REG,2,MSP_DATCNT1_REG,7)) != MSP_NOERROR)
		return status;
	if((status = MSP_TPC_WriteReg((kal_uint32*)data,7)) != MSP_NOERROR)
		return status;	     		

   // send TPC SetCmd with cmd
	if((status = MSP_TPC_ExSetCmd(adrs,count,CMD_ERASE,&msc_sta)) != MSP_NOERROR)
		return status;

	while(!(msc_sta & MSC_STA_CED))
	{
		kal_uint8 p[24];
					
		// erase is in onging
		if(msc_sta & MSC_STA_BREQ)
		{
			if((status = MSP_TPC_ReadShortData((kal_uint32*) p, SHORT_DATA_24)) != MSP_NOERROR)
				goto error_exit;				
			if(callback != NULL)
				(*callback)();
		}
		MSP_WaitCard_INT(&msc_sta);
		if(msc_sta & MSC_STA_ERR)
		{
			status = MSP_ERR_INTERR;
			goto error_exit;
		}
		if(msc_sta & MSC_STA_CED)
			break;				
			
	};
	
	return MSP_NOERROR;
error_exit:
   MSP_CMD_Stop();
   return status;		
}

/*************************************************************************
* FUNCTION
*  MSP_CMD_FORMAT
*
* DESCRIPTION
*	Self-format with unique values.(Recober to factory default)
*
* PARAMETERS
*	adrs: start address 
*	count: sectors to be erased 
*	callback: callback function to indicate the progress status
*
* RETURNS
*	MSP_STATUS
*
* GLOBALS AFFECTED
*	
*************************************************************************/
MSP_STATUS MSP_CMD_Format(msp_format_enum type, MSP_CallBack callback)
{
	MSP_STATUS status;
	kal_uint16 msc_sta;
	kal_uint8 data[4] = {0};
	
	// set parameter registers for Format
	data[0] = 0; // use READ_SHORT_DATA for reading the progress data
	data[1] = (kal_uint8)type;
	if((status = MSP_TPC_SetRWAdrs(MSP_INT_REG,2,MSP_DATCNT1_REG,2)) != MSP_NOERROR)
		return status;
	if((status = MSP_TPC_WriteReg((kal_uint32*)data,2)) != MSP_NOERROR)
		return status;	     		

   // send TPC SetCmd with cmd
	if((status = MSP_TPC_SetCmd(CMD_FORMAT,&msc_sta)) != MSP_NOERROR)
		return status;

	while(!(msc_sta & MSC_STA_CED))
	{
		kal_uint8 p[32];
					
		// erase is in onging
		if(msc_sta & MSC_STA_BREQ)
		{
			if((status = MSP_TPC_ReadShortData((kal_uint32*) p, SHORT_DATA_24)) != MSP_NOERROR)
				goto error_exit;				
			if(callback != NULL)
				(*callback)();
		}
		MSP_WaitCard_INT(&msc_sta);
		if(msc_sta & MSC_STA_ERR)
		{
			status = MSP_ERR_INTERR;
			goto error_exit;
		}
		if(msc_sta & MSC_STA_CED)
			break;				
			
	};
	
	return MSP_NOERROR;
error_exit:
   MSP_CMD_Stop();
   return status;		
}

/*************************************************************************
* FUNCTION
*  MSP_CMD_Stop
*
* DESCRIPTION
*	Terminate the operation by READ_DATA, WRITE_DATA, READ_ATRB, ERASE and FORMAT
*
* PARAMETERS
*
* RETURNS
*	MSP_STATUS
*
* GLOBALS AFFECTED
*	
*************************************************************************/
MSP_STATUS MSP_CMD_Stop(void)
{
	kal_uint16 msc_sta;
	kal_uint8 reg[4];

	// check if INT_BREQ is set
	MSP_TPC_GetInt((kal_uint32 *)reg);
	if(reg[0] & INT_BREQ)
		return MSP_TPC_SetCmd(CMD_STOP,&msc_sta);
	
	return MSP_NOERROR;
}
/*************************************************************************
* FUNCTION
*  MSP_TPC_ReadShortData
*
* DESCRIPTION
*	TTPC for read 1 byte INT register
*
* PARAMETERS
*	intreg: intreg contains the result of the INT register
*
* RETURNS
*	MSP_STATUS
*
* GLOBALS AFFECTED
*	
*************************************************************************/
MSP_STATUS MSP_CMD_Sleep(void)
{
	kal_uint16 msc_sta;
	return MSP_TPC_SetCmd(CMD_SLEEP,&msc_sta);
}



//===========================================================================================//

#ifdef MSP_TEST

#define SIZE   (512*32)
kal_uint8 gbuffer[SIZE] = {0};

void MSP_Mass(void)
{
	MSP_STATUS status;
	kal_uint32 i,adrs,count,error;
	kal_uint16 bytes;

		adrs = 512*128; // start of address
		count = SIZE/512;     // number of pages
		for(i=0;i<SIZE;i++) {
			gbuffer[i] = (kal_uint8)(i%256);
      }

   // write 8 M bytes
	for(i=0;i<(1024*1024*8)/SIZE;i++)
	{
		status = MSP_CMD_ReadWriteData(adrs,count,(kal_uint32*)gbuffer,&bytes,MSP_WRITE);
		if(status)
			MSP_TPC_GetInt((kal_uint32*)gbuffer);
	}

	for(i=0;i<SIZE;i++)
		gbuffer[i] = 0;

	for(i=0;i<(1024*1024*8)/SIZE;i++)
	{
		status = MSP_CMD_ReadWriteData(adrs,count,(kal_uint32*)gbuffer,&bytes,MSP_READ);
		if(status)
			MSP_TPC_GetInt((kal_uint32*)gbuffer);
	}
		for(i=0;i<SIZE;i++)
		{
		   if(gbuffer[i] != (i%256))
		      error++;
		     gbuffer[i] = 0;
      }
}

void MSP_Test(void)
{
	MSP_STATUS status;
	volatile kal_uint8 c = 1;
	
	// initialization
	status = MSP_Initialize();
	while(c)
	{
		kal_uint32 adrs,i,error;
		kal_uint16 count,bytes;
		
		
		adrs = 512*100; // start of address
		count = SIZE/512;     // number of pages
		for(i=0;i<SIZE;i++) {
			//if (i%2==0) gbuffer[i] = (kal_uint8)0x70;
			//else	      gbuffer[i] = (kal_uint8)0x0f;
			gbuffer[i] = (kal_uint8)(i%256);
      }

		// read data
		status = MSP_CMD_ReadWriteData(adrs,count,(kal_uint32*)gbuffer,&bytes,MSP_READ);
		if(status)
			MSP_TPC_GetInt((kal_uint32*)gbuffer);

		for(i=0;i<SIZE;i++) {
			//if (i%2==0) gbuffer[i] = (kal_uint8)0x70;
			//else	      gbuffer[i] = (kal_uint8)0x0f;
		   gbuffer[i] = (kal_uint8)(i%256);
      }

		status = MSP_CMD_ReadWriteData(adrs,count,(kal_uint32*)gbuffer,&bytes,1);
		if(status)
			MSP_TPC_GetInt((kal_uint32*)gbuffer);
			
		for(i=0;i<SIZE;i++)
			gbuffer[i] = 0;		
		bytes = 0;	
		// read data
		status = MSP_CMD_ReadWriteData(adrs,count,(kal_uint32*)gbuffer,&bytes,0);		
		if(status)
			MSP_TPC_GetInt((kal_uint32*)gbuffer);
		error = 0;
		for(i=0;i<SIZE;i++)
		{
		   if(gbuffer[i] != (i%256))
		      error++;	
		     gbuffer[i] = 0;
      }
#define MSP_MASS_TEST
#ifdef MSP_MASS_TEST
		MSP_Mass();
#endif
		status = MSP_CMD_ReadAtrb(0,1,(kal_uint32*)gbuffer,&bytes);
		status = MSP_CMD_Erase(100,3,NULL);
		status = MSP_CMD_Format(MSP_FULL,NULL);
		
	}
}
#endif // MSP_TEST
#endif // __MSDC_MSPRO__


⌨️ 快捷键说明

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