This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Tool/software: Code Composer Studio
hello sir
I meet a strange problem, as following:
I erase a blank flash first, and write data into one sector of the flash successfully.
if I directly to erase the sector, it can success to erase it.
if I read data from the sector of the flash, then erase the sector, it failed to erase the sector. I didn't modiry any code about the flash handle.
about the flash handle code, please see the attachment.
/* * flash_handle.c * * Created on: 2019��12��4�� * Author: Waite */ #include <stdio.h> #include "flash_handle.h" void OSTimeDly (uint32_t ticks); /* The partition table of TMS570LC4357 */ struct Sector_List gSector_List[SECTOR_NUM] = { /* Bank ID Sector ID low Address Sector Size */ {Fapi_FlashBank0, 0, 0x00000000, 16 * 1024}, {Fapi_FlashBank0, 1, 0x00004000, 16 * 1024}, {Fapi_FlashBank0, 2, 0x00008000, 16 * 1024}, {Fapi_FlashBank0, 3, 0x0000C000, 16 * 1024}, {Fapi_FlashBank0, 4, 0x00010000, 16 * 1024}, {Fapi_FlashBank0, 5, 0x00014000, 16 * 1024}, {Fapi_FlashBank0, 6, 0x00018000, 32 * 1024}, {Fapi_FlashBank0, 7, 0x00020000, 128 * 1024}, {Fapi_FlashBank0, 8, 0x00040000, 128 * 1024}, {Fapi_FlashBank0, 9, 0x00060000, 128 * 1024}, {Fapi_FlashBank0, 10, 0x00080000, 256 * 1024}, {Fapi_FlashBank0, 11, 0x000C0000, 256 * 1024}, {Fapi_FlashBank0, 12, 0x00100000, 256 * 1024}, {Fapi_FlashBank0, 13, 0x00140000, 256 * 1024}, {Fapi_FlashBank0, 14, 0x00180000, 256 * 1024}, {Fapi_FlashBank0, 15, 0x001C0000, 256 * 1024}, {Fapi_FlashBank1, 0, 0x00200000, 128 * 1024}, {Fapi_FlashBank1, 1, 0x00220000, 128 * 1024}, {Fapi_FlashBank1, 2, 0x00240000, 128 * 1024}, {Fapi_FlashBank1, 3, 0x00260000, 128 * 1024}, {Fapi_FlashBank1, 4, 0x00280000, 128 * 1024}, {Fapi_FlashBank1, 5, 0x002A0000, 128 * 1024}, {Fapi_FlashBank1, 6, 0x002C0000, 128 * 1024}, {Fapi_FlashBank1, 7, 0x002E0000, 128 * 1024}, {Fapi_FlashBank1, 8, 0x00300000, 128 * 1024}, {Fapi_FlashBank1, 9, 0x00320000, 128 * 1024}, {Fapi_FlashBank1, 10, 0x00340000, 128 * 1024}, {Fapi_FlashBank1, 11, 0x00360000, 128 * 1024}, {Fapi_FlashBank1, 12, 0x00380000, 128 * 1024}, {Fapi_FlashBank1, 13, 0x003A0000, 128 * 1024}, {Fapi_FlashBank1, 14, 0x003C0000, 128 * 1024}, {Fapi_FlashBank1, 15, 0x003E0000, 128 * 1024}, {Fapi_FlashBank7, 0, 0xF0200000, 4 * 1024}, {Fapi_FlashBank7, 1, 0xF0201000, 4 * 1024}, {Fapi_FlashBank7, 2, 0xF0202000, 4 * 1024}, {Fapi_FlashBank7, 3, 0xF0203000, 4 * 1024}, {Fapi_FlashBank7, 4, 0xF0204000, 4 * 1024}, {Fapi_FlashBank7, 5, 0xF0205000, 4 * 1024}, {Fapi_FlashBank7, 6, 0xF0206000, 4 * 1024}, {Fapi_FlashBank7, 7, 0xF0207000, 4 * 1024}, {Fapi_FlashBank7, 8, 0xF0208000, 4 * 1024}, {Fapi_FlashBank7, 9, 0xF0209000, 4 * 1024}, {Fapi_FlashBank7, 10, 0xF020A000, 4 * 1024}, {Fapi_FlashBank7, 11, 0xF020B000, 4 * 1024}, {Fapi_FlashBank7, 12, 0xF020C000, 4 * 1024}, {Fapi_FlashBank7, 13, 0xF020D000, 4 * 1024}, {Fapi_FlashBank7, 14, 0xF020E000, 4 * 1024}, {Fapi_FlashBank7, 15, 0xF020F000, 4 * 1024}, {Fapi_FlashBank7, 16, 0xF0210000, 4 * 1024}, {Fapi_FlashBank7, 17, 0xF0211000, 4 * 1024}, {Fapi_FlashBank7, 18, 0xF0212000, 4 * 1024}, {Fapi_FlashBank7, 19, 0xF0213000, 4 * 1024}, {Fapi_FlashBank7, 20, 0xF0214000, 4 * 1024}, {Fapi_FlashBank7, 21, 0xF0215000, 4 * 1024}, {Fapi_FlashBank7, 22, 0xF0216000, 4 * 1024}, {Fapi_FlashBank7, 23, 0xF0217000, 4 * 1024}, {Fapi_FlashBank7, 24, 0xF0218000, 4 * 1024}, {Fapi_FlashBank7, 25, 0xF0219000, 4 * 1024}, {Fapi_FlashBank7, 26, 0xF021A000, 4 * 1024}, {Fapi_FlashBank7, 27, 0xF021B000, 4 * 1024}, {Fapi_FlashBank7, 28, 0xF021C000, 4 * 1024}, {Fapi_FlashBank7, 29, 0xF021D000, 4 * 1024}, {Fapi_FlashBank7, 30, 0xF021E000, 4 * 1024}, {Fapi_FlashBank7, 31, 0xF021F000, 4 * 1024}, }; void DelayMSt(void) { uint32_t count = 0; for (count = 0; count < 10000; count++) {} } uint32_t find_sector(Fapi_FlashBankType oNewFlashBank, uint32_t sector) { uint32_t i = 0; for(i = 0; i<SECTOR_NUM; i++) { if( (gSector_List[i].banks_num == oNewFlashBank) && (gSector_List[i].sector_num == sector) ) { return i; } } return (uint32_t)-1; } uint32_t find_StartSectorNum(uint32_t flashSectorAddr) { uint32_t i = 0; for(i = 0; i<SECTOR_NUM; i++) { if (flashSectorAddr >= gSector_List[i].low_addr && flashSectorAddr < gSector_List[i + 1].low_addr) { return i; } } return (uint32_t)-1; } enum e_flash_status Flash_EraseSector(uint32_t num) { Fapi_StatusType oReturnCheck = Fapi_Status_Success; oReturnCheck = Fapi_initializeFlashBanks(150); /* According to the Chip Manual*/ if((oReturnCheck == Fapi_Status_Success) && (FLASH_CONTROL_REGISTER->FmStat.FMSTAT_BITS.BUSY != Fapi_Status_FsmBusy)) { oReturnCheck = Fapi_setActiveFlashBank(gSector_List[num].banks_num); /* Set Flash Bank Activate */ if( (gSector_List[num].banks_num == Fapi_FlashBank0) || (gSector_List[num].banks_num == Fapi_FlashBank1) ) { /* Set the sectors available in the EEPROM store for erasure and programming */ oReturnCheck = Fapi_enableMainBankSectors( 1<<(gSector_List[num].sector_num) ); } else if( gSector_List[num].banks_num == Fapi_FlashBank7 ) { /* Set the sectors available in the EEPROM store for erasure and programming */ oReturnCheck = Fapi_enableEepromBankSectors(1<<(gSector_List[num].sector_num) ,0); } while(FLASH_CONTROL_REGISTER->FmStat.FMSTAT_BITS.BUSY == Fapi_Status_FsmBusy); /* Issue command to flash state machine */ oReturnCheck = Fapi_issueAsyncCommandWithAddress(Fapi_EraseSector, (uint32_t*)(gSector_List[num].low_addr)); DelayMSt(); /* Place specific example code here */ DelayMSt(); /* Wait for FSM to finish */ while(FLASH_CONTROL_REGISTER->FmStat.FMSTAT_BITS.BUSY == Fapi_Status_FsmBusy); /* Check the FSM Status to see if there were no errors */ if (FLASH_CONTROL_REGISTER->FmStat.u32Register != 0) { /* Put Error handling code here */ return flash_failure; } } return flash_succeed; } enum e_flash_status Flash_EraseBanks(Fapi_FlashBankType oNewFlashBank) { int num = 0; Fapi_StatusType oReturnCheck = Fapi_Status_Success; Fapi_FlashStatusWordType poFlashStatus; num = find_sector(oNewFlashBank, 0); /* Initialize flash bank for API operations */ oReturnCheck = Fapi_initializeFlashBanks(150); if ((oReturnCheck == Fapi_Status_Success) && (FLASH_CONTROL_REGISTER->FmStat.FMSTAT_BITS.BUSY != Fapi_Status_FsmBusy)) { /* Set Flash Bank Activate */ oReturnCheck = Fapi_setActiveFlashBank(oNewFlashBank); if ((oNewFlashBank == Fapi_FlashBank0) || (oNewFlashBank == Fapi_FlashBank1)) { oReturnCheck = Fapi_enableMainBankSectors( 0xFFFF ); } else if( oNewFlashBank == Fapi_FlashBank7 ) { oReturnCheck = Fapi_enableEepromBankSectors(0xFFFFFFFF, 0xFFFFFFFF); } while(FLASH_CONTROL_REGISTER->FmStat.FMSTAT_BITS.BUSY == Fapi_Status_FsmBusy); oReturnCheck = Fapi_issueAsyncCommandWithAddress(Fapi_EraseBank, (uint32_t*)(gSector_List[num].low_addr)); OSTimeDly(10); oReturnCheck = Fapi_doBlankCheck((uint32_t*)(gSector_List[num].low_addr), 4096*9, &poFlashStatus); OSTimeDly(10); /* Place specific example code here */ /* Wait for FSM to finish */ while(FLASH_CONTROL_REGISTER->FmStat.FMSTAT_BITS.BUSY == Fapi_Status_FsmBusy); /* Check the FSM Status to see if there were no errors */ if (FLASH_CONTROL_REGISTER->FmStat.u32Register != 0) { /* Put Error handling code here */ return flash_failure; } } return flash_succeed; } enum e_flash_status Flash_WriteData(uint32_t num, uint32_t offset, uint8_t * buff, uint32_t len) { int write_count = 0; Fapi_StatusType oReturnCheck = Fapi_Status_Success; if( (offset+len) > gSector_List[num].size ) { return flash_failure; } oReturnCheck = Fapi_initializeFlashBanks(150); if((oReturnCheck == Fapi_Status_Success) && (FLASH_CONTROL_REGISTER->FmStat.FMSTAT_BITS.BUSY != Fapi_Status_FsmBusy)) { oReturnCheck = Fapi_setActiveFlashBank(gSector_List[num].banks_num); if( (gSector_List[num].banks_num == Fapi_FlashBank0) || (gSector_List[num].banks_num == Fapi_FlashBank1) ) { oReturnCheck = Fapi_enableMainBankSectors( 0xFFFF ); } else if( gSector_List[num].banks_num == Fapi_FlashBank7 ) { oReturnCheck = Fapi_enableEepromBankSectors(0xFFFFFFFF, 0xFFFFFFFF); } while(FLASH_CONTROL_REGISTER->FmStat.FMSTAT_BITS.BUSY == Fapi_Status_FsmBusy); while (len > 0) { if (len/8 > 0) { oReturnCheck = Fapi_issueProgrammingCommand((uint32_t*)(gSector_List[num].low_addr + offset + 8 * write_count), buff + 8 * write_count, 8, 0, 0, Fapi_AutoEccGeneration); len = len - 8; } else { oReturnCheck = Fapi_issueProgrammingCommand((uint32_t*)(gSector_List[num].low_addr + offset + 8 * write_count), buff + 8 * write_count, len % 8, 0, 0, Fapi_AutoEccGeneration); len = len - len % 8; } write_count = write_count + 1; DelayMSt(); } /* Place specific example code here */ /* Wait for FSM to finish */ while(FLASH_CONTROL_REGISTER->FmStat.FMSTAT_BITS.BUSY == Fapi_Status_FsmBusy); /* Check the FSM Status to see if there were no errors */ if (FLASH_CONTROL_REGISTER->FmStat.u32Register != 0) { /* Put Error handling code here */ return flash_failure; } } return flash_succeed; } int Flash_Erase(uint32_t flashAddr, uint32_t eraseSize) { uint32_t sector_pos = 0; uint32_t sector_diff = 0; uint8_t finish_flag = 1; uint32_t tempAddr; enum e_flash_status ret_status; tempAddr = flashAddr; sector_pos = find_StartSectorNum(tempAddr); while (finish_flag) { sector_diff = gSector_List[sector_pos].size + (uint32_t)gSector_List[sector_pos].low_addr - (uint32_t)tempAddr; if (eraseSize <= sector_diff) { ret_status = Flash_EraseSector(sector_pos); if (ret_status == flash_failure) { return -1; } finish_flag = 0; } else { ret_status = Flash_EraseSector(sector_pos); eraseSize = eraseSize - sector_diff; tempAddr = tempAddr + sector_diff; sector_pos = find_StartSectorNum(tempAddr); if (ret_status == flash_failure) { return -1; } } } return 0; } int Flash_Read(uint8_t * dataBuffer, uint32_t flashAddr, uint32_t length) { uint32_t sector_pos = 0; uint32_t sector_diff = 0; uint8_t finish_flag = 1; uint32_t tempAddr; tempAddr = flashAddr; sector_pos = find_StartSectorNum(tempAddr); Fapi_initializeAPI((Fapi_FmcRegistersType *)F021_CPU0_REGISTER_ADDRESS, 150); Fapi_flushPipeline(); while (finish_flag) { sector_diff = gSector_List[sector_pos].size + (uint32_t)gSector_List[sector_pos].low_addr - (uint32_t)tempAddr; if (length <= sector_diff) { // memcpy(dataBuffer, (uint8_t *)flashAddr, length); Fapi_doMarginReadByByte( (uint8_t *)flashAddr, dataBuffer, length, Fapi_RM0); finish_flag = 0; } else { // memcpy(dataBuffer, (uint8_t *)flashAddr, sector_diff); Fapi_doMarginReadByByte( (uint8_t *)flashAddr, dataBuffer, length, Fapi_RM0); length = length - sector_diff; tempAddr = tempAddr + sector_diff; sector_pos = find_StartSectorNum(tempAddr); } } return 0; } int Flash_Write(uint8_t * dataBuffer, uint32_t flashAddr, uint32_t length) { uint32_t sector_pos = 0; uint32_t sector_diff = 0; uint8_t finish_flag = 1; uint32_t tempAddr; uint32_t sector_offset; tempAddr = flashAddr; sector_pos = find_StartSectorNum(tempAddr); while (finish_flag) { sector_offset = tempAddr - gSector_List[sector_pos].low_addr; sector_diff = gSector_List[sector_pos].size + (uint32_t)gSector_List[sector_pos].low_addr - (uint32_t)tempAddr; if (length <= sector_diff) { Flash_WriteData(sector_pos, sector_offset, dataBuffer, length); finish_flag = 0; } else { Flash_WriteData(sector_pos, sector_offset, dataBuffer, sector_diff); length = length - sector_diff; tempAddr = tempAddr + sector_diff; dataBuffer = dataBuffer + sector_diff; sector_pos = find_StartSectorNum(tempAddr); } } return 0; }
Hello Waite,
After your read operation, what is the value of the FMC status register? When you erasing the sector, what error message did you get?
hello
FLASH_CONTROL_REGISTER->FmStat.u32Register = 0
I printf the register, it is 0.
I find that, if I operate the bank7 (EEPROM), it is ok, it didn't happen the problem.
if I operate the bank0 and bank1, it will happen the problem.
Did you change the flash wait state when you read the data from the flash? Actually flash reading operation should not impact the erase/write operations (your functions)
hello sir
I resoved the problem.
I disabled the cache , then it is OK.
can you tell me the relevance between the internal FLASH and the Cache?
Or you can advice me some document?
Hello Waite,
The MPU settings don't impact the execution of F021 Flash APIs. How do you read the flash?
Can you do your test using this code:
readData[0] = *(uint32_t *)0x0200000;
readData[1] = *(uint32_t *)0x0200004;
readData[2] = *(uint32_t *)0x0200008;
readData[3] = *(uint32_t *)0x020000c;
readData[4] = *(uint32_t *)0x0200010;
readData[5] = *(uint32_t *)0x0200014;
status = Fapi_BlockErase(1, 0x0200000, 64);
Hello Waite,
The MPU settings (cacheable, WH, or WB) for flash region don't affect flash write operation using flash API. The Flash write API uses flash state machine rather than the CPU. The CPU writes data to flash wrapper register, and the flash state machine programs the state to flash address.
Reading data from flash is affected by the MPU settings. If the flash region is cacheable, if the cache hit occurs, the data in the cahce will be returned. To make sure the data is read from flash, you need to clean the cache before reading operation.