lib_flash_at49.c

来自「使用JTAG口对AT91R40008芯片进行FLASH编程的程序」· C语言 代码 · 共 781 行 · 第 1/2 页

C
781
字号
//*----------------------------------------------------------------------------int flash_at49_check_sector_erased ( flash_word *sector_addr,int size)//* Begin{    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)) != (flash_word)0xFFFF )        {            return ( FALSE ) ;        }        //* Endif    }    //* Endfor    //* Return True    return ( TRUE ) ;}//* End//*----------------------------------------------------------------------------//* Function Name       : flash_at49_check_flash_erased//* Object              : check if all sector are erased//* Input Parameters    : <flash> Flash descriptor//*                       <sector_addr> base sector address//* Output Parameters   : if data sector erase TRUE or FALSE//*----------------------------------------------------------------------------/*int flash_at49_check_flash_erased ( FlashAt49BVDef *flash, flash_word *addr_base )//* Begin{    int         i ;    flash_word  read_data ;    int         size = flash->flash_size ;    //* For each word of the flash    for ( i = 0 ; i < (size/2) ; i ++ )    {        //* Check erased value reading, if not        if (( read_data = *(addr_base + i)) != (flash_word)0xFFFF )        {            //*  return False            return ( FALSE ) ;        }        //* Endif    }    //* Endfor    //* Return True    return ( TRUE ) ;}*///* End//*----------------------------------------------------------------------------//* Function Name       : flash_at49_erase_flash//* Object              : ERASE FLASH//* Input Parameters    : <flash> Flash descriptor//*                       <base_address> base sector address//* Output Parameters   : if data sector erase TRUE or FALSE//*----------------------------------------------------------------------------/*int flash_at49_erase_flash ( FlashAt49BVDef *flash,flash_word *base_addr )//* Begin{    int     trial = 0;    int     cmpt;    int     status= TRUE;    //* While flash is not erased or too much erasing performed    while (( flash_at49_check_flash_erased ( flash, base_addr ) == FALSE ) &&           ( trial++ < NB_TRIAL_ERASE ))    {        //* 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;        *(base_addr + FLASH_SEQ_ADD1) = CHIP_ERASE_CODE;        //* wait        for ( cmpt = 0 ; cmpt < DELAY_ERASE ; cmpt ++ ) ;        //* Wait for Flash Ready after Erase, if timeout        if ( flash_wait_flash_ready ( base_addr,0xFFFF ) == FALSE )        {            //* return False            if ( trial==NB_TRIAL_ERASE) return (FALSE) ;        }        //* Endif    }    //* EndWhile    //* Return True    return ( status ) ;}*///* End//*----------------------------------------------------------------------------//* Function Name       : flash_at49_erase_sector//* Object              : erase flash sector//* Input Parameters    : <base_addr> Flash base address//*                       <sector_addr> Flash sector address//* Output Parameters   : none//*----------------------------------------------------------------------------int  flash_at49_erase_sector ( flash_word *base_addr,flash_word *sector_addr)//* Begin{	u_int i;    //* 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;    for(i=0;i<5;i++); //* Wait a minimum of 150 ns      *sector_addr = ERASE_SECTOR_CODE2 ;    //* Wait for Flash Ready after Erase, if timeout    if ( flash_wait_flash_ready ( sector_addr, (flash_word)0xFFFF ) == FALSE )    {          return(FALSE);    }    return(TRUE);}//* End//*----------------------------------------------------------------------------//* Function Name       : flash_at49_write_flash//* Object              : Write short in Flash//* Input Parameters    ://* Output Parameters   : none//*----------------------------------------------------------------------------int flash_at49_write_flash (flash_word *base_addr,flash_word *load_addr,flash_word data){    flash_word  read_data ;    //* 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    if ( flash_wait_flash_ready ( load_addr, data ) != TRUE )    {        return ( FALSE ) ;    }    //* Endif    //* If Data written does not equal data    if (( read_data = *load_addr ) != data )    {        //* Return False        return ( FALSE );    }    //* Endif    //* Return False    return ( TRUE ) ;}//*----------------------------------------------------------------------------//* Function Name       : flash_at49_erase_write_block//* Object              : check if sector is erased if not erase erase and write//* Input Parameters    : <buffer> data block addressFlash//*                       <size> sector size in byte//* Output Parameters   : if data sector erase TRUE or FALSE//*----------------------------------------------------------------------------//* int flash_at49_erase_write_block ( u_char *buffer,int size)//* Beginint main(){    u_char		data;    u_int   	count, read;    int         sector_found;    int         change_sector;    u_int 			buffer;    int 			size;	led_on(0, LED_3);	led_on(0, LED_4);	led_on(0, LED_5);	led_on(0, LED_6);	led_on(0, LED_16);	led_on(0, LED_17);	led_on(0, LED_18);	led_on(0, LED_19);	/*	for (count = 0; count<17225; count++){			if ((count%2)==0){			led_on(1, LED_3);		} else {			led_on(0, LED_3);		}				while( ((read_dbg_control() & R_BIT) != R_BIT) ); //* Wait a new data to read		tab[count] = read_dbg_data();  //* Get address where to store data				if ((count%2)==0){			led_on(1, LED_4);		} else {			led_on(0, LED_4);		}			}*/			while( ((read_dbg_control() & R_BIT) != R_BIT) ); //* Wait a new data to read	addr_load = (flash_word *)read_dbg_data();  //* Get address where to store data		//*led_on(1, LED_3);	    while( ((read_dbg_control() & R_BIT) != R_BIT) ); //* Wait a new data to read	flash = Get_Flash_Type( read_dbg_data() ); //* Get flash type		//*led_on(1, LED_4);	while( ((read_dbg_control() & R_BIT) != R_BIT) ); //* Wait a new data to read	size = read_dbg_data(); 	//* Get size of data to load								//* Size in bytes		read = TRUE;    //* For each word read from the file    for  ( count =0 ; count < size ; count +=2 )    {	    if (read == TRUE){	    		    	while( ((read_dbg_control() & R_BIT) != R_BIT) ); //* Wait a new data to read	    	buffer = read_dbg_data(); //* size buffer = 32 bits	    	read = FALSE;	    	//*led_on(1, LED_5);	    } else {			buffer = buffer>>16;	    	read = TRUE;	    	//*led_on(0, LED_5);	    }	    //*data = buffer[count];        //*data = (unsigned short) buffer[count] | (unsigned short)buffer[count+1] << 8 ;        data = (flash_word)buffer; //* size data = 16 bits        //* Clear sector found flag        sector_found = FALSE ;        //* Clear Sector change flag        change_sector = FALSE ;        //* While sector not found        while ( sector_found == FALSE )        {            //* If program address lower than current sector address + its size            if (( addr_prg_sector + (flash->flash_org[block].sector_size/2))                > addr_load )            {                //* Flag sector found                sector_found = TRUE ;            }            //* Else            else            {                //* Flag sector change                change_sector = TRUE ;                //* Add current sector size to program address                addr_prg_sector += (flash->flash_org[block].sector_size/2) ;                //* Increment the sector number                nb_sector++ ;                //* If last sector in block tested                if ( nb_sector >= flash->flash_org[block].sector_number )                {                    //* Re-initialize sector number in block                    nb_sector = 0 ;                    //* Increment block number                    block ++ ;                    //* If last block tested                    if ( block >= flash->flash_block_nb )                    {                        //*  Error Address not found in the Flash Address Field Return False                        //* led_on(1, LED_8);                    }                    //* Endif                }                //* Endif            }            //* EndIf        }        //* EndWhile        //* Unflag Erasing        erase = FALSE ;        //* If new sector or first sector        if (( change_sector == TRUE ) || ( first == TRUE ))        {            //* If not first sector            if ( first == FALSE )            {                //* Flag Erasing                erase = TRUE ;            }            //* Else, if first sector            else            {                //* Flag to erase the sector                erase = TRUE ;            }            //* Endif        }        //* Endif        //* If Erasing flagged        if ( erase == TRUE )        {            //* Erase, if Timeout            if ( erase_sector ( addr_base,                                addr_prg_sector,                                flash->flash_org[block].sector_size) != TRUE )            {                //* Return False                //* led_on(1, LED_7);            }            //* Endif        }        //* Endif        //* Write the value read in Flash, if error        if ( flash_at49_write_flash ( addr_base,addr_load, data )!= TRUE )        {            //* Return False            //* led_on(1, LED_6);        }        //* Endif        //* Increment load address        addr_load ++ ;        //* Remove first address to program flag        first = FALSE ;    }    //* EndFor	//* Send the data	while( ((read_dbg_control() & W_BIT) == W_BIT) ); //* Wait a new data to write	write_dbg_data(0xCAFECAFE);	while(1){			led_on(1, LED_5);		for (size=0; size<300000;size++);		led_on(0, LED_5);		for (size=0; size<300000;size++);			}	//*	led_on(1, LED_5);	//*	for (size=0; size<300000;size++);	//*	led_on(0, LED_5);	//*	for (size=0; size<300000;size++);		    //* Return True    //  return ( TRUE ) ;}//* End

⌨️ 快捷键说明

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