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

📄 davincievm_flash_write.c

📁 TI的DM6446的硬件平台搭建的相关例子
💻 C
字号:
/*
 *  Copyright 2005 by Spectrum Digital Incorporated.
 *  All rights reserved. Property of Spectrum Digital Incorporated.
 *
 *  Not for distribution.
 */

/*
 *  Flash implementation - Write AMD Flash
 *
 */

#include "davincievm_flash.h"

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  DAVINCIEVM_FLASH_write( src, dst, length )                              *
 *      Write to Flash address ( dst ) the data at non-Flash address ( src )*
 *      for ( length ) bytes.                                               *
 *                                                                          *
 *      src     <- source address                                           *
 *      dest    <- destination address                                      *
 *      length  <- length in bytes                                          *
 *                                                                          *
 * ------------------------------------------------------------------------ */
Int16 DAVINCIEVM_FLASH_write( Uint32 src, Uint32 dst, Uint32 length )
{
    Uint32 i = 0;
    volatile Uint8 tmpval;
    volatile Uint16 oldval;
    volatile Uint16 newval;
    Uint32 oddlen;
    Int32 timecount, timeout = 0xFFFF;

    /* Flash Mode: Read Array */
    *( volatile Uint16* )dst = FLASH_RESET;

    /* -------- Align to 16-bit words -------- */
    if ( dst & 1 )
    {
        /* Retrieve the data to write and overwrite */
        tmpval = *( volatile Uint8* )src;
        oldval = *( volatile Uint16* )dst;
        newval = ( oldval & 0x00ff ) + ( tmpval << 8 );

        /* Program one 16-bit word */
        FLASH_CTL555 = FLASH_CMD_AA;
        FLASH_CTL2AA = FLASH_CMD_55;
        FLASH_CTL555 = FLASH_PROGRAM;
        *( volatile Uint16* )dst     = newval;

        /* Wait for operation to complete */
        timecount = 0;
        while( ( *( volatile Uint16* )dst != newval ) && ( ++timecount < timeout ) );

        if ( timecount == timeout )
            return -1;

        /* Increment variables */
        i++;
        length--;
        src++;
        dst++;
    }

    oddlen = length & 1;
    length &= 0xFFFFFFFE;

    /* -------- Main Flash writing loop ( 16-bit writes ) -------- */
    for ( ; i < length ; i += 2, src += 2, dst += 2 )
    {
        /* Create the new 16-bit word.  We cannot simply use *dst b/c the MMU
         * can generate an alignment error. */
        newval = ( ( *( volatile Uint8* )( src + 1 ) ) << 8 )
               + ( *( volatile Uint8* )( src ) );

        /* Program one 16-bit word */
        FLASH_CTL555 = FLASH_CMD_AA;
        FLASH_CTL2AA = FLASH_CMD_55;
        FLASH_CTL555 = FLASH_PROGRAM;
        *( volatile Uint16* )dst     = newval;

        /* Wait for operation to complete */
        timecount = 0;
        while( ( *( volatile Uint16* )dst != newval ) && ( ++timecount < timeout ) );

        if ( timecount == timeout )
            return -1;
    }

    /* -------- Write any data leftover -------- */
    if ( oddlen )
    {
        /* Retrieve the data to write and overwrite*/
        tmpval = *( volatile Uint8* )src;
        oldval = *( volatile Uint16* )dst;
        newval = ( oldval & 0xff00 ) + tmpval;

        /* Program one 16-bit word */
        FLASH_CTL555 = FLASH_CMD_AA;
        FLASH_CTL2AA = FLASH_CMD_55;
        FLASH_CTL555 = FLASH_PROGRAM;
        *( volatile Uint16* )dst     = newval;

        /* Wait for operation to complete */
        timecount = 0;
        while( ( *( volatile Uint16* )dst != newval ) && ( ++timecount < timeout ) );

        if ( timecount == timeout )
            return -1;
    }

    /* Flash Mode: Read Array */
    *( volatile Uint16* )dst = FLASH_RESET;

    return 0;
}

⌨️ 快捷键说明

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