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

📄 ethdown.c

📁 Xcale270Bsp包,wince平台
💻 C
📖 第 1 页 / 共 3 页
字号:
            EdbgOutputDebugString("\r\nVoltage Range Error ... Lower flash.\r\n");
        if ((i & 0x00080000) == 0x00080000)
            EdbgOutputDebugString("\r\nVoltage Range Error ... Upper flash.\r\n");

        if ((i & 0x00000030) == 0x00000030)
            EdbgOutputDebugString("\r\nCommand Sequence Error ... Lower flash.\r\n");
        if ((i & 0x00300000) == 0x00300000)
            EdbgOutputDebugString("\r\nCommand Sequence Error ... Upper flash.\r\n");

        if ((i & 0x00000020) == 0x00000020)
            EdbgOutputDebugString("\r\nClear Lock Bits Error ... Lower flash.\r\n");
        if ((i & 0x00200000) == 0x00200000)
            EdbgOutputDebugString("\r\nClear Lock Bits Error ... Upper flash.\r\n");

        if (i != 0x00800080)
        {
            EdbgOutputDebugString("Status register returned 0x%X\r\n", i);
            EdbgOutputDebugString("Unrecoverable failure encountered while erasing flash.  System halted!\r\n");
            while (1);
        }
        if ( ((flashType == L18) || (flashType == L30)) && (num_l3_blocks_erased > 0) )
        {
            // We need to take care of the first 16K blocks if there are any
            pdwFlash += (L3_BLOCK_SIZE / 4);  
            num_l3_blocks_erased--;
        }
        else
        {
            pdwFlash += (BLOCK_SIZE / 4);  
        }
    }
    pdwFlash = (volatile DWORD *)(dwPhysStart | CACHED_TO_UNCACHED_OFFSET);

    // Clear the status register
    *pdwFlash = 0x00500050;

    if ( ((flashType == L18) || (flashType == L30)) && (num_l3_blocks_to_erase > 0) )
    {
        EdbgOutputDebugString("\r\n Erasing Flash %X to %X: Please wait ... \r\n", dwPhysStart, dwPhysStart + (((num_blocks-num_l3_blocks_to_erase)*256*1024) + (num_l3_blocks_to_erase*64*1024)) - 1);
    }
    else
    {
        EdbgOutputDebugString("\r\nErasing Flash %X to %X: Please wait ... \r\n", dwPhysStart, dwPhysStart + (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++)
    {
        //EdbgOutputDebugString("Erasing 0x%X\r\n", pdwFlash);

        // Issue erase and confirm command
        *pdwFlash = 0x00200020;
        *pdwFlash = 0x00d000d0;
        while ((*pdwFlash & 0x00800080) != 0x00800080);

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

            if (status & 0x00000020)
            {
                EdbgOutputDebugString("\n\rBlock erase failure.  Lock bit lower flash set!\r\n");
            }

            EdbgOutputDebugString("Unrecoverable failure encountered while erasing flash.  System halted!\r\n");
            while (1);
        }
        EdbgOutputDebugString(".");

        if ( ((flashType == L18) || (flashType == L30)) && (num_l3_blocks_erased > 0) )
        {
            pdwFlash += (L3_BLOCK_SIZE / 4);  
            num_l3_blocks_erased--;
        }
        else
        {
            pdwFlash += (BLOCK_SIZE / 4); 
        }
    }

    pdwFlash = (volatile DWORD *)(dwPhysStart | CACHED_TO_UNCACHED_OFFSET);

    // Put the flash back into read mode
    if ((flashType == L18) || (flashType == 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++)
        {
            *pdwFlash = 0x00FF00FF;

            if ( num_l3_blocks_erased )
            {
                pdwFlash += (L3_BLOCK_SIZE / 4);  
                num_l3_blocks_erased--;
            }
            else
            {
                pdwFlash += (BLOCK_SIZE / 4);  
            }
        }
        // Now reset the flash pointer
        pdwFlash = (volatile DWORD *)(dwPhysStart | CACHED_TO_UNCACHED_OFFSET);
    }
    else
    {
        *pdwFlash = 0x00FF00FF;
    }

    EdbgOutputDebugString("\r\nPerforming erase verification ... \r\n");
    for (i = 0; i < dwPhysLen / 4; i++)
    {
        if (*pdwFlash++ != 0xFFFFFFFF)
        {
            EdbgOutputDebugString("\r\nErase verification failure at address 0x%X Data 0x%X. \r\n", pdwFlash-1, *(pdwFlash-1));
            EdbgOutputDebugString("Unrecoverable failure encountered while erasing flash.  System halted!\r\n");
            while (1);
        }
    }

    EdbgOutputDebugString("\r\nFlash erasing complete. \r\n");
    return 0;
}

UINT16 FlashWrite( DWORD dwPhysStart, DWORD dwPhysLen) 
{

    volatile DWORD *pdwFlash;
    volatile DWORD *pdwBlockAddress;
    volatile DWORD *pdwDeviceAddress;
    volatile DWORD *pdwFlashCache;
    volatile DWORD *pdwFlashStart;
    volatile DWORD *pdwRamStart;
    DWORD dwLength = dwPhysLen;
    DWORD i,j,b;
    DWORD val = 0;
    DWORD count;
    DWORD sizeFlashCache; // size in bytes of the flash cache
    DWORD chunksPerBlock;

    pdwFlash =          (volatile DWORD *)((FLASH_START + dwEBOOT_OFFSET) | CACHED_TO_UNCACHED_OFFSET);
    pdwBlockAddress =   (volatile DWORD *)((FLASH_START + dwEBOOT_OFFSET) | CACHED_TO_UNCACHED_OFFSET);
    pdwDeviceAddress =  (volatile DWORD *)((FLASH_START + dwEBOOT_OFFSET) | CACHED_TO_UNCACHED_OFFSET);
    pdwFlashCache =     (volatile DWORD *)(dwPhysStart | CACHED_TO_UNCACHED_OFFSET);

    FlashErase((DWORD)pdwFlash, dwPhysLen);

    b = 0;

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

    EdbgOutputDebugString("\n\rNow programming Flash ... \r\n");
    while ( dwPhysLen > 0)
    {
        // Calculate the number of DWORDS to program in each iteration based on the size in bytes
        // of the flash cache.
        if (dwPhysLen > sizeFlashCache)
            count = sizeFlashCache / 4;
        else
            count = dwPhysLen / 4;

        // Issue Write to Buffer Command
        *pdwBlockAddress = 0x00E800E8;

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

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

        pdwFlashStart = pdwDeviceAddress;
        pdwRamStart = pdwFlashCache;

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

        // increment the number of 64 or 128 byte segments written.
        b++;

        // Now program the buffer into Flash
        *pdwBlockAddress = 0x00D000D0;

        i = 0;
        while ((i & 0x00800080) != 0x00800080)
        {
            i = *pdwBlockAddress;
        }

        dwPhysLen -= count << 2;

        // Read back the segment just written
        *pdwFlash = 0x00FF00FF;
        for (i = 0; i < count; i++)
        {
            if ((flashType == L18) || (flashType == L30))
            {
                *pdwFlashStart = 0x00FF00FF;
            }

            if (*pdwFlashStart++ != *pdwRamStart++)
            {
                EdbgOutputDebugString( "\r\nFlash programming failure at address\r\n%X: Ideal %X Actual %X\r\n", pdwFlashStart-1, *(pdwRamStart-1), *(pdwFlashStart-1) );
                return 1;
            }
        }

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

    pdwFlash =  (volatile DWORD *)((FLASH_START+dwEBOOT_OFFSET) | CACHED_TO_UNCACHED_OFFSET);

    // Put Flash into read mode.
    *pdwFlash = 0x00FF00FF;

    EdbgOutputDebugString( "\r\nComparing Flash vs RAM image ...\n\r");

    pdwFlashCache = (volatile DWORD *)(dwPhysStart | CACHED_TO_UNCACHED_OFFSET);

    for ( j = 0; j < dwLength/4; j++ )
    {

        if (*pdwFlash++ != *pdwFlashCache++)
        {
            EdbgOutputDebugString( "\r\nBeat!\r\n%X: Ideal %X Actual %X\r\n", pdwFlash-1, *(pdwFlashCache-1), *(pdwFlash-1) );
            return 1;
        }
    }
    EdbgOutputDebugString( "Flash programmed successfully!\r\n" );

    return 0;
}

⌨️ 快捷键说明

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