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

📄 nf_drv.c

📁 通过车载终端采集GPS数据
💻 C
📖 第 1 页 / 共 2 页
字号:
    }
    else
    {
        Test_nf_write_open_B_area(pos>>9, (Byte)pos);
    }

	for (tmpCount=0; tmpCount<nLen; tmpCount++)
		D12_Y3 = pBuf[tmpCount];

    Nf_wait_busy();
    Test_nf_send_command(NF_PAGE_PROGRAM_CMD);

    Nf_wait_busy();
	return OK;
}


/*F**************************************************************************
* NAME: nf_write_quarterpage
*----------------------------------------------------------------------------
* PARAMS:   
*	pos: the start row address
*   pbuf: the content to write
* return:
*   OK : write done
*   KO : write not done
*----------------------------------------------------------------------------
* PURPOSE: write one on Nand Flash Media. It is only permitted to write 128 Bytes
* when writting on flash(our assume).
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   This function use the global variable Uint32 address
*----------------------------------------------------------------------------
* REQUIREMENTS: 
* ram/xram:
* cycle:
* stack: 
* code:
*****************************************************************************/
/*bit nf_write_quarterpage(Uint32 pos, Byte* pBuf)
{
	Uint8 status = 0;
	Uint32 nTmp;
	Byte nCol;

	nTmp = pos >> 13;
	if ((FALSE == xEraseStatus) || (xCurErasedBlock != nTmp))
	{
		nf_block_erase(pos);

        nf_check_status();

		xEraseStatus = TRUE;
		xCurErasedBlock = nTmp;
	}

	Nf_wait_busy();
	nTmp = (pos >> 8);
	nCol = pos;
	if (0 == halfpage)
	{
        Test_nf_write_open_A_area(nTmp, nCol);
	}
	else
   	{
        Test_nf_write_open_B_area(nTmp, nCol);
	}
	for (nTmp=0; nTmp<256; nTmp++)
		Nf_wr_byte(pBuf[nTmp]);


    Nf_wait_busy();
    Test_nf_send_command(NF_PAGE_PROGRAM_CMD);


  Nf_wait_busy();

	return OK;
}*/

/*F**************************************************************************
* NAME: nf_read_tobuf
*----------------------------------------------------------------------------
* PARAMS:   
*	
* return:
*   OK : write done
*   KO : write not done
*----------------------------------------------------------------------------
* PURPOSE: read flash and copy to flash
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   This function use the global variable Uint32 address
*----------------------------------------------------------------------------
* REQUIREMENTS: 
* ram/xram:
* cycle:
* stack: 
* code:
*****************************************************************************/
bit nf_read_tobuf(Uint32 pos, Byte* pbuf, Uint8 nlen)  
{
	/*first check whether start A zone or B zone*/
	Uint32 nPageAddr;
    Uint32 nBlockAddr;
	Uint32 nTmp;
	Uint8 i;
    nPageAddr = pos>>9;
    nBlockAddr = nPageAddr>>5;
    i = (Byte)pos;
	Nf_wait_busy();

	if (0 == (pos & 0x00000100))
	{
        Test_nf_read_open_A_area(nPageAddr, i);
        
	}
	else
	{
        Test_nf_read_open_B_area(nPageAddr, i);
	}

	nBlockAddr = ((pos>>14)+1) << 14;
    nPageAddr = (nPageAddr+1)<<9;
    Nf_wait_busy();
	for (i=0; i<nlen; i++)
	{
       Test_nf_rd_byte(pbuf[i]);

		pos++;
		if (pos >= nPageAddr)
		{
			/*new block*/
            if (pos >= nBlockAddr)
            {
    			nTmp = nBlockAddr>>9;
                Test_nf_read_open_A_area(nTmp, 0x00);
                nBlockAddr = ((nBlockAddr>>14)+1) << 14;
            }
            else
            {
                Test_nf_read_open_A_area(pos>>9, 0x00);
            }   
			nPageAddr = ((nPageAddr>>9)+1) << 9;
			Nf_wait_busy();
		}
	}
	return OK;
}


data Uint32 nopenPage;
//函数假定一次读操作内不会跨过两页
/*bit nf_read_tobuf2(Uint32 pos, Byte* pbuf, Uint8 nlen)  
{
    static bit bInitFlag = 0;
    data Uint32 nPageAddr;
    data Byte i;
    nPageAddr = pos>>9;
    Nf_wait_busy();
    if (0 == bInitFlag)
    {
        bInitFlag = 1;
        nopenPage = nPageAddr;
        i = (Byte)pos;
        Test_nf_read_open_A_area(nPageAddr, i);
        Nf_wait_busy();
        for (i=0; i<nlen; i++)
            pbuf[i] = D12_Y3;
    }
    else
    {
        if (nopenPage == nPageAddr)
        {
            //不需要重新打开页
            for (i=0; i<nlen; i++)
                pbuf[i] = D12_Y3;
        }
        else
        {
            nopenPage = nPageAddr;
            i = (Byte)pos;
            Test_nf_read_open_A_area(nPageAddr, i);
            Nf_wait_busy();
            for (i=0; i<nlen; i++)
                pbuf[i] = D12_Y3;
        }
    }
	return OK;
}*/

//函数支持一次读两页
bit nf_read_tobuf3(Uint32 pos, Byte* pbuf, Uint8 nlen)  
{
    static bit bInit = 0;
    data Uint32 nPageAddr, nNextPage, nCurAddr;
    data Byte i;
    nPageAddr = pos>>9;
    nCurAddr = pos;
    Nf_wait_busy();
    if (0 == bInit)
    {
        bInit = 1;
        nopenPage = nPageAddr;
        i = (Byte)pos;
        Test_nf_read_open_A_area(nPageAddr, i);
        Nf_wait_busy();
        nPageAddr++;
        nNextPage = nPageAddr<<9;
        for (i=0; i<nlen; i++)
        {
            pbuf[i] = D12_Y3;
            nCurAddr++;
            if (nCurAddr >= nNextPage)
            {
                Test_nf_read_open_A_area(nPageAddr, 0);
                nopenPage = nPageAddr;
                nNextPage += 512;
            }
        }
    }
    else
    {        
        if (nopenPage == nPageAddr)
        {
            nPageAddr++;
            nNextPage = nPageAddr<<9;
            //不需要重新打开页
            for (i=0; i<nlen; i++)
            {
                pbuf[i] = D12_Y3;
                nCurAddr++;
                if (nCurAddr >= nNextPage)
                {
                    Test_nf_read_open_A_area(nPageAddr, 0);
                    nopenPage = nPageAddr;
                    nNextPage += 512;
                }
            }
        }
        else
        {
            nopenPage = nPageAddr;
            i = (Byte)pos;
            //Test_nf_read_open_A_area(nPageAddr, i);old
            /**原有程序存在一定的bug,但是不会有影响**/
            if (0 == (pos & 0x00000100))
        	{
                Test_nf_read_open_A_area(nPageAddr, i);
                
        	}
        	else
        	{
                Test_nf_read_open_B_area(nPageAddr, i);
        	}
            /***结束,原有的最多只会影响256个字节***/
            Nf_wait_busy();
            nPageAddr++;
            nNextPage = nPageAddr<<9;
            for (i=0; i<nlen; i++)
            {
                pbuf[i] = D12_Y3;
                nCurAddr++;
                if (nCurAddr >= nNextPage)
                {
                    Test_nf_read_open_A_area(nPageAddr, 0);
                    nopenPage = nPageAddr;
                    nNextPage += 512;
                }
            }
        }
    }
	return OK;
}


/*F**************************************************************************
* NAME: nf_init
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*   OK : init complete
*   KO : - NF not supported or not recognise
*----------------------------------------------------------------------------
* PURPOSE:
*   NF initialisation
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_init (void)
{
    Uint8 volatile test;
	D17CS = 0xff;
	D17CS &= 0x7f;
	D12_Y0 = D17CS;
    xCurErasedBlock = 0;
	xEraseStatus = FALSE;
	halfpage = 0;

    nf_wp_on();
    Test_nf_send_command(NF_RESET_CMD);



#if (NF_CAPACITY_AUTO_DETECT == TRUE)               /* Auto Detect the type of nand-flash  */
        //test here
        test = 0;
		
        Test_nf_send_command(NF_READ_ID_CMD);


        Test_nf_send_address(0x00);

        
        Nf_wait_busy();
        delay();
        test = D12_Y3;
        test = D12_Y3;                                     /* Maker  code */
    	switch (test)                             /* Device code */
    	{
	      /***************************************************************************/
	      case 0x73 :                                     /*------- 16 Mbyte --------*/
	        nf_device_type = NF_SIZE_16MB;
	        nf_zone_max = 1;                              /* 1 zone : 1024 blocks    */
	        nf_4_cycle_address = 0;                       /* 3 address cycles        */
	        break;
	      /***************************************************************************/
	      case 0x75 :                                     /*------- 32 Mbyte --------*/
	        nf_device_type = NF_SIZE_32MB;
	        nf_zone_max = 2;                              /* 2 zones : 2048 blocks   */
	        nf_4_cycle_address = 0;                       /* 3 address cycles        */
	        break;
	      /***************************************************************************/
	      case 0x76 :                                     /*------- 64 Mbyte --------*/
	        nf_device_type = NF_SIZE_64MB;
	        nf_zone_max = 4;                              /* 4 zones : 4096 blocks   */
	        nf_4_cycle_address = 1;                       /* 4 address cycles        */
	        break;
	      /***************************************************************************/
	      case 0x79 :                                     /*------ 128 Mbyte --------*/
	        nf_device_type = NF_SIZE_128MB;
	        nf_zone_max = 8;                              /* 8 zones : 8192 blocks   */
	        nf_4_cycle_address = 1;                       /* 4 address cycles        */
	        break;
	      default:
	        return KO;
	    }                   
#endif
	nf_check_status();
	
//	nf_testchip();
	return OK;
}

bit nf_testchip()
{
    Uint16 nBlockNum;
    Uint16 i;
    Uint32 nTestBlock;
    Uint8 nValidFlag;
    switch (nf_device_type)
    {
        case NF_SIZE_32MB:
            nBlockNum = 2048;
            break;
		case NF_SIZE_16MB:
			nBlockNum = 1024;
            break;
    }
    
    nTestBlock = 0;
    for (i=0; i<nBlockNum; i++)
    {
        Test_nf_read_open_C_area(nTestBlock, 0x05);
		nValidFlag = D12_Y3;
//        Test_nf_rd_byte(nValidFlag);
        if (0xff != nValidFlag)
        {
            return KO;
        }
        else
        {
            nValidFlag = 0;
        }
    }

    return OK;
}

void delay()
{
    idata unsigned int temp;
    for (temp=0; temp<3000;temp++)
    {
        temp += 1;
        temp -= 1;
    }
}

⌨️ 快捷键说明

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