📄 flash_strata.c
字号:
int len ; UINT8 *psrc ; UINT8 *pdst ; /* set addresses */ psrc = (UINT8*) p_param->buffer ; pdst = (UINT8*) p_param->adr ; len = p_param->length ; /* call our speedy memcpy */ memcpy( pdst, psrc, len ) ; return(OK) ;}/************************************************************************ * * FLASH_STRATA_program_flash * Description : * ------------- * Programs a Intel 28Fxxx-compliant flash device. * * * Parameters : * ------------ * * 'p_param', IN, variable of type, t_FLASH_write_descriptor. * * * Return values : * --------------- * * 'OK' = 0x00: FLASH programmed succesfully * ERROR_FLASH_PROGRAM_ERROR Flash device failure * * ************************************************************************/staticINT32 FLASH_STRATA_program_flash( t_FLASH_write_descriptor *p_param ){ int rcode = OK ; t_flash_access dw ; /* destination words */ int i, j, wc, len, rest ; UINT8 *psrc ; UINT32 pdst ; /* convert addresses to kseg1 */ psrc = p_param->buffer ; pdst = KSEG1(p_param->adr) ; len = p_param->length ; /* check any special case */ if (len <= 0) { return(OK) ; } /* test for initial alignment */ rest = pdst & 0x3 ; if (rest) { /* Need for initial word alignment */ /* read destination word */ pdst &= ~0x3; dw.d32 = REG32(pdst) ; /* calculate upper byte boundary inside word */ j = 4 ; if (len < (4-rest)) { j = rest + len ; } /* merge rest into last dest. word */ for (i=rest; i<j; i++) { /* copy source bytes into last word */ dw.d8[i] = REG8(psrc) ; psrc++ ; len-- ; } /* issue 'CLEAR STATUS: SR-5,4,3,1' command */ REG32(pdst) = FLASH_CLEAR_STATUS_COMMAND ; /* program this word */ REG32(pdst) = FLASH_WRITE_WORD_COMMAND ; REG32(pdst) = dw.d32 ; /* await completion */ rcode = FLASH_STRATA_wait_ready( pdst, FLASH_RETRY_10); if (rcode) { return(rcode) ; } /* Verify programmed word */ REG32(pdst) = FLASH_READ_COMMAND ; if ( REG32(pdst) != dw.d32 ) { memset( flash_diag_msg, 0, sizeof(flash_diag_msg) ) ; sprintf( flash_diag_msg, "Data check read: (0x%08x)=0x%08x, Data written: 0x%08x", (UINT32)pdst, REG32(pdst), dw.d32) ; return(ERROR_FLASH_VERIFY_ERROR) ; } /* adjust destination pointer */ pdst += 4 ; } /* calculate words to program and some rest of bytes */ wc = len / 4 ; rest = len % 4 ; /* now, destination is word aligned, check source */ /* issue 'CLEAR STATUS: SR-5,4,3,1' command */ REG32(pdst) = FLASH_CLEAR_STATUS_COMMAND ; if ((UINT32)psrc & 0x3) { /* destination only is word aligned */ /* program word by word */ for (i=0; i<wc; i++) { /* copy source bytes into dest. word */ for (j=0; j<4; j++) { dw.d8[j] = REG8(psrc) ; psrc++ ; } /* program this word */ REG32(pdst) = FLASH_WRITE_WORD_COMMAND ; REG32(pdst) = dw.d32 ; /* await completion */ rcode = FLASH_STRATA_wait_ready( pdst, FLASH_RETRY_10); if (rcode != OK) { return(rcode) ; } /* Verify programmed word */ REG32(pdst) = FLASH_READ_COMMAND ; if ( REG32(pdst) != dw.d32 ) { memset( flash_diag_msg, 0, sizeof(flash_diag_msg) ) ; sprintf( flash_diag_msg, "Data check read: (0x%08x)=0x%08x, Data written: 0x%08x", (UINT32)pdst, REG32(pdst), dw.d32) ; return(ERROR_FLASH_VERIFY_ERROR) ; } /* next destination word */ pdst += 4 ; } } else { /* both source and destination is word aligned */ /* program word by word */ for (i=0; i<wc; i++) { /* program this word */ dw.d32 = REG32(psrc) ; /* Source may be a FLASH device */ REG32(pdst) = FLASH_WRITE_WORD_COMMAND ; REG32(pdst) = dw.d32 ; /* await completion */ rcode = FLASH_STRATA_wait_ready( pdst, FLASH_RETRY_10); if (rcode != OK) { return(rcode) ; } /* Verify programmed word */ REG32(pdst) = FLASH_READ_COMMAND ; if ( REG32(pdst) != dw.d32 ) { memset( flash_diag_msg, 0, sizeof(flash_diag_msg) ) ; sprintf( flash_diag_msg, "Data check read: (0x%08x)=0x%08x, Data written: 0x%08x", (UINT32)pdst, REG32(pdst), dw.d32) ; return(ERROR_FLASH_VERIFY_ERROR) ; } /* next destination and source word */ pdst += 4 ; psrc += 4 ; } } if (rest) { /* we still have some bytes left */ /* clear status and set flash to read mode */ /* issue 'CLEAR STATUS: SR-5,4,3,1' command */ REG32(pdst) = FLASH_CLEAR_STATUS_COMMAND ; /* issue 'READ ARRAY' command */ REG32(pdst) = FLASH_READ_COMMAND ; /* read destination word */ dw.d32 = REG32(pdst) ; /* merge rest into last dest. word */ for (i=0; i<rest; i++) { /* copy source bytes into last word */ dw.d8[i] = REG8(psrc) ; psrc++ ; } /* program this word */ REG32(pdst) = FLASH_WRITE_WORD_COMMAND ; REG32(pdst) = dw.d32 ; /* await completion */ rcode = FLASH_STRATA_wait_ready( pdst, FLASH_RETRY_10); if (rcode != OK) { return(rcode) ; } /* Verify programmed word */ REG32(pdst) = FLASH_READ_COMMAND ; if ( REG32(pdst) != dw.d32 ) { memset( flash_diag_msg, 0, sizeof(flash_diag_msg) ) ; sprintf( flash_diag_msg, "Data check read: (0x%08x)=0x%08x, Data written: 0x%08x", (UINT32)pdst, REG32(pdst), dw.d32 ) ; return(ERROR_FLASH_VERIFY_ERROR) ; } } return( rcode ) ;}/************************************************************************ * * FLASH_STRATA_set_systemflash_read * Description : * ------------- * Set system flash device in read mode. * * * Parameters : * ------------ * * - * * * Return values : * --------------- * * 'OK' = 0x00: System FLASH set to read mode * ERROR_FLASH_PROGRAM_ERROR Flash device failure * * ************************************************************************/staticINT32 FLASH_STRATA_set_systemflash_read( void ){ UINT32 bank ; volatile UINT32 *pw ; pw = (UINT32*) KSEG1(systemflash_phys_start) ; for (bank = 0; bank < systemflash_bank_count; bank++) { /* issue 'CLEAR STATUS: SR-5,4,3,1' command */ REG32(pw) = FLASH_CLEAR_STATUS_COMMAND; /* issue 'READ ARRAY' command */ REG32(pw) = FLASH_READ_COMMAND; /* next bank: */ pw = (UINT32*)((UINT32)pw + (systemflash_block_count * systemflash_block_size)) ; } return( OK ) ;}/************************************************************************ * * FLASH_STRATA_set_fileflash_read * Description : * ------------- * Set file flash device in read mode. * * * Parameters : * ------------ * * - * * * Return values : * --------------- * * 'OK' = 0x00: File FLASH set to read mode * ERROR_FLASH_PROGRAM_ERROR Flash device failure * * ************************************************************************/staticINT32 FLASH_STRATA_set_fileflash_read( void ){ volatile UINT32 *pw ; pw = (UINT32*) KSEG1(fileflash_phys_start) ; /* issue 'CLEAR STATUS: SR-5,4,3,1' command */ REG32(pw) = FLASH_CLEAR_STATUS_COMMAND; /* issue 'READ ARRAY' command */ REG32(pw) = FLASH_READ_COMMAND; return( OK ) ;}/************************************************************************ * * FLASH_STRATA_set_monitorflash_read * Description : * ------------- * Set monitor flash device in read mode. * * * Parameters : * ------------ * * - * * * Return values : * --------------- * * 'OK' = 0x00: File FLASH set to read mode * ERROR_FLASH_PROGRAM_ERROR Flash device failure * * ************************************************************************/staticINT32 FLASH_STRATA_set_monitorflash_read( void ){ volatile UINT32 *pw ; pw = (UINT32*) KSEG1(monitorflash_phys_start) ; /* issue 'CLEAR STATUS: SR-5,4,3,1' command */ REG32(pw) = FLASH_CLEAR_STATUS_COMMAND; /* issue 'READ ARRAY' command */ REG32(pw) = FLASH_READ_COMMAND; return( OK ) ;}/************************************************************************ * * FLASH_STRATA_erase_fileflash * Description : * ------------- * Erase file flash, which is the last block (in each FLASH device) * of the monitor flash. * * * Parameters : * ------------ * * - * * Return values : * --------------- * * 'OK' = 0x00: File FLASH erased succesfully * ERROR_FLASH_PROGRAM_ERROR Flash device failure * * ************************************************************************/staticINT32 FLASH_STRATA_erase_fileflash( void ){ int rcode = OK ; UINT32 pw; /* We know, this is just one block to erase */ pw = KSEG1(fileflash_phys_start) ; /* issue 'CLEAR STATUS: SR-5,4,3,1' command */ REG32(pw) = FLASH_CLEAR_STATUS_COMMAND ; /* issue 'READ_ID_CODES' command */ REG32(pw) = FLASH_READ_ID_CODES_COMMAND ; /* read status and check for LOCKED block */ if ( REG32(pw+FLASH_BLOCKSTATUS_OFS) & FLASH_BLOCKSTATUS_LOCK_MSK ) { /* issue 'clear lock-bit' command */ REG32(pw) = FLASH_CLEAR_LOCK_COMMAND ; /* issue 'confirm' command */ REG32(pw) = FLASH_CONFIRM_COMMAND; /* await completion */ rcode = FLASH_STRATA_wait_ready( pw, FLASH_RETRY_10); } if (rcode == OK) { /* issue 'erase' command */ REG32(pw) = FLASH_ERASE_COMMAND; /* issue 'confirm' command */ REG32(pw) = FLASH_CONFIRM_COMMAND; /* await completion */ rcode = FLASH_STRATA_wait_ready( pw, FLASH_RETRY_10); } /* check for hw write protect */ if (rcode == ERROR_FLASH_LOCKED) { if ( FLASH_STRATA_is_file_flash_write_protected() ) { rcode = ERROR_FLASH_FILE_FLASH_LOCK ; } } /* issue 'CLEAR STATUS: SR-5,4,3,1' command */ REG32(pw) = FLASH_CLEAR_STATUS_COMMAND;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -