📄 flash_16x4.c
字号:
//* 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, (flash_word)0xFFFF ) == FALSE ) { //* Display Timeout and return False printf ( "Timeout while erasing\n" ) ; } //* Endif } //* EndWhile //* Return True return ( TRUE ) ;}//* End//*--------------------------------------------------------------------------------------//* 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 ){ 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 ( wait_flash_ready ( load_addr, data ) != TRUE ) { //* Display Error and Exit printf ( "Timeout while programming\n" ) ; return ( FALSE ) ; } //* Endif //* If Data written does not equal data if (( read_data = *load_addr ) != data ) { //* 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 return ( FALSE ); } //* Endif //* Return False return ( TRUE ) ;}//* End//*--------------------------------------------------------------------------------------//* Function Name : download_file_to_flash//* Object : Read data from file and write it into flash memory//* Input Parameters : <addr_base> base flash address//* <addr_load> address to load//*//* Output Parameters : TRUE or FALSE//*--------------------------------------------------------------------------------------int download_file_to_flash ( FILE *image, const FlashDef *flash, flash_word *addr_base, flash_word *addr_load )//* Begin{ unsigned short data ; flash_word *addr_prg_sector = addr_base ; int block = 0 ; int nb_sector = 0 ; int sector_id = 0 ; int first = TRUE ; int sector_found = FALSE ; int change_sector = FALSE ; int erase = FALSE ; //* For each word read from the file while ( fread ( &data, 1, 2, image ) == 2 ) { //* 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 Identifier sector_id ++ ; //* 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 ) { //* Display Error and Return False printf ( "Error : Address not found in the Flash Address Field\n" ) ; return ( FALSE ) ; } //* 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 { //* Dispaly First sector to program printf ( "First Sector to program:\t\t%d \n", sector_id ) ; //* If the first address to program is within a sector if ( addr_load > addr_prg_sector ) { //* Display Warning : first address within a sector printf ( "The first address to program is within a sector!!\n" ) ; printf ( "First Data of the sector will be lost !!!\n" ) ; //* Flag to erase the sector erase = TRUE ; } //* Else, if fisrt address equals sector address else { //* Force erasing erase = TRUE ; } //* Endif } //* 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, sector_id ) != TRUE ) { //* Return False return ( FALSE ) ; } //* Endif } //* Endif //* If new sector or first sector if (( change_sector == TRUE ) || ( first == TRUE )) { //* Display Programmaing sector printf ( "Programming Sector %d\n", sector_id ) ; } //* Endif //* Write the value read in Flash, if error if ( write_flash ( addr_base, addr_load, data ) != TRUE ) { //* Return False return ( FALSE ) ; } //* Endif //* Increment load address addr_load ++ ; //* Remove first address to program flag first = FALSE ; } //* EndWhile //* Return True return ( TRUE ) ;}//* End//*--------------------------------------------------------------------------------------//* Function Name : check_flash_from_file//* Object : Check the file downloaded in flash//* Input Parameters : <image> File//* <flash> Flash descriptor//* <addr_base> base flash address//* <addr_load> address to load//*//* Output Parameters : if data sector erase TRUE or FALSE//*--------------------------------------------------------------------------------------int check_flash_from_file ( FILE *image, const FlashDef *flash, flash_word *addr_base, flash_word *addr_load )//* Begin{ flash_word *pt = addr_load ; unsigned short data ; //* Display check in progress printf ( "Checking Flash...\n" ) ; //* Restart the input file fseek ( image, 0, SEEK_SET ) ; //* For each word read from the file while ( fread ( &data, 1, 2, image ) == 2 ) { //* If data from file is different from data in flash if ( *pt++ != data ) { //* Return False return ( FALSE ) ; } //* Endif } //* EndWhile //* Return True return ( TRUE ) ;}//* End//*--------------------------------------------------------------------------------------//* Function Name : main//* Object : Get parameter//* Input Parameters : <argc> : Load address//* : <*argv[]> : File name to download.//*//* Output Parameters : none//*--------------------------------------------------------------------------------------int main ( int argc, char *argv[] )//* Begin{ flash_word *base_addr ; flash_word *load_addr ; FILE *image ; const FlashDef *flash ; //* Display Flash Downloader Header printf ( "\n**** %s Flash Programming Utility ****\n", TARGET_ID ) ; //* If not enough arguments if (argc < MIN_NO_OF_ARGS) { //* Display Help then Return False display_help () ; return ( FALSE ) ; } //* Endif //* If Error while opening the external Flash Image file if (( image = fopen ( argv[1], "rb" )) == NULL ) { //* Display Error then exit printf ( "Error - Cannot open file image \"%s\"\n", argv[1] ) ; return ( FALSE ) ; } //* Else else { //* Display input file name printf ( "Input file is : %s \n", argv[1] ) ; } /* Get load address */ sscanf ( argv[2], "0x%x", &load_addr) ; //* Display Load Address printf ( "Load address is: %x \n", (int)load_addr ) ; //* If FLASH Device is not recognised if (( flash = flash_identify ( load_addr )) == (const FlashDef *)0 ) { //* Display Error and exit printf ( "Error - The Flash device is not recognised\n" ) ; return ( FALSE ); } //* Initialize Flash Base Address base_addr = (flash_word *) ((int)load_addr & ~(flash->flash_mask)) ; //* Display Flash Base Address printf ( "Base address is: %x \n", (int)base_addr ) ; //* Init PIO MCKO/P25 as busy ready input signal init_flash_ready () ; //* If File Download into flash is not OK if ( download_file_to_flash ( image, flash, base_addr, load_addr ) != TRUE ) { //* Display Error and exit printf ( "Error while programming Flash\n" ) ; return ( FALSE ) ; } //* If programmed flash is not checked if ( check_flash_from_file ( image, flash, base_addr, load_addr ) != TRUE ) { //* Display Error and exit printf ( "Flash not checked after programming\n" ) ; return ( FALSE ) ; } /* Close the external file and exit the program */ fclose ( image ) ; //* Display Flash written and exit printf ( "Flash written and checked\n" ) ; return ( TRUE ) ;}//* End
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -