📄 flash.c
字号:
/* 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;
}
/* Original */
/* If it's already erased, don't bother */
/*if (check_eeprom(addr, length) == OK)*/
/* return OK;*/
/* check_bstat(int block_num); */
if (flash_erase((void *)addr, num_blocks * FLASH_BLOCK_SIZE, (void **)&err_addr) != 0) {
cmd_stat = E_EEPROM_FAIL;
return ERR;
}
return OK;
}
/********************************************************/
/* CHECK PROGRAM OR LOCK STATUS */
/* */
/* Check the status of program or lock operations */
/* using the Status Register of the Flash */
/* */
/* Returns: OK - Write successful */
/* VPP_LOW_DETECT - Vpp low detected */
/* PROGRAM_LOCK_ERROR - Write error */
/* UNKNOWN_ERR - Unknown error condition */
/* */
/********************************************************/
#if 0
int check_program_lock(volatile FLASH_TYPE *flash)
{
FLASH_TYPE stat;
flash = FLASH_P2V(flash);
*flash = READ_STAT_REG;
stat = *flash;
/* poll and wait for Write State Machine Ready */
while (!(stat & WSM_READY))
stat = *flash;
/* now check completion status */
if (stat & VPP_LOW_DETECT)
{
*flash = CLEAR_STAT_REG;
return VPP_LOW_DETECT;
}
if (stat & PROGRAM_LOCK_ERROR)
{
*flash = CLEAR_STAT_REG;
return PROGRAM_LOCK_ERROR;
}
if ((stat & ALL_FLASH_STATUS) == (WSM_READY | PROGRAM_LOCK_SUCCESS))
{
*flash = CLEAR_STAT_REG;
return OK;
}
else
{
*flash = CLEAR_STAT_REG;
return UNKNOWN_ERR;
}
}
#endif
/********************************************************/
/* WRITE EEPROM */
/* */
/* returns OK if successful; otherwise returns ERROR */
/* and sets cmd_stat to an error code */
/* */
/********************************************************/
int
write_eeprom(ADDR start_addr, const void *data_arg, int data_size)
{
void *err_addr;
if (flash_program(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(void)
{
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;
#if 0
printf ("Disabling Instruction Cache... ");
_disableICache(); /* disable instruction cache */
printf ("Done\n\n");
/* switch the MMU over to use RAM-based page table entries */
printf ("Switching MMU to RAM-based page table... ");
_switchMMUpageTables();
printf ("Done\n\n");
#endif
/* 10/31/00 */
/*
check_lock_bit_status();
printf ("Setting Block Lock Bits... \n");
printf("Do you wish to continue? (y/n)\n");
sgets(answer);
printf("\n");
if ((answer[0] != 'y') && (answer[0] != 'Y'))
return;
if( (status = set_all_lock_bits() ) == OK )
printf("Done!\n");
else
{
printf("Error!\n");
printf( "error status = 0x%x\n", status );
}
check_lock_bit_status();
printf ("\nClearing Block Lock Bits... \n");
printf("Do you wish to continue? (y/n)\n");
sgets(answer);
printf("\n");
if ((answer[0] != 'y') && (answer[0] != 'Y'))
return;
if( ( status=clear_all_lock_bits(NO_ADDR) ) == OK )
printf("Done!\n");
else
{
printf("Error!\n");
printf( "error status = 0x%x\n", status );
}
check_lock_bit_status();
*/
/* 10/31/00 */
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");
/* check_lock_bit_status(); */
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)flash_addr = FLASH_BLK4_BASE_ADDR;
(ADDR)start_addr = (ADDR)flash_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;
(ADDR)flash_addr = FLASH_BLK4_BASE_ADDR;
f_ptr = (unsigned long *)flash_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;
(ADDR)flash_addr = FLASH_BLK4_BASE_ADDR;
start_addr = (ADDR)flash_addr;
f_ptr = (unsigned long *)flash_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!\n");
printf( "error status = 0x%x\n", status );
// check_lock_bit_status();
}
/*
printf ("Setting Lock Bits for Blocks 0-3... \n");
if(set_all_lock_bits()==OK)
{
printf("Done!\n\n");
}
else
{
printf("Error!\n\n");
check_lock_bit_status();
}
*/
/* 11/02/00 */
finish:
_flushICache();
#if 0
_enableICache();
#endif
printf ("\nHit <CR> to Continue...\n");
(void)hexIn();
return;
}
#endif // CYGPKG_IO_FLASH
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -