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

📄 dataflash.c

📁 测试SPI接口
💻 C
📖 第 1 页 / 共 2 页
字号:

	//* fill the  command  buffer */
    pDataFlash->pDataFlashDesc->command[0] = OpCode;
    pDataFlash->pDataFlashDesc->command[1] = (unsigned char)((adr & 0x00FF0000) >> 16);
    pDataFlash->pDataFlashDesc->command[2] = (unsigned char)((adr & 0x0000FF00) >> 8);
    pDataFlash->pDataFlashDesc->command[3] = (unsigned char)(adr & 0x000000FF) ;
    pDataFlash->pDataFlashDesc->command[4] = 0;
    pDataFlash->pDataFlashDesc->command[5] = 0;
    pDataFlash->pDataFlashDesc->command[6] = 0;
    pDataFlash->pDataFlashDesc->command[7] = 0;

	/* Initialize the SpiData structure for the spi write fuction */
    pDataFlash->pDataFlashDesc->tx_cmd_pt   =  pDataFlash->pDataFlashDesc->command ;	
    pDataFlash->pDataFlashDesc->tx_cmd_size =  CmdSize ;
    pDataFlash->pDataFlashDesc->rx_cmd_pt   =  pDataFlash->pDataFlashDesc->command ;
    pDataFlash->pDataFlashDesc->rx_cmd_size =  CmdSize ;

	/* send the command and read the data */
    AT91F_SpiWrite (pDataFlash->pDataFlashDesc);

	return DATAFLASH_OK;
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_DataFlashPageRead
//* \brief Main memory page read
//*----------------------------------------------------------------------------
AT91S_DataFlashStatus AT91F_DataFlashPageRead (
	AT91PS_DataFlash pDataFlash,
	unsigned int src,
	unsigned char *dataBuffer,
	int sizeToRead )
{
    pDataFlash->pDataFlashDesc->rx_data_pt = dataBuffer ;	//* buffer for the read operation
    pDataFlash->pDataFlashDesc->rx_data_size = sizeToRead;	//* Number of byte to read
    pDataFlash->pDataFlashDesc->tx_data_pt = dataBuffer ;
    pDataFlash->pDataFlashDesc->tx_data_size = sizeToRead;

   //* Send the command to the dataflash
   return (AT91F_DataFlashSendCommand (pDataFlash, DB_PAGE_READ, 8, src));
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_DataFlashPagePgmBuf
//* \brief Main memory page program through buffer 1 or buffer 2
//*----------------------------------------------------------------------------
AT91S_DataFlashStatus AT91F_DataFlashPagePgmBuf(
	AT91PS_DataFlash pDataFlash,
	unsigned char *src,
	unsigned int dest,
	unsigned int SizeToWrite)
{	
    pDataFlash->pDataFlashDesc->tx_data_pt = src ;
    pDataFlash->pDataFlashDesc->tx_data_size = SizeToWrite ;
    pDataFlash->pDataFlashDesc->rx_data_pt = src;
    pDataFlash->pDataFlashDesc->rx_data_size = SizeToWrite;

   /* Send the command to the dataflash */
   return(AT91F_DataFlashSendCommand (pDataFlash,DB_PAGE_PGM_BUF1, 4, dest));
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_DataFlashPageErase
//* \brief Main memory page program through buffer 1 or buffer 2
//*----------------------------------------------------------------------------
AT91S_DataFlashStatus AT91F_DataFlashPageErase(
	AT91PS_DataFlash pDataFlash,
	unsigned char *src,
	unsigned int address,
	unsigned int SizeToWrite)
{	
    pDataFlash->pDataFlashDesc->tx_data_pt = src ;
    pDataFlash->pDataFlashDesc->tx_data_size = SizeToWrite ;
    pDataFlash->pDataFlashDesc->rx_data_pt = src;
    pDataFlash->pDataFlashDesc->rx_data_size = SizeToWrite;

   /* Send the command to the dataflash */
   return(AT91F_DataFlashSendCommand (pDataFlash, DB_PAGE_ERASE, 4, address));
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_DataFlashWaitReady
//* \brief wait for dataflash ready (bit7 of the status register == 1)
//*----------------------------------------------------------------------------
static AT91S_DataFlashStatus AT91F_DataFlashWaitReady(AT91PS_DataflashDesc pDataFlashDesc, unsigned int timeout)
{
	unsigned int i;

	pDataFlashDesc->DataFlash_state = IDLE;	

	do
	{

		pDataFlashDesc->DataFlash_state = AT91F_DataFlashGetStatus(pDataFlashDesc);
               // uprintf("\n\rread stat is 0x%x",stat);
		timeout--;
		// dummy waiting time
		for(i=0;i<10;i++);
	}
	while( ((pDataFlashDesc->DataFlash_state & 0x80) != 0x80) && (timeout>0) );

	if((pDataFlashDesc->DataFlash_state & 0x80) != 0x80)
		return DATAFLASH_ERROR;
	
	return DATAFLASH_OK;
}

/*
//#################################################################################################
void AT91F_Test(void)
{
     int i,j;
        AT91F_DBGU_Printk("\n\rTest start!!! \n\r");
       // AT91F_DBGU_Printk("\n\rPlease waitting for dataflash to be written and read  \n\r");
       // AT91F_DBGU_Printk("\n\rThis may takes a few seconds......\n\r");

	

	//* Wait DataFlash Ready
	AT91F_DataFlashWaitReady(DataFlash.pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT);


        //* Read Page 1
	for(i=0;i<BUFFER_SIZE_DATAFLASH;i++) 	Buffer_Dataflash[i] = 0x00;	
	AT91F_DataFlashPageRead(&DataFlash,(1*DataFlash.pDevice->pages_size),(unsigned char*)Buffer_Dataflash,(DataFlash.pDevice->pages_size) );

	//* Wait end of Read
	AT91F_DataFlashWaitReady(DataFlash.pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT);

	//* Write Page 1
    sprintf(Buffer_Dataflash,"\n\rThis sentence is written in your DataFlash... Congratulations!!!\n\r");
	AT91F_DBGU_Printk("\n\rThis sentence is written in your DataFlash... Congratulations!!!\n\r");
       // AT91F_DBGU_Printk("\n\rPlease wait...\n\r");

        AT91F_DataFlashPagePgmBuf(&DataFlash,(unsigned char*)Buffer_Dataflash,(j*DataFlash.pDevice->pages_size),(DataFlash.pDevice->pages_size) );
      	
	//* Wait end of Write
	 AT91F_DataFlashWaitReady(DataFlash.pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT);
       // for(j=1;j<8192;j++)
       // {
       // AT91F_DataFlashPageErase (&DataFlash,(unsigned char*)Buffer_Dataflash,(j*DataFlash.pDevice->pages_size),(DataFlash.pDevice->pages_size) );
       // }

	//* Read Page 1
	for(i=0;i<BUFFER_SIZE_DATAFLASH;i++) 	Buffer_Dataflash[i] = 0x00;	

        AT91F_DataFlashPageRead(&DataFlash,(j*DataFlash.pDevice->pages_size),(unsigned char*)Buffer_Dataflash,(DataFlash.pDevice->pages_size) );

	//* Wait end of Read
	AT91F_DataFlashWaitReady(DataFlash.pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT);

	//* End Of Test
	AT91F_DBGU_Printk("\n\rWrite Completed !!!\n\r");
	if (Buffer_Dataflash[0]!= 0xff)
	{
	// uprintf("\n\rOK, now reading dataflash ... done!\n\r ");
         uprintf("\n\rThe data in your dataflash is :\n\r%s",Buffer_Dataflash);
	}
	else
	{
	 AT91F_DBGU_Printk("\n\rData flash NOT Ready: !!!\n\r");
	}
}
//#############################################################################################
*/
char SPI_DATAFLASH_Test(char *databuffer)
{
	int i,j;

	//* Wait DataFlash Ready
	AT91F_DataFlashWaitReady(DataFlash.pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT);

	//* Read Page 1
	for(i=0;i<BUFFER_SIZE_DATAFLASH;i++) 	Buffer_Dataflash[i] = 0x00;	
	AT91F_DataFlashPageRead(&DataFlash,(1*DataFlash.pDevice->pages_size),(unsigned char*)Buffer_Dataflash,(DataFlash.pDevice->pages_size) );

	//* Wait end of Read
	AT91F_DataFlashWaitReady(DataFlash.pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT);

	//* Write Page 1
	Buffer_Dataflash[0]=databuffer[0];
    AT91F_DataFlashPagePgmBuf(&DataFlash,(unsigned char*)Buffer_Dataflash,(j*DataFlash.pDevice->pages_size),(DataFlash.pDevice->pages_size) );
      	
	//* Wait end of Write
	AT91F_DataFlashWaitReady(DataFlash.pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT);

	//* Read Page 1
	for(i=0;i<BUFFER_SIZE_DATAFLASH;i++) 	Buffer_Dataflash[i] = 0x00;	

	AT91F_DataFlashPageRead(&DataFlash,(j*DataFlash.pDevice->pages_size),(unsigned char*)Buffer_Dataflash,(DataFlash.pDevice->pages_size) );

	//* Wait end of Read
	AT91F_DataFlashWaitReady(DataFlash.pDataFlashDesc,AT91C_DATAFLASH_TIMEOUT);

	return(Buffer_Dataflash[0]);
	//* End Of Test
	
}
//#############################################################################################

⌨️ 快捷键说明

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