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

📄 flash_ads_semi.c

📁 本资料说明如何采用Multi-ICE 和ADS通过JTAG口在线烧写FLASH 1
💻 C
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************
* FILE NAME                                     VERSION                
*                                                                      
*        29LV160.C               		            1.0              
*                                                                      
* COMPONENT                                                            
*                                                                      
*        Flash Operation such as erase/program 
*        Using semihosting method                                  
*                                                                      
* DESCRIPTION                                                          
*                                                                      
*      	  
*                                                                      
* DATA STRUCTURES                                                      
*                                                                      
*               
*                                                                      
* FUNCTIONS                                                            
*
*        
*                                                                      
* DEPENDENCIES                                                         
*        
*       
* HISTORY
*	
*
*                                                               
************************************************************************/

/* Include Standard c Libraries to allow stand alone compiling and operation */
#include <stdio.h>
#include <stdlib.h>

#include "flash_29lv.h"

/* Target Identification */
#define TARGET_ID           "S3C44B0X DEV BOARD"

#define MAX_WORDS_IN_SECTOR 32*1024

unsigned short RAMSectorData[MAX_WORDS_IN_SECTOR];

//*--------------------------------------------------------------------------------------
//* Function Name       : flash_identify
//* Object              : Read the flash manufacturer code and Flash ID code
//* Input Parameters    : flash_word *load_addr = Flash bass address
//* Output Parameters   : Pointer to the Flash identified
//* Functions called    : none
//*--------------------------------------------------------------------------------------
const FlashDef *flash_identify ( flash_word *load_addr )
{
    flash_word      manuf_code ;
    flash_word      device_code ;
    flash_word      extra_id ;
    const FlashDef  *flash_pt ;
    flash_word      *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_word *) ((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 ) ;
        
        // Reset Flash
        *(base_addr) = FLASH_RESET_CODE;
		
        // 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  = *base_addr ;
        device_code = *(base_addr + 1) ;
        //extra_id = *(base_addr + 3);
		
        // If both manufacturer and device codes corresponds
        if (( flash_pt->flash_id == device_code ) &&
            ( flash_pt->flash_manuf_id == manuf_code ))//&&
            //( flash_pt->flash_extra_id == extra_id ))
        {
            // 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 FlashDef *)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       : init_flash_ready
//* Object              : Setup the PIO line connected to the RDY/BUSY line of the flash
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : none
//*--------------------------------------------------------------------------------------
void init_flash_ready ( void )
{
#ifdef POLL_RDY_NBUSY
    *(( volatile unsigned int *) 0xFFFF0000 ) = RDY_PIO_BUSY ;
    *(( volatile unsigned int *) 0xFFFF0014 ) = RDY_PIO_BUSY ;
#endif
}

//*--------------------------------------------------------------------------------------
//* 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_word *address, flash_word data )
{
    int i = 0 ;
	
#ifdef POLL_RDY_NBUSY
    // While RDY/BUSY is not set
    while (((*(( volatile unsigned int *) 0xFFFF0024 ) & RDY_PIO_BUSY ) == 0 ) &&
		( i++ < TIME_OUT )) ;
#else
    // While two consecutive read don't give same value or timeout
    //while (( *address != data ) && ( i++ < TIME_OUT )) ;
    //while (( (*address&0x80) != 0x80 ) && ( i++ < TIME_OUT )) ;
    while ( (*address&0x80) != 0x80 ) ;
#endif
	
    // 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_word *sector_addr, int size, int sector_id )
{
    int     i ;
    flash_word  read_data ;
	
    // For each word of the sector
    for ( i = 0 ; i < (size/2) ; i ++ )
    {
        // Check erased value reading, if not
        if (( read_data = *(sector_addr + i)) != (unsigned short)0xFFFF )
        {
            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_word *base_addr, flash_word *sector_addr, int size, int sector_id )
{
    int     trial = 0 ;
    int     happyErase = TRUE;
	
    // 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 ) ;
		
		// Reset Flash
		*base_addr = FLASH_RESET_CODE;
		
        // 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 ;
		
        // Wait for Flash Ready after Erase, if timeout
        if ( wait_flash_ready ( sector_addr, (unsigned short)0xFFFF ) == 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_word *base_addr : Flash base address
//*                       flash_word *load_addr : Flash data address
//*                       flash_word data       : Data value
//*
//* Output Parameters   : TRUE if data has been written correctly, else FALSE
//*
//* Functions called    : wait_flash_ready
//*--------------------------------------------------------------------------------------
int write_flash ( flash_word *base_addr, flash_word *load_addr, flash_word data )

⌨️ 快捷键说明

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