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

📄 cflash.c

📁 数字信号处理
💻 C
📖 第 1 页 / 共 2 页
字号:
    }
    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 TMS320C3xDef *flash, flash_byte *addr_base, flash_byte *addr_load )
{
    flash_byte  *addr_prg_sector = addr_base ;
    int         block = 0 ;
    int         nb_sector = 0 ;
    int         sector_id = 0 ;
    int         sector_found = FALSE ;
    int         dataCount = 0;
    int         kbyteCount = 0;
    int         happyProgramming = TRUE;

    unsigned char  *RAMDataPtr ;
    flash_byte      *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) ;
        /* 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;
        printf ( "Loading binary file,Please wait...\n" ) ;
        numWordsRead = fread ( RAMSectorData, 1, sectorSize, 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 += 1;
            if (dataCount%1024 == 0) {
                kbyteCount++;
                printf(".");
                printf("%d kbytes\n", kbyteCount);
            }
            if (*RAMDataPtr++ != (0xFF&(*flashDataPtr++))) {
                happyProgramming = FALSE;
                break; /* leave the while loop   */
            }
        }
        addr_load = flashDataPtr;
        printf("\n");

        if (numWordsRead < sectorSize) {
            /* 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_byte      *base_addr ;
    flash_byte      *load_addr ;
    FILE            *image ;
    const TMS320C3xDef  *flash ;
    char str[30];
    char name[256];

        /** Display Flash Downloader Header*/
        printf ( "\n**** %s Flash Programming Utility ****\n", TARGET_ID ) ;

/*                for (;;)     
                {         */
                printf("\nLoad address : \n\r");
            /* Get load address */
                gets(str);
                sscanf(str,"0x%x",&load_addr);

            printf("\n**** File to download ****\n\r");
                /* 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 ) ;
                }

                /** Display Load Address */
                printf ( "Load address is:   0x%x \n", (int)load_addr ) ;

                /* If FLASH Device is not recognized */
                if (( flash = flash_identify( load_addr )) == (const TMS320C3xDef *)0 )
                {
                    /* Display Error and exit */
                printf ( "Error - The Flash device is not recognized\n" ) ;
                            return ( FALSE ) ;
                }

                else
                {
                    /* Initialize Flash Base Address */
                    base_addr = (flash_byte *) ((int)load_addr & ~(flash->flash_mask)) ;
                    /* Display Flash Base Address */
                    printf ( "Base address is:   0x%x \n", (int)base_addr ) ;

                    /* 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" ) ;
/*                }      */
                
}

⌨️ 快捷键说明

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