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

📄 flash.c

📁 Windows CE 6.0 BSP for VOIPAC Board (PXA270) Version 2b.
💻 C
📖 第 1 页 / 共 2 页
字号:
        *pFlash = (FIOW) 0x00600060;
        *pFlash = (FIOW) 0x00d000d0;
        while ((*pFlash & 0x00800080) != (FIOW) 0x00800080);

        status = *pFlash;
        if ((status & 0x00000008) == 0x00000008)
		{
            KITLOutputDebugString("ERROR: FlashErase: voltage range error ... lower flash.\r\n");
		}
        if ((status & 0x00080000) == 0x00080000)
		{
            KITLOutputDebugString("ERROR: FlashErase: voltage range error ... upper flash.\r\n");
		}
        if ((status & 0x00000030) == 0x00000030)
		{
            KITLOutputDebugString("ERROR: FlashErase: command sequence error ... lower flash.\r\n");
		}
        if ((status & 0x00300000) == 0x00300000)
		{
            KITLOutputDebugString("ERROR: FlashErase: command sequence error ... upper flash.\r\n");
		}
        if ((status & 0x00000020) == 0x00000020)
		{
            KITLOutputDebugString("ERROR: FlashErase: clear lock bits error ... lower flash.\r\n");
		}
        if ((status & 0x00200000) == 0x00200000)
		{
            KITLOutputDebugString("ERROR: FlashErase: clear lock bits error ... upper flash.\r\n");
		}

        if (status != (FIOW) 0x00800080)
        {
            KITLOutputDebugString("ERROR: FlashErase: status register returned 0x%X\r\n", status);
            KITLOutputDebugString("ERROR: FlashErase: unrecoverable failure encountered while erasing flash.  System halted!\r\n");
            return(FALSE);
        }
        if ( ((g_FlashDeviceType == L18) || (g_FlashDeviceType == L30)) && (num_l3_blocks_erased > 0) )
        {
            // We need to take care of the first 16K blocks if there are any
            pFlash += (L3_BLOCK_SIZE / 4);
            num_l3_blocks_erased--;
        }
        else
        {
            pFlash += (BLOCK_SIZE / sizeof(FIOW));
        }
    }
    pFlash = (volatile FIOW *)(FlashStart);

    // Clear the status register
    *pFlash = (FIOW) 0x00500050;

    if ( ((g_FlashDeviceType == L18) || (g_FlashDeviceType == L30)) && (num_l3_blocks_to_erase > 0) )
    {
        KITLOutputDebugString("INFO: FlashErase: erasing flash %X to %X.  Please wait... \r\n", FlashStart, FlashStart + (((num_blocks-num_l3_blocks_to_erase)*256*1024) + (num_l3_blocks_to_erase*64*1024)) - 1);
    }
    else
    {
        KITLOutputDebugString("INFO: FlashErase: erasing flash %X to %X.  Please wait... \r\n", FlashStart, FlashStart + (num_blocks*256*1024) - 1);
    }

    // For L3, need to set for use within the FOR loop
    num_l3_blocks_erased = num_l3_blocks_to_erase;

    // Erase each block.
    for (j = 0; j < num_blocks; j++)
    {
        // Issue erase and confirm command
        *pFlash = (FIOW) 0x00200020;
        *pFlash = (FIOW) 0x00d000d0;
        while ((*pFlash & 0x00800080) != (FIOW) 0x00800080);

        // Check if the flash block erased successfully.
        // Was either block locked?
        status = *pFlash;
        if ((status & 0x00200000) || (status & 0x00000020))
        {
            if (status & 0x00200000)
            {
                KITLOutputDebugString("ERROR: FlashErase: block erase failure.  Lock bit upper flash set!\r\n");
            }

            if (status & 0x00000020)
            {
                KITLOutputDebugString("ERROR: FlashErase: block erase failure.  Lock bit lower flash set!\r\n");
            }

            KITLOutputDebugString("ERROR: FlashErase: unrecoverable failure encountered while erasing flash.\r\n");
            return(FALSE);
        }
        KITLOutputDebugString(".");

        if ( ((g_FlashDeviceType == L18) || (g_FlashDeviceType == L30)) && (num_l3_blocks_erased > 0) )
        {
            pFlash += (L3_BLOCK_SIZE / sizeof(FIOW));
            num_l3_blocks_erased--;
        }
        else
        {
            pFlash += (BLOCK_SIZE / sizeof(FIOW));
        }
    }

    KITLOutputDebugString("\r\n");
    pFlash = (volatile FIOW *)(FlashStart);

    // Put the flash back into read mode
    if ((g_FlashDeviceType == L18) || (g_FlashDeviceType == L30))
    {
        // Setting the number of 16K blocks that we have to deal with
        num_l3_blocks_erased = num_l3_blocks_to_erase;

        // Put each block back into read mode
        for (j = 0; j < num_blocks; j++)
        {
            *pFlash = (FIOW) 0x00FF00FF;

            if ( num_l3_blocks_erased )
            {
                pFlash += (L3_BLOCK_SIZE / sizeof(FIOW));
                num_l3_blocks_erased--;
            }
            else
            {
                pFlash += (BLOCK_SIZE / sizeof(FIOW));
            }
        }
        // Now reset the flash pointer
        pFlash = (volatile FIOW *)(FlashStart);
    }
    else
    {
        *pFlash = (FIOW) 0x00FF00FF;
    }

    // Flash erase verification.
    for (j = 0; j < (FlashLength / sizeof(FIOW)); j++)
    {
        if (*pFlash != (FIOW) 0xFFFFFFFF)
        {
            KITLOutputDebugString("ERROR: FlashErase: erase verification failure at address 0x%X Data 0x%X.\r\n", pFlash, *pFlash);
            KITLOutputDebugString("ERROR: FlashErase: unrecoverable failure encountered while erasing flash.\r\n");
            return(FALSE);
        }
		pFlash++;
    }

    return(TRUE);
}


BOOL FlashInit(UINT32 FlashStart, UINT32 FlashLength)
{
    FlashStart = (UINT32) OALCAtoUA(FlashStart);

    // Read the device ID and determine the flash part type.
    //
    g_FlashDeviceType = DetectFlashDevice(FlashStart);

    return((g_FlashDeviceType != NONE) ? TRUE : FALSE);
}


BOOL FlashRead(UINT32 FlashStart, UINT8 *pDataBuffer, UINT32 DataLength)
{
    if (!pDataBuffer) return(FALSE);

    FlashStart = (UINT32) OALCAtoUA(FlashStart);

    // TODO - check flash address...
    memcpy((void *)pDataBuffer, (const void *)FlashStart, DataLength);

    return(TRUE);
}


// TODO - block-aligned address.
BOOL FlashWrite(UINT32 FlashStart, UINT8 *pDataBuffer, UINT32 DataLength)
{
    UINT32 j,b;
    UINT32 sizeFlashCache; // size in bytes of the flash cache
    UINT32 chunksPerBlock;
    UINT32 count;
    UINT32 val = 0;
    UINT32 dwLength = DataLength;

    volatile FIOW *pFlash         = (volatile FIOW *) FlashStart;
    volatile FIOW *pBlockAddress  = (volatile FIOW *) FlashStart;
    volatile FIOW *pDeviceAddress = (volatile FIOW *) FlashStart;
    volatile FIOW *pFlashCache    = (FIOW *) pDataBuffer;

    volatile FIOW *pdwFlashStart;
    volatile FIOW *pdwRamStart;


    if (g_FlashDeviceType == NONE)
    {
        KITLOutputDebugString("ERROR: FlashWrite: unrecognized flash part.\r\n");
        return(FALSE);
    }

    if (!FlashErase(FlashStart, DataLength))
    {
        return(FALSE);
    }
        
    b = 0;

    if (g_FlashDeviceType == J3)
    {
        sizeFlashCache = 64;   // 64 bytes total, 32 bytes per device
        chunksPerBlock = 4096; // 256K / 64bytes = 4096
    }
    else if ((g_FlashDeviceType == K3) || (g_FlashDeviceType == K18) ||
		(g_FlashDeviceType == L18) || (g_FlashDeviceType == L30))
    {
        sizeFlashCache = 128;  // 128 bytes total, 64 bytes per device
        chunksPerBlock = 2048; // 256K / 128bytes = 2048
    }
	else
	{
        sizeFlashCache = 64;  // 64 bytes total, 64 bytes per device
        chunksPerBlock = 2048; // 128K / 64bytes = 2048
	}

    KITLOutputDebugString("INFO: FlashWrite: writing to flash...\r\n");

    while (DataLength > 0)
    {
        // Calculate the number of DWORDS to program in each iteration based on the size in bytes
        // of the flash cache.
        if (DataLength > sizeFlashCache)
            count = sizeFlashCache / sizeof(FIOW);
        else
            count = DataLength / sizeof(FIOW);

        // Issue Write to Buffer Command
        *pBlockAddress = (FIOW) 0x00E800E8;

        // Check that the write buffer is available.
        // Read the Extended Status Register and
        // wait for the assertion of XSR[7] before continuing.
        while ( (*pBlockAddress & 0x00800080) != (FIOW) 0x00800080)
        {
            *pBlockAddress = (FIOW) 0x00E800E8;
        }

        // Configure the size of the buffer that we will write.
        // Write the word count (device expects a 0 based number)
        *pBlockAddress = (FIOW) ((count - 1) << 16 | (count - 1));

        pdwFlashStart = pDeviceAddress;
        pdwRamStart   = pFlashCache;

        // Write up to "count" DWORDS into the Flash memory staging area
        for (j = 0; j < count; j++)
        {
            *pDeviceAddress++ = *pFlashCache++;
        }

        // increment the number of 64 or 128 byte segments written.
        b++;
        // Now program the buffer into Flash
        *pBlockAddress = (FIOW) 0x00D000D0;
        while ((*pBlockAddress & 0x00800080) != (FIOW) 0x00800080);

        DataLength -= (count * sizeof(FIOW));

        // Read back the segment just written
        *pFlash = (FIOW) 0x00FF00FF;
        for (j = 0; j < count; j++)
        {
            if ((g_FlashDeviceType == L18) || (g_FlashDeviceType == L30))
            {
                *pdwFlashStart = (FIOW) 0x00FF00FF;
            }

            if (*pdwFlashStart != *pdwRamStart)
            {
                KITLOutputDebugString("ERROR: FlashWrite: flash programming failure at address 0x%x (expected 0x%x actual 0x%x).\r\n", pdwFlashStart, *pdwRamStart, *pdwFlashStart);
                return(FALSE);
            }
			pdwFlashStart++;
			pdwRamStart++;
        }

        if ((b % chunksPerBlock) == 0)
        {
            KITLOutputDebugString(".");
        }
    }

    // Verify the data written to flash...
    //
    pFlash      = (volatile FIOW *) FlashStart;
    pFlashCache = (FIOW *) pDataBuffer;

    // Put Flash into read mode.
    *pFlash = (FIOW) 0x00FF00FF;

    KITLOutputDebugString("\r\nINFO: FlashWrite: verifying the data written to flash...\r\n");

    for ( j = 0; j < (dwLength/sizeof(FIOW)); j++ )
    {
        if (*pFlash != *pFlashCache)
        {
            KITLOutputDebugString( "ERROR: FlashWrite: verification failure at address 0x%x (expected 0x%x actual 0x%x))\r\n", pFlash, *pFlashCache, *pFlash );
            return(FALSE);
        }
		pFlash++;
		pFlashCache++;
    }

    KITLOutputDebugString("INFO: FlashWrite: flash programmed successfully!\r\n");

    return(TRUE);
}

// END: these routines will be replaced by the FMD when it's done.

⌨️ 快捷键说明

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