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

📄 cflash.c

📁 数字信号处理
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Include Standard c Libraries to allow stand alone compiling and operation */
#include <stdio.h>
#include <stdlib.h>

#include "flash.h"

/* Target Identification */
#define TARGET_ID           "TMS320C3x On Board"

#define MAX_BYTES_IN_SECTOR 64*1024

unsigned char RAMSectorData[MAX_BYTES_IN_SECTOR];

/**--------------------------------------------------------------------------------------*/
/** Function Name       : flash_identify
/** Object              : Read the flash manufacturer code and Flash ID code
/** Input Parameters    : flash_byte *load_addr = Flash bass address
/** Output Parameters   : Pointer to the Flash identified
/** Functions called    : none      
/**--------------------------------------------------------------------------------------*/
const TMS320C3xDef *flash_identify ( flash_byte *load_addr )
{
    flash_byte      manuf_code ;
    flash_byte      device_code ;
    flash_byte      extra_id ;
    const TMS320C3xDef  *flash_pt ;
    flash_byte      *base_addr ;
    int             exit = FALSE ;

    /* Initialize Flash Table pointer*/
    flash_pt = FlashTable ;

    /* Look for the  device in the known flash table*/
    while ( exit == FALSE )
    {
        /* Initialize Flash Base Address*/
        base_addr = (flash_byte *) ((int)load_addr & ~(flash_pt->flash_mask)) ;
        /* Display Flash Identification Header*/
        printf ( "Trying to identify Flash at base address (0x%x)\n", (int)base_addr) ;

        /* Display Flash Tested*/
        printf ( "Trying %s\n", flash_pt->flash_name ) ;

        /* Enter Software Product Identification Mode*/
        *(base_addr + FLASH_SEQ_ADD1) = FLASH_CODE1;
        *(base_addr + FLASH_SEQ_ADD2) = FLASH_CODE2;
        *(base_addr + FLASH_SEQ_ADD1) = ID_IN_CODE;

        /* Read Manufacturer and device code from the device*/
        manuf_code  = 0xFF&(*base_addr) ;
        device_code = 0xFF&(*(base_addr + 1)) ;
/*        extra_id = *(base_addr + 3);         */

        /* Exit Software Product Identification Mode*/
        *(base_addr + FLASH_SEQ_ADD1) = FLASH_CODE1;
        *(base_addr + FLASH_SEQ_ADD2) = FLASH_CODE2;
        *(base_addr + FLASH_SEQ_ADD1) = ID_OUT_CODE;

        /* If both manufacturer and device codes corresponds  */
        if (( flash_pt->flash_id == device_code ) &&
            ( flash_pt->flash_manuf_id == manuf_code ))
        {
            /* Exit the search loop */
            exit = TRUE ;
        }
        else
        {
            /* Next Flash, If end of table */
            if ( ++flash_pt >= FlashTable + NB_FLASH_SUPPORTED )
            {
                /* Return 0, Display Error and Exit loop  */
                flash_pt = (const TMS320C3xDef *)0 ;
                printf ( "Error - Unknown device: manufacturer %02x / device %02x \n",
                        manuf_code, device_code );
                exit = TRUE ;
            }
        }
    }
    /* Return pointer to Flash found */
    return ( flash_pt ) ;
}

/**--------------------------------------------------------------------------------------*/
/** Function Name       : wait_flash_ready
/** Object              : check if data is written by software polling
/** Input Parameters    : Data and address of the data.
/** Output Parameters   : TRUE or FALSE
/** Functions called    : none
/**--------------------------------------------------------------------------------------*/
int wait_flash_ready ( flash_byte *address, flash_byte data )
{
    int i = 0 ;


    /* While two consecutive read don't give same value or timeout*/
    while (( (0x80&(*address)) != data ) && ( i++ < TIME_OUT )) ;


    /* If timeout */
    if ( i < TIME_OUT )
    {
        /*printf ( "Counter = %d\n", i ) ; */
        return ( TRUE ) ;
    }
    else
    {
        /*printf ( "Timeout\n" ) ;    */
        return ( FALSE ) ;
    }
}


/**-------------------------------------------------------------------------------------- */
/** Function Name       : check_sector_erased
/** Object              : check if sector is erased. If not erase it.
/**
/** Input Parameters    : <sector_addr> base sector address
/**                       <size> sector size in byte
/**                       <sector_id> sector ID
/**
/** Output Parameters   : If data sector erase TRUE, else FALSE
/**-------------------------------------------------------------------------------------- */
int check_sector_erased ( flash_byte *sector_addr, int size, int sector_id )
{
    int     i ;
    flash_byte  read_data ;

    /* For each word of the sector*/
    for ( i = 0 ; i < (size/2) ; i ++ )
    {
        /* Check erased value reading, if not*/
        if (( read_data = 0xFF&(*(sector_addr + i))) != (unsigned short)0xFF )
        {
            printf ( "Sector %d not erased !\n", sector_id ) ;
            printf ( "Sector %d, Address 0x%08x, Value 0x%08x\n",
                     sector_id, (int)(sector_addr + i), read_data ) ;
            return ( FALSE ) ;
        }

    }

    /* Display Sector Erased   */
    printf ( "Sector %d erased !\n", sector_id ) ;
    return ( TRUE ) ;
}

/**--------------------------------------------------------------------------------------  */
/** Function Name       : erase_sector
/** Object              : check if sector is erased if not erase
/** Input Parameters    : <base_addr> Flash base address
/**                       <sector_addr> base sector address
/**                       <size> sector size in byte
/**                       <sector_id> sector ID
/** Output Parameters   : if data sector erase TRUE or FALSE
/**--------------------------------------------------------------------------------------  */
int erase_sector ( flash_byte *base_addr, flash_byte *sector_addr, int size, int sector_id )
{
    int     trial = 0 ;
    int     happyErase = TRUE;
    
    int i=0;

    /* While flash is not erased or too much erasing performed   */
    while (( check_sector_erased ( sector_addr, size, sector_id ) == FALSE ) &&
           ( trial++ < NB_TRIAL_ERASE ))
    {
        /* Display Erasing Sector  */
        printf ( "Erasing Sector %d\n", sector_id ) ;

        /* Enter Sector Erase Sequence codes */
        *(base_addr + FLASH_SEQ_ADD1) = FLASH_CODE1;
        *(base_addr + FLASH_SEQ_ADD2) = FLASH_CODE2;
        *(base_addr + FLASH_SEQ_ADD1) = ERASE_SECTOR_CODE1;
        *(base_addr + FLASH_SEQ_ADD1) = FLASH_CODE1;
        *(base_addr + FLASH_SEQ_ADD2) = FLASH_CODE2;
        *sector_addr = ERASE_SECTOR_CODE2 ;  
/*        *(base_addr + FLASH_SEQ_ADD1) = CHIP_ERASE_CODE;  */


        /* Wait for Flash Ready after Erase, if timeout  */
        if ( wait_flash_ready ( sector_addr, (unsigned short)0x80 ) == FALSE )
        {
            /* Display Timeout and return False   */
            printf ( "Timeout while erasing\n" ) ;
            happyErase = FALSE;
            break;
        }
      
		

    }

    return (happyErase) ;
}

/**--------------------------------------------------------------------------------------*/
/** Function Name       : write_flash
/** Object              : write a word in flash
/**
/** Input Parameters    : flash_byte *base_addr : Flash base address
/**                       flash_byte *load_addr : Flash data address
/**                       flash_byte data       : Data value
/**
/** Output Parameters   : TRUE if data has been written correctly, else FALSE
/**
/** Functions called    : wait_flash_ready
/**--------------------------------------------------------------------------------------*/
int write_flash ( flash_byte *base_addr, flash_byte *load_addr, flash_byte data )
{
    flash_byte  read_data ;
    int i = 0 ;

    /* Enter Programming code sequence*/
    *(base_addr + FLASH_SEQ_ADD1) = FLASH_CODE1 ;
    *(base_addr + FLASH_SEQ_ADD2) = FLASH_CODE2 ;
    *(base_addr + FLASH_SEQ_ADD1) = WRITE_CODE ;
    *load_addr = data ;

    /* Wait for Flash ready after erasing, if timeout*/

    /* While two consecutive read don't give same value or timeout*/
    for (i = 0; ((i< TIME_OUT) && ( 0xFF&(*load_addr)) != data ); i++) {};

    /* If Data written does not equal data*/
    read_data = 0xFF&(*load_addr);
    if (( read_data != data ) || ( i == TIME_OUT )) {
        /* Display Error and return False */
        printf ( "Program Error\n" ) ;
        printf ( "Address 0x%08x / Data 0x%04x / 0x%04x \n", (int)load_addr, data, read_data ) ;
        return ( FALSE );

⌨️ 快捷键说明

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