?? flash.c
字號:
{ FLASH_TYPE *p, *end; if (eeprom_size == 0) { cmd_stat = E_NO_FLASH; return ERR; } if (addr == NO_ADDR) { addr = FLASH_BLK4_BASE_ADDR; /* start at base address of block */ length = eeprom_size - RESERVED_AREA_SIZE; } else if (length == 0) length = 1;/* Original *//* if (is_eeprom(addr, length) != 1) *//* if (is_eeprom(addr, length) != TRUE) { cmd_stat = E_EEPROM_ADDR; return ERR; }*/ p = (FLASH_TYPE *)addr;/* Original *//* end = p + length; */ /* find first non_blank address *//* while (p != end) *//* { *//* if (*p != 0xff) *//* { *//* cmd_stat = E_EEPROM_PROG; *//* eeprom_prog_first = (ADDR)p; */ /* find last non_blank address *//* for (p = end; *--p == 0xff; ); *//* eeprom_prog_last = (ADDR)p; *//* return ERR; *//* } *//* p++; *//* } *//* return OK; */ end = (FLASH_TYPE *)FLASH_TOP_ADDR; /* search for first non blank address starting at base address of Flash Block 2 */ while (p != end) { if (*FLASH_P2V(p) != 0xff) { eeprom_prog_first = (ADDR)p; /* found first non blank memory cell */ /* now find last non blank memory cell starting from top of Flash memory */ for (p = end - 1; *FLASH_P2V(p) == 0xff; --p); eeprom_prog_last = (ADDR)p; /* found last non blank memory cell */ cmd_stat = E_EEPROM_PROG; return ERR; } p++; } return OK;}/********************************************************//* LOCK BREEZE FLASH AREA *//* *//* Lock the Flash ROM blocks which contain the Breeze *//* Development environment to prevent inadvertent *//* erasure/reprogramming. *//* *//* RETURNS: 1 = success, 0 = failure *//********************************************************/int lock_breeze(){ void *err_addr; if (flash_lock((void *)BREEZE_BLOCK_0, NUM_BREEZE_BLOCKS*FLASH_BLOCK_SIZE, (void **)&err_addr) != 0) { cmd_stat = E_EEPROM_FAIL; return (BLOCK_UNLOCKED); } return(BLOCK_LOCKED);}/********************************************************//* CHECK BLOCK STATUS *//* *//* Check the lock status of a flash block *//* *//* Input: block number to check *//* -1 = master lock */ /* *//* Returns: 1 = locked, 0 = unlocked *//* 2 = invalid block number *//********************************************************/#if 0int check_bstat(int block_num){ volatile FLASH_TYPE *lock_data_addr; FLASH_TYPE lock_data; /* shut the compiler up */ lock_data_addr = 0x00000000; /* derive the address for block lock configuration data */ if ((block_num >= 0) && (block_num <= NUM_FLASH_BLOCKS)) lock_data_addr = (FLASH_TYPE*)(FLASH_ADDR + (FLASH_BLOCK_SIZE * block_num) + 2); else if (block_num == -1) lock_data_addr = (FLASH_TYPE*)(FLASH_ADDR + 3); else return (2); lock_data_addr = FLASH_P2V(lock_data_addr); /* read block lock configuration data from address */ *lock_data_addr = READ_ID_CMD; lock_data = *lock_data_addr; /* reset flash to read mode */ *lock_data_addr = RESET_CMD; delay_and_flush(); /* wait for Flash to re-enter Read Array mode */ /* now check data to see if block is indeed locked */ if (lock_data & BLOCK_LOCKED) return (BLOCK_LOCKED); else return (BLOCK_UNLOCKED);}#endif/********************************************************//* CHECK ERASE OR UNLOCK STATUS *//* *//* Check the status of erase or unlock operation *//* using the Status Register of the Flash *//* *//* Returns: OK - Erase successful *//* VPP_LOW_DETECT - Vpp low detected *//* ERASE_UNLOCK_ERROR - Erase / Unlock error *//* CMD_SEQ_ERR - Command sequencing error */ /* UNKNOWN_ERR - Unknown error condition *//* *//********************************************************/#if 0int check_erase_unlock(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) == WSM_BUSY) { stat = *flash; } /* now check completion status */ if (stat & VPP_LOW_DETECT) { *flash = CLEAR_STAT_REG; return VPP_LOW_DETECT; } if ((stat & CMD_SEQ_ERR) == CMD_SEQ_ERR) { *flash = CLEAR_STAT_REG; return CMD_SEQ_ERR; } if (stat & ERASE_UNLOCK_ERROR) { *flash = CLEAR_STAT_REG; return ERASE_UNLOCK_ERROR; } if ((stat & ALL_FLASH_STATUS) == WSM_READY) { *flash = CLEAR_STAT_REG; return OK; } else { *flash = CLEAR_STAT_REG; return UNKNOWN_ERR; }}#endif/********************************************************//* CHECK OPERATION STATUS *//* *//* Check the status of an operation *//* using the Status Register of the Flash *//* *//* if the "cmd" argument flag is TRUE, then a *//* READ_STAT_REG command should be issued first *//* *//* Returns: *//* OK - Operation successful *//* value of Status register - Otherwise *//* *//********************************************************/#if 0int check_op_status(int cmd, volatile FLASH_TYPE *flash){ FLASH_TYPE stat; flash = FLASH_P2V(flash); if (cmd == TRUE) { *flash = READ_STAT_REG; } stat = *flash; /* poll and wait for Write State Machine Ready */ while ((stat & WSM_READY) == WSM_BUSY) { stat = *flash; } /* now check completion status */ if ((stat & ALL_FLASH_STATUS) == WSM_READY) { *flash = CLEAR_STAT_REG; return OK; } else { *flash = CLEAR_STAT_REG; return stat; }}#endif#if 0/* used for debugging only *//* check block lock configuration and display status of 64 blocks, 1=locked, 0=unlocked */void check_lock_bit_status (void){ int block; volatile FLASH_TYPE *block_addr; /* address bit A0 is not used when obtaining identifier codes *//* 11/01/00 *//* unsigned long addr = 0x2<<1; */ unsigned long addr = 0x4; unsigned char block_lock_status[64]; block_addr = (volatile FLASH_TYPE *) addr; /* printf("Checking lock status of %d blocks, 1=locked, 0=unlocked...\n", block ); */ /* address bit A0 is not used when obtaining identifier codes */ for (block=0; block<=63; block++) { *FLASH_P2V(block_addr) = READ_ID_CMD; block_lock_status[block] = *FLASH_P2V(block_addr); *FLASH_P2V(block_addr) = RESET_CMD; do_nothing();/* 11/01/00 */ do_nothing(); block_lock_status[block] &= 0x01; /* Checking lock status of block, 1=locked, 0=unlocked */ block_addr = (volatile FLASH_TYPE *)((unsigned long)block_addr + (unsigned long)FLASH_BLOCK_SIZE); /* block address offset for byte wide data storage */ } for (block=0; block<=63; block++) { if (block == 32) { printf("\n\r"); } printf("%d ", block_lock_status[block] ); } printf("\nDone!\n\n" );/** return; **/}#endif/********************************************************//* SET ALL LOCK BITS *//* *//* returns OK if successful; otherwise returns ERROR *//* and sets cmd_stat to an error code *//* The 28F640J3A is divided into 64, 128Kbyte blocks *//* This routine sets a lock bit in the block specified *//* by a given address *//********************************************************/int set_all_lock_bits(){ unsigned long addr = 0x0; void *err_addr; int stat; if ((stat = flash_lock((void *)addr, 4 * FLASH_BLOCK_SIZE, (void **)&err_addr)) != 0) { return stat; } return( OK );}/********************************************************//* CLEAR ALL LOCK BITS *//* *//* returns OK if successful; otherwise returns ERROR *//* and sets cmd_stat to an error code *//* The 28F640J3A is divided into 64, 128Kbyte blocks *//* This routine clears all block lock bits *//********************************************************/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) { /* 10/06/00 *//* Original */ /*addr = flash_addr;*/ addr = FLASH_BLK4_BASE_ADDR; length = eeprom_size - RESERVED_AREA_SIZE; } /* Original */ /* check for reserved area if one is used */ /* check to see if the address is within the reserved area *//* if (reserved_check(addr, length) == TRUE) { cmd_stat = E_EEPROM_ADDR; return ERR; }*/ 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; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -