📄 flash_8011_ghs.c
字号:
{ 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 ) { 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 ) ; // 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" ) ; 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 ){ flash_word 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) && ( *load_addr != data ); i++) {}; // If Data written does not equal data read_data = *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 ); } else { return ( TRUE ) ; }}//*--------------------------------------------------------------------------------------//* 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 ){ 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 ; int dataCount = 0; int kbyteCount = 0; int happyProgramming = TRUE; RAM_flash_word *RAMDataPtr ; flash_word *flashDataPtr; //int sectorNum; unsigned int sectorSize; unsigned int numWordsRead; unsigned int wordCount; // Find the starting flash sector sector_found = FALSE ; while ( sector_found == FALSE ) { // compute the end address + 1 of the current sector addr_prg_sector += (flash->flash_org[block].sector_size/2) ; // If program address lower than this, we start programming in this sector if (addr_prg_sector > addr_load ) { // Display 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" ) ; } sector_found = TRUE ; break; // leave the while loop } else { // 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 : Programming address is not within the Flash device's address space\n" ) ; happyProgramming = FALSE; break; // leave the while loop } } //if ( nb_sector... } //if (( addr_prg_sector } //while ( sector_found /* Loop over each sector within each block, erase it, then program it */ while (happyProgramming == TRUE) { // Erase the current Sector printf ( "Programming Sector %d\n", sector_id ) ; if ( erase_sector( addr_base, addr_load, flash->flash_org[block].sector_size, sector_id ) != TRUE ) { happyProgramming = FALSE; break; // while (happyProgramming... } // read a whole sector from the file into RAM sectorSize = flash->flash_org[block].sector_size; numWordsRead = fread ( RAMSectorData, 2, sectorSize/2, image ); RAMDataPtr = RAMSectorData; flashDataPtr = addr_load ; for (wordCount = 0; wordCount < numWordsRead; wordCount ++) { if ( write_flash ( addr_base, flashDataPtr, *RAMDataPtr ) != TRUE ) { happyProgramming = FALSE; break; } flashDataPtr++; RAMDataPtr++; } /* Verify the flash sector we just wrote with the RAM data */ RAMDataPtr = RAMSectorData; flashDataPtr = addr_load ; for (wordCount = 0; wordCount < numWordsRead; wordCount ++) { // print a dot for the user dataCount += 2; if (dataCount%1024 == 0) { kbyteCount++; printf("%d kbytes\n", kbyteCount); printf("."); } if (*RAMDataPtr++ != *flashDataPtr++) { happyProgramming = FALSE; break; // leave the while loop } } addr_load = flashDataPtr; printf("\n"); if (numWordsRead < sectorSize/2) { // There is no more data in the file break; // leave the while loop } // 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 : Binary file too large for flash device\n" ) ; happyProgramming = FALSE; break; // leave the while loop } } //if ( nb_sector... } // while (happyProgramm... return ( happyProgramming ) ;}//*--------------------------------------------------------------------------------------//* Function Name : main//* Object : Get parameter//* Input Parameters : none//* Output Parameters : none//*--------------------------------------------------------------------------------------int main ( int argc, char *argv[] ){ flash_word *base_addr ; flash_word *load_addr ; FILE *image ; const FlashDef *flash ; char str[30]; char name[256]; //* Display Flash Downloader Header printf ( "\n**** %s Flash Programming Utility ****\n", TARGET_ID ) ; printf("\n**** Load address ****\n"); /* Get load address */ gets(str); sscanf(str,"0x%x",&load_addr); printf("\n**** File to download ****\n"); /* Get File */ gets(name); //* If Error while opening the external Flash Image file if (( image = fopen ( name, "rb" )) == NULL ) { //* Display Error then exit printf ( "Error - Cannot open file image \"%s\"\n", name ) ; return ( FALSE ) ; } //* Else else { //* Display input file name printf ( "Input file is : %s \n", name ) ; } /* Get load address */ sscanf ( str, "0x%x", &load_addr) ; //* Display Load Address printf ( "Load address is: %x \n", (int)load_addr ) ; // If FLASH Device is not recognized if (( flash = flash_identify( load_addr )) == (const FlashDef *)0 ) { // Display Error and exit printf ( "Error - The Flash device is not recognized\n" ) ; return ( FALSE ) ; } else { // 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 ) ; } } /* Close the external file and exit the program */ fclose ( image ) ; // Display Flash written and exit printf ( "Flash written and verified\n" ) ; return ( TRUE ) ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -