flash.c

来自「eCos操作系统源码」· C语言 代码 · 共 574 行 · 第 1/2 页

C
574
字号
int clear_all_lock_bits(ADDR addr){    void *err_addr;    int stat;    if ((stat = flash_unlock((void *)0, eeprom_size, (void **)&err_addr)) != 0)	return stat;    return OK;}/********************************************************//* ERASE EEPROM						*//*							*//* returns OK if erase was successful,			*//* otherwise returns ERROR				*//* and sets cmd_stat to an error code			*//*							*//********************************************************/int erase_eeprom(ADDR addr, unsigned long length){    void *err_addr;    int num_blocks;    /********************************************************/    /* The 28F640J3A is divided into 64, 128Kbyte blocks    */    /* each of which must be individually erased.	    */    /* This routine and erases a whole number of blocks     */    /********************************************************/    /* don't erase boot area even if entire eeprom is specified */    if (addr == NO_ADDR) {	addr = FLASH_BLK4_BASE_ADDR;	length = eeprom_size - RESERVED_AREA_SIZE;    }    if (length == 0) {	/* 10/06/00 */	printf( "erase_eeprom, return OK, length=0\n");	return OK;    }    /* start address must be block-aligned */    if ((addr % FLASH_BLOCK_SIZE) != 0)	{	cmd_stat = E_EEPROM_ADDR;	printf( "erase_eeprom, addr = 0x%x\n", addr);	printf( "erase_eeprom, FLASH_BLOCK_SIZE = 0x%x\n", FLASH_BLOCK_SIZE);	printf( "erase_eeprom, return ERR, (addr %% FLASH_BLOCK_SIZE) = %d\n", addr % FLASH_BLOCK_SIZE);	return ERR;    }    /* figure out how many blocks require erasure - round up using integer division */    if (length % FLASH_BLOCK_SIZE)		/* non-multiple, round up */	num_blocks = (length + FLASH_BLOCK_SIZE) / FLASH_BLOCK_SIZE;    else					/* multiple number of blocks */	num_blocks = length / FLASH_BLOCK_SIZE;    if (eeprom_size == 0) {	cmd_stat = E_NO_FLASH;	return ERR;    }    if (flash_erase((void *)addr, num_blocks * FLASH_BLOCK_SIZE, (void **)&err_addr) != 0) {	cmd_stat = E_EEPROM_FAIL;	return ERR;    }    return OK;}/********************************************************//* WRITE EEPROM                				*//*                     					*//* returns OK if successful; otherwise returns ERROR    *//* and sets cmd_stat to an error code		 	*//*							*//********************************************************/intwrite_eeprom(ADDR start_addr, const void *data_arg, int data_size){    void *err_addr;    if (flash_program((void *)start_addr, data_arg, data_size, &err_addr) != 0) {	cmd_stat = E_EEPROM_FAIL;	return ERR;    }    return OK;}/******************************************************************************* flash_test - System Flash ROM diagnostics** A destructive Flash ROM Read/Write test.  Note that the area of Flash* which is tested changes based on whether the diagnostic is being invoked* from the System code or from the Factory code (can't write over MON960).** This test basically does a Longword Address test to the Flash area.**/void flash_test(MENU_ARG arg){    ADDR start_addr = (ADDR)FLASH_ADDR;	/* Original */    int i;    unsigned long *f_ptr = (unsigned long *)FLASH_ADDR;    int bytes_written = 0;    unsigned long flash_data;    char answer[20];/* 10/31/00 */    int status;    init_eeprom();    printf("***********************************\n");    printf("***           WARNING           ***\n");    printf("*** This test is destructive to ***\n");    printf("*** all contents of the FLASH!  ***\n");    printf("***********************************\n");    printf("\nDo you wish to continue? (y/n)\n");    sgets(answer);    printf("\n\n");    if ((answer[0] != 'y') && (answer[0] != 'Y'))	return;     printf ("FLASH begins at 0x%X\n", FLASH_ADDR);    printf ("Total FLASH size = 0x%X\n\n", eeprom_size);    printf ("Checking FLASH ...\n");    if (check_eeprom(NO_ADDR, 0) == OK)        printf("FLASH is erased\n\n");    else        printf("FLASH is programmed between 0x%X and 0x%X\n\n",                eeprom_prog_first, eeprom_prog_last);    printf ("\nClearing Block Lock Bits... \n");    if(clear_all_lock_bits(NO_ADDR)==OK)        printf("Done!\n\n");    else        printf("Error!\n\n");	    printf ("Erasing FLASH...\n");    if (erase_eeprom(NO_ADDR, 0) != OK)        printf("Error on erase_eeprom()\n\n");    else        printf("Done Erasing FLASH!\n\n");     (ADDR)start_addr = (ADDR)FLASH_BLK4_BASE_ADDR;    printf ("Writing Longword Data to FLASH...\n");     /* write to all of available Flash ROM.  Don't do this thousands of times       since the Flash has only 100,000 write cycles in its lifespan */    while (bytes_written < (eeprom_size - RESERVED_AREA_SIZE)) {	flash_data = (unsigned long)start_addr;    	for (i=0; i<TEST_BUF_LONGS; i++) {            flash_buffer[i] = flash_data;	/* put address in buffer */	    flash_data += 4;			/* increment address     */	}        if (write_eeprom (start_addr, (void *)flash_buffer, TEST_BUF_CHARS) != OK) {            printf("Error on write_eeprom()\n");            goto finish;        }        start_addr = (unsigned long)start_addr + TEST_BUF_CHARS;        bytes_written += TEST_BUF_CHARS;    }    printf ("Write Complete, Verifying Data...\n");    bytes_written = 0;     f_ptr = (unsigned long *)FLASH_BLK4_BASE_ADDR;        while (bytes_written < (eeprom_size - RESERVED_AREA_SIZE)) {        if (*f_ptr != (unsigned long)f_ptr) {            printf ("Data verification error at 0x%X\n", (unsigned long)f_ptr);            printf ("Expected 0x%X Got 0x%X\n", (unsigned long)f_ptr, *f_ptr);            goto finish;        }        f_ptr++;        bytes_written += 4;    }    printf ("Done Verifying Longword Data!\n\n");    printf ("Checking FLASH...\n");    if (check_eeprom(NO_ADDR, 0) == OK)        printf("FLASH is erased\n\n");    else        printf("FLASH is programmed between 0x%X and 0x%X\n\n",                eeprom_prog_first, eeprom_prog_last);    printf ("Erasing FLASH...\n");    if (erase_eeprom(NO_ADDR, 0) != OK)        printf("Error on erase_eeprom()\n\n");    else        printf("Done Erasing FLASH!\n\n");    printf ("Checking FLASH...\n");    if (check_eeprom(NO_ADDR, 0) == OK)        printf("FLASH is erased\n\n");    else        printf("FLASH is programmed between 0x%X and 0x%X\n\n",	       eeprom_prog_first, eeprom_prog_last);    /* reinitialize variables */    bytes_written = 0;     start_addr = (ADDR)FLASH_BLK4_BASE_ADDR;    f_ptr = (unsigned long *)FLASH_BLK4_BASE_ADDR;    printf ("Writing Inverted Longword Data to FLASH...\n");     /* write to all of available Flash ROM.  Don't do this thousands of times       since the Flash has only 100,000 write cycles in its lifespan */    while (bytes_written < (eeprom_size - RESERVED_AREA_SIZE)) {	flash_data = (unsigned long)start_addr;    	for (i=0; i<TEST_BUF_LONGS; i++) {            flash_buffer[i] = ~flash_data;	/* put address BAR in buffer */	    flash_data += 4;			/* increment address     */	}        if (write_eeprom (start_addr, (void *)flash_buffer, TEST_BUF_CHARS) != OK) {            printf("Error on write_eeprom()\n");            goto finish;        }        start_addr = (unsigned long)start_addr + TEST_BUF_CHARS;        bytes_written += TEST_BUF_CHARS;    }     printf ("Write Complete, Verifying Data...\n");    bytes_written = 0;     while (bytes_written < (eeprom_size - RESERVED_AREA_SIZE)) {        if (*f_ptr != (~(unsigned long)f_ptr)) {            printf ("Data verification error at 0x%X\n", (unsigned long)f_ptr);            printf ("Expected 0x%X Got 0x%X\n", (~(unsigned long)f_ptr), *f_ptr);            goto finish;        }        f_ptr++;        bytes_written += 4;    }    printf ("Done Verifying Inverted Longword Data!\n\n");    printf ("Checking FLASH...\n");    if (check_eeprom(NO_ADDR, 0) == OK)        printf("FLASH is erased\n\n");    else {        printf("FLASH is programmed between 0x%X and 0x%X\n\n",	       eeprom_prog_first, eeprom_prog_last);    }    printf ("Erasing FLASH...\n");    if (erase_eeprom(NO_ADDR, 0) != OK)        printf("Error on erase_eeprom()\n\n");    else        printf("Done Erasing FLASH!\n\n");    printf ("Checking FLASH...\n");    if (check_eeprom(NO_ADDR, 0) == OK)        printf("FLASH is erased\n\n");    else        printf("FLASH is programmed between 0x%X and 0x%X\n\n",	       eeprom_prog_first, eeprom_prog_last);/* 11/02/00 */    printf ("Setting Lock Bits for Blocks 0-3... \n");    if( (status = set_all_lock_bits() ) == OK )        printf("Done!\n");    else        printf("Error! status =0x%x\n", status);finish:    _flushICache();    printf ("\nHit <CR> to Continue...\n");    (void)hexIn();    return;}#endif // CYGPKG_IO_FLASH

⌨️ 快捷键说明

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